Skip to contentSkip to conversation

fs: minix: Fix handling of corrupted directories

PATCH In review Andrey Kriulin 2 participants 2 messages May 26, 2025 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH v4 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")

---
v4: Add nlinks check for parent dirictory to minix_rmdir per Jan Kara <[email protected]> request.
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..ab86fd16e548 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 || dir->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
Reviewed-by on v4 1/1
#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")

OK, I'm fine with this. Feel free to add:

Honza

Quoted 44 lines

---
v4: Add nlinks check for parent dirictory to minix_rmdir per Jan Kara <[email protected]> request.
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..ab86fd16e548 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 || dir->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
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR