1c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar/* 2c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * Access kernel memory without faulting. 3c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar */ 4b95f1b31b75588306e32b2afd32166cad48f670bPaul Gortmaker#include <linux/export.h> 5c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar#include <linux/mm.h> 67c7fcf762e405eb040ee10d22d656a791f616122David Howells#include <linux/uaccess.h> 7c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar 8c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar/** 9c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * probe_kernel_read(): safely attempt to read from a location 10c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * @dst: pointer to the buffer that shall take the data 11c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * @src: address to read from 12c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * @size: size of the data chunk 13c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * 14c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * Safely read from address @src to the buffer at @dst. If a kernel fault 15c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * happens, handle that and return -EFAULT. 16c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar */ 176144a85a0e018c19bc4b24f7eb6c1f3f7431813dJason Wessel 18f29c50419c8d1998edd759f1990c4243a248f469Steven Rostedtlong __weak probe_kernel_read(void *dst, const void *src, size_t size) 196144a85a0e018c19bc4b24f7eb6c1f3f7431813dJason Wessel __attribute__((alias("__probe_kernel_read"))); 206144a85a0e018c19bc4b24f7eb6c1f3f7431813dJason Wessel 21f29c50419c8d1998edd759f1990c4243a248f469Steven Rostedtlong __probe_kernel_read(void *dst, const void *src, size_t size) 22c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar{ 23c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar long ret; 24b4b8ac524d9b6ed7229017145afa1d7afbea4a48Jason Wessel mm_segment_t old_fs = get_fs(); 25c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar 26b4b8ac524d9b6ed7229017145afa1d7afbea4a48Jason Wessel set_fs(KERNEL_DS); 27c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar pagefault_disable(); 28c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar ret = __copy_from_user_inatomic(dst, 29c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar (__force const void __user *)src, size); 30c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar pagefault_enable(); 31b4b8ac524d9b6ed7229017145afa1d7afbea4a48Jason Wessel set_fs(old_fs); 32c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar 33c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar return ret ? -EFAULT : 0; 34c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar} 35c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo MolnarEXPORT_SYMBOL_GPL(probe_kernel_read); 36c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar 37c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar/** 38c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * probe_kernel_write(): safely attempt to write to a location 39c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * @dst: address to write to 40c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * @src: pointer to the data that shall be written 41c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * @size: size of the data chunk 42c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * 43c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * Safely write to address @dst from the buffer at @src. If a kernel fault 44c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar * happens, handle that and return -EFAULT. 45c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar */ 46f29c50419c8d1998edd759f1990c4243a248f469Steven Rostedtlong __weak probe_kernel_write(void *dst, const void *src, size_t size) 476144a85a0e018c19bc4b24f7eb6c1f3f7431813dJason Wessel __attribute__((alias("__probe_kernel_write"))); 486144a85a0e018c19bc4b24f7eb6c1f3f7431813dJason Wessel 49f29c50419c8d1998edd759f1990c4243a248f469Steven Rostedtlong __probe_kernel_write(void *dst, const void *src, size_t size) 50c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar{ 51c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar long ret; 52b4b8ac524d9b6ed7229017145afa1d7afbea4a48Jason Wessel mm_segment_t old_fs = get_fs(); 53c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar 54b4b8ac524d9b6ed7229017145afa1d7afbea4a48Jason Wessel set_fs(KERNEL_DS); 55c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar pagefault_disable(); 56c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar ret = __copy_to_user_inatomic((__force void __user *)dst, src, size); 57c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar pagefault_enable(); 58b4b8ac524d9b6ed7229017145afa1d7afbea4a48Jason Wessel set_fs(old_fs); 59c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar 60c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar return ret ? -EFAULT : 0; 61c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo Molnar} 62c33fa9f5609e918824446ef9a75319d4a802f1f4Ingo MolnarEXPORT_SYMBOL_GPL(probe_kernel_write); 63