kfence.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Kernel Electric-Fence (KFENCE). For more info please see
  4. * Documentation/dev-tools/kfence.rst.
  5. *
  6. * Copyright (C) 2020, Google LLC.
  7. */
  8. #ifndef MM_KFENCE_KFENCE_H
  9. #define MM_KFENCE_KFENCE_H
  10. #include <linux/mm.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/types.h>
  14. #include "../slab.h" /* for struct kmem_cache */
  15. /*
  16. * Get the canary byte pattern for @addr. Use a pattern that varies based on the
  17. * lower 3 bits of the address, to detect memory corruptions with higher
  18. * probability, where similar constants are used.
  19. */
  20. #define KFENCE_CANARY_PATTERN_U8(addr) ((u8)0xaa ^ (u8)((unsigned long)(addr) & 0x7))
  21. /*
  22. * Define a continuous 8-byte canary starting from a multiple of 8. The canary
  23. * of each byte is only related to the lowest three bits of its address, so the
  24. * canary of every 8 bytes is the same. 64-bit memory can be filled and checked
  25. * at a time instead of byte by byte to improve performance.
  26. */
  27. #define KFENCE_CANARY_PATTERN_U64 ((u64)0xaaaaaaaaaaaaaaaa ^ (u64)(le64_to_cpu(0x0706050403020100)))
  28. /* Maximum stack depth for reports. */
  29. #define KFENCE_STACK_DEPTH 64
  30. extern raw_spinlock_t kfence_freelist_lock;
  31. /* KFENCE object states. */
  32. enum kfence_object_state {
  33. KFENCE_OBJECT_UNUSED, /* Object is unused. */
  34. KFENCE_OBJECT_ALLOCATED, /* Object is currently allocated. */
  35. KFENCE_OBJECT_RCU_FREEING, /* Object was allocated, and then being freed by rcu. */
  36. KFENCE_OBJECT_FREED, /* Object was allocated, and then freed. */
  37. };
  38. /* Alloc/free tracking information. */
  39. struct kfence_track {
  40. pid_t pid;
  41. int cpu;
  42. u64 ts_nsec;
  43. int num_stack_entries;
  44. unsigned long stack_entries[KFENCE_STACK_DEPTH];
  45. };
  46. /* KFENCE metadata per guarded allocation. */
  47. struct kfence_metadata {
  48. struct list_head list __guarded_by(&kfence_freelist_lock); /* Freelist node. */
  49. struct rcu_head rcu_head; /* For delayed freeing. */
  50. /*
  51. * Lock protecting below data; to ensure consistency of the below data,
  52. * since the following may execute concurrently: __kfence_alloc(),
  53. * __kfence_free(), kfence_handle_page_fault(). However, note that we
  54. * cannot grab the same metadata off the freelist twice, and multiple
  55. * __kfence_alloc() cannot run concurrently on the same metadata.
  56. */
  57. raw_spinlock_t lock;
  58. /* The current state of the object; see above. */
  59. enum kfence_object_state state;
  60. /*
  61. * Allocated object address; cannot be calculated from size, because of
  62. * alignment requirements.
  63. *
  64. * Invariant: ALIGN_DOWN(addr, PAGE_SIZE) is constant.
  65. */
  66. unsigned long addr;
  67. /*
  68. * The size of the original allocation.
  69. */
  70. size_t size;
  71. /*
  72. * The kmem_cache cache of the last allocation; NULL if never allocated
  73. * or the cache has already been destroyed.
  74. */
  75. struct kmem_cache *cache;
  76. /*
  77. * In case of an invalid access, the page that was unprotected; we
  78. * optimistically only store one address.
  79. */
  80. unsigned long unprotected_page __guarded_by(&lock);
  81. /* Allocation and free stack information. */
  82. struct kfence_track alloc_track __guarded_by(&lock);
  83. struct kfence_track free_track __guarded_by(&lock);
  84. /* For updating alloc_covered on frees. */
  85. u32 alloc_stack_hash __guarded_by(&lock);
  86. #ifdef CONFIG_MEMCG
  87. struct slabobj_ext obj_exts;
  88. #endif
  89. };
  90. #define KFENCE_METADATA_SIZE PAGE_ALIGN(sizeof(struct kfence_metadata) * \
  91. CONFIG_KFENCE_NUM_OBJECTS)
  92. extern struct kfence_metadata *kfence_metadata;
  93. static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)
  94. {
  95. long index;
  96. /* The checks do not affect performance; only called from slow-paths. */
  97. if (!is_kfence_address((void *)addr))
  98. return NULL;
  99. /*
  100. * May be an invalid index if called with an address at the edge of
  101. * __kfence_pool, in which case we would report an "invalid access"
  102. * error.
  103. */
  104. index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;
  105. if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)
  106. return NULL;
  107. return &kfence_metadata[index];
  108. }
  109. /* KFENCE error types for report generation. */
  110. enum kfence_error_type {
  111. KFENCE_ERROR_OOB, /* Detected a out-of-bounds access. */
  112. KFENCE_ERROR_UAF, /* Detected a use-after-free access. */
  113. KFENCE_ERROR_CORRUPTION, /* Detected a memory corruption on free. */
  114. KFENCE_ERROR_INVALID, /* Invalid access of unknown type. */
  115. KFENCE_ERROR_INVALID_FREE, /* Invalid free. */
  116. };
  117. void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *regs,
  118. const struct kfence_metadata *meta, enum kfence_error_type type);
  119. void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta) __must_hold(&meta->lock);
  120. #endif /* MM_KFENCE_KFENCE_H */