linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Vlastimil Babka <vbabka@suse.cz>
To: Jann Horn <jannh@google.com>,
	Andrey Ryabinin <ryabinin.a.a@gmail.com>,
	Alexander Potapenko <glider@google.com>,
	Andrey Konovalov <andreyknvl@gmail.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>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Marco Elver <elver@google.com>,
	kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org
Subject: Re: [PATCH v2 1/2] kasan: catch invalid free before SLUB reinitializes the object
Date: Thu, 25 Jul 2024 15:20:18 +0200	[thread overview]
Message-ID: <1d6913c7-5a6a-412b-a4fa-0eb7b7622d4f@suse.cz> (raw)
In-Reply-To: <20240724-kasan-tsbrcu-v2-1-45f898064468@google.com>

On 7/24/24 6:34 PM, Jann Horn wrote:
> Currently, when KASAN is combined with init-on-free behavior, the
> initialization happens before KASAN's "invalid free" checks.
> 
> More importantly, a subsequent commit will want to use the object metadata
> region to store an rcu_head, and we should let KASAN check that the object
> pointer is valid before that. (Otherwise that change will make the existing
> testcase kmem_cache_invalid_free fail.)
> 
> So add a new KASAN hook that allows KASAN to pre-validate a
> kmem_cache_free() operation before SLUB actually starts modifying the
> object or its metadata.
> 
> Signed-off-by: Jann Horn <jannh@google.com>

Acked-by: Vlastimil Babka <vbabka@suse.cz> #slub

> ---
>  include/linux/kasan.h | 10 ++++++++++
>  mm/kasan/common.c     | 51 +++++++++++++++++++++++++++++++++++++++------------
>  mm/slub.c             |  7 +++++++
>  3 files changed, 56 insertions(+), 12 deletions(-)
> 
> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index 70d6a8f6e25d..eee8ca1dcb40 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -175,6 +175,16 @@ static __always_inline void * __must_check kasan_init_slab_obj(
>  	return (void *)object;
>  }
>  
> +bool __kasan_slab_pre_free(struct kmem_cache *s, void *object,
> +			unsigned long ip);
> +static __always_inline bool kasan_slab_pre_free(struct kmem_cache *s,
> +						void *object)
> +{
> +	if (kasan_enabled())
> +		return __kasan_slab_pre_free(s, object, _RET_IP_);
> +	return false;
> +}
> +
>  bool __kasan_slab_free(struct kmem_cache *s, void *object,
>  			unsigned long ip, bool init);
>  static __always_inline bool kasan_slab_free(struct kmem_cache *s,
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 85e7c6b4575c..7c7fc6ce7eb7 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -208,31 +208,52 @@ void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
>  	return (void *)object;
>  }
>  
> -static inline bool poison_slab_object(struct kmem_cache *cache, void *object,
> -				      unsigned long ip, bool init)
> +enum free_validation_result {
> +	KASAN_FREE_IS_IGNORED,
> +	KASAN_FREE_IS_VALID,
> +	KASAN_FREE_IS_INVALID
> +};
> +
> +static enum free_validation_result check_slab_free(struct kmem_cache *cache,
> +						void *object, unsigned long ip)
>  {
> -	void *tagged_object;
> +	void *tagged_object = object;
>  
> -	if (!kasan_arch_is_ready())
> -		return false;
> +	if (is_kfence_address(object) || !kasan_arch_is_ready())
> +		return KASAN_FREE_IS_IGNORED;
>  
> -	tagged_object = object;
>  	object = kasan_reset_tag(object);
>  
>  	if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != object)) {
>  		kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_INVALID_FREE);
> -		return true;
> +		return KASAN_FREE_IS_INVALID;
>  	}
>  
> -	/* RCU slabs could be legally used after free within the RCU period. */
> -	if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
> -		return false;
> -
>  	if (!kasan_byte_accessible(tagged_object)) {
>  		kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_DOUBLE_FREE);
> -		return true;
> +		return KASAN_FREE_IS_INVALID;
>  	}
>  
> +	return KASAN_FREE_IS_VALID;
> +}
> +
> +static inline bool poison_slab_object(struct kmem_cache *cache, void *object,
> +				      unsigned long ip, bool init)
> +{
> +	void *tagged_object = object;
> +	enum free_validation_result valid = check_slab_free(cache, object, ip);
> +
> +	if (valid == KASAN_FREE_IS_IGNORED)
> +		return false;
> +	if (valid == KASAN_FREE_IS_INVALID)
> +		return true;
> +
> +	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))
> +		return false;
> +
>  	kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE),
>  			KASAN_SLAB_FREE, init);
>  
> @@ -242,6 +263,12 @@ static inline bool poison_slab_object(struct kmem_cache *cache, void *object,
>  	return false;
>  }
>  
> +bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object,
> +				unsigned long ip)
> +{
> +	return check_slab_free(cache, object, ip) == KASAN_FREE_IS_INVALID;
> +}
> +
>  bool __kasan_slab_free(struct kmem_cache *cache, void *object,
>  				unsigned long ip, bool init)
>  {
> diff --git a/mm/slub.c b/mm/slub.c
> index 4927edec6a8c..34724704c52d 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2170,6 +2170,13 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init)
>  	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;
> +
>  	/*
>  	 * As memory initialization might be integrated into KASAN,
>  	 * kasan_slab_free and initialization memset's must be
> 



  parent reply	other threads:[~2024-07-25 13:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-24 16:34 [PATCH v2 0/2] allow KASAN to detect UAF in SLAB_TYPESAFE_BY_RCU slabs Jann Horn
2024-07-24 16:34 ` [PATCH v2 1/2] kasan: catch invalid free before SLUB reinitializes the object Jann Horn
2024-07-24 21:17   ` Andrew Morton
2024-07-25 10:54     ` Jann Horn
2024-07-25 13:20   ` Vlastimil Babka [this message]
2024-07-24 16:34 ` [PATCH v2 2/2] slub: Introduce CONFIG_SLUB_RCU_DEBUG Jann Horn
2024-07-25 13:28   ` Vlastimil Babka
2024-07-25 14:34     ` 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=1d6913c7-5a6a-412b-a4fa-0eb7b7622d4f@suse.cz \
    --to=vbabka@suse.cz \
    --cc=42.hyeyoo@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@gmail.com \
    --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=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