From patchwork Mon Jul 29 18:56:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13745591 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF1A1C3DA4A for ; Mon, 29 Jul 2024 18:56:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E9F96B0092; Mon, 29 Jul 2024 14:56:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 522B76B0093; Mon, 29 Jul 2024 14:56:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D9D26B0095; Mon, 29 Jul 2024 14:56:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0FE146B0092 for ; Mon, 29 Jul 2024 14:56:28 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AEB6F1A05B9 for ; Mon, 29 Jul 2024 18:56:27 +0000 (UTC) X-FDA: 82393696014.22.A2AF5CB Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) by imf01.hostedemail.com (Postfix) with ESMTP id AF3D74000F for ; Mon, 29 Jul 2024 18:56:25 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BWZFwFl3; spf=pass (imf01.hostedemail.com: domain of jannh@google.com designates 209.85.208.46 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722279343; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VCoOCGeg30WnFijIytAVeEgtIkuQktWy1Oz+PoUgncg=; b=eS8/C9eNHnXXpbI7Np1yE/15UOBVT5LnYqRMC9Lcflx5/VGiLP8BoFrf5fluD3ZIQ7silJ AnNkrAhYd5bHPVrOjeGNcme5rE84kjXHbllF6IK7FENfvIsI5VVPFtW9Nxq3P0Nu/GFlIb UfqTEEZ/FHjLAA5FT/3kgCFxxy/GZsk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BWZFwFl3; spf=pass (imf01.hostedemail.com: domain of jannh@google.com designates 209.85.208.46 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722279343; a=rsa-sha256; cv=none; b=2EO8zDuomYSTcxVZFN0y5xviYZpNHOj+b18My8LD0vdBeb8JoPbMsmYtkrXbxXAzuatbtv 1Gkz5+iriSiogzyWi7SxNLgRERSeK1jBok+8wmxrVC3+06RqgEGToq3tI4i5uSc+78szPe +fqTTdAWhz3x3cdb4x8hZDak0DIlzsQ= Received: by mail-ed1-f46.google.com with SMTP id 4fb4d7f45d1cf-5a1b073d7cdso3056a12.0 for ; Mon, 29 Jul 2024 11:56:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722279384; x=1722884184; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=VCoOCGeg30WnFijIytAVeEgtIkuQktWy1Oz+PoUgncg=; b=BWZFwFl33M9grKDraJOZrp2z935G5AQVYLTI5rVqUjTiJRXQlPNMSnm4IbUkHCn1lN jcExIIZ+SRQ+SA9scvi4ga0mTl5cLC/8zTVdMU8D8lcRqyLab0xvfcaFJZFo6MzX4Tiv kgw/z3+hjIzkiUCU4wJkwarGLH1f6Qe5iXMBs+mT3sVll3cazr/tWSsC5kMT/0RmmL3L 6+XNQBydN/Rg+nMyLnoezpoos/3gwxpNj12dbdK98tstbMOIauuva0UeLm3QTUWe3pDp f3YT0YY5ChiI4x/+vEHCX94xG7JyDlDBV9o2LFQcee6oaOrG7IRm+6kZAeo48wgZddUu QVmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722279384; x=1722884184; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VCoOCGeg30WnFijIytAVeEgtIkuQktWy1Oz+PoUgncg=; b=SP+fPnfteXJVLcs6QjvSnAJpY217FOBeyrThj1vPE75WcGI9Qw5PXUcROuDlklmVuJ Bth0uBtdb+fX55SbwZDfZueVqpnOphemISr90um/uHD3Z/WyLWMGSFx1/L7ts3/gkzW9 36xUgnQdJvt3XXSfnMJECCEsjFrRcdYXVdqD1HKJD1Q+WETXKv2uzBs3L1WtXBv79ARv SgJ3BFlaQ1JmafYGoK9B5VblWsyAWM4S0DsS9E9YmVWwXTgJGqLkOJQHYhWliVkiJ2rs 3w/A5odDIgVoG6TyhlQSyykRB5TAvF1W2AnpC3qbuUtfdNoCzNyoVpIz8V8XNEUC0/jt ssLw== X-Forwarded-Encrypted: i=1; AJvYcCULbMJpAL2N6pqT4hPaufeDbH0EXrm84rAhoFsT4B8NTyULoC/8bZiAaiHxoBPYTQnRp5CKnGbV6ENsdQ0/NlH/HJ8= X-Gm-Message-State: AOJu0Yz11BGjs0bkSnG6++Y0qxFJDbrJoGRrjsai3uZCiDckux/1tZzx Uuh6Hi6Jg0NQqHabDAUCVz285SnxncSJ2Jv0LS7QSmu80KfgRkXOqMT67+R0rA== X-Google-Smtp-Source: AGHT+IG+onbItmvIUtDfdEdb4uATJ1weCR4udl5xW61u6T9tSrynBlWJJKir22zMeH/EyVqa3RcnYQ== X-Received: by 2002:a05:6402:4314:b0:58b:93:b624 with SMTP id 4fb4d7f45d1cf-5b40b12a598mr84009a12.1.1722279383641; Mon, 29 Jul 2024 11:56:23 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:a1f4:32c9:4fcd:ec6c]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4281a936944sm60963535e9.31.2024.07.29.11.56.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 11:56:23 -0700 (PDT) From: Jann Horn Date: Mon, 29 Jul 2024 20:56:11 +0200 Subject: [PATCH v4 1/2] kasan: catch invalid free before SLUB reinitializes the object MIME-Version: 1.0 Message-Id: <20240729-kasan-tsbrcu-v4-1-57ec85ef80c6@google.com> References: <20240729-kasan-tsbrcu-v4-0-57ec85ef80c6@google.com> In-Reply-To: <20240729-kasan-tsbrcu-v4-0-57ec85ef80c6@google.com> To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Marco Elver , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Jann Horn X-Mailer: b4 0.15-dev X-Stat-Signature: mzq4a4tgdtb7jmfnw8sz4z3zrfy17wro X-Rspam-User: X-Rspamd-Queue-Id: AF3D74000F X-Rspamd-Server: rspam02 X-HE-Tag: 1722279385-89027 X-HE-Meta: U2FsdGVkX1+567UltGxLxhyrUBVmhr9Apjjho9HMqOEyd2irO8qZWACcwefgaJJDBPKc/QzNgnnnX9btwaoBZPx/q5/l2sJjgmWTZQzZVqsq7w/E4/kxAOJ1+QfxI7eE7xmD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Kv6y7NVL5EhA473ipCUY2QhyBujpCE42EOOa99Z0uRFiBhYxJvKuOyKgAUYzpa5HGNB31ToYS51Jg4GX+//53Uze4lnFsqBpiv5AreIE43YBLcPEu8N52uGOR9gmkWXoLk+U6E6qqQ8jSz19bbIUadR+wCXCxQg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 RCU-delay the actual SLUB freeing of an object, and we'd like KASAN to still validate synchronously that freeing the object is permitted. (Otherwise this 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. Inside KASAN, this: - moves checks from poison_slab_object() into check_slab_free() - moves kasan_arch_is_ready() up into callers of poison_slab_object() - removes "ip" argument of poison_slab_object() and __kasan_slab_free() (since those functions no longer do any reporting) - renames check_slab_free() to check_slab_allocation() Acked-by: Vlastimil Babka #slub Signed-off-by: Jann Horn --- include/linux/kasan.h | 43 ++++++++++++++++++++++++++++++++++--- mm/kasan/common.c | 59 +++++++++++++++++++++++++++++++-------------------- mm/slub.c | 7 ++++++ 3 files changed, 83 insertions(+), 26 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 70d6a8f6e25d..34cb7a25aacb 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -172,19 +172,50 @@ static __always_inline void * __must_check kasan_init_slab_obj( { if (kasan_enabled()) return __kasan_init_slab_obj(cache, object); return (void *)object; } -bool __kasan_slab_free(struct kmem_cache *s, void *object, - unsigned long ip, bool init); +bool __kasan_slab_pre_free(struct kmem_cache *s, void *object, + unsigned long ip); +/** + * kasan_slab_pre_free - Validate a slab object freeing request. + * @object: Object to free. + * + * This function checks whether freeing the given object might be permitted; it + * checks things like whether the given object is properly aligned and not + * already freed. + * + * This function is only intended for use by the slab allocator. + * + * @Return true if freeing the object is known to be invalid; false otherwise. + */ +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, bool init); +/** + * kasan_slab_free - Possibly handle slab object freeing. + * @object: Object to free. + * + * 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) { if (kasan_enabled()) - return __kasan_slab_free(s, object, _RET_IP_, init); + return __kasan_slab_free(s, object, init); return false; } void __kasan_kfree_large(void *ptr, unsigned long ip); static __always_inline void kasan_kfree_large(void *ptr) { @@ -368,12 +399,18 @@ static inline void kasan_poison_new_object(struct kmem_cache *cache, void *object) {} static inline void *kasan_init_slab_obj(struct kmem_cache *cache, const void *object) { return (void *)object; } + +static inline bool kasan_slab_pre_free(struct kmem_cache *s, void *object) +{ + return false; +} + static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init) { return false; } static inline void kasan_kfree_large(void *ptr) {} static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 85e7c6b4575c..8cede1ce00e1 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -205,59 +205,65 @@ void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */ object = set_tag(object, assign_tag(cache, object, true)); return (void *)object; } -static inline bool poison_slab_object(struct kmem_cache *cache, void *object, - unsigned long ip, bool init) +/* returns true for invalid request */ +static bool check_slab_allocation(struct kmem_cache *cache, void *object, + unsigned long ip) { - void *tagged_object; - - if (!kasan_arch_is_ready()) - return false; + void *tagged_object = object; - 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; } - /* 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 false; +} + +static inline void poison_slab_object(struct kmem_cache *cache, void *object, + bool init) +{ + 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)) + return; + kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), KASAN_SLAB_FREE, init); if (kasan_stack_collection_enabled()) kasan_save_free_info(cache, tagged_object); +} - return false; +bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object, + unsigned long ip) +{ + if (!kasan_arch_is_ready() || is_kfence_address(object)) + return false; + return check_slab_allocation(cache, object, ip); } -bool __kasan_slab_free(struct kmem_cache *cache, void *object, - unsigned long ip, bool init) +bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init) { - if (is_kfence_address(object)) + if (!kasan_arch_is_ready() || is_kfence_address(object)) return false; - /* - * If the object is buggy, do not let slab put the object onto the - * freelist. The object will thus never be allocated again and its - * metadata will never get released. - */ - if (poison_slab_object(cache, object, ip, init)) - return true; + poison_slab_object(cache, object, init); /* * If the object is put into quarantine, do not let slab put the object * onto the freelist for now. The object's metadata is kept until the * object gets evicted from quarantine. */ @@ -503,15 +509,22 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false); return true; } if (is_kfence_address(ptr)) return false; + if (!kasan_arch_is_ready()) + return true; slab = folio_slab(folio); - return !poison_slab_object(slab->slab_cache, ptr, ip, false); + + if (check_slab_allocation(slab->slab_cache, ptr, ip)) + return false; + + poison_slab_object(slab->slab_cache, ptr, false); + return true; } void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip) { struct slab *slab; gfp_t flags = 0; /* Might be executing under a lock. */ diff --git a/mm/slub.c b/mm/slub.c index 4927edec6a8c..34724704c52d 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2167,12 +2167,19 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init) __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; + /* * As memory initialization might be integrated into KASAN, * kasan_slab_free and initialization memset's must be * kept together to avoid discrepancies in behavior. * * The initialization memset's clear the object and the metadata,