kernfs: fix potential NULL dereference in __kernfs_remove

Bugzilla: http://bugzilla.redhat.com/2152737
Upstream status: master tree.

commit 72b5d5aef246a0387cefa23121dd90901c7a691a
Author: Yushan Zhou <katrinzhou@tencent.com>
Date:   Thu Jun 30 16:25:12 2022 +0800

    kernfs: fix potential NULL dereference in __kernfs_remove

    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 <katrinzhou@tencent.com>
    Link: https://lore.kernel.org/r/20220630082512.3482581-1-zys.zljxml@gmail.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>
This commit is contained in:
Luis Claudio R. Goncalves 2022-12-12 19:32:17 -03:00
parent 3c025b06be
commit 5d95658a78
1 changed files with 5 additions and 2 deletions

View File

@ -1327,14 +1327,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);