x86/mm/ASLR: Propagate base load address calculation
[sfrench/cifs-2.6.git] / arch / x86 / boot / compressed / aslr.c
1 #include "misc.h"
2
3 #include <asm/msr.h>
4 #include <asm/archrandom.h>
5 #include <asm/e820.h>
6
7 #include <generated/compile.h>
8 #include <linux/module.h>
9 #include <linux/uts.h>
10 #include <linux/utsname.h>
11 #include <generated/utsrelease.h>
12
13 /* Simplified build-specific string for starting entropy. */
14 static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
15                 LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
16
17 struct kaslr_setup_data {
18         __u64 next;
19         __u32 type;
20         __u32 len;
21         __u8 data[1];
22 } kaslr_setup_data;
23
24 #define I8254_PORT_CONTROL      0x43
25 #define I8254_PORT_COUNTER0     0x40
26 #define I8254_CMD_READBACK      0xC0
27 #define I8254_SELECT_COUNTER0   0x02
28 #define I8254_STATUS_NOTREADY   0x40
29 static inline u16 i8254(void)
30 {
31         u16 status, timer;
32
33         do {
34                 outb(I8254_PORT_CONTROL,
35                      I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
36                 status = inb(I8254_PORT_COUNTER0);
37                 timer  = inb(I8254_PORT_COUNTER0);
38                 timer |= inb(I8254_PORT_COUNTER0) << 8;
39         } while (status & I8254_STATUS_NOTREADY);
40
41         return timer;
42 }
43
44 static unsigned long rotate_xor(unsigned long hash, const void *area,
45                                 size_t size)
46 {
47         size_t i;
48         unsigned long *ptr = (unsigned long *)area;
49
50         for (i = 0; i < size / sizeof(hash); i++) {
51                 /* Rotate by odd number of bits and XOR. */
52                 hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
53                 hash ^= ptr[i];
54         }
55
56         return hash;
57 }
58
59 /* Attempt to create a simple but unpredictable starting entropy. */
60 static unsigned long get_random_boot(void)
61 {
62         unsigned long hash = 0;
63
64         hash = rotate_xor(hash, build_str, sizeof(build_str));
65         hash = rotate_xor(hash, real_mode, sizeof(*real_mode));
66
67         return hash;
68 }
69
70 static unsigned long get_random_long(void)
71 {
72 #ifdef CONFIG_X86_64
73         const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
74 #else
75         const unsigned long mix_const = 0x3f39e593UL;
76 #endif
77         unsigned long raw, random = get_random_boot();
78         bool use_i8254 = true;
79
80         debug_putstr("KASLR using");
81
82         if (has_cpuflag(X86_FEATURE_RDRAND)) {
83                 debug_putstr(" RDRAND");
84                 if (rdrand_long(&raw)) {
85                         random ^= raw;
86                         use_i8254 = false;
87                 }
88         }
89
90         if (has_cpuflag(X86_FEATURE_TSC)) {
91                 debug_putstr(" RDTSC");
92                 rdtscll(raw);
93
94                 random ^= raw;
95                 use_i8254 = false;
96         }
97
98         if (use_i8254) {
99                 debug_putstr(" i8254");
100                 random ^= i8254();
101         }
102
103         /* Circular multiply for better bit diffusion */
104         asm("mul %3"
105             : "=a" (random), "=d" (raw)
106             : "a" (random), "rm" (mix_const));
107         random += raw;
108
109         debug_putstr("...\n");
110
111         return random;
112 }
113
114 struct mem_vector {
115         unsigned long start;
116         unsigned long size;
117 };
118
119 #define MEM_AVOID_MAX 5
120 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
121
122 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
123 {
124         /* Item at least partially before region. */
125         if (item->start < region->start)
126                 return false;
127         /* Item at least partially after region. */
128         if (item->start + item->size > region->start + region->size)
129                 return false;
130         return true;
131 }
132
133 static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two)
134 {
135         /* Item one is entirely before item two. */
136         if (one->start + one->size <= two->start)
137                 return false;
138         /* Item one is entirely after item two. */
139         if (one->start >= two->start + two->size)
140                 return false;
141         return true;
142 }
143
144 static void mem_avoid_init(unsigned long input, unsigned long input_size,
145                            unsigned long output, unsigned long output_size)
146 {
147         u64 initrd_start, initrd_size;
148         u64 cmd_line, cmd_line_size;
149         unsigned long unsafe, unsafe_len;
150         char *ptr;
151
152         /*
153          * Avoid the region that is unsafe to overlap during
154          * decompression (see calculations at top of misc.c).
155          */
156         unsafe_len = (output_size >> 12) + 32768 + 18;
157         unsafe = (unsigned long)input + input_size - unsafe_len;
158         mem_avoid[0].start = unsafe;
159         mem_avoid[0].size = unsafe_len;
160
161         /* Avoid initrd. */
162         initrd_start  = (u64)real_mode->ext_ramdisk_image << 32;
163         initrd_start |= real_mode->hdr.ramdisk_image;
164         initrd_size  = (u64)real_mode->ext_ramdisk_size << 32;
165         initrd_size |= real_mode->hdr.ramdisk_size;
166         mem_avoid[1].start = initrd_start;
167         mem_avoid[1].size = initrd_size;
168
169         /* Avoid kernel command line. */
170         cmd_line  = (u64)real_mode->ext_cmd_line_ptr << 32;
171         cmd_line |= real_mode->hdr.cmd_line_ptr;
172         /* Calculate size of cmd_line. */
173         ptr = (char *)(unsigned long)cmd_line;
174         for (cmd_line_size = 0; ptr[cmd_line_size++]; )
175                 ;
176         mem_avoid[2].start = cmd_line;
177         mem_avoid[2].size = cmd_line_size;
178
179         /* Avoid heap memory. */
180         mem_avoid[3].start = (unsigned long)free_mem_ptr;
181         mem_avoid[3].size = BOOT_HEAP_SIZE;
182
183         /* Avoid stack memory. */
184         mem_avoid[4].start = (unsigned long)free_mem_end_ptr;
185         mem_avoid[4].size = BOOT_STACK_SIZE;
186 }
187
188 /* Does this memory vector overlap a known avoided area? */
189 static bool mem_avoid_overlap(struct mem_vector *img)
190 {
191         int i;
192         struct setup_data *ptr;
193
194         for (i = 0; i < MEM_AVOID_MAX; i++) {
195                 if (mem_overlaps(img, &mem_avoid[i]))
196                         return true;
197         }
198
199         /* Avoid all entries in the setup_data linked list. */
200         ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data;
201         while (ptr) {
202                 struct mem_vector avoid;
203
204                 avoid.start = (unsigned long)ptr;
205                 avoid.size = sizeof(*ptr) + ptr->len;
206
207                 if (mem_overlaps(img, &avoid))
208                         return true;
209
210                 ptr = (struct setup_data *)(unsigned long)ptr->next;
211         }
212
213         return false;
214 }
215
216 static unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET /
217                            CONFIG_PHYSICAL_ALIGN];
218 static unsigned long slot_max;
219
220 static void slots_append(unsigned long addr)
221 {
222         /* Overflowing the slots list should be impossible. */
223         if (slot_max >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET /
224                         CONFIG_PHYSICAL_ALIGN)
225                 return;
226
227         slots[slot_max++] = addr;
228 }
229
230 static unsigned long slots_fetch_random(void)
231 {
232         /* Handle case of no slots stored. */
233         if (slot_max == 0)
234                 return 0;
235
236         return slots[get_random_long() % slot_max];
237 }
238
239 static void process_e820_entry(struct e820entry *entry,
240                                unsigned long minimum,
241                                unsigned long image_size)
242 {
243         struct mem_vector region, img;
244
245         /* Skip non-RAM entries. */
246         if (entry->type != E820_RAM)
247                 return;
248
249         /* Ignore entries entirely above our maximum. */
250         if (entry->addr >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET)
251                 return;
252
253         /* Ignore entries entirely below our minimum. */
254         if (entry->addr + entry->size < minimum)
255                 return;
256
257         region.start = entry->addr;
258         region.size = entry->size;
259
260         /* Potentially raise address to minimum location. */
261         if (region.start < minimum)
262                 region.start = minimum;
263
264         /* Potentially raise address to meet alignment requirements. */
265         region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN);
266
267         /* Did we raise the address above the bounds of this e820 region? */
268         if (region.start > entry->addr + entry->size)
269                 return;
270
271         /* Reduce size by any delta from the original address. */
272         region.size -= region.start - entry->addr;
273
274         /* Reduce maximum size to fit end of image within maximum limit. */
275         if (region.start + region.size > CONFIG_RANDOMIZE_BASE_MAX_OFFSET)
276                 region.size = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - region.start;
277
278         /* Walk each aligned slot and check for avoided areas. */
279         for (img.start = region.start, img.size = image_size ;
280              mem_contains(&region, &img) ;
281              img.start += CONFIG_PHYSICAL_ALIGN) {
282                 if (mem_avoid_overlap(&img))
283                         continue;
284                 slots_append(img.start);
285         }
286 }
287
288 static unsigned long find_random_addr(unsigned long minimum,
289                                       unsigned long size)
290 {
291         int i;
292         unsigned long addr;
293
294         /* Make sure minimum is aligned. */
295         minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
296
297         /* Verify potential e820 positions, appending to slots list. */
298         for (i = 0; i < real_mode->e820_entries; i++) {
299                 process_e820_entry(&real_mode->e820_map[i], minimum, size);
300         }
301
302         return slots_fetch_random();
303 }
304
305 static void add_kaslr_setup_data(struct boot_params *params, __u8 enabled)
306 {
307         struct setup_data *data;
308
309         kaslr_setup_data.type = SETUP_KASLR;
310         kaslr_setup_data.len = 1;
311         kaslr_setup_data.next = 0;
312         kaslr_setup_data.data[0] = enabled;
313
314         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
315
316         while (data && data->next)
317                 data = (struct setup_data *)(unsigned long)data->next;
318
319         if (data)
320                 data->next = (unsigned long)&kaslr_setup_data;
321         else
322                 params->hdr.setup_data = (unsigned long)&kaslr_setup_data;
323
324 }
325
326 unsigned char *choose_kernel_location(struct boot_params *params,
327                                       unsigned char *input,
328                                       unsigned long input_size,
329                                       unsigned char *output,
330                                       unsigned long output_size)
331 {
332         unsigned long choice = (unsigned long)output;
333         unsigned long random;
334
335 #ifdef CONFIG_HIBERNATION
336         if (!cmdline_find_option_bool("kaslr")) {
337                 debug_putstr("KASLR disabled by default...\n");
338                 add_kaslr_setup_data(params, 0);
339                 goto out;
340         }
341 #else
342         if (cmdline_find_option_bool("nokaslr")) {
343                 debug_putstr("KASLR disabled by cmdline...\n");
344                 add_kaslr_setup_data(params, 0);
345                 goto out;
346         }
347 #endif
348         add_kaslr_setup_data(params, 1);
349
350         /* Record the various known unsafe memory ranges. */
351         mem_avoid_init((unsigned long)input, input_size,
352                        (unsigned long)output, output_size);
353
354         /* Walk e820 and find a random address. */
355         random = find_random_addr(choice, output_size);
356         if (!random) {
357                 debug_putstr("KASLR could not find suitable E820 region...\n");
358                 goto out;
359         }
360
361         /* Always enforce the minimum. */
362         if (random < choice)
363                 goto out;
364
365         choice = random;
366 out:
367         return (unsigned char *)choice;
368 }