maccess.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Access kernel memory without faulting.
  3. */
  4. #include <linux/uaccess.h>
  5. #include <linux/module.h>
  6. #include <linux/mm.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. long probe_kernel_read(void *dst, void *src, size_t size)
  17. {
  18. long ret;
  19. pagefault_disable();
  20. ret = __copy_from_user_inatomic(dst,
  21. (__force const void __user *)src, size);
  22. pagefault_enable();
  23. return ret ? -EFAULT : 0;
  24. }
  25. EXPORT_SYMBOL_GPL(probe_kernel_read);
  26. /**
  27. * probe_kernel_write(): safely attempt to write to a location
  28. * @dst: address to write to
  29. * @src: pointer to the data that shall be written
  30. * @size: size of the data chunk
  31. *
  32. * Safely write to address @dst from the buffer at @src. If a kernel fault
  33. * happens, handle that and return -EFAULT.
  34. */
  35. long probe_kernel_write(void *dst, void *src, size_t size)
  36. {
  37. long ret;
  38. pagefault_disable();
  39. ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
  40. pagefault_enable();
  41. return ret ? -EFAULT : 0;
  42. }
  43. EXPORT_SYMBOL_GPL(probe_kernel_write);