kasan.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef __MM_KASAN_KASAN_H
  2. #define __MM_KASAN_KASAN_H
  3. #include <linux/kasan.h>
  4. #include <linux/stackdepot.h>
  5. #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
  6. #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
  7. #define KASAN_FREE_PAGE 0xFF /* page was freed */
  8. #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
  9. #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
  10. #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
  11. #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
  12. /*
  13. * Stack redzone shadow values
  14. * (Those are compiler's ABI, don't change them)
  15. */
  16. #define KASAN_STACK_LEFT 0xF1
  17. #define KASAN_STACK_MID 0xF2
  18. #define KASAN_STACK_RIGHT 0xF3
  19. #define KASAN_STACK_PARTIAL 0xF4
  20. #define KASAN_USE_AFTER_SCOPE 0xF8
  21. /* Don't break randconfig/all*config builds */
  22. #ifndef KASAN_ABI_VERSION
  23. #define KASAN_ABI_VERSION 1
  24. #endif
  25. struct kasan_access_info {
  26. const void *access_addr;
  27. const void *first_bad_addr;
  28. size_t access_size;
  29. bool is_write;
  30. unsigned long ip;
  31. };
  32. /* The layout of struct dictated by compiler */
  33. struct kasan_source_location {
  34. const char *filename;
  35. int line_no;
  36. int column_no;
  37. };
  38. /* The layout of struct dictated by compiler */
  39. struct kasan_global {
  40. const void *beg; /* Address of the beginning of the global variable. */
  41. size_t size; /* Size of the global variable. */
  42. size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
  43. const void *name;
  44. const void *module_name; /* Name of the module where the global variable is declared. */
  45. unsigned long has_dynamic_init; /* This needed for C++ */
  46. #if KASAN_ABI_VERSION >= 4
  47. struct kasan_source_location *location;
  48. #endif
  49. #if KASAN_ABI_VERSION >= 5
  50. char *odr_indicator;
  51. #endif
  52. };
  53. /**
  54. * Structures to keep alloc and free tracks *
  55. */
  56. #define KASAN_STACK_DEPTH 64
  57. struct kasan_track {
  58. u32 pid;
  59. depot_stack_handle_t stack;
  60. };
  61. struct kasan_alloc_meta {
  62. struct kasan_track alloc_track;
  63. struct kasan_track free_track;
  64. };
  65. struct qlist_node {
  66. struct qlist_node *next;
  67. };
  68. struct kasan_free_meta {
  69. /* This field is used while the object is in the quarantine.
  70. * Otherwise it might be used for the allocator freelist.
  71. */
  72. struct qlist_node quarantine_link;
  73. };
  74. struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
  75. const void *object);
  76. struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
  77. const void *object);
  78. static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
  79. {
  80. return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
  81. << KASAN_SHADOW_SCALE_SHIFT);
  82. }
  83. static inline bool kasan_report_enabled(void)
  84. {
  85. return !current->kasan_depth;
  86. }
  87. void kasan_report(unsigned long addr, size_t size,
  88. bool is_write, unsigned long ip);
  89. void kasan_report_double_free(struct kmem_cache *cache, void *object,
  90. s8 shadow);
  91. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
  92. void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
  93. void quarantine_reduce(void);
  94. void quarantine_remove_cache(struct kmem_cache *cache);
  95. #else
  96. static inline void quarantine_put(struct kasan_free_meta *info,
  97. struct kmem_cache *cache) { }
  98. static inline void quarantine_reduce(void) { }
  99. static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
  100. #endif
  101. #endif