fs: minix: Fix handling of corrupted directories
PATCH 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 directory in minix_lookup.
Found by Linux Verification Center (linuxtesting.org) with Syzkaller.
--- fs/minix/namei.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/minix/namei.c +6 -1
@@ -28,8 +28,13 @@ static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unreturn ERR_PTR (-ENAMETOOLONG ); ino =minix_inode_by_name (dentry); -if (ino) +if (ino) { inode =minix_iget (dir->i_sb, ino); +if (S_ISDIR (inode->i_mode) && inode->i_nlink <2 ) { +iput (inode); +return ERR_PTR (-EIO ); + } + }return d_splice_alias (inode, dentry); }
Signature · 1 line
2.47.2
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_rmdirwill try to reduce the nlinks(unsigned int) to a negative value.
Quoted 7 lines
Make nlinks validity check for directory in
minix_lookup.Found by Linux Verification Center (linuxtesting.org) with Syzkaller.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Andrey Kriulin <[email protected]>
Thanks for the patch. One comment below.
Quoted 8 lines
fs/minix/namei.c +0
-0@@ -28,8 +28,13 @@ static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unreturn ERR_PTR (-ENAMETOOLONG ); ino =minix_inode_by_name (dentry);
fs/minix/namei.c static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, un · line 39
Quoted 10 lines
fs/minix/namei.c +6
-1-if (ino) +if (ino) { inode =minix_iget (dir->i_sb, ino); +if (S_ISDIR (inode->i_mode) && inode->i_nlink <2 ) { +iput (inode); +return ERR_PTR (-EIO ); + } + }return d_splice_alias (inode, dentry); }
I don't think this is the best place to handle such check. IMO it would be more logical to do it in minix_iget() - V[12]_minix_iget() to be more precise - to properly catch all the paths where the inode is loaded into memory. This way your check will not happen for the root directory inode for example.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR