return ((s64)vaddr << (64 - vaddr_bits)) >> (64 - vaddr_bits);
}
-static __always_inline bool invalid_probe_range(u64 vaddr)
+bool probe_kernel_read_allowed(const void *unsafe_src, size_t size, bool strict)
{
+ unsigned long vaddr = (unsigned long)unsafe_src;
+
+ if (!strict)
+ return true;
+
/*
* Range covering the highest possible canonical userspace address
* as well as non-canonical address range. For the canonical range
* we also need to include the userspace guard page.
*/
- return vaddr < TASK_SIZE_MAX + PAGE_SIZE ||
- canonical_address(vaddr, boot_cpu_data.x86_virt_bits) != vaddr;
+ return vaddr >= TASK_SIZE_MAX + PAGE_SIZE &&
+ canonical_address(vaddr, boot_cpu_data.x86_virt_bits) == vaddr;
}
#else
-static __always_inline bool invalid_probe_range(u64 vaddr)
+bool probe_kernel_read_allowed(const void *unsafe_src, size_t size, bool strict)
{
- return vaddr < TASK_SIZE_MAX;
+ if (!strict)
+ return true;
+ return (unsigned long)unsafe_src >= TASK_SIZE_MAX;
}
#endif
-
-long probe_kernel_read_strict(void *dst, const void *src, size_t size)
-{
- if (unlikely(invalid_probe_range((unsigned long)src)))
- return -EFAULT;
-
- return __probe_kernel_read(dst, src, size);
-}
-
-long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
-{
- if (unlikely(invalid_probe_range((unsigned long)unsafe_addr)))
- return -EFAULT;
-
- return __strncpy_from_unsafe(dst, unsafe_addr, count);
-}
return 0;
}
+bool probe_kernel_read_allowed(const void *unsafe_src, size_t size,
+ bool strict);
+
extern long probe_kernel_read(void *dst, const void *src, size_t size);
extern long probe_kernel_read_strict(void *dst, const void *src, size_t size);
-extern long __probe_kernel_read(void *dst, const void *src, size_t size);
extern long probe_user_read(void *dst, const void __user *src, size_t size);
extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr,
long count);
-extern long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
+
long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
long count);
long strnlen_user_nofault(const void __user *unsafe_addr, long count);
#include <linux/mm.h>
#include <linux/uaccess.h>
+static long __probe_kernel_read(void *dst, const void *src, size_t size,
+ bool strict);
+static long __strncpy_from_unsafe(char *dst, const void *unsafe_addr,
+ long count, bool strict);
+
+bool __weak probe_kernel_read_allowed(const void *unsafe_src, size_t size,
+ bool strict)
+{
+ return true;
+}
+
/**
* probe_kernel_read(): safely attempt to read from any location
* @dst: pointer to the buffer that shall take the data
* DO NOT USE THIS FUNCTION - it is broken on architectures with entirely
* separate kernel and user address spaces, and also a bad idea otherwise.
*/
-long __weak probe_kernel_read(void *dst, const void *src, size_t size)
- __attribute__((alias("__probe_kernel_read")));
+long probe_kernel_read(void *dst, const void *src, size_t size)
+{
+ return __probe_kernel_read(dst, src, size, false);
+}
+EXPORT_SYMBOL_GPL(probe_kernel_read);
/**
* probe_kernel_read_strict(): safely attempt to read from kernel-space
* probe_kernel_read() suitable for use within regions where the caller
* already holds mmap_lock, or other locks which nest inside mmap_lock.
*/
+long probe_kernel_read_strict(void *dst, const void *src, size_t size)
+{
+ return __probe_kernel_read(dst, src, size, true);
+}
-long __weak probe_kernel_read_strict(void *dst, const void *src, size_t size)
- __attribute__((alias("__probe_kernel_read")));
-
-long __probe_kernel_read(void *dst, const void *src, size_t size)
+static long __probe_kernel_read(void *dst, const void *src, size_t size,
+ bool strict)
{
long ret;
mm_segment_t old_fs = get_fs();
+ if (!probe_kernel_read_allowed(src, size, strict))
+ return -EFAULT;
+
set_fs(KERNEL_DS);
pagefault_disable();
ret = __copy_from_user_inatomic(dst, (__force const void __user *)src,
return -EFAULT;
return 0;
}
-EXPORT_SYMBOL_GPL(probe_kernel_read);
/**
* probe_user_read(): safely attempt to read from a user-space location
* DO NOT USE THIS FUNCTION - it is broken on architectures with entirely
* separate kernel and user address spaces, and also a bad idea otherwise.
*/
-long __weak strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
- __attribute__((alias("__strncpy_from_unsafe")));
+long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
+{
+ return __strncpy_from_unsafe(dst, unsafe_addr, count, false);
+}
/**
* strncpy_from_kernel_nofault: - Copy a NUL terminated string from unsafe
* If @count is smaller than the length of the string, copies @count-1 bytes,
* sets the last byte of @dst buffer to NUL and returns @count.
*/
-long __weak strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr,
- long count)
- __attribute__((alias("__strncpy_from_unsafe")));
+long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
+{
+ return __strncpy_from_unsafe(dst, unsafe_addr, count, true);
+}
-long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
+static long __strncpy_from_unsafe(char *dst, const void *unsafe_addr,
+ long count, bool strict)
{
mm_segment_t old_fs = get_fs();
const void *src = unsafe_addr;
if (unlikely(count <= 0))
return 0;
+ if (!probe_kernel_read_allowed(unsafe_addr, count, strict))
+ return -EFAULT;
set_fs(KERNEL_DS);
pagefault_disable();