xfs: Replace one-element arrays with flexible-array members
Quoted 123 lines · Gustavo A. R. Silva
The current codebase makes use of one-element arrays in the following form:
struct something { int length; u8 data[1]; };struct something *instance;
instance =
kmalloc(sizeof(*instance) + size, GFP_KERNEL);instance->length= size;memcpy(instance->data, source, size);but the preferred mechanism to declare variable-length types such as these ones is a flexible array member[1][2], introduced in C99:
struct foo { int stuff; struct boo array[]; };By making use of the mechanism above, we will get a compiler warning in case the flexible array does not occur last in the structure, which will help us prevent some kind of undefined behavior bugs from being inadvertently introduced[3] to the codebase from now on. So, replace the one-element array with a flexible-array member.
This issue was found with the help of Coccinelle and audited _manually_.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit76497732932f("cxgb3/l2t: Fix undefined behaviour")Signed-off-by Gustavo A. R. Silva <[email protected]>--- fs/xfs/libxfs/xfs_log_format.h | 12 ++++++------ fs/xfs/xfs_extfree_item.c | 18 +++++++++--------- fs/xfs/xfs_ondisk.h | 8 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-)fs/xfs/libxfs/xfs_log_format.h +6
-6@@ -571,7 +571,7 @@ typedef struct xfs_efi_log_format {uint16_t efi_size;/* size of this item */ uint32_t efi_nextents;/* # extents to free */ uint64_t efi_id;/* efi identifier */ -xfs_extent_t efi_extents[1 ];/* array of extents to free */ +xfs_extent_t efi_extents[];/* array of extents to free */ } xfs_efi_log_format_t; typedef struct xfs_efi_log_format_32 {@@ -579,7 +579,7 @@ typedef struct xfs_efi_log_format_32 {uint16_t efi_size;/* size of this item */ uint32_t efi_nextents;/* # extents to free */ uint64_t efi_id;/* efi identifier */ -xfs_extent_32_t efi_extents[1 ];/* array of extents to free */ +xfs_extent_32_t efi_extents[];/* array of extents to free */ } __attribute__((packed)) xfs_efi_log_format_32_t; typedef struct xfs_efi_log_format_64 {@@ -587,7 +587,7 @@ typedef struct xfs_efi_log_format_64 {uint16_t efi_size;/* size of this item */ uint32_t efi_nextents;/* # extents to free */ uint64_t efi_id;/* efi identifier */ -xfs_extent_64_t efi_extents[1 ];/* array of extents to free */ +xfs_extent_64_t efi_extents[];/* array of extents to free */ } xfs_efi_log_format_64_t;/* @@ -600,7 +600,7 @@ typedef struct xfs_efd_log_format {uint16_t efd_size;/* size of this item */ uint32_t efd_nextents;/* # of extents freed */ uint64_t efd_efi_id;/* id of corresponding efi */ -xfs_extent_t efd_extents[1 ];/* array of extents freed */ +xfs_extent_t efd_extents[];/* array of extents freed */ } xfs_efd_log_format_t; typedef struct xfs_efd_log_format_32 {@@ -608,7 +608,7 @@ typedef struct xfs_efd_log_format_32 {uint16_t efd_size;/* size of this item */ uint32_t efd_nextents;/* # of extents freed */ uint64_t efd_efi_id;/* id of corresponding efi */ -xfs_extent_32_t efd_extents[1 ];/* array of extents freed */ +xfs_extent_32_t efd_extents[];/* array of extents freed */ } __attribute__((packed)) xfs_efd_log_format_32_t; typedef struct xfs_efd_log_format_64 {@@ -616,7 +616,7 @@ typedef struct xfs_efd_log_format_64 {uint16_t efd_size;/* size of this item */ uint32_t efd_nextents;/* # of extents freed */ uint64_t efd_efi_id;/* id of corresponding efi */ -xfs_extent_64_t efd_extents[1 ];/* array of extents freed */ +xfs_extent_64_t efd_extents[];/* array of extents freed */ } xfs_efd_log_format_64_t;/* fs/xfs/xfs_extfree_item.c +2
-2@@ -74,7 +74,7 @@ xfs_efi_item_sizeof( struct xfs_efi_log_item *efip) {return sizeof (struct xfs_efi_log_format ) + - (efip->efi_format.efi_nextents -1 ) *sizeof (xfs_extent_t); + efip->efi_format.efi_nextents *sizeof (xfs_extent_t); }STATIC void@@ -158,7 +158,7 @@ xfs_efi_init(ASSERT (nextents >0 );if (nextents >XFS_EFI_MAX_FAST_EXTENTS ) { size = (uint)(sizeof (struct xfs_efi_log_item ) + - ((nextents -1 ) *sizeof (xfs_extent_t))); + nextents *sizeof (xfs_extent_t)); efip =kmem_zalloc (size,0 ); }else { efip =kmem_zone_zalloc (xfs_efi_zone,0 );
fs/xfs/xfs_extfree_item.c xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) · line 189
Quoted 5 lines
fs/xfs/xfs_extfree_item.c +1
-1@@ -186,11 +186,11 @@ xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;uint i;uint len =sizeof (xfs_efi_log_format_t) + - (src_efi_fmt->efi_nextents -1 ) *sizeof (xfs_extent_t); + src_efi_fmt->efi_nextents *sizeof (xfs_extent_t);
I think these should all be using struct_size(), yes?
-Kees
Quoted 61 lines
fs/xfs/xfs_extfree_item.c +6
-6uint len32 =sizeof (xfs_efi_log_format_32_t) + - (src_efi_fmt->efi_nextents -1 ) *sizeof (xfs_extent_32_t); + src_efi_fmt->efi_nextents *sizeof (xfs_extent_32_t);uint len64 =sizeof (xfs_efi_log_format_64_t) + - (src_efi_fmt->efi_nextents -1 ) *sizeof (xfs_extent_64_t); + src_efi_fmt->efi_nextents *sizeof (xfs_extent_64_t);if (buf->i_len == len) {memcpy ((char *)dst_efi_fmt, (char *)src_efi_fmt, len);@@ -253,7 +253,7 @@ xfs_efd_item_sizeof( struct xfs_efd_log_item *efdp) {return sizeof (xfs_efd_log_format_t) + - (efdp->efd_format.efd_nextents -1 ) *sizeof (xfs_extent_t); + efdp->efd_format.efd_nextents *sizeof (xfs_extent_t); }STATIC void@@ -329,7 +329,7 @@ xfs_trans_get_efd(if (nextents >XFS_EFD_MAX_FAST_EXTENTS ) { efdp =kmem_zalloc (sizeof (struct xfs_efd_log_item ) + - (nextents -1 ) *sizeof (struct xfs_extent ), + nextents *sizeof (struct xfs_extent ),0 ); }else { efdp =kmem_zone_zalloc (xfs_efd_zone,0 );@@ -716,9 +716,9 @@ xlog_recover_efd_commit_pass2( efd_formatp = item->ri_buf[0 ].i_addr;ASSERT ((item->ri_buf[0 ].i_len == (sizeof (xfs_efd_log_format_32_t) + - ((efd_formatp->efd_nextents -1 ) *sizeof (xfs_extent_32_t)))) || + efd_formatp->efd_nextents *sizeof (xfs_extent_32_t))) || (item->ri_buf[0 ].i_len == (sizeof (xfs_efd_log_format_64_t) + - ((efd_formatp->efd_nextents -1 ) *sizeof (xfs_extent_64_t))))); + efd_formatp->efd_nextents *sizeof (xfs_extent_64_t))));xlog_recover_release_intent (log,XFS_LI_EFI , efd_formatp->efd_efi_id);return 0 ;fs/xfs/xfs_ondisk.h +4
-4@@ -113,10 +113,10 @@ xfs_check_ondisk_structs(void)/* log structures */ XFS_CHECK_STRUCT_SIZE(struct xfs_buf_log_format, 88); XFS_CHECK_STRUCT_SIZE(struct xfs_dq_logformat, 24); - XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_32, 28); - XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_64, 32); - XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_32, 28); - XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_64, 32); + XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_32, 16); + XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_64, 16); + XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_32, 16); + XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_64, 16); XFS_CHECK_STRUCT_SIZE(struct xfs_extent_32, 12); XFS_CHECK_STRUCT_SIZE(struct xfs_extent_64, 16); XFS_CHECK_STRUCT_SIZE(struct xfs_log_dinode, 176);Signature · 1 line
2.26.2
Signature · 1 line
Kees Cook
Please always cc linux-xfs when you're changing fs/xfs code.
*Especially* when it involves changes to ondisk structures.
Quoted 44 lines · Gustavo A. R. Silva
Quoted 43 lines · Kees Cook
Quoted 42 lines · Gustavo A. R. Silva
The current codebase makes use of one-element arrays in the following form:
struct something { int length; u8 data[1]; };struct something *instance;
instance =
kmalloc(sizeof(*instance) + size, GFP_KERNEL);instance->length= size;memcpy(instance->data, source, size);but the preferred mechanism to declare variable-length types such as these ones is a flexible array member[1][2], introduced in C99:
struct foo { int stuff; struct boo array[]; };By making use of the mechanism above, we will get a compiler warning in case the flexible array does not occur last in the structure, which will help us prevent some kind of undefined behavior bugs from being inadvertently introduced[3] to the codebase from now on. So, replace the one-element array with a flexible-array member.
This issue was found with the help of Coccinelle and audited _manually_.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit76497732932f("cxgb3/l2t: Fix undefined behaviour")Signed-off-by Gustavo A. R. Silva <[email protected]>--- fs/xfs/libxfs/xfs_log_format.h | 12 ++++++------ fs/xfs/xfs_extfree_item.c | 18 +++++++++--------- fs/xfs/xfs_ondisk.h | 8 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-)
<snip>
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
fs/xfs/xfs_ondisk.h +0
-0@@ -113,10 +113,10 @@ xfs_check_ondisk_structs(void)/* log structures */ XFS_CHECK_STRUCT_SIZE(struct xfs_buf_log_format, 88); XFS_CHECK_STRUCT_SIZE(struct xfs_dq_logformat, 24);
Quoted 8 lines
Quoted 8 lines
fs/xfs/xfs_ondisk.h xfs_check_ondisk_structs(void) · line 119
Quoted 8 lines
fs/xfs/xfs_ondisk.h +4
-4- XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_32, 28); - XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_64, 32); - XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_32, 28); - XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_64, 32); + XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_32, 16); + XFS_CHECK_STRUCT_SIZE(struct xfs_efd_log_format_64, 16); + XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_32, 16); + XFS_CHECK_STRUCT_SIZE(struct xfs_efi_log_format_64, 16);
Seeing as you're changing ondisk structure size checks, I gotta ask:
You /did/ run fstests before and after to make sure that the log recovery tests still work, right?
--D
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
fs/xfs/xfs_ondisk.h +0
-0XFS_CHECK_STRUCT_SIZE(struct xfs_extent_32, 12); XFS_CHECK_STRUCT_SIZE(struct xfs_extent_64, 16); XFS_CHECK_STRUCT_SIZE(struct xfs_log_dinode, 176);Signature · 1 line
2.26.2Signature · 1 line
Kees Cook
Quoted 3 lines · Darrick J. Wong
Please always cc linux-xfs when you're changing fs/xfs code.
*Especially* when it involves changes to ondisk structures.
I can't find this patch in lkml or -fsdevel on lore.kernel.org, so I have no idea where this patch even came from. Which means I can't even check what the structure change is because it wasn't quoted in full.
So, NACK on this until the entire patch is resent to linux-xfs and run through all the shutdown and error recovery tests in fstests....
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Hi all,
Quoted 10 lines · Dave Chinner
Quoted 3 lines · Darrick J. Wong
Please always cc linux-xfs when you're changing fs/xfs code.
*Especially* when it involves changes to ondisk structures.
I can't find this patch in lkml or -fsdevel on lore.kernel.org, so I have no idea where this patch even came from. Which means I can't even check what the structure change is because it wasn't quoted in full.
Yeah, there was a bug in one of my scripts that was messing up with some email addresses. I just fixed it up.
Quoted 3 lines
So, NACK on this until the entire patch is resent to linux-xfs and run through all the shutdown and error recovery tests in fstests....
I've added the patch[1] to my testing/xfs branch. I'll wait for Kernel test robot <[email protected]> to help me test it.
Thanks
--
Gustavo
[1] https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?h=testing/xfs