linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item
@ 2019-02-20  3:08 YueHaibing
  2019-02-20  3:38 ` Qu Wenruo
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: YueHaibing @ 2019-02-20  3:08 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba
  Cc: YueHaibing, linux-btrfs, linux-kernel, kernel-janitors

btrfs_item_size_nr return value is u32, convert it to int may result
in truncation.Also read_extent_buffer expect a unsigned param, so
min_t should use type u32 to compare.

Fixes: 8ea05e3a4262 ("Btrfs: introduce subvol uuids and times")
Signed-off-by: YueHaibing <yuehaibing@huawei.com>
---
 fs/btrfs/root-tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 02d1a57af78b..893d12fbfda0 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
 				struct btrfs_root_item *item)
 {
 	uuid_le uuid;
-	int len;
+	u32 len;
 	int need_reset = 0;
 
 	len = btrfs_item_size_nr(eb, slot);
 	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
-			min_t(int, len, (int)sizeof(*item)));
+			   min_t(u32, len, sizeof(*item)));
 	if (len < sizeof(*item))
 		need_reset = 1;
 	if (!need_reset && btrfs_root_generation(item)




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

* Re: [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item
  2019-02-20  3:08 [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item YueHaibing
@ 2019-02-20  3:38 ` Qu Wenruo
  2019-02-20  5:58 ` Dan Carpenter
  2019-02-20 12:32 ` [PATCH v2 -next] btrfs: Remove unnecessary casts " YueHaibing
  2 siblings, 0 replies; 9+ messages in thread
From: Qu Wenruo @ 2019-02-20  3:38 UTC (permalink / raw)
  To: YueHaibing, Chris Mason, Josef Bacik, David Sterba
  Cc: linux-btrfs, linux-kernel, kernel-janitors


[-- Attachment #1.1: Type: text/plain, Size: 1394 bytes --]



On 2019/2/20 上午11:08, YueHaibing wrote:
> btrfs_item_size_nr return value is u32, convert it to int may result
> in truncation.Also read_extent_buffer expect a unsigned param, so
> min_t should use type u32 to compare.

Btrfs has a up limit on item size, it will never exceed 64K - various
overhead.

Furthermore, btrfs has metadata read time check to exclude such
obviously corrupted tree blocks, thus corrupted tree block will never
reach here.

Thanks,
Qu

> 
> Fixes: 8ea05e3a4262 ("Btrfs: introduce subvol uuids and times")
> Signed-off-by: YueHaibing <yuehaibing@huawei.com>
> ---
>  fs/btrfs/root-tree.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> index 02d1a57af78b..893d12fbfda0 100644
> --- a/fs/btrfs/root-tree.c
> +++ b/fs/btrfs/root-tree.c
> @@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
>  				struct btrfs_root_item *item)
>  {
>  	uuid_le uuid;
> -	int len;
> +	u32 len;
>  	int need_reset = 0;
>  
>  	len = btrfs_item_size_nr(eb, slot);
>  	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
> -			min_t(int, len, (int)sizeof(*item)));
> +			   min_t(u32, len, sizeof(*item)));
>  	if (len < sizeof(*item))
>  		need_reset = 1;
>  	if (!need_reset && btrfs_root_generation(item)
> 
> 
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item
  2019-02-20  3:08 [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item YueHaibing
  2019-02-20  3:38 ` Qu Wenruo
@ 2019-02-20  5:58 ` Dan Carpenter
  2019-02-20  6:10   ` Dan Carpenter
  2019-02-20 12:32 ` [PATCH v2 -next] btrfs: Remove unnecessary casts " YueHaibing
  2 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2019-02-20  5:58 UTC (permalink / raw)
  To: YueHaibing
  Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs,
	linux-kernel, kernel-janitors

On Wed, Feb 20, 2019 at 03:08:40AM +0000, YueHaibing wrote:
> btrfs_item_size_nr return value is u32, convert it to int may result
> in truncation.Also read_extent_buffer expect a unsigned param, so
> min_t should use type u32 to compare.
> 
> Fixes: 8ea05e3a4262 ("Btrfs: introduce subvol uuids and times")
> Signed-off-by: YueHaibing <yuehaibing@huawei.com>
> ---
>  fs/btrfs/root-tree.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> index 02d1a57af78b..893d12fbfda0 100644
> --- a/fs/btrfs/root-tree.c
> +++ b/fs/btrfs/root-tree.c
> @@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
>  				struct btrfs_root_item *item)
>  {
>  	uuid_le uuid;
> -	int len;
> +	u32 len;
>  	int need_reset = 0;
>  
>  	len = btrfs_item_size_nr(eb, slot);
>  	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
> -			min_t(int, len, (int)sizeof(*item)));
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Yeah, min_t() should normally cast to unsigned and the extra cast is
silly.

> +			   min_t(u32, len, sizeof(*item)));

regards,
dan carpenter


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

* Re: [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item
  2019-02-20  5:58 ` Dan Carpenter
@ 2019-02-20  6:10   ` Dan Carpenter
  2019-02-20  9:10     ` YueHaibing
  0 siblings, 1 reply; 9+ messages in thread
From: Dan Carpenter @ 2019-02-20  6:10 UTC (permalink / raw)
  To: YueHaibing
  Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs,
	linux-kernel, kernel-janitors

On Wed, Feb 20, 2019 at 08:58:43AM +0300, Dan Carpenter wrote:
> On Wed, Feb 20, 2019 at 03:08:40AM +0000, YueHaibing wrote:
> > btrfs_item_size_nr return value is u32, convert it to int may result
> > in truncation.Also read_extent_buffer expect a unsigned param, so
> > min_t should use type u32 to compare.
> > 
> > Fixes: 8ea05e3a4262 ("Btrfs: introduce subvol uuids and times")
> > Signed-off-by: YueHaibing <yuehaibing@huawei.com>
> > ---
> >  fs/btrfs/root-tree.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> > index 02d1a57af78b..893d12fbfda0 100644
> > --- a/fs/btrfs/root-tree.c
> > +++ b/fs/btrfs/root-tree.c
> > @@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
> >  				struct btrfs_root_item *item)
> >  {
> >  	uuid_le uuid;
> > -	int len;
> > +	u32 len;
> >  	int need_reset = 0;
> >  
> >  	len = btrfs_item_size_nr(eb, slot);
> >  	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
> > -			min_t(int, len, (int)sizeof(*item)));
>                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Yeah, min_t() should normally cast to unsigned and the extra cast is
> silly.
> 

Btw, I shouldn't have had to dig through the patch to find the *real*
reason you wrote it.  A better description would have said:

    There is a messy cast here:

	min_t(int, len, (int)sizeof(*item)));

    min_t() should normally cast to unsigned.  It's not possible for
    "len" to be negative, but if it were then then we definitely
    wouldn't want to pass negatives to read_extent_buffer().  Also there
    is an extra cast.

    This patch shouldn't affect runtime, it's just a clean up.

regards,
dan carpenter


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

* Re: [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item
  2019-02-20  6:10   ` Dan Carpenter
@ 2019-02-20  9:10     ` YueHaibing
  0 siblings, 0 replies; 9+ messages in thread
From: YueHaibing @ 2019-02-20  9:10 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs,
	linux-kernel, kernel-janitors


On 2019/2/20 14:10, Dan Carpenter wrote:
> On Wed, Feb 20, 2019 at 08:58:43AM +0300, Dan Carpenter wrote:
>> On Wed, Feb 20, 2019 at 03:08:40AM +0000, YueHaibing wrote:
>>> btrfs_item_size_nr return value is u32, convert it to int may result
>>> in truncation.Also read_extent_buffer expect a unsigned param, so
>>> min_t should use type u32 to compare.
>>>
>>> Fixes: 8ea05e3a4262 ("Btrfs: introduce subvol uuids and times")
>>> Signed-off-by: YueHaibing <yuehaibing@huawei.com>
>>> ---
>>>  fs/btrfs/root-tree.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
>>> index 02d1a57af78b..893d12fbfda0 100644
>>> --- a/fs/btrfs/root-tree.c
>>> +++ b/fs/btrfs/root-tree.c
>>> @@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
>>>  				struct btrfs_root_item *item)
>>>  {
>>>  	uuid_le uuid;
>>> -	int len;
>>> +	u32 len;
>>>  	int need_reset = 0;
>>>  
>>>  	len = btrfs_item_size_nr(eb, slot);
>>>  	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
>>> -			min_t(int, len, (int)sizeof(*item)));
>>                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> Yeah, min_t() should normally cast to unsigned and the extra cast is
>> silly.
>>
> 
> Btw, I shouldn't have had to dig through the patch to find the *real*
> reason you wrote it.  A better description would have said:
> 
>     There is a messy cast here:
> 
> 	min_t(int, len, (int)sizeof(*item)));
> 
>     min_t() should normally cast to unsigned.  It's not possible for
>     "len" to be negative, but if it were then then we definitely
>     wouldn't want to pass negatives to read_extent_buffer().  Also there
>     is an extra cast.
> 
>     This patch shouldn't affect runtime, it's just a clean up.

Yes, This just a cleanup, Thanks!

> 
> regards,
> dan carpenter
> 
> 
> .
> 


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

* [PATCH v2 -next] btrfs: Remove unnecessary casts in btrfs_read_root_item
  2019-02-20  3:08 [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item YueHaibing
  2019-02-20  3:38 ` Qu Wenruo
  2019-02-20  5:58 ` Dan Carpenter
@ 2019-02-20 12:32 ` YueHaibing
  2019-02-20 12:33   ` Dan Carpenter
                     ` (2 more replies)
  2 siblings, 3 replies; 9+ messages in thread
From: YueHaibing @ 2019-02-20 12:32 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba
  Cc: YueHaibing, linux-btrfs, linux-kernel, kernel-janitors, Dan Carpenter

There is a messy cast here:
	min_t(int, len, (int)sizeof(*item)));

min_t() should normally cast to unsigned.  It's not possible for
"len" to be negative, but if it were then we definitely
wouldn't want to pass negatives to read_extent_buffer().  Also there
is an extra cast.

This patch shouldn't affect runtime, it's just a clean up.

Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: YueHaibing <yuehaibing@huawei.com>
---
v2: modify commit message as Dan suggested 
---
 fs/btrfs/root-tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 02d1a57af78b..893d12fbfda0 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
 				struct btrfs_root_item *item)
 {
 	uuid_le uuid;
-	int len;
+	u32 len;
 	int need_reset = 0;
 
 	len = btrfs_item_size_nr(eb, slot);
 	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
-			min_t(int, len, (int)sizeof(*item)));
+			   min_t(u32, len, sizeof(*item)));
 	if (len < sizeof(*item))
 		need_reset = 1;
 	if (!need_reset && btrfs_root_generation(item)




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

* Re: [PATCH v2 -next] btrfs: Remove unnecessary casts in btrfs_read_root_item
  2019-02-20 12:32 ` [PATCH v2 -next] btrfs: Remove unnecessary casts " YueHaibing
@ 2019-02-20 12:33   ` Dan Carpenter
  2019-02-20 12:36   ` Qu Wenruo
  2019-02-22 14:39   ` David Sterba
  2 siblings, 0 replies; 9+ messages in thread
From: Dan Carpenter @ 2019-02-20 12:33 UTC (permalink / raw)
  To: YueHaibing
  Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs,
	linux-kernel, kernel-janitors

On Wed, Feb 20, 2019 at 12:32:02PM +0000, YueHaibing wrote:
> There is a messy cast here:
> 	min_t(int, len, (int)sizeof(*item)));
> 
> min_t() should normally cast to unsigned.  It's not possible for
> "len" to be negative, but if it were then we definitely
> wouldn't want to pass negatives to read_extent_buffer().  Also there
> is an extra cast.
> 
> This patch shouldn't affect runtime, it's just a clean up.
> 
> Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>

It wasn't really suggested by me...  But I do think it's the right
thing.

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

regards,
dan carpenter


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

* Re: [PATCH v2 -next] btrfs: Remove unnecessary casts in btrfs_read_root_item
  2019-02-20 12:32 ` [PATCH v2 -next] btrfs: Remove unnecessary casts " YueHaibing
  2019-02-20 12:33   ` Dan Carpenter
@ 2019-02-20 12:36   ` Qu Wenruo
  2019-02-22 14:39   ` David Sterba
  2 siblings, 0 replies; 9+ messages in thread
From: Qu Wenruo @ 2019-02-20 12:36 UTC (permalink / raw)
  To: YueHaibing, Chris Mason, Josef Bacik, David Sterba
  Cc: linux-btrfs, linux-kernel, kernel-janitors, Dan Carpenter


[-- Attachment #1.1: Type: text/plain, Size: 1463 bytes --]



On 2019/2/20 下午8:32, YueHaibing wrote:
> There is a messy cast here:
> 	min_t(int, len, (int)sizeof(*item)));
> 
> min_t() should normally cast to unsigned.  It's not possible for
> "len" to be negative, but if it were then we definitely
> wouldn't want to pass negatives to read_extent_buffer().  Also there
> is an extra cast.
> 
> This patch shouldn't affect runtime, it's just a clean up.
> 
> Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: YueHaibing <yuehaibing@huawei.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

The commit message is much better.

Thanks,
Qu

> ---
> v2: modify commit message as Dan suggested 
> ---
>  fs/btrfs/root-tree.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> index 02d1a57af78b..893d12fbfda0 100644
> --- a/fs/btrfs/root-tree.c
> +++ b/fs/btrfs/root-tree.c
> @@ -21,12 +21,12 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
>  				struct btrfs_root_item *item)
>  {
>  	uuid_le uuid;
> -	int len;
> +	u32 len;
>  	int need_reset = 0;
>  
>  	len = btrfs_item_size_nr(eb, slot);
>  	read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
> -			min_t(int, len, (int)sizeof(*item)));
> +			   min_t(u32, len, sizeof(*item)));
>  	if (len < sizeof(*item))
>  		need_reset = 1;
>  	if (!need_reset && btrfs_root_generation(item)
> 
> 
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v2 -next] btrfs: Remove unnecessary casts in btrfs_read_root_item
  2019-02-20 12:32 ` [PATCH v2 -next] btrfs: Remove unnecessary casts " YueHaibing
  2019-02-20 12:33   ` Dan Carpenter
  2019-02-20 12:36   ` Qu Wenruo
@ 2019-02-22 14:39   ` David Sterba
  2 siblings, 0 replies; 9+ messages in thread
From: David Sterba @ 2019-02-22 14:39 UTC (permalink / raw)
  To: YueHaibing
  Cc: Chris Mason, Josef Bacik, David Sterba, linux-btrfs,
	linux-kernel, kernel-janitors, Dan Carpenter

On Wed, Feb 20, 2019 at 12:32:02PM +0000, YueHaibing wrote:
> There is a messy cast here:
> 	min_t(int, len, (int)sizeof(*item)));
> 
> min_t() should normally cast to unsigned.  It's not possible for
> "len" to be negative, but if it were then we definitely
> wouldn't want to pass negatives to read_extent_buffer().  Also there
> is an extra cast.
> 
> This patch shouldn't affect runtime, it's just a clean up.
> 
> Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: YueHaibing <yuehaibing@huawei.com>

Applied, thanks.

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

end of thread, other threads:[~2019-02-22 14:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-20  3:08 [PATCH -next] btrfs: Fix type conversion in btrfs_read_root_item YueHaibing
2019-02-20  3:38 ` Qu Wenruo
2019-02-20  5:58 ` Dan Carpenter
2019-02-20  6:10   ` Dan Carpenter
2019-02-20  9:10     ` YueHaibing
2019-02-20 12:32 ` [PATCH v2 -next] btrfs: Remove unnecessary casts " YueHaibing
2019-02-20 12:33   ` Dan Carpenter
2019-02-20 12:36   ` Qu Wenruo
2019-02-22 14:39   ` David Sterba

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