From patchwork Fri Aug 9 15:36:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13758894 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 B0B8DC52D7C for ; Fri, 9 Aug 2024 15:37:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 506336B008C; Fri, 9 Aug 2024 11:37:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B4C56B0092; Fri, 9 Aug 2024 11:37:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21C296B0098; Fri, 9 Aug 2024 11:37:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F1A836B008C for ; Fri, 9 Aug 2024 11:37:10 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A872B1A0BCF for ; Fri, 9 Aug 2024 15:37:10 +0000 (UTC) X-FDA: 82433110620.05.9A2BA09 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by imf04.hostedemail.com (Postfix) with ESMTP id A50CB40007 for ; Fri, 9 Aug 2024 15:37:08 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FBV6zh28; spf=pass (imf04.hostedemail.com: domain of jannh@google.com designates 209.85.128.47 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=1723217796; 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=04hH29wKOUh4zO671E3nNC0Fq21jgPwt4FhdP1F+2dI=; b=8LCpe7zI7lCTvcb9n06IL9BgoEcA11Sa6C7S4SIj7eylgVyod7TGumVDq+FFBt0kgLhpQD rExYHH2MfacaoX1nzfyA2SxrAb7LgvqV2MkRy7DVwxlLgA+mPd5q9RXLnTgNFLdnxd5kWr rpSeiYoiC85uKMtIGdnrspaB6UBVbHI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FBV6zh28; spf=pass (imf04.hostedemail.com: domain of jannh@google.com designates 209.85.128.47 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=1723217796; a=rsa-sha256; cv=none; b=ZG4X+/MDnVgK5fpPcISRBT0SNHBJz7PvlZVh932umBq5IWIifbAgooUFlVOGUsEBY98wVr omj3ve1sq/apMNHmFOYDThHKqXSW+qBlvjpS9Ik+V2CDNXLOrPBXyYj3a8sPz5QS1cxebD 1LkCRie8wD1gzmXG2bal0nGEyiEWPJE= Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-427fc9834deso65815e9.0 for ; Fri, 09 Aug 2024 08:37:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723217827; x=1723822627; 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=04hH29wKOUh4zO671E3nNC0Fq21jgPwt4FhdP1F+2dI=; b=FBV6zh284c2ax1IC8IEl7CRhZVKvKr0aN/A7tBaxbq2QzuMd8tu7DFkYS8MAb8SaC6 SZWejJMmX1GKvhkHpLLPOV8DxTuS6XQWAjgcO/YPGOpfVV4IEdXalarCuc0+705jO9oe /JvhKuhlw00QlYNYajqEcjXNSITInsU9j2lMjT1UVgzDPlm7HWQiNvy5Q2a2r+PxBeVJ DtRzpGOVrs6XPg6hR9VNhxP80FMuajdFzxF25os7fhWA+AhIedzb1DvDVg0hrGq3UlMt ud5gNp3COkYH905aeoX0j+4Z6m3gwHCWzNmwvUqeq8JpTwWn/iE+jgOqVIPzt9rtR4Pl 3cMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723217827; x=1723822627; 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=04hH29wKOUh4zO671E3nNC0Fq21jgPwt4FhdP1F+2dI=; b=V7d/V/d8xHQvUyR0PpLB5Oyu5Pfh6dY4Ul7zIbRUKGjsnI3aFgzAqJ8JgVJwbtw2yQ y4WTEuPosM/GxiMN+HlYKK9pQlgGyF51p4MZ7REL88TYjTAXZIi4QfVLmvuq58HCAVs4 eAKe9EfuIJ+C75lakrR/QaSdeIu6RaF/cebfLI/4Q5K3L88oGMttCjhvGAaHZcfoSDbz DYM3ZlQuPsUs2ABkt8rTfJf72Gb2hipXoI088GxakJyjK526Ot3kK7J83te3fVD0CqXg tCE5IIzKIiPovFxNlJpAwg+6PkIcD40p2sRkYg7xvelaOcaDHrYKCqtVLcbPlcObUAdO NcxA== X-Forwarded-Encrypted: i=1; AJvYcCVDJapT4RIFl5yyEpMN9HGAhqiEi4O9qfZEPIJkYtcKZOoEmG6aHzQ75dRsUfi8rjO6riy9zKNSGQ==@kvack.org X-Gm-Message-State: AOJu0YxKMoy1L7bjCzyJ20GPu1tswZTIk+ViGqWZYL/0coB8fV3PiG1j yQSooMdtK6EAA3i5ixjgd6ngAstkaBDeiFeqYW7Bfq/2JhTUFmH0FDqTQpBFMA== X-Google-Smtp-Source: AGHT+IGOr0c0CjkgwTBRF8fGT7noWEp1T+yKhvV1d2VxrR9xYPGziTD7vNkFfcFIeHv6Und2nan4Eg== X-Received: by 2002:a05:600c:3d0e:b0:426:8ee5:3e9c with SMTP id 5b1f17b1804b1-429c170502fmr1680895e9.6.1723217826322; Fri, 09 Aug 2024 08:37:06 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:1cbc:ea05:2b3e:79e6]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42905971d06sm131370035e9.19.2024.08.09.08.37.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 08:37:05 -0700 (PDT) From: Jann Horn Date: Fri, 09 Aug 2024 17:36:55 +0200 Subject: [PATCH v8 1/2] kasan: catch invalid free before SLUB reinitializes the object MIME-Version: 1.0 Message-Id: <20240809-kasan-tsbrcu-v8-1-aef4593f9532@google.com> References: <20240809-kasan-tsbrcu-v8-0-aef4593f9532@google.com> In-Reply-To: <20240809-kasan-tsbrcu-v8-0-aef4593f9532@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, David Sterba , Jann Horn X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1723217820; l=8980; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=6bMIRMeh/1RVCY2STakwRPDtPkfm0ed8OAz2i9tHk/A=; b=cgEyMnkYj8fG04Ew4yST4NXZcj+IbXd0To9wuYcsObX2VYDJHqQIXKwsJaH/bAV6w1Fth+GPk VGb/0LWxPB2Cf5nOFT6Ov7qUiEcbqCHMirvJ0P2oCCgijDCf5jxeXGE X-Developer-Key: i=jann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UKH/L6wNAwfJCs1ylvrzTNXE/ZVC8bYRS1yq5R7BTxkOFbd1pS7Guxn5dGunSmdZpDi/UyrSTME/IK3Y5FXWI cXmuiKBZ KXrqkkrZTUCPTkOlBnqvRHYwQGVfyO7S6cbxd9A9GBOolIk3Px6KLzJtRvjqr4I7t7A0QvLKeV3dnw/xNA0NUvGhjsLJtYfwSynOMvOjfl8/iyaewGu/Gg40TLgNgIJ+t76b8ws2gl4YriQZN7tnLT7A1v3BqeEjc+koOIeQxUfqTX0MvS1yQzQ6ZuWTEmhShIL0+SwV2OmpxiibG+Ufb4wlc919na9TOE2wsGzaCmuqkZWLS3XvnXeb9v0Xn7segpxCQhpYXB7TGTurOUd2+KL0lQnFDqSPH1O1mq4vcvQQ436hP0/sdfcZmB+4gQaIpzUdr9nR30+6IS8XCIflqrT2FqCKrWUOBDBE5WcQZToIwc/vNy2lFN9nDhX6rTLckOVYwaRap8YbHQxI= 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_allocation() - 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) Acked-by: Vlastimil Babka #slub Reviewed-by: Andrey Konovalov Signed-off-by: Jann Horn --- include/linux/kasan.h | 54 ++++++++++++++++++++++++++++++++++++++++++--- mm/kasan/common.c | 61 ++++++++++++++++++++++++++++++--------------------- mm/slub.c | 7 ++++++ 3 files changed, 94 insertions(+), 28 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 70d6a8f6e25d..1570c7191176 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -172,19 +172,61 @@ 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 - Check whether freeing a slab object is safe. + * @object: Object to be freed. + * + * This function checks whether freeing the given object is safe. It may + * check for double-free and invalid-free bugs and report them. + * + * This function is intended only for use by the slab allocator. + * + * @Return true if freeing the object is unsafe; 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 - Poison, initialize, and quarantine a slab object. + * @object: Object to be freed. + * @init: Whether to initialize the object. + * + * This function informs that a slab object has been freed and is not + * supposed to be accessed anymore, except for objects in + * SLAB_TYPESAFE_BY_RCU caches. + * + * For KASAN modes that have integrated memory initialization + * (kasan_has_integrated_init() == true), this function also initializes + * the object's memory. For other modes, the @init argument is ignored. + * + * This function might also take ownership of the object to quarantine it. + * When this happens, KASAN will defer freeing the object to a later + * stage and handle it internally until then. The return value indicates + * whether KASAN took ownership of the object. + * + * This function is intended only for use by the slab allocator. + * + * @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 +410,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..f26bbc087b3b 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 when freeing the object is not safe. */ +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. */ @@ -501,17 +507,22 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) if (check_page_allocation(ptr, ip)) return false; kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false); return true; } - if (is_kfence_address(ptr)) - return false; + if (is_kfence_address(ptr) || !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 3520acaf9afa..0c98b6a2124f 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2223,12 +2223,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,