kmsan-checks.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * KMSAN checks to be used for one-off annotations in subsystems.
  4. *
  5. * Copyright (C) 2017-2022 Google LLC
  6. * Author: Alexander Potapenko <[email protected]>
  7. *
  8. */
  9. #ifndef _LINUX_KMSAN_CHECKS_H
  10. #define _LINUX_KMSAN_CHECKS_H
  11. #include <linux/types.h>
  12. #ifdef CONFIG_KMSAN
  13. /**
  14. * kmsan_poison_memory() - Mark the memory range as uninitialized.
  15. * @address: address to start with.
  16. * @size: size of buffer to poison.
  17. * @flags: GFP flags for allocations done by this function.
  18. *
  19. * Until other data is written to this range, KMSAN will treat it as
  20. * uninitialized. Error reports for this memory will reference the call site of
  21. * kmsan_poison_memory() as origin.
  22. */
  23. void kmsan_poison_memory(const void *address, size_t size, gfp_t flags);
  24. /**
  25. * kmsan_unpoison_memory() - Mark the memory range as initialized.
  26. * @address: address to start with.
  27. * @size: size of buffer to unpoison.
  28. *
  29. * Until other data is written to this range, KMSAN will treat it as
  30. * initialized.
  31. */
  32. void kmsan_unpoison_memory(const void *address, size_t size);
  33. /**
  34. * kmsan_check_memory() - Check the memory range for being initialized.
  35. * @address: address to start with.
  36. * @size: size of buffer to check.
  37. *
  38. * If any piece of the given range is marked as uninitialized, KMSAN will report
  39. * an error.
  40. */
  41. void kmsan_check_memory(const void *address, size_t size);
  42. /**
  43. * kmsan_copy_to_user() - Notify KMSAN about a data transfer to userspace.
  44. * @to: destination address in the userspace.
  45. * @from: source address in the kernel.
  46. * @to_copy: number of bytes to copy.
  47. * @left: number of bytes not copied.
  48. *
  49. * If this is a real userspace data transfer, KMSAN checks the bytes that were
  50. * actually copied to ensure there was no information leak. If @to belongs to
  51. * the kernel space (which is possible for compat syscalls), KMSAN just copies
  52. * the metadata.
  53. */
  54. void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy,
  55. size_t left);
  56. #else
  57. static inline void kmsan_poison_memory(const void *address, size_t size,
  58. gfp_t flags)
  59. {
  60. }
  61. static inline void kmsan_unpoison_memory(const void *address, size_t size)
  62. {
  63. }
  64. static inline void kmsan_check_memory(const void *address, size_t size)
  65. {
  66. }
  67. static inline void kmsan_copy_to_user(void __user *to, const void *from,
  68. size_t to_copy, size_t left)
  69. {
  70. }
  71. #endif
  72. #endif /* _LINUX_KMSAN_CHECKS_H */