]> git.baikalelectronics.ru Git - kernel.git/commitdiff
kernfs: fix potential NULL dereference in __kernfs_remove
authorYushan Zhou <katrinzhou@tencent.com>
Thu, 30 Jun 2022 08:25:12 +0000 (16:25 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 1 Jul 2022 08:36:36 +0000 (10:36 +0200)
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: 01a757656425 ("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>
fs/kernfs/dir.c

index 6eca72cfa1f28138ca2b445964becee110c5af02..1cc88ba6de9077c8250f4edf09d3279b507e289b 100644 (file)
@@ -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);