Skip to contentSkip to conversation

fs: Restrict lock_two_nondirectories() to non-directory inodes

bug report Applied Dan Carpenter 3 participants 3 messages Jun 6, 2023 – Jun 7, 2023 VFS

lore
Share & tools
lore
#0 lore raw

Hello Jan Kara,

This is a semi-automatic email about new static checker warnings.

The patch afb4adc7c3ef: "fs: Restrict lock_two_nondirectories() to non-directory inodes" from Jun 1, 2023, leads to the following Smatch complaint:

fs/inode.c:1174 unlock_two_nondirectories()
warn: variable dereferenced before check 'inode1' (see line 1172)

fs/inode.c:1176 unlock_two_nondirectories()
warn: variable dereferenced before check 'inode2' (see line 1173)

fs/inode.c
1171 {

  1172		WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
                                     ^^^^^^^^^^^^^^
  1173		WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
  1174		if (inode1)
                    ^^^^^^
  1175			inode_unlock(inode1);
  1176		if (inode2 && inode2 != inode1)
                    ^^^^^

regards,
dan carpenter

reviewer
#1 lore raw
Quoted 8 lines · Dan Carpenter

Hello Jan Kara,

This is a semi-automatic email about new static checker warnings.

The patch afb4adc7c3ef: "fs: Restrict lock_two_nondirectories() to non-directory inodes" from Jun 1, 2023, leads to the following Smatch complaint:

Quoted 5 lines

fs/inode.c:1174 unlock_two_nondirectories()
warn: variable dereferenced before check 'inode1' (see line 1172)

fs/inode.c:1176 unlock_two_nondirectories()
warn: variable dereferenced before check 'inode2' (see line 1173)

Indeed, thanks for spotting this! Luckily there are currently no in-tree users passing NULL. Attached patch fixes this. Christian, can you please add this to your branch or squash it into the fixed commit? Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

From 4afde047fb4408553b9d2c548ebe355db7c95f0f Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Tue, 6 Jun 2023 11:52:33 +0200
Subject: [PATCH] fs: Fixup unlock_two_nondirectories() for NULL inodes

When inode is NULL, we cannot WARN on S_ISDIR(inode->i_mode) as that would be a NULL ptr dereference. Move the warnings below the NULL checks.

Fixes afb4adc7c3ef ("fs: Restrict lock_two_nondirectories() to non-directory inodes")
---
 fs/inode.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/inode.c +6 -4
index e2707ee88459..53ae3b76d232 100644
@@ -1169,12 +1169,14 @@ EXPORT_SYMBOL(lock_two_nondirectories);
  */
 void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
 {
-	WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
-	WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
-	if (inode1)
+	if (inode1) {
+		WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
 		inode_unlock(inode1);
-	if (inode2 && inode2 != inode1)
+	}
+	if (inode2 && inode2 != inode1) {
+		WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
 		inode_unlock(inode2);
+	}
 }
 EXPORT_SYMBOL(unlock_two_nondirectories);
 
Signature · 1 line
2.35.3
maintainer Applied
#2 lore raw
Quoted 9 lines · Jan Kara
Quoted 8 lines · Dan Carpenter

Hello Jan Kara,

This is a semi-automatic email about new static checker warnings.

The patch afb4adc7c3ef: "fs: Restrict lock_two_nondirectories() to non-directory inodes" from Jun 1, 2023, leads to the following Smatch complaint:

Quoted 8 lines
Quoted 5 lines

fs/inode.c:1174 unlock_two_nondirectories()
warn: variable dereferenced before check 'inode1' (see line 1172)

fs/inode.c:1176 unlock_two_nondirectories()
warn: variable dereferenced before check 'inode2' (see line 1173)

Indeed, thanks for spotting this! Luckily there are currently no in-tree users passing NULL. Attached patch fixes this. Christian, can you please

Thanks for the fixup!

Quoted 1 line

add this to your branch or squash it into the fixed commit? Thanks!

Of course. I've squashed the fix into the original patch.

---

Applied to the vfs.rename.locking branch of the vfs/vfs.git tree. Patches in the vfs.rename.locking branch should appear in linux-next soon.