* [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations
@ 2023-07-11 13:46 Vlastimil Babka
2023-07-11 13:46 ` [PATCH 2/2] mm/slub: remove freelist_dereference() Vlastimil Babka
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Vlastimil Babka @ 2023-07-11 13:46 UTC (permalink / raw)
To: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim
Cc: Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, Kees Cook, linux-hardening,
Vlastimil Babka
Commit d36a63a943e3 ("kasan, slub: fix more conflicts with
CONFIG_SLAB_FREELIST_HARDENED") has introduced kasan_reset_tags() to
freelist_ptr() encoding/decoding when CONFIG_SLAB_FREELIST_HARDENED is
enabled to resolve issues when passing tagged or untagged pointers
inconsistently would lead to incorrect calculations.
Later, commit aa1ef4d7b3f6 ("kasan, mm: reset tags when accessing
metadata") made sure all pointers have tags reset regardless of
CONFIG_SLAB_FREELIST_HARDENED, because there was no other way to access
the freepointer metadata safely with hw tag-based KASAN.
Therefore the kasan_reset_tag() usage in freelist_ptr_encode()/decode()
is now redundant, as all callers use kasan_reset_tag() unconditionally
when constructing ptr_addr. Remove the redundant calls and simplify the
code and remove obsolete comments.
Also in freelist_ptr_encode() introduce an 'encoded' variable to make
the lines shorter and make it similar to the _decode() one.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
These 2 patches build on top of:
https://lore.kernel.org/all/20230704135834.3884421-1-matteorizzo@google.com/
mm/slub.c | 22 ++++++----------------
1 file changed, 6 insertions(+), 16 deletions(-)
diff --git a/mm/slub.c b/mm/slub.c
index f8cc47eff742..07edad305512 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -374,22 +374,14 @@ typedef struct { unsigned long v; } freeptr_t;
static inline freeptr_t freelist_ptr_encode(const struct kmem_cache *s,
void *ptr, unsigned long ptr_addr)
{
+ unsigned long encoded;
+
#ifdef CONFIG_SLAB_FREELIST_HARDENED
- /*
- * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged.
- * Normally, this doesn't cause any issues, as both set_freepointer()
- * and get_freepointer() are called with a pointer with the same tag.
- * However, there are some issues with CONFIG_SLUB_DEBUG code. For
- * example, when __free_slub() iterates over objects in a cache, it
- * passes untagged pointers to check_object(). check_object() in turns
- * calls get_freepointer() with an untagged pointer, which causes the
- * freepointer to be restored incorrectly.
- */
- return (freeptr_t){.v = (unsigned long)ptr ^ s->random ^
- swab((unsigned long)kasan_reset_tag((void *)ptr_addr))};
+ encoded = (unsigned long)ptr ^ s->random ^ swab(ptr_addr);
#else
- return (freeptr_t){.v = (unsigned long)ptr};
+ encoded = (unsigned long)ptr;
#endif
+ return (freeptr_t){.v = encoded};
}
static inline void *freelist_ptr_decode(const struct kmem_cache *s,
@@ -398,9 +390,7 @@ static inline void *freelist_ptr_decode(const struct kmem_cache *s,
void *decoded;
#ifdef CONFIG_SLAB_FREELIST_HARDENED
- /* See the comment in freelist_ptr_encode */
- decoded = (void *)(ptr.v ^ s->random ^
- swab((unsigned long)kasan_reset_tag((void *)ptr_addr)));
+ decoded = (void *)(ptr.v ^ s->random ^ swab(ptr_addr));
#else
decoded = (void *)ptr.v;
#endif
--
2.41.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 2/2] mm/slub: remove freelist_dereference()
2023-07-11 13:46 [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Vlastimil Babka
@ 2023-07-11 13:46 ` Vlastimil Babka
2023-07-11 16:21 ` Kees Cook
2023-07-11 16:14 ` [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Kees Cook
2023-07-12 16:23 ` Andrey Konovalov
2 siblings, 1 reply; 7+ messages in thread
From: Vlastimil Babka @ 2023-07-11 13:46 UTC (permalink / raw)
To: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim
Cc: Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, Kees Cook, linux-hardening,
Vlastimil Babka
freelist_dereference() is a one-liner only used from get_freepointer().
Remove it and make get_freepointer() call freelist_ptr_decode()
directly to make the code easier to follow.
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
mm/slub.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/mm/slub.c b/mm/slub.c
index 07edad305512..c4556a5dab4b 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -397,18 +397,14 @@ static inline void *freelist_ptr_decode(const struct kmem_cache *s,
return decoded;
}
-/* Returns the freelist pointer recorded at location ptr_addr. */
-static inline void *freelist_dereference(const struct kmem_cache *s,
- void *ptr_addr)
-{
- return freelist_ptr_decode(s, *(freeptr_t *)(ptr_addr),
- (unsigned long)ptr_addr);
-}
-
static inline void *get_freepointer(struct kmem_cache *s, void *object)
{
- object = kasan_reset_tag(object);
- return freelist_dereference(s, (freeptr_t *)(object + s->offset));
+ unsigned long ptr_addr;
+ freeptr_t p;
+
+ ptr_addr = ((unsigned long)kasan_reset_tag(object)) + s->offset;
+ p = *(freeptr_t *)(ptr_addr);
+ return freelist_ptr_decode(s, p, ptr_addr);
}
#ifndef CONFIG_SLUB_TINY
--
2.41.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 2/2] mm/slub: remove freelist_dereference()
2023-07-11 13:46 ` [PATCH 2/2] mm/slub: remove freelist_dereference() Vlastimil Babka
@ 2023-07-11 16:21 ` Kees Cook
2023-07-13 7:44 ` Vlastimil Babka
0 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2023-07-11 16:21 UTC (permalink / raw)
To: Vlastimil Babka
Cc: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim,
Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, linux-hardening
On Tue, Jul 11, 2023 at 03:46:25PM +0200, Vlastimil Babka wrote:
> freelist_dereference() is a one-liner only used from get_freepointer().
> Remove it and make get_freepointer() call freelist_ptr_decode()
> directly to make the code easier to follow.
>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
> mm/slub.c | 16 ++++++----------
> 1 file changed, 6 insertions(+), 10 deletions(-)
>
> diff --git a/mm/slub.c b/mm/slub.c
> index 07edad305512..c4556a5dab4b 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -397,18 +397,14 @@ static inline void *freelist_ptr_decode(const struct kmem_cache *s,
> return decoded;
> }
>
> -/* Returns the freelist pointer recorded at location ptr_addr. */
> -static inline void *freelist_dereference(const struct kmem_cache *s,
> - void *ptr_addr)
> -{
> - return freelist_ptr_decode(s, *(freeptr_t *)(ptr_addr),
> - (unsigned long)ptr_addr);
> -}
> -
> static inline void *get_freepointer(struct kmem_cache *s, void *object)
> {
> - object = kasan_reset_tag(object);
> - return freelist_dereference(s, (freeptr_t *)(object + s->offset));
> + unsigned long ptr_addr;
> + freeptr_t p;
> +
> + ptr_addr = ((unsigned long)kasan_reset_tag(object)) + s->offset;
> + p = *(freeptr_t *)(ptr_addr);
> + return freelist_ptr_decode(s, p, ptr_addr);
> }
>
> #ifndef CONFIG_SLUB_TINY
> --
> 2.41.0
>
I like reducing the complexity here, but I find dropping the "object"
reassignment makes this a bit harder to read. What about:
object = kasan_reset_tag(object);
unsigned long ptr_addr = (unsigned long)object + s->offset;
freeptr_t p = *(freeptr_t *)(ptr_addr);
return freelist_ptr_decode(s, p, ptr_addr);
?
They're the same result, so either way:
Acked-by: Kees Cook <keescook@chromium.org>
--
Kees Cook
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 2/2] mm/slub: remove freelist_dereference()
2023-07-11 16:21 ` Kees Cook
@ 2023-07-13 7:44 ` Vlastimil Babka
2023-07-14 8:01 ` Vlastimil Babka
0 siblings, 1 reply; 7+ messages in thread
From: Vlastimil Babka @ 2023-07-13 7:44 UTC (permalink / raw)
To: Kees Cook
Cc: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim,
Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, linux-hardening
On 7/11/23 18:21, Kees Cook wrote:
> On Tue, Jul 11, 2023 at 03:46:25PM +0200, Vlastimil Babka wrote:
>>
>> #ifndef CONFIG_SLUB_TINY
>> --
>> 2.41.0
>>
>
> I like reducing the complexity here, but I find dropping the "object"
> reassignment makes this a bit harder to read. What about:
Alright.
> object = kasan_reset_tag(object);
> unsigned long ptr_addr = (unsigned long)object + s->offset;
> freeptr_t p = *(freeptr_t *)(ptr_addr);
Are we really so benevolent with declaration-after-statement now? :)
> return freelist_ptr_decode(s, p, ptr_addr);
>
> ?
>
> They're the same result, so either way:
>
> Acked-by: Kees Cook <keescook@chromium.org>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] mm/slub: remove freelist_dereference()
2023-07-13 7:44 ` Vlastimil Babka
@ 2023-07-14 8:01 ` Vlastimil Babka
0 siblings, 0 replies; 7+ messages in thread
From: Vlastimil Babka @ 2023-07-14 8:01 UTC (permalink / raw)
To: Kees Cook
Cc: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim,
Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, linux-hardening
On 7/13/23 09:44, Vlastimil Babka wrote:
> On 7/11/23 18:21, Kees Cook wrote:
>> On Tue, Jul 11, 2023 at 03:46:25PM +0200, Vlastimil Babka wrote:
>>>
>>> #ifndef CONFIG_SLUB_TINY
>>> --
>>> 2.41.0
>>>
>>
>> I like reducing the complexity here, but I find dropping the "object"
>> reassignment makes this a bit harder to read. What about:
>
> Alright.
>
>> object = kasan_reset_tag(object);
>> unsigned long ptr_addr = (unsigned long)object + s->offset;
>> freeptr_t p = *(freeptr_t *)(ptr_addr);
>
> Are we really so benevolent with declaration-after-statement now? :)
I've left the declarations separate for now so it's similar to
get_freepointer_safe(). Pushed the result to slab/for-6.6/cleanup and
for-next. Thanks for the reviews!
>> return freelist_ptr_decode(s, p, ptr_addr);
>>
>> ?
>>
>> They're the same result, so either way:
>>
>> Acked-by: Kees Cook <keescook@chromium.org>
>>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations
2023-07-11 13:46 [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Vlastimil Babka
2023-07-11 13:46 ` [PATCH 2/2] mm/slub: remove freelist_dereference() Vlastimil Babka
@ 2023-07-11 16:14 ` Kees Cook
2023-07-12 16:23 ` Andrey Konovalov
2 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-07-11 16:14 UTC (permalink / raw)
To: Vlastimil Babka
Cc: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim,
Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, linux-hardening
On Tue, Jul 11, 2023 at 03:46:24PM +0200, Vlastimil Babka wrote:
> Commit d36a63a943e3 ("kasan, slub: fix more conflicts with
> CONFIG_SLAB_FREELIST_HARDENED") has introduced kasan_reset_tags() to
> freelist_ptr() encoding/decoding when CONFIG_SLAB_FREELIST_HARDENED is
> enabled to resolve issues when passing tagged or untagged pointers
> inconsistently would lead to incorrect calculations.
>
> Later, commit aa1ef4d7b3f6 ("kasan, mm: reset tags when accessing
> metadata") made sure all pointers have tags reset regardless of
> CONFIG_SLAB_FREELIST_HARDENED, because there was no other way to access
> the freepointer metadata safely with hw tag-based KASAN.
>
> Therefore the kasan_reset_tag() usage in freelist_ptr_encode()/decode()
> is now redundant, as all callers use kasan_reset_tag() unconditionally
> when constructing ptr_addr. Remove the redundant calls and simplify the
> code and remove obsolete comments.
>
> Also in freelist_ptr_encode() introduce an 'encoded' variable to make
> the lines shorter and make it similar to the _decode() one.
>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Thanks, this is much more readable!
Acked-by: Kees Cook <keescook@chromium.org>
--
Kees Cook
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations
2023-07-11 13:46 [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Vlastimil Babka
2023-07-11 13:46 ` [PATCH 2/2] mm/slub: remove freelist_dereference() Vlastimil Babka
2023-07-11 16:14 ` [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Kees Cook
@ 2023-07-12 16:23 ` Andrey Konovalov
2 siblings, 0 replies; 7+ messages in thread
From: Andrey Konovalov @ 2023-07-12 16:23 UTC (permalink / raw)
To: Vlastimil Babka
Cc: Christoph Lameter, David Rientjes, Pekka Enberg, Joonsoo Kim,
Hyeonggon Yoo, Roman Gushchin, linux-mm, patches, linux-kernel,
Matteo Rizzo, Jann Horn, Andrey Konovalov, Marco Elver,
Alexander Potapenko, kasan-dev, Kees Cook, linux-hardening
On Tue, Jul 11, 2023 at 3:46 PM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> Commit d36a63a943e3 ("kasan, slub: fix more conflicts with
> CONFIG_SLAB_FREELIST_HARDENED") has introduced kasan_reset_tags() to
> freelist_ptr() encoding/decoding when CONFIG_SLAB_FREELIST_HARDENED is
> enabled to resolve issues when passing tagged or untagged pointers
> inconsistently would lead to incorrect calculations.
>
> Later, commit aa1ef4d7b3f6 ("kasan, mm: reset tags when accessing
> metadata") made sure all pointers have tags reset regardless of
> CONFIG_SLAB_FREELIST_HARDENED, because there was no other way to access
> the freepointer metadata safely with hw tag-based KASAN.
>
> Therefore the kasan_reset_tag() usage in freelist_ptr_encode()/decode()
> is now redundant, as all callers use kasan_reset_tag() unconditionally
> when constructing ptr_addr. Remove the redundant calls and simplify the
> code and remove obsolete comments.
>
> Also in freelist_ptr_encode() introduce an 'encoded' variable to make
> the lines shorter and make it similar to the _decode() one.
>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
> These 2 patches build on top of:
> https://lore.kernel.org/all/20230704135834.3884421-1-matteorizzo@google.com/
>
> mm/slub.c | 22 ++++++----------------
> 1 file changed, 6 insertions(+), 16 deletions(-)
>
> diff --git a/mm/slub.c b/mm/slub.c
> index f8cc47eff742..07edad305512 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -374,22 +374,14 @@ typedef struct { unsigned long v; } freeptr_t;
> static inline freeptr_t freelist_ptr_encode(const struct kmem_cache *s,
> void *ptr, unsigned long ptr_addr)
> {
> + unsigned long encoded;
> +
> #ifdef CONFIG_SLAB_FREELIST_HARDENED
> - /*
> - * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged.
> - * Normally, this doesn't cause any issues, as both set_freepointer()
> - * and get_freepointer() are called with a pointer with the same tag.
> - * However, there are some issues with CONFIG_SLUB_DEBUG code. For
> - * example, when __free_slub() iterates over objects in a cache, it
> - * passes untagged pointers to check_object(). check_object() in turns
> - * calls get_freepointer() with an untagged pointer, which causes the
> - * freepointer to be restored incorrectly.
> - */
> - return (freeptr_t){.v = (unsigned long)ptr ^ s->random ^
> - swab((unsigned long)kasan_reset_tag((void *)ptr_addr))};
> + encoded = (unsigned long)ptr ^ s->random ^ swab(ptr_addr);
> #else
> - return (freeptr_t){.v = (unsigned long)ptr};
> + encoded = (unsigned long)ptr;
> #endif
> + return (freeptr_t){.v = encoded};
> }
>
> static inline void *freelist_ptr_decode(const struct kmem_cache *s,
> @@ -398,9 +390,7 @@ static inline void *freelist_ptr_decode(const struct kmem_cache *s,
> void *decoded;
>
> #ifdef CONFIG_SLAB_FREELIST_HARDENED
> - /* See the comment in freelist_ptr_encode */
> - decoded = (void *)(ptr.v ^ s->random ^
> - swab((unsigned long)kasan_reset_tag((void *)ptr_addr)));
> + decoded = (void *)(ptr.v ^ s->random ^ swab(ptr_addr));
> #else
> decoded = (void *)ptr.v;
> #endif
> --
> 2.41.0
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-07-14 8:25 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-11 13:46 [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Vlastimil Babka
2023-07-11 13:46 ` [PATCH 2/2] mm/slub: remove freelist_dereference() Vlastimil Babka
2023-07-11 16:21 ` Kees Cook
2023-07-13 7:44 ` Vlastimil Babka
2023-07-14 8:01 ` Vlastimil Babka
2023-07-11 16:14 ` [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Kees Cook
2023-07-12 16:23 ` 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).