Re: [PATCH] xfs: Replace one-element arrays with flexible-array members

From: Kees Cook
Date: Fri May 22 2020 - 19:06:42 EST


On Fri, May 22, 2020 at 04:55:42PM -0500, Gustavo A. R. Silva wrote:
> 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] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")
>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@xxxxxxxxxx>
> ---
> 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(-)
>
> diff --git a/fs/xfs/libxfs/xfs_log_format.h b/fs/xfs/libxfs/xfs_log_format.h
> index e3400c9c71cdb..2864ec47eee5c 100644
> --- a/fs/xfs/libxfs/xfs_log_format.h
> +++ b/fs/xfs/libxfs/xfs_log_format.h
> @@ -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;
>
> /*
> diff --git a/fs/xfs/xfs_extfree_item.c b/fs/xfs/xfs_extfree_item.c
> index b9c333bae0a12..faf1b780fe8c4 100644
> --- a/fs/xfs/xfs_extfree_item.c
> +++ b/fs/xfs/xfs_extfree_item.c
> @@ -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);
> @@ -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

> uint 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;
> diff --git a/fs/xfs/xfs_ondisk.h b/fs/xfs/xfs_ondisk.h
> index 5f04d8a5ab2a9..ceba638fd99ce 100644
> --- a/fs/xfs/xfs_ondisk.h
> +++ b/fs/xfs/xfs_ondisk.h
> @@ -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);
> --
> 2.26.2
>

--
Kees Cook