1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright IBM Corp. 2019
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun #include <linux/pgtable.h>
6*4882a593Smuzhiyun #include <asm/mem_detect.h>
7*4882a593Smuzhiyun #include <asm/cpacf.h>
8*4882a593Smuzhiyun #include <asm/timex.h>
9*4882a593Smuzhiyun #include <asm/sclp.h>
10*4882a593Smuzhiyun #include "compressed/decompressor.h"
11*4882a593Smuzhiyun #include "boot.h"
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #define PRNG_MODE_TDES 1
14*4882a593Smuzhiyun #define PRNG_MODE_SHA512 2
15*4882a593Smuzhiyun #define PRNG_MODE_TRNG 3
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun struct prno_parm {
18*4882a593Smuzhiyun u32 res;
19*4882a593Smuzhiyun u32 reseed_counter;
20*4882a593Smuzhiyun u64 stream_bytes;
21*4882a593Smuzhiyun u8 V[112];
22*4882a593Smuzhiyun u8 C[112];
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct prng_parm {
26*4882a593Smuzhiyun u8 parm_block[32];
27*4882a593Smuzhiyun u32 reseed_counter;
28*4882a593Smuzhiyun u64 byte_counter;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
check_prng(void)31*4882a593Smuzhiyun static int check_prng(void)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun if (!cpacf_query_func(CPACF_KMC, CPACF_KMC_PRNG)) {
34*4882a593Smuzhiyun sclp_early_printk("KASLR disabled: CPU has no PRNG\n");
35*4882a593Smuzhiyun return 0;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
38*4882a593Smuzhiyun return PRNG_MODE_TRNG;
39*4882a593Smuzhiyun if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_SHA512_DRNG_GEN))
40*4882a593Smuzhiyun return PRNG_MODE_SHA512;
41*4882a593Smuzhiyun else
42*4882a593Smuzhiyun return PRNG_MODE_TDES;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
get_random(unsigned long limit,unsigned long * value)45*4882a593Smuzhiyun static int get_random(unsigned long limit, unsigned long *value)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct prng_parm prng = {
48*4882a593Smuzhiyun /* initial parameter block for tdes mode, copied from libica */
49*4882a593Smuzhiyun .parm_block = {
50*4882a593Smuzhiyun 0x0F, 0x2B, 0x8E, 0x63, 0x8C, 0x8E, 0xD2, 0x52,
51*4882a593Smuzhiyun 0x64, 0xB7, 0xA0, 0x7B, 0x75, 0x28, 0xB8, 0xF4,
52*4882a593Smuzhiyun 0x75, 0x5F, 0xD2, 0xA6, 0x8D, 0x97, 0x11, 0xFF,
53*4882a593Smuzhiyun 0x49, 0xD8, 0x23, 0xF3, 0x7E, 0x21, 0xEC, 0xA0
54*4882a593Smuzhiyun },
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun unsigned long seed, random;
57*4882a593Smuzhiyun struct prno_parm prno;
58*4882a593Smuzhiyun __u64 entropy[4];
59*4882a593Smuzhiyun int mode, i;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun mode = check_prng();
62*4882a593Smuzhiyun seed = get_tod_clock_fast();
63*4882a593Smuzhiyun switch (mode) {
64*4882a593Smuzhiyun case PRNG_MODE_TRNG:
65*4882a593Smuzhiyun cpacf_trng(NULL, 0, (u8 *) &random, sizeof(random));
66*4882a593Smuzhiyun break;
67*4882a593Smuzhiyun case PRNG_MODE_SHA512:
68*4882a593Smuzhiyun cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED, &prno, NULL, 0,
69*4882a593Smuzhiyun (u8 *) &seed, sizeof(seed));
70*4882a593Smuzhiyun cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN, &prno, (u8 *) &random,
71*4882a593Smuzhiyun sizeof(random), NULL, 0);
72*4882a593Smuzhiyun break;
73*4882a593Smuzhiyun case PRNG_MODE_TDES:
74*4882a593Smuzhiyun /* add entropy */
75*4882a593Smuzhiyun *(unsigned long *) prng.parm_block ^= seed;
76*4882a593Smuzhiyun for (i = 0; i < 16; i++) {
77*4882a593Smuzhiyun cpacf_kmc(CPACF_KMC_PRNG, prng.parm_block,
78*4882a593Smuzhiyun (u8 *) entropy, (u8 *) entropy,
79*4882a593Smuzhiyun sizeof(entropy));
80*4882a593Smuzhiyun memcpy(prng.parm_block, entropy, sizeof(entropy));
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun random = seed;
83*4882a593Smuzhiyun cpacf_kmc(CPACF_KMC_PRNG, prng.parm_block, (u8 *) &random,
84*4882a593Smuzhiyun (u8 *) &random, sizeof(random));
85*4882a593Smuzhiyun break;
86*4882a593Smuzhiyun default:
87*4882a593Smuzhiyun return -1;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun *value = random % limit;
90*4882a593Smuzhiyun return 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * To randomize kernel base address we have to consider several facts:
95*4882a593Smuzhiyun * 1. physical online memory might not be continuous and have holes. mem_detect
96*4882a593Smuzhiyun * info contains list of online memory ranges we should consider.
97*4882a593Smuzhiyun * 2. we have several memory regions which are occupied and we should not
98*4882a593Smuzhiyun * overlap and destroy them. Currently safe_addr tells us the border below
99*4882a593Smuzhiyun * which all those occupied regions are. We are safe to use anything above
100*4882a593Smuzhiyun * safe_addr.
101*4882a593Smuzhiyun * 3. the upper limit might apply as well, even if memory above that limit is
102*4882a593Smuzhiyun * online. Currently those limitations are:
103*4882a593Smuzhiyun * 3.1. Limit set by "mem=" kernel command line option
104*4882a593Smuzhiyun * 3.2. memory reserved at the end for kasan initialization.
105*4882a593Smuzhiyun * 4. kernel base address must be aligned to THREAD_SIZE (kernel stack size).
106*4882a593Smuzhiyun * Which is required for CONFIG_CHECK_STACK. Currently THREAD_SIZE is 4 pages
107*4882a593Smuzhiyun * (16 pages when the kernel is built with kasan enabled)
108*4882a593Smuzhiyun * Assumptions:
109*4882a593Smuzhiyun * 1. kernel size (including .bss size) and upper memory limit are page aligned.
110*4882a593Smuzhiyun * 2. mem_detect memory region start is THREAD_SIZE aligned / end is PAGE_SIZE
111*4882a593Smuzhiyun * aligned (in practice memory configurations granularity on z/VM and LPAR
112*4882a593Smuzhiyun * is 1mb).
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * To guarantee uniform distribution of kernel base address among all suitable
115*4882a593Smuzhiyun * addresses we generate random value just once. For that we need to build a
116*4882a593Smuzhiyun * continuous range in which every value would be suitable. We can build this
117*4882a593Smuzhiyun * range by simply counting all suitable addresses (let's call them positions)
118*4882a593Smuzhiyun * which would be valid as kernel base address. To count positions we iterate
119*4882a593Smuzhiyun * over online memory ranges. For each range which is big enough for the
120*4882a593Smuzhiyun * kernel image we count all suitable addresses we can put the kernel image at
121*4882a593Smuzhiyun * that is
122*4882a593Smuzhiyun * (end - start - kernel_size) / THREAD_SIZE + 1
123*4882a593Smuzhiyun * Two functions count_valid_kernel_positions and position_to_address help
124*4882a593Smuzhiyun * to count positions in memory range given and then convert position back
125*4882a593Smuzhiyun * to address.
126*4882a593Smuzhiyun */
count_valid_kernel_positions(unsigned long kernel_size,unsigned long _min,unsigned long _max)127*4882a593Smuzhiyun static unsigned long count_valid_kernel_positions(unsigned long kernel_size,
128*4882a593Smuzhiyun unsigned long _min,
129*4882a593Smuzhiyun unsigned long _max)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun unsigned long start, end, pos = 0;
132*4882a593Smuzhiyun int i;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun for_each_mem_detect_block(i, &start, &end) {
135*4882a593Smuzhiyun if (_min >= end)
136*4882a593Smuzhiyun continue;
137*4882a593Smuzhiyun if (start >= _max)
138*4882a593Smuzhiyun break;
139*4882a593Smuzhiyun start = max(_min, start);
140*4882a593Smuzhiyun end = min(_max, end);
141*4882a593Smuzhiyun if (end - start < kernel_size)
142*4882a593Smuzhiyun continue;
143*4882a593Smuzhiyun pos += (end - start - kernel_size) / THREAD_SIZE + 1;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun return pos;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
position_to_address(unsigned long pos,unsigned long kernel_size,unsigned long _min,unsigned long _max)149*4882a593Smuzhiyun static unsigned long position_to_address(unsigned long pos, unsigned long kernel_size,
150*4882a593Smuzhiyun unsigned long _min, unsigned long _max)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun unsigned long start, end;
153*4882a593Smuzhiyun int i;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun for_each_mem_detect_block(i, &start, &end) {
156*4882a593Smuzhiyun if (_min >= end)
157*4882a593Smuzhiyun continue;
158*4882a593Smuzhiyun if (start >= _max)
159*4882a593Smuzhiyun break;
160*4882a593Smuzhiyun start = max(_min, start);
161*4882a593Smuzhiyun end = min(_max, end);
162*4882a593Smuzhiyun if (end - start < kernel_size)
163*4882a593Smuzhiyun continue;
164*4882a593Smuzhiyun if ((end - start - kernel_size) / THREAD_SIZE + 1 >= pos)
165*4882a593Smuzhiyun return start + (pos - 1) * THREAD_SIZE;
166*4882a593Smuzhiyun pos -= (end - start - kernel_size) / THREAD_SIZE + 1;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
get_random_base(unsigned long safe_addr)172*4882a593Smuzhiyun unsigned long get_random_base(unsigned long safe_addr)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun unsigned long memory_limit = get_mem_detect_end();
175*4882a593Smuzhiyun unsigned long base_pos, max_pos, kernel_size;
176*4882a593Smuzhiyun unsigned long kasan_needs;
177*4882a593Smuzhiyun int i;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (memory_end_set)
180*4882a593Smuzhiyun memory_limit = min(memory_limit, memory_end);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && INITRD_START && INITRD_SIZE) {
183*4882a593Smuzhiyun if (safe_addr < INITRD_START + INITRD_SIZE)
184*4882a593Smuzhiyun safe_addr = INITRD_START + INITRD_SIZE;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun safe_addr = ALIGN(safe_addr, THREAD_SIZE);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if ((IS_ENABLED(CONFIG_KASAN))) {
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * Estimate kasan memory requirements, which it will reserve
191*4882a593Smuzhiyun * at the very end of available physical memory. To estimate
192*4882a593Smuzhiyun * that, we take into account that kasan would require
193*4882a593Smuzhiyun * 1/8 of available physical memory (for shadow memory) +
194*4882a593Smuzhiyun * creating page tables for the whole memory + shadow memory
195*4882a593Smuzhiyun * region (1 + 1/8). To keep page tables estimates simple take
196*4882a593Smuzhiyun * the double of combined ptes size.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun memory_limit = get_mem_detect_end();
199*4882a593Smuzhiyun if (memory_end_set && memory_limit > memory_end)
200*4882a593Smuzhiyun memory_limit = memory_end;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* for shadow memory */
203*4882a593Smuzhiyun kasan_needs = memory_limit / 8;
204*4882a593Smuzhiyun /* for paging structures */
205*4882a593Smuzhiyun kasan_needs += (memory_limit + kasan_needs) / PAGE_SIZE /
206*4882a593Smuzhiyun _PAGE_ENTRIES * _PAGE_TABLE_SIZE * 2;
207*4882a593Smuzhiyun memory_limit -= kasan_needs;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun kernel_size = vmlinux.image_size + vmlinux.bss_size;
211*4882a593Smuzhiyun if (safe_addr + kernel_size > memory_limit)
212*4882a593Smuzhiyun return 0;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun max_pos = count_valid_kernel_positions(kernel_size, safe_addr, memory_limit);
215*4882a593Smuzhiyun if (!max_pos) {
216*4882a593Smuzhiyun sclp_early_printk("KASLR disabled: not enough memory\n");
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* we need a value in the range [1, base_pos] inclusive */
221*4882a593Smuzhiyun if (get_random(max_pos, &base_pos))
222*4882a593Smuzhiyun return 0;
223*4882a593Smuzhiyun return position_to_address(base_pos + 1, kernel_size, safe_addr, memory_limit);
224*4882a593Smuzhiyun }
225