kasan_def.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * arch/arm/include/asm/kasan_def.h
  4. *
  5. * Copyright (c) 2018 Huawei Technologies Co., Ltd.
  6. *
  7. * Author: Abbott Liu <[email protected]>
  8. */
  9. #ifndef __ASM_KASAN_DEF_H
  10. #define __ASM_KASAN_DEF_H
  11. #ifdef CONFIG_KASAN
  12. /*
  13. * Define KASAN_SHADOW_OFFSET,KASAN_SHADOW_START and KASAN_SHADOW_END for
  14. * the Arm kernel address sanitizer. We are "stealing" lowmem (the 4GB
  15. * addressable by a 32bit architecture) out of the virtual address
  16. * space to use as shadow memory for KASan as follows:
  17. *
  18. * +----+ 0xffffffff
  19. * | | \
  20. * | | |-> Static kernel image (vmlinux) BSS and page table
  21. * | |/
  22. * +----+ PAGE_OFFSET
  23. * | | \
  24. * | | |-> Loadable kernel modules virtual address space area
  25. * | |/
  26. * +----+ MODULES_VADDR = KASAN_SHADOW_END
  27. * | | \
  28. * | | |-> The shadow area of kernel virtual address.
  29. * | |/
  30. * +----+-> TASK_SIZE (start of kernel space) = KASAN_SHADOW_START the
  31. * | |\ shadow address of MODULES_VADDR
  32. * | | |
  33. * | | |
  34. * | | |-> The user space area in lowmem. The kernel address
  35. * | | | sanitizer do not use this space, nor does it map it.
  36. * | | |
  37. * | | |
  38. * | | |
  39. * | | |
  40. * | |/
  41. * ------ 0
  42. *
  43. * 1) KASAN_SHADOW_START
  44. * This value begins with the MODULE_VADDR's shadow address. It is the
  45. * start of kernel virtual space. Since we have modules to load, we need
  46. * to cover also that area with shadow memory so we can find memory
  47. * bugs in modules.
  48. *
  49. * 2) KASAN_SHADOW_END
  50. * This value is the 0x100000000's shadow address: the mapping that would
  51. * be after the end of the kernel memory at 0xffffffff. It is the end of
  52. * kernel address sanitizer shadow area. It is also the start of the
  53. * module area.
  54. *
  55. * 3) KASAN_SHADOW_OFFSET:
  56. * This value is used to map an address to the corresponding shadow
  57. * address by the following formula:
  58. *
  59. * shadow_addr = (address >> 3) + KASAN_SHADOW_OFFSET;
  60. *
  61. * As you would expect, >> 3 is equal to dividing by 8, meaning each
  62. * byte in the shadow memory covers 8 bytes of kernel memory, so one
  63. * bit shadow memory per byte of kernel memory is used.
  64. *
  65. * The KASAN_SHADOW_OFFSET is provided in a Kconfig option depending
  66. * on the VMSPLIT layout of the system: the kernel and userspace can
  67. * split up lowmem in different ways according to needs, so we calculate
  68. * the shadow offset depending on this.
  69. */
  70. #define KASAN_SHADOW_SCALE_SHIFT 3
  71. #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
  72. #define KASAN_SHADOW_END ((UL(1) << (32 - KASAN_SHADOW_SCALE_SHIFT)) \
  73. + KASAN_SHADOW_OFFSET)
  74. #define KASAN_SHADOW_START ((KASAN_SHADOW_END >> 3) + KASAN_SHADOW_OFFSET)
  75. #endif
  76. #endif