linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] kasan: fixes for 5.11-rc
@ 2021-01-13 16:03 Andrey Konovalov
  2021-01-13 16:03 ` [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free Andrey Konovalov
  2021-01-13 16:03 ` [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports Andrey Konovalov
  0 siblings, 2 replies; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-13 16:03 UTC (permalink / raw)
  To: Catalin Marinas, Vincenzo Frascino, Dmitry Vyukov,
	Alexander Potapenko, Marco Elver
  Cc: Andrew Morton, Will Deacon, Andrey Ryabinin, Peter Collingbourne,
	Evgenii Stepanov, Branislav Rankov, Kevin Brodsky, kasan-dev,
	linux-arm-kernel, linux-mm, linux-kernel, Andrey Konovalov

Andrey Konovalov (2):
  kasan, mm: fix conflicts with init_on_alloc/free
  kasan, arm64: fix pointer tags in KASAN reports

 arch/arm64/mm/fault.c | 2 ++
 mm/slub.c             | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

-- 
2.30.0.284.gd98b1dd5eaa7-goog


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

* [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free
  2021-01-13 16:03 [PATCH 0/2] kasan: fixes for 5.11-rc Andrey Konovalov
@ 2021-01-13 16:03 ` Andrey Konovalov
  2021-01-13 17:24   ` Vlastimil Babka
  2021-01-13 16:03 ` [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports Andrey Konovalov
  1 sibling, 1 reply; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-13 16:03 UTC (permalink / raw)
  To: Catalin Marinas, Vincenzo Frascino, Dmitry Vyukov,
	Alexander Potapenko, Marco Elver
  Cc: Andrew Morton, Will Deacon, Andrey Ryabinin, Peter Collingbourne,
	Evgenii Stepanov, Branislav Rankov, Kevin Brodsky, kasan-dev,
	linux-arm-kernel, linux-mm, linux-kernel, Andrey Konovalov

A few places where SLUB accesses object's data or metadata were missed in
a previous patch. This leads to false positives with hardware tag-based
KASAN when bulk allocations are used with init_on_alloc/free.

Fix the false-positives by resetting pointer tags during these accesses.

Link: https://linux-review.googlesource.com/id/I50dd32838a666e173fe06c3c5c766f2c36aae901
Fixes: aa1ef4d7b3f67 ("kasan, mm: reset tags when accessing metadata")
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 mm/slub.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index dc5b42e700b8..75fb097d990d 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2791,7 +2791,8 @@ static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
 						   void *obj)
 {
 	if (unlikely(slab_want_init_on_free(s)) && obj)
-		memset((void *)((char *)obj + s->offset), 0, sizeof(void *));
+		memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
+			0, sizeof(void *));
 }
 
 /*
@@ -2883,7 +2884,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
 		stat(s, ALLOC_FASTPATH);
 	}
 
-	maybe_wipe_obj_freeptr(s, kasan_reset_tag(object));
+	maybe_wipe_obj_freeptr(s, object);
 
 	if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object)
 		memset(kasan_reset_tag(object), 0, s->object_size);
@@ -3329,7 +3330,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 		int j;
 
 		for (j = 0; j < i; j++)
-			memset(p[j], 0, s->object_size);
+			memset(kasan_reset_tag(p[j]), 0, s->object_size);
 	}
 
 	/* memcg and kmem_cache debug support */
-- 
2.30.0.284.gd98b1dd5eaa7-goog


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

* [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-13 16:03 [PATCH 0/2] kasan: fixes for 5.11-rc Andrey Konovalov
  2021-01-13 16:03 ` [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free Andrey Konovalov
@ 2021-01-13 16:03 ` Andrey Konovalov
  2021-01-13 16:54   ` Catalin Marinas
  1 sibling, 1 reply; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-13 16:03 UTC (permalink / raw)
  To: Catalin Marinas, Vincenzo Frascino, Dmitry Vyukov,
	Alexander Potapenko, Marco Elver
  Cc: Andrew Morton, Will Deacon, Andrey Ryabinin, Peter Collingbourne,
	Evgenii Stepanov, Branislav Rankov, Kevin Brodsky, kasan-dev,
	linux-arm-kernel, linux-mm, linux-kernel, Andrey Konovalov

As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
that is passed to report_tag_fault has pointer tags in the format of 0x0X,
while KASAN uses 0xFX format (note the difference in the top 4 bits).

Fix up the pointer tag before calling kasan_report.

Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 arch/arm64/mm/fault.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index 3c40da479899..a218f6f2fdc8 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
 {
 	bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
 
+	/* The format of KASAN tags is 0xF<x>. */
+	addr |= (0xF0UL << MTE_TAG_SHIFT);
 	/*
 	 * SAS bits aren't set for all faults reported in EL1, so we can't
 	 * find out access size.
-- 
2.30.0.284.gd98b1dd5eaa7-goog


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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-13 16:03 ` [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports Andrey Konovalov
@ 2021-01-13 16:54   ` Catalin Marinas
  2021-01-15 13:12     ` Andrey Konovalov
  2021-01-15 16:30     ` Andrey Konovalov
  0 siblings, 2 replies; 14+ messages in thread
From: Catalin Marinas @ 2021-01-13 16:54 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, linux-arm-kernel, linux-mm,
	linux-kernel

On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> while KASAN uses 0xFX format (note the difference in the top 4 bits).
> 
> Fix up the pointer tag before calling kasan_report.
> 
> Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> ---
>  arch/arm64/mm/fault.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> index 3c40da479899..a218f6f2fdc8 100644
> --- a/arch/arm64/mm/fault.c
> +++ b/arch/arm64/mm/fault.c
> @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
>  {
>  	bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
>  
> +	/* The format of KASAN tags is 0xF<x>. */
> +	addr |= (0xF0UL << MTE_TAG_SHIFT);

Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
was added, the only tag faults were generated for user addresses.

Anyway, I'd rather fix it in there based on bit 55, something like (only
compile-tested):

diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index 3c40da479899..2b71079d2d32 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
 			      struct pt_regs *regs)
 {
 	/*
-	 * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
-	 * check faults. Mask them out now so that userspace doesn't see them.
+	 * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
+	 * for tag check faults. Set them to the corresponding bits in the
+	 * untagged address.
 	 */
-	far &= (1UL << 60) - 1;
+	far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
 	do_bad_area(far, esr, regs);
 	return 0;
 }

-- 
Catalin

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

* Re: [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free
  2021-01-13 16:03 ` [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free Andrey Konovalov
@ 2021-01-13 17:24   ` Vlastimil Babka
  2021-01-14 15:31     ` Andrey Konovalov
  0 siblings, 1 reply; 14+ messages in thread
From: Vlastimil Babka @ 2021-01-13 17:24 UTC (permalink / raw)
  To: Andrey Konovalov, Catalin Marinas, Vincenzo Frascino,
	Dmitry Vyukov, Alexander Potapenko, Marco Elver
  Cc: Andrew Morton, Will Deacon, Andrey Ryabinin, Peter Collingbourne,
	Evgenii Stepanov, Branislav Rankov, Kevin Brodsky, kasan-dev,
	linux-arm-kernel, linux-mm, linux-kernel

On 1/13/21 5:03 PM, Andrey Konovalov wrote:
> A few places where SLUB accesses object's data or metadata were missed in
> a previous patch. This leads to false positives with hardware tag-based
> KASAN when bulk allocations are used with init_on_alloc/free.
> 
> Fix the false-positives by resetting pointer tags during these accesses.
> 
> Link: https://linux-review.googlesource.com/id/I50dd32838a666e173fe06c3c5c766f2c36aae901
> Fixes: aa1ef4d7b3f67 ("kasan, mm: reset tags when accessing metadata")
> Reported-by: Dmitry Vyukov <dvyukov@google.com>
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/slub.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index dc5b42e700b8..75fb097d990d 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2791,7 +2791,8 @@ static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
>  						   void *obj)
>  {
>  	if (unlikely(slab_want_init_on_free(s)) && obj)
> -		memset((void *)((char *)obj + s->offset), 0, sizeof(void *));
> +		memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
> +			0, sizeof(void *));
>  }
>  
>  /*
> @@ -2883,7 +2884,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
>  		stat(s, ALLOC_FASTPATH);
>  	}
>  
> -	maybe_wipe_obj_freeptr(s, kasan_reset_tag(object));
> +	maybe_wipe_obj_freeptr(s, object);

And in that case the reset was unnecessary, right. (commit log only mentions
adding missing resets).

>  	if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object)
>  		memset(kasan_reset_tag(object), 0, s->object_size);
> @@ -3329,7 +3330,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
>  		int j;
>  
>  		for (j = 0; j < i; j++)
> -			memset(p[j], 0, s->object_size);
> +			memset(kasan_reset_tag(p[j]), 0, s->object_size);
>  	}
>  
>  	/* memcg and kmem_cache debug support */
> 


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

* Re: [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free
  2021-01-13 17:24   ` Vlastimil Babka
@ 2021-01-14 15:31     ` Andrey Konovalov
  0 siblings, 0 replies; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-14 15:31 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Catalin Marinas, Vincenzo Frascino, Dmitry Vyukov,
	Alexander Potapenko, Marco Elver, Andrew Morton, Will Deacon,
	Andrey Ryabinin, Peter Collingbourne, Evgenii Stepanov,
	Branislav Rankov, Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Wed, Jan 13, 2021 at 6:25 PM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 1/13/21 5:03 PM, Andrey Konovalov wrote:
> > A few places where SLUB accesses object's data or metadata were missed in
> > a previous patch. This leads to false positives with hardware tag-based
> > KASAN when bulk allocations are used with init_on_alloc/free.
> >
> > Fix the false-positives by resetting pointer tags during these accesses.
> >
> > Link: https://linux-review.googlesource.com/id/I50dd32838a666e173fe06c3c5c766f2c36aae901
> > Fixes: aa1ef4d7b3f67 ("kasan, mm: reset tags when accessing metadata")
> > Reported-by: Dmitry Vyukov <dvyukov@google.com>
> > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
>
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
>
> > ---
> >  mm/slub.c | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> >
> > diff --git a/mm/slub.c b/mm/slub.c
> > index dc5b42e700b8..75fb097d990d 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -2791,7 +2791,8 @@ static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
> >                                                  void *obj)
> >  {
> >       if (unlikely(slab_want_init_on_free(s)) && obj)
> > -             memset((void *)((char *)obj + s->offset), 0, sizeof(void *));
> > +             memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
> > +                     0, sizeof(void *));
> >  }
> >
> >  /*
> > @@ -2883,7 +2884,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
> >               stat(s, ALLOC_FASTPATH);
> >       }
> >
> > -     maybe_wipe_obj_freeptr(s, kasan_reset_tag(object));
> > +     maybe_wipe_obj_freeptr(s, object);
>
> And in that case the reset was unnecessary, right. (commit log only mentions
> adding missing resets).

The reset has been moved into maybe_wipe_obj_freeptr(). I'll mention
it in the changelog in v2.

Thanks!

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-13 16:54   ` Catalin Marinas
@ 2021-01-15 13:12     ` Andrey Konovalov
  2021-01-15 15:06       ` Catalin Marinas
  2021-01-15 16:30     ` Andrey Konovalov
  1 sibling, 1 reply; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-15 13:12 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
>
> On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> >
> > Fix up the pointer tag before calling kasan_report.
> >
> > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > ---
> >  arch/arm64/mm/fault.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > index 3c40da479899..a218f6f2fdc8 100644
> > --- a/arch/arm64/mm/fault.c
> > +++ b/arch/arm64/mm/fault.c
> > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> >  {
> >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> >
> > +     /* The format of KASAN tags is 0xF<x>. */
> > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
>
> Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> was added, the only tag faults were generated for user addresses.
>
> Anyway, I'd rather fix it in there based on bit 55, something like (only
> compile-tested):
>
> diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> index 3c40da479899..2b71079d2d32 100644
> --- a/arch/arm64/mm/fault.c
> +++ b/arch/arm64/mm/fault.c
> @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
>                               struct pt_regs *regs)
>  {
>         /*
> -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> -        * check faults. Mask them out now so that userspace doesn't see them.
> +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> +        * for tag check faults. Set them to the corresponding bits in the
> +        * untagged address.
>          */
> -       far &= (1UL << 60) - 1;
> +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
>         do_bad_area(far, esr, regs);
>         return 0;
>  }

Sounds good, will do in v3, thanks!

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-15 13:12     ` Andrey Konovalov
@ 2021-01-15 15:06       ` Catalin Marinas
  2021-01-15 16:25         ` Andrey Konovalov
  0 siblings, 1 reply; 14+ messages in thread
From: Catalin Marinas @ 2021-01-15 15:06 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Fri, Jan 15, 2021 at 02:12:24PM +0100, Andrey Konovalov wrote:
> On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> >
> > On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> > >
> > > Fix up the pointer tag before calling kasan_report.
> > >
> > > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > > ---
> > >  arch/arm64/mm/fault.c | 2 ++
> > >  1 file changed, 2 insertions(+)
> > >
> > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > index 3c40da479899..a218f6f2fdc8 100644
> > > --- a/arch/arm64/mm/fault.c
> > > +++ b/arch/arm64/mm/fault.c
> > > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> > >  {
> > >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> > >
> > > +     /* The format of KASAN tags is 0xF<x>. */
> > > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
> >
> > Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> > was added, the only tag faults were generated for user addresses.
> >
> > Anyway, I'd rather fix it in there based on bit 55, something like (only
> > compile-tested):
> >
> > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > index 3c40da479899..2b71079d2d32 100644
> > --- a/arch/arm64/mm/fault.c
> > +++ b/arch/arm64/mm/fault.c
> > @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
> >                               struct pt_regs *regs)
> >  {
> >         /*
> > -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> > -        * check faults. Mask them out now so that userspace doesn't see them.
> > +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> > +        * for tag check faults. Set them to the corresponding bits in the
> > +        * untagged address.
> >          */
> > -       far &= (1UL << 60) - 1;
> > +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
> >         do_bad_area(far, esr, regs);
> >         return 0;
> >  }
> 
> Sounds good, will do in v3, thanks!

I wonder if this one gives the same result (so please check):

	far = u64_replace_bits(untagged_addr(far), far, MTE_TAG_MASK);

(defined in linux/bitfield.h)

-- 
Catalin

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-15 15:06       ` Catalin Marinas
@ 2021-01-15 16:25         ` Andrey Konovalov
  0 siblings, 0 replies; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-15 16:25 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Fri, Jan 15, 2021 at 4:07 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
>
> On Fri, Jan 15, 2021 at 02:12:24PM +0100, Andrey Konovalov wrote:
> > On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> > >
> > > On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > > > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > > > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > > > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> > > >
> > > > Fix up the pointer tag before calling kasan_report.
> > > >
> > > > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > > > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > > > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > > > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > > > ---
> > > >  arch/arm64/mm/fault.c | 2 ++
> > > >  1 file changed, 2 insertions(+)
> > > >
> > > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > > index 3c40da479899..a218f6f2fdc8 100644
> > > > --- a/arch/arm64/mm/fault.c
> > > > +++ b/arch/arm64/mm/fault.c
> > > > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> > > >  {
> > > >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> > > >
> > > > +     /* The format of KASAN tags is 0xF<x>. */
> > > > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
> > >
> > > Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> > > was added, the only tag faults were generated for user addresses.
> > >
> > > Anyway, I'd rather fix it in there based on bit 55, something like (only
> > > compile-tested):
> > >
> > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > index 3c40da479899..2b71079d2d32 100644
> > > --- a/arch/arm64/mm/fault.c
> > > +++ b/arch/arm64/mm/fault.c
> > > @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
> > >                               struct pt_regs *regs)
> > >  {
> > >         /*
> > > -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> > > -        * check faults. Mask them out now so that userspace doesn't see them.
> > > +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> > > +        * for tag check faults. Set them to the corresponding bits in the
> > > +        * untagged address.
> > >          */
> > > -       far &= (1UL << 60) - 1;
> > > +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
> > >         do_bad_area(far, esr, regs);
> > >         return 0;
> > >  }
> >
> > Sounds good, will do in v3, thanks!
>
> I wonder if this one gives the same result (so please check):
>
>         far = u64_replace_bits(untagged_addr(far), far, MTE_TAG_MASK);
>
> (defined in linux/bitfield.h)

No, it zeroes out the tag. Not sure why. I took a brief look at the
implementation and didn't get how it's supposed to work - too much bit
trickery.

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-13 16:54   ` Catalin Marinas
  2021-01-15 13:12     ` Andrey Konovalov
@ 2021-01-15 16:30     ` Andrey Konovalov
  2021-01-15 16:55       ` Catalin Marinas
  1 sibling, 1 reply; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-15 16:30 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
>
> On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> >
> > Fix up the pointer tag before calling kasan_report.
> >
> > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > ---
> >  arch/arm64/mm/fault.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > index 3c40da479899..a218f6f2fdc8 100644
> > --- a/arch/arm64/mm/fault.c
> > +++ b/arch/arm64/mm/fault.c
> > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> >  {
> >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> >
> > +     /* The format of KASAN tags is 0xF<x>. */
> > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
>
> Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> was added, the only tag faults were generated for user addresses.
>
> Anyway, I'd rather fix it in there based on bit 55, something like (only
> compile-tested):
>
> diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> index 3c40da479899..2b71079d2d32 100644
> --- a/arch/arm64/mm/fault.c
> +++ b/arch/arm64/mm/fault.c
> @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
>                               struct pt_regs *regs)
>  {
>         /*
> -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> -        * check faults. Mask them out now so that userspace doesn't see them.
> +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> +        * for tag check faults. Set them to the corresponding bits in the
> +        * untagged address.
>          */
> -       far &= (1UL << 60) - 1;
> +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
>         do_bad_area(far, esr, regs);
>         return 0;
>  }

BTW, we can do "untagged_addr(far) | (far & MTE_TAG_MASK)" here, as
untagged_addr() doesn't change kernel pointers.

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-15 16:30     ` Andrey Konovalov
@ 2021-01-15 16:55       ` Catalin Marinas
  2021-01-15 17:00         ` Andrey Konovalov
  0 siblings, 1 reply; 14+ messages in thread
From: Catalin Marinas @ 2021-01-15 16:55 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Fri, Jan 15, 2021 at 05:30:40PM +0100, Andrey Konovalov wrote:
> On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> > On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> > >
> > > Fix up the pointer tag before calling kasan_report.
> > >
> > > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > > ---
> > >  arch/arm64/mm/fault.c | 2 ++
> > >  1 file changed, 2 insertions(+)
> > >
> > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > index 3c40da479899..a218f6f2fdc8 100644
> > > --- a/arch/arm64/mm/fault.c
> > > +++ b/arch/arm64/mm/fault.c
> > > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> > >  {
> > >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> > >
> > > +     /* The format of KASAN tags is 0xF<x>. */
> > > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
> >
> > Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> > was added, the only tag faults were generated for user addresses.
> >
> > Anyway, I'd rather fix it in there based on bit 55, something like (only
> > compile-tested):
> >
> > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > index 3c40da479899..2b71079d2d32 100644
> > --- a/arch/arm64/mm/fault.c
> > +++ b/arch/arm64/mm/fault.c
> > @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
> >                               struct pt_regs *regs)
> >  {
> >         /*
> > -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> > -        * check faults. Mask them out now so that userspace doesn't see them.
> > +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> > +        * for tag check faults. Set them to the corresponding bits in the
> > +        * untagged address.
> >          */
> > -       far &= (1UL << 60) - 1;
> > +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
> >         do_bad_area(far, esr, regs);
> >         return 0;
> >  }
> 
> BTW, we can do "untagged_addr(far) | (far & MTE_TAG_MASK)" here, as
> untagged_addr() doesn't change kernel pointers.

untagged_addr() does change tagged kernel pointers, it sign-extends from
bit 55. So the top byte becomes 0xff and you can no longer or the tag
bits in.

-- 
Catalin

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-15 16:55       ` Catalin Marinas
@ 2021-01-15 17:00         ` Andrey Konovalov
  2021-01-15 17:05           ` Catalin Marinas
  0 siblings, 1 reply; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-15 17:00 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Fri, Jan 15, 2021 at 5:56 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
>
> On Fri, Jan 15, 2021 at 05:30:40PM +0100, Andrey Konovalov wrote:
> > On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> > > On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > > > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > > > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > > > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> > > >
> > > > Fix up the pointer tag before calling kasan_report.
> > > >
> > > > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > > > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > > > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > > > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > > > ---
> > > >  arch/arm64/mm/fault.c | 2 ++
> > > >  1 file changed, 2 insertions(+)
> > > >
> > > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > > index 3c40da479899..a218f6f2fdc8 100644
> > > > --- a/arch/arm64/mm/fault.c
> > > > +++ b/arch/arm64/mm/fault.c
> > > > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> > > >  {
> > > >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> > > >
> > > > +     /* The format of KASAN tags is 0xF<x>. */
> > > > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
> > >
> > > Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> > > was added, the only tag faults were generated for user addresses.
> > >
> > > Anyway, I'd rather fix it in there based on bit 55, something like (only
> > > compile-tested):
> > >
> > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > index 3c40da479899..2b71079d2d32 100644
> > > --- a/arch/arm64/mm/fault.c
> > > +++ b/arch/arm64/mm/fault.c
> > > @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
> > >                               struct pt_regs *regs)
> > >  {
> > >         /*
> > > -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> > > -        * check faults. Mask them out now so that userspace doesn't see them.
> > > +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> > > +        * for tag check faults. Set them to the corresponding bits in the
> > > +        * untagged address.
> > >          */
> > > -       far &= (1UL << 60) - 1;
> > > +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
> > >         do_bad_area(far, esr, regs);
> > >         return 0;
> > >  }
> >
> > BTW, we can do "untagged_addr(far) | (far & MTE_TAG_MASK)" here, as
> > untagged_addr() doesn't change kernel pointers.
>
> untagged_addr() does change tagged kernel pointers, it sign-extends from
> bit 55. So the top byte becomes 0xff and you can no longer or the tag
> bits in.

That's __untagged_addr(), untagged_addr() keeps the bits for kernel
pointers as of  597399d0cb91.

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-15 17:00         ` Andrey Konovalov
@ 2021-01-15 17:05           ` Catalin Marinas
  2021-01-15 17:39             ` Andrey Konovalov
  0 siblings, 1 reply; 14+ messages in thread
From: Catalin Marinas @ 2021-01-15 17:05 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Fri, Jan 15, 2021 at 06:00:36PM +0100, Andrey Konovalov wrote:
> On Fri, Jan 15, 2021 at 5:56 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> >
> > On Fri, Jan 15, 2021 at 05:30:40PM +0100, Andrey Konovalov wrote:
> > > On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> > > > On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > > > > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > > > > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > > > > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> > > > >
> > > > > Fix up the pointer tag before calling kasan_report.
> > > > >
> > > > > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > > > > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > > > > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > > > > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > > > > ---
> > > > >  arch/arm64/mm/fault.c | 2 ++
> > > > >  1 file changed, 2 insertions(+)
> > > > >
> > > > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > > > index 3c40da479899..a218f6f2fdc8 100644
> > > > > --- a/arch/arm64/mm/fault.c
> > > > > +++ b/arch/arm64/mm/fault.c
> > > > > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> > > > >  {
> > > > >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> > > > >
> > > > > +     /* The format of KASAN tags is 0xF<x>. */
> > > > > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
> > > >
> > > > Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> > > > was added, the only tag faults were generated for user addresses.
> > > >
> > > > Anyway, I'd rather fix it in there based on bit 55, something like (only
> > > > compile-tested):
> > > >
> > > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > > index 3c40da479899..2b71079d2d32 100644
> > > > --- a/arch/arm64/mm/fault.c
> > > > +++ b/arch/arm64/mm/fault.c
> > > > @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
> > > >                               struct pt_regs *regs)
> > > >  {
> > > >         /*
> > > > -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> > > > -        * check faults. Mask them out now so that userspace doesn't see them.
> > > > +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> > > > +        * for tag check faults. Set them to the corresponding bits in the
> > > > +        * untagged address.
> > > >          */
> > > > -       far &= (1UL << 60) - 1;
> > > > +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
> > > >         do_bad_area(far, esr, regs);
> > > >         return 0;
> > > >  }
> > >
> > > BTW, we can do "untagged_addr(far) | (far & MTE_TAG_MASK)" here, as
> > > untagged_addr() doesn't change kernel pointers.
> >
> > untagged_addr() does change tagged kernel pointers, it sign-extends from
> > bit 55. So the top byte becomes 0xff and you can no longer or the tag
> > bits in.
> 
> That's __untagged_addr(), untagged_addr() keeps the bits for kernel
> pointers as of  597399d0cb91.

Ah, you are right. In this case I think we should use __untagged_addr()
above. Even if the tag check fault happened on a kernel address, bits
63:60 are still unknown.

-- 
Catalin

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

* Re: [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports
  2021-01-15 17:05           ` Catalin Marinas
@ 2021-01-15 17:39             ` Andrey Konovalov
  0 siblings, 0 replies; 14+ messages in thread
From: Andrey Konovalov @ 2021-01-15 17:39 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: Vincenzo Frascino, Dmitry Vyukov, Alexander Potapenko,
	Marco Elver, Andrew Morton, Will Deacon, Andrey Ryabinin,
	Peter Collingbourne, Evgenii Stepanov, Branislav Rankov,
	Kevin Brodsky, kasan-dev, Linux ARM,
	Linux Memory Management List, LKML

On Fri, Jan 15, 2021 at 6:06 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
>
> On Fri, Jan 15, 2021 at 06:00:36PM +0100, Andrey Konovalov wrote:
> > On Fri, Jan 15, 2021 at 5:56 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> > >
> > > On Fri, Jan 15, 2021 at 05:30:40PM +0100, Andrey Konovalov wrote:
> > > > On Wed, Jan 13, 2021 at 5:54 PM Catalin Marinas <catalin.marinas@arm.com> wrote:
> > > > > On Wed, Jan 13, 2021 at 05:03:30PM +0100, Andrey Konovalov wrote:
> > > > > > As of the "arm64: expose FAR_EL1 tag bits in siginfo" patch, the address
> > > > > > that is passed to report_tag_fault has pointer tags in the format of 0x0X,
> > > > > > while KASAN uses 0xFX format (note the difference in the top 4 bits).
> > > > > >
> > > > > > Fix up the pointer tag before calling kasan_report.
> > > > > >
> > > > > > Link: https://linux-review.googlesource.com/id/I9ced973866036d8679e8f4ae325de547eb969649
> > > > > > Fixes: dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
> > > > > > Fixes: 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
> > > > > > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> > > > > > ---
> > > > > >  arch/arm64/mm/fault.c | 2 ++
> > > > > >  1 file changed, 2 insertions(+)
> > > > > >
> > > > > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > > > > index 3c40da479899..a218f6f2fdc8 100644
> > > > > > --- a/arch/arm64/mm/fault.c
> > > > > > +++ b/arch/arm64/mm/fault.c
> > > > > > @@ -304,6 +304,8 @@ static void report_tag_fault(unsigned long addr, unsigned int esr,
> > > > > >  {
> > > > > >       bool is_write  = ((esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT) != 0;
> > > > > >
> > > > > > +     /* The format of KASAN tags is 0xF<x>. */
> > > > > > +     addr |= (0xF0UL << MTE_TAG_SHIFT);
> > > > >
> > > > > Ah, I see, that top 4 bits are zeroed by do_tag_check_fault(). When this
> > > > > was added, the only tag faults were generated for user addresses.
> > > > >
> > > > > Anyway, I'd rather fix it in there based on bit 55, something like (only
> > > > > compile-tested):
> > > > >
> > > > > diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
> > > > > index 3c40da479899..2b71079d2d32 100644
> > > > > --- a/arch/arm64/mm/fault.c
> > > > > +++ b/arch/arm64/mm/fault.c
> > > > > @@ -709,10 +709,11 @@ static int do_tag_check_fault(unsigned long far, unsigned int esr,
> > > > >                               struct pt_regs *regs)
> > > > >  {
> > > > >         /*
> > > > > -        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN for tag
> > > > > -        * check faults. Mask them out now so that userspace doesn't see them.
> > > > > +        * The architecture specifies that bits 63:60 of FAR_EL1 are UNKNOWN
> > > > > +        * for tag check faults. Set them to the corresponding bits in the
> > > > > +        * untagged address.
> > > > >          */
> > > > > -       far &= (1UL << 60) - 1;
> > > > > +       far = (untagged_addr(far) & ~MTE_TAG_MASK) | (far & MTE_TAG_MASK) ;
> > > > >         do_bad_area(far, esr, regs);
> > > > >         return 0;
> > > > >  }
> > > >
> > > > BTW, we can do "untagged_addr(far) | (far & MTE_TAG_MASK)" here, as
> > > > untagged_addr() doesn't change kernel pointers.
> > >
> > > untagged_addr() does change tagged kernel pointers, it sign-extends from
> > > bit 55. So the top byte becomes 0xff and you can no longer or the tag
> > > bits in.
> >
> > That's __untagged_addr(), untagged_addr() keeps the bits for kernel
> > pointers as of  597399d0cb91.
>
> Ah, you are right. In this case I think we should use __untagged_addr()
> above. Even if the tag check fault happened on a kernel address, bits
> 63:60 are still unknown.

Yeah, I keep forgetting about [__]untagged_addr() too. Maybe we need
better names? Like untagged_addr() and untagged_addr_ttbr0()?

Anyway, I'll do the explicit calculation with __untagged_addr() in the
next version.

Thanks!

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

end of thread, other threads:[~2021-01-15 17:41 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-13 16:03 [PATCH 0/2] kasan: fixes for 5.11-rc Andrey Konovalov
2021-01-13 16:03 ` [PATCH 1/2] kasan, mm: fix conflicts with init_on_alloc/free Andrey Konovalov
2021-01-13 17:24   ` Vlastimil Babka
2021-01-14 15:31     ` Andrey Konovalov
2021-01-13 16:03 ` [PATCH 2/2] kasan, arm64: fix pointer tags in KASAN reports Andrey Konovalov
2021-01-13 16:54   ` Catalin Marinas
2021-01-15 13:12     ` Andrey Konovalov
2021-01-15 15:06       ` Catalin Marinas
2021-01-15 16:25         ` Andrey Konovalov
2021-01-15 16:30     ` Andrey Konovalov
2021-01-15 16:55       ` Catalin Marinas
2021-01-15 17:00         ` Andrey Konovalov
2021-01-15 17:05           ` Catalin Marinas
2021-01-15 17:39             ` Andrey Konovalov

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).