[PATCH 1/2] ext4: fail ext4_iget if special inode unallocated

Baokun Li posted 2 patches 2 years, 8 months ago
There is a newer version of this series
[PATCH 1/2] ext4: fail ext4_iget if special inode unallocated
Posted by Baokun Li 2 years, 8 months ago
In ext4_fill_super(), EXT4_ORPHAN_FS flag is cleared after
ext4_orphan_cleanup() is executed. Therefore, when __ext4_iget() is
called to get an inode whose i_nlink is 0 when the flag exists, no error
is returned. If the inode is a special inode, a null pointer dereference
may occur. If the value of i_nlink is 0 for any inodes (except boot loader
inodes) got by using the EXT4_IGET_SPECIAL flag, the current file system
is corrupted. Therefore, make the ext4_iget() function return an error if
it gets such an abnormal special inode.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=199179
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216541
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216539
Reported-by: Luís Henriques <lhenriques@suse.de>
Suggested-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Baokun Li <libaokun1@huawei.com>
---
 fs/ext4/inode.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 9d9f414f99fe..3f7cfa91ba89 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -4872,13 +4872,6 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
 		goto bad_inode;
 	raw_inode = ext4_raw_inode(&iloc);
 
-	if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
-		ext4_error_inode(inode, function, line, 0,
-				 "iget: root inode unallocated");
-		ret = -EFSCORRUPTED;
-		goto bad_inode;
-	}
-
 	if ((flags & EXT4_IGET_HANDLE) &&
 	    (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
 		ret = -ESTALE;
@@ -4951,10 +4944,13 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
 	 * NeilBrown 1999oct15
 	 */
 	if (inode->i_nlink == 0) {
-		if ((inode->i_mode == 0 ||
+		if ((inode->i_mode == 0 || (flags & EXT4_IGET_SPECIAL) ||
 		     !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
 		    ino != EXT4_BOOT_LOADER_INO) {
 			/* this inode is deleted */
+			if (flags & EXT4_IGET_SPECIAL)
+				ext4_error_inode(inode, function, line, 0,
+						 "iget: special inode unallocated");
 			ret = -ESTALE;
 			goto bad_inode;
 		}
-- 
2.31.1

Re: [PATCH 1/2] ext4: fail ext4_iget if special inode unallocated
Posted by Jan Kara 2 years, 8 months ago
On Fri 06-01-23 18:47:05, Baokun Li wrote:
> In ext4_fill_super(), EXT4_ORPHAN_FS flag is cleared after
> ext4_orphan_cleanup() is executed. Therefore, when __ext4_iget() is
> called to get an inode whose i_nlink is 0 when the flag exists, no error
> is returned. If the inode is a special inode, a null pointer dereference
> may occur. If the value of i_nlink is 0 for any inodes (except boot loader
> inodes) got by using the EXT4_IGET_SPECIAL flag, the current file system
> is corrupted. Therefore, make the ext4_iget() function return an error if
> it gets such an abnormal special inode.
> 
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=199179
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=216541
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=216539
> Reported-by: Luís Henriques <lhenriques@suse.de>
> Suggested-by: Theodore Ts'o <tytso@mit.edu>
> Signed-off-by: Baokun Li <libaokun1@huawei.com>

Thanks for the patch! A few comments below.

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 9d9f414f99fe..3f7cfa91ba89 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -4872,13 +4872,6 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
>  		goto bad_inode;
>  	raw_inode = ext4_raw_inode(&iloc);
>  
> -	if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
> -		ext4_error_inode(inode, function, line, 0,
> -				 "iget: root inode unallocated");
> -		ret = -EFSCORRUPTED;
> -		goto bad_inode;
> -	}
> -
>  	if ((flags & EXT4_IGET_HANDLE) &&
>  	    (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
>  		ret = -ESTALE;
> @@ -4951,10 +4944,13 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
>  	 * NeilBrown 1999oct15
>  	 */
>  	if (inode->i_nlink == 0) {
> -		if ((inode->i_mode == 0 ||
> +		if ((inode->i_mode == 0 || (flags & EXT4_IGET_SPECIAL) ||
					   ^ extra parenthesis here  ^
>  		     !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
>  		    ino != EXT4_BOOT_LOADER_INO) {
>  			/* this inode is deleted */
> +			if (flags & EXT4_IGET_SPECIAL)
> +				ext4_error_inode(inode, function, line, 0,
> +						 "iget: special inode unallocated");

I think it would be better to return -EFSCORRUPTED and not -ESTALE in this
case.

>  			ret = -ESTALE;
>  			goto bad_inode;
>  		}

Otherwise the patch looks good to me.

									Honza

-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH 1/2] ext4: fail ext4_iget if special inode unallocated
Posted by Baokun Li 2 years, 8 months ago
On 2023/1/6 22:28, Jan Kara wrote:
> On Fri 06-01-23 18:47:05, Baokun Li wrote:
>> In ext4_fill_super(), EXT4_ORPHAN_FS flag is cleared after
>> ext4_orphan_cleanup() is executed. Therefore, when __ext4_iget() is
>> called to get an inode whose i_nlink is 0 when the flag exists, no error
>> is returned. If the inode is a special inode, a null pointer dereference
>> may occur. If the value of i_nlink is 0 for any inodes (except boot loader
>> inodes) got by using the EXT4_IGET_SPECIAL flag, the current file system
>> is corrupted. Therefore, make the ext4_iget() function return an error if
>> it gets such an abnormal special inode.
>>
>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=199179
>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=216541
>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=216539
>> Reported-by: Luís Henriques <lhenriques@suse.de>
>> Suggested-by: Theodore Ts'o <tytso@mit.edu>
>> Signed-off-by: Baokun Li <libaokun1@huawei.com>
> Thanks for the patch! A few comments below.
>
>> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
>> index 9d9f414f99fe..3f7cfa91ba89 100644
>> --- a/fs/ext4/inode.c
>> +++ b/fs/ext4/inode.c
>> @@ -4872,13 +4872,6 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
>>   		goto bad_inode;
>>   	raw_inode = ext4_raw_inode(&iloc);
>>   
>> -	if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
>> -		ext4_error_inode(inode, function, line, 0,
>> -				 "iget: root inode unallocated");
>> -		ret = -EFSCORRUPTED;
>> -		goto bad_inode;
>> -	}
>> -
>>   	if ((flags & EXT4_IGET_HANDLE) &&
>>   	    (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
>>   		ret = -ESTALE;
>> @@ -4951,10 +4944,13 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
>>   	 * NeilBrown 1999oct15
>>   	 */
>>   	if (inode->i_nlink == 0) {
>> -		if ((inode->i_mode == 0 ||
>> +		if ((inode->i_mode == 0 || (flags & EXT4_IGET_SPECIAL) ||
> 					   ^ extra parenthesis here  ^
Indeed, I'll remove the extra parenthesis.
>>   		     !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
>>   		    ino != EXT4_BOOT_LOADER_INO) {
>>   			/* this inode is deleted */
>> +			if (flags & EXT4_IGET_SPECIAL)
>> +				ext4_error_inode(inode, function, line, 0,
>> +						 "iget: special inode unallocated");
> I think it would be better to return -EFSCORRUPTED and not -ESTALE in this
> case.
Totally agree! For special inode, -EFSCORRUPTED is more reasonable.
>>   			ret = -ESTALE;
>>   			goto bad_inode;
>>   		}
> Otherwise the patch looks good to me.
>
> 									Honza
>
Thank you very much for your review!
I will send a patch V2 with the changes suggested by you.

-- 
With Best Regards,
Baokun Li
.