btrfs: Fix multiple out-of-bounds warnings
PATCH 1/1 btrfs: Fix multiple out-of-bounds warnings
Fix the following out-of-bounds warnings by using a flexible-array member *pages[] at the bottom of struct extent_buffer:
fs/btrfs/disk-io.c:225:34: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:80:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:101:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:133:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:156:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:80:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:101:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:133:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:156:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:80:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:101:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:133:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds] fs/btrfs/struct-funcs.c:156:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]
Also, make use of the struct_size() helper to properly calculate the total size of struct extent_buffer for the kmem cache allocation.
This is part of the ongoing efforts to globally enable -Warray-bounds.
The code was built with ppc64_defconfig and -Warray-bounds enabled by default in the main Makefile.
--- fs/btrfs/extent_io.c | 5 +++-- fs/btrfs/extent_io.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +3 -2
@@ -232,8 +232,9 @@ int __init extent_state_cache_init(void)int __init extent_io_init (void ) { extent_buffer_cache =kmem_cache_create ("btrfs_extent_buffer" , -sizeof (struct extent_buffer ),0 , -SLAB_MEM_SPREAD ,NULL ); +struct_size ((struct extent_buffer *)0 , pages, +INLINE_EXTENT_BUFFER_PAGES ), +0 ,SLAB_MEM_SPREAD ,NULL );if (!extent_buffer_cache)return -ENOMEM ;
fs/btrfs/extent_io.h +1 -1
@@ -94,11 +94,11 @@ struct extent_buffer {struct rw_semaphore lock; -struct page *pages[INLINE_EXTENT_BUFFER_PAGES ];struct list_head release_list;#ifdef CONFIG_BTRFS_DEBUG struct list_head leak_list;#endif +struct page *pages[]; };/*
Signature · 1 line
2.27.0
Quoted 4 lines · Gustavo A. R. Silva
Fix the following out-of-bounds warnings by using a flexible-array member *pages[] at the bottom of struct
extent_buffer:
fs/btrfs/disk-io.c:225:34: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]
The involved code looks like:
static void csum_tree_block(struct extent_buffer *buf, u8 *result)
{
struct btrfs_fs_info *fs_info = buf->fs_info;
const int num_pages = fs_info->nodesize >> PAGE_SHIFT;
...
for (i = 1; i < num_pages; i++) {
kaddr = page_address(buf->pages[i]);
crypto_shash_update(shash, kaddr, PAGE_SIZE);
}For Power case, the page size is 64K and nodesize is at most 64K for btrfs, thus num_pages will either be 0 or 1.
In that case, the for loop should never get reached, thus it's not possible to really get beyond the boundary.
To me, the real problem is we have no way to tell compiler that fs_info->nodesize is ensured to be no larger than 64K.
Although using flex array can mask the problem, but it's really masking the problem as now compiler has no idea how large the array can really be.
David still has the final say on how to fix it, but I'm really wondering is there any way to give compiler some hint about the possible value range for things like fs_info->nodesize?
(BTW, at mount time we have super block sanity checker to ensure all those basic values from on-disk super block is sane, before populating the in memory btrfs_fs_info structure, thus unless runtime memory corruption, fs_info->nodesize/sectorsize should be untouched)
Thanks,
Qu
Quoted 64 lines
fs/btrfs/struct-funcs.c:80:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:101:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:133:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:156:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:80:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:101:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:133:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:156:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:80:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:101:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:133:46: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]fs/btrfs/struct-funcs.c:156:32: warning: array subscript 1 is above array bounds of ‘struct page * const[1]’ [-Warray-bounds]Also, make use of the
struct_size()helper to properly calculate the total size of structextent_bufferfor the kmem cache allocation.This is part of the ongoing efforts to globally enable -Warray-bounds.
The code was built with
ppc64_defconfigand -Warray-bounds enabled by default in the mainMakefile.Cc Qu Wenruo <[email protected]>Cc David Sterba <[email protected]>Signed-off-by Gustavo A. R. Silva <[email protected]>--- fs/btrfs/extent_io.c | 5 +++-- fs/btrfs/extent_io.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)fs/btrfs/extent_io.c +3
-2@@ -232,8 +232,9 @@ int __init extent_state_cache_init(void)int __init extent_io_init (void ) { extent_buffer_cache =kmem_cache_create ("btrfs_extent_buffer" , -sizeof (struct extent_buffer ),0 , -SLAB_MEM_SPREAD ,NULL ); +struct_size ((struct extent_buffer *)0 , pages, +INLINE_EXTENT_BUFFER_PAGES ), +0 ,SLAB_MEM_SPREAD ,NULL );if (!extent_buffer_cache)return -ENOMEM ;fs/btrfs/extent_io.h +1
-1@@ -94,11 +94,11 @@ struct extent_buffer {struct rw_semaphore lock; -struct page *pages[INLINE_EXTENT_BUFFER_PAGES ];struct list_head release_list;#ifdef CONFIG_BTRFS_DEBUG struct list_head leak_list;#endif +struct page *pages[]; };/*
Quoted 26 lines · Qu Wenruo
Quoted 4 lines · Gustavo A. R. Silva
Fix the following out-of-bounds warnings by using a flexible-array member *pages[] at the bottom of struct
extent_buffer:
fs/btrfs/disk-io.c:225:34: warning: array subscript 1 is above array bounds of ‘struct page *[1]’ [-Warray-bounds]The involved code looks like:
static void
csum_tree_block(struct extent_buffer *buf, u8 *result){ struct btrfs_fs_info *fs_info = buf->fs_info; const int num_pages = fs_info->nodesize >> PAGE_SHIFT; ... for (i = 1; i < num_pages; i++) { kaddr = page_address(buf->pages[i]); crypto_shash_update(shash, kaddr, PAGE_SIZE); }For Power case, the page size is 64K and nodesize is at most 64K for btrfs, thus
num_pageswill either be 0 or 1.In that case, the for loop should never get reached, thus it's not possible to really get beyond the boundary.
Quoted 6 lines
To me, the real problem is we have no way to tell compiler that
fs_info->nodesizeis ensured to be no larger than 64K.Although using flex array can mask the problem, but it's really masking the problem as now compiler has no idea how large the array can really be.
Agreed, that's the problem, we'd be switching compile-time static information about the array with dynamic.
Quoted 3 lines
David still has the final say on how to fix it, but I'm really wondering is there any way to give compiler some hint about the possible value range for things like
fs_info->nodesize?
We can add some macros that are also page size dependent and evaluate to a constant that can be in turn used to optimize the loop to a single call of the loop body.
Looking at csum_tree_block we should really use the num_extent_pages helper that does the same thing but handles when nodesize >> PAGE_SIZE is zero (and returns 1).