From nobody Thu Sep 19 01:21:12 2024 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEC0F198856 for ; Fri, 9 Aug 2024 15:37:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723217831; cv=none; b=OXuhbRYx3e2JVtUU5FwD7DF4vHIbozBmX7kj6K/t5AkPsfMT2v9pUwDqR0yxJc2zpKygo7g5ubmw7c4UKjzxiyO8gdBKzY5BOj8OVd8N0vG7fi81EzyBvfmiPSuTcNyRUIpYM9aUP132S1FVt64a3cWlarWMZyhn+oYlneddQoE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723217831; c=relaxed/simple; bh=6bMIRMeh/1RVCY2STakwRPDtPkfm0ed8OAz2i9tHk/A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TqYdTTh9c3t5Fkd2ksWjuvQHDdrqqQmZM0Fku7G8HwF9HvPMHOP4+d3W+KjlL4vjs5rnSCy72sNRA40TVIDop04MHREFqXHTrjEux/wRRdZHcpIuXTpGL42k5KeVtSHCffi9uPWtLLKtmRLH6GvJVC8bG/bgBB4cYym7IHtR7sE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eJ4iKZIn; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eJ4iKZIn" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-427fc9834deso65795e9.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=vger.kernel.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=eJ4iKZIn/rSyhDmGwpEtOkN+EBUmb+HLMyr1Yaz1KKrRJrDwsM7Syv3aqU4cf9gwUE K463CY2y151O9R94odHiY4mnhE/pRqNpj0N7MHhuJJmDZF25FONkKUI1oG0RSdDCMW4S 7eizJEW9tqNDku5RyZcFzK73kZ3wlXkvaowlPkn9LxTi7V/AXZsW8V9fke7v+Y2+FEqL RtFL+xSSkCfHQlaFwXUSrTT9nTYRmdiWxLC/iYvZCH7IyO5wqr4sBmgsZsTSGZj+xSAF A7zzEYkGkdt3tGGWe1wJYiWHdvPB4/HcyJdKAFhcs4TTfOjiJLQfeZjeTwuL4BcsgH/4 5hZQ== 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=t0zKLjF/DAFbqGOWEldqJfhNOeT5c/Hep+ymcai2/kQQFbDKcVsUCgRCKpsWO2wxC5 J+BEDL6E/Rh8R/Uxr5yznDdUeLHfigudT6XQff/IUEcjGl/LA45lcq53I4TFOVH4WvfY sj0GLSD/f6lUMG543NX+Mrhp2bcrQrVd/y5YY/RrXNNVgeN8U4anxXYl8i/gJpj1Eld7 yIOyHK/IImzzdyM8YN0lXKXaPJWeZZGyQFTmoWbIm4VpJB7JLjubyaz+SPDwY94OYd+1 qkYH3sVE9+C5Zxk6hho8MmXneR6xc5Xgt816JOgEa5nxM1uJlCFFWmMNA8dp+2iNTtLr xr+A== X-Forwarded-Encrypted: i=1; AJvYcCUlaxnFBKj4jIcCboeqI0iCwv9nM+sOVHuIuk7bQi9+9FVTiBla2h00fDUb49PalS/uLytQv98j5Wbi2OY=@vger.kernel.org X-Gm-Message-State: AOJu0YzgMFMZ+Gd6JuwqvRRHd/URybNQb4tc+lYnBLMh/l8Dy6utZH+y GRK8Jx06yND+A0tXN9ln2NGqD+DXdLoTJ3pC1zogrrSNTKrPvYhnH2upDinAxw== 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= 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; } =20 -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() =3D=3D true), this function also initializ= es + * 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; } =20 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 kme= m_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, boo= l 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 =3D set_tag(object, assign_tag(cache, object, true)); =20 return (void *)object; } =20 -static inline bool poison_slab_object(struct kmem_cache *cache, void *obje= ct, - 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 =3D object; =20 - tagged_object =3D object; object =3D kasan_reset_tag(object); =20 if (unlikely(nearest_obj(cache, virt_to_slab(object), object) !=3D object= )) { kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_INVALID_FREE); return true; } =20 - /* 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; } =20 + return false; +} + +static inline void poison_slab_object(struct kmem_cache *cache, void *obje= ct, + bool init) +{ + void *tagged_object =3D object; + + object =3D 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); =20 if (kasan_stack_collection_enabled()) kasan_save_free_info(cache, tagged_object); +} =20 - 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); } =20 -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; =20 - /* - * 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); =20 /* * 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, unsigne= d long ip) if (check_page_allocation(ptr, ip)) return false; kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false); return true; } =20 - if (is_kfence_address(ptr)) - return false; + if (is_kfence_address(ptr) || !kasan_arch_is_ready()) + return true; =20 slab =3D 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; } =20 void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long= ip) { struct slab *slab; gfp_t flags =3D 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); =20 if (kfence_free(x)) return false; =20 + /* + * 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, --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 01:21:12 2024 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38DB81990C6 for ; Fri, 9 Aug 2024 15:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723217833; cv=none; b=oj0nbkWX2bLkPwWgM2W98BG6q4LQjoedT5oNSbcNdA7u/7B5pW/LFQ2GEyOuua1NLnL9vBbi5MPp160Azxlzy5KFUhl3TJJJ5iwlkx7hfaYYjlPUjghRKLHf79qSmukXykopbL47PWwQgdXdkmmYjUj5Jix9lpSzA1o3BTvLRQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723217833; c=relaxed/simple; bh=1PgmzLlEUsMDeT/cjOBHdbwuBEXeoZXwr/+BB6CuICY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aiJFFwlPO60gLb12/rRGg+C4EP/tVbQsCzasrMTvNh+GESGCLz5VLqHKErsmxWezntm1GpZ7ArjDTRX62Q3CitXBMh7hWvWBI9cJE6FaV/1fJC/MDA9ef0KbeyhtmqJbFAiKyKYYVvXb5LtWWm0AF1fCzFGBh6+NyoWGnEXMUaY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bLCv3YMH; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bLCv3YMH" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-428063f4d71so55065e9.1 for ; Fri, 09 Aug 2024 08:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723217829; x=1723822629; darn=vger.kernel.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=Nta7J3JqGYOD5OvM0IZyyB8KAW+gkwbuyLrKSCqLhjY=; b=bLCv3YMHy/dldfmpmNRw1bFvKMH15b6kkdDJe+kLW6lInbm//zBRUrAac6kzKzNNXf t0m9f0sg+DmX4Qj7KtIvV1FeceS/FgRfzBw8mQi3jkikLaYfXzVRHFY0feKf9orVAO6G E9JljR/mioAmHe0P9ZSacWV2zBtnpNkEkxWGtLMQQsufV/8tyvWY00pCW+RfwBhO7V8b ZQOSfX6iNYeYQ8hYFiXC75ZL8yZ8PXhXp3s5/ppUPpo4Te+HenHbnpLKtVRbqXmg/6qs 3F5rj/TUz5oLA7pfEr0zkwoBAw4MMeQntz5ll2GDLzBAt19FaxJ9V26sxJavMUYx8vUH s2Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723217829; x=1723822629; 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=Nta7J3JqGYOD5OvM0IZyyB8KAW+gkwbuyLrKSCqLhjY=; b=DmcNMU7QN9Mv/db3SsKhkJw/gOQeX6E691emsM1rhSMsm48aJRHM6lLxoBekQ21Zin PtaquDF0wf5TL34ytwdcGOYOGd3TCWXMLPOIXFTYSXblPK5+FRUqpp9WOIQbAWpEoVzh YO1X4uKUDK8mW1+P3iy74lqMrgVEp4KERdenumKuTaVYAYmpsIssRT1hhx2B/MHrxWi1 JZVIeJZC8d156SUIuNDJn7HV5jTr9StHRe1Xts80BDY6Y3IQ1Dpoo1oKlrWHRja1B1k5 fx5be24qyijWUJ6hW7txIFHUAnPEZpYHTMw0IQ1zrxVV8GCemdJlso4UdgoKRD4r1SIU jbrA== X-Forwarded-Encrypted: i=1; AJvYcCWS/BdjaIOVnjewUUIyELwM0U1ZfjQbnnSn9daz4eap6vlGp2KgDrSDuq9WVXilw0U+mg6EPT/M1qmAlXHw4mmXhEmiW1VYV0RSdtX6 X-Gm-Message-State: AOJu0YxToDdv9C/dxffI0WWrkijNbi6nAdYZFrSU/818H5BZ/3PZneaP 6Ts/IL5rg4nTM2Xm/Xz2AQmPyKRWxQVybij+PXhHSd47M8sBVO2IWev8ksHDcA== X-Google-Smtp-Source: AGHT+IF5IutVEQNrDt88SK7HPph8YJBmOKR0vOY4CJmzi0ZYJfl2efa9B/WZhUGp7F/ILxDc8oTE5g== X-Received: by 2002:a05:600c:870b:b0:426:6413:b681 with SMTP id 5b1f17b1804b1-429c171589dmr1772365e9.6.1723217827484; Fri, 09 Aug 2024 08:37:07 -0700 (PDT) Received: from localhost ([2a00:79e0:9d:4:1cbc:ea05:2b3e:79e6]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4290c7a35ccsm83541645e9.44.2024.08.09.08.37.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 08:37:06 -0700 (PDT) From: Jann Horn Date: Fri, 09 Aug 2024 17:36:56 +0200 Subject: [PATCH v8 2/2] slub: Introduce CONFIG_SLUB_RCU_DEBUG Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240809-kasan-tsbrcu-v8-2-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 , syzbot+263726e59eab6b442723@syzkaller.appspotmail.com X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1723217820; l=18530; i=jannh@google.com; s=20240730; h=from:subject:message-id; bh=1PgmzLlEUsMDeT/cjOBHdbwuBEXeoZXwr/+BB6CuICY=; b=YklEH9R3eQxtk1j0uXkt6YcAwFnc6faw7kDyZtccMugqnNq4w+3t5jWwZ9wBNanz+Gati/6tt OSwRkmbRsOABRQ4lIdt4uwGF9Pf1IF/I1xve7vQX1YUVwhd/iefsP2a X-Developer-Key: i=jannh@google.com; a=ed25519; pk=AljNtGOzXeF6khBXDJVVvwSEkVDGnnZZYqfWhP1V+C8= Currently, KASAN is unable to catch use-after-free in SLAB_TYPESAFE_BY_RCU slabs because use-after-free is allowed within the RCU grace period by design. Add a SLUB debugging feature which RCU-delays every individual kmem_cache_free() before either actually freeing the object or handing it off to KASAN, and change KASAN to poison freed objects as normal when this option is enabled. For now I've configured Kconfig.debug to default-enable this feature in the KASAN GENERIC and SW_TAGS modes; I'm not enabling it by default in HW_TAGS mode because I'm not sure if it might have unwanted performance degradation effects there. Note that this is mostly useful with KASAN in the quarantine-based GENERIC mode; SLAB_TYPESAFE_BY_RCU slabs are basically always also slabs with a ->ctor, and KASAN's assign_tag() currently has to assign fixed tags for those, reducing the effectiveness of SW_TAGS/HW_TAGS mode. (A possible future extension of this work would be to also let SLUB call the ->ctor() on every allocation instead of only when the slab page is allocated; then tag-based modes would be able to assign new tags on every reallocation.) Tested-by: syzbot+263726e59eab6b442723@syzkaller.appspotmail.com Reviewed-by: Andrey Konovalov Acked-by: Marco Elver Acked-by: Vlastimil Babka (slab) Signed-off-by: Jann Horn --- include/linux/kasan.h | 17 +++++++---- mm/Kconfig.debug | 32 +++++++++++++++++++++ mm/kasan/common.c | 11 +++---- mm/kasan/kasan_test.c | 46 ++++++++++++++++++++++++++++++ mm/slab_common.c | 16 +++++++++++ mm/slub.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--= ---- 6 files changed, 182 insertions(+), 19 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 1570c7191176..00a3bf7c0d8f 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -193,40 +193,44 @@ static __always_inline bool kasan_slab_pre_free(struc= t kmem_cache *s, { if (kasan_enabled()) return __kasan_slab_pre_free(s, object, _RET_IP_); return false; } =20 -bool __kasan_slab_free(struct kmem_cache *s, void *object, bool init); +bool __kasan_slab_free(struct kmem_cache *s, void *object, bool init, + bool still_accessible); /** * kasan_slab_free - Poison, initialize, and quarantine a slab object. * @object: Object to be freed. * @init: Whether to initialize the object. + * @still_accessible: Whether the object contents are still accessible. * * 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. + * supposed to be accessed anymore, except when @still_accessible is set + * (indicating that the object is in a SLAB_TYPESAFE_BY_RCU cache and an R= CU + * grace period might not have passed yet). * * For KASAN modes that have integrated memory initialization * (kasan_has_integrated_init() =3D=3D true), this function also initializ= es * 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) + void *object, bool init, + bool still_accessible) { if (kasan_enabled()) - return __kasan_slab_free(s, object, init); + return __kasan_slab_free(s, object, init, still_accessible); return false; } =20 void __kasan_kfree_large(void *ptr, unsigned long ip); static __always_inline void kasan_kfree_large(void *ptr) { @@ -416,13 +420,14 @@ static inline void *kasan_init_slab_obj(struct kmem_c= ache *cache, =20 static inline bool kasan_slab_pre_free(struct kmem_cache *s, void *object) { return false; } =20 -static inline bool kasan_slab_free(struct kmem_cache *s, void *object, boo= l init) +static inline bool kasan_slab_free(struct kmem_cache *s, void *object, + bool init, bool still_accessible) { return false; } static inline void kasan_kfree_large(void *ptr) {} static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags, bool init) diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index afc72fde0f03..41a58536531d 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -67,12 +67,44 @@ config SLUB_DEBUG_ON equivalent to specifying the "slab_debug" parameter on boot. There is no support for more fine grained debug control like possible with slab_debug=3Dxxx. SLUB debugging may be switched off in a kernel built with CONFIG_SLUB_DEBUG_ON by specifying "slab_debug=3D-". =20 +config SLUB_RCU_DEBUG + bool "Enable UAF detection in TYPESAFE_BY_RCU caches (for KASAN)" + depends on SLUB_DEBUG + # SLUB_RCU_DEBUG should build fine without KASAN, but is currently useless + # without KASAN, so mark it as a dependency of KASAN for now. + depends on KASAN + default KASAN_GENERIC || KASAN_SW_TAGS + help + Make SLAB_TYPESAFE_BY_RCU caches behave approximately as if the cache + was not marked as SLAB_TYPESAFE_BY_RCU and every caller used + kfree_rcu() instead. + + This is intended for use in combination with KASAN, to enable KASAN to + detect use-after-free accesses in such caches. + (KFENCE is able to do that independent of this flag.) + + This might degrade performance. + Unfortunately this also prevents a very specific bug pattern from + triggering (insufficient checks against an object being recycled + within the RCU grace period); so this option can be turned off even on + KASAN builds, in case you want to test for such a bug. + + If you're using this for testing bugs / fuzzing and care about + catching all the bugs WAY more than performance, you might want to + also turn on CONFIG_RCU_STRICT_GRACE_PERIOD. + + WARNING: + This is designed as a debugging feature, not a security feature. + Objects are sometimes recycled without RCU delay under memory pressure. + + If unsure, say N. + config PAGE_OWNER bool "Track page owner" depends on DEBUG_KERNEL && STACKTRACE_SUPPORT select DEBUG_FS select STACKTRACE select STACKDEPOT diff --git a/mm/kasan/common.c b/mm/kasan/common.c index f26bbc087b3b..ed4873e18c75 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -227,43 +227,44 @@ static bool check_slab_allocation(struct kmem_cache *= cache, void *object, } =20 return false; } =20 static inline void poison_slab_object(struct kmem_cache *cache, void *obje= ct, - bool init) + bool init, bool still_accessible) { void *tagged_object =3D object; =20 object =3D kasan_reset_tag(object); =20 /* RCU slabs could be legally used after free within the RCU period. */ - if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) + if (unlikely(still_accessible)) return; =20 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), KASAN_SLAB_FREE, init); =20 if (kasan_stack_collection_enabled()) kasan_save_free_info(cache, tagged_object); } =20 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); } =20 -bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init) +bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init, + bool still_accessible) { if (!kasan_arch_is_ready() || is_kfence_address(object)) return false; =20 - poison_slab_object(cache, object, init); + poison_slab_object(cache, object, init, still_accessible); =20 /* * 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. */ @@ -515,13 +516,13 @@ bool __kasan_mempool_poison_object(void *ptr, unsigne= d long ip) =20 slab =3D folio_slab(folio); =20 if (check_slab_allocation(slab->slab_cache, ptr, ip)) return false; =20 - poison_slab_object(slab->slab_cache, ptr, false); + poison_slab_object(slab->slab_cache, ptr, false, false); return true; } =20 void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long= ip) { struct slab *slab; diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 7b32be2a3cf0..567d33b493e2 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -993,12 +993,57 @@ static void kmem_cache_invalid_free(struct kunit *tes= t) */ kmem_cache_free(cache, p); =20 kmem_cache_destroy(cache); } =20 +static void kmem_cache_rcu_uaf(struct kunit *test) +{ + char *p; + size_t size =3D 200; + struct kmem_cache *cache; + + KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB_RCU_DEBUG); + + cache =3D kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU, + NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cache); + + p =3D kmem_cache_alloc(cache, GFP_KERNEL); + if (!p) { + kunit_err(test, "Allocation failed: %s\n", __func__); + kmem_cache_destroy(cache); + return; + } + *p =3D 1; + + rcu_read_lock(); + + /* Free the object - this will internally schedule an RCU callback. */ + kmem_cache_free(cache, p); + + /* + * We should still be allowed to access the object at this point because + * the cache is SLAB_TYPESAFE_BY_RCU and we've been in an RCU read-side + * critical section since before the kmem_cache_free(). + */ + READ_ONCE(*p); + + rcu_read_unlock(); + + /* + * Wait for the RCU callback to execute; after this, the object should + * have actually been freed from KASAN's perspective. + */ + rcu_barrier(); + + KUNIT_EXPECT_KASAN_FAIL(test, READ_ONCE(*p)); + + kmem_cache_destroy(cache); +} + static void empty_cache_ctor(void *object) { } =20 static void kmem_cache_double_destroy(struct kunit *test) { struct kmem_cache *cache; =20 @@ -1934,12 +1979,13 @@ static struct kunit_case kasan_kunit_test_cases[] = =3D { KUNIT_CASE(workqueue_uaf), KUNIT_CASE(kfree_via_page), KUNIT_CASE(kfree_via_phys), KUNIT_CASE(kmem_cache_oob), KUNIT_CASE(kmem_cache_double_free), KUNIT_CASE(kmem_cache_invalid_free), + KUNIT_CASE(kmem_cache_rcu_uaf), KUNIT_CASE(kmem_cache_double_destroy), KUNIT_CASE(kmem_cache_accounted), KUNIT_CASE(kmem_cache_bulk), KUNIT_CASE(mempool_kmalloc_oob_right), KUNIT_CASE(mempool_kmalloc_large_oob_right), KUNIT_CASE(mempool_slab_oob_right), diff --git a/mm/slab_common.c b/mm/slab_common.c index 40b582a014b8..9025e85c6750 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -573,12 +573,28 @@ void kmem_cache_destroy(struct kmem_cache *s) int err =3D -EBUSY; bool rcu_set; =20 if (unlikely(!s) || !kasan_check_byte(s)) return; =20 + if (IS_ENABLED(CONFIG_SLUB_RCU_DEBUG) && + (s->flags & SLAB_TYPESAFE_BY_RCU)) { + /* + * Under CONFIG_SLUB_RCU_DEBUG, when objects in a + * SLAB_TYPESAFE_BY_RCU slab are freed, SLUB will internally + * defer their freeing with call_rcu(). + * Wait for such call_rcu() invocations here before actually + * destroying the cache. + * + * It doesn't matter that we haven't looked at the slab refcount + * yet - slabs with SLAB_TYPESAFE_BY_RCU can't be merged, so + * the refcount should be 1 here. + */ + rcu_barrier(); + } + cpus_read_lock(); mutex_lock(&slab_mutex); =20 rcu_set =3D s->flags & SLAB_TYPESAFE_BY_RCU; =20 s->refcount--; diff --git a/mm/slub.c b/mm/slub.c index 0c98b6a2124f..86ab9477a1ae 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2197,45 +2197,81 @@ static inline bool memcg_slab_post_alloc_hook(struc= t kmem_cache *s, static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab = *slab, void **p, int objects) { } #endif /* CONFIG_MEMCG */ =20 +#ifdef CONFIG_SLUB_RCU_DEBUG +static void slab_free_after_rcu_debug(struct rcu_head *rcu_head); + +struct rcu_delayed_free { + struct rcu_head head; + void *object; +}; +#endif + /* * Hooks for other subsystems that check memory allocations. In a typical * production configuration these hooks all should produce no code at all. * * Returns true if freeing of the object can proceed, false if its reuse - * was delayed by KASAN quarantine, or it was returned to KFENCE. + * was delayed by CONFIG_SLUB_RCU_DEBUG or KASAN quarantine, or it was ret= urned + * to KFENCE. */ static __always_inline -bool slab_free_hook(struct kmem_cache *s, void *x, bool init) +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 =3D (s->flags & SLAB_TYPESAFE_BY_RCU) && !after_rcu= _delay; + kmemleak_free_recursive(x, s->flags); kmsan_slab_free(s, x); =20 debug_check_no_locks_freed(x, s->object_size); =20 if (!(s->flags & SLAB_DEBUG_OBJECTS)) debug_check_no_obj_freed(x, s->object_size); =20 /* Use KCSAN to help debug racy use-after-free. */ - if (!(s->flags & SLAB_TYPESAFE_BY_RCU)) + if (!still_accessible) __kcsan_check_access(x, s->object_size, KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT); =20 if (kfence_free(x)) return false; =20 /* * Give KASAN a chance to notice an invalid free operation before we * modify the object. */ if (kasan_slab_pre_free(s, x)) return false; =20 +#ifdef CONFIG_SLUB_RCU_DEBUG + if (still_accessible) { + struct rcu_delayed_free *delayed_free; + + delayed_free =3D kmalloc(sizeof(*delayed_free), GFP_NOWAIT); + if (delayed_free) { + /* + * Let KASAN track our call stack as a "related work + * creation", just like if the object had been freed + * normally via kfree_rcu(). + * We have to do this manually because the rcu_head is + * not located inside the object. + */ + kasan_record_aux_stack_noalloc(x); + + delayed_free->object =3D x; + call_rcu(&delayed_free->head, slab_free_after_rcu_debug); + return false; + } + } +#endif /* CONFIG_SLUB_RCU_DEBUG */ + /* * 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, @@ -2253,42 +2289,42 @@ bool slab_free_hook(struct kmem_cache *s, void *x, = bool init) memset(kasan_reset_tag(x), 0, s->object_size); rsize =3D (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0; memset((char *)kasan_reset_tag(x) + inuse, 0, s->size - inuse - rsize); } /* KASAN might put x into memory quarantine, delaying its reuse. */ - return !kasan_slab_free(s, x, init); + return !kasan_slab_free(s, x, init, still_accessible); } =20 static __fastpath_inline bool slab_free_freelist_hook(struct kmem_cache *s, void **head, void **tai= l, int *cnt) { =20 void *object; void *next =3D *head; void *old_tail =3D *tail; bool init; =20 if (is_kfence_address(next)) { - slab_free_hook(s, next, false); + slab_free_hook(s, next, false, false); return false; } =20 /* Head and tail of the reconstructed freelist */ *head =3D NULL; *tail =3D NULL; =20 init =3D slab_want_init_on_free(s); =20 do { object =3D next; next =3D get_freepointer(s, object); =20 /* If object's reuse doesn't have to be delayed */ - if (likely(slab_free_hook(s, object, init))) { + if (likely(slab_free_hook(s, object, init, false))) { /* Move object to the new freelist */ set_freepointer(s, object, *head); *head =3D object; if (!*tail) *tail =3D object; } else { @@ -4474,40 +4510,67 @@ static __fastpath_inline void slab_free(struct kmem_cache *s, struct slab *slab, void *object, unsigned long addr) { memcg_slab_free_hook(s, slab, &object, 1); alloc_tagging_slab_free_hook(s, slab, &object, 1); =20 - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), false))) do_slab_free(s, slab, object, object, 1, addr); } =20 #ifdef CONFIG_MEMCG /* Do not inline the rare memcg charging failed path into the allocation p= ath */ static noinline void memcg_alloc_abort_single(struct kmem_cache *s, void *object) { - if (likely(slab_free_hook(s, object, slab_want_init_on_free(s)))) + if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), false))) do_slab_free(s, virt_to_slab(object), object, object, 1, _RET_IP_); } #endif =20 static __fastpath_inline void slab_free_bulk(struct kmem_cache *s, struct slab *slab, void *head, void *tail, void **p, int cnt, unsigned long addr) { memcg_slab_free_hook(s, slab, p, cnt); alloc_tagging_slab_free_hook(s, slab, p, cnt); /* * With KASAN enabled slab_free_freelist_hook modifies the freelist * to remove objects, whose reuse must be delayed. */ if (likely(slab_free_freelist_hook(s, &head, &tail, &cnt))) do_slab_free(s, slab, head, tail, cnt, addr); } =20 +#ifdef CONFIG_SLUB_RCU_DEBUG +static void slab_free_after_rcu_debug(struct rcu_head *rcu_head) +{ + struct rcu_delayed_free *delayed_free =3D + container_of(rcu_head, struct rcu_delayed_free, head); + void *object =3D delayed_free->object; + struct slab *slab =3D virt_to_slab(object); + struct kmem_cache *s; + + kfree(delayed_free); + + if (WARN_ON(is_kfence_address(object))) + return; + + /* find the object and the cache again */ + if (WARN_ON(!slab)) + return; + s =3D slab->slab_cache; + if (WARN_ON(!(s->flags & SLAB_TYPESAFE_BY_RCU))) + return; + + /* resume freeing */ + if (slab_free_hook(s, object, slab_want_init_on_free(s), true)) + do_slab_free(s, slab, object, object, 1, _THIS_IP_); +} +#endif /* CONFIG_SLUB_RCU_DEBUG */ + #ifdef CONFIG_KASAN_GENERIC void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr) { do_slab_free(cache, virt_to_slab(x), x, x, 1, addr); } #endif --=20 2.46.0.76.ge559c4bf1a-goog