Commit | Line | Data |
---|---|---|
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. */ | |
16 | static 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 | |
24 | static 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 |
39 | static 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. */ | |
55 | static 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 |
65 | static unsigned long get_random_long(void) |
66 | { | |
a653f356 KC |
67 | unsigned long raw, random = get_random_boot(); |
68 | bool use_i8254 = true; | |
69 | ||
70 | debug_putstr("KASLR using"); | |
5bfce5ef KC |
71 | |
72 | if (has_cpuflag(X86_FEATURE_RDRAND)) { | |
a653f356 KC |
73 | debug_putstr(" RDRAND"); |
74 | if (rdrand_long(&raw)) { | |
75 | random ^= raw; | |
76 | use_i8254 = false; | |
77 | } | |
5bfce5ef KC |
78 | } |
79 | ||
80 | if (has_cpuflag(X86_FEATURE_TSC)) { | |
a653f356 KC |
81 | debug_putstr(" RDTSC"); |
82 | rdtscll(raw); | |
5bfce5ef | 83 | |
a653f356 KC |
84 | random ^= raw; |
85 | use_i8254 = false; | |
86 | } | |
5bfce5ef | 87 | |
a653f356 KC |
88 | if (use_i8254) { |
89 | debug_putstr(" i8254"); | |
90 | random ^= i8254(); | |
5bfce5ef KC |
91 | } |
92 | ||
a653f356 KC |
93 | debug_putstr("...\n"); |
94 | ||
5bfce5ef KC |
95 | return random; |
96 | } | |
8ab3820f | 97 | |
82fa9637 KC |
98 | struct mem_vector { |
99 | unsigned long start; | |
100 | unsigned long size; | |
101 | }; | |
102 | ||
103 | #define MEM_AVOID_MAX 5 | |
104 | struct mem_vector mem_avoid[MEM_AVOID_MAX]; | |
105 | ||
106 | static bool mem_contains(struct mem_vector *region, struct mem_vector *item) | |
107 | { | |
108 | /* Item at least partially before region. */ | |
109 | if (item->start < region->start) | |
110 | return false; | |
111 | /* Item at least partially after region. */ | |
112 | if (item->start + item->size > region->start + region->size) | |
113 | return false; | |
114 | return true; | |
115 | } | |
116 | ||
117 | static bool mem_overlaps(struct mem_vector *one, struct mem_vector *two) | |
118 | { | |
119 | /* Item one is entirely before item two. */ | |
120 | if (one->start + one->size <= two->start) | |
121 | return false; | |
122 | /* Item one is entirely after item two. */ | |
123 | if (one->start >= two->start + two->size) | |
124 | return false; | |
125 | return true; | |
126 | } | |
127 | ||
128 | static void mem_avoid_init(unsigned long input, unsigned long input_size, | |
129 | unsigned long output, unsigned long output_size) | |
130 | { | |
131 | u64 initrd_start, initrd_size; | |
132 | u64 cmd_line, cmd_line_size; | |
133 | unsigned long unsafe, unsafe_len; | |
134 | char *ptr; | |
135 | ||
136 | /* | |
137 | * Avoid the region that is unsafe to overlap during | |
138 | * decompression (see calculations at top of misc.c). | |
139 | */ | |
140 | unsafe_len = (output_size >> 12) + 32768 + 18; | |
141 | unsafe = (unsigned long)input + input_size - unsafe_len; | |
142 | mem_avoid[0].start = unsafe; | |
143 | mem_avoid[0].size = unsafe_len; | |
144 | ||
145 | /* Avoid initrd. */ | |
146 | initrd_start = (u64)real_mode->ext_ramdisk_image << 32; | |
147 | initrd_start |= real_mode->hdr.ramdisk_image; | |
148 | initrd_size = (u64)real_mode->ext_ramdisk_size << 32; | |
149 | initrd_size |= real_mode->hdr.ramdisk_size; | |
150 | mem_avoid[1].start = initrd_start; | |
151 | mem_avoid[1].size = initrd_size; | |
152 | ||
153 | /* Avoid kernel command line. */ | |
154 | cmd_line = (u64)real_mode->ext_cmd_line_ptr << 32; | |
155 | cmd_line |= real_mode->hdr.cmd_line_ptr; | |
156 | /* Calculate size of cmd_line. */ | |
157 | ptr = (char *)(unsigned long)cmd_line; | |
158 | for (cmd_line_size = 0; ptr[cmd_line_size++]; ) | |
159 | ; | |
160 | mem_avoid[2].start = cmd_line; | |
161 | mem_avoid[2].size = cmd_line_size; | |
162 | ||
163 | /* Avoid heap memory. */ | |
164 | mem_avoid[3].start = (unsigned long)free_mem_ptr; | |
165 | mem_avoid[3].size = BOOT_HEAP_SIZE; | |
166 | ||
167 | /* Avoid stack memory. */ | |
168 | mem_avoid[4].start = (unsigned long)free_mem_end_ptr; | |
169 | mem_avoid[4].size = BOOT_STACK_SIZE; | |
170 | } | |
171 | ||
172 | /* Does this memory vector overlap a known avoided area? */ | |
173 | bool mem_avoid_overlap(struct mem_vector *img) | |
174 | { | |
175 | int i; | |
176 | ||
177 | for (i = 0; i < MEM_AVOID_MAX; i++) { | |
178 | if (mem_overlaps(img, &mem_avoid[i])) | |
179 | return true; | |
180 | } | |
181 | ||
182 | return false; | |
183 | } | |
184 | ||
185 | unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET / CONFIG_PHYSICAL_ALIGN]; | |
186 | unsigned long slot_max = 0; | |
187 | ||
188 | static void slots_append(unsigned long addr) | |
189 | { | |
190 | /* Overflowing the slots list should be impossible. */ | |
191 | if (slot_max >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET / | |
192 | CONFIG_PHYSICAL_ALIGN) | |
193 | return; | |
194 | ||
195 | slots[slot_max++] = addr; | |
196 | } | |
197 | ||
198 | static unsigned long slots_fetch_random(void) | |
199 | { | |
200 | /* Handle case of no slots stored. */ | |
201 | if (slot_max == 0) | |
202 | return 0; | |
203 | ||
204 | return slots[get_random_long() % slot_max]; | |
205 | } | |
206 | ||
207 | static void process_e820_entry(struct e820entry *entry, | |
208 | unsigned long minimum, | |
209 | unsigned long image_size) | |
210 | { | |
211 | struct mem_vector region, img; | |
212 | ||
213 | /* Skip non-RAM entries. */ | |
214 | if (entry->type != E820_RAM) | |
215 | return; | |
216 | ||
217 | /* Ignore entries entirely above our maximum. */ | |
218 | if (entry->addr >= CONFIG_RANDOMIZE_BASE_MAX_OFFSET) | |
219 | return; | |
220 | ||
221 | /* Ignore entries entirely below our minimum. */ | |
222 | if (entry->addr + entry->size < minimum) | |
223 | return; | |
224 | ||
225 | region.start = entry->addr; | |
226 | region.size = entry->size; | |
227 | ||
228 | /* Potentially raise address to minimum location. */ | |
229 | if (region.start < minimum) | |
230 | region.start = minimum; | |
231 | ||
232 | /* Potentially raise address to meet alignment requirements. */ | |
233 | region.start = ALIGN(region.start, CONFIG_PHYSICAL_ALIGN); | |
234 | ||
235 | /* Did we raise the address above the bounds of this e820 region? */ | |
236 | if (region.start > entry->addr + entry->size) | |
237 | return; | |
238 | ||
239 | /* Reduce size by any delta from the original address. */ | |
240 | region.size -= region.start - entry->addr; | |
241 | ||
242 | /* Reduce maximum size to fit end of image within maximum limit. */ | |
243 | if (region.start + region.size > CONFIG_RANDOMIZE_BASE_MAX_OFFSET) | |
244 | region.size = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - region.start; | |
245 | ||
246 | /* Walk each aligned slot and check for avoided areas. */ | |
247 | for (img.start = region.start, img.size = image_size ; | |
248 | mem_contains(®ion, &img) ; | |
249 | img.start += CONFIG_PHYSICAL_ALIGN) { | |
250 | if (mem_avoid_overlap(&img)) | |
251 | continue; | |
252 | slots_append(img.start); | |
253 | } | |
254 | } | |
255 | ||
256 | static unsigned long find_random_addr(unsigned long minimum, | |
257 | unsigned long size) | |
258 | { | |
259 | int i; | |
260 | unsigned long addr; | |
261 | ||
262 | /* Make sure minimum is aligned. */ | |
263 | minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN); | |
264 | ||
265 | /* Verify potential e820 positions, appending to slots list. */ | |
266 | for (i = 0; i < real_mode->e820_entries; i++) { | |
267 | process_e820_entry(&real_mode->e820_map[i], minimum, size); | |
268 | } | |
269 | ||
270 | return slots_fetch_random(); | |
271 | } | |
272 | ||
8ab3820f KC |
273 | unsigned char *choose_kernel_location(unsigned char *input, |
274 | unsigned long input_size, | |
275 | unsigned char *output, | |
276 | unsigned long output_size) | |
277 | { | |
278 | unsigned long choice = (unsigned long)output; | |
82fa9637 | 279 | unsigned long random; |
8ab3820f KC |
280 | |
281 | if (cmdline_find_option_bool("nokaslr")) { | |
282 | debug_putstr("KASLR disabled...\n"); | |
283 | goto out; | |
284 | } | |
285 | ||
82fa9637 KC |
286 | /* Record the various known unsafe memory ranges. */ |
287 | mem_avoid_init((unsigned long)input, input_size, | |
288 | (unsigned long)output, output_size); | |
289 | ||
290 | /* Walk e820 and find a random address. */ | |
291 | random = find_random_addr(choice, output_size); | |
292 | if (!random) { | |
293 | debug_putstr("KASLR could not find suitable E820 region...\n"); | |
294 | goto out; | |
295 | } | |
296 | ||
297 | /* Always enforce the minimum. */ | |
298 | if (random < choice) | |
299 | goto out; | |
8ab3820f | 300 | |
82fa9637 | 301 | choice = random; |
8ab3820f KC |
302 | out: |
303 | return (unsigned char *)choice; | |
304 | } | |
305 | ||
306 | #endif /* CONFIG_RANDOMIZE_BASE */ |