kmsan.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Functions used by the KMSAN runtime.
  4. *
  5. * Copyright (C) 2017-2022 Google LLC
  6. * Author: Alexander Potapenko <glider@google.com>
  7. *
  8. */
  9. #ifndef __MM_KMSAN_KMSAN_H
  10. #define __MM_KMSAN_KMSAN_H
  11. #include <linux/irqflags.h>
  12. #include <linux/kmsan.h>
  13. #include <linux/mm.h>
  14. #include <linux/nmi.h>
  15. #include <linux/pgtable.h>
  16. #include <linux/printk.h>
  17. #include <linux/sched.h>
  18. #include <linux/stackdepot.h>
  19. #include <linux/stacktrace.h>
  20. #define KMSAN_ALLOCA_MAGIC_ORIGIN 0xabcd0100
  21. #define KMSAN_CHAIN_MAGIC_ORIGIN 0xabcd0200
  22. #define KMSAN_POISON_NOCHECK 0x0
  23. #define KMSAN_POISON_CHECK 0x1
  24. #define KMSAN_POISON_FREE 0x2
  25. #define KMSAN_ORIGIN_SIZE 4
  26. #define KMSAN_MAX_ORIGIN_DEPTH 7
  27. #define KMSAN_STACK_DEPTH 64
  28. #define KMSAN_META_SHADOW (false)
  29. #define KMSAN_META_ORIGIN (true)
  30. /*
  31. * A pair of metadata pointers to be returned by the instrumentation functions.
  32. */
  33. struct shadow_origin_ptr {
  34. void *shadow, *origin;
  35. };
  36. struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *addr, u64 size,
  37. bool store);
  38. void __init kmsan_init_alloc_meta_for_range(void *start, void *end);
  39. enum kmsan_bug_reason {
  40. REASON_ANY,
  41. REASON_COPY_TO_USER,
  42. REASON_SUBMIT_URB,
  43. };
  44. void kmsan_print_origin(depot_stack_handle_t origin);
  45. /**
  46. * kmsan_report() - Report a use of uninitialized value.
  47. * @origin: Stack ID of the uninitialized value.
  48. * @address: Address at which the memory access happens.
  49. * @size: Memory access size.
  50. * @off_first: Offset (from @address) of the first byte to be reported.
  51. * @off_last: Offset (from @address) of the last byte to be reported.
  52. * @user_addr: When non-NULL, denotes the userspace address to which the kernel
  53. * is leaking data.
  54. * @reason: Error type from enum kmsan_bug_reason.
  55. *
  56. * kmsan_report() prints an error message for a consequent group of bytes
  57. * sharing the same origin. If an uninitialized value is used in a comparison,
  58. * this function is called once without specifying the addresses. When checking
  59. * a memory range, KMSAN may call kmsan_report() multiple times with the same
  60. * @address, @size, @user_addr and @reason, but different @off_first and
  61. * @off_last corresponding to different @origin values.
  62. */
  63. void kmsan_report(depot_stack_handle_t origin, void *address, int size,
  64. int off_first, int off_last, const void __user *user_addr,
  65. enum kmsan_bug_reason reason);
  66. DECLARE_PER_CPU(struct kmsan_ctx, kmsan_percpu_ctx);
  67. static __always_inline struct kmsan_ctx *kmsan_get_context(void)
  68. {
  69. return in_task() ? &current->kmsan_ctx : raw_cpu_ptr(&kmsan_percpu_ctx);
  70. }
  71. /*
  72. * When a compiler hook or KMSAN runtime function is invoked, it may make a
  73. * call to instrumented code and eventually call itself recursively. To avoid
  74. * that, we guard the runtime entry regions with
  75. * kmsan_enter_runtime()/kmsan_leave_runtime() and exit the hook if
  76. * kmsan_in_runtime() is true.
  77. *
  78. * Non-runtime code may occasionally get executed in nested IRQs from the
  79. * runtime code (e.g. when called via smp_call_function_single()). Because some
  80. * KMSAN routines may take locks (e.g. for memory allocation), we conservatively
  81. * bail out instead of calling them. To minimize the effect of this (potentially
  82. * missing initialization events) kmsan_in_runtime() is not checked in
  83. * non-blocking runtime functions.
  84. */
  85. static __always_inline bool kmsan_in_runtime(void)
  86. {
  87. if ((hardirq_count() >> HARDIRQ_SHIFT) > 1)
  88. return true;
  89. if (in_nmi())
  90. return true;
  91. return kmsan_get_context()->kmsan_in_runtime;
  92. }
  93. static __always_inline void kmsan_enter_runtime(void)
  94. {
  95. struct kmsan_ctx *ctx;
  96. ctx = kmsan_get_context();
  97. KMSAN_WARN_ON(ctx->kmsan_in_runtime++);
  98. }
  99. static __always_inline void kmsan_leave_runtime(void)
  100. {
  101. struct kmsan_ctx *ctx = kmsan_get_context();
  102. KMSAN_WARN_ON(--ctx->kmsan_in_runtime);
  103. }
  104. depot_stack_handle_t kmsan_save_stack(void);
  105. depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags,
  106. unsigned int extra_bits);
  107. /*
  108. * Pack and unpack the origin chain depth and UAF flag to/from the extra bits
  109. * provided by the stack depot.
  110. * The UAF flag is stored in the lowest bit, followed by the depth in the upper
  111. * bits.
  112. * set_dsh_extra_bits() is responsible for clamping the value.
  113. */
  114. static __always_inline unsigned int kmsan_extra_bits(unsigned int depth,
  115. bool uaf)
  116. {
  117. return (depth << 1) | uaf;
  118. }
  119. static __always_inline bool kmsan_uaf_from_eb(unsigned int extra_bits)
  120. {
  121. return extra_bits & 1;
  122. }
  123. static __always_inline unsigned int kmsan_depth_from_eb(unsigned int extra_bits)
  124. {
  125. return extra_bits >> 1;
  126. }
  127. /*
  128. * kmsan_internal_ functions are supposed to be very simple and not require the
  129. * kmsan_in_runtime() checks.
  130. */
  131. void kmsan_internal_memmove_metadata(void *dst, void *src, size_t n);
  132. void kmsan_internal_poison_memory(void *address, size_t size, gfp_t flags,
  133. unsigned int poison_flags);
  134. void kmsan_internal_unpoison_memory(void *address, size_t size, bool checked);
  135. void kmsan_internal_set_shadow_origin(void *address, size_t size, int b,
  136. u32 origin, bool checked);
  137. depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id);
  138. void kmsan_internal_task_create(struct task_struct *task);
  139. bool kmsan_metadata_is_contiguous(void *addr, size_t size);
  140. void kmsan_internal_check_memory(void *addr, size_t size,
  141. const void __user *user_addr, int reason);
  142. struct page *kmsan_vmalloc_to_page_or_null(void *vaddr);
  143. void kmsan_setup_meta(struct page *page, struct page *shadow,
  144. struct page *origin, int order);
  145. /*
  146. * kmsan_internal_is_module_addr() and kmsan_internal_is_vmalloc_addr() are
  147. * non-instrumented versions of is_module_address() and is_vmalloc_addr() that
  148. * are safe to call from KMSAN runtime without recursion.
  149. */
  150. static inline bool kmsan_internal_is_module_addr(void *vaddr)
  151. {
  152. return ((u64)vaddr >= MODULES_VADDR) && ((u64)vaddr < MODULES_END);
  153. }
  154. static inline bool kmsan_internal_is_vmalloc_addr(void *addr)
  155. {
  156. return ((u64)addr >= VMALLOC_START) && ((u64)addr < VMALLOC_END);
  157. }
  158. #endif /* __MM_KMSAN_KMSAN_H */