mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] jfs: validate inode link counts before decrementing in rmdir, unlink, and rename
@ 2026-11-23  5:34 Arbab Haider
  0 siblings, 0 replies; only message in thread
From: Arbab Haider @ 2026-11-23  5:34 UTC (permalink / raw)
  To: Dave Kleikamp
  Cc: jfs-discussion, linux-kernel, syzbot+9c3df8acb6283d0cf12d,
	stable, Arbab Haider

syzbot reported a WARN_ON(inode->i_nlink == 0) in drop_nlink() called
from jfs_rmdir() when removing subdirectories from a corrupted JFS
directory with an undercounted on-disk di_nlink:

  inode->i_nlink == 0
  WARNING: fs/inode.c:408 at drop_nlink+0xc5/0x110 fs/inode.c:408
  Call Trace:
   inode_dec_link_count include/linux/fs.h:2276 [inline]
   jfs_rmdir+0x416/0x850 fs/jfs/namei.c:395
   vfs_rmdir+0x3e9/0x700 fs/namei.c:5533

In JFS, every valid directory containing at least one subdirectory has
i_nlink >= 3 (its own entry, '.', and the child's '..'), and every valid
empty directory has i_nlink == 2.  If a corrupted on-disk directory has
an undercounted di_nlink, jfs_rmdir() decrements dip->i_nlink down to
0 and then underflows it on the next rmdir, triggering the drop_nlink()
warning and wrapping i_nlink to 0xffffffff.

The same link-count underflow can occur in jfs_unlink() if ip->i_nlink
is already 0, and in jfs_rename() when moving or overwriting a
directory (old_dir->i_nlink < 3 or target directory new_ip->i_nlink != 2)
or overwriting a non-directory with new_ip->i_nlink == 0.

Validate the expected link counts before starting a transaction in
jfs_rmdir(), jfs_unlink(), and jfs_rename(), reporting filesystem
corruption via jfs_error() and returning -EIO.

Tested locally in QEMU (x86_64, KASAN+UBSAN) using syzbot's C
reproducer and a corner-case suite covering corrupt rmdir (nlink=1, 2),
corrupt rename (parent nlink=1, target dir nlink=1), and valid rmdir
and directory-overwrite rename operations: triggered the exact
drop_nlink() warning before the patch, and returned -EIO cleanly with
zero warnings or regressions after the patch.

Reported-by: syzbot+9c3df8acb6283d0cf12d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9c3df8acb6283d0cf12d
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/jfs/namei.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/fs/jfs/namei.c b/fs/jfs/namei.c
index 8ab2e952c..5a06fd8c4 100644
--- a/fs/jfs/namei.c
+++ b/fs/jfs/namei.c
@@ -357,6 +357,14 @@ static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
 		goto out;
 	}
 
+	if (dip->i_nlink < 3) {
+		jfs_error(dip->i_sb,
+			  "corrupt parent directory link count (%u)\n",
+			  dip->i_nlink);
+		rc = -EIO;
+		goto out;
+	}
+
 	if ((rc = get_UCSname(&dname, dentry))) {
 		goto out;
 	}
@@ -483,6 +491,12 @@ static int jfs_unlink(struct inode *dip, struct dentry *dentry)
 	if (rc)
 		goto out;
 
+	if (!ip->i_nlink) {
+		jfs_error(ip->i_sb, "corrupt inode link count (0)\n");
+		rc = -EIO;
+		goto out;
+	}
+
 	if ((rc = get_UCSname(&dname, dentry)))
 		goto out;
 
@@ -1135,13 +1149,33 @@ static int jfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	if (S_ISDIR(old_ip->i_mode)) {
+		if ((new_ip || old_dir != new_dir) && old_dir->i_nlink < 3) {
+			jfs_error(old_dir->i_sb,
+				  "corrupt parent directory link count (%u)\n",
+				  old_dir->i_nlink);
+			rc = -EIO;
+			goto out3;
+		}
 		if (new_ip) {
 			if (!dtEmpty(new_ip)) {
 				rc = -ENOTEMPTY;
 				goto out3;
 			}
+			if (new_ip->i_nlink != 2) {
+				jfs_error(new_ip->i_sb,
+					  "corrupt target directory link count (%u)\n",
+					  new_ip->i_nlink);
+				rc = -EIO;
+				goto out3;
+			}
 		}
 	} else if (new_ip) {
+		if (!new_ip->i_nlink) {
+			jfs_error(new_ip->i_sb,
+				  "corrupt target inode link count (0)\n");
+			rc = -EIO;
+			goto out3;
+		}
 		IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
 		/* Init inode for quota operations. */
 		rc = dquot_initialize(new_ip);
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-23  5:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  5:34 [PATCH] jfs: validate inode link counts before decrementing in rmdir, unlink, and rename Arbab Haider

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®