All of lore.kernel.org
 help / color / mirror / Atom feed
From: Walter Wu <walter-zh.wu@mediatek.com>
To: Dmitry Vyukov <dvyukov@google.com>
Cc: Marc Gonzalez <marc.w.gonzalez@free.fr>,
	LKML <linux-kernel@vger.kernel.org>,
	kasan-dev <kasan-dev@googlegroups.com>,
	Alexander Potapenko <glider@google.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	"Linux ARM" <linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH] kasan: fix the missing underflow in memmove and memcpy with CONFIG_KASAN_GENERIC=y
Date: Tue, 1 Oct 2019 11:18:36 +0800	[thread overview]
Message-ID: <1569899916.17361.36.camel@mtksdccf07> (raw)
In-Reply-To: <CACT4Y+b3NPemYwJJsD_oC0vde5Ybz1qDNWb=cFu2HpOTMrGSnQ@mail.gmail.com>

On Tue, 2019-10-01 at 05:01 +0200, Dmitry Vyukov wrote:
> On Tue, Oct 1, 2019 at 4:36 AM Walter Wu <walter-zh.wu@mediatek.com> wrote:
> >
> > On Mon, 2019-09-30 at 10:57 +0200, Marc Gonzalez wrote:
> > > On 30/09/2019 06:36, Walter Wu wrote:
> > >
> > > >  bool check_memory_region(unsigned long addr, size_t size, bool write,
> > > >                                 unsigned long ret_ip)
> > > >  {
> > > > +       if (long(size) < 0) {
> > > > +               kasan_report_invalid_size(src, dest, len, _RET_IP_);
> > > > +               return false;
> > > > +       }
> > > > +
> > > >         return check_memory_region_inline(addr, size, write, ret_ip);
> > > >  }
> > >
> > > Is it expected that memcpy/memmove may sometimes (incorrectly) be passed
> > > a negative value? (It would indeed turn up as a "large" size_t)
> > >
> > > IMO, casting to long is suspicious.
> > >
> > > There seem to be some two implicit assumptions.
> > >
> > > 1) size >= ULONG_MAX/2 is invalid input
> > > 2) casting a size >= ULONG_MAX/2 to long yields a negative value
> > >
> > > 1) seems reasonable because we can't copy more than half of memory to
> > > the other half of memory. I suppose the constraint could be even tighter,
> > > but it's not clear where to draw the line, especially when considering
> > > 32b vs 64b arches.
> > >
> > > 2) is implementation-defined, and gcc works "as expected" (clang too
> > > probably) https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
> > >
> > > A comment might be warranted to explain the rationale.
> > > Regards.
> >
> > Thanks for your suggestion.
> > Yes, It is passed a negative value issue in memcpy/memmove/memset.
> > Our current idea should be assumption 1 and only consider 64b arch,
> > because KASAN only supports 64b. In fact, we really can't use so much
> > memory in 64b arch. so assumption 1 make sense.
> 
> Note there are arm KASAN patches floating around, so we should not
> make assumptions about 64-bit arch.
I think arm KASAN patch doesn't merge in mainline, because virtual
memory of shadow memory is so bigger, the kernel virtual memory only has
1GB or 2GB in 32-bit arch, it is hard to solve the issue. it may need
some trade-off.

> 
> But there seems to be a number of such casts already:
> 
It seems that everyone is the same assumption.

> $ find -name "*.c" -exec egrep "\(long\).* < 0" {} \; -print
>     } else if ((long) delta < 0) {
> ./kernel/time/timer.c
>     if ((long)state < 0)
> ./drivers/thermal/thermal_sysfs.c
>     if ((long)delay < 0)
> ./drivers/infiniband/core/addr.c
>     if ((long)tmo < 0)
> ./drivers/net/wireless/st/cw1200/pm.c
>     if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
> ./sound/core/info.c
>         if ((long)hwrpb->sys_type < 0) {
> ./arch/alpha/kernel/setup.c
>     if ((long)m->driver_data < 0)
> ./arch/x86/kernel/apic/apic.c
>             if ((long) size < 0L)
>     if ((long)addr < 0L) {
> ./arch/sparc/mm/init_64.c
>     if ((long)lpid < 0)
> ./arch/powerpc/kvm/book3s_hv.c
>             if ((long)regs->regs[insn.mm_i_format.rs] < 0)
>             if ((long)regs->regs[insn.i_format.rs] < 0) {
>             if ((long)regs->regs[insn.i_format.rs] < 0) {
> ./arch/mips/kernel/branch.c
>             if ((long)arch->gprs[insn.i_format.rs] < 0)
>             if ((long)arch->gprs[insn.i_format.rs] < 0)
> ./arch/mips/kvm/emulate.c
>             if ((long)regs->regs[insn.i_format.rs] < 0)
> ./arch/mips/math-emu/cp1emu.c
>         if ((int32_t)(long)prom_vec < 0) {
> ./arch/mips/sibyte/common/cfe.c
>     if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
>     if (msqid < 0 || (long) bufsz < 0)
> ./ipc/msg.c
>     if ((long)x < 0)
> ./mm/page-writeback.c
>     if ((long)(next - val) < 0) {
> ./mm/memcontrol.c



WARNING: multiple messages have this Message-ID (diff)
From: Walter Wu <walter-zh.wu@mediatek.com>
To: Dmitry Vyukov <dvyukov@google.com>
Cc: Marc Gonzalez <marc.w.gonzalez@free.fr>,
	LKML <linux-kernel@vger.kernel.org>,
	kasan-dev <kasan-dev@googlegroups.com>,
	Alexander Potapenko <glider@google.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Linux ARM <linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH] kasan: fix the missing underflow in memmove and memcpy with CONFIG_KASAN_GENERIC=y
Date: Tue, 1 Oct 2019 11:18:36 +0800	[thread overview]
Message-ID: <1569899916.17361.36.camel@mtksdccf07> (raw)
In-Reply-To: <CACT4Y+b3NPemYwJJsD_oC0vde5Ybz1qDNWb=cFu2HpOTMrGSnQ@mail.gmail.com>

On Tue, 2019-10-01 at 05:01 +0200, Dmitry Vyukov wrote:
> On Tue, Oct 1, 2019 at 4:36 AM Walter Wu <walter-zh.wu@mediatek.com> wrote:
> >
> > On Mon, 2019-09-30 at 10:57 +0200, Marc Gonzalez wrote:
> > > On 30/09/2019 06:36, Walter Wu wrote:
> > >
> > > >  bool check_memory_region(unsigned long addr, size_t size, bool write,
> > > >                                 unsigned long ret_ip)
> > > >  {
> > > > +       if (long(size) < 0) {
> > > > +               kasan_report_invalid_size(src, dest, len, _RET_IP_);
> > > > +               return false;
> > > > +       }
> > > > +
> > > >         return check_memory_region_inline(addr, size, write, ret_ip);
> > > >  }
> > >
> > > Is it expected that memcpy/memmove may sometimes (incorrectly) be passed
> > > a negative value? (It would indeed turn up as a "large" size_t)
> > >
> > > IMO, casting to long is suspicious.
> > >
> > > There seem to be some two implicit assumptions.
> > >
> > > 1) size >= ULONG_MAX/2 is invalid input
> > > 2) casting a size >= ULONG_MAX/2 to long yields a negative value
> > >
> > > 1) seems reasonable because we can't copy more than half of memory to
> > > the other half of memory. I suppose the constraint could be even tighter,
> > > but it's not clear where to draw the line, especially when considering
> > > 32b vs 64b arches.
> > >
> > > 2) is implementation-defined, and gcc works "as expected" (clang too
> > > probably) https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
> > >
> > > A comment might be warranted to explain the rationale.
> > > Regards.
> >
> > Thanks for your suggestion.
> > Yes, It is passed a negative value issue in memcpy/memmove/memset.
> > Our current idea should be assumption 1 and only consider 64b arch,
> > because KASAN only supports 64b. In fact, we really can't use so much
> > memory in 64b arch. so assumption 1 make sense.
> 
> Note there are arm KASAN patches floating around, so we should not
> make assumptions about 64-bit arch.
I think arm KASAN patch doesn't merge in mainline, because virtual
memory of shadow memory is so bigger, the kernel virtual memory only has
1GB or 2GB in 32-bit arch, it is hard to solve the issue. it may need
some trade-off.

> 
> But there seems to be a number of such casts already:
> 
It seems that everyone is the same assumption.

> $ find -name "*.c" -exec egrep "\(long\).* < 0" {} \; -print
>     } else if ((long) delta < 0) {
> ./kernel/time/timer.c
>     if ((long)state < 0)
> ./drivers/thermal/thermal_sysfs.c
>     if ((long)delay < 0)
> ./drivers/infiniband/core/addr.c
>     if ((long)tmo < 0)
> ./drivers/net/wireless/st/cw1200/pm.c
>     if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
> ./sound/core/info.c
>         if ((long)hwrpb->sys_type < 0) {
> ./arch/alpha/kernel/setup.c
>     if ((long)m->driver_data < 0)
> ./arch/x86/kernel/apic/apic.c
>             if ((long) size < 0L)
>     if ((long)addr < 0L) {
> ./arch/sparc/mm/init_64.c
>     if ((long)lpid < 0)
> ./arch/powerpc/kvm/book3s_hv.c
>             if ((long)regs->regs[insn.mm_i_format.rs] < 0)
>             if ((long)regs->regs[insn.i_format.rs] < 0) {
>             if ((long)regs->regs[insn.i_format.rs] < 0) {
> ./arch/mips/kernel/branch.c
>             if ((long)arch->gprs[insn.i_format.rs] < 0)
>             if ((long)arch->gprs[insn.i_format.rs] < 0)
> ./arch/mips/kvm/emulate.c
>             if ((long)regs->regs[insn.i_format.rs] < 0)
> ./arch/mips/math-emu/cp1emu.c
>         if ((int32_t)(long)prom_vec < 0) {
> ./arch/mips/sibyte/common/cfe.c
>     if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
>     if (msqid < 0 || (long) bufsz < 0)
> ./ipc/msg.c
>     if ((long)x < 0)
> ./mm/page-writeback.c
>     if ((long)(next - val) < 0) {
> ./mm/memcontrol.c



_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2019-10-01  3:18 UTC|newest]

Thread overview: 131+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-27  3:43 [PATCH] kasan: fix the missing underflow in memmove and memcpy with CONFIG_KASAN_GENERIC=y Walter Wu
2019-09-27  3:43 ` Walter Wu
2019-09-27  3:43 ` Walter Wu
2019-09-27 13:07 ` Dmitry Vyukov
2019-09-27 13:07   ` Dmitry Vyukov
2019-09-27 13:07   ` Dmitry Vyukov
2019-09-27 14:22   ` Walter Wu
2019-09-27 14:22     ` Walter Wu
2019-09-27 14:22     ` Walter Wu
2019-09-27 19:41     ` Dmitry Vyukov
2019-09-27 19:41       ` Dmitry Vyukov
2019-09-27 19:41       ` Dmitry Vyukov
2019-09-30  4:36       ` Walter Wu
2019-09-30  4:36         ` Walter Wu
2019-09-30  8:57         ` Marc Gonzalez
2019-09-30  8:57           ` Marc Gonzalez
2019-10-01  2:36           ` Walter Wu
2019-10-01  2:36             ` Walter Wu
2019-10-01  3:01             ` Dmitry Vyukov
2019-10-01  3:01               ` Dmitry Vyukov
2019-10-01  3:18               ` Walter Wu [this message]
2019-10-01  3:18                 ` Walter Wu
2019-10-02 12:15         ` Walter Wu
2019-10-02 12:15           ` Walter Wu
2019-10-02 12:15           ` Walter Wu
2019-10-02 13:57           ` Dmitry Vyukov
2019-10-02 13:57             ` Dmitry Vyukov
2019-10-02 13:57             ` Dmitry Vyukov
2019-10-03  2:17             ` Walter Wu
2019-10-03  2:17               ` Walter Wu
2019-10-03  6:26               ` Dmitry Vyukov
2019-10-03  6:26                 ` Dmitry Vyukov
2019-10-03  6:26                 ` Dmitry Vyukov
2019-10-03  9:38                 ` Walter Wu
2019-10-03  9:38                   ` Walter Wu
2019-10-03  9:38                   ` Walter Wu
2019-10-03 13:51                   ` Walter Wu
2019-10-03 13:51                     ` Walter Wu
2019-10-03 13:51                     ` Walter Wu
2019-10-03 14:53                     ` Dmitry Vyukov
2019-10-03 14:53                       ` Dmitry Vyukov
2019-10-03 14:53                       ` Dmitry Vyukov
2019-10-04  4:42                       ` Walter Wu
2019-10-04  4:42                         ` Walter Wu
2019-10-04  4:42                         ` Walter Wu
2019-10-04  8:02                         ` Walter Wu
2019-10-04  8:02                           ` Walter Wu
2019-10-04  8:02                           ` Walter Wu
2019-10-04  9:18                           ` Dmitry Vyukov
2019-10-04  9:18                             ` Dmitry Vyukov
2019-10-04  9:18                             ` Dmitry Vyukov
2019-10-04  9:44                             ` Walter Wu
2019-10-04  9:44                               ` Walter Wu
2019-10-04  9:44                               ` Walter Wu
2019-10-04  9:54                               ` Dmitry Vyukov
2019-10-04  9:54                                 ` Dmitry Vyukov
2019-10-04  9:54                                 ` Dmitry Vyukov
2019-10-04  9:54                                 ` Dmitry Vyukov
2019-10-04 12:05                                 ` Walter Wu
2019-10-04 12:05                                   ` Walter Wu
2019-10-04 12:05                                   ` Walter Wu
2019-10-04 13:52                                   ` Dmitry Vyukov
2019-10-04 13:52                                     ` Dmitry Vyukov
2019-10-04 13:52                                     ` Dmitry Vyukov
2019-10-07  3:22                                     ` Walter Wu
2019-10-07  3:22                                       ` Walter Wu
2019-10-07  3:22                                       ` Walter Wu
2019-10-07  7:29                                       ` Dmitry Vyukov
2019-10-07  7:29                                         ` Dmitry Vyukov
2019-10-07  7:29                                         ` Dmitry Vyukov
2019-10-07  8:18                                         ` Walter Wu
2019-10-07  8:18                                           ` Walter Wu
2019-10-07  8:18                                           ` Walter Wu
2019-10-07  8:24                                           ` Dmitry Vyukov
2019-10-07  8:24                                             ` Dmitry Vyukov
2019-10-07  8:24                                             ` Dmitry Vyukov
2019-10-07  8:24                                             ` Dmitry Vyukov
2019-10-07  8:51                                             ` Walter Wu
2019-10-07  8:51                                               ` Walter Wu
2019-10-07  8:51                                               ` Walter Wu
2019-10-07  8:54                                               ` Dmitry Vyukov
2019-10-07  8:54                                                 ` Dmitry Vyukov
2019-10-07  8:54                                                 ` Dmitry Vyukov
2019-10-07  9:03                                                 ` Walter Wu
2019-10-07  9:03                                                   ` Walter Wu
2019-10-07  9:03                                                   ` Walter Wu
2019-10-07  9:10                                                   ` Dmitry Vyukov
2019-10-07  9:10                                                     ` Dmitry Vyukov
2019-10-07  9:10                                                     ` Dmitry Vyukov
2019-10-07  9:10                                                     ` Dmitry Vyukov
2019-10-07  9:28                                                     ` Walter Wu
2019-10-07  9:28                                                       ` Walter Wu
2019-10-07  9:28                                                       ` Walter Wu
2019-10-07  9:50                                                       ` Walter Wu
2019-10-07  9:50                                                         ` Walter Wu
2019-10-07  9:50                                                         ` Walter Wu
2019-10-07 10:51                                                         ` Dmitry Vyukov
2019-10-07 10:51                                                           ` Dmitry Vyukov
2019-10-07 10:51                                                           ` Dmitry Vyukov
2019-10-07 12:03                                                           ` Walter Wu
2019-10-07 12:03                                                             ` Walter Wu
2019-10-07 12:03                                                             ` Walter Wu
2019-10-07 12:19                                                             ` Dmitry Vyukov
2019-10-07 12:19                                                               ` Dmitry Vyukov
2019-10-07 12:19                                                               ` Dmitry Vyukov
2019-10-07 12:32                                                               ` Walter Wu
2019-10-07 12:32                                                                 ` Walter Wu
2019-10-07 12:32                                                                 ` Walter Wu
2019-10-07 13:33                                                                 ` Dmitry Vyukov
2019-10-07 13:33                                                                   ` Dmitry Vyukov
2019-10-07 13:33                                                                   ` Dmitry Vyukov
2019-10-08  6:15                                                                   ` Walter Wu
2019-10-08  6:15                                                                     ` Walter Wu
2019-10-08  6:15                                                                     ` Walter Wu
2019-10-08  9:47                                                                     ` Qian Cai
2019-10-08  9:47                                                                       ` Qian Cai
2019-10-08 11:02                                                                       ` Walter Wu
2019-10-08 11:02                                                                         ` Walter Wu
2019-10-08 11:02                                                                         ` Walter Wu
2019-10-08 11:42                                                                         ` Qian Cai
2019-10-08 11:42                                                                           ` Qian Cai
2019-10-08 12:07                                                                           ` Walter Wu
2019-10-08 12:07                                                                             ` Walter Wu
2019-10-08 12:07                                                                             ` Walter Wu
2019-10-08 12:11                                                                           ` Dmitry Vyukov
2019-10-08 12:11                                                                             ` Dmitry Vyukov
2019-10-08 12:11                                                                             ` Dmitry Vyukov
2019-10-08 12:11                                                                             ` Dmitry Vyukov
2019-10-14  2:19                                                                             ` Walter Wu
2019-10-14  2:19                                                                               ` Walter Wu
2019-10-14  2:19                                                                               ` Walter Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1569899916.17361.36.camel@mtksdccf07 \
    --to=walter-zh.wu@mediatek.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=dvyukov@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marc.w.gonzalez@free.fr \
    --cc=matthias.bgg@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.