Commit d869844b authored by Linus Torvalds's avatar Linus Torvalds

x86: fix special __probe_kernel_write() tail zeroing case

Commit cae2a173 ("x86: clean up/fix 'copy_in_user()' tail zeroing")
fixed the failure case tail zeroing of one special case of the x86-64
generic user-copy routine, namely when used for the user-to-user case
("copy_in_user()").

But in the process it broke an even more unusual case: using the user
copy routine for kernel-to-kernel copying.

Now, normally kernel-kernel copies are obviously done using memcpy(),
but we have a couple of special cases when we use the user-copy
functions.  One is when we pass a kernel buffer to a regular user-buffer
routine, using set_fs(KERNEL_DS).  That's a "normal" case, and continued
to work fine, because it never takes any faults (with the possible
exception of a silent and successful vmalloc fault).

But Jan Beulich pointed out another, very unusual, special case: when we
use the user-copy routines not because it's a path that expects a user
pointer, but for a couple of ftrace/kgdb cases that want to do a kernel
copy, but do so using "unsafe" buffers, and use the user-copy routine to
gracefully handle faults.  IOW, for probe_kernel_write().

And that broke for the case of a faulting kernel destination, because we
saw the kernel destination and wanted to try to clear the tail of the
buffer.  Which doesn't work, since that's what faults.

This only triggers for things like kgdb and ftrace users (eg trying
setting a breakpoint on read-only memory), but it's definitely a bug.
The fix is to not compare against the kernel address start (TASK_SIZE),
but instead use the same limits "access_ok()" uses.
Reported-and-tested-by: default avatarJan Beulich <jbeulich@suse.com>
Cc: stable@vger.kernel.org # 4.0
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 27cf3a16
...@@ -82,7 +82,7 @@ copy_user_handle_tail(char *to, char *from, unsigned len) ...@@ -82,7 +82,7 @@ copy_user_handle_tail(char *to, char *from, unsigned len)
clac(); clac();
/* If the destination is a kernel buffer, we always clear the end */ /* If the destination is a kernel buffer, we always clear the end */
if ((unsigned long)to >= TASK_SIZE_MAX) if (!__addr_ok(to))
memset(to, 0, len); memset(to, 0, len);
return len; return len;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment