linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch] ext4: potential NULL dereference on error
@ 2012-05-13 14:41 Dan Carpenter
  2012-05-14 22:25 ` Jan Kara
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2012-05-13 14:41 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: Andreas Dilger, linux-ext4, kernel-janitors

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.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
Static checker fix.

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index a044a9b..1526f33 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -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;

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [patch] ext4: potential NULL dereference on error
  2012-05-13 14:41 [patch] ext4: potential NULL dereference on error Dan Carpenter
@ 2012-05-14 22:25 ` Jan Kara
  2012-05-15  6:13   ` Dan Carpenter
  2012-05-15  8:46   ` [patch v2] " Dan Carpenter
  0 siblings, 2 replies; 6+ messages in thread
From: Jan Kara @ 2012-05-14 22:25 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Theodore Ts'o, Andreas Dilger, linux-ext4, kernel-janitors

On Sun 13-05-12 17:41:04, Dan Carpenter wrote:
> 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.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> Static checker fix.
> 
> diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
> index a044a9b..1526f33 100644
> --- a/fs/ext4/ialloc.c
> +++ b/fs/ext4/ialloc.c
> @@ -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) {
  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
-- 
Jan Kara <jack@suse.cz>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] ext4: potential NULL dereference on error
  2012-05-14 22:25 ` Jan Kara
@ 2012-05-15  6:13   ` Dan Carpenter
  2012-05-15  8:46   ` [patch v2] " Dan Carpenter
  1 sibling, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2012-05-15  6:13 UTC (permalink / raw)
  To: Jan Kara; +Cc: Theodore Ts'o, Andreas Dilger, linux-ext4, kernel-janitors

On Tue, May 15, 2012 at 12:25:35AM +0200, Jan Kara wrote:
> On Sun 13-05-12 17:41:04, Dan Carpenter wrote:
> > @@ -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?

Yeah.  You're obviously right.  I'll resend.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [patch v2] ext4: potential NULL dereference on error
  2012-05-14 22:25 ` Jan Kara
  2012-05-15  6:13   ` Dan Carpenter
@ 2012-05-15  8:46   ` Dan Carpenter
  2012-05-15 10:01     ` Jan Kara
  2012-05-28 15:01     ` Ted Ts'o
  1 sibling, 2 replies; 6+ messages in thread
From: Dan Carpenter @ 2012-05-15  8:46 UTC (permalink / raw)
  To: Theodore Ts'o, Jan Kara; +Cc: Andreas Dilger, linux-ext4, kernel-janitors

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.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
v2: cleaned up the style

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index a044a9b..ea32d7e 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -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;
+			}
 		}
 	}
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [patch v2] ext4: potential NULL dereference on error
  2012-05-15  8:46   ` [patch v2] " Dan Carpenter
@ 2012-05-15 10:01     ` Jan Kara
  2012-05-28 15:01     ` Ted Ts'o
  1 sibling, 0 replies; 6+ messages in thread
From: Jan Kara @ 2012-05-15 10:01 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Theodore Ts'o, Jan Kara, Andreas Dilger, linux-ext4,
	kernel-janitors

On Tue 15-05-12 11:46:40, Dan Carpenter wrote:
> 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.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
  Looks good. Thanks! You can add:
Reviewed-by: Jan Kara <jack@suse.cz>

								Honza
> ---
> v2: cleaned up the style
> 
> diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
> index a044a9b..ea32d7e 100644
> --- a/fs/ext4/ialloc.c
> +++ b/fs/ext4/ialloc.c
> @@ -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;
> +			}
>  		}
>  	}
>  
-- 
Jan Kara <jack@suse.cz>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch v2] ext4: potential NULL dereference on error
  2012-05-15  8:46   ` [patch v2] " Dan Carpenter
  2012-05-15 10:01     ` Jan Kara
@ 2012-05-28 15:01     ` Ted Ts'o
  1 sibling, 0 replies; 6+ messages in thread
From: Ted Ts'o @ 2012-05-28 15:01 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: Jan Kara, Andreas Dilger, linux-ext4, kernel-janitors

On Tue, May 15, 2012 at 11:46:40AM +0300, Dan Carpenter wrote:
> 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.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Thanks, applied.

					- Ted

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-05-28 15:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-13 14:41 [patch] ext4: potential NULL dereference on error Dan Carpenter
2012-05-14 22:25 ` Jan Kara
2012-05-15  6:13   ` Dan Carpenter
2012-05-15  8:46   ` [patch v2] " Dan Carpenter
2012-05-15 10:01     ` Jan Kara
2012-05-28 15:01     ` Ted Ts'o

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).