instrumented.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This header provides generic wrappers for memory access instrumentation that
  4. * the compiler cannot emit for: KASAN, KCSAN, KMSAN.
  5. */
  6. #ifndef _LINUX_INSTRUMENTED_H
  7. #define _LINUX_INSTRUMENTED_H
  8. #include <linux/compiler.h>
  9. #include <linux/kasan-checks.h>
  10. #include <linux/kcsan-checks.h>
  11. #include <linux/kmsan-checks.h>
  12. #include <linux/types.h>
  13. /**
  14. * instrument_read - instrument regular read access
  15. *
  16. * Instrument a regular read access. The instrumentation should be inserted
  17. * before the actual read happens.
  18. *
  19. * @ptr address of access
  20. * @size size of access
  21. */
  22. static __always_inline void instrument_read(const volatile void *v, size_t size)
  23. {
  24. kasan_check_read(v, size);
  25. kcsan_check_read(v, size);
  26. }
  27. /**
  28. * instrument_write - instrument regular write access
  29. *
  30. * Instrument a regular write access. The instrumentation should be inserted
  31. * before the actual write happens.
  32. *
  33. * @ptr address of access
  34. * @size size of access
  35. */
  36. static __always_inline void instrument_write(const volatile void *v, size_t size)
  37. {
  38. kasan_check_write(v, size);
  39. kcsan_check_write(v, size);
  40. }
  41. /**
  42. * instrument_read_write - instrument regular read-write access
  43. *
  44. * Instrument a regular write access. The instrumentation should be inserted
  45. * before the actual write happens.
  46. *
  47. * @ptr address of access
  48. * @size size of access
  49. */
  50. static __always_inline void instrument_read_write(const volatile void *v, size_t size)
  51. {
  52. kasan_check_write(v, size);
  53. kcsan_check_read_write(v, size);
  54. }
  55. /**
  56. * instrument_atomic_read - instrument atomic read access
  57. *
  58. * Instrument an atomic read access. The instrumentation should be inserted
  59. * before the actual read happens.
  60. *
  61. * @ptr address of access
  62. * @size size of access
  63. */
  64. static __always_inline void instrument_atomic_read(const volatile void *v, size_t size)
  65. {
  66. kasan_check_read(v, size);
  67. kcsan_check_atomic_read(v, size);
  68. }
  69. /**
  70. * instrument_atomic_write - instrument atomic write access
  71. *
  72. * Instrument an atomic write access. The instrumentation should be inserted
  73. * before the actual write happens.
  74. *
  75. * @ptr address of access
  76. * @size size of access
  77. */
  78. static __always_inline void instrument_atomic_write(const volatile void *v, size_t size)
  79. {
  80. kasan_check_write(v, size);
  81. kcsan_check_atomic_write(v, size);
  82. }
  83. /**
  84. * instrument_atomic_read_write - instrument atomic read-write access
  85. *
  86. * Instrument an atomic read-write access. The instrumentation should be
  87. * inserted before the actual write happens.
  88. *
  89. * @ptr address of access
  90. * @size size of access
  91. */
  92. static __always_inline void instrument_atomic_read_write(const volatile void *v, size_t size)
  93. {
  94. kasan_check_write(v, size);
  95. kcsan_check_atomic_read_write(v, size);
  96. }
  97. /**
  98. * instrument_copy_to_user - instrument reads of copy_to_user
  99. *
  100. * Instrument reads from kernel memory, that are due to copy_to_user (and
  101. * variants). The instrumentation must be inserted before the accesses.
  102. *
  103. * @to destination address
  104. * @from source address
  105. * @n number of bytes to copy
  106. */
  107. static __always_inline void
  108. instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
  109. {
  110. kasan_check_read(from, n);
  111. kcsan_check_read(from, n);
  112. kmsan_copy_to_user(to, from, n, 0);
  113. }
  114. /**
  115. * instrument_copy_from_user_before - add instrumentation before copy_from_user
  116. *
  117. * Instrument writes to kernel memory, that are due to copy_from_user (and
  118. * variants). The instrumentation should be inserted before the accesses.
  119. *
  120. * @to destination address
  121. * @from source address
  122. * @n number of bytes to copy
  123. */
  124. static __always_inline void
  125. instrument_copy_from_user_before(const void *to, const void __user *from, unsigned long n)
  126. {
  127. kasan_check_write(to, n);
  128. kcsan_check_write(to, n);
  129. }
  130. /**
  131. * instrument_copy_from_user_after - add instrumentation after copy_from_user
  132. *
  133. * Instrument writes to kernel memory, that are due to copy_from_user (and
  134. * variants). The instrumentation should be inserted after the accesses.
  135. *
  136. * @to destination address
  137. * @from source address
  138. * @n number of bytes to copy
  139. * @left number of bytes not copied (as returned by copy_from_user)
  140. */
  141. static __always_inline void
  142. instrument_copy_from_user_after(const void *to, const void __user *from,
  143. unsigned long n, unsigned long left)
  144. {
  145. kmsan_unpoison_memory(to, n - left);
  146. }
  147. /**
  148. * instrument_get_user() - add instrumentation to get_user()-like macros
  149. *
  150. * get_user() and friends are fragile, so it may depend on the implementation
  151. * whether the instrumentation happens before or after the data is copied from
  152. * the userspace.
  153. *
  154. * @to destination variable, may not be address-taken
  155. */
  156. #define instrument_get_user(to) \
  157. ({ \
  158. u64 __tmp = (u64)(to); \
  159. kmsan_unpoison_memory(&__tmp, sizeof(__tmp)); \
  160. to = __tmp; \
  161. })
  162. /**
  163. * instrument_put_user() - add instrumentation to put_user()-like macros
  164. *
  165. * put_user() and friends are fragile, so it may depend on the implementation
  166. * whether the instrumentation happens before or after the data is copied from
  167. * the userspace.
  168. *
  169. * @from source address
  170. * @ptr userspace pointer to copy to
  171. * @size number of bytes to copy
  172. */
  173. #define instrument_put_user(from, ptr, size) \
  174. ({ \
  175. kmsan_copy_to_user(ptr, &from, sizeof(from), 0); \
  176. })
  177. #endif /* _LINUX_INSTRUMENTED_H */