Skip to content

Commit b2d24b9

Browse files
gerald-schaeferMartin Schwidefsky
authored andcommitted
s390/kernel: add support for kernel address space layout randomization (KASLR)
This patch adds support for relocating the kernel to a random address. The random kernel offset is obtained from cpacf, using either TRNG, PRNO, or KMC_PRNG, depending on supported MSA level. KERNELOFFSET is added to vmcoreinfo, for crash --kaslr support. Signed-off-by: Gerald Schaefer <gerald.schaefer@de.ibm.com> Reviewed-by: Philipp Rudo <prudo@linux.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent a80313f commit b2d24b9

9 files changed

Lines changed: 207 additions & 5 deletions

File tree

‎arch/s390/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,16 @@ config RELOCATABLE
637637
The relocations make the kernel image about 15% larger (compressed
638638
10%), but are discarded at runtime.
639639

640+
config RANDOMIZE_BASE
641+
bool "Randomize the address of the kernel image (KASLR)"
642+
depends on RELOCATABLE
643+
default y
644+
help
645+
In support of Kernel Address Space Layout Randomization (KASLR),
646+
this randomizes the address at which the kernel image is loaded,
647+
as a security feature that deters exploit attempts relying on
648+
knowledge of the location of kernel internals.
649+
640650
endmenu
641651

642652
menu "Memory setup"

‎arch/s390/boot/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ obj-y += string.o ebcdic.o sclp_early_core.o mem.o ipl_vmparm.o cmdline.o
3333
obj-y += ctype.o text_dma.o
3434
obj-$(CONFIG_PROTECTED_VIRTUALIZATION_GUEST) += uv.o
3535
obj-$(CONFIG_RELOCATABLE) += machine_kexec_reloc.o
36+
obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
3637
targets := bzImage startup.a section_cmp.boot.data section_cmp.boot.preserved.data $(obj-y)
3738
subdir- := compressed
3839

‎arch/s390/boot/boot.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ void setup_boot_command_line(void);
99
void parse_boot_command_line(void);
1010
void setup_memory_end(void);
1111
void print_missing_facilities(void);
12+
unsigned long get_random_base(unsigned long safe_addr);
13+
14+
extern int kaslr_enabled;
1215

1316
unsigned long read_ipl_report(unsigned long safe_offset);
1417

‎arch/s390/boot/ipl_parm.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ unsigned long __bootdata(memory_end);
1818
int __bootdata(memory_end_set);
1919
int __bootdata(noexec_disabled);
2020

21+
int kaslr_enabled __section(.data);
22+
2123
static inline int __diag308(unsigned long subcode, void *addr)
2224
{
2325
register unsigned long _addr asm("0") = (unsigned long)addr;
@@ -214,6 +216,7 @@ void parse_boot_command_line(void)
214216
char *args;
215217
int rc;
216218

219+
kaslr_enabled = IS_ENABLED(CONFIG_RANDOMIZE_BASE);
217220
args = strcpy(command_line_buf, early_command_line);
218221
while (*args) {
219222
args = next_arg(args, &param, &val);
@@ -231,15 +234,21 @@ void parse_boot_command_line(void)
231234

232235
if (!strcmp(param, "facilities"))
233236
modify_fac_list(val);
237+
238+
if (!strcmp(param, "nokaslr"))
239+
kaslr_enabled = 0;
234240
}
235241
}
236242

237243
void setup_memory_end(void)
238244
{
239245
#ifdef CONFIG_CRASH_DUMP
240-
if (!OLDMEM_BASE && ipl_block_valid &&
241-
ipl_block.pb0_hdr.pbt == IPL_PBT_FCP &&
242-
ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP) {
246+
if (OLDMEM_BASE) {
247+
kaslr_enabled = 0;
248+
} else if (ipl_block_valid &&
249+
ipl_block.pb0_hdr.pbt == IPL_PBT_FCP &&
250+
ipl_block.fcp.opt == IPL_PB0_FCP_OPT_DUMP) {
251+
kaslr_enabled = 0;
243252
if (!sclp_early_get_hsa_size(&memory_end) && memory_end)
244253
memory_end_set = 1;
245254
}

‎arch/s390/boot/kaslr.c‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright IBM Corp. 2019
4+
*/
5+
#include <asm/mem_detect.h>
6+
#include <asm/cpacf.h>
7+
#include <asm/timex.h>
8+
#include <asm/sclp.h>
9+
#include "compressed/decompressor.h"
10+
11+
#define PRNG_MODE_TDES 1
12+
#define PRNG_MODE_SHA512 2
13+
#define PRNG_MODE_TRNG 3
14+
15+
struct prno_parm {
16+
u32 res;
17+
u32 reseed_counter;
18+
u64 stream_bytes;
19+
u8 V[112];
20+
u8 C[112];
21+
};
22+
23+
struct prng_parm {
24+
u8 parm_block[32];
25+
u32 reseed_counter;
26+
u64 byte_counter;
27+
};
28+
29+
static int check_prng(void)
30+
{
31+
if (!cpacf_query_func(CPACF_KMC, CPACF_KMC_PRNG)) {
32+
sclp_early_printk("KASLR disabled: CPU has no PRNG\n");
33+
return 0;
34+
}
35+
if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
36+
return PRNG_MODE_TRNG;
37+
if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_SHA512_DRNG_GEN))
38+
return PRNG_MODE_SHA512;
39+
else
40+
return PRNG_MODE_TDES;
41+
}
42+
43+
static unsigned long get_random(unsigned long limit)
44+
{
45+
struct prng_parm prng = {
46+
/* initial parameter block for tdes mode, copied from libica */
47+
.parm_block = {
48+
0x0F, 0x2B, 0x8E, 0x63, 0x8C, 0x8E, 0xD2, 0x52,
49+
0x64, 0xB7, 0xA0, 0x7B, 0x75, 0x28, 0xB8, 0xF4,
50+
0x75, 0x5F, 0xD2, 0xA6, 0x8D, 0x97, 0x11, 0xFF,
51+
0x49, 0xD8, 0x23, 0xF3, 0x7E, 0x21, 0xEC, 0xA0
52+
},
53+
};
54+
unsigned long seed, random;
55+
struct prno_parm prno;
56+
__u64 entropy[4];
57+
int mode, i;
58+
59+
mode = check_prng();
60+
seed = get_tod_clock_fast();
61+
switch (mode) {
62+
case PRNG_MODE_TRNG:
63+
cpacf_trng(NULL, 0, (u8 *) &random, sizeof(random));
64+
break;
65+
case PRNG_MODE_SHA512:
66+
cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED, &prno, NULL, 0,
67+
(u8 *) &seed, sizeof(seed));
68+
cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN, &prno, (u8 *) &random,
69+
sizeof(random), NULL, 0);
70+
break;
71+
case PRNG_MODE_TDES:
72+
/* add entropy */
73+
*(unsigned long *) prng.parm_block ^= seed;
74+
for (i = 0; i < 16; i++) {
75+
cpacf_kmc(CPACF_KMC_PRNG, prng.parm_block,
76+
(char *) entropy, (char *) entropy,
77+
sizeof(entropy));
78+
memcpy(prng.parm_block, entropy, sizeof(entropy));
79+
}
80+
random = seed;
81+
cpacf_kmc(CPACF_KMC_PRNG, prng.parm_block, (u8 *) &random,
82+
(u8 *) &random, sizeof(random));
83+
break;
84+
default:
85+
random = 0;
86+
}
87+
return random % limit;
88+
}
89+
90+
unsigned long get_random_base(unsigned long safe_addr)
91+
{
92+
unsigned long base, start, end, kernel_size;
93+
unsigned long block_sum, offset;
94+
int i;
95+
96+
if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && INITRD_START && INITRD_SIZE) {
97+
if (safe_addr < INITRD_START + INITRD_SIZE)
98+
safe_addr = INITRD_START + INITRD_SIZE;
99+
}
100+
safe_addr = ALIGN(safe_addr, THREAD_SIZE);
101+
102+
kernel_size = vmlinux.image_size + vmlinux.bss_size;
103+
block_sum = 0;
104+
for_each_mem_detect_block(i, &start, &end) {
105+
if (memory_end_set) {
106+
if (start >= memory_end)
107+
break;
108+
if (end > memory_end)
109+
end = memory_end;
110+
}
111+
if (end - start < kernel_size)
112+
continue;
113+
block_sum += end - start - kernel_size;
114+
}
115+
if (!block_sum) {
116+
sclp_early_printk("KASLR disabled: not enough memory\n");
117+
return 0;
118+
}
119+
120+
base = get_random(block_sum);
121+
if (base == 0)
122+
return 0;
123+
if (base < safe_addr)
124+
base = safe_addr;
125+
block_sum = offset = 0;
126+
for_each_mem_detect_block(i, &start, &end) {
127+
if (memory_end_set) {
128+
if (start >= memory_end)
129+
break;
130+
if (end > memory_end)
131+
end = memory_end;
132+
}
133+
if (end - start < kernel_size)
134+
continue;
135+
block_sum += end - start - kernel_size;
136+
if (base <= block_sum) {
137+
base = start + base - offset;
138+
base = ALIGN_DOWN(base, THREAD_SIZE);
139+
break;
140+
}
141+
offset = block_sum;
142+
}
143+
return base;
144+
}

‎arch/s390/boot/startup.c‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
extern char __boot_data_start[], __boot_data_end[];
1414
extern char __boot_data_preserved_start[], __boot_data_preserved_end[];
15+
unsigned long __bootdata_preserved(__kaslr_offset);
1516

1617
/*
1718
* Some code and data needs to stay below 2 GB, even when the kernel would be
@@ -113,6 +114,7 @@ static void handle_relocs(unsigned long offset)
113114

114115
void startup_kernel(void)
115116
{
117+
unsigned long random_lma;
116118
unsigned long safe_addr;
117119
void *img;
118120

@@ -126,12 +128,37 @@ void startup_kernel(void)
126128
parse_boot_command_line();
127129
setup_memory_end();
128130
detect_memory();
131+
132+
random_lma = __kaslr_offset = 0;
133+
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
134+
random_lma = get_random_base(safe_addr);
135+
if (random_lma) {
136+
__kaslr_offset = random_lma - vmlinux.default_lma;
137+
img = (void *)vmlinux.default_lma;
138+
vmlinux.default_lma += __kaslr_offset;
139+
vmlinux.entry += __kaslr_offset;
140+
vmlinux.bootdata_off += __kaslr_offset;
141+
vmlinux.bootdata_preserved_off += __kaslr_offset;
142+
vmlinux.rela_dyn_start += __kaslr_offset;
143+
vmlinux.rela_dyn_end += __kaslr_offset;
144+
vmlinux.dynsym_start += __kaslr_offset;
145+
}
146+
}
147+
129148
if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
130149
img = decompress_kernel();
131150
memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
132-
}
151+
} else if (__kaslr_offset)
152+
memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
153+
133154
copy_bootdata();
134155
if (IS_ENABLED(CONFIG_RELOCATABLE))
135-
handle_relocs(0);
156+
handle_relocs(__kaslr_offset);
157+
158+
if (__kaslr_offset) {
159+
/* Clear non-relocated kernel */
160+
if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
161+
memset(img, 0, vmlinux.image_size);
162+
}
136163
vmlinux.entry();
137164
}

‎arch/s390/include/asm/setup.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ extern void (*_machine_restart)(char *command);
147147
extern void (*_machine_halt)(void);
148148
extern void (*_machine_power_off)(void);
149149

150+
extern unsigned long __kaslr_offset;
151+
static inline unsigned long kaslr_offset(void)
152+
{
153+
return __kaslr_offset;
154+
}
155+
150156
#else /* __ASSEMBLY__ */
151157

152158
#define IPL_DEVICE (IPL_DEVICE_OFFSET)

‎arch/s390/kernel/machine_kexec.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ void arch_crash_save_vmcoreinfo(void)
255255
mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
256256
vmcoreinfo_append_str("SDMA=%lx\n", __sdma);
257257
vmcoreinfo_append_str("EDMA=%lx\n", __edma);
258+
vmcoreinfo_append_str("KERNELOFFSET=%lx\n", kaslr_offset());
258259
}
259260

260261
void machine_shutdown(void)

‎arch/s390/kernel/setup.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ unsigned long __bootdata_preserved(__stext_dma);
108108
unsigned long __bootdata_preserved(__etext_dma);
109109
unsigned long __bootdata_preserved(__sdma);
110110
unsigned long __bootdata_preserved(__edma);
111+
unsigned long __bootdata_preserved(__kaslr_offset);
111112

112113
unsigned long VMALLOC_START;
113114
EXPORT_SYMBOL(VMALLOC_START);

0 commit comments

Comments
 (0)