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