aslr.c 8.3 KB

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