Skip to content

Commit 2a71e81

Browse files
Christoph Hellwigtorvalds
authored andcommitted
maccess: return -ERANGE when probe_kernel_read() fails
Allow the callers to distinguish a real unmapped address vs a range that can't be probed. Suggested-by: Masami Hiramatsu <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Masami Hiramatsu <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent fa94111 commit 2a71e81

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

mm/maccess.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bool __weak probe_kernel_read_allowed(const void *unsafe_src, size_t size)
2424
long probe_kernel_read(void *dst, const void *src, size_t size)
2525
{
2626
if (!probe_kernel_read_allowed(src, size))
27-
return -EFAULT;
27+
return -ERANGE;
2828

2929
pagefault_disable();
3030
probe_kernel_read_loop(dst, src, size, u64, Efault);
@@ -68,7 +68,7 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
6868
if (unlikely(count <= 0))
6969
return 0;
7070
if (!probe_kernel_read_allowed(unsafe_addr, count))
71-
return -EFAULT;
71+
return -ERANGE;
7272

7373
pagefault_disable();
7474
do {
@@ -93,7 +93,8 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
9393
* @size: size of the data chunk
9494
*
9595
* Safely read from kernel address @src to the buffer at @dst. If a kernel
96-
* fault happens, handle that and return -EFAULT.
96+
* fault happens, handle that and return -EFAULT. If @src is not a valid kernel
97+
* address, return -ERANGE.
9798
*
9899
* We ensure that the copy_from_user is executed in atomic context so that
99100
* do_page_fault() doesn't attempt to take mmap_lock. This makes
@@ -106,7 +107,7 @@ long probe_kernel_read(void *dst, const void *src, size_t size)
106107
mm_segment_t old_fs = get_fs();
107108

108109
if (!probe_kernel_read_allowed(src, size))
109-
return -EFAULT;
110+
return -ERANGE;
110111

111112
set_fs(KERNEL_DS);
112113
pagefault_disable();
@@ -158,8 +159,9 @@ long probe_kernel_write(void *dst, const void *src, size_t size)
158159
*
159160
* On success, returns the length of the string INCLUDING the trailing NUL.
160161
*
161-
* If access fails, returns -EFAULT (some data may have been copied
162-
* and the trailing NUL added).
162+
* If access fails, returns -EFAULT (some data may have been copied and the
163+
* trailing NUL added). If @unsafe_addr is not a valid kernel address, return
164+
* -ERANGE.
163165
*
164166
* If @count is smaller than the length of the string, copies @count-1 bytes,
165167
* sets the last byte of @dst buffer to NUL and returns @count.
@@ -173,7 +175,7 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
173175
if (unlikely(count <= 0))
174176
return 0;
175177
if (!probe_kernel_read_allowed(unsafe_addr, count))
176-
return -EFAULT;
178+
return -ERANGE;
177179

178180
set_fs(KERNEL_DS);
179181
pagefault_disable();

0 commit comments

Comments
 (0)