mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arbab Haider <arbabhaider649@gmail.com>
To: Dave Kleikamp <shaggy@kernel.org>
Cc: jfs-discussion@lists.sourceforge.net,
	linux-kernel@vger.kernel.org,
	syzbot+9c3df8acb6283d0cf12d@syzkaller.appspotmail.com,
	stable@vger.kernel.org, Arbab Haider <arbabhaider649@gmail.com>
Subject: [PATCH] jfs: validate inode link counts before decrementing in rmdir, unlink, and rename
Date: Mon, 23 Nov 2026 10:34:09 +0500	[thread overview]
Message-ID: <20261123053409.53956-1-arbabhaider649@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-23  5:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123053409.53956-1-arbabhaider649@gmail.com \
    --to=arbabhaider649@gmail.com \
    --cc=jfs-discussion@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shaggy@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+9c3df8acb6283d0cf12d@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®