mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC] fs/kcore: change copy_to_user to copy_in_user
@ 2015-08-20  8:59 yalin wang
  2015-08-20  9:58 ` Frans Klaver
  2015-08-21  0:05 ` Linus Torvalds
  0 siblings, 2 replies; 4+ messages in thread
From: yalin wang @ 2015-08-20  8:59 UTC (permalink / raw)
  To: bp, mingo, dave, bhe, akpm, yalin.wang2010, linux-kernel

the copy_to_user() here expect can fix the fault on both kernel and
user address, this is not true on other platforms except x86,
change to user copy_in_user() so that can detect the page fault,
work as expected.

Signed-off-by: yalin wang <yalin.wang2010@gmail.com>
---
 fs/proc/kcore.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
index 92e6726..4f28deb 100644
--- a/fs/proc/kcore.c
+++ b/fs/proc/kcore.c
@@ -515,8 +515,12 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
 		} else {
 			if (kern_addr_valid(start)) {
 				unsigned long n;
-
-				n = copy_to_user(buffer, (char *)start, tsz);
+				if ((start + tsz < tsz) ||
+						(start + tsz) > TASK_SIZE)
+					return -EFAULT;
+				set_fs(KERNEL_DS);
+				n = copy_in_user(buffer, (char *)start, tsz);
+				set_fs(USER_DS);
 				/*
 				 * We cannot distinguish between fault on source
 				 * and fault on destination. When this happens
-- 
1.9.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] fs/kcore: change copy_to_user to copy_in_user
  2015-08-20  8:59 [RFC] fs/kcore: change copy_to_user to copy_in_user yalin wang
@ 2015-08-20  9:58 ` Frans Klaver
  2015-08-20 10:45   ` yalin wang
  2015-08-21  0:05 ` Linus Torvalds
  1 sibling, 1 reply; 4+ messages in thread
From: Frans Klaver @ 2015-08-20  9:58 UTC (permalink / raw)
  To: yalin wang; +Cc: bp, Ingo Molnar, dave, bhe, Andrew Morton, linux-kernel

On Thu, Aug 20, 2015 at 10:59 AM, yalin wang <yalin.wang2010@gmail.com> wrote:
> the copy_to_user() here expect can fix the fault on both kernel and
> user address, this is not true on other platforms except x86,
> change to user copy_in_user() so that can detect the page fault,
> work as expected.

Could you rephrase this into multiple sentences in comprehensible
English? What is the expected behavior, what is the unexpected
behavior and what can people do to trigger it?


> Signed-off-by: yalin wang <yalin.wang2010@gmail.com>
> ---
>  fs/proc/kcore.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
> index 92e6726..4f28deb 100644
> --- a/fs/proc/kcore.c
> +++ b/fs/proc/kcore.c
> @@ -515,8 +515,12 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
>                 } else {
>                         if (kern_addr_valid(start)) {
>                                 unsigned long n;
> -
> -                               n = copy_to_user(buffer, (char *)start, tsz);
> +                               if ((start + tsz < tsz) ||
> +                                               (start + tsz) > TASK_SIZE)
> +                                       return -EFAULT;
> +                               set_fs(KERNEL_DS);
> +                               n = copy_in_user(buffer, (char *)start, tsz);
> +                               set_fs(USER_DS);
>                                 /*
>                                  * We cannot distinguish between fault on source
>                                  * and fault on destination. When this happens
> --
> 1.9.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] fs/kcore: change copy_to_user to copy_in_user
  2015-08-20  9:58 ` Frans Klaver
@ 2015-08-20 10:45   ` yalin wang
  0 siblings, 0 replies; 4+ messages in thread
From: yalin wang @ 2015-08-20 10:45 UTC (permalink / raw)
  To: Frans Klaver; +Cc: bp, Ingo Molnar, dave, bhe, Andrew Morton, linux-kernel


> On Aug 20, 2015, at 17:58, Frans Klaver <fransklaver@gmail.com> wrote:
> 
> On Thu, Aug 20, 2015 at 10:59 AM, yalin wang <yalin.wang2010@gmail.com> wrote:
>> the copy_to_user() here expect can fix the fault on both kernel and
>> user address, this is not true on other platforms except x86,
>> change to user copy_in_user() so that can detect the page fault,
>> work as expected.
> 
> Could you rephrase this into multiple sentences in comprehensible
> English? What is the expected behavior, what is the unexpected
> behavior and what can people do to trigger it?
> 
ok, i will send a V2 patch.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] fs/kcore: change copy_to_user to copy_in_user
  2015-08-20  8:59 [RFC] fs/kcore: change copy_to_user to copy_in_user yalin wang
  2015-08-20  9:58 ` Frans Klaver
@ 2015-08-21  0:05 ` Linus Torvalds
  1 sibling, 0 replies; 4+ messages in thread
From: Linus Torvalds @ 2015-08-21  0:05 UTC (permalink / raw)
  To: yalin wang
  Cc: Borislav Petkov, Ingo Molnar, Dave Hansen, bhe, Andrew Morton,
	Linux Kernel Mailing List

On Thu, Aug 20, 2015 at 1:59 AM, yalin wang <yalin.wang2010@gmail.com> wrote:
> -
> -                               n = copy_to_user(buffer, (char *)start, tsz);
> +                               if ((start + tsz < tsz) ||
> +                                               (start + tsz) > TASK_SIZE)
> +                                       return -EFAULT;

This is wrong. You apparently want to have

        if (!access_ok(start, tsz))
                return -EFAULT;

> +                               set_fs(KERNEL_DS);
> +                               n = copy_in_user(buffer, (char *)start, tsz);
> +                               set_fs(USER_DS);

.. and this is actually worse and even less portable than what we have
now, in that it's actively wrong on platforms that may have a user
address and a kernel address with the same value (ie they have
explicitly separate kernel/user address spaces).

Now, that's admittedly unusual, but I think sparc32 actually can do that.

Anyway, I absolutely detest this patch. It replaces one piece of code
that admittedly doesn't work on all architectures because kernel
memory is accessed without testing, with another hack that happens to
work on other architectures and is fragile and prone to be a security
issue.

In other words, I think the end result is _worse_ than the current situation.

You probably want to use "probe_kernel_read()" and do it into a
temporary buffer, and then just do the copy_to_user() from the
temporary buffer. Sure, it's less efficient, but at least it's not
actively wrong and a possible security problem in the long run.

                    Linus

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-08-21  0:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-20  8:59 [RFC] fs/kcore: change copy_to_user to copy_in_user yalin wang
2015-08-20  9:58 ` Frans Klaver
2015-08-20 10:45   ` yalin wang
2015-08-21  0:05 ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®