Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2037660rwb; Sun, 18 Sep 2022 20:50:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5JHEbUw+xO5OscL4wWVONDY7UGz0JriOeCBnG5EZ6JpqUWYmJP9jy/OaWHbW85UqBBLrQg X-Received: by 2002:a17:902:d484:b0:178:1b69:1488 with SMTP id c4-20020a170902d48400b001781b691488mr11159218plg.156.1663559403729; Sun, 18 Sep 2022 20:50:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663559403; cv=none; d=google.com; s=arc-20160816; b=frL8Kxf7QYzN1hCx9kaYASzbtmKDrS/Xo74F5/o37K6MiqAu5GIImanobRPdqDSYeK K45UT4GIg5ND0Aj9MmGWWU+X0ljyU78rLUY3BQkCB0c3TJbZB9x4q4xgxFq76w2sUq+e Xulrbszig1zQKrZrs461iUE1IJgeOwsSUfZcVp0UTgYc+19pMFajfOWYe1KAFojnnl3l 67CbL0eiub9UBqJept1tV0VaGfz2Sg4/iPJP/xM80VwbLC2Q7EJhh6QCWpSqJkP/gg0X M6Oc5Nhj3VaExjihUqzhtAQg1CAm/i/qSNmQSJ5NHs4xkNoOZi9OldQfgqjBvUXkkyzh nDcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=FZjlYl7FscekgfCjfYFDbh04Q1yveWpCRvs3I06iz68=; b=kftWyoMjS6b0cA2a3UzWLRTOcA2j+447K4xGjhWWNDA0W2l7Z3Qn9NjTqBCht9Si7Z JlSnuGZpSClLlZp9SDRJnTCbQARWYBSLbkeqt/EdjpEpwoj397QnckTekocgBcYnm0mv K1fxuvjRyE50qDqbi0X5XT302jCDcczMM0UcGFHPdSl+OxxN1YaEyxEPRjTQ7jIrl6GC P8NNlg05CZbsoNNpigu32apccO+urK7WI7IF8V0Qd+pM7Zmqqt2hEgYhl9DJF+khMoea WJDF2o6mu9KSMlxzpRWwkcqvCkhSAa9nZeL2Z1wGpI9CbqH4SS72rYX0CTjK1ogNr3Pc 2XpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=F0HzyqpD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020a056a0014c600b005381dd35d73si27439832pfu.357.2022.09.18.20.49.51; Sun, 18 Sep 2022 20:50:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=F0HzyqpD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229854AbiISDNF (ORCPT + 99 others); Sun, 18 Sep 2022 23:13:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229826AbiISDNC (ORCPT ); Sun, 18 Sep 2022 23:13:02 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63A4A1A3A1 for ; Sun, 18 Sep 2022 20:13:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663557181; x=1695093181; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=TJneCTmLlrztD6YAx7hnp5eutOdkGZbB+hdFQjt3lUk=; b=F0HzyqpD1eA+wqjZQwp8IBufVoP5mqlogbktqTt0mFqPEfitqYk8eI/y RWOYj8dBmRHNwpbeiJxEBKU5M7aTxIcK8H7yYl3yw8zPrhTN5nu/fs6sm HqJVi8McqnK2lZhdVVDFyRPTZs5If6kPkj3UMSjuSn4+35PkJ4cly2LTc u1afthKKRLKKQXqVWjchV5g/0SiZTN0ssBjITFem69Aeu2WP9uPxTwf7W neEEDOpoxVQY6AKoQeEx8i6znnFRknD2uwTn8yxzgNMzsYEdczFBHVLH5 rOSi/PWylpTKcVAqrdi2fPOQ+LfrwfpxhsQr8ibWu9EcXZ3an5BD53R2w w==; X-IronPort-AV: E=McAfee;i="6500,9779,10474"; a="385583749" X-IronPort-AV: E=Sophos;i="5.93,325,1654585200"; d="scan'208";a="385583749" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2022 20:13:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,325,1654585200"; d="scan'208";a="569477795" Received: from feng-clx.sh.intel.com ([10.238.200.228]) by orsmga003.jf.intel.com with ESMTP; 18 Sep 2022 20:12:57 -0700 From: Feng Tang To: Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Andrew Morton , Waiman Long Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, Feng Tang Subject: [PATCH] mm/slab_common: fix possiable double free of kmem_cache Date: Mon, 19 Sep 2022 11:12:41 +0800 Message-Id: <20220919031241.1358001-1-feng.tang@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When doing slub_debug test, kfence's 'test_memcache_typesafe_by_rcu' kunit test case cause a use-after-free error: BUG: KASAN: use-after-free in kobject_del+0x14/0x30 Read of size 8 at addr ffff888007679090 by task kunit_try_catch/261 CPU: 1 PID: 261 Comm: kunit_try_catch Tainted: G B N 6.0.0-rc5-next-20220916 #17 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 Call Trace: dump_stack_lvl+0x34/0x48 print_address_description.constprop.0+0x87/0x2a5 print_report+0x103/0x1ed kasan_report+0xb7/0x140 kobject_del+0x14/0x30 kmem_cache_destroy+0x130/0x170 test_exit+0x1a/0x30 kunit_try_run_case+0xad/0xc0 kunit_generic_run_threadfn_adapter+0x26/0x50 kthread+0x17b/0x1b0 The cause is inside kmem_cache_destroy(): kmem_cache_destroy acquire lock/mutex shutdown_cache schedule_work(kmem_cache_release) (if RCU flag set) release lock/mutex kmem_cache_release (if RCU flag set) in some certain timing, the scheduled work could be run before the next RCU flag checking which will get a wrong state. Fix it by caching the RCU flag inside protected area, just like 'refcnt' Signed-off-by: Feng Tang --- note: The error only happens on linux-next tree, and not in Linus' tree, which already has Waiman's commit: 0495e337b703 ("mm/slab_common: Deleting kobject in kmem_cache_destroy() without holding slab_mutex/cpu_hotplug_lock") mm/slab_common.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 07b948288f84..ccc02573588f 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -475,6 +475,7 @@ void slab_kmem_cache_release(struct kmem_cache *s) void kmem_cache_destroy(struct kmem_cache *s) { int refcnt; + bool rcu_set; if (unlikely(!s) || !kasan_check_byte(s)) return; @@ -482,6 +483,8 @@ void kmem_cache_destroy(struct kmem_cache *s) cpus_read_lock(); mutex_lock(&slab_mutex); + rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU; + refcnt = --s->refcount; if (refcnt) goto out_unlock; @@ -492,7 +495,7 @@ void kmem_cache_destroy(struct kmem_cache *s) out_unlock: mutex_unlock(&slab_mutex); cpus_read_unlock(); - if (!refcnt && !(s->flags & SLAB_TYPESAFE_BY_RCU)) + if (!refcnt && !rcu_set) kmem_cache_release(s); } EXPORT_SYMBOL(kmem_cache_destroy); -- 2.34.1