maccess.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Access kernel memory without faulting.
  3. */
  4. #include <linux/export.h>
  5. #include <linux/mm.h>
  6. #include <linux/uaccess.h>
  7. /**
  8. * probe_kernel_read(): safely attempt to read from a location
  9. * @dst: pointer to the buffer that shall take the data
  10. * @src: address to read from
  11. * @size: size of the data chunk
  12. *
  13. * Safely read from address @src to the buffer at @dst. If a kernel fault
  14. * happens, handle that and return -EFAULT.
  15. *
  16. * We ensure that the copy_from_user is executed in atomic context so that
  17. * do_page_fault() doesn't attempt to take mmap_sem. This makes
  18. * probe_kernel_read() suitable for use within regions where the caller
  19. * already holds mmap_sem, or other locks which nest inside mmap_sem.
  20. */
  21. long __weak probe_kernel_read(void *dst, const void *src, size_t size)
  22. __attribute__((alias("__probe_kernel_read")));
  23. long __probe_kernel_read(void *dst, const void *src, size_t size)
  24. {
  25. long ret;
  26. mm_segment_t old_fs = get_fs();
  27. set_fs(KERNEL_DS);
  28. pagefault_disable();
  29. current->kernel_uaccess_faults_ok++;
  30. ret = __copy_from_user_inatomic(dst,
  31. (__force const void __user *)src, size);
  32. current->kernel_uaccess_faults_ok--;
  33. pagefault_enable();
  34. set_fs(old_fs);
  35. return ret ? -EFAULT : 0;
  36. }
  37. EXPORT_SYMBOL_GPL(probe_kernel_read);
  38. /**
  39. * probe_kernel_write(): safely attempt to write to a location
  40. * @dst: address to write to
  41. * @src: pointer to the data that shall be written
  42. * @size: size of the data chunk
  43. *
  44. * Safely write to address @dst from the buffer at @src. If a kernel fault
  45. * happens, handle that and return -EFAULT.
  46. */
  47. long __weak probe_kernel_write(void *dst, const void *src, size_t size)
  48. __attribute__((alias("__probe_kernel_write")));
  49. long __probe_kernel_write(void *dst, const void *src, size_t size)
  50. {
  51. long ret;
  52. mm_segment_t old_fs = get_fs();
  53. set_fs(KERNEL_DS);
  54. pagefault_disable();
  55. current->kernel_uaccess_faults_ok++;
  56. ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
  57. current->kernel_uaccess_faults_ok--;
  58. pagefault_enable();
  59. set_fs(old_fs);
  60. return ret ? -EFAULT : 0;
  61. }
  62. EXPORT_SYMBOL_GPL(probe_kernel_write);
  63. /**
  64. * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
  65. * @dst: Destination address, in kernel space. This buffer must be at
  66. * least @count bytes long.
  67. * @unsafe_addr: Unsafe address.
  68. * @count: Maximum number of bytes to copy, including the trailing NUL.
  69. *
  70. * Copies a NUL-terminated string from unsafe address to kernel buffer.
  71. *
  72. * On success, returns the length of the string INCLUDING the trailing NUL.
  73. *
  74. * If access fails, returns -EFAULT (some data may have been copied
  75. * and the trailing NUL added).
  76. *
  77. * If @count is smaller than the length of the string, copies @count-1 bytes,
  78. * sets the last byte of @dst buffer to NUL and returns @count.
  79. */
  80. long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
  81. {
  82. mm_segment_t old_fs = get_fs();
  83. const void *src = unsafe_addr;
  84. long ret;
  85. if (unlikely(count <= 0))
  86. return 0;
  87. set_fs(KERNEL_DS);
  88. pagefault_disable();
  89. current->kernel_uaccess_faults_ok++;
  90. do {
  91. ret = __get_user(*dst++, (const char __user __force *)src++);
  92. } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
  93. current->kernel_uaccess_faults_ok--;
  94. dst[-1] = '\0';
  95. pagefault_enable();
  96. set_fs(old_fs);
  97. return ret ? -EFAULT : src - unsafe_addr;
  98. }