linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Vyukov <dvyukov@google.com>
To: Dennis Zhou <dennisszhou@gmail.com>
Cc: Fengguang Wu <fengguang.wu@intel.com>,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	Kees Cook <keescook@chromium.org>, Linux-MM <linux-mm@kvack.org>,
	Tejun Heo <tj@kernel.org>, Christoph Lameter <cl@linux.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Josef Bacik <jbacik@fb.com>, LKML <linux-kernel@vger.kernel.org>,
	LKP <lkp@01.org>, Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Mark Rutland <mark.rutland@arm.com>
Subject: Re: [pcpu] BUG: KASAN: use-after-scope in pcpu_setup_first_chunk+0x1e3b/0x29e2
Date: Thu, 30 Nov 2017 10:59:05 +0100	[thread overview]
Message-ID: <CACT4Y+bji1JMJVJZdv=+bD8JZ1kqrmJ0PWXvHdYzRFcnAKDSGw@mail.gmail.com> (raw)
In-Reply-To: <20171129175430.GA58181@big-sky.attlocal.net>

On Wed, Nov 29, 2017 at 6:54 PM, Dennis Zhou <dennisszhou@gmail.com> wrote:
> Hi everyone,
>
> I spent a bit of time learning more about this problem as Fengguang was
> able to determine the root commit f7dd2507893cc3. I reproduced the bug
> in userspace to make life a bit easier and below the assignment occurs
> before the unpoison. This is fine if we're sequentially proceeding, but
> as in the case in percpu, it's calling the function in a for loop
> causing the assignment to happen after it has been poisoned in the prior
> iteration.
>
> <bb 3> [0.00%]:
>   _1 = (long unsigned int) i_4;
>   _2 = _1 * 16;
>   _3 = p_8 + _2;
>   list_14 = _3;
>   __u = {};
>   ASAN_MARK (UNPOISON, &__u, 8);
>   __u.__val = list_14;
>
> <bb 9> [0.00%]:
>   _24 = __u.__val;
>   ASAN_MARK (POISON, &__u, 8);
>   list_14->prev = list_14;
>   i_13 = i_4 + 1;
>
> <bb 10> [0.00%]:
>   # i_4 = PHI <i_9(2), i_13(9)>
>   if (i_4 <= 9)
>     goto <bb 3>; [0.00%]
>   else
>     goto <bb 11>; [0.00%]
>
> I don't know how to go about fixing this though. The reproducing code is
> below and was compiled with gcc-7 and the structleak_plugin.


Are we sure that structleak plugin is not at fault? If yes, then we
need to report this to https://gcc.gnu.org/bugzilla/ with instructions
on how to build/use the plugin.


> I hope this helps.
>
> Thanks,
> Dennis
>
> ----
> #include <stdint.h>
> #include <stdlib.h>
>
> #define barrier()
>
> #define WRITE_ONCE(x, val) \
> ({                                                      \
>         union { typeof(x) __val; char __c[1]; } __u =   \
>                 { .__val = (typeof(x)) (val) }; \
>         __write_once_size(&(x), __u.__c, sizeof(x));    \
>         __u.__val;                                      \
> })
>
> typedef         uint8_t         __u8;
> typedef         uint16_t        __u16;
> typedef         uint32_t        __u32;
> typedef         uint64_t        __u64;
>
> static inline __attribute__((always_inline)) void __write_once_size(volatile void *p, void *res, int size)
> {
>         switch (size) {
>         case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
>         case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
>         case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
>         case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
>         default:
>                 barrier();
>                 __builtin_memcpy((void *)p, (const void *)res, size);
>                 barrier();
>         }
> }
>
> struct list_head {
>         struct list_head *next, *prev;
> };
>
> static inline __attribute__((always_inline)) void INIT_LIST_HEAD(struct list_head *list)
> {
>         WRITE_ONCE(list->next, list);
>         list->prev = list;
> }
>
> int main(int argc, char *argv[])
> {
>         struct list_head *p = malloc(10 * sizeof(struct list_head));
>         int i;
>
>         for (i = 0; i < 10; i++) {
>                 INIT_LIST_HEAD(&p[i]);
>         }
>
>         free(p);
>
>         return 0;
> }

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  reply	other threads:[~2017-11-30  9:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-26  6:31 [pcpu] BUG: KASAN: use-after-scope in pcpu_setup_first_chunk+0x1e3b/0x29e2 Fengguang Wu
2017-11-27 21:03 ` Dennis Zhou
2017-11-28 12:45   ` Fengguang Wu
2017-11-29 17:54     ` Dennis Zhou
2017-11-30  9:59       ` Dmitry Vyukov [this message]
2017-11-30 18:10         ` Kees Cook
2017-11-30 19:22           ` Dennis Zhou
2017-11-30 19:56             ` Kees Cook
2017-11-30 19:58               ` Ard Biesheuvel

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='CACT4Y+bji1JMJVJZdv=+bD8JZ1kqrmJ0PWXvHdYzRFcnAKDSGw@mail.gmail.com' \
    --to=dvyukov@google.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=aryabinin@virtuozzo.com \
    --cc=cl@linux.com \
    --cc=dennisszhou@gmail.com \
    --cc=fengguang.wu@intel.com \
    --cc=jbacik@fb.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lkp@01.org \
    --cc=mark.rutland@arm.com \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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 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).