kfence.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(addr) ((u8)0xaa ^ (u8)((unsigned long)(addr) & 0x7))
  21. /* Maximum stack depth for reports. */
  22. #define KFENCE_STACK_DEPTH 64
  23. /* KFENCE object states. */
  24. enum kfence_object_state {
  25. KFENCE_OBJECT_UNUSED, /* Object is unused. */
  26. KFENCE_OBJECT_ALLOCATED, /* Object is currently allocated. */
  27. KFENCE_OBJECT_FREED, /* Object was allocated, and then freed. */
  28. };
  29. /* Alloc/free tracking information. */
  30. struct kfence_track {
  31. pid_t pid;
  32. int cpu;
  33. u64 ts_nsec;
  34. int num_stack_entries;
  35. unsigned long stack_entries[KFENCE_STACK_DEPTH];
  36. };
  37. /* KFENCE metadata per guarded allocation. */
  38. struct kfence_metadata {
  39. struct list_head list; /* Freelist node; access under kfence_freelist_lock. */
  40. struct rcu_head rcu_head; /* For delayed freeing. */
  41. /*
  42. * Lock protecting below data; to ensure consistency of the below data,
  43. * since the following may execute concurrently: __kfence_alloc(),
  44. * __kfence_free(), kfence_handle_page_fault(). However, note that we
  45. * cannot grab the same metadata off the freelist twice, and multiple
  46. * __kfence_alloc() cannot run concurrently on the same metadata.
  47. */
  48. raw_spinlock_t lock;
  49. /* The current state of the object; see above. */
  50. enum kfence_object_state state;
  51. /*
  52. * Allocated object address; cannot be calculated from size, because of
  53. * alignment requirements.
  54. *
  55. * Invariant: ALIGN_DOWN(addr, PAGE_SIZE) is constant.
  56. */
  57. unsigned long addr;
  58. /*
  59. * The size of the original allocation.
  60. */
  61. size_t size;
  62. /*
  63. * The kmem_cache cache of the last allocation; NULL if never allocated
  64. * or the cache has already been destroyed.
  65. */
  66. struct kmem_cache *cache;
  67. /*
  68. * In case of an invalid access, the page that was unprotected; we
  69. * optimistically only store one address.
  70. */
  71. unsigned long unprotected_page;
  72. /* Allocation and free stack information. */
  73. struct kfence_track alloc_track;
  74. struct kfence_track free_track;
  75. /* For updating alloc_covered on frees. */
  76. u32 alloc_stack_hash;
  77. #ifdef CONFIG_MEMCG
  78. struct obj_cgroup *objcg;
  79. #endif
  80. };
  81. extern struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS];
  82. static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)
  83. {
  84. long index;
  85. /* The checks do not affect performance; only called from slow-paths. */
  86. if (!is_kfence_address((void *)addr))
  87. return NULL;
  88. /*
  89. * May be an invalid index if called with an address at the edge of
  90. * __kfence_pool, in which case we would report an "invalid access"
  91. * error.
  92. */
  93. index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;
  94. if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)
  95. return NULL;
  96. return &kfence_metadata[index];
  97. }
  98. /* KFENCE error types for report generation. */
  99. enum kfence_error_type {
  100. KFENCE_ERROR_OOB, /* Detected a out-of-bounds access. */
  101. KFENCE_ERROR_UAF, /* Detected a use-after-free access. */
  102. KFENCE_ERROR_CORRUPTION, /* Detected a memory corruption on free. */
  103. KFENCE_ERROR_INVALID, /* Invalid access of unknown type. */
  104. KFENCE_ERROR_INVALID_FREE, /* Invalid free. */
  105. };
  106. void kfence_report_error(unsigned long address, bool is_write, struct pt_regs *regs,
  107. const struct kfence_metadata *meta, enum kfence_error_type type);
  108. void kfence_print_object(struct seq_file *seq, const struct kfence_metadata *meta);
  109. #endif /* MM_KFENCE_KFENCE_H */