kasan: fix redzone overwritten issue under SLUB debug
Subsystems (3)
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
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
@@ -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
Andrey Konovalov
reviewerRe: [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.Signed-off-by Yee Lee <[email protected]>--- mm/kasan/kasan.h | 6 ++++++ 1 file changed, 6 insertions(+)mm/kasan/kasan.h +0
-0
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
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.Signed-off-by Yee Lee <[email protected]>--- mm/kasan/kasan.h | 6 ++++++ 1 file changed, 6 insertions(+)mm/kasan/kasan.h +0
-0@@ -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
-0if (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
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.Signed-off-by Yee Lee <[email protected]>--- mm/kasan/kasan.h | 6 ++++++ 1 file changed, 6 insertions(+)mm/kasan/kasan.h +0
-0
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