From: Andrey Konovalov <andreyknvl@gmail.com>
To: Jann Horn <jannh@google.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
Christoph Lameter <cl@linux.com>,
Pekka Enberg <penberg@kernel.org>,
David Rientjes <rientjes@google.com>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Vlastimil Babka <vbabka@suse.cz>,
Roman Gushchin <roman.gushchin@linux.dev>,
Hyeonggon Yoo <42.hyeyoo@gmail.com>,
Marco Elver <elver@google.com>,
kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH v5 2/2] slub: Introduce CONFIG_SLUB_RCU_DEBUG
Date: Fri, 2 Aug 2024 21:35:10 +0200 [thread overview]
Message-ID: <CA+fCnZdNWPnjSvPF5dg0NA+f8y=rtxtbDC13cZJCz+rQVb=ouA@mail.gmail.com> (raw)
In-Reply-To: <CAG48ez2bqYMPS2D7gFZ-9V3p3-NJUYmYNA113QbMg0JRG+pNEQ@mail.gmail.com>
On Fri, Aug 2, 2024 at 1:23 PM Jann Horn <jannh@google.com> wrote:
>
> On Fri, Aug 2, 2024 at 11:09 AM Jann Horn <jannh@google.com> wrote:
> > I guess I could also change the API to pass something different - like
> > a flag meaning "the object is guaranteed to no longer be in use".
> > There is already code in slab_free_hook() that computes this
> > expression, so we could easily pass that to KASAN and then avoid doing
> > the same logic in KASAN again... I think that would be the most
> > elegant approach?
>
> Regarding this, I think I'll add something like this on top of this patch in v6:
>
> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index b63f5351c5f3..50bad011352e 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -201,16 +201,17 @@ bool __kasan_slab_free(struct kmem_cache *s,
> void *object, bool init,
> /**
> * kasan_slab_free - Possibly handle slab object freeing.
> * @object: Object to free.
> + * @still_accessible: Whether the object contents are still accessible.
> *
> * This hook is called from the slab allocator to give KASAN a chance to take
> * ownership of the object and handle its freeing.
> * kasan_slab_pre_free() must have already been called on the same object.
> *
> * @Return true if KASAN took ownership of the object; false otherwise.
> */
> static __always_inline bool kasan_slab_free(struct kmem_cache *s,
> void *object, bool init,
> - bool after_rcu_delay)
> + bool still_accessible)
> {
> if (kasan_enabled())
> return __kasan_slab_free(s, object, init, after_rcu_delay);
> @@ -410,7 +411,7 @@ static inline bool kasan_slab_pre_free(struct
> kmem_cache *s, void *object)
> }
>
> static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
> - bool init, bool after_rcu_delay)
> + bool init, bool still_accessible)
> {
> return false;
> }
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 71a20818b122..ed4873e18c75 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -230,14 +230,14 @@ static bool check_slab_allocation(struct
> kmem_cache *cache, void *object,
> }
>
> static inline void poison_slab_object(struct kmem_cache *cache, void *object,
> - bool init, bool after_rcu_delay)
> + bool init, bool still_accessible)
> {
> void *tagged_object = object;
>
> object = kasan_reset_tag(object);
>
> /* RCU slabs could be legally used after free within the RCU period. */
> - if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU) && !after_rcu_delay)
> + if (unlikely(still_accessible))
> return;
>
> kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
> @@ -256,12 +256,12 @@ bool __kasan_slab_pre_free(struct kmem_cache
> *cache, void *object,
> }
>
> bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init,
> - bool after_rcu_delay)
> + bool still_accessible)
> {
> if (!kasan_arch_is_ready() || is_kfence_address(object))
> return false;
>
> - poison_slab_object(cache, object, init, after_rcu_delay);
> + poison_slab_object(cache, object, init, still_accessible);
>
> /*
> * If the object is put into quarantine, do not let slab put the object
> diff --git a/mm/slub.c b/mm/slub.c
> index 49571d5ded75..a89f2006d46e 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2221,31 +2221,34 @@ static __always_inline
> bool slab_free_hook(struct kmem_cache *s, void *x, bool init,
> bool after_rcu_delay)
> {
> + /* Are the object contents still accessible? */
> + bool still_accessible = (s->flags & SLAB_TYPESAFE_BY_RCU) &&
> !after_rcu_delay;
> +
> kmemleak_free_recursive(x, s->flags);
> kmsan_slab_free(s, x);
>
> debug_check_no_locks_freed(x, s->object_size);
>
> if (!(s->flags & SLAB_DEBUG_OBJECTS))
> debug_check_no_obj_freed(x, s->object_size);
>
> /* Use KCSAN to help debug racy use-after-free. */
> - if (!(s->flags & SLAB_TYPESAFE_BY_RCU) || after_rcu_delay)
> + if (!still_accessible)
> __kcsan_check_access(x, s->object_size,
> KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT);
>
> if (kfence_free(x))
> return false;
>
> /*
> * Give KASAN a chance to notice an invalid free operation before we
> * modify the object.
> */
> if (kasan_slab_pre_free(s, x))
> return false;
>
> #ifdef CONFIG_SLUB_RCU_DEBUG
> - if ((s->flags & SLAB_TYPESAFE_BY_RCU) && !after_rcu_delay) {
> + if (still_accessible) {
> struct rcu_delayed_free *delayed_free;
>
> delayed_free = kmalloc(sizeof(*delayed_free), GFP_NOWAIT);
> @@ -2289,7 +2292,7 @@ bool slab_free_hook(struct kmem_cache *s, void
> *x, bool init,
> s->size - inuse - rsize);
> }
> /* KASAN might put x into memory quarantine, delaying its reuse. */
> - return !kasan_slab_free(s, x, init, after_rcu_delay);
> + return !kasan_slab_free(s, x, init, still_accessible);
> }
>
> static __fastpath_inline
Ok, let's do it like this.
Thank you!
next prev parent reply other threads:[~2024-08-02 19:35 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-30 11:06 [PATCH v5 0/2] allow KASAN to detect UAF in SLAB_TYPESAFE_BY_RCU slabs Jann Horn
2024-07-30 11:06 ` [PATCH v5 1/2] kasan: catch invalid free before SLUB reinitializes the object Jann Horn
2024-08-01 0:22 ` Andrey Konovalov
2024-08-01 4:00 ` Jann Horn
2024-08-01 12:54 ` Andrey Konovalov
2024-08-02 11:05 ` Jann Horn
2024-08-02 9:56 ` Jann Horn
2024-08-02 19:35 ` Andrey Konovalov
2024-07-30 11:06 ` [PATCH v5 2/2] slub: Introduce CONFIG_SLUB_RCU_DEBUG Jann Horn
2024-07-30 11:30 ` Vlastimil Babka
2024-08-01 0:23 ` Andrey Konovalov
2024-08-02 9:09 ` Jann Horn
2024-08-02 11:22 ` Jann Horn
2024-08-02 19:35 ` Andrey Konovalov [this message]
2024-08-02 8:06 ` Marco Elver
2024-08-02 8:16 ` Jann Horn
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+fCnZdNWPnjSvPF5dg0NA+f8y=rtxtbDC13cZJCz+rQVb=ouA@mail.gmail.com' \
--to=andreyknvl@gmail.com \
--cc=42.hyeyoo@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=iamjoonsoo.kim@lge.com \
--cc=jannh@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=ryabinin.a.a@gmail.com \
--cc=vbabka@suse.cz \
--cc=vincenzo.frascino@arm.com \
/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