kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch] ext4: underflow in alignment check
@ 2016-06-16  7:07 Dan Carpenter
  2016-06-20 16:02 ` Jan Kara
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2016-06-16  7:07 UTC (permalink / raw)
  To: Theodore Ts'o
  Cc: Andreas Dilger, linux-ext4, linux-kernel, kernel-janitors

My static checker complains that this can underflow if arg is negative
which is true.

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

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index b1a3471..a2a17e9 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1576,7 +1576,7 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
 	} else if (token = Opt_min_batch_time) {
 		sbi->s_min_batch_time = arg;
 	} else if (token = Opt_inode_readahead_blks) {
-		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
+		if (arg && (arg > (1U << 30) || !is_power_of_2(arg))) {
 			ext4_msg(sb, KERN_ERR,
 				 "EXT4-fs: inode_readahead_blks must be "
 				 "0 or a power of 2 smaller than 2^31");

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

* Re: [patch] ext4: underflow in alignment check
  2016-06-16  7:07 [patch] ext4: underflow in alignment check Dan Carpenter
@ 2016-06-20 16:02 ` Jan Kara
  2016-06-20 19:53   ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Kara @ 2016-06-20 16:02 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Theodore Ts'o, Andreas Dilger, linux-ext4, linux-kernel,
	kernel-janitors

On Thu 16-06-16 10:07:09, Dan Carpenter wrote:
> My static checker complains that this can underflow if arg is negative
> which is true.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

How come? (1 << 30) fits even into 32-bit signed type. So where's the
problem?

								Honza

> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index b1a3471..a2a17e9 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -1576,7 +1576,7 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
>  	} else if (token = Opt_min_batch_time) {
>  		sbi->s_min_batch_time = arg;
>  	} else if (token = Opt_inode_readahead_blks) {
> -		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
> +		if (arg && (arg > (1U << 30) || !is_power_of_2(arg))) {
>  			ext4_msg(sb, KERN_ERR,
>  				 "EXT4-fs: inode_readahead_blks must be "
>  				 "0 or a power of 2 smaller than 2^31");
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [patch] ext4: underflow in alignment check
  2016-06-20 16:02 ` Jan Kara
@ 2016-06-20 19:53   ` Dan Carpenter
  2016-06-21  7:43     ` Jan Kara
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2016-06-20 19:53 UTC (permalink / raw)
  To: Jan Kara
  Cc: Theodore Ts'o, Andreas Dilger, linux-ext4, linux-kernel,
	kernel-janitors

On Mon, Jun 20, 2016 at 06:02:04PM +0200, Jan Kara wrote:
> On Thu 16-06-16 10:07:09, Dan Carpenter wrote:
> > My static checker complains that this can underflow if arg is negative
> > which is true.
> > 
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> How come? (1 << 30) fits even into 32-bit signed type. So where's the
> problem?

Bad changelog...  I was talking about a different issue.  I was casting
it to unsigned to take advantage of type promototion.  Assume we have:

int arg = 1 << 31;

(arg > (1 << 30)) // <-- this is false
(arg > (1U << 30)) // <-- this is true so there is no underflow.

regards,
dan carpenter


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

* Re: [patch] ext4: underflow in alignment check
  2016-06-20 19:53   ` Dan Carpenter
@ 2016-06-21  7:43     ` Jan Kara
  2016-06-21 13:06       ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Kara @ 2016-06-21  7:43 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Jan Kara, Theodore Ts'o, Andreas Dilger, linux-ext4,
	linux-kernel, kernel-janitors

On Mon 20-06-16 22:53:26, Dan Carpenter wrote:
> On Mon, Jun 20, 2016 at 06:02:04PM +0200, Jan Kara wrote:
> > On Thu 16-06-16 10:07:09, Dan Carpenter wrote:
> > > My static checker complains that this can underflow if arg is negative
> > > which is true.
> > > 
> > > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > 
> > How come? (1 << 30) fits even into 32-bit signed type. So where's the
> > problem?
> 
> Bad changelog...  I was talking about a different issue.  I was casting
> it to unsigned to take advantage of type promototion.  Assume we have:
> 
> int arg = 1 << 31;
> 
> (arg > (1 << 30)) // <-- this is false
> (arg > (1U << 30)) // <-- this is true so there is no underflow.

I see, but match_int() - or more precisely match_number() returns -ERANGE
when the number is > INT_MAX, subsequently we check whether the number is <
0 (Opt_inode_readahead_blks has flag MOPT_GTE0 set) and bail out if yes. So
at the place you are modifying we are sure the number is in [0, INT_MAX].
So the condition (arg > (1 << 30)) is pointless - just defensive
programming in case we decide e.g. to upgrade the type of 'arg' to long - but
not wrong...

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [patch] ext4: underflow in alignment check
  2016-06-21  7:43     ` Jan Kara
@ 2016-06-21 13:06       ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2016-06-21 13:06 UTC (permalink / raw)
  To: Jan Kara
  Cc: Theodore Ts'o, Andreas Dilger, linux-ext4, linux-kernel,
	kernel-janitors

On Tue, Jun 21, 2016 at 09:43:53AM +0200, Jan Kara wrote:
> On Mon 20-06-16 22:53:26, Dan Carpenter wrote:
> > On Mon, Jun 20, 2016 at 06:02:04PM +0200, Jan Kara wrote:
> > > On Thu 16-06-16 10:07:09, Dan Carpenter wrote:
> > > > My static checker complains that this can underflow if arg is negative
> > > > which is true.
> > > > 
> > > > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > > 
> > > How come? (1 << 30) fits even into 32-bit signed type. So where's the
> > > problem?
> > 
> > Bad changelog...  I was talking about a different issue.  I was casting
> > it to unsigned to take advantage of type promototion.  Assume we have:
> > 
> > int arg = 1 << 31;
> > 
> > (arg > (1 << 30)) // <-- this is false
> > (arg > (1U << 30)) // <-- this is true so there is no underflow.
> 
> I see, but match_int() - or more precisely match_number() returns -ERANGE
> when the number is > INT_MAX, subsequently we check whether the number is <
> 0 (Opt_inode_readahead_blks has flag MOPT_GTE0 set) and bail out if yes. So
> at the place you are modifying we are sure the number is in [0, INT_MAX].
> So the condition (arg > (1 << 30)) is pointless - just defensive
> programming in case we decide e.g. to upgrade the type of 'arg' to long - but
> not wrong...

Ah.  Smatch wasn't able to figure out that MOPT_GTE0 was set.

Thanks for reviewing this.

regards,
dan carpenter


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

end of thread, other threads:[~2016-06-21 13:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-16  7:07 [patch] ext4: underflow in alignment check Dan Carpenter
2016-06-20 16:02 ` Jan Kara
2016-06-20 19:53   ` Dan Carpenter
2016-06-21  7:43     ` Jan Kara
2016-06-21 13:06       ` Dan Carpenter

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).