Couple of coverity fixes
cover Couple of coverity fixes
Here are 3 patches based on the latest coverity defect scans. Mostly minor, quality-of-life type of fixes. One redundant assignment removal, 1 bounds checking in qgroup and 1 possible overflow in qgroups please merge.
Nikolay Borisov (3):
btrfs: Remove unused -EIO assignment in end_bio_extent_readpage
btrfs: Fix bound checking in qgroup_trace_new_subtree_blocks
btrfs: Avoid possible qgroup_rsv_size overflow in
btrfs_calculate_inode_block_rsv_size
fs/btrfs/extent-tree.c | 2 +- fs/btrfs/extent_io.c | 2 -- fs/btrfs/qgroup.c | 6 +++--- 3 files changed, 4 insertions(+), 6 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/3 btrfs: Remove unused -EIO assignment in end_bio_extent_readpage
In case we hit the error case for a metadata buffer in end_bio_extent_readpage then 'ret' won't really be checked before it's written again to. This means the -EIO in this case will never be checked, just remove it.
Fixes-coverity-id: 1442513
--- fs/btrfs/extent_io.c | 2 -- 1 file changed, 2 deletions(-)
fs/btrfs/extent_io.c +0 -2
@@ -2705,8 +2705,6 @@ static void end_bio_extent_readpage(struct bio *bio)if (test_and_clear_bit (EXTENT_BUFFER_READAHEAD , &eb->bflags))btree_readahead_hook (eb, -EIO ); - - ret = -EIO ; } readpage_ok:if (likely (uptodate)) {
Signature · 1 line
2.17.1
Re: [PATCH 1/3] btrfs: Remove unused -EIO assignment in end_bio_extent_readpage
Quoted 7 lines · Nikolay Borisov
In case we hit the error case for a metadata buffer in
end_bio_extent_readpagethen 'ret' won't really be checked before it's written again to. This means the-EIOin this case will never be checked, just remove it.Fixes-coverity-id: 1442513
Signed-off-by Nikolay Borisov <[email protected]>
PATCH 2/3 btrfs: Fix bound checking in qgroup_trace_new_subtree_blocks
In reply to Nikolay BorisovIf 'cur_level' is 7 then the bound checking at the top of the function will actually pass. Later on, it's possible to dereference ds_path->nodes[cur_level+1] which will be an out of bounds.
The correct check will be cur_level >= BTRFS_MAX_LEVEL - 1 .
Fixes-coverty-id: 1440918
Fixes-coverty-id: 1440911
--- fs/btrfs/qgroup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/qgroup.c +3 -3
@@ -1922,8 +1922,8 @@ static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,int i;/* Level sanity check */ -if (cur_level <0 || cur_level >=BTRFS_MAX_LEVEL || - root_level <0 || root_level >=BTRFS_MAX_LEVEL || +if (cur_level <0 || cur_level >=BTRFS_MAX_LEVEL -1 || + root_level <0 || root_level >=BTRFS_MAX_LEVEL -1 || root_level < cur_level) { btrfs_err_rl(fs_info, "%s: bad levels, cur_level=%d root_level=%d",
@@ -3482,7 +3482,7 @@ static int __btrfs_qgroup_release_data(struct inode *inode,if (free && reserved)return qgroup_free_reserved_data (inode, reserved, start, len);extent_changeset_init (&changeset); - ret =clear_record_extent_bits (&BTRFS_I (inode)->io_tree, start, + ret =clear_record_extent_bits (&BTRFS_I (inode)->io_tree, start, start + len -1 ,EXTENT_QGROUP_RESERVED , &changeset);if (ret <0 ) goto out;
Signature · 1 line
2.17.1
Re: [PATCH 2/3] btrfs: Fix bound checking in qgroup_trace_new_subtree_blocks
Quoted 10 lines · Nikolay Borisov
If '
cur_level' is 7 then the bound checking at the top of the function will actually pass. Later on, it's possible to dereference ds_path->nodes[cur_level+1] which will be an out of bounds.The correct check will be
cur_level>=BTRFS_MAX_LEVEL- 1 .Fixes-coverty-id: 1440918
Fixes-coverty-id: 1440911Fixes ea49f3e73c4b ("btrfs: qgroup: Introduce function to find all new tree blocks of reloc tree")Signed-off-by Nikolay Borisov <[email protected]>
Thanks,
Qu
Quoted 29 lines
--- fs/btrfs/qgroup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/btrfs/qgroup.c +3
-3@@ -1922,8 +1922,8 @@ static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,int i;/* Level sanity check */ -if (cur_level <0 || cur_level >=BTRFS_MAX_LEVEL || - root_level <0 || root_level >=BTRFS_MAX_LEVEL || +if (cur_level <0 || cur_level >=BTRFS_MAX_LEVEL -1 || + root_level <0 || root_level >=BTRFS_MAX_LEVEL -1 || root_level < cur_level) { btrfs_err_rl(fs_info, "%s: bad levels, cur_level=%d root_level=%d",@@ -3482,7 +3482,7 @@ static int __btrfs_qgroup_release_data(struct inode *inode,if (free && reserved)return qgroup_free_reserved_data (inode, reserved, start, len);extent_changeset_init (&changeset); - ret =clear_record_extent_bits (&BTRFS_I (inode)->io_tree, start, + ret =clear_record_extent_bits (&BTRFS_I (inode)->io_tree, start, start + len -1 ,EXTENT_QGROUP_RESERVED , &changeset);if (ret <0 ) goto out;
PATCH 3/3 btrfs: Avoid possible qgroup_rsv_size overflow in btrfs_calculate_inode_block_rsv_size
In reply to Nikolay Borisovqgroup_rsv_size is calculated as the product of outstanding_extent * fs_info->nodesize. The product is calculated with 32 bith precision since both variables are defined as u32. Yet qgroup_rsv_size expects a 64 bit result.
Avoid possible multiplication overflow by casting outstanding_extent to u64.
Fixes-coverity-id: 1435101ff6bc37eb7f6 ("btrfs: qgroup: Use independent and accurate per inode qgroup rsv")
--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
@@ -6173,7 +6173,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,* * This is overestimating in most cases. */ - qgroup_rsv_size = outstanding_extents * fs_info->nodesize; + qgroup_rsv_size = (u64 ) outstanding_extents * fs_info->nodesize;spin_lock (&block_rsv->lock); block_rsv->size = reserve_size;
Signature · 1 line
2.17.1
Re: [PATCH 3/3] btrfs: Avoid possible qgroup_rsv_size overflow in btrfs_calculate_inode_block_rsv_size
Quoted 21 lines · Nikolay Borisov
qgroup_rsv_sizeis calculated as the product ofoutstanding_extent*fs_info->nodesize. The product is calculated with 32 bith precision since both variables are defined as u32. Yetqgroup_rsv_sizeexpects a 64 bit result.Avoid possible multiplication overflow by casting
outstanding_extentto u64.Fixes-coverity-id: 1435101
ff6bc37eb7f6("btrfs: qgroup: Use independent and accurate per inode qgroup rsv")Signed-off-by Nikolay Borisov <[email protected]>--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/extent-tree.c +0
-0
fs/btrfs/extent-tree.c static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info, · line 6176
Quoted 5 lines
fs/btrfs/extent-tree.c +1
-1@@ -6173,7 +6173,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,* * This is overestimating in most cases. */ - qgroup_rsv_size = outstanding_extents * fs_info->nodesize; + qgroup_rsv_size = (u64 ) outstanding_extents * fs_info->nodesize;
I'm a little uncertain about what's the proper way to do a u32 * u32 and get a u64 in C.
For division we have DIV macro but not for multiple.
Thanks,
Qu
Quoted 4 lines
fs/btrfs/extent-tree.c +0
-0spin_lock (&block_rsv->lock); block_rsv->size = reserve_size;
Re: [PATCH 3/3] btrfs: Avoid possible qgroup_rsv_size overflow in btrfs_calculate_inode_block_rsv_size
Quoted 33 lines · Qu Wenruo
Quoted 21 lines · Nikolay Borisov
qgroup_rsv_sizeis calculated as the product ofoutstanding_extent*fs_info->nodesize. The product is calculated with 32 bith precision since both variables are defined as u32. Yetqgroup_rsv_sizeexpects a 64 bit result.Avoid possible multiplication overflow by casting
outstanding_extentto u64.Fixes-coverity-id: 1435101
ff6bc37eb7f6("btrfs: qgroup: Use independent and accurate per inode qgroup rsv")Signed-off-by Nikolay Borisov <[email protected]>--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/extent-tree.c +0
-0fs/btrfs/extent-tree.c static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info, · line 6176
Quoted 5 lines
fs/btrfs/extent-tree.c +1
-1@@ -6173,7 +6173,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,* * This is overestimating in most cases. */ - qgroup_rsv_size = outstanding_extents * fs_info->nodesize; + qgroup_rsv_size = (u64 ) outstanding_extents * fs_info->nodesize;I'm a little uncertain about what's the proper way to do a u32 * u32 and get a u64 in C.
Quoted 1 line
For division we have DIV macro but not for multiple.
You should definitely read this:
In particular the 2nd "Noncompliant Code Example
" described there is exactly the case you have in this code.
Quoted 9 lines
Thanks,
QuQuoted 4 lines
fs/btrfs/extent-tree.c +0
-0spin_lock (&block_rsv->lock); block_rsv->size = reserve_size;
Qu Wenruo
Reviewed-byRe: [PATCH 3/3] btrfs: Avoid possible qgroup_rsv_size overflow in btrfs_calculate_inode_block_rsv_size
Quoted 36 lines · Nikolay Borisov
Quoted 33 lines · Qu Wenruo
Quoted 21 lines · Nikolay Borisov
qgroup_rsv_sizeis calculated as the product ofoutstanding_extent*fs_info->nodesize. The product is calculated with 32 bith precision since both variables are defined as u32. Yetqgroup_rsv_sizeexpects a 64 bit result.Avoid possible multiplication overflow by casting
outstanding_extentto u64.Fixes-coverity-id: 1435101
ff6bc37eb7f6("btrfs: qgroup: Use independent and accurate per inode qgroup rsv")Signed-off-by Nikolay Borisov <[email protected]>--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/extent-tree.c +0
-0fs/btrfs/extent-tree.c static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info, · line 6176
Quoted 5 lines
fs/btrfs/extent-tree.c +1
-1@@ -6173,7 +6173,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,* * This is overestimating in most cases. */ - qgroup_rsv_size = outstanding_extents * fs_info->nodesize; + qgroup_rsv_size = (u64 ) outstanding_extents * fs_info->nodesize;I'm a little uncertain about what's the proper way to do a u32 * u32 and get a u64 in C.
Quoted 8 lines
Quoted 1 line
For division we have DIV macro but not for multiple.
You should definitely read this:
In particular the 2nd "Noncompliant Code Example
" described there is exactly the case you have in this code.
Thanks,
Qu
Quoted 10 lines
Quoted 10 lines
Thanks,
QuQuoted 4 lines
fs/btrfs/extent-tree.c +0
-0spin_lock (&block_rsv->lock); block_rsv->size = reserve_size;
Re: [PATCH 3/3] btrfs: Avoid possible qgroup_rsv_size overflow in btrfs_calculate_inode_block_rsv_size
In reply to Nikolay BorisovQuoted 10 lines · Nikolay Borisov
qgroup_rsv_sizeis calculated as the product ofoutstanding_extent*fs_info->nodesize. The product is calculated with 32 bith precision since both variables are defined as u32. Yetqgroup_rsv_sizeexpects a 64 bit result.Avoid possible multiplication overflow by casting
outstanding_extentto u64.Fixes-coverity-id: 1435101
ff6bc37eb7f6("btrfs: qgroup: Use independent and accurate per inode qgroup rsv")
I've added a note about the worst case when the overflow would happen, which is 65536 outstanding extents with 64K nodesize. Unlikely to happen.
Quoted 9 lines · Nikolay Borisov
Here are 3 patches based on the latest coverity defect scans. Mostly minor, quality-of-life type of fixes. One redundant assignment removal, 1 bounds checking in qgroup and 1 possible overflow in qgroups please merge.
Nikolay Borisov (3):
btrfs: Remove unused-EIOassignment inend_bio_extent_readpage
btrfs: Fix bound checking inqgroup_trace_new_subtree_blocks
btrfs: Avoid possibleqgroup_rsv_sizeoverflow in
btrfs_calculate_inode_block_rsv_size
Added to misc-next, thanks.