LKML Archive mirror
 help / color / mirror / Atom feed
From: Peter Collingbourne <pcc@google.com>
To: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Marco Elver <elver@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Alexander Potapenko <glider@google.com>,
	Evgenii Stepanov <eugenis@google.com>,
	Linux Memory Management List <linux-mm@kvack.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kasan: fix kasan_byte_accessible() to be consistent with actual checks
Date: Mon, 5 Apr 2021 15:07:25 -0700	[thread overview]
Message-ID: <CAMn1gO65_-gxd4ocQ2DF94wZB_X0XTuy59-Bi7R0XCqG2yf6KA@mail.gmail.com> (raw)
In-Reply-To: <CA+fCnZeC20t5Lhswo7zcULNgfksxqb2_fP4j0pNy29E_q6hJyg@mail.gmail.com>

On Mon, Apr 5, 2021 at 2:53 PM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
> On Mon, Apr 5, 2021 at 11:43 PM Peter Collingbourne <pcc@google.com> wrote:
> >
> > We can sometimes end up with kasan_byte_accessible() being called
> > on non-slab memory. For example ksize() and krealloc() may end up
> > calling it on KFENCE allocated memory. In this case the memory will
> > be tagged with KASAN_SHADOW_INIT, which a subsequent patch ("kasan:
> > initialize shadow to TAG_INVALID for SW_TAGS") will set to the same
> > value as KASAN_TAG_INVALID, causing kasan_byte_accessible() to fail
> > when called on non-slab memory.
> >
> > This highlighted the fact that the check in kasan_byte_accessible()
> > was inconsistent with checks as implemented for loads and stores
> > (kasan_check_range() in SW tags mode and hardware-implemented
> > checks in HW tags mode). kasan_check_range() does not have a
> > check for KASAN_TAG_INVALID, and instead has a comparison against
> > KASAN_SHADOW_START. In HW tags mode, we do not have either, but we
> > do set TCR_EL1.TCMA which corresponds with the comparison against
> > KASAN_TAG_KERNEL.
> >
> > Therefore, update kasan_byte_accessible() for both SW and HW tags
> > modes to correspond with the respective checks on loads and stores.
> >
> > Link: https://linux-review.googlesource.com/id/Ic6d40803c57dcc6331bd97fbb9a60b0d38a65a36
> > Signed-off-by: Peter Collingbourne <pcc@google.com>
> > ---
> >  mm/kasan/kasan.h   | 3 +--
> >  mm/kasan/sw_tags.c | 8 +++++---
> >  2 files changed, 6 insertions(+), 5 deletions(-)
> >
> > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> > index 8c55634d6edd..e18e8da35255 100644
> > --- a/mm/kasan/kasan.h
> > +++ b/mm/kasan/kasan.h
> > @@ -368,8 +368,7 @@ static inline bool kasan_byte_accessible(const void *addr)
> >         u8 ptr_tag = get_tag(addr);
> >         u8 mem_tag = hw_get_mem_tag((void *)addr);
> >
> > -       return (mem_tag != KASAN_TAG_INVALID) &&
> > -               (ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag);
> > +       return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag;
> >  }
> >
> >  #else /* CONFIG_KASAN_HW_TAGS */
> > diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c
> > index 94c2d33be333..914225eeda99 100644
> > --- a/mm/kasan/sw_tags.c
> > +++ b/mm/kasan/sw_tags.c
> > @@ -121,10 +121,12 @@ bool kasan_check_range(unsigned long addr, size_t size, bool write,
> >  bool kasan_byte_accessible(const void *addr)
> >  {
> >         u8 tag = get_tag(addr);
> > -       u8 shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(kasan_reset_tag(addr)));
> > +       void *untagged_addr = kasan_reset_tag(addr);
> > +       u8 shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr));
>
> Hi Peter,
>
> Let's move dereferencing shadow memory past the KASAN_SHADOW_START
> check. Otherwise, in case the check is to fail, accessing shadow will
> likely crash the kernel.
>
> Thanks!

Makes sense, fixed in v2.

Peter

      reply	other threads:[~2021-04-05 22:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-05 21:43 [PATCH] kasan: fix kasan_byte_accessible() to be consistent with actual checks Peter Collingbourne
2021-04-05 21:52 ` Andrey Konovalov
2021-04-05 22:07   ` Peter Collingbourne [this message]

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=CAMn1gO65_-gxd4ocQ2DF94wZB_X0XTuy59-Bi7R0XCqG2yf6KA@mail.gmail.com \
    --to=pcc@google.com \
    --cc=andreyknvl@gmail.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=eugenis@google.com \
    --cc=glider@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.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 read-only IMAP folder(s) and NNTP newsgroup(s).