Skip to content

Commit 162e006

Browse files
heicarstMartin Schwidefsky
authored andcommitted
[S390] Mark kernel text section read-only.
Set read-only flag in the page table entries for the kernel image text section. This will catch all instruction caused corruptions withing the text section. Instruction replacement via kprobes still works, since it bypasses now dynamic address translation. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent ab14de6 commit 162e006

5 files changed

Lines changed: 31 additions & 11 deletions

File tree

‎arch/s390/kernel/early.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#define DEFSYS_CMD_SIZE 96
2828
#define SAVESYS_CMD_SIZE 32
2929

30-
extern int _eshared;
3130
char kernel_nss_name[NSS_NAME_SIZE + 1];
3231

3332
#ifdef CONFIG_SHARED_KERNEL

‎arch/s390/kernel/kprobes.c‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,34 @@ void __kprobes get_instruction_type(struct arch_specific_insn *ainsn)
155155
static int __kprobes swap_instruction(void *aref)
156156
{
157157
struct ins_replace_args *args = aref;
158+
u32 *addr;
159+
u32 instr;
158160
int err = -EFAULT;
159161

162+
/*
163+
* Text segment is read-only, hence we use stura to bypass dynamic
164+
* address translation to exchange the instruction. Since stura
165+
* always operates on four bytes, but we only want to exchange two
166+
* bytes do some calculations to get things right. In addition we
167+
* shall not cross any page boundaries (vmalloc area!) when writing
168+
* the new instruction.
169+
*/
170+
addr = (u32 *)ALIGN((unsigned long)args->ptr, 4);
171+
if ((unsigned long)args->ptr & 2)
172+
instr = ((*addr) & 0xffff0000) | args->new;
173+
else
174+
instr = ((*addr) & 0x0000ffff) | args->new << 16;
175+
160176
asm volatile(
161-
"0: mvc 0(2,%2),0(%3)\n"
162-
"1: la %0,0\n"
177+
" lra %1,0(%1)\n"
178+
"0: stura %2,%1\n"
179+
"1: la %0,0\n"
163180
"2:\n"
164181
EX_TABLE(0b,2b)
165-
: "+d" (err), "=m" (*args->ptr)
166-
: "a" (args->ptr), "a" (&args->new), "m" (args->new));
182+
: "+d" (err)
183+
: "a" (addr), "d" (instr)
184+
: "memory", "cc");
185+
167186
return err;
168187
}
169188

‎arch/s390/kernel/vmlinux.lds.S‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ SECTIONS
3535

3636
#ifdef CONFIG_SHARED_KERNEL
3737
. = ALIGN(1048576); /* VM shared segments are 1MB aligned */
38+
#endif
3839

40+
. = ALIGN(4096);
3941
_eshared = .; /* End of shareable data */
40-
#endif
4142

4243
. = ALIGN(16); /* Exception table */
4344
__start___ex_table = .;

‎arch/s390/mm/init.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <linux/pfn.h>
2727
#include <linux/poison.h>
2828
#include <linux/initrd.h>
29-
3029
#include <asm/processor.h>
3130
#include <asm/system.h>
3231
#include <asm/uaccess.h>
@@ -96,8 +95,8 @@ static void __init setup_ro_region(void)
9695
pte_t new_pte;
9796
unsigned long address, end;
9897

99-
address = ((unsigned long)&__start_rodata) & PAGE_MASK;
100-
end = PFN_ALIGN((unsigned long)&__end_rodata);
98+
address = ((unsigned long)&_stext) & PAGE_MASK;
99+
end = PFN_ALIGN((unsigned long)&_eshared);
101100

102101
for (; address < end; address += PAGE_SIZE) {
103102
pgd = pgd_offset_k(address);
@@ -173,8 +172,8 @@ void __init mem_init(void)
173172
datasize >>10,
174173
initsize >> 10);
175174
printk("Write protected kernel read-only data: %#lx - %#lx\n",
176-
(unsigned long)&__start_rodata,
177-
PFN_ALIGN((unsigned long)&__end_rodata) - 1);
175+
(unsigned long)&_stext,
176+
PFN_ALIGN((unsigned long)&_eshared) - 1);
178177
}
179178

180179
void free_initmem(void)

‎include/asm-s390/sections.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33

44
#include <asm-generic/sections.h>
55

6+
extern char _eshared[];
7+
68
#endif

0 commit comments

Comments
 (0)