Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3254236rwb; Mon, 15 Aug 2022 22:24:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR7fPH8SOxcM7CeeQyV70r6Za2JEiQu0Z55WD+ZFuezRBLUUxlrgbj2EqCFAwRkT7xFVBNot X-Received: by 2002:a17:903:234f:b0:16f:18a6:451a with SMTP id c15-20020a170903234f00b0016f18a6451amr19949359plh.82.1660627485112; Mon, 15 Aug 2022 22:24:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660627485; cv=none; d=google.com; s=arc-20160816; b=upDekcIkVhMeXxF7sdVVM9+eFZI/3gRjIgFDJZgxFiKvkBbaEgNKKWAikwR4VqOokp mhjgtDRGTgXDVJZzxAWJfJSD2hXJniMPCZNPHVoqd2O8OZ33P4bRWW2pZmUYlOfEwhYr ojWtqtAqxsre3v/8R20/MED/g0UJ3bj3wLgPbvDLqXthW8/mV24NBtZrqzgqtc56rTh0 /KoDVLbzcSy3AT+nb49qLDjbt5qXCo0Jn7wDApMRubTRPokTqLT9GBfNb+KaDNIU2ewX 2FtLWMZb7HmT2kYc5cF+VjfjykHAg/dfL8PLW11iOxk6yfRxsFs82GMv7R9vtUNd17Q/ vPPw== 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=gQIOFTFxkH7IZZLgvbLZn91mvYA7pGoieI0ivI6Yy+i7lxlIE6/37r6y6AKtWI8VhU hFzLz0AM69QKIIHComofaxZvjYI7nSgXp18gkeFmsQ5Deq/LntsHFisecdLxCkcEc/7m 1SD0mRYTbDJodn/mG2bxtt5O9tPd60L5geOyCfa2qiDygg9LIFFNVGNZjmxZTh4B2YUq +Iw3rQCxJJa4qjIdb/94j8xdbJnr4fkVLYTnLd2WNi3B4UcNLtp3b1X8yMHchUgPGWFx hHsVInF6XBT4m4sl4BWynJZMJR2ZLIlvlXSIbtZkqou/94l5PJbRxJa8GU3y9hRZVL3N FS9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CuNmipCR; 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 p29-20020a056a0026dd00b0052523d203a0si11214669pfw.77.2022.08.15.22.24.33; Mon, 15 Aug 2022 22:24:45 -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=CuNmipCR; 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 S231844AbiHPEoW (ORCPT + 99 others); Tue, 16 Aug 2022 00:44:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232382AbiHPElH (ORCPT ); Tue, 16 Aug 2022 00:41:07 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8975D61; Mon, 15 Aug 2022 13:32:15 -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 ams.source.kernel.org (Postfix) with ESMTPS id 80010B8119B; Mon, 15 Aug 2022 20:32:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3682C433D6; Mon, 15 Aug 2022 20:32:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660595533; bh=8O+aw44hiz3gQ+R3zmwagrKbzLPzdYibj+A58m4ChMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CuNmipCRT6726htxFJDHutVTUf88VFiKZpFLs/XJ/phxheegCevV/Jbgx+Xe/j2IJ dSEHlouWPcJlYBr31IR4r0jH+4VMhUkuYwJwLOeTlANPSvVzLq4Drrnbw3t3rACKDV CxxuDzpE0zN8qt4+bPI/GcsavZk4ud4u1Xbm07sw= 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.19 0763/1157] kernfs: fix potential NULL dereference in __kernfs_remove Date: Mon, 15 Aug 2022 20:01:59 +0200 Message-Id: <20220815180509.997775009@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@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