Skip to contentSkip to conversation

kasan: fix redzone overwritten issue under SLUB debug

PATCH Superseded [email protected] 3 participants 5 messages Jun 24, 2021 – Jun 28, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover kasan: fix redzone overwritten issue under SLUB debug

From: Yee Lee <[email protected]>

Issue: In SLUB debug, hwtag kasan_unpoison() would overwrite the redzone
in those objects with unaligned size.

This patch Adds memzero_explict() to separate the initialization for such condition. The new code path is executed about 1.1% during nromal booting process.

=============
Exp: QEMUv5.2(+mte)/SLUB_debug mode
code path exec : 941/80854 (1.1%)

---
Changed since v1:
- Apply IS_ENABLED to wrap codes under SLUB debug mode.
- Replace memset() by memzero_explict().

---

Yee Lee (1):
kasan: Add memzero init for unaligned size under SLUB debug

 mm/kasan/kasan.h | 6 ++++++
 1 file changed, 6 insertions(+)
Signature · 5 lines
2.18.0
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
#1 lore raw

PATCH v2 1/1 kasan: Add memzero init for unaligned size under SLUB debug

From: Yee Lee <[email protected]>

Issue: when SLUB debug is on, hwtag kasan_unpoison() would overwrite the redzone of object with unaligned size.

An additional memzero_explicit() path is added to replacing init by hwtag instruction for those unaligned size at SLUB debug mode.

---
 mm/kasan/kasan.h | 6 ++++++
 1 file changed, 6 insertions(+)
mm/kasan/kasan.h +6 -0
index 8f450bc28045..d1054f35838f 100644
@@ -387,6 +387,12 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)
 
 	if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
 		return;
+#if IS_ENABLED(CONFIG_SLUB_DEBUG)
+	if (init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
+		init = false;
+		memzero_explicit((void *)addr, size);
+	}
+#endif
 	size = round_up(size, KASAN_GRANULE_SIZE);
 
 	hw_set_mem_tag_range((void *)addr, size, tag, init);
Signature · 1 line
2.18.0
#2 lore raw

Re: [PATCH v2 1/1] kasan: Add memzero init for unaligned size under SLUB debug

Quoted 19 lines · On Thu, Jun 24, 2021 at 2:26 PM <[email protected]> wrote:

From: Yee Lee <[email protected]>

Issue: when SLUB debug is on, hwtag kasan_unpoison() would overwrite the redzone of object with unaligned size.

An additional memzero_explicit() path is added to replacing init by hwtag instruction for those unaligned size at SLUB debug mode.

---
 mm/kasan/kasan.h | 6 ++++++
 1 file changed, 6 insertions(+)
mm/kasan/kasan.h +0 -0
index 8f450bc28045..d1054f35838f 100644

mm/kasan/kasan.h static inline void kasan_unpoison(const void *addr, size_t size, bool init) · line 390

Quoted 4 lines
mm/kasan/kasan.h +1 -0
@@ -387,6 +387,12 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)
 
        if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
                return;
+#if IS_ENABLED(CONFIG_SLUB_DEBUG)

Is this an issue only with SLUB? SLAB also uses redzones.

mm/kasan/kasan.h static inline void kasan_unpoison(const void *addr, size_t size, bool init) · line 391

Quoted 1 line
mm/kasan/kasan.h +1 -0
+       if (init && ((unsigned long)size & KASAN_GRANULE_MASK)) {

This needs a comment along the lines of:

/* Explicitly initialize the memory with the precise object size to avoid overwriting the SLAB redzone. This disables initialization in the arch code and may thus lead to performance penalty. The penalty is accepted since SLAB redzones aren't enabled in production builds. */

Quoted 10 lines
mm/kasan/kasan.h +4 -1
+               init = false;
+               memzero_explicit((void *)addr, size);
+       }
+#endif
        size = round_up(size, KASAN_GRANULE_SIZE);
 
        hw_set_mem_tag_range((void *)addr, size, tag, init);
--

2.18.0

author
#3 lore raw

Re: [PATCH v2 1/1] kasan: Add memzero init for unaligned size under SLUB debug

Quoted 23 lines · Andrey Konovalov
Quoted 22 lines · On Thu, Jun 24, 2021 at 2:26 PM <[email protected]> wrote:

From: Yee Lee <[email protected]>

Issue: when SLUB debug is on, hwtag kasan_unpoison() would overwrite
the redzone of object with unaligned size.

An additional memzero_explicit() path is added to replacing init by hwtag instruction for those unaligned size at SLUB debug mode.

---
 mm/kasan/kasan.h | 6 ++++++
 1 file changed, 6 insertions(+)
mm/kasan/kasan.h +0 -0
index 8f450bc28045..d1054f35838f 100644
@@ -387,6 +387,12 @@ static inline void kasan_unpoison(const void
 *addr, size_t size, bool init)
 
Quoted 5 lines

mm/kasan/kasan.h static inline void kasan_unpoison(const void *addr, size_t size, bool init) · line 390

Quoted 3 lines
mm/kasan/kasan.h +1 -0
        if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
                return;
+#if IS_ENABLED(CONFIG_SLUB_DEBUG)

Is this an issue only with SLUB? SLAB also uses redzones.

As I known, hw-tag kasan only works with SLUB.

Quoted 10 lines

mm/kasan/kasan.h static inline void kasan_unpoison(const void *addr, size_t size, bool init) · line 391

Quoted 2 lines
mm/kasan/kasan.h +1 -0
+       if (init && ((unsigned long)size & KASAN_GRANULE_MASK)) {

This needs a comment along the lines of:

/* Explicitly initialize the memory with the precise object size to avoid overwriting the SLAB redzone. This disables initialization in the arch code and may thus lead to performance penalty. The penalty is
accepted since SLAB redzones aren't enabled in production builds. */

Sure, will work on this.

Quoted 11 lines
Quoted 11 lines
mm/kasan/kasan.h +4 -0
+               init = false;
+               memzero_explicit((void *)addr, size);
+       }
+#endif
        size = round_up(size, KASAN_GRANULE_SIZE);
 
        hw_set_mem_tag_range((void *)addr, size, tag, init);

--
2.18.0

#4 lore raw

Re: [PATCH v2 1/1] kasan: Add memzero init for unaligned size under SLUB debug

In reply to [email protected]
Quoted 19 lines · On Thu, 24 Jun 2021 at 13:27, <[email protected]> wrote:

From: Yee Lee <[email protected]>

Issue: when SLUB debug is on, hwtag kasan_unpoison() would overwrite the redzone of object with unaligned size.

An additional memzero_explicit() path is added to replacing init by hwtag instruction for those unaligned size at SLUB debug mode.

---
 mm/kasan/kasan.h | 6 ++++++
 1 file changed, 6 insertions(+)
mm/kasan/kasan.h +0 -0
index 8f450bc28045..d1054f35838f 100644

mm/kasan/kasan.h static inline void kasan_unpoison(const void *addr, size_t size, bool init) · line 390

Quoted 4 lines
mm/kasan/kasan.h +1 -0
@@ -387,6 +387,12 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)
 
        if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
                return;
+#if IS_ENABLED(CONFIG_SLUB_DEBUG)

Avoid the #if. I think none of the stuff referenced here is only available if CONFIG_SLUB_DEBUG. In that case, please just write:

if (IS_ENABLED(CONFIG_SLUB_DEBUG) && init && .........) {

The compiler will correctly optimize out the branch if the config option is not enabled. But the benefit is we compile-test this code with all configs.

mm/kasan/kasan.h static inline void kasan_unpoison(const void *addr, size_t size, bool init) · line 395

Quoted 5 lines
mm/kasan/kasan.h +5 -0
+       if (init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
+               init = false;
+               memzero_explicit((void *)addr, size);
+       }
+#endif

Thanks,
-- Marco