public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Cengiz Can <cengiz@kernel.wtf>
Cc: linux-btrfs@vger.kernel.org, Chris Mason <clm@fb.com>,
	Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>
Subject: Re: [PATCH] fs: btrfs: prevent unintentional int overflow
Date: Mon, 6 Jan 2020 16:53:28 +0100	[thread overview]
Message-ID: <20200106155328.GK3929@twin.jikos.cz> (raw)
In-Reply-To: <20200103184739.26903-1-cengiz@kernel.wtf>

On Fri, Jan 03, 2020 at 01:47:40PM -0500, Cengiz Can wrote:
> Coverity scan for 5.5.0-rc4 found a possible integer overflow in
> tree-checker.c line 364.
> 
> `prev_csum_end = (prev_item_size / csumsize) * sectorsize;`
> 
> Quoting from scan results:
> 
> ```
> CID 1456959 Unintentional integer overflow
> 
> Unintentional integer overflow (OVERFLOW_BEFORE_WIDEN) overflow_before_widen:
> Potentially overflowing expression `prev_item_size / csumsize * sectorsize`
> with type unsigned int (32 bits, unsigned) is evaluated using 32-bit
> arithmetic, and then used in a context that expects an expression of type u64.
> (64 bits, unsigned).
> ```
> 
> Added a cast to `u64` on the left hand side of the expression.
> 
> Compiles fine on x86_64_defconfig with all btrfs config flags enabled.
> 
> Signed-off-by: Cengiz Can <cengiz@kernel.wtf>
> ---
>  fs/btrfs/tree-checker.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index 97f3520b8d98..9f58f07be779 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
> @@ -361,7 +361,7 @@ static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
>  		u32 prev_item_size;
>  
>  		prev_item_size = btrfs_item_size_nr(leaf, slot - 1);
> -		prev_csum_end = (prev_item_size / csumsize) * sectorsize;
> +		prev_csum_end = (u64) (prev_item_size / csumsize) * sectorsize;

The overflow can't happen in practice. Taking generous maximum value for
an item and sectorsize (64K) and doing the math will reach nowhere the
overflow limit for 32bit type:

64K / 4 * 64K = 1G

I'm not sure if this is worth adding the cast, or mark the coverity
issue as not important.

  reply	other threads:[~2020-01-06 15:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-03 18:47 [PATCH] fs: btrfs: prevent unintentional int overflow Cengiz Can
2020-01-06 15:53 ` David Sterba [this message]
2020-01-07 15:23   ` Cengiz Can
2020-01-07 16:01     ` David Sterba
2020-01-07 19:44       ` Cengiz Can

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200106155328.GK3929@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=cengiz@kernel.wtf \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox