kaslr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Linaro Ltd <[email protected]>
  4. */
  5. #include <linux/cache.h>
  6. #include <linux/crc32.h>
  7. #include <linux/init.h>
  8. #include <linux/libfdt.h>
  9. #include <linux/mm_types.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <linux/pgtable.h>
  13. #include <linux/random.h>
  14. #include <asm/fixmap.h>
  15. #include <asm/kernel-pgtable.h>
  16. #include <asm/memory.h>
  17. #include <asm/mmu.h>
  18. #include <asm/sections.h>
  19. #include <asm/setup.h>
  20. u64 __ro_after_init module_alloc_base;
  21. u16 __initdata memstart_offset_seed;
  22. struct arm64_ftr_override kaslr_feature_override __initdata;
  23. static int __init kaslr_init(void)
  24. {
  25. u64 module_range;
  26. u32 seed;
  27. /*
  28. * Set a reasonable default for module_alloc_base in case
  29. * we end up running with module randomization disabled.
  30. */
  31. module_alloc_base = (u64)_etext - MODULES_VSIZE;
  32. if (kaslr_feature_override.val & kaslr_feature_override.mask & 0xf) {
  33. pr_info("KASLR disabled on command line\n");
  34. return 0;
  35. }
  36. if (!kaslr_offset()) {
  37. pr_warn("KASLR disabled due to lack of seed\n");
  38. return 0;
  39. }
  40. pr_info("KASLR enabled\n");
  41. /*
  42. * KASAN without KASAN_VMALLOC does not expect the module region to
  43. * intersect the vmalloc region, since shadow memory is allocated for
  44. * each module at load time, whereas the vmalloc region will already be
  45. * shadowed by KASAN zero pages.
  46. */
  47. BUILD_BUG_ON((IS_ENABLED(CONFIG_KASAN_GENERIC) ||
  48. IS_ENABLED(CONFIG_KASAN_SW_TAGS)) &&
  49. !IS_ENABLED(CONFIG_KASAN_VMALLOC));
  50. seed = get_random_u32();
  51. if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) {
  52. /*
  53. * Randomize the module region over a 2 GB window covering the
  54. * kernel. This reduces the risk of modules leaking information
  55. * about the address of the kernel itself, but results in
  56. * branches between modules and the core kernel that are
  57. * resolved via PLTs. (Branches between modules will be
  58. * resolved normally.)
  59. */
  60. module_range = SZ_2G - (u64)(_end - _stext);
  61. module_alloc_base = max((u64)_end - SZ_2G, (u64)MODULES_VADDR);
  62. } else {
  63. /*
  64. * Randomize the module region by setting module_alloc_base to
  65. * a PAGE_SIZE multiple in the range [_etext - MODULES_VSIZE,
  66. * _stext) . This guarantees that the resulting region still
  67. * covers [_stext, _etext], and that all relative branches can
  68. * be resolved without veneers unless this region is exhausted
  69. * and we fall back to a larger 2GB window in module_alloc()
  70. * when ARM64_MODULE_PLTS is enabled.
  71. */
  72. module_range = MODULES_VSIZE - (u64)(_etext - _stext);
  73. }
  74. /* use the lower 21 bits to randomize the base of the module region */
  75. module_alloc_base += (module_range * (seed & ((1 << 21) - 1))) >> 21;
  76. module_alloc_base &= PAGE_MASK;
  77. return 0;
  78. }
  79. subsys_initcall(kaslr_init)