From patchwork Mon Oct 18 22:16:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 12568199 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC29CC4332F for ; Mon, 18 Oct 2021 22:16:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 85FEF6115A for ; Mon, 18 Oct 2021 22:16:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 85FEF6115A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 251F794000F; Mon, 18 Oct 2021 18:16:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D995940007; Mon, 18 Oct 2021 18:16:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07AC294000F; Mon, 18 Oct 2021 18:16:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0137.hostedemail.com [216.40.44.137]) by kanga.kvack.org (Postfix) with ESMTP id E576A940007 for ; Mon, 18 Oct 2021 18:16:04 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id AF2101807DC6D for ; Mon, 18 Oct 2021 22:16:04 +0000 (UTC) X-FDA: 78710967048.29.EFA3E82 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf24.hostedemail.com (Postfix) with ESMTP id 3DB48B0000A1 for ; Mon, 18 Oct 2021 22:16:02 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 21C896112D; Mon, 18 Oct 2021 22:16:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1634595363; bh=rQIaCPmTfVKa5eoipEl2gYNrJeNe//O9B9GN311CNKk=; h=Date:From:To:Subject:In-Reply-To:From; b=ZiL/jubMikQD70xUHl2sr3AAEKNK/3jfiWi8mQ5SAx43j94JIcd5F72it1JHyszm7 fb02+3P+F5HQ8OWA+Yq1AKHKIjEzILw2Bqi0N+qUy3AjShIQjBokBeojWznKvHJDBL lSCynKHj5Kse7o2+3oB9FGLkxeWWuq+Bv5SPHB9Q= Date: Mon, 18 Oct 2021 15:16:02 -0700 From: Andrew Morton To: akpm@linux-foundation.org, andreyknvl@gmail.com, bharata@linux.ibm.com, cl@linux.com, faiyazm@codeaurora.org, gregkh@linuxfoundation.org, guro@fb.com, iamjoonsoo.kim@lge.com, keescook@chromium.org, linmiaohe@huawei.com, linux-mm@kvack.org, mm-commits@vger.kernel.org, penberg@kernel.org, rientjes@google.com, ryabinin.a.a@gmail.com, stable@vger.kernel.org, torvalds@linux-foundation.org, vbabka@suse.cz Subject: [patch 13/19] mm, slub: fix potential use-after-free in slab_debugfs_fops Message-ID: <20211018221602.L12GIMrUl%akpm@linux-foundation.org> In-Reply-To: <20211018151438.f2246e2656c041b6753a8bdd@linux-foundation.org> User-Agent: s-nail v14.8.16 X-Stat-Signature: xiecrjdqmjtbmg3fkz93dugt3qawcdws Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="ZiL/jubM"; spf=pass (imf24.hostedemail.com: domain of akpm@linux-foundation.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3DB48B0000A1 X-HE-Tag: 1634595362-421387 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: From: Miaohe Lin Subject: mm, slub: fix potential use-after-free in slab_debugfs_fops When sysfs_slab_add failed, we shouldn't call debugfs_slab_add() for s because s will be freed soon. And slab_debugfs_fops will use s later leading to a use-after-free. Link: https://lkml.kernel.org/r/20210916123920.48704-5-linmiaohe@huawei.com Fixes: 64dd68497be7 ("mm: slub: move sysfs slab alloc/free interfaces to debugfs") Signed-off-by: Miaohe Lin Reviewed-by: Vlastimil Babka Cc: Andrey Konovalov Cc: Andrey Ryabinin Cc: Bharata B Rao Cc: Christoph Lameter Cc: David Rientjes Cc: Faiyaz Mohammed Cc: Greg Kroah-Hartman Cc: Joonsoo Kim Cc: Kees Cook Cc: Pekka Enberg Cc: Roman Gushchin Cc: Signed-off-by: Andrew Morton --- mm/slub.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/mm/slub.c~mm-slub-fix-potential-use-after-free-in-slab_debugfs_fops +++ a/mm/slub.c @@ -4887,13 +4887,15 @@ int __kmem_cache_create(struct kmem_cach return 0; err = sysfs_slab_add(s); - if (err) + if (err) { __kmem_cache_release(s); + return err; + } if (s->flags & SLAB_STORE_USER) debugfs_slab_add(s); - return err; + return 0; } void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)