Skip to contentSkip to conversation

kasan: fixes for 5.11-rc

PATCH Superseded Andrey Konovalov 2 participants 5 messages Jan 14, 2021 – Jan 15, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover kasan: fixes for 5.11-rc

Changes v1->v2:
- Update changelog of patch #1.

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(-)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
#1 lore raw

PATCH v2 1/2 kasan, mm: fix conflicts with init_on_alloc/free

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.

(The kasan_reset_tag call is removed from slab_alloc_node, as it's added
into maybe_wipe_obj_freeptr.)

---
 mm/slub.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/slub.c +4 -3
index dc5b42e700b8..75fb097d990d 100644
@@ -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 */
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
#2 lore raw

PATCH v2 2/2 kasan, arm64: fix pointer tags in KASAN reports

In reply to 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.

Fixes dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
Fixes 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
---
 arch/arm64/mm/fault.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/mm/fault.c +2 -0
index 3c40da479899..a218f6f2fdc8 100644
@@ -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.
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
maintainer
#3 lore raw

Re: [PATCH v2 2/2] kasan, arm64: fix pointer tags in KASAN reports

Quoted 19 lines · 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.

Fixes dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
Fixes 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
---
 arch/arm64/mm/fault.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/mm/fault.c +0 -0
index 3c40da479899..a218f6f2fdc8 100644

arch/arm64/mm/fault.c static void report_tag_fault(unsigned long addr, unsigned int esr, · line 311

Quoted 8 lines
arch/arm64/mm/fault.c +2 -0
@@ -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.

I already replied here but I don't see any change in v2:

https://lore.kernel.org/linux-arm-kernel/20210113165441.GC27045@gaia/

Signature · 1 line
Catalin
#4 lore raw

Re: [PATCH v2 2/2] kasan, arm64: fix pointer tags in KASAN reports

Quoted 32 lines · Catalin Marinas
Quoted 19 lines · 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.

Fixes dceec3ff7807 ("arm64: expose FAR_EL1 tag bits in siginfo")
Fixes 4291e9ee6189 ("kasan, arm64: print report from tag fault handler")
---
 arch/arm64/mm/fault.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/mm/fault.c +0 -0
index 3c40da479899..a218f6f2fdc8 100644

arch/arm64/mm/fault.c static void report_tag_fault(unsigned long addr, unsigned int esr, · line 311

Quoted 8 lines
arch/arm64/mm/fault.c +2 -0
@@ -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.

I already replied here but I don't see any change in v2:

Quoted 1 line

https://lore.kernel.org/linux-arm-kernel/20210113165441.GC27045@gaia/

Hi Catalin,

Sorry, Gmail decided to mark all your emails as spam for some reason, so I didn't see any of them :(

I'll fix this in v3.

Thanks!