From: Andrey Konovalov <andreyknvl@gmail.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Christoph Lameter <cl@linux.com>,
David Rientjes <rientjes@google.com>,
Pekka Enberg <penberg@kernel.org>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Hyeonggon Yoo <42.hyeyoo@gmail.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
linux-mm@kvack.org, patches@lists.linux.dev,
linux-kernel@vger.kernel.org,
Matteo Rizzo <matteorizzo@google.com>,
Jann Horn <jannh@google.com>,
Andrey Konovalov <andreyknvl@google.com>,
Marco Elver <elver@google.com>,
Alexander Potapenko <glider@google.com>,
kasan-dev@googlegroups.com, Kees Cook <keescook@chromium.org>,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations
Date: Wed, 12 Jul 2023 18:23:09 +0200 [thread overview]
Message-ID: <CA+fCnZci9E8Snjuc-rJqSeX+Gn84_AVO5OjQwyFT=vL+pw22HQ@mail.gmail.com> (raw)
In-Reply-To: <20230711134623.12695-3-vbabka@suse.cz>
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!
prev parent reply other threads:[~2023-07-12 16:23 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-11 13:46 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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+fCnZci9E8Snjuc-rJqSeX+Gn84_AVO5OjQwyFT=vL+pw22HQ@mail.gmail.com' \
--to=andreyknvl@gmail.com \
--cc=42.hyeyoo@gmail.com \
--cc=andreyknvl@google.com \
--cc=cl@linux.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=iamjoonsoo.kim@lge.com \
--cc=jannh@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=keescook@chromium.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=matteorizzo@google.com \
--cc=patches@lists.linux.dev \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox