Skip to contentSkip to conversation

kernfs: fix potential NULL dereference in __kernfs_remove

PATCH Superseded [email protected] 2 participants 3 messages Jun 30, 2022 kernfs

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernfs: fix potential NULL dereference in __kernfs_remove

From: katrinzhou <[email protected]>

When lockdep is enabled, lockdep_assert_held_write would cause potential NULL pointer dereference.

Fix the folloeing smatch warnings:

fs/kernfs/dir.c:1353 __kernfs_remove() warn: variable dereferenced before check 'kn' (see line 1346)

---
 fs/kernfs/dir.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/kernfs/dir.c +5 -2
index 6eca72cfa1f2..1cc88ba6de90 100644
@@ -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);
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 10 lines · On Thu, Jun 30, 2022 at 12:00:47PM +0800, [email protected] wrote:

From: katrinzhou <[email protected]>

When lockdep is enabled, lockdep_assert_held_write would cause potential NULL pointer dereference.

Fix the folloeing smatch warnings:

fs/kernfs/dir.c:1353 __kernfs_remove() warn: variable dereferenced before check 'kn' (see line 1346)

Can you please submit this with your legal name that you use to sign documents?

Also, what commit id does this fix? Did you actually hit this with a real workload? How can this be reproduced and tested?

thanks,

greg k-h

author
#2 lore raw
Quoted 16 lines · Greg KH
Quoted 10 lines · On Thu, Jun 30, 2022 at 12:00:47PM +0800, [email protected] wrote:

From: katrinzhou <[email protected]>

When lockdep is enabled, lockdep_assert_held_write would cause potential NULL pointer dereference.

Fix the folloeing smatch warnings:

fs/kernfs/dir.c:1353 __kernfs_remove() warn: variable dereferenced before check 'kn' (see line 1346)

Can you please submit this with your legal name that you use to sign documents?

Quoted 6 lines

Also, what commit id does this fix? Did you actually hit this with a real workload? How can this be reproduced and tested?

thanks,

greg k-h

Sorry for the formatting issue. I'll submit a new patch soon. This problem is found via static code analysis, and by now I have not reproduced / tested it.

Best regards,
Katrin