fs: btrfs: coverity fixes
cover fs: btrfs: coverity fixes
Most of the coverity reports are at least a good hint to us to improve the code style.
Although there are some false alerts, but it's really hard to teach coverity to be a real human (yet), thus fix the warning even it's just false alerts.
Qu Wenruo (4):
fs: btrfs: inode: handle uninitialized type before returning it
fs: btrfs: volumes: prevent overflow for multiplying
fs: btrfs: initialize @ret to 0 to prevent uninitialized return value
fs: btrfs: initialize @ii in show_dir() to make coverity happy
fs/btrfs/btrfs.c | 4 ++-- fs/btrfs/inode.c | 6 +++++- fs/btrfs/volumes.c | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-)
Signature · 1 line
2.29.1
PATCH 1/4 fs: btrfs: inode: handle uninitialized type before returning it
In btrfs_lookup_path() the local variable @type should always be updated after we hit any file/dir.
But if @filename is NULL from the very beginning, then we don't initialize it and return it directly.
To prevent such problem from happening, we initialize @type to BTRFS_FT_UNKNOWN.
For normal execution route, it will get updated for each filename we resolved.
Buf if we didn't find any path, we check if the type is still FT_UNKNOWN and ret == 0. If true we know there is something wrong, just return -EUCLEAN to inform the caller.
--- fs/btrfs/inode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +5 -1
@@ -251,7 +251,7 @@ int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,const char *cur = filename;u64 next_ino;u8 next_type; -u8 type; +u8 type =BTRFS_FT_UNKNOWN ;int len;int ret =0 ;
@@ -335,6 +335,10 @@ next: cur += len; } +/* We haven't found anything, but still get no error? */ +if (type ==BTRFS_FT_UNKNOWN && !ret) + ret = -EUCLEAN ; +if (!ret) { *root_ret = root; *ino_ret = ino;
Signature · 1 line
2.29.1
PATCH 2/4 fs: btrfs: volumes: prevent overflow for multiplying
In reply to Qu WenruoIn __btrfs_map_block() we do a int * int and assign it to u64. This is not safe as the result (int * int) is still evaluated as (int) thus it can overflow.
Convert one of the multiplier to u64 to prevent such problem.
In real world, this should not cause problem as we have device number limit thus it won't go beyond 4G for a single stripe.
But it's harder to teach coverity about all these hidden limits, so just fix the possible overflow.
--- fs/btrfs/volumes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/volumes.c +2 -2
@@ -1030,7 +1030,7 @@ again:*/ stripe_nr = stripe_nr / map->stripe_len; - stripe_offset = stripe_nr * map->stripe_len; + stripe_offset = stripe_nr * (u64 )map->stripe_len;BUG_ON (offset < stripe_offset);/* stripe_offset is the offset of this block in its stripe*/
@@ -1103,7 +1103,7 @@ again: rot = stripe_nr % map->num_stripes;/* Fill in the logical address of each stripe */ - tmp = stripe_nr *nr_data_stripes (map); + tmp = (u64 )stripe_nr *nr_data_stripes (map);for (i =0 ; i <nr_data_stripes (map); i++) raid_map[(i+rot) % map->num_stripes] =
Signature · 1 line
2.29.1
PATCH 3/4 fs: btrfs: initialize @ret to 0 to prevent uninitialized return value
In reply to Qu WenruoIn show_dir() if we hit a ROOT_ITEM, we can exit with uninitialized @ret.
Fix it by initializing it to 0.
--- fs/btrfs/btrfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/btrfs.c +1 -1
@@ -36,7 +36,7 @@ static int show_dir(struct btrfs_root *root, struct extent_buffer *eb,char *target =NULL ;char filetime[32 ];time_t mtime; -int ret; +int ret =0 ;btrfs_dir_item_key_to_cpu (eb, di, &key);
Signature · 1 line
2.29.1
PATCH 4/4 fs: btrfs: initialize @ii in show_dir() to make coverity happy
In reply to Qu WenruoIn show_dir() if we hit file type FT_CHRDEV or FT_BLKDEV, we expect an BTRFS_INODE_ITEM_KEY, and for that case, we should have @ii filled with data read from disk.
We even have ASSERT() for this purpose, but unfortunately coverity can't understand the ASSERT() nor if we get key type BTRFS_INODE_ITEM_KEY, previous if() branch must go to the read_extent_buffer() branch to fill the @ii.
So to make coverity happy, just initialize the variable @ii to all zero.
--- fs/btrfs/btrfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/btrfs.c +1 -1
@@ -19,7 +19,7 @@ static int show_dir(struct btrfs_root *root, struct extent_buffer *eb, struct btrfs_dir_item *di) {struct btrfs_fs_info *fs_info = root->fs_info; -struct btrfs_inode_item ii; +struct btrfs_inode_item ii = {0 };struct btrfs_key key;static const char * dir_item_str[] = { [BTRFS_FT_REG_FILE ] ="FILE" ,
Signature · 1 line
2.29.1
PATCH 1/4 fs: btrfs: inode: handle uninitialized type before returning it
In reply to Qu WenruoPATCH 2/4 fs: btrfs: volumes: prevent overflow for multiplying
In reply to Qu WenruoQuoted 2 lines · On Sat, 31 Oct 2020 09:07:50 +0800 Qu Wenruo <[email protected]> wrote:
In real world, this should not cause problem as we have device number limit thus it won't go beyond 4G for a single stripe.
So we can't run into this overflow in U-Boot because only one device is supported? But in Linux we can run into this issue?
PATCH 3/4 fs: btrfs: initialize @ret to 0 to prevent uninitialized return value
In reply to Qu WenruoPATCH 4/4 fs: btrfs: initialize @ii in show_dir() to make coverity happy
In reply to Qu WenruoQuoted 10 lines · On Sat, 31 Oct 2020 09:07:52 +0800 Qu Wenruo <[email protected]> wrote:
In
show_dir()if we hit file typeFT_CHRDEVorFT_BLKDEV, we expect anBTRFS_INODE_ITEM_KEY, and for that case, we should have @ii filled with data read from disk.We even have
ASSERT()for this purpose, but unfortunately coverity can't understand theASSERT()nor if we get key typeBTRFS_INODE_ITEM_KEY, previousif()branch must go to theread_extent_buffer()branch to fill the @ii.So to make coverity happy, just initialize the variable @ii to all zero.
WTF. If ASSERT excludes this from happening, coverity should understand this. We live in 2020. I don't much like the idea to do things just to get coverity happy if it can't understand and infer from things like ASSERT.
PATCH 2/4 fs: btrfs: volumes: prevent overflow for multiplying
In reply to Marek BehunQuoted 9 lines · Marek Behun
Quoted 2 lines · On Sat, 31 Oct 2020 09:07:50 +0800 Qu Wenruo <[email protected]> wrote:
In real world, this should not cause problem as we have device number limit thus it won't go beyond 4G for a single stripe.
So we can't run into this overflow in U-Boot because only one device is supported? But in Linux we can run into this issue?
In kernel, we have device number check, IIRC for default nodesize is just several donzens of devices.
And since each stripe is only 64K fixed in btrfs, you can see it won't go beyond 4G even in kernel.
Thanks,
Qu
PATCH 4/4 fs: btrfs: initialize @ii in show_dir() to make coverity happy
In reply to Marek BehunQuoted 12 lines · Marek Behun
Quoted 9 lines · On Sat, 31 Oct 2020 09:07:52 +0800 Qu Wenruo <[email protected]> wrote:
In
show_dir()if we hit file typeFT_CHRDEVorFT_BLKDEV, we expect anBTRFS_INODE_ITEM_KEY, and for that case, we should have @ii filled with data read from disk.We even have
ASSERT()for this purpose, but unfortunately coverity can't understand theASSERT()nor if we get key typeBTRFS_INODE_ITEM_KEY, previousif()branch must go to theread_extent_buffer()branch to fill the @ii.
Quoted 7 lines
Quoted 1 line
So to make coverity happy, just initialize the variable @ii to all zero.
WTF. If ASSERT excludes this from happening, coverity should understand this. We live in 2020. I don't much like the idea to do things just to get coverity happy if it can't understand and infer from things like ASSERT.
It's not ASSERT(), it's the previous if () branch. The ASSERT() is just to ensure we didn't miss anything.
The previous if () looks like this:
if (key.type == BTRFS_ROOT_ITEM_KEY) {
/* @ii is not touched */
/* Further more, in this branch type should only be FT_DIR */
} else {
/* This includes the key.type == INODE_ITEM_KEY case */
read_extent_buffer( &ii ); /* Here we fill &ii */
}Then in the offending code we have:
if (type == BTRFS_FT_CHRDEV || type == BTRFS_FT_BLKDEV) {
ASSERT(key.type == BTRFS_INODE_ITEM_KEY);
/*
* If the type is above type, we must have key type INODE_ITEM
* Thus the ii must be filled.
*/
printf("%4llu,%5llu ", btrfs_stack_inode_rdev(&ii) >> 20,
btrfs_stack_inode_rdev(&ii) & 0xfffff);
} else {
if (key.type == BTRFS_INODE_ITEM_KEY)
printf("%10llu ", btrfs_stack_inode_size(&ii));
else
printf("%10llu ", 0ULL);
}Thus I really tend to believe it's just a bug in coverity.
All locations accessing @ii all have its key.type checked to ensure it get filled in the first place.
Thanks,
Qu
Re: [PATCH 2/4] fs: btrfs: volumes: prevent overflow for multiplying
In reply to Qu WenruoQuoted 9 lines · Qu Wenruo
Quoted 6 lines · Marek Behun
Quoted 2 lines · On Sat, 31 Oct 2020 09:07:50 +0800 Qu Wenruo <[email protected]> wrote:
In real world, this should not cause problem as we have device number limit thus it won't go beyond 4G for a single stripe.
Quoted 7 lines
Quoted 3 lines
So we can't run into this overflow in U-Boot because only one device is supported? But in Linux we can run into this issue?
In kernel, we have device number check, IIRC for default nodesize is just several donzens of devices.
And since each stripe is only 64K fixed in btrfs, you can see it won't go beyond 4G even in kernel.
Sorry, the 64K stripe_len is only for RAID56, and for kernel, we always use u64 for stripe_len, thus we won't hit the problem.
The problem is in btrfs-progs, where the code comes from, we use int, other than u64.
Thanks for the hint for the root cause, would related values to be u64 for both btrfs-progs and u-boot to follow the kernel scheme.
Thanks,
Qu
Quoted 4 lines
Thanks,
Qu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201102/98fc7936/attachment.sig>
PATCH 4/4 fs: btrfs: initialize @ii in show_dir() to make coverity happy
In reply to Qu WenruoQuoted 2 lines · On Mon, 2 Nov 2020 08:27:16 +0800 Qu Wenruo <[email protected]> wrote:
Thus I really tend to believe it's just a bug in coverity.
All locations accessing @ii all have its key.type checked to ensure it get filled in the first place.
If this is a bug in coverity, we should fix coverity, not add extra code to U-Boot, even if it is just one instruction. That simply stinks the same way like when systemd crashed when "debug" parameter was present in /proc/cmdline, and they sent a patch to kernel which removed the "debug" parameter, instead of fixing systemd. IMO.
Marek
PATCH 4/4 fs: btrfs: initialize @ii in show_dir() to make coverity happy
Quoted 11 lines · Marek Behun
Quoted 2 lines · On Mon, 2 Nov 2020 08:27:16 +0800 Qu Wenruo <[email protected]> wrote:
Thus I really tend to believe it's just a bug in coverity.
All locations accessing @ii all have its key.type checked to ensure it get filled in the first place.If this is a bug in coverity, we should fix coverity, not add extra code to U-Boot, even if it is just one instruction. That simply stinks the same way like when systemd crashed when "debug" parameter was present in
/proc/cmdline, and they sent a patch to kernel which removed the "debug" parameter, instead of fixing systemd. IMO.
Yep, you're right.
Please discard this patch.
Thanks,
Qu
Quoted 3 lines
Marek
PATCH 4/4 fs: btrfs: initialize @ii in show_dir() to make coverity happy
In reply to Marek BehunQuoted 11 lines · Marek Behun
Quoted 2 lines · On Mon, 2 Nov 2020 08:27:16 +0800 Qu Wenruo <[email protected]> wrote:
Thus I really tend to believe it's just a bug in coverity.
All locations accessing @ii all have its key.type checked to ensure it get filled in the first place.If this is a bug in coverity, we should fix coverity, not add extra code to U-Boot, even if it is just one instruction. That simply stinks the same way like when systemd crashed when "debug" parameter was present in
/proc/cmdline, and they sent a patch to kernel which removed the "debug" parameter, instead of fixing systemd. IMO.
To be clear, I am quite happy to mark as intentional any issues that are flagged by Coverity but, well, intentional and not a problem. We may also benefit from adding a "modeling" file to help Coverity know when some cases of issues are handled by us already.
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201102/5afd412b/attachment.sig>
PATCH 1/4 fs: btrfs: inode: handle uninitialized type before returning it
In reply to Qu WenruoQuoted 14 lines · Qu Wenruo
In
btrfs_lookup_path()the local variable @type should always be updated after we hit any file/dir.But if @filename is NULL from the very beginning, then we don't initialize it and return it directly.
To prevent such problem from happening, we initialize @type to
BTRFS_FT_UNKNOWN.
For normal execution route, it will get updated for each filename we resolved.
Buf if we didn't find any path, we check if the type is stillFT_UNKNOWNand ret == 0. If true we know there is something wrong, just return-EUCLEANto inform the caller.
Quoted 3 lines
Reported-by Coverity CID 312958Signed-off-by Qu Wenruo <[email protected]>Reviewed-by Marek Beh?n <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201119/f0faa784/attachment.sig>
PATCH 3/4 fs: btrfs: initialize @ret to 0 to prevent uninitialized return value
In reply to Qu WenruoQuoted 8 lines · Qu Wenruo
In
show_dir()if we hit aROOT_ITEM, we can exit with uninitialized @ret.Fix it by initializing it to 0.
Reported-by Coverity CID 312955Signed-off-by Qu Wenruo <[email protected]>Reviewed-by Marek Beh?n <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201119/48518516/attachment.sig>
PATCH 2/4 fs: btrfs: volumes: prevent overflow for multiplying
In reply to Qu WenruoQuoted 9 lines · Qu Wenruo
In
__btrfs_map_block()we do a int * int and assign it to u64. This is not safe as the result (int * int) is still evaluated as (int) thus it can overflow.Convert one of the multiplier to u64 to prevent such problem.
In real world, this should not cause problem as we have device number limit thus it won't go beyond 4G for a single stripe.
Quoted 6 lines
But it's harder to teach coverity about all these hidden limits, so just fix the possible overflow.
Reported-by Coverity CID 312957Reported-by Coverity CID 312948Signed-off-by Qu Wenruo <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 659 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210120/b7766dfc/attachment-0001.sig>