Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3078387rwb; Mon, 15 Aug 2022 17:35:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR6bVCCSSxHQJQ2Tu8mmbcxeJ3kjTLgLVkDGJRo4Uvn/jGM/Jv4vbRDB1dFD9/bllMSU3/4/ X-Received: by 2002:a05:6a00:4304:b0:532:3c0f:3597 with SMTP id cb4-20020a056a00430400b005323c0f3597mr18720289pfb.44.1660610128525; Mon, 15 Aug 2022 17:35:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660610128; cv=none; d=google.com; s=arc-20160816; b=Baueeu4Or21fdgKliYPARVSPwww+iwvHPzXGKXF4ueZS4/klBn42cKaeceac0qdAcd EWxEX7Jk3FIOm/d+xZ7l3iI3WK/q0KVBhy2nMoTCwEHAwrkTd+nw4soY5w/o+Lg5n/Gp u3Z+PqeS6oF78dDUlU/KdpAAbHSoXFYMvqb1otINr0UAKzZhqS0M+BZCf+qF3oBygQB6 SuOTGD3MvJUuwpG1vFA8EDDG2oAZx78d7YFP8Sv4cTnfrEQ26IEvNbUM3DfSQrB5W87j 20QUIwt3gPhvj/bDWFkvBPTbX7mqE8P96OhTcHFJEpzVITrCDEkwZT3n/Nanza21nhxY v4uw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=f75q9GZ4B+xAUCB0QBom12cAZG5xrqO8PEuqAUDch3E=; b=F0RXGbYBvwVH0tpM+h31VwoXqfq0/i8ujaV0tSaR807dx/DVrk0dkkM2SyrFayH6wf JDRdQMiPoA8x0uR0IbC1io6Qxv8t/4b695RsM3FcLzIiNs1iuzV0wHa6jfDrlbx4XNQc RdrzRgYBP1MCy3h0Bs7zxESFpQ91Xe62lqx3YdiUjr3LAqUjiwQU8wf46MvBytKpI/UY OBcgmQCRCxBo5GVi65X30AuYnSurLF8YEQHNFIaZI3R8MClBqfff3+RzUDV9G9LlrShx jQWiHg3UHVh4BwZOxBr6PGX18ax6vSHmku8q1wgLRNyYy7DN3TN12obnAdyhz+Xc9XWS E7ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Qupxr4FQ; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bd33-20020a056a0027a100b00534b6674cb4si7064315pfb.301.2022.08.15.17.35.16; Mon, 15 Aug 2022 17:35:28 -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=@linuxfoundation.org header.s=korg header.b=Qupxr4FQ; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348553AbiHOVyz (ORCPT + 99 others); Mon, 15 Aug 2022 17:54:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350586AbiHOVvm (ORCPT ); Mon, 15 Aug 2022 17:51:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C53FD106FB3; Mon, 15 Aug 2022 12:33:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E14C960FB9; Mon, 15 Aug 2022 19:33:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B440C433D6; Mon, 15 Aug 2022 19:33:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591989; bh=8O+aw44hiz3gQ+R3zmwagrKbzLPzdYibj+A58m4ChMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qupxr4FQ59hXqUMVt4lNhtKeBGAVS8TtDm3MFPKVeNOrD7muqwhvjGVXe+how6/vT EPYf1N2mvc+d2bw/xCzVX16mpFZNFIuZ2Nl0mXLE4wOszALX7BnuCjl99NCSOWzu16 lNVAy3pbfw7uioespEoAaY5z1hKaZC6Y/XTaLkmA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yushan Zhou , Sasha Levin Subject: [PATCH 5.18 0714/1095] kernfs: fix potential NULL dereference in __kernfs_remove Date: Mon, 15 Aug 2022 20:01:53 +0200 Message-Id: <20220815180458.935428494@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Yushan Zhou [ Upstream commit 72b5d5aef246a0387cefa23121dd90901c7a691a ] When lockdep is enabled, lockdep_assert_held_write would cause potential NULL pointer dereference. Fix the following smatch warnings: fs/kernfs/dir.c:1353 __kernfs_remove() warn: variable dereferenced before check 'kn' (see line 1346) Fixes: 393c3714081a ("kernfs: switch global kernfs_rwsem lock to per-fs lock") Signed-off-by: Yushan Zhou Link: https://lore.kernel.org/r/20220630082512.3482581-1-zys.zljxml@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- fs/kernfs/dir.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 6eca72cfa1f2..1cc88ba6de90 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1343,14 +1343,17 @@ static void __kernfs_remove(struct kernfs_node *kn) { struct kernfs_node *pos; + /* Short-circuit if non-root @kn has already finished removal. */ + if (!kn) + return; + lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); /* - * Short-circuit if non-root @kn has already finished removal. * This is for kernfs_remove_self() which plays with active ref * after removal. */ - if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb))) + if (kn->parent && RB_EMPTY_NODE(&kn->rb)) return; pr_debug("kernfs %s: removing\n", kn->name); -- 2.35.1