@@ -57,8 +57,7 @@ void * memcpy(void * dst,const void *src, size_t count)
EXPORT_SYMBOL(raw_copy_in_user);
EXPORT_SYMBOL(memcpy);
-bool probe_kernel_read_allowed(void *dst, const void *unsafe_src, size_t size,
- bool strict)
+bool probe_kernel_read_allowed(void *dst, const void *unsafe_src, size_t size)
{
if ((unsigned long)unsafe_src < PAGE_SIZE)
return false;
@@ -7,8 +7,7 @@
#include <linux/kernel.h>
#include <os.h>
-bool probe_kernel_read_allowed(void *dst, const void *src, size_t size,
- bool strict)
+bool probe_kernel_read_allowed(void *dst, const void *src, size_t size)
{
void *psrc = (void *)rounddown((unsigned long)src, PAGE_SIZE);
@@ -26,10 +26,7 @@ static __always_inline bool invalid_probe_range(u64 vaddr)
}
#endif
-bool probe_kernel_read_allowed(void *dst, const void *unsafe_src, size_t size,
- bool strict)
+bool probe_kernel_read_allowed(void *dst, const void *unsafe_src, size_t size)
{
- if (!strict)
- return true;
return !invalid_probe_range((unsigned long)unsafe_src);
}
@@ -301,11 +301,9 @@ copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
return 0;
}
-bool probe_kernel_read_allowed(void *dst, const void *unsafe_src,
- size_t size, bool strict);
+bool probe_kernel_read_allowed(void *dst, const void *unsafe_src, size_t size);
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_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);
@@ -180,15 +180,23 @@ static __always_inline int
bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr,
const bool compat)
{
+ const void __user *user_ptr = (__force const void __user *)unsafe_ptr;
int ret = security_locked_down(LOCKDOWN_BPF_READ);
if (unlikely(ret < 0))
- goto out;
- ret = compat ? probe_kernel_read(dst, unsafe_ptr, size) :
- probe_kernel_read_strict(dst, unsafe_ptr, size);
- if (unlikely(ret < 0))
-out:
- memset(dst, 0, size);
+ goto fail;
+
+ ret = probe_kernel_read(dst, unsafe_ptr, size);
+ if (unlikely(ret < 0)) {
+ if (compat)
+ ret = probe_user_read(dst, user_ptr, size);
+ if (unlikely(ret < 0))
+ goto fail;
+ }
+
+ return 0;
+fail:
+ memset(dst, 0, size);
return ret;
}
@@ -1209,6 +1209,9 @@ fetch_store_strlen(unsigned long addr)
do {
ret = probe_kernel_read(&c, (u8 *)addr + len, 1);
+ if (ret)
+ ret = probe_user_read(&c,
+ (__force u8 __user *)addr + len, 1);
len++;
} while (c && ret == 0 && len < MAX_STRING_SIZE);
@@ -1281,7 +1284,13 @@ fetch_store_string_user(unsigned long addr, void *dest, void *base)
static nokprobe_inline int
probe_mem_read(void *dest, void *src, size_t size)
{
- return probe_kernel_read(dest, src, size);
+ const void __user *user_ptr = (__force const void __user *)src;
+ int ret;
+
+ ret = probe_kernel_read(dest, src, size);
+ if (ret)
+ ret = probe_user_read(dest, user_ptr, size);
+ return ret;
}
static nokprobe_inline int
@@ -6,36 +6,14 @@
#include <linux/mm.h>
#include <linux/uaccess.h>
-static long __probe_kernel_read(void *dst, const void *src, size_t size,
- bool strict);
-
bool __weak probe_kernel_read_allowed(void *dst, const void *unsafe_src,
- size_t size, bool strict)
+ size_t size)
{
return true;
}
/**
- * probe_kernel_read(): safely attempt to read from any location
- * @dst: pointer to the buffer that shall take the data
- * @src: address to read from
- * @size: size of the data chunk
- *
- * Same as probe_kernel_read_strict() except that for architectures with
- * not fully separated user and kernel address spaces this function also works
- * for user address tanges.
- *
- * 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 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(): safely attempt to read from kernel-space
* @dst: pointer to the buffer that shall take the data
* @src: address to read from
* @size: size of the data chunk
@@ -48,18 +26,12 @@ EXPORT_SYMBOL_GPL(probe_kernel_read);
* probe_kernel_read() suitable for use within regions where the caller
* already holds mmap_sem, or other locks which nest inside mmap_sem.
*/
-long probe_kernel_read_strict(void *dst, const void *src, size_t size)
-{
- return __probe_kernel_read(dst, src, size, true);
-}
-
-static long __probe_kernel_read(void *dst, const void *src, size_t size,
- bool strict)
+long probe_kernel_read(void *dst, const void *src, size_t size)
{
long ret;
mm_segment_t old_fs = get_fs();
- if (!probe_kernel_read_allowed(dst, src, size, strict))
+ if (!probe_kernel_read_allowed(dst, src, size))
return -EFAULT;
set_fs(KERNEL_DS);
@@ -73,6 +45,7 @@ static long __probe_kernel_read(void *dst, const void *src, size_t size,
return -EFAULT;
return 0;
}
+EXPORT_SYMBOL_GPL(probe_kernel_read);
/**
* probe_user_read(): safely attempt to read from a user-space location
@@ -180,7 +153,7 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
if (unlikely(count <= 0))
return 0;
- if (!probe_kernel_read_allowed(dst, unsafe_addr, count, true))
+ if (!probe_kernel_read_allowed(dst, unsafe_addr, count))
return -EFAULT;
set_fs(KERNEL_DS);