diff mbox series

[v2] kasan: fix kasan_byte_accessible() to be consistent with actual checks

Message ID 20210405220647.1965262-1-pcc@google.com (mailing list archive)
State New, archived
Headers show
Series [v2] kasan: fix kasan_byte_accessible() to be consistent with actual checks | expand

Commit Message

Peter Collingbourne April 5, 2021, 10:06 p.m. UTC
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 | 10 +++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)

Comments

Andrey Konovalov April 5, 2021, 10:43 p.m. UTC | #1
On Tue, Apr 6, 2021 at 12:07 AM 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 | 10 +++++++---
>  2 files changed, 8 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..00ae8913fc74 100644
> --- a/mm/kasan/sw_tags.c
> +++ b/mm/kasan/sw_tags.c
> @@ -121,10 +121,14 @@ 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;
>
> -       return (shadow_byte != KASAN_TAG_INVALID) &&
> -               (tag == KASAN_TAG_KERNEL || tag == shadow_byte);
> +       if (untagged_addr < kasan_shadow_to_mem((void *)KASAN_SHADOW_START))
> +               return false;
> +
> +       shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr));
> +       return tag == KASAN_TAG_KERNEL || tag == shadow_byte;
>  }
>
>  #define DEFINE_HWASAN_LOAD_STORE(size)                                 \
> --
> 2.31.0.208.g409f899ff0-goog
>

Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>

Thank you, Peter!
Marco Elver April 5, 2021, 11:46 p.m. UTC | #2
On Tue, 6 Apr 2021 at 00:43, Andrey Konovalov <andreyknvl@gmail.com> wrote:
> On Tue, Apr 6, 2021 at 12:07 AM 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 | 10 +++++++---
> >  2 files changed, 8 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..00ae8913fc74 100644
> > --- a/mm/kasan/sw_tags.c
> > +++ b/mm/kasan/sw_tags.c
> > @@ -121,10 +121,14 @@ 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;
> >
> > -       return (shadow_byte != KASAN_TAG_INVALID) &&
> > -               (tag == KASAN_TAG_KERNEL || tag == shadow_byte);
> > +       if (untagged_addr < kasan_shadow_to_mem((void *)KASAN_SHADOW_START))
> > +               return false;
> > +
> > +       shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr));
> > +       return tag == KASAN_TAG_KERNEL || tag == shadow_byte;
> >  }
> >
> >  #define DEFINE_HWASAN_LOAD_STORE(size)                                 \
> > --
> > 2.31.0.208.g409f899ff0-goog
> >
>
> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
>
> Thank you, Peter!

Reviewed-by: Marco Elver <elver@google.com>

Thanks!
diff mbox series

Patch

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..00ae8913fc74 100644
--- a/mm/kasan/sw_tags.c
+++ b/mm/kasan/sw_tags.c
@@ -121,10 +121,14 @@  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;
 
-	return (shadow_byte != KASAN_TAG_INVALID) &&
-		(tag == KASAN_TAG_KERNEL || tag == shadow_byte);
+	if (untagged_addr < kasan_shadow_to_mem((void *)KASAN_SHADOW_START))
+		return false;
+
+	shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr));
+	return tag == KASAN_TAG_KERNEL || tag == shadow_byte;
 }
 
 #define DEFINE_HWASAN_LOAD_STORE(size)					\