clean up and streamline probe_kernel_* and friends
Subsystems (12)
Hi all,
this series start cleaning up the safe kernel and user memory probing helpers in mm/maccess.c, and then allows architectures to implement the kernel probing without overriding the address space limit and temporarily allowing access to user memory. It then switches x86 over to this new mechanism by reusing the unsafe_* uaccess logic.
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 1/15 maccess: unexport probe_kernel_write and probe_user_write
These two functions are not used by any modular code.
--- mm/maccess.c | 2 -- 1 file changed, 2 deletions(-)
mm/maccess.c +0 -2
@@ -121,7 +121,6 @@ long __probe_kernel_write(void *dst, const void *src, size_t size)return ret; } -EXPORT_SYMBOL_GPL (probe_kernel_write);/** * probe_user_write(): safely attempt to write to a user-space location
@@ -148,7 +147,6 @@ long __probe_user_write(void __user *dst, const void *src, size_t size)return ret; } -EXPORT_SYMBOL_GPL (probe_user_write);/** * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 2/15 maccess: remove various unused weak aliases
In reply to Christoph Hellwigmaccess tends to define lots of underscore prefixed symbols that then have other weak aliases. But except for two cases they are never actually used, so remove them.
--- include/linux/uaccess.h | 3 --- mm/maccess.c | 19 +++---------------- 2 files changed, 3 insertions(+), 19 deletions(-)
include/linux/uaccess.h +0 -3
@@ -324,7 +324,6 @@ extern long __probe_kernel_read(void *dst, const void *src, size_t size);* happens, handle that and return -EFAULT. */ extern long probe_user_read (void *dst,const void __user *src,size_t size); -extern long __probe_user_read (void *dst,const void __user *src,size_t size);/* * probe_kernel_write(): safely attempt to write to a location
@@ -336,7 +335,6 @@ extern long __probe_user_read(void *dst, const void __user *src, size_t size);* happens, handle that and return -EFAULT. */ extern long notrace probe_kernel_write (void *dst,const void *src,size_t size); -extern long notrace __probe_kernel_write (void *dst,const void *src,size_t size);/* * probe_user_write(): safely attempt to write to a location in user space
@@ -348,7 +346,6 @@ extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size* happens, handle that and return -EFAULT. */ extern long notrace probe_user_write (void __user *dst,const void *src,size_t size); -extern long notrace __probe_user_write (void __user *dst,const void *src,size_t size);extern long strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count); extern long strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr,
mm/maccess.c +3 -16
@@ -79,11 +79,7 @@ EXPORT_SYMBOL_GPL(probe_kernel_read);* Safely read from user address @src to the buffer at @dst. If a kernel fault * happens, handle that and return -EFAULT. */ - -long __weak probe_user_read (void *dst,const void __user *src,size_t size) - __attribute__((alias ("__probe_user_read" ))); - -long __probe_user_read (void *dst,const void __user *src,size_t size) +long probe_user_read (void *dst,const void __user *src,size_t size) {long ret = -EFAULT ;mm_segment_t old_fs =get_fs ();
@@ -106,11 +102,7 @@ EXPORT_SYMBOL_GPL(probe_user_read);* Safely write to address @dst from the buffer at @src. If a kernel fault * happens, handle that and return -EFAULT. */ - -long __weak probe_kernel_write (void *dst,const void *src,size_t size) - __attribute__((alias ("__probe_kernel_write" ))); - -long __probe_kernel_write (void *dst,const void *src,size_t size) +long probe_kernel_write (void *dst,const void *src,size_t size) {long ret;mm_segment_t old_fs =get_fs ();
@@ -131,11 +123,7 @@ long __probe_kernel_write(void *dst, const void *src, size_t size)* Safely write to address @dst from the buffer at @src. If a kernel fault * happens, handle that and return -EFAULT. */ - -long __weak probe_user_write (void __user *dst,const void *src,size_t size) - __attribute__((alias ("__probe_user_write" ))); - -long __probe_user_write (void __user *dst,const void *src,size_t size) +long probe_user_write (void __user *dst,const void *src,size_t size) {long ret = -EFAULT ;mm_segment_t old_fs =get_fs ();
@@ -171,7 +159,6 @@ long __probe_user_write(void __user *dst, const void *src, size_t size)* probing memory on a user address range where strncpy_from_unsafe_user() is * supposed to be used instead. */ -long __weak strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count) __attribute__((alias ("__strncpy_from_unsafe" )));
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 3/15 maccess: remove duplicate kerneldoc commens
In reply to Christoph HellwigMany of the maccess routines have a copy of the kerneldoc comment in the header. Remove it as it is not useful and will get out of sync sooner or later.
--- include/linux/uaccess.h | 38 -------------------------------------- 1 file changed, 38 deletions(-)
include/linux/uaccess.h +0 -38
@@ -301,50 +301,12 @@ copy_struct_from_user(void *dst, size_t ksize, const void __user *src,return 0 ; } -/* -* probe_kernel_read(): safely attempt to read from a location -* @dst: pointer to the buffer that shall take the data -* @src: address to read from -* @size: size of the data chunk -* -* Safely read from address @src to the buffer at @dst. If a kernel fault -* happens, handle that and return -EFAULT. -*/ 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); - -/* -* probe_user_read(): safely attempt to read from a location in user space -* @dst: pointer to the buffer that shall take the data -* @src: address to read from -* @size: size of the data chunk -* -* Safely read from address @src to the buffer at @dst. If a kernel fault -* happens, handle that and return -EFAULT. -*/ extern long probe_user_read (void *dst,const void __user *src,size_t size); -/* -* probe_kernel_write(): safely attempt to write to a location -* @dst: address to write to -* @src: pointer to the data that shall be written -* @size: size of the data chunk -* -* Safely write to address @dst from the buffer at @src. If a kernel fault -* happens, handle that and return -EFAULT. -*/ extern long notrace probe_kernel_write (void *dst,const void *src,size_t size); - -/* -* probe_user_write(): safely attempt to write to a location in user space -* @dst: address to write to -* @src: pointer to the data that shall be written -* @size: size of the data chunk -* -* Safely write to address @dst from the buffer at @src. If a kernel fault -* happens, handle that and return -EFAULT. -*/ extern long notrace probe_user_write (void __user *dst,const void *src,size_t size);extern long strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count);
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 4/15 maccess: clarify kerneldoc comments
In reply to Christoph HellwigAdd proper kerneldoc comments for probe_kernel_read_strict and probe_kernel_read strncpy_from_unsafe_strict and explain the different versus the non-strict version.
--- mm/maccess.c | 61 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 18 deletions(-)
mm/maccess.c +43 -18
@@ -31,29 +31,35 @@ probe_write_common(void __user *dst, const void *src, size_t size) }/** -* probe_kernel_read(): safely attempt to read from a kernel-space location +* 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 * -* Safely read from address @src to the buffer at @dst. If a kernel fault -* happens, handle that and return -EFAULT. +* 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 __weak probe_kernel_read (void *dst,const void *src,size_t size) + __attribute__((alias ("__probe_kernel_read" ))); + +/** +* probe_kernel_read_strict(): 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 +* +* Safely read from kernel address @src to the buffer at @dst. If a kernel +* fault happens, handle that and return -EFAULT. * * We ensure that the copy_from_user is executed in atomic context so that * do_page_fault() doesn't attempt to take mmap_sem. This makes * probe_kernel_read() suitable for use within regions where the caller * already holds mmap_sem, or other locks which nest inside mmap_sem. -* -* probe_kernel_read_strict() is the same as probe_kernel_read() except for -* the case where architectures have non-overlapping user and kernel address -* ranges: probe_kernel_read_strict() will additionally return -EFAULT for -* probing memory on a user address range where probe_user_read() is supposed -* to be used instead. */ - -long __weak probe_kernel_read (void *dst,const void *src,size_t size) - __attribute__((alias ("__probe_kernel_read" ))); -long __weak probe_kernel_read_strict (void *dst,const void *src,size_t size) __attribute__((alias ("__probe_kernel_read" )));
@@ -153,15 +159,34 @@ long probe_user_write(void __user *dst, const void *src, size_t size)* 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. * -* strncpy_from_unsafe_strict() is the same as strncpy_from_unsafe() except -* for the case where architectures have non-overlapping user and kernel address -* ranges: strncpy_from_unsafe_strict() will additionally return -EFAULT for -* probing memory on a user address range where strncpy_from_unsafe_user() is -* supposed to be used instead. +* Same as strncpy_from_unsafe_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 __weak strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count) __attribute__((alias ("__strncpy_from_unsafe" ))); +/** +* strncpy_from_unsafe_strict: - Copy a NUL terminated string from unsafe +* address. +* @dst: Destination address, in kernel space. This buffer must be at +* least @count bytes long. +* @unsafe_addr: Unsafe address. +* @count: Maximum number of bytes to copy, including the trailing NUL. +* +* Copies a NUL-terminated string from unsafe address to kernel buffer. +* +* On success, returns the length of the string INCLUDING the trailing NUL. +* +* If access fails, returns -EFAULT (some data may have been copied +* and the trailing NUL added). +* +* 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_unsafe_strict (char *dst,const void *unsafe_addr,long count) __attribute__((alias ("__strncpy_from_unsafe" )));
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 5/15 maccess: update the top of file comment
In reply to Christoph HellwigThis file now also contains several helpers for accessing user memory.
--- mm/maccess.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/maccess.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* -* Access kernel memory without faulting. +* Access kernel or user memory without faulting. */ #include <linux/export.h> #include <linux/mm.h>
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 6/15 maccess: rename strncpy_from_unsafe_user to strncpy_from_user_unsafe
In reply to Christoph HellwigThis matches the convention of always having _unsafe as a suffix, as well as match the naming of strncpy_from_user.
--- include/linux/uaccess.h | 2 +- kernel/trace/bpf_trace.c | 2 +- kernel/trace/trace_kprobe.c | 2 +- mm/maccess.c | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-)
include/linux/uaccess.h +1 -1
@@ -313,7 +313,7 @@ extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);extern long strncpy_from_unsafe_strict (char *dst,const void *unsafe_addr,long count);extern long __strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count); -extern long strncpy_from_unsafe_user (char *dst,const void __user *unsafe_addr, +extern long strncpy_from_user_unsafe (char *dst,const void __user *unsafe_addr,long count);extern long strnlen_unsafe_user (const void __user *unsafe_addr,long count);
kernel/trace/bpf_trace.c +1 -1
@@ -159,7 +159,7 @@ static const struct bpf_func_proto bpf_probe_read_user_proto = { BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size, const void __user *, unsafe_ptr) { -int ret =strncpy_from_unsafe_user (dst, unsafe_ptr, size); +int ret =strncpy_from_user_unsafe (dst, unsafe_ptr, size);if (unlikely (ret <0 ))memset (dst,0 , size);
kernel/trace/trace_kprobe.c +1 -1
@@ -1262,7 +1262,7 @@ fetch_store_string_user(unsigned long addr, void *dest, void *base) __dest =get_loc_data (dest, base); - ret =strncpy_from_unsafe_user (__dest, uaddr, maxlen); + ret =strncpy_from_user_unsafe (__dest, uaddr, maxlen);if (ret >=0 ) *(u32 *)dest =make_data_loc (ret, __dest - base);
mm/maccess.c +2 -2
@@ -215,7 +215,7 @@ long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count) }/** -* strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user +* strncpy_from_user_unsafe: - Copy a NUL terminated string from unsafe user * address. * @dst: Destination address, in kernel space. This buffer must be at * least @count bytes long.
@@ -232,7 +232,7 @@ long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)* 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 strncpy_from_unsafe_user (char *dst,const void __user *unsafe_addr, +long strncpy_from_user_unsafe (char *dst,const void __user *unsafe_addr,long count) {mm_segment_t old_fs =get_fs ();
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 7/15 maccess: rename strncpy_from_unsafe_strict to strncpy_from_kernel_unsafe
In reply to Christoph HellwigThis matches the convention of always having _unsafe as a suffix, as well as match the naming of strncpy_from_user_unsafe.
--- arch/x86/mm/maccess.c | 2 +- include/linux/uaccess.h | 2 +- kernel/trace/bpf_trace.c | 2 +- mm/maccess.c | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/mm/maccess.c +1 -1
@@ -34,7 +34,7 @@ long probe_kernel_read_strict(void *dst, const void *src, size_t size)return __probe_kernel_read (dst, src, size); } -long strncpy_from_unsafe_strict (char *dst,const void *unsafe_addr,long count) +long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count) {if (unlikely (invalid_probe_range ((unsigned long )unsafe_addr)))return -EFAULT ;
include/linux/uaccess.h +1 -1
@@ -310,7 +310,7 @@ extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);extern long notrace probe_user_write (void __user *dst,const void *src,size_t size);extern long strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count); -extern long strncpy_from_unsafe_strict (char *dst,const void *unsafe_addr, +extern long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count);extern long __strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count); extern long strncpy_from_user_unsafe(char *dst, const void __user *unsafe_addr,
kernel/trace/bpf_trace.c +1 -1
@@ -240,7 +240,7 @@ bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,* is returned that can be used for bpf_perf_event_output() et al. */ ret = compat ?strncpy_from_unsafe (dst, unsafe_ptr, size) : -strncpy_from_unsafe_strict (dst, unsafe_ptr, size); +strncpy_from_kernel_unsafe (dst, unsafe_ptr, size);if (unlikely (ret <0 )) out:memset (dst,0 , size);
mm/maccess.c +3 -3
@@ -159,7 +159,7 @@ long probe_user_write(void __user *dst, const void *src, size_t size)* 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. * -* Same as strncpy_from_unsafe_strict() except that for architectures with +* Same as strncpy_from_kernel_unsafe() except that for architectures with * not fully separated user and kernel address spaces this function also works * for user address tanges. *
@@ -170,7 +170,7 @@ long __weak strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count) __attribute__((alias("__strncpy_from_unsafe")));/** -* strncpy_from_unsafe_strict: - Copy a NUL terminated string from unsafe +* strncpy_from_kernel_unsafe: - Copy a NUL terminated string from unsafe * address. * @dst: Destination address, in kernel space. This buffer must be at * least @count bytes long.
@@ -187,7 +187,7 @@ long __weak strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)* 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_unsafe_strict (char *dst,const void *unsafe_addr, +long __weak strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count) __attribute__((alias ("__strncpy_from_unsafe" )));
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 8/15 maccess: rename strnlen_unsafe_user to strnlen_user_unsafe
In reply to Christoph HellwigThis matches the convention of always having _unsafe as a suffix, as well as match the naming of strncpy_from_user_unsafe.
--- include/linux/uaccess.h | 2 +- kernel/trace/trace_kprobe.c | 2 +- mm/maccess.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
include/linux/uaccess.h +1 -1
@@ -315,7 +315,7 @@ extern long strncpy_from_kernel_unsafe(char *dst, const void *unsafe_addr,extern long __strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count);extern long strncpy_from_user_unsafe (char *dst,const void __user *unsafe_addr,long count); -extern long strnlen_unsafe_user (const void __user *unsafe_addr,long count); +extern long strnlen_user_unsafe (const void __user *unsafe_addr,long count);/** * probe_kernel_address(): safely attempt to read from a location
kernel/trace/trace_kprobe.c +1 -1
@@ -1215,7 +1215,7 @@ fetch_store_strlen_user(unsigned long addr) {const void __user *uaddr = (__force const void __user *)addr; -return strnlen_unsafe_user (uaddr,MAX_STRING_SIZE ); +return strnlen_user_unsafe (uaddr,MAX_STRING_SIZE ); }/*
mm/maccess.c +2 -2
@@ -258,7 +258,7 @@ long strncpy_from_user_unsafe(char *dst, const void __user *unsafe_addr, }/** -* strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL. +* strnlen_user_unsafe: - Get the size of a user string INCLUDING final NUL. * @unsafe_addr: The string to measure. * @count: Maximum count (including NUL) *
@@ -273,7 +273,7 @@ long strncpy_from_user_unsafe(char *dst, const void __user *unsafe_addr,* Unlike strnlen_user, this can be used from IRQ handler etc. because * it disables pagefaults. */ -long strnlen_unsafe_user (const void __user *unsafe_addr,long count) +long strnlen_user_unsafe (const void __user *unsafe_addr,long count) {mm_segment_t old_fs =get_fs ();int ret;
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 08/15] maccess: rename strnlen_unsafe_user to strnlen_user_unsafe
Quoted 3 lines · Christoph Hellwig
This matches the convention of always having
_unsafeas a suffix, as well as match the naming ofstrncpy_from_user_unsafe.
Hmm. While renaming them, can we perhaps clarify what the rules are?
We now have two different kinds of "unsafe". We have the "unsafe_get_user()" kind of unsafe: the user pointer itself is unsafe because it isn't checked, and you need to use a "user_access_begin()" to verify.
It's the new form of "__get_user()".
And then we have the strncpy_from_user_unsafe(), which is really more like the "probe_kernel_read()" kind of funtion, in that it's about the context, and not faulting.
Honestly, I don't like the "unsafe" in the second case, because there's nothing "unsafe" about the function. It's used in odd contexts. I guess to some degree those are "unsafe" contexts, but I think it might be better to clarify.
So while I think using a consistent convention is good, and it's true that there is a difference in the convention between the two cases ("unsafe" at the beginning vs end), one of them is actually about the safety and security of the operation (and we have automated logic these days to verify it on x86), the other has nothing to do with "safety", really.
Would it be better to standardize around a "probe_xyz()" naming?
Or perhaps a "xyz_nofault()" naming?
I'm not a huge fan of the "probe" naming, but it sure is descriptive, which is a really good thing.
Another option would be to make it explicitly about _what_ is "unsafe", ie that it's about not having a process context that can be faulted in. But "xyz_unsafe_context()" is much too verbose. "xyz_noctx()" might work.
I realize this is nit-picky, and I think the patch series as-is is already an improvement, but I do think our naming in this area is really quite bad.
The fact that we have "probe_kernel_read()" but then "strncpy_from_user_unsafe()" for the _same_ conceptual difference really tells me how inconsistent the naming for these kinds of "we can't take page faults" is. No?
Linus
Re: [PATCH 08/15] maccess: rename strnlen_unsafe_user to strnlen_user_unsafe
Quoted 8 lines · Linus Torvalds
So while I think using a consistent convention is good, and it's true that there is a difference in the convention between the two cases ("unsafe" at the beginning vs end), one of them is actually about the safety and security of the operation (and we have automated logic these days to verify it on x86), the other has nothing to do with "safety", really.
Would it be better to standardize around a "
probe_xyz()" naming?
So:
probe_strncpy, probe_strncpy_user, probe_strnlen_user?
Sounds weird, but at least it is consistent.
Quoted 1 line
Or perhaps a "
xyz_nofault()" naming?
That sounds a little better:
strncpy_nofault, strncpy_user_nofault, strnlen_user_nofault
Quoted 3 lines
I realize this is nit-picky, and I think the patch series as-is is already an improvement, but I do think our naming in this area is really quite bad.
Always open for improvements :)
Quoted 4 lines
The fact that we have "
probe_kernel_read()" but then "strncpy_from_user_unsafe()" for the _same_ conceptual difference really tells me how inconsistent the naming for these kinds of "we can't take page faults" is. No?
True. If we wanted to do _nofaul, what would the basic read/write versions be?
Re: [PATCH 08/15] maccess: rename strnlen_unsafe_user to strnlen_user_unsafe
Quoted 8 lines · Christoph Hellwig
Quoted 5 lines
The fact that we have "
probe_kernel_read()" but then "strncpy_from_user_unsafe()" for the _same_ conceptual difference really tells me how inconsistent the naming for these kinds of "we can't take page faults" is. No?True. If we wanted to do
_nofaul, what would the basic read/write versions be?
I think "copy_to/from_kernel_nofault()" might be the most consistent model, if we are looking to be kind of consistent with the user access functions..
Unless we want to make "memcpy" be part of the name, but I think that we really want to have that 'from/to' part anyway, which forces the "copy_from/to_xyz" kind of naming.
I dunno. I don't want to be too nit-picky, I just would like us to be more consistent and have the naming say what's up without having multiple different versions of the same thing.
We've had this same discussion with the nvdimm case, but there the issues are somewhat different (faulting is ok on user addresses - you can sleep - but kernel address faults aren't about the _context_ any more, they are about the data not being safe to access any more)
Anybody else?
Linus
PATCH 9/15 maccess: remove probe_read_common and probe_write_common
In reply to Christoph HellwigEach of the helpers has just two callers, which also different in dealing with kernel or userspace pointers. Just open code the logic in the callers.
--- mm/maccess.c | 63 ++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 34 deletions(-)
mm/maccess.c +29 -34
@@ -6,30 +6,6 @@#include <linux/mm.h> #include <linux/uaccess.h> -static __always_inline long -probe_read_common (void *dst,const void __user *src,size_t size) -{ -long ret; - -pagefault_disable (); - ret =__copy_from_user_inatomic (dst, src, size); -pagefault_enable (); - -return ret ? -EFAULT :0 ; -} - -static __always_inline long -probe_write_common (void __user *dst,const void *src,size_t size) -{ -long ret; - -pagefault_disable (); - ret =__copy_to_user_inatomic (dst, src, size); -pagefault_enable (); - -return ret ? -EFAULT :0 ; -} -/** * probe_kernel_read(): safely attempt to read from any location * @dst: pointer to the buffer that shall take the data
@@ -69,10 +45,15 @@ long __probe_kernel_read(void *dst, const void *src, size_t size)mm_segment_t old_fs =get_fs ();set_fs (KERNEL_DS ); - ret =probe_read_common (dst, (__force const void __user *)src, size); +pagefault_disable (); + ret =__copy_from_user_inatomic (dst, (__force const void __user *)src, + size); +pagefault_enable ();set_fs (old_fs); -return ret; +if (ret) +return -EFAULT ; +return 0 ; }EXPORT_SYMBOL_GPL (probe_kernel_read);
@@ -91,11 +72,16 @@ long probe_user_read(void *dst, const void __user *src, size_t size)mm_segment_t old_fs =get_fs ();set_fs (USER_DS ); -if (access_ok (src, size)) - ret =probe_read_common (dst, src, size); +if (access_ok (src, size)) { +pagefault_disable (); + ret =__copy_from_user_inatomic (dst, src, size); +pagefault_enable (); + }set_fs (old_fs); -return ret; +if (ret) +return -EFAULT ; +return 0 ; }EXPORT_SYMBOL_GPL (probe_user_read);
@@ -114,10 +100,14 @@ long probe_kernel_write(void *dst, const void *src, size_t size)mm_segment_t old_fs =get_fs ();set_fs (KERNEL_DS ); - ret =probe_write_common ((__force void __user *)dst, src, size); +pagefault_disable (); + ret =__copy_to_user_inatomic ((__force void __user *)dst, src, size); +pagefault_enable ();set_fs (old_fs); -return ret; +if (ret) +return -EFAULT ; +return 0 ; }/**
@@ -135,11 +125,16 @@ long probe_user_write(void __user *dst, const void *src, size_t size)mm_segment_t old_fs =get_fs ();set_fs (USER_DS ); -if (access_ok (dst, size)) - ret =probe_write_common (dst, src, size); +if (access_ok (dst, size)) { +pagefault_disable (); + ret =__copy_to_user_inatomic (dst, src, size); +pagefault_enable (); + }set_fs (old_fs); -return ret; +if (ret) +return -EFAULT ; +return 0 ; }/**
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 10/15 maccess: unify the probe kernel arch hooks
In reply to Christoph HellwigCurrently architectures have to override every routine that probes kernel memory, which includes a pure read and strcpy, both in strict and not strict variants. Just provide a single arch hooks instead to make sure all architectures cover all the cases.
--- arch/parisc/lib/memcpy.c | 13 ++++------- arch/um/kernel/maccess.c | 11 ++++----- arch/x86/mm/maccess.c | 18 ++++----------- include/linux/uaccess.h | 5 ++-- mm/maccess.c | 49 ++++++++++++++++++++++++++++++---------- 5 files changed, 55 insertions(+), 41 deletions(-)
arch/parisc/lib/memcpy.c +5 -8
@@ -57,14 +57,11 @@ void * memcpy(void * dst,const void *src, size_t count)EXPORT_SYMBOL (raw_copy_in_user);EXPORT_SYMBOL (memcpy); -long probe_kernel_read (void *dst,const void *src,size_t size) +bool probe_kernel_read_allowed (void *dst,const void *unsafe_src,size_t size, +bool strict) { -unsigned long addr = (unsigned long )src; - -if (addr <PAGE_SIZE ) -return -EFAULT ; - +if ((unsigned long )unsafe_src <PAGE_SIZE ) +return false;/* check for I/O space F_EXTEND(0xfff00000) access as well? */ - -return __probe_kernel_read (dst, src, size); +return true; }
arch/um/kernel/maccess.c +5 -6
@@ -7,15 +7,14 @@#include <linux/kernel.h> #include <os.h> -long probe_kernel_read (void *dst,const void *src,size_t size) +bool probe_kernel_read_allowed (void *dst,const void *src,size_t size, +bool strict) {void *psrc = (void *)rounddown ((unsigned long )src,PAGE_SIZE );if ((unsigned long )src <PAGE_SIZE || size <=0 ) -return -EFAULT ; - +return false;if (os_mincore (psrc, size + src - psrc) <=0 ) -return -EFAULT ; - -return __probe_kernel_read (dst, src, size); +return false; +return true; }
arch/x86/mm/maccess.c +5 -13
@@ -26,18 +26,10 @@ static __always_inline bool invalid_probe_range(u64 vaddr) }#endif -long probe_kernel_read_strict (void *dst,const void *src,size_t size) +bool probe_kernel_read_allowed (void *dst,const void *unsafe_src,size_t size, +bool strict) { -if (unlikely (invalid_probe_range ((unsigned long )src))) -return -EFAULT ; - -return __probe_kernel_read (dst, src, size); -} - -long strncpy_from_kernel_unsafe (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); +if (!strict) +return true; +return !invalid_probe_range ((unsigned long )unsafe_src); }
include/linux/uaccess.h +3 -2
@@ -301,9 +301,11 @@ 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); +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);
@@ -312,7 +314,6 @@ extern long notrace probe_user_write(void __user *dst, const void *src, size_t sextern long strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count);extern long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count); -extern long __strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count);extern long strncpy_from_user_unsafe (char *dst,const void __user *unsafe_addr,long count);extern long strnlen_user_unsafe (const void __user *unsafe_addr,long count);
mm/maccess.c +37 -12
97 lines not on this page · raw message
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 11/15 maccess: remove strncpy_from_unsafe
In reply to Christoph HellwigAll three callers really should try the explicit kernel and user copies instead. One has already deprecated the somewhat dangerous either kernel or user address concept, the other two still need to follow up eventually.
--- include/linux/uaccess.h | 1 - kernel/trace/bpf_trace.c | 40 ++++++++++++++++++++++++++----------- kernel/trace/trace_kprobe.c | 5 ++++- mm/maccess.c | 39 +----------------------------------- 4 files changed, 33 insertions(+), 52 deletions(-)
include/linux/uaccess.h +0 -1
@@ -311,7 +311,6 @@ 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 notrace probe_user_write (void __user *dst,const void *src,size_t size); -extern long strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count);extern long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count); extern long strncpy_from_user_unsafe(char *dst, const void __user *unsafe_addr,
kernel/trace/bpf_trace.c +28 -12
@@ -229,9 +229,10 @@ bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,int ret =security_locked_down (LOCKDOWN_BPF_READ );if (unlikely (ret <0 )) - goto out; + goto fail; +/* -* The strncpy_from_unsafe_*() call will likely not fill the entire +* The strncpy_from_*_unsafe() call will likely not fill the entire * buffer, but that's okay in this circumstance as we're probing * arbitrary memory anyway similar to bpf_probe_read_*() and might * as well probe the stack. Thus, memory is explicitly cleared
@@ -239,11 +240,18 @@ bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,* code altogether don't copy garbage; otherwise length of string * is returned that can be used for bpf_perf_event_output() et al. */ - ret = compat ?strncpy_from_unsafe (dst, unsafe_ptr, size) : -strncpy_from_kernel_unsafe (dst, unsafe_ptr, size); -if (unlikely (ret <0 )) -out: -memset (dst,0 , size); + ret =strncpy_from_kernel_unsafe (dst, unsafe_ptr, size); +if (unlikely (ret <0 )) { +if (compat) + ret =strncpy_from_user_unsafe (dst, + (__force const void __user *)unsafe_ptr, + size); +if (ret <0 ) + goto fail; + } +return 0 ; +fail: +memset (dst,0 , size);return ret; }
@@ -321,6 +329,17 @@ static const struct bpf_func_proto *bpf_get_probe_write_proto(void)return &bpf_probe_write_user_proto; } +#define BPF_STRNCPY_LEN 64 + +static void bpf_strncpy (char *buf,long unsafe_addr) +{ + buf[0 ] =0 ; +if (strncpy_from_kernel_unsafe (buf, (void *)unsafe_addr, +BPF_STRNCPY_LEN )) +strncpy_from_user_unsafe (buf, (void __user *)unsafe_addr, +BPF_STRNCPY_LEN ); +} +/* * Only limited trace_printk() conversion specifiers allowed: * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
@@ -332,7 +351,7 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,int mod[3 ] = {};int fmt_cnt =0 ;u64 unsafe_addr; -char buf[64 ]; +char buf[BPF_STRNCPY_LEN ];int i;/*
@@ -387,10 +406,7 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, arg3 = (long ) buf;break ; } - buf[0 ] =0 ; -strncpy_from_unsafe (buf, - (void *) (long ) unsafe_addr, -sizeof (buf)); +bpf_strncpy (buf, unsafe_addr); }continue ; }
kernel/trace/trace_kprobe.c +4 -1
@@ -1238,7 +1238,10 @@ fetch_store_string(unsigned long addr, void *dest, void *base)* Try to get string again, since the string can be changed while * probing. */ - ret =strncpy_from_unsafe (__dest, (void *)addr, maxlen); + ret =strncpy_from_kernel_unsafe (__dest, (void *)addr, maxlen); +if (ret <0 ) + ret =strncpy_from_user_unsafe (__dest, (void __user *)addr, + maxlen);if (ret >=0 ) *(u32 *)dest =make_data_loc (ret, __dest - base);
mm/maccess.c +1 -38
@@ -8,8 +8,6 @@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 (void *dst,const void *unsafe_src,size_t size,bool strict)
@@ -156,35 +154,6 @@ long probe_user_write(void __user *dst, const void *src, size_t size)return 0 ; } -/** -* strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address. -* @dst: Destination address, in kernel space. This buffer must be at -* least @count bytes long. -* @unsafe_addr: Unsafe address. -* @count: Maximum number of bytes to copy, including the trailing NUL. -* -* Copies a NUL-terminated string from unsafe address to kernel buffer. -* -* On success, returns the length of the string INCLUDING the trailing NUL. -* -* If access fails, returns -EFAULT (some data may have been copied -* and the trailing NUL added). -* -* 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. -* -* Same as strncpy_from_kernel_unsafe() 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 strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count) -{ -return __strncpy_from_unsafe (dst, unsafe_addr, count, false); -} -/** * strncpy_from_kernel_unsafe: - Copy a NUL terminated string from unsafe * address.
@@ -204,12 +173,6 @@ long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)* sets the last byte of @dst buffer to NUL and returns @count. */ long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count) -{ -return __strncpy_from_unsafe (dst, unsafe_addr, count, true); -} - -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;
@@ -217,7 +180,7 @@ static long __strncpy_from_unsafe(char *dst, const void *unsafe_addr,if (unlikely (count <=0 ))return 0 ; -if (!probe_kernel_read_allowed (dst, unsafe_addr, count, strict)) +if (!probe_kernel_read_allowed (dst, unsafe_addr, count, true))return -EFAULT ;set_fs (KERNEL_DS );
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 11/15] maccess: remove strncpy_from_unsafe
Quoted 6 lines · On Wed, 6 May 2020 08:22:19 +0200 Christoph Hellwig <[email protected]> wrote:
All three callers really should try the explicit kernel and user copies instead. One has already deprecated the somewhat dangerous either kernel or user address concept, the other two still need to follow up eventually.
Signed-off-by Christoph Hellwig <[email protected]>
This looks good to me.
Thank you,
Quoted 189 lines
--- include/linux/uaccess.h | 1 - kernel/trace/bpf_trace.c | 40 ++++++++++++++++++++++++++----------- kernel/trace/trace_kprobe.c | 5 ++++- mm/maccess.c | 39 +----------------------------------- 4 files changed, 33 insertions(+), 52 deletions(-)include/linux/uaccess.h +0
-1@@ -311,7 +311,6 @@ 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 notrace probe_user_write (void __user *dst,const void *src,size_t size); -extern long strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count);extern long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count); extern long strncpy_from_user_unsafe(char *dst, const void __user *unsafe_addr,kernel/trace/bpf_trace.c +28
-12@@ -229,9 +229,10 @@ bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,int ret =security_locked_down (LOCKDOWN_BPF_READ );if (unlikely (ret <0 )) - goto out; + goto fail; +/* -* The strncpy_from_unsafe_*() call will likely not fill the entire +* The strncpy_from_*_unsafe() call will likely not fill the entire * buffer, but that's okay in this circumstance as we're probing * arbitrary memory anyway similar to bpf_probe_read_*() and might * as well probe the stack. Thus, memory is explicitly cleared @@ -239,11 +240,18 @@ bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr,* code altogether don't copy garbage; otherwise length of string * is returned that can be used for bpf_perf_event_output() et al. */ - ret = compat ?strncpy_from_unsafe (dst, unsafe_ptr, size) : -strncpy_from_kernel_unsafe (dst, unsafe_ptr, size); -if (unlikely (ret <0 )) -out: -memset (dst,0 , size); + ret =strncpy_from_kernel_unsafe (dst, unsafe_ptr, size); +if (unlikely (ret <0 )) { +if (compat) + ret =strncpy_from_user_unsafe (dst, + (__force const void __user *)unsafe_ptr, + size); +if (ret <0 ) + goto fail; + } +return 0 ; +fail: +memset (dst,0 , size);return ret; }@@ -321,6 +329,17 @@ static const struct bpf_func_proto *bpf_get_probe_write_proto(void)return &bpf_probe_write_user_proto; } +#define BPF_STRNCPY_LEN 64 + +static void bpf_strncpy (char *buf,long unsafe_addr) +{ + buf[0 ] =0 ; +if (strncpy_from_kernel_unsafe (buf, (void *)unsafe_addr, +BPF_STRNCPY_LEN )) +strncpy_from_user_unsafe (buf, (void __user *)unsafe_addr, +BPF_STRNCPY_LEN ); +} +/* * Only limited trace_printk() conversion specifiers allowed: * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s @@ -332,7 +351,7 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,int mod[3 ] = {};int fmt_cnt =0 ;u64 unsafe_addr; -char buf[64 ]; +char buf[BPF_STRNCPY_LEN ];int i;/* @@ -387,10 +406,7 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, arg3 = (long ) buf;break ; } - buf[0 ] =0 ; -strncpy_from_unsafe (buf, - (void *) (long ) unsafe_addr, -sizeof (buf)); +bpf_strncpy (buf, unsafe_addr); }continue ; }kernel/trace/trace_kprobe.c +4
-1@@ -1238,7 +1238,10 @@ fetch_store_string(unsigned long addr, void *dest, void *base)* Try to get string again, since the string can be changed while * probing. */ - ret =strncpy_from_unsafe (__dest, (void *)addr, maxlen); + ret =strncpy_from_kernel_unsafe (__dest, (void *)addr, maxlen); +if (ret <0 ) + ret =strncpy_from_user_unsafe (__dest, (void __user *)addr, + maxlen);if (ret >=0 ) *(u32 *)dest =make_data_loc (ret, __dest - base);mm/maccess.c +1
-38@@ -8,8 +8,6 @@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 (void *dst,const void *unsafe_src,size_t size,bool strict)@@ -156,35 +154,6 @@ long probe_user_write(void __user *dst, const void *src, size_t size)return 0 ; } -/** -* strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address. -* @dst: Destination address, in kernel space. This buffer must be at -* least @count bytes long. -* @unsafe_addr: Unsafe address. -* @count: Maximum number of bytes to copy, including the trailing NUL. -* -* Copies a NUL-terminated string from unsafe address to kernel buffer. -* -* On success, returns the length of the string INCLUDING the trailing NUL. -* -* If access fails, returns -EFAULT (some data may have been copied -* and the trailing NUL added). -* -* 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. -* -* Same as strncpy_from_kernel_unsafe() 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 strncpy_from_unsafe (char *dst,const void *unsafe_addr,long count) -{ -return __strncpy_from_unsafe (dst, unsafe_addr, count, false); -} -/** * strncpy_from_kernel_unsafe: - Copy a NUL terminated string from unsafe * address. @@ -204,12 +173,6 @@ long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)* sets the last byte of @dst buffer to NUL and returns @count. */ long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count) -{ -return __strncpy_from_unsafe (dst, unsafe_addr, count, true); -} - -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;@@ -217,7 +180,7 @@ static long __strncpy_from_unsafe(char *dst, const void *unsafe_addr,if (unlikely (count <=0 ))return 0 ; -if (!probe_kernel_read_allowed (dst, unsafe_addr, count, strict)) +if (!probe_kernel_read_allowed (dst, unsafe_addr, count, true))return -EFAULT ;set_fs (KERNEL_DS );Signature · 1 line
2.26.2
Signature · 1 line
Masami Hiramatsu <[email protected]>
PATCH 12/15 maccess: always use strict semantics for probe_kernel_read
In reply to Christoph HellwigExcept for historical confusion in the kprobes/uprobes and bpf tracers there is no good reason to ever allow user memory accesses from probe_kernel_read. Make the tracers fall back to a probe_user_read if the probe_kernel_read falls to keep the core API clean.
--- arch/parisc/lib/memcpy.c | 3 +-- arch/um/kernel/maccess.c | 3 +-- arch/x86/mm/maccess.c | 5 +---- include/linux/uaccess.h | 4 +--- kernel/trace/bpf_trace.c | 20 +++++++++++++------ kernel/trace/trace_kprobe.c | 11 ++++++++++- mm/maccess.c | 39 ++++++------------------------------- 7 files changed, 34 insertions(+), 51 deletions(-)
arch/parisc/lib/memcpy.c +1 -2
@@ -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;
arch/um/kernel/maccess.c +1 -2
@@ -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 );
arch/x86/mm/maccess.c +1 -4
@@ -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); }
include/linux/uaccess.h +1 -3
@@ -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);
kernel/trace/bpf_trace.c +14 -6
@@ -183,12 +183,20 @@ bpf_probe_read_kernel_common(void *dst, u32 size, const void *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, + (__force const void __user *)unsafe_ptr, size); +if (unlikely (ret <0 )) + goto fail; + } + +return 0 ; +fail: +memset (dst,0 , size);return ret; }
kernel/trace/trace_kprobe.c +10 -1
@@ -1203,6 +1203,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 );
@@ -1275,7 +1278,13 @@ fetch_store_string_user(unsigned long addr, void *dest, void *base) static nokprobe_inline intprobe_mem_read (void *dest,void *src,size_t size) { -return probe_kernel_read (dest, src, size); +int ret; + + ret =probe_kernel_read (dest, src, size); +if (ret) + ret =probe_user_read (dest, (__force const void __user *)src, + size); +return ret; }static nokprobe_inline int
mm/maccess.c +6 -33
@@ -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_unsafe(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 );
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Masami Hiramatsu
maintainerRe: [PATCH 12/15] maccess: always use strict semantics for probe_kernel_read
Hi Christoph,
At first, thank you for your work on cleaning up these functions!
Quoted 3 lines · On Wed, 6 May 2020 08:22:20 +0200 Christoph Hellwig <[email protected]> wrote:
Except for historical confusion in the kprobes/uprobes and bpf tracers there is no good reason to ever allow user memory accesses from
probe_kernel_read.
Yes, thus now trace_kprobe supports "ustring" type for accessing user space memory. (If the address spaces are overwrapped, we have no way to distinguish whether an address is kernel or user)
Quoted 2 lines
Make the tracers fall back to a
probe_user_read
if theprobe_kernel_readfalls to keep the core API clean.
For trace_kprobes doesn't need to fall back. User must specify the probe should be read from user space or kernel space. This is because it has fetch_store_string_user() and probe_mem_read_user() variants.
Thank you,
Quoted 217 lines
Signed-off-by Christoph Hellwig <[email protected]>--- arch/parisc/lib/memcpy.c | 3 +-- arch/um/kernel/maccess.c | 3 +-- arch/x86/mm/maccess.c | 5 +---- include/linux/uaccess.h | 4 +--- kernel/trace/bpf_trace.c | 20 +++++++++++++------ kernel/trace/trace_kprobe.c | 11 ++++++++++- mm/maccess.c | 39 ++++++------------------------------- 7 files changed, 34 insertions(+), 51 deletions(-)arch/parisc/lib/memcpy.c +1
-2@@ -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;arch/um/kernel/maccess.c +1
-2@@ -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 );arch/x86/mm/maccess.c +1
-4@@ -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); }include/linux/uaccess.h +1
-3@@ -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);kernel/trace/bpf_trace.c +14
-6@@ -183,12 +183,20 @@ bpf_probe_read_kernel_common(void *dst, u32 size, const void *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, + (__force const void __user *)unsafe_ptr, size); +if (unlikely (ret <0 )) + goto fail; + } + +return 0 ; +fail: +memset (dst,0 , size);return ret; }kernel/trace/trace_kprobe.c +10
-1@@ -1203,6 +1203,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 );@@ -1275,7 +1278,13 @@ fetch_store_string_user(unsigned long addr, void *dest, void *base) static nokprobe_inline intprobe_mem_read (void *dest,void *src,size_t size) { -return probe_kernel_read (dest, src, size); +int ret; + + ret =probe_kernel_read (dest, src, size); +if (ret) + ret =probe_user_read (dest, (__force const void __user *)src, + size); +return ret; }static nokprobe_inline intmm/maccess.c +6
-33@@ -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_unsafe(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 );Signature · 1 line
2.26.2
Signature · 1 line
Masami Hiramatsu <[email protected]>
Re: [PATCH 12/15] maccess: always use strict semantics for probe_kernel_read
Quoted 15 lines · On Mon, 11 May 2020 14:05:36 +0900 Masami Hiramatsu <[email protected]> wrote:
Hi Christoph,
At first, thank you for your work on cleaning up these functions!
Quoted 3 lines · On Wed, 6 May 2020 08:22:20 +0200 Christoph Hellwig <[email protected]> wrote:
Except for historical confusion in the kprobes/uprobes and bpf tracers there is no good reason to ever allow user memory accesses from
probe_kernel_read.Yes, thus now
trace_kprobesupports "ustring" type for accessing user space memory. (If the address spaces are overwrapped, we have no way to distinguish whether an address is kernel or user)
Quoted 7 lines
Quoted 2 lines
Make the tracers fall back to a
probe_user_read
if theprobe_kernel_readfalls to keep the core API clean.For
trace_kprobesdoesn't need to fall back. User must specify the probe should be read from user space or kernel space. This is because it hasfetch_store_string_user()andprobe_mem_read_user()variants.
Hmm, wait, I changed my mind. The "string" type currently supports kernel and user (on some archs, e.g. x86), there is no reason to restrict it. So let's keep the behavior.
Only if users want to trace "user" data, they can use "ustring".
Thank you,
Quoted 225 lines
Thank you,
Quoted 217 lines
Signed-off-by Christoph Hellwig <[email protected]>--- arch/parisc/lib/memcpy.c | 3 +-- arch/um/kernel/maccess.c | 3 +-- arch/x86/mm/maccess.c | 5 +---- include/linux/uaccess.h | 4 +--- kernel/trace/bpf_trace.c | 20 +++++++++++++------ kernel/trace/trace_kprobe.c | 11 ++++++++++- mm/maccess.c | 39 ++++++------------------------------- 7 files changed, 34 insertions(+), 51 deletions(-)arch/parisc/lib/memcpy.c +1
-2@@ -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;arch/um/kernel/maccess.c +1
-2@@ -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 );arch/x86/mm/maccess.c +1
-4@@ -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); }include/linux/uaccess.h +1
-3@@ -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);kernel/trace/bpf_trace.c +14
-6@@ -183,12 +183,20 @@ bpf_probe_read_kernel_common(void *dst, u32 size, const void *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, + (__force const void __user *)unsafe_ptr, size); +if (unlikely (ret <0 )) + goto fail; + } + +return 0 ; +fail: +memset (dst,0 , size);return ret; }kernel/trace/trace_kprobe.c +10
-1@@ -1203,6 +1203,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 );@@ -1275,7 +1278,13 @@ fetch_store_string_user(unsigned long addr, void *dest, void *base) static nokprobe_inline intprobe_mem_read (void *dest,void *src,size_t size) { -return probe_kernel_read (dest, src, size); +int ret; + + ret =probe_kernel_read (dest, src, size); +if (ret) + ret =probe_user_read (dest, (__force const void __user *)src, + size); +return ret; }static nokprobe_inline intmm/maccess.c +6
-33@@ -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_unsafe(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 );Signature · 1 line
2.26.2Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 1 line
Masami Hiramatsu <[email protected]>
PATCH 13/15 maccess: move user access routines together
In reply to Christoph HellwigMove kernel access vs user access routines together to ease upcoming ifdefs.
--- mm/maccess.c | 110 +++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 55 deletions(-)
mm/maccess.c +55 -55
@@ -47,34 +47,6 @@ long probe_kernel_read(void *dst, const void *src, size_t size) }EXPORT_SYMBOL_GPL (probe_kernel_read); -/** -* probe_user_read(): safely attempt to read from a user-space location -* @dst: pointer to the buffer that shall take the data -* @src: address to read from. This must be a user address. -* @size: size of the data chunk -* -* Safely read from user address @src to the buffer at @dst. If a kernel fault -* happens, handle that and return -EFAULT. -*/ -long probe_user_read (void *dst,const void __user *src,size_t size) -{ -long ret = -EFAULT ; -mm_segment_t old_fs =get_fs (); - -set_fs (USER_DS ); -if (access_ok (src, size)) { -pagefault_disable (); - ret =__copy_from_user_inatomic (dst, src, size); -pagefault_enable (); - } -set_fs (old_fs); - -if (ret) -return -EFAULT ; -return 0 ; -} -EXPORT_SYMBOL_GPL (probe_user_read); -/** * probe_kernel_write(): safely attempt to write to a location * @dst: address to write to
@@ -100,33 +72,6 @@ long probe_kernel_write(void *dst, const void *src, size_t size)return 0 ; } -/** -* probe_user_write(): safely attempt to write to a user-space location -* @dst: address to write to -* @src: pointer to the data that shall be written -* @size: size of the data chunk -* -* Safely write to address @dst from the buffer at @src. If a kernel fault -* happens, handle that and return -EFAULT. -*/ -long probe_user_write (void __user *dst,const void *src,size_t size) -{ -long ret = -EFAULT ; -mm_segment_t old_fs =get_fs (); - -set_fs (USER_DS ); -if (access_ok (dst, size)) { -pagefault_disable (); - ret =__copy_to_user_inatomic (dst, src, size); -pagefault_enable (); - } -set_fs (old_fs); - -if (ret) -return -EFAULT ; -return 0 ; -} -/** * strncpy_from_kernel_unsafe: - Copy a NUL terminated string from unsafe * address.
@@ -170,6 +115,61 @@ long strncpy_from_kernel_unsafe(char *dst, const void *unsafe_addr, long count)return ret ? -EFAULT : src - unsafe_addr; } +/** +* probe_user_read(): safely attempt to read from a user-space location +* @dst: pointer to the buffer that shall take the data +* @src: address to read from. This must be a user address. +* @size: size of the data chunk +* +* Safely read from user address @src to the buffer at @dst. If a kernel fault +* happens, handle that and return -EFAULT. +*/ +long probe_user_read (void *dst,const void __user *src,size_t size) +{ +long ret = -EFAULT ; +mm_segment_t old_fs =get_fs (); + +set_fs (USER_DS ); +if (access_ok (src, size)) { +pagefault_disable (); + ret =__copy_from_user_inatomic (dst, src, size); +pagefault_enable (); + } +set_fs (old_fs); + +if (ret) +return -EFAULT ; +return 0 ; +} +EXPORT_SYMBOL_GPL (probe_user_read); + +/** +* probe_user_write(): safely attempt to write to a user-space location +* @dst: address to write to +* @src: pointer to the data that shall be written +* @size: size of the data chunk +* +* Safely write to address @dst from the buffer at @src. If a kernel fault +* happens, handle that and return -EFAULT. +*/ +long probe_user_write (void __user *dst,const void *src,size_t size) +{ +long ret = -EFAULT ; +mm_segment_t old_fs =get_fs (); + +set_fs (USER_DS ); +if (access_ok (dst, size)) { +pagefault_disable (); + ret =__copy_to_user_inatomic (dst, src, size); +pagefault_enable (); + } +set_fs (old_fs); + +if (ret) +return -EFAULT ; +return 0 ; +} +/** * strncpy_from_user_unsafe: - Copy a NUL terminated string from unsafe user * address.
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 14/15 maccess: allow architectures to provide kernel probing directly
In reply to Christoph HellwigProvide alternative versions of probe_kernel_read, probe_kernel_write and strncpy_from_kernel_unsafe that don't need set_fs magic, but instead use arch hooks that are modelled after unsafe_{get,put}_user to access kernel memory in an exception safe way.
--- mm/maccess.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
mm/maccess.c +76 -0
@@ -12,6 +12,81 @@ bool __weak probe_kernel_read_allowed(void *dst, const void *unsafe_src,return true; } +#ifdef HAVE_ARCH_PROBE_KERNEL + +#define probe_kernel_read_loop (dst, src, len, type, err_label) \ + while (len >= sizeof(type)) { \ + arch_kernel_read(dst, src, type, err_label); \ + dst += sizeof(type); \ + src += sizeof(type); \ + len -= sizeof(type); \ + } + +long probe_kernel_read (void *dst,const void *src,size_t size) +{ +if (!probe_kernel_read_allowed (dst, src, size)) +return -EFAULT ; + +pagefault_disable (); +probe_kernel_read_loop (dst, src, size, u64, Efault); +probe_kernel_read_loop (dst, src, size, u32, Efault); +probe_kernel_read_loop (dst, src, size, u16, Efault); +probe_kernel_read_loop (dst, src, size, u8, Efault); +pagefault_enable (); +return 0 ; +Efault: +pagefault_enable (); +return -EFAULT ; +} +EXPORT_SYMBOL_GPL (probe_kernel_read); + +#define probe_kernel_write_loop (dst, src, len, type, err_label) \ + while (len >= sizeof(type)) { \ + arch_kernel_write(dst, src, type, err_label); \ + dst += sizeof(type); \ + src += sizeof(type); \ + len -= sizeof(type); \ + } + +long probe_kernel_write (void *dst,const void *src,size_t size) +{ +pagefault_disable (); +probe_kernel_write_loop (dst, src, size, u64, Efault); +probe_kernel_write_loop (dst, src, size, u32, Efault); +probe_kernel_write_loop (dst, src, size, u16, Efault); +probe_kernel_write_loop (dst, src, size, u8, Efault); +pagefault_enable (); +return 0 ; +Efault: +pagefault_enable (); +return -EFAULT ; +} + +long strncpy_from_kernel_unsafe (char *dst,const void *unsafe_addr,long count) +{ +const void *src = unsafe_addr; + +if (unlikely (count <=0 )) +return 0 ; +if (!probe_kernel_read_allowed (dst, unsafe_addr, count)) +return -EFAULT ; + +pagefault_disable (); +do { +arch_kernel_read (dst, src, u8, Efault); + dst++; + src++; + }while (dst[-1 ] && src - unsafe_addr < count); +pagefault_enable (); + + dst[-1 ] ='\0' ; +return src - unsafe_addr; +Efault: +pagefault_enable (); + dst[-1 ] ='\0' ; +return -EFAULT ; +} +#else /* HAVE_ARCH_PROBE_KERNEL */ /** * probe_kernel_read(): safely attempt to read from kernel-space * @dst: pointer to the buffer that shall take the data
@@ -114,6 +189,7 @@ long strncpy_from_kernel_unsafe(char *dst, const void *unsafe_addr, long count) return ret ? -EFAULT : src - unsafe_addr; } +#endif /* HAVE_ARCH_PROBE_KERNEL */ /** * probe_user_read(): safely attempt to read from a user-space location
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
PATCH 15/15 x86: use non-set_fs based maccess routines
In reply to Christoph HellwigProvide arch_kernel_read and arch_kernel_write routines to implement the maccess routines without messing with set_fs and without stac/clac that opens up access to user space.
--- arch/x86/include/asm/uaccess.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/x86/include/asm/uaccess.h +16 -0
@@ -523,5 +523,21 @@ do { \unsafe_copy_loop (__ucu_dst, __ucu_src, __ucu_len, u8, label); \ } while (0) +#define HAVE_ARCH_PROBE_KERNEL + +#define arch_kernel_read (dst, src, type, err_label) \ +do { \ + int __kr_err; \ + \ + __get_user_size(*((type *)dst), (__force type __user *)src, \ + sizeof(type), __kr_err); \ + if (unlikely(__kr_err)) \ + goto err_label; \ +} while (0) + +#define arch_kernel_write (dst, src, type, err_label) \ + __put_user_size(*((type *)(src)), (__force type __user *)(dst), \ + sizeof(type), err_label) +#endif /* _ASM_X86_UACCESS_H */
Signature · 7 lines
2.26.2 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 15/15] x86: use non-set_fs based maccess routines
Quoted 4 lines · Christoph Hellwig
arch/x86/include/asm/uaccess.h +3
-0+#define arch_kernel_read (dst, src, type, err_label) \ + __get_user_size(*((type *)dst), (__force type __user *)src, \ + sizeof(type), __kr_err); \
..
arch/x86/include/asm/uaccess.h do { \ · line 540
Quoted 3 lines
arch/x86/include/asm/uaccess.h +3
-0+#define arch_kernel_write (dst, src, type, err_label) \ + __put_user_size(*((type *)(src)), (__force type __user *)(dst), \ + sizeof(type), err_label)
My private tree no longer has those __get/put_user_size() things, because "unsafe_get/put_user()" is the only thing that remains with my conversion to asm goto.
And we're actively trying to get rid of the whole __get_user() mess. Admittedly "__get_user_size()" is just the internal helper that doesn't have the problem, but it really is an internal helper for a legacy operation, and the new op that uses it is that "unsafe_get_user()".
Also, because you use __get_user_size(), you then have to duplicate the error handling logic that we already have in unsafe_get_user().
IOW - is there some reason why you didn't just make these use "unsafe_get/put_user()" directly, and avoid both of those issues?
Linus
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Re: [PATCH 15/15] x86: use non-set_fs based maccess routines
Quoted 10 lines · Linus Torvalds
My private tree no longer has those __get/put_user_size() things, because "unsafe_get/put_user()" is the only thing that remains with my conversion to asm goto.
And we're actively trying to get rid of the whole
__get_user()mess. Admittedly "__get_user_size()" is just the internal helper that doesn't have the problem, but it really is an internal helper for a legacy operation, and the new op that uses it is that "unsafe_get_user()".
Quoted 5 lines
Also, because you use
__get_user_size(), you then have to duplicate the error handling logic that we already have inunsafe_get_user().IOW - is there some reason why you didn't just make these use "unsafe_get/put_user()" directly, and avoid both of those issues?
That was the first prototype, and or x86 it works great, just the __user cases in maccess.c are a little ugly. And they point to the real problem - for architectures like sparc and s390 that use an entirely separate address space for the kernel vs userspace I dont think just use unsafe_{get,put}_user will work, as they need different instructions.
Btw, where is you magic private tree and what is the plan for it?
Re: [PATCH 15/15] x86: use non-set_fs based maccess routines
Quoted 7 lines · Christoph Hellwig
That was the first prototype, and or x86 it works great, just the
__usercases inmaccess.care a little ugly. And they point to the real problem - for architectures like sparc and s390 that use an entirely separate address space for the kernel vs userspace I dont think just use unsafe_{get,put}_user will work, as they need different instructions.
Oh, absolutely. I did *NOT* mean that you'd use "unsafe_get_user()" as the actual interface. I just meant that as an implementation detail on x86, using "unsafe_get_user()" instead of "__get_user_size()" internally both simplifies the implementation, and means that it doesn't clash horribly with my local changes.
Btw, that brings up another issue: so that people can't mis-use those kernel accessors and use them for user addresses, they probably should actually do something like
if ((long)addr >= 0)
goto error_label;on x86. IOW, have the "strict" kernel pointer behavior.
Otherwise somebody will start using them for user pointers, and it will happen to work on old x86 without CLAC/STAC support.
Of course, maybe CLAC/STAC is so common these days (at least with developers) that we don't have to worry about it.
Quoted 1 line
Btw, where is you magic private tree and what is the plan for it?
I don't want to make it a public branch, but here's a private bundle.
It's based on top of my current -git tree - I just maintain a separate tree that I keep up-to-date locally for testing. My "normal" tree I do build tests on (allmodconfig etc), this separate tree I keep around to actually do boot tests on, and I end up using "current Linus' tree plus this" as the code I actually run om my main desktop.
But this *ONLY* works with clang, and only with current HEAD of the clang development tree. So it's almosty entirely useless to anybody else right now. You literally have to clone the llvm tree, build your own clang, and install it to even _build_ this.
I'm not planning on going any further than my local testing until the whole thing calms down. The llvm tree still has some known bugs in the asm goto with output area, and I want there to be an actual release of it before I actually merge anything like this (and I need to do the small extra work to then have that conditional "does the compiler support asm goto with outputs" so that it works with gcc too).
But here you see what it is, if you want to. __get_user_size() technically still exists, but it has the "target branch" semantics in here, so your patch clashes badly with it. (Well, those are the semantics you want, so "badly" may not be the right word, but basically it means that if you _had_ used unsafe_get_user(), there wouldn't have been those kinds of semantic conflicts).
Linus
Re: [PATCH 15/15] x86: use non-set_fs based maccess routines
Quoted 5 lines · Linus Torvalds
Oh, absolutely. I did *NOT* mean that you'd use "
unsafe_get_user()" as the actual interface. I just meant that as an implementation detail on x86, using "unsafe_get_user()" instead of "__get_user_size()" internally both simplifies the implementation, and means that it doesn't clash horribly with my local changes.
I had a version that just wrapped them, but somehow wasn't able to make it work due to all the side effects vs macros issues. Maybe I need to try again, the current version seemed like a nice way out as it avoided a lot of the silly casting.
Quoted 7 lines
Btw, that brings up another issue: so that people can't mis-use those kernel accessors and use them for user addresses, they probably should actually do something like
if ((long)addr >= 0) goto error_label;
Quoted 7 lines
on x86. IOW, have the "strict" kernel pointer behavior.
Otherwise somebody will start using them for user pointers, and it will happen to work on old x86 without CLAC/STAC support.
Of course, maybe CLAC/STAC is so common these days (at least with developers) that we don't have to worry about it.
The actual public routines (probe_kernel_read and co) get these checks through probe_kernel_read_allowed, which is implemented by the x86 code. Doing this for every 1-8 byte access might be a little slow, though. Do you really fear drivers starting to use the low-level helper? Maybe we need to move those into a different header than <asm/uaccess.h> that makes it more clear that they are internal?
Quoted 3 lines
But here you see what it is, if you want to.
__get_user_size()technically still exists, but it has the "target branch" semantics in here, so your patch clashes badly with it.
The target branch semantics actually are what I want, that is how the maccess code is structured. This is the diff I'd need for the calling conventions in your bundle:
arch/x86/include/asm/uaccess.h +1 -7
@@ -526,14 +526,8 @@ do { \#define HAVE_ARCH_PROBE_KERNEL #define arch_kernel_read (dst, src, type, err_label) \ -do { \ - int __kr_err; \ - \ __get_user_size(*((type *)dst), (__force type __user *)src, \ - sizeof(type), __kr_err); \ - if (unlikely(__kr_err)) \ - goto err_label; \ -} while (0) + sizeof(type), err_label); \#define arch_kernel_write (dst, src, type, err_label) \ __put_user_size(*((type *)(src)), (__force type __user *)(dst), \