kasan.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __MM_KASAN_KASAN_H
  3. #define __MM_KASAN_KASAN_H
  4. #include <linux/kasan.h>
  5. #include <linux/stackdepot.h>
  6. #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
  7. #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
  8. #define KASAN_FREE_PAGE 0xFF /* page was freed */
  9. #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
  10. #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
  11. #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
  12. #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
  13. /*
  14. * Stack redzone shadow values
  15. * (Those are compiler's ABI, don't change them)
  16. */
  17. #define KASAN_STACK_LEFT 0xF1
  18. #define KASAN_STACK_MID 0xF2
  19. #define KASAN_STACK_RIGHT 0xF3
  20. #define KASAN_STACK_PARTIAL 0xF4
  21. #define KASAN_USE_AFTER_SCOPE 0xF8
  22. /*
  23. * alloca redzone shadow values
  24. */
  25. #define KASAN_ALLOCA_LEFT 0xCA
  26. #define KASAN_ALLOCA_RIGHT 0xCB
  27. #define KASAN_ALLOCA_REDZONE_SIZE 32
  28. /* Don't break randconfig/all*config builds */
  29. #ifndef KASAN_ABI_VERSION
  30. #define KASAN_ABI_VERSION 1
  31. #endif
  32. struct kasan_access_info {
  33. const void *access_addr;
  34. const void *first_bad_addr;
  35. size_t access_size;
  36. bool is_write;
  37. unsigned long ip;
  38. };
  39. /* The layout of struct dictated by compiler */
  40. struct kasan_source_location {
  41. const char *filename;
  42. int line_no;
  43. int column_no;
  44. };
  45. /* The layout of struct dictated by compiler */
  46. struct kasan_global {
  47. const void *beg; /* Address of the beginning of the global variable. */
  48. size_t size; /* Size of the global variable. */
  49. size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
  50. const void *name;
  51. const void *module_name; /* Name of the module where the global variable is declared. */
  52. unsigned long has_dynamic_init; /* This needed for C++ */
  53. #if KASAN_ABI_VERSION >= 4
  54. struct kasan_source_location *location;
  55. #endif
  56. #if KASAN_ABI_VERSION >= 5
  57. char *odr_indicator;
  58. #endif
  59. };
  60. /**
  61. * Structures to keep alloc and free tracks *
  62. */
  63. #define KASAN_STACK_DEPTH 64
  64. struct kasan_track {
  65. u32 pid;
  66. depot_stack_handle_t stack;
  67. };
  68. struct kasan_alloc_meta {
  69. struct kasan_track alloc_track;
  70. struct kasan_track free_track;
  71. };
  72. struct qlist_node {
  73. struct qlist_node *next;
  74. };
  75. struct kasan_free_meta {
  76. /* This field is used while the object is in the quarantine.
  77. * Otherwise it might be used for the allocator freelist.
  78. */
  79. struct qlist_node quarantine_link;
  80. };
  81. struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
  82. const void *object);
  83. struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
  84. const void *object);
  85. static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
  86. {
  87. return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
  88. << KASAN_SHADOW_SCALE_SHIFT);
  89. }
  90. void kasan_report(unsigned long addr, size_t size,
  91. bool is_write, unsigned long ip);
  92. void kasan_report_invalid_free(void *object, unsigned long ip);
  93. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
  94. void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
  95. void quarantine_reduce(void);
  96. void quarantine_remove_cache(struct kmem_cache *cache);
  97. #else
  98. static inline void quarantine_put(struct kasan_free_meta *info,
  99. struct kmem_cache *cache) { }
  100. static inline void quarantine_reduce(void) { }
  101. static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
  102. #endif
  103. /*
  104. * Exported functions for interfaces called from assembly or from generated
  105. * code. Declarations here to avoid warning about missing declarations.
  106. */
  107. asmlinkage void kasan_unpoison_task_stack_below(const void *watermark);
  108. void __asan_register_globals(struct kasan_global *globals, size_t size);
  109. void __asan_unregister_globals(struct kasan_global *globals, size_t size);
  110. void __asan_loadN(unsigned long addr, size_t size);
  111. void __asan_storeN(unsigned long addr, size_t size);
  112. void __asan_handle_no_return(void);
  113. void __asan_poison_stack_memory(const void *addr, size_t size);
  114. void __asan_unpoison_stack_memory(const void *addr, size_t size);
  115. void __asan_alloca_poison(unsigned long addr, size_t size);
  116. void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom);
  117. void __asan_load1(unsigned long addr);
  118. void __asan_store1(unsigned long addr);
  119. void __asan_load2(unsigned long addr);
  120. void __asan_store2(unsigned long addr);
  121. void __asan_load4(unsigned long addr);
  122. void __asan_store4(unsigned long addr);
  123. void __asan_load8(unsigned long addr);
  124. void __asan_store8(unsigned long addr);
  125. void __asan_load16(unsigned long addr);
  126. void __asan_store16(unsigned long addr);
  127. void __asan_load1_noabort(unsigned long addr);
  128. void __asan_store1_noabort(unsigned long addr);
  129. void __asan_load2_noabort(unsigned long addr);
  130. void __asan_store2_noabort(unsigned long addr);
  131. void __asan_load4_noabort(unsigned long addr);
  132. void __asan_store4_noabort(unsigned long addr);
  133. void __asan_load8_noabort(unsigned long addr);
  134. void __asan_store8_noabort(unsigned long addr);
  135. void __asan_load16_noabort(unsigned long addr);
  136. void __asan_store16_noabort(unsigned long addr);
  137. void __asan_set_shadow_00(const void *addr, size_t size);
  138. void __asan_set_shadow_f1(const void *addr, size_t size);
  139. void __asan_set_shadow_f2(const void *addr, size_t size);
  140. void __asan_set_shadow_f3(const void *addr, size_t size);
  141. void __asan_set_shadow_f5(const void *addr, size_t size);
  142. void __asan_set_shadow_f8(const void *addr, size_t size);
  143. #endif