]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/boot/compressed/kaslr.c
4 * This contains the routines needed to generate a reasonable level of
5 * entropy to choose a randomized kernel base address offset in support
6 * of Kernel Address Space Layout Randomization (KASLR). Additionally
7 * handles walking the physical memory maps (and tracking memory regions
8 * to avoid) in order to select a physical memory location that can
9 * contain the entire properly aligned running kernel image.
16 #include <asm/archrandom.h>
19 #include <generated/compile.h>
20 #include <linux/module.h>
21 #include <linux/uts.h>
22 #include <linux/utsname.h>
23 #include <generated/utsrelease.h>
25 /* Simplified build-specific string for starting entropy. */
26 static const char build_str
[] = UTS_RELEASE
" (" LINUX_COMPILE_BY
"@"
27 LINUX_COMPILE_HOST
") (" LINUX_COMPILER
") " UTS_VERSION
;
29 #define I8254_PORT_CONTROL 0x43
30 #define I8254_PORT_COUNTER0 0x40
31 #define I8254_CMD_READBACK 0xC0
32 #define I8254_SELECT_COUNTER0 0x02
33 #define I8254_STATUS_NOTREADY 0x40
34 static inline u16
i8254(void)
39 outb(I8254_PORT_CONTROL
,
40 I8254_CMD_READBACK
| I8254_SELECT_COUNTER0
);
41 status
= inb(I8254_PORT_COUNTER0
);
42 timer
= inb(I8254_PORT_COUNTER0
);
43 timer
|= inb(I8254_PORT_COUNTER0
) << 8;
44 } while (status
& I8254_STATUS_NOTREADY
);
49 static unsigned long rotate_xor(unsigned long hash
, const void *area
,
53 unsigned long *ptr
= (unsigned long *)area
;
55 for (i
= 0; i
< size
/ sizeof(hash
); i
++) {
56 /* Rotate by odd number of bits and XOR. */
57 hash
= (hash
<< ((sizeof(hash
) * 8) - 7)) | (hash
>> 7);
64 /* Attempt to create a simple but unpredictable starting entropy. */
65 static unsigned long get_random_boot(void)
67 unsigned long hash
= 0;
69 hash
= rotate_xor(hash
, build_str
, sizeof(build_str
));
70 hash
= rotate_xor(hash
, boot_params
, sizeof(*boot_params
));
75 static unsigned long get_random_long(void)
78 const unsigned long mix_const
= 0x5d6008cbf3848dd3UL
;
80 const unsigned long mix_const
= 0x3f39e593UL
;
82 unsigned long raw
, random
= get_random_boot();
83 bool use_i8254
= true;
85 debug_putstr("KASLR using");
87 if (has_cpuflag(X86_FEATURE_RDRAND
)) {
88 debug_putstr(" RDRAND");
89 if (rdrand_long(&raw
)) {
95 if (has_cpuflag(X86_FEATURE_TSC
)) {
96 debug_putstr(" RDTSC");
104 debug_putstr(" i8254");
108 /* Circular multiply for better bit diffusion */
110 : "=a" (random
), "=d" (raw
)
111 : "a" (random
), "rm" (mix_const
));
114 debug_putstr("...\n");
124 #define MEM_AVOID_MAX 5
125 static struct mem_vector mem_avoid
[MEM_AVOID_MAX
];
127 static bool mem_contains(struct mem_vector
*region
, struct mem_vector
*item
)
129 /* Item at least partially before region. */
130 if (item
->start
< region
->start
)
132 /* Item at least partially after region. */
133 if (item
->start
+ item
->size
> region
->start
+ region
->size
)
138 static bool mem_overlaps(struct mem_vector
*one
, struct mem_vector
*two
)
140 /* Item one is entirely before item two. */
141 if (one
->start
+ one
->size
<= two
->start
)
143 /* Item one is entirely after item two. */
144 if (one
->start
>= two
->start
+ two
->size
)
149 static void mem_avoid_init(unsigned long input
, unsigned long input_size
,
150 unsigned long output
, unsigned long output_size
)
152 u64 initrd_start
, initrd_size
;
153 u64 cmd_line
, cmd_line_size
;
154 unsigned long unsafe
, unsafe_len
;
158 * Avoid the region that is unsafe to overlap during
159 * decompression (see calculations in ../header.S).
161 unsafe_len
= (output_size
>> 12) + 32768 + 18;
162 unsafe
= (unsigned long)input
+ input_size
- unsafe_len
;
163 mem_avoid
[0].start
= unsafe
;
164 mem_avoid
[0].size
= unsafe_len
;
167 initrd_start
= (u64
)boot_params
->ext_ramdisk_image
<< 32;
168 initrd_start
|= boot_params
->hdr
.ramdisk_image
;
169 initrd_size
= (u64
)boot_params
->ext_ramdisk_size
<< 32;
170 initrd_size
|= boot_params
->hdr
.ramdisk_size
;
171 mem_avoid
[1].start
= initrd_start
;
172 mem_avoid
[1].size
= initrd_size
;
174 /* Avoid kernel command line. */
175 cmd_line
= (u64
)boot_params
->ext_cmd_line_ptr
<< 32;
176 cmd_line
|= boot_params
->hdr
.cmd_line_ptr
;
177 /* Calculate size of cmd_line. */
178 ptr
= (char *)(unsigned long)cmd_line
;
179 for (cmd_line_size
= 0; ptr
[cmd_line_size
++]; )
181 mem_avoid
[2].start
= cmd_line
;
182 mem_avoid
[2].size
= cmd_line_size
;
184 /* Avoid heap memory. */
185 mem_avoid
[3].start
= (unsigned long)free_mem_ptr
;
186 mem_avoid
[3].size
= BOOT_HEAP_SIZE
;
188 /* Avoid stack memory. */
189 mem_avoid
[4].start
= (unsigned long)free_mem_end_ptr
;
190 mem_avoid
[4].size
= BOOT_STACK_SIZE
;
193 /* Does this memory vector overlap a known avoided area? */
194 static bool mem_avoid_overlap(struct mem_vector
*img
)
197 struct setup_data
*ptr
;
199 for (i
= 0; i
< MEM_AVOID_MAX
; i
++) {
200 if (mem_overlaps(img
, &mem_avoid
[i
]))
204 /* Avoid all entries in the setup_data linked list. */
205 ptr
= (struct setup_data
*)(unsigned long)boot_params
->hdr
.setup_data
;
207 struct mem_vector avoid
;
209 avoid
.start
= (unsigned long)ptr
;
210 avoid
.size
= sizeof(*ptr
) + ptr
->len
;
212 if (mem_overlaps(img
, &avoid
))
215 ptr
= (struct setup_data
*)(unsigned long)ptr
->next
;
221 static unsigned long slots
[KERNEL_IMAGE_SIZE
/ CONFIG_PHYSICAL_ALIGN
];
222 static unsigned long slot_max
;
224 static void slots_append(unsigned long addr
)
226 /* Overflowing the slots list should be impossible. */
227 if (slot_max
>= KERNEL_IMAGE_SIZE
/ CONFIG_PHYSICAL_ALIGN
)
230 slots
[slot_max
++] = addr
;
233 static unsigned long slots_fetch_random(void)
235 /* Handle case of no slots stored. */
239 return slots
[get_random_long() % slot_max
];
242 static void process_e820_entry(struct e820entry
*entry
,
243 unsigned long minimum
,
244 unsigned long image_size
)
246 struct mem_vector region
, img
;
248 /* Skip non-RAM entries. */
249 if (entry
->type
!= E820_RAM
)
252 /* Ignore entries entirely above our maximum. */
253 if (entry
->addr
>= KERNEL_IMAGE_SIZE
)
256 /* Ignore entries entirely below our minimum. */
257 if (entry
->addr
+ entry
->size
< minimum
)
260 region
.start
= entry
->addr
;
261 region
.size
= entry
->size
;
263 /* Potentially raise address to minimum location. */
264 if (region
.start
< minimum
)
265 region
.start
= minimum
;
267 /* Potentially raise address to meet alignment requirements. */
268 region
.start
= ALIGN(region
.start
, CONFIG_PHYSICAL_ALIGN
);
270 /* Did we raise the address above the bounds of this e820 region? */
271 if (region
.start
> entry
->addr
+ entry
->size
)
274 /* Reduce size by any delta from the original address. */
275 region
.size
-= region
.start
- entry
->addr
;
277 /* Reduce maximum size to fit end of image within maximum limit. */
278 if (region
.start
+ region
.size
> KERNEL_IMAGE_SIZE
)
279 region
.size
= KERNEL_IMAGE_SIZE
- region
.start
;
281 /* Walk each aligned slot and check for avoided areas. */
282 for (img
.start
= region
.start
, img
.size
= image_size
;
283 mem_contains(®ion
, &img
) ;
284 img
.start
+= CONFIG_PHYSICAL_ALIGN
) {
285 if (mem_avoid_overlap(&img
))
287 slots_append(img
.start
);
291 static unsigned long find_random_addr(unsigned long minimum
,
297 /* Make sure minimum is aligned. */
298 minimum
= ALIGN(minimum
, CONFIG_PHYSICAL_ALIGN
);
300 /* Verify potential e820 positions, appending to slots list. */
301 for (i
= 0; i
< boot_params
->e820_entries
; i
++) {
302 process_e820_entry(&boot_params
->e820_map
[i
], minimum
, size
);
305 return slots_fetch_random();
308 unsigned char *choose_random_location(unsigned char *input
,
309 unsigned long input_size
,
310 unsigned char *output
,
311 unsigned long output_size
)
313 unsigned long choice
= (unsigned long)output
;
314 unsigned long random_addr
;
316 #ifdef CONFIG_HIBERNATION
317 if (!cmdline_find_option_bool("kaslr")) {
318 warn("KASLR disabled: 'kaslr' not on cmdline (hibernation selected).");
322 if (cmdline_find_option_bool("nokaslr")) {
323 warn("KASLR disabled: 'nokaslr' on cmdline.");
328 boot_params
->hdr
.loadflags
|= KASLR_FLAG
;
330 /* Record the various known unsafe memory ranges. */
331 mem_avoid_init((unsigned long)input
, input_size
,
332 (unsigned long)output
, output_size
);
334 /* Walk e820 and find a random address. */
335 random_addr
= find_random_addr(choice
, output_size
);
337 warn("KASLR disabled: could not find suitable E820 region!");
341 /* Always enforce the minimum. */
342 if (random_addr
< choice
)
345 choice
= random_addr
;
347 return (unsigned char *)choice
;