2021-07-05 10:35:22

by Yee Lee (李建誼)

[permalink] [raw]
Subject: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at 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.

The penalty is acceptable since they are only enabled in debug mode,
not production builds. A block of comment is added for explanation.

Cc: Andrey Ryabinin <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Andrew Morton <[email protected]>
Suggested-by: Marco Elver <[email protected]>
Suggested-by: Andrey Konovalov <[email protected]>
Signed-off-by: Yee Lee <[email protected]>
---
mm/kasan/kasan.h | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
index 98e3059bfea4..d739cdd1621a 100644
--- a/mm/kasan/kasan.h
+++ b/mm/kasan/kasan.h
@@ -9,6 +9,7 @@
#ifdef CONFIG_KASAN_HW_TAGS

#include <linux/static_key.h>
+#include "../slab.h"

DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
extern bool kasan_flag_async __ro_after_init;
@@ -387,6 +388,17 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)

if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
return;
+ /*
+ * 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.
+ */
+ if (__slub_debug_enabled() &&
+ init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
+ init = false;
+ memzero_explicit((void *)addr, size);
+ }
size = round_up(size, KASAN_GRANULE_SIZE);

hw_set_mem_tag_range((void *)addr, size, tag, init);
--
2.18.0


2021-07-05 10:46:20

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG

On Mon, 5 Jul 2021 at 12:33, <[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.
>
> The penalty is acceptable since they are only enabled in debug mode,
> not production builds. A block of comment is added for explanation.
>
> Cc: Andrey Ryabinin <[email protected]>
> Cc: Alexander Potapenko <[email protected]>
> Cc: Dmitry Vyukov <[email protected]>
> Cc: Andrew Morton <[email protected]>
> Suggested-by: Marco Elver <[email protected]>
> Suggested-by: Andrey Konovalov <[email protected]>
> Signed-off-by: Yee Lee <[email protected]>

Reviewed-by: Marco Elver <[email protected]>

Thank you!

> ---
> mm/kasan/kasan.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> index 98e3059bfea4..d739cdd1621a 100644
> --- a/mm/kasan/kasan.h
> +++ b/mm/kasan/kasan.h
> @@ -9,6 +9,7 @@
> #ifdef CONFIG_KASAN_HW_TAGS
>
> #include <linux/static_key.h>
> +#include "../slab.h"
>
> DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
> extern bool kasan_flag_async __ro_after_init;
> @@ -387,6 +388,17 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)
>
> if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
> return;
> + /*
> + * 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.
> + */
> + if (__slub_debug_enabled() &&
> + init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
> + init = false;
> + memzero_explicit((void *)addr, size);
> + }
> size = round_up(size, KASAN_GRANULE_SIZE);
>
> hw_set_mem_tag_range((void *)addr, size, tag, init);
> --
> 2.18.0

2021-07-05 11:14:15

by Andrey Konovalov

[permalink] [raw]
Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG

On Mon, Jul 5, 2021 at 12:33 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.
>
> The penalty is acceptable since they are only enabled in debug mode,
> not production builds. A block of comment is added for explanation.
>
> Cc: Andrey Ryabinin <[email protected]>
> Cc: Alexander Potapenko <[email protected]>
> Cc: Dmitry Vyukov <[email protected]>
> Cc: Andrew Morton <[email protected]>
> Suggested-by: Marco Elver <[email protected]>
> Suggested-by: Andrey Konovalov <[email protected]>
> Signed-off-by: Yee Lee <[email protected]>
> ---
> mm/kasan/kasan.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> index 98e3059bfea4..d739cdd1621a 100644
> --- a/mm/kasan/kasan.h
> +++ b/mm/kasan/kasan.h
> @@ -9,6 +9,7 @@
> #ifdef CONFIG_KASAN_HW_TAGS
>
> #include <linux/static_key.h>
> +#include "../slab.h"
>
> DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
> extern bool kasan_flag_async __ro_after_init;
> @@ -387,6 +388,17 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)
>
> if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
> return;
> + /*
> + * 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.
> + */
> + if (__slub_debug_enabled() &&

What happened to slub_debug_enabled_unlikely()? Was it renamed? Why? I
didn't receive patch #1 of v6 (nor of v5).

> + init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
> + init = false;
> + memzero_explicit((void *)addr, size);
> + }
> size = round_up(size, KASAN_GRANULE_SIZE);
>
> hw_set_mem_tag_range((void *)addr, size, tag, init);
> --
> 2.18.0
>

2021-07-05 11:19:21

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG

On Mon, 5 Jul 2021 at 13:12, Andrey Konovalov <[email protected]> wrote:
[...]
> > + /*
> > + * 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.
> > + */
> > + if (__slub_debug_enabled() &&
>
> What happened to slub_debug_enabled_unlikely()? Was it renamed? Why? I
> didn't receive patch #1 of v6 (nor of v5).

Somebody had the same idea with the helper:
https://lkml.kernel.org/r/[email protected]
and Matthew didn't like the _unlikely() prefix.

Which meant we should just move the existing helper introduced in the
merge window.

Patch 1/2: https://lkml.kernel.org/r/[email protected]

Thanks,
-- Marco

2021-07-05 11:24:07

by Andrey Konovalov

[permalink] [raw]
Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG

On Mon, Jul 5, 2021 at 1:18 PM Marco Elver <[email protected]> wrote:
>
> On Mon, 5 Jul 2021 at 13:12, Andrey Konovalov <[email protected]> wrote:
> [...]
> > > + /*
> > > + * 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.
> > > + */
> > > + if (__slub_debug_enabled() &&
> >
> > What happened to slub_debug_enabled_unlikely()? Was it renamed? Why? I
> > didn't receive patch #1 of v6 (nor of v5).
>
> Somebody had the same idea with the helper:
> https://lkml.kernel.org/r/[email protected]
> and Matthew didn't like the _unlikely() prefix.
>
> Which meant we should just move the existing helper introduced in the
> merge window.
>
> Patch 1/2: https://lkml.kernel.org/r/[email protected]

Got it. Thank you, Marco!

2021-07-05 11:25:42

by Andrey Konovalov

[permalink] [raw]
Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG

On Mon, Jul 5, 2021 at 12:33 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.
>
> The penalty is acceptable since they are only enabled in debug mode,
> not production builds. A block of comment is added for explanation.
>
> Cc: Andrey Ryabinin <[email protected]>
> Cc: Alexander Potapenko <[email protected]>
> Cc: Dmitry Vyukov <[email protected]>
> Cc: Andrew Morton <[email protected]>
> Suggested-by: Marco Elver <[email protected]>
> Suggested-by: Andrey Konovalov <[email protected]>
> Signed-off-by: Yee Lee <[email protected]>
> ---
> mm/kasan/kasan.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> index 98e3059bfea4..d739cdd1621a 100644
> --- a/mm/kasan/kasan.h
> +++ b/mm/kasan/kasan.h
> @@ -9,6 +9,7 @@
> #ifdef CONFIG_KASAN_HW_TAGS
>
> #include <linux/static_key.h>
> +#include "../slab.h"
>
> DECLARE_STATIC_KEY_FALSE(kasan_flag_stacktrace);
> extern bool kasan_flag_async __ro_after_init;
> @@ -387,6 +388,17 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init)
>
> if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
> return;
> + /*
> + * 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.
> + */
> + if (__slub_debug_enabled() &&
> + init && ((unsigned long)size & KASAN_GRANULE_MASK)) {
> + init = false;
> + memzero_explicit((void *)addr, size);
> + }
> size = round_up(size, KASAN_GRANULE_SIZE);
>
> hw_set_mem_tag_range((void *)addr, size, tag, init);
> --
> 2.18.0
>

Reviewed-by: Andrey Konovalov <[email protected]>

2021-07-05 13:57:42

by Yee Lee (李建誼)

[permalink] [raw]
Subject: Re: [PATCH v6 2/2] kasan: Add memzero int for unaligned size at DEBUG

Thank you, Macro. I thought members in "suggested-by" would be put in
the list as well... And thank you guys for the review these days.


@Andrew Motom
Hi Andrew,

Could you help to push the patches? We are dealing with the issue and
would backport to Android porject right after the action.
Appreciated!


BR,
Yee


On Mon, 2021-07-05 at 13:23 +0200, Andrey Konovalov wrote:
> On Mon, Jul 5, 2021 at 1:18 PM Marco Elver <[email protected]> wrote:
> >
> > On Mon, 5 Jul 2021 at 13:12, Andrey Konovalov <[email protected]
> > > wrote:
> > [...]
> > > > + /*
> > > > + * 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.
> > > > + */
> > > > + if (__slub_debug_enabled() &&
> > >
> > > What happened to slub_debug_enabled_unlikely()? Was it renamed?
> > > Why? I
> > > didn't receive patch #1 of v6 (nor of v5).
> >
> > Somebody had the same idea with the helper:
> > https://lkml.kernel.org/r/[email protected]
> > and Matthew didn't like the _unlikely() prefix.
> >
> > Which meant we should just move the existing helper introduced in
> > the
> > merge window.
> >
> > Patch 1/2:
> > https://lkml.kernel.org/r/[email protected]
>
> Got it. Thank you, Marco!