Skip to contentSkip to conversation

fs: minix: Fix handling of corrupted directories

PATCH Superseded Andrey Kriulin 2 participants 3 messages May 15, 2025 – May 16, 2025 Stable branch

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 fs: minix: Fix handling of corrupted directories

If the directory is corrupted and the number of nlinks is less than 2 (valid nlinks have at least 2), then when the directory is deleted, the minix_rmdir will try to reduce the nlinks(unsigned int) to a negative value.

Make nlinks validity check for directories.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
v3: Move nlinks validaty check to minix_rmdir and minix_rename per Jan Kara <[email protected]> request.
v2: Move nlinks validaty check to V[12]_minix_iget() per Jan Kara <[email protected]> request. Change return error code to EUCLEAN. Don't block directory in r/o mode per Al Viro <[email protected]> request.

 fs/minix/namei.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/minix/namei.c +9 -1
index 8938536d8d3c..5a1e5f8ef443 100644
@@ -161,8 +161,12 @@ static int minix_unlink(struct inode * dir, struct dentry *dentry)
 static int minix_rmdir(struct inode * dir, struct dentry *dentry)
 {
 	struct inode * inode = d_inode(dentry);
-	int err = -ENOTEMPTY;
+	int err = -EUCLEAN;
 
+	if (inode->i_nlink < 2)
+		return err;
+
+	err = -ENOTEMPTY;
 	if (minix_empty_dir(inode)) {
 		err = minix_unlink(dir, dentry);
 		if (!err) {
@@ -235,6 +239,10 @@ static int minix_rename(struct mnt_idmap *idmap,
 	mark_inode_dirty(old_inode);
 
 	if (dir_de) {
+		if (old_dir->i_nlink <= 2) {
+			err = -EUCLEAN;
+			goto out_dir;
+		}
 		err = minix_set_link(dir_de, dir_folio, new_dir);
 		if (!err)
 			inode_dec_link_count(old_dir);
Signature · 1 line
2.47.2
#1 lore raw
Quoted 5 lines · Andrey Kriulin

If the directory is corrupted and the number of nlinks is less than 2 (valid nlinks have at least 2), then when the directory is deleted, the minix_rmdir will try to reduce the nlinks(unsigned int) to a negative value.

Quoted 7 lines

Make nlinks validity check for directories.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

This patch is mostly fine (see just one nit below).

Quoted 15 lines

---
v3: Move nlinks validaty check to minix_rmdir and minix_rename per Jan Kara <[email protected]> request.
v2: Move nlinks validaty check to V[12]_minix_iget() per Jan Kara <[email protected]> request. Change return error code to EUCLEAN. Don't block directory in r/o mode per Al Viro <[email protected]> request.

 fs/minix/namei.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/minix/namei.c +0 -0
index 8938536d8d3c..5a1e5f8ef443 100644

fs/minix/namei.c static int minix_unlink(struct inode * dir, struct dentry *dentry) · line 166

Quoted 7 lines
fs/minix/namei.c +2 -1
@@ -161,8 +161,12 @@ static int minix_unlink(struct inode * dir, struct dentry *dentry)
 static int minix_rmdir(struct inode * dir, struct dentry *dentry)
 {
 	struct inode * inode = d_inode(dentry);
-	int err = -ENOTEMPTY;
+	int err = -EUCLEAN;
 
+	if (inode->i_nlink < 2)

This should be <= 2 as well?

Honza

Quoted 20 lines
fs/minix/namei.c +7 -0
+		return err;
+
+	err = -ENOTEMPTY;
 	if (minix_empty_dir(inode)) {
 		err = minix_unlink(dir, dentry);
 		if (!err) {
@@ -235,6 +239,10 @@ static int minix_rename(struct mnt_idmap *idmap,
 	mark_inode_dirty(old_inode);
 
 	if (dir_de) {
+		if (old_dir->i_nlink <= 2) {
+			err = -EUCLEAN;
+			goto out_dir;
+		}
 		err = minix_set_link(dir_de, dir_folio, new_dir);
 		if (!err)
 			inode_dec_link_count(old_dir);
Signature · 1 line
2.47.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#2 lore raw

No, because the live directory has nlinks = 2 when deleted. But if I understand correctly, it's worth adding a check for parent directory(dir->i_nlinks <= 2)