jfs: validate inode link counts before decrementing in rmdir, unlink, and rename
Subsystems (2)
PATCH 1/1 jfs: validate inode link counts before decrementing in rmdir, unlink, and rename
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.
--- fs/jfs/namei.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
fs/jfs/namei.c +34 -0
@@ -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);
Signature · 1 line
2.53.0