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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Please always cc linux-xfs when you're changing fs/xfs code.

*Especially* when it involves changes to ondisk structures.

On Sat, May 23, 2020 at 03:21:50PM -0500, Gustavo A. R. Silva wrote:
> On Fri, May 22, 2020 at 04:06:38PM -0700, Kees Cook wrote:
> > 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(-)
> > > 

<snip>

> > > 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);

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

> > >  	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



[Index of Archives]     [XFS Filesystem Development (older mail)]     [Linux Filesystem Development]     [Linux Audio Users]     [Yosemite Trails]     [Linux Kernel]     [Linux RAID]     [Linux SCSI]


  Powered by Linux