* [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy()
@ 2017-12-02 13:27 Heiko Carstens
2017-12-02 20:59 ` Kees Cook
2017-12-07 0:43 ` Andrew Morton
0 siblings, 2 replies; 4+ messages in thread
From: Heiko Carstens @ 2017-12-02 13:27 UTC (permalink / raw)
To: Linus Torvalds, Andrew Morton
Cc: Kees Cook, Jiri Olsa, Al Viro, linux-kernel, Heiko Carstens
git commit df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext
data") added a bounce buffer to avoid hardened usercopy
checks. Copying to the bounce buffer was implemented with a simple
memcpy() assuming that it is always valid to read from kernel memory
iff the kern_addr_valid() check passed.
A simple, but pointless, test case like "dd if=/proc/kcore
of=/dev/null" now can easily crash the kernel, since the former
execption handling on invalid kernel addresses now doesn't work
anymore.
Also adding a kern_addr_valid() implementation wouldn't help
here. Most architectures simply return 1 here, while a couple
implemented a page table walk to figure out if something is mapped at
the address in question.
With DEBUG_PAGEALLOC active mappings are established and removed all
the time, so that relying on the result of kern_addr_valid() before
executing the memcpy() also doesn't work.
Therefore simply use probe_kernel_read() to copy to the bounce
buffer. This also allows to simplify read_kcore().
At least on s390 this fixes the observed crashes and doesn't introduce
warnings that were removed with df04abfd181a ("fs/proc/kcore.c: Add
bounce buffer for ktext data"), even though the generic
probe_kernel_read() implementation uses uaccess functions.
While looking into this I'm also wondering if kern_addr_valid() could
be completely removed...(?)
Fixes: df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data")
Fixes: f5509cc18daa ("mm: Hardened usercopy")
Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
fs/proc/kcore.c | 18 +++++-------------
1 file changed, 5 insertions(+), 13 deletions(-)
diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
index 4bc85cb8be6a..e8a93bc8285d 100644
--- a/fs/proc/kcore.c
+++ b/fs/proc/kcore.c
@@ -512,23 +512,15 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
return -EFAULT;
} else {
if (kern_addr_valid(start)) {
- unsigned long n;
-
/*
* Using bounce buffer to bypass the
* hardened user copy kernel text checks.
*/
- memcpy(buf, (char *) start, tsz);
- n = copy_to_user(buffer, buf, tsz);
- /*
- * We cannot distinguish between fault on source
- * and fault on destination. When this happens
- * we clear too and hope it will trigger the
- * EFAULT again.
- */
- if (n) {
- if (clear_user(buffer + tsz - n,
- n))
+ if (probe_kernel_read(buf, (void *) start, tsz)) {
+ if (clear_user(buffer, tsz))
+ return -EFAULT;
+ } else {
+ if (copy_to_user(buffer, buf, tsz))
return -EFAULT;
}
} else {
--
2.13.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy()
2017-12-02 13:27 [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy() Heiko Carstens
@ 2017-12-02 20:59 ` Kees Cook
2017-12-07 0:43 ` Andrew Morton
1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2017-12-02 20:59 UTC (permalink / raw)
To: Heiko Carstens; +Cc: Linus Torvalds, Andrew Morton, Jiri Olsa, Al Viro, LKML
On Sat, Dec 2, 2017 at 5:27 AM, Heiko Carstens
<heiko.carstens@de.ibm.com> wrote:
> git commit df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext
> data") added a bounce buffer to avoid hardened usercopy
> checks. Copying to the bounce buffer was implemented with a simple
> memcpy() assuming that it is always valid to read from kernel memory
> iff the kern_addr_valid() check passed.
>
> A simple, but pointless, test case like "dd if=/proc/kcore
> of=/dev/null" now can easily crash the kernel, since the former
> execption handling on invalid kernel addresses now doesn't work
> anymore.
>
> Also adding a kern_addr_valid() implementation wouldn't help
> here. Most architectures simply return 1 here, while a couple
> implemented a page table walk to figure out if something is mapped at
> the address in question.
> With DEBUG_PAGEALLOC active mappings are established and removed all
> the time, so that relying on the result of kern_addr_valid() before
> executing the memcpy() also doesn't work.
>
> Therefore simply use probe_kernel_read() to copy to the bounce
> buffer. This also allows to simplify read_kcore().
>
> At least on s390 this fixes the observed crashes and doesn't introduce
> warnings that were removed with df04abfd181a ("fs/proc/kcore.c: Add
> bounce buffer for ktext data"), even though the generic
> probe_kernel_read() implementation uses uaccess functions.
>
> While looking into this I'm also wondering if kern_addr_valid() could
> be completely removed...(?)
>
> Fixes: df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data")
> Fixes: f5509cc18daa ("mm: Hardened usercopy")
> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
Thanks for the catch! Yeah, this matches what I just sent to Greg for /dev/mem:
https://lkml.org/lkml/2017/12/1/792
Acked-by: Kees Cook <keescook@chromium.org>
-Kees
> ---
> fs/proc/kcore.c | 18 +++++-------------
> 1 file changed, 5 insertions(+), 13 deletions(-)
>
> diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
> index 4bc85cb8be6a..e8a93bc8285d 100644
> --- a/fs/proc/kcore.c
> +++ b/fs/proc/kcore.c
> @@ -512,23 +512,15 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
> return -EFAULT;
> } else {
> if (kern_addr_valid(start)) {
> - unsigned long n;
> -
> /*
> * Using bounce buffer to bypass the
> * hardened user copy kernel text checks.
> */
> - memcpy(buf, (char *) start, tsz);
> - n = copy_to_user(buffer, buf, tsz);
> - /*
> - * We cannot distinguish between fault on source
> - * and fault on destination. When this happens
> - * we clear too and hope it will trigger the
> - * EFAULT again.
> - */
> - if (n) {
> - if (clear_user(buffer + tsz - n,
> - n))
> + if (probe_kernel_read(buf, (void *) start, tsz)) {
> + if (clear_user(buffer, tsz))
> + return -EFAULT;
> + } else {
> + if (copy_to_user(buffer, buf, tsz))
> return -EFAULT;
> }
> } else {
> --
> 2.13.5
>
--
Kees Cook
Pixel Security
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy()
2017-12-02 13:27 [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy() Heiko Carstens
2017-12-02 20:59 ` Kees Cook
@ 2017-12-07 0:43 ` Andrew Morton
2017-12-07 8:41 ` Heiko Carstens
1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2017-12-07 0:43 UTC (permalink / raw)
To: Heiko Carstens
Cc: Linus Torvalds, Kees Cook, Jiri Olsa, Al Viro, linux-kernel
On Sat, 2 Dec 2017 14:27:39 +0100 Heiko Carstens <heiko.carstens@de.ibm.com> wrote:
> git commit df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext
> data") added a bounce buffer to avoid hardened usercopy
> checks. Copying to the bounce buffer was implemented with a simple
> memcpy() assuming that it is always valid to read from kernel memory
> iff the kern_addr_valid() check passed.
>
> A simple, but pointless, test case like "dd if=/proc/kcore
> of=/dev/null" now can easily crash the kernel, since the former
> execption handling on invalid kernel addresses now doesn't work
> anymore.
>
> Also adding a kern_addr_valid() implementation wouldn't help
> here. Most architectures simply return 1 here, while a couple
> implemented a page table walk to figure out if something is mapped at
> the address in question.
> With DEBUG_PAGEALLOC active mappings are established and removed all
> the time, so that relying on the result of kern_addr_valid() before
> executing the memcpy() also doesn't work.
>
> Therefore simply use probe_kernel_read() to copy to the bounce
> buffer. This also allows to simplify read_kcore().
>
> At least on s390 this fixes the observed crashes and doesn't introduce
> warnings that were removed with df04abfd181a ("fs/proc/kcore.c: Add
> bounce buffer for ktext data"), even though the generic
> probe_kernel_read() implementation uses uaccess functions.
>
> While looking into this I'm also wondering if kern_addr_valid() could
> be completely removed...(?)
>
> Fixes: df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data")
> Fixes: f5509cc18daa ("mm: Hardened usercopy")
It's a privileged operation, but oopsing root's kernel is still a bit
rude. So I'll add cc:stable. And let it bake until 4.16-rc1, since
the bug has been there for a year or more. Sound OK?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy()
2017-12-07 0:43 ` Andrew Morton
@ 2017-12-07 8:41 ` Heiko Carstens
0 siblings, 0 replies; 4+ messages in thread
From: Heiko Carstens @ 2017-12-07 8:41 UTC (permalink / raw)
To: Andrew Morton; +Cc: Linus Torvalds, Kees Cook, Jiri Olsa, Al Viro, linux-kernel
On Wed, Dec 06, 2017 at 04:43:50PM -0800, Andrew Morton wrote:
> On Sat, 2 Dec 2017 14:27:39 +0100 Heiko Carstens <heiko.carstens@de.ibm.com> wrote:
>
> > git commit df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext
> > data") added a bounce buffer to avoid hardened usercopy
> > checks. Copying to the bounce buffer was implemented with a simple
> > memcpy() assuming that it is always valid to read from kernel memory
> > iff the kern_addr_valid() check passed.
> >
> > A simple, but pointless, test case like "dd if=/proc/kcore
> > of=/dev/null" now can easily crash the kernel, since the former
> > execption handling on invalid kernel addresses now doesn't work
> > anymore.
> >
> > Also adding a kern_addr_valid() implementation wouldn't help
> > here. Most architectures simply return 1 here, while a couple
> > implemented a page table walk to figure out if something is mapped at
> > the address in question.
> > With DEBUG_PAGEALLOC active mappings are established and removed all
> > the time, so that relying on the result of kern_addr_valid() before
> > executing the memcpy() also doesn't work.
> >
> > Therefore simply use probe_kernel_read() to copy to the bounce
> > buffer. This also allows to simplify read_kcore().
> >
> > At least on s390 this fixes the observed crashes and doesn't introduce
> > warnings that were removed with df04abfd181a ("fs/proc/kcore.c: Add
> > bounce buffer for ktext data"), even though the generic
> > probe_kernel_read() implementation uses uaccess functions.
> >
> > While looking into this I'm also wondering if kern_addr_valid() could
> > be completely removed...(?)
> >
> > Fixes: df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data")
> > Fixes: f5509cc18daa ("mm: Hardened usercopy")
>
> It's a privileged operation, but oopsing root's kernel is still a bit
> rude. So I'll add cc:stable. And let it bake until 4.16-rc1, since
> the bug has been there for a year or more. Sound OK?
Yes, that sounds ok to me. Thank you!
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-12-07 8:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-02 13:27 [PATCH] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy() Heiko Carstens
2017-12-02 20:59 ` Kees Cook
2017-12-07 0:43 ` Andrew Morton
2017-12-07 8:41 ` Heiko Carstens
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).