fs: minix: Fix handling of corrupted directories
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.
---
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
@@ -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