ext4: potential NULL dereference on error
Subsystems (2)
PATCH 1/1 ext4: potential NULL dereference on error
The ext4_get_group_desc() function returns NULL on error, and ext4_free_inodes_count() function dereferences it without checking. There is a check on the next line, but it's too late.
---
Static checker fix.
fs/ext4/ialloc.c +4 -2
@@ -389,7 +389,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,struct ext4_sb_info *sbi =EXT4_SB (sb);ext4_group_t real_ngroups =ext4_get_groups_count (sb);int inodes_per_group =EXT4_INODES_PER_GROUP (sb); -unsigned int freei, avefreei, grp_free; +unsigned int freei, avefreei;ext4_fsblk_t freeb, avefreec;unsigned int ndirs;int max_dirs, min_inodes;
@@ -399,6 +399,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,struct orlov_stats stats;int flex_size =ext4_flex_bg_size (sbi);struct dx_hash_info hinfo; +unsigned int grp_free =0 ; ngroups = real_ngroups;if (flex_size >1 ) {
@@ -508,7 +509,8 @@ fallback_retry:for (i =0 ; i < ngroups; i++) { grp = (parent_group + i) % ngroups; desc =ext4_get_group_desc (sb, grp,NULL ); - grp_free =ext4_free_inodes_count (sb, desc); +if (desc) + grp_free =ext4_free_inodes_count (sb, desc);if (desc && grp_free && grp_free >= avefreei) { *group = grp;return 0 ;
Jan Kara
reviewerQuoted 30 lines · Dan Carpenter
The
ext4_get_group_desc()function returns NULL on error, andext4_free_inodes_count()function dereferences it without checking. There is a check on the next line, but it's too late.Signed-off-by Dan Carpenter <[email protected]>---
Static checker fix.fs/ext4/ialloc.c +2
-1@@ -389,7 +389,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,struct ext4_sb_info *sbi =EXT4_SB (sb);ext4_group_t real_ngroups =ext4_get_groups_count (sb);int inodes_per_group =EXT4_INODES_PER_GROUP (sb); -unsigned int freei, avefreei, grp_free; +unsigned int freei, avefreei;ext4_fsblk_t freeb, avefreec;unsigned int ndirs;int max_dirs, min_inodes;@@ -399,6 +399,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,struct orlov_stats stats;int flex_size =ext4_flex_bg_size (sbi);struct dx_hash_info hinfo; +unsigned int grp_free =0 ; ngroups = real_ngroups;if (flex_size >1 ) {
fs/ext4/ialloc.c fallback_retry: · line 514
Quoted 7 lines
fs/ext4/ialloc.c +2
-1@@ -508,7 +509,8 @@ fallback_retry:for (i =0 ; i < ngroups; i++) { grp = (parent_group + i) % ngroups; desc =ext4_get_group_desc (sb, grp,NULL ); - grp_free =ext4_free_inodes_count (sb, desc); +if (desc) + grp_free =ext4_free_inodes_count (sb, desc);if (desc && grp_free && grp_free >= avefreei) {
So you it would be more logical to do:
if (desc) {
grp_free = ext4_free_inodes_count(sb, desc);
if (grp_free && grpfree >= avefreei) {
*group = grp;
return 0;
}
}Wouldn't it?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 18 lines · Jan Kara
fs/ext4/ialloc.c fallback_retry: · line 514
Quoted 8 lines · Dan Carpenter
fs/ext4/ialloc.c +2
-1@@ -508,7 +509,8 @@ fallback_retry:for (i =0 ; i < ngroups; i++) { grp = (parent_group + i) % ngroups; desc =ext4_get_group_desc (sb, grp,NULL ); - grp_free =ext4_free_inodes_count (sb, desc); +if (desc) + grp_free =ext4_free_inodes_count (sb, desc);if (desc && grp_free && grp_free >= avefreei) {So you it would be more logical to do:
if (desc) { grp_free = ext4_free_inodes_count(sb, desc); if (grp_free && grpfree >= avefreei) { *group = grp; return 0; } }
Quoted 1 line
Wouldn't it?
Yeah. You're obviously right. I'll resend.
regards,
dan carpenter
PATCH v2 1/1 ext4: potential NULL dereference on error
In reply to Jan KaraThe ext4_get_group_desc() function returns NULL on error, and ext4_free_inodes_count() function dereferences it without checking. There is a check on the next line, but it's too late.
---
v2: cleaned up the style
fs/ext4/ialloc.c +6 -4
@@ -508,10 +508,12 @@ fallback_retry:for (i =0 ; i < ngroups; i++) { grp = (parent_group + i) % ngroups; desc =ext4_get_group_desc (sb, grp,NULL ); - grp_free =ext4_free_inodes_count (sb, desc); -if (desc && grp_free && grp_free >= avefreei) { - *group = grp; -return 0 ; +if (desc) { + grp_free =ext4_free_inodes_count (sb, desc); +if (grp_free && grp_free >= avefreei) { + *group = grp; +return 0 ; + } } }
Quoted 5 lines · Dan Carpenter
The
ext4_get_group_desc()function returns NULL on error, andext4_free_inodes_count()function dereferences it without checking. There is a check on the next line, but it's too late.Signed-off-by Dan Carpenter <[email protected]>
Looks good. Thanks! You can add:
Honza
Quoted 24 lines
---
v2: cleaned up the stylefs/ext4/ialloc.c +6
-4@@ -508,10 +508,12 @@ fallback_retry:for (i =0 ; i < ngroups; i++) { grp = (parent_group + i) % ngroups; desc =ext4_get_group_desc (sb, grp,NULL ); - grp_free =ext4_free_inodes_count (sb, desc); -if (desc && grp_free && grp_free >= avefreei) { - *group = grp; -return 0 ; +if (desc) { + grp_free =ext4_free_inodes_count (sb, desc); +if (grp_free && grp_free >= avefreei) { + *group = grp; +return 0 ; + } } }
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 5 lines · Dan Carpenter
The
ext4_get_group_desc()function returns NULL on error, andext4_free_inodes_count()function dereferences it without checking. There is a check on the next line, but it's too late.Signed-off-by Dan Carpenter <[email protected]>
Thanks, applied.
- Ted