libxfs: Redefine 1-element arrays as flexible arrays
PATCH 1/1 libxfs: Redefine 1-element arrays as flexible arrays
To allow for code bases that include libxfs (e.g. the Linux kernel) and build with strict flexible array handling (-fstrict-flex-arrays=3), FORTIFY_SOURCE, and/or UBSAN bounds checking, redefine the remaining 1-element trailing arrays as true flexible arrays, but without changing their structure sizes. This is done via a union to retain a single element (named "legacy_padding"). As not all distro headers may yet have the UAPI stddef.h __DECLARE_FLEX_ARRAY macro, include it explicitly in platform_defs.h.in.
Addresses the warnings being seen under Linux:
https://lore.kernel.org/all/20230710170243.GF11456@frogsfrogsfrogs
--- include/platform_defs.h.in | 18 ++++++++++++++++++ libxfs/xfs_da_format.h | 38 ++++++++++++++++++++++++++++---------- libxfs/xfs_fs.h | 12 ++++++++++-- 3 files changed, 56 insertions(+), 12 deletions(-)
include/platform_defs.h.in +18 -0
@@ -134,6 +134,24 @@ static inline size_t __ab_c_size(size_t a, size_t b, size_t c) # define fallthrough do {} while (0) /* fallthrough */ #endif +#ifndef __DECLARE_FLEX_ARRAY +/** + * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union + * + * @type: The type of each flexible array element + * @name: The name of the flexible array member + * + * In order to have a flexible array member in a union or alone in a + * struct, it needs to be wrapped in an anonymous struct with at least 1 + * named member, but that member can be empty. + */ +#define __DECLARE_FLEX_ARRAY(type, name) \ + struct { \ + struct { } __empty_ ## name; \ + type name[]; \ + } +#endif + /* Only needed for the kernel. */ #define __init
libxfs/xfs_da_format.h +28 -10
@@ -580,18 +580,23 @@ xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)/* * Entries are packed toward the top as tight as possible. */ +struct xfs_attr_sf_entry { +uint8_t namelen;/* actual length of name (no NULL) */ +uint8_t valuelen;/* actual length of value (no NULL) */ +uint8_t flags;/* flags bits (see xfs_attr_leaf.h) */ +uint8_t nameval[];/* name & value bytes concatenated */ +}; +struct xfs_attr_shortform {struct xfs_attr_sf_hdr {/* constant-structure header block */ __be16 totsize;/* total bytes in shortform list */ __u8 count;/* count of active entries */ __u8 padding; } hdr; -struct xfs_attr_sf_entry { -uint8_t namelen;/* actual length of name (no NULL) */ -uint8_t valuelen;/* actual length of value (no NULL) */ -uint8_t flags;/* flags bits (see xfs_attr_leaf.h) */ -uint8_t nameval[];/* name & value bytes concatenated */ - } list[1 ];/* variable sized array */ +union { +struct xfs_attr_sf_entry legacy_padding; + __DECLARE_FLEX_ARRAY(struct xfs_attr_sf_entry, list); + }; }; typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
@@ -620,19 +625,29 @@ typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */typedef struct xfs_attr_leaf_name_local {__be16 valuelen;/* number of bytes in value */ __u8 namelen;/* length of name bytes */ -__u8 nameval[1 ];/* name/value bytes */ +union { +__u8 legacy_padding; + __DECLARE_FLEX_ARRAY(__u8, nameval); /* name/value bytes */ + }; }xfs_attr_leaf_name_local_t ;typedef struct xfs_attr_leaf_name_remote {__be32 valueblk;/* block number of value bytes */ __be32 valuelen;/* number of bytes in value */ __u8 namelen;/* length of name bytes */ -__u8 name[1 ];/* name bytes */ +union { +__u8 legacy_padding; + __DECLARE_FLEX_ARRAY(__u8, name); /* name bytes */ + }; }xfs_attr_leaf_name_remote_t ; typedef struct xfs_attr_leafblock { xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */ - xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */ + union { + xfs_attr_leaf_entry_t legacy_padding; + /* sorted on key, not name */ + __DECLARE_FLEX_ARRAY(xfs_attr_leaf_entry_t, entries); + }; /* * The rest of the block contains the following structures after the * leaf entries, growing from the bottom up. The variables are never
@@ -664,7 +679,10 @@ struct xfs_attr3_leaf_hdr {struct xfs_attr3_leafblock {struct xfs_attr3_leaf_hdr hdr; -struct xfs_attr_leaf_entry entries[1 ]; +union { +struct xfs_attr_leaf_entry legacy_padding; + __DECLARE_FLEX_ARRAY(struct xfs_attr_leaf_entry, entries); + };/* * The rest of the block contains the following structures after the
libxfs/xfs_fs.h +10 -2
@@ -590,12 +590,20 @@ typedef struct xfs_attrlist_cursor {struct xfs_attrlist {__s32 al_count;/* number of entries in attrlist */ __s32 al_more;/* T/F: more attrs (do call again) */ -__s32 al_offset[1 ];/* byte offsets of attrs [var-sized] */ +union { +__s32 legacy_padding; +/* byte offsets of attrs [var-sized] */ + __DECLARE_FLEX_ARRAY(__s32, al_offset); + }; };struct xfs_attrlist_ent {/* data from attr_list() */ __u32 a_valuelen;/* number bytes in value of attr */ -char a_name[1 ];/* attr name (NULL terminated) */ +union { +char legacy_padding; +/* attr name (NULL terminated) */ + __DECLARE_FLEX_ARRAY(char, a_name); + }; }; typedef struct xfs_fsop_attrlist_handlereq {
Signature · 1 line
2.34.1
Quoted 34 lines · Kees Cook
To allow for code bases that include libxfs (e.g. the Linux kernel) and build with strict flexible array handling (-fstrict-flex-arrays=3),
FORTIFY_SOURCE, and/or UBSAN bounds checking, redefine the remaining 1-element trailing arrays as true flexible arrays, but without changing their structure sizes. This is done via a union to retain a single element (named "legacy_padding"). As not all distro headers may yet have the UAPIstddef.h__DECLARE_FLEX_ARRAYmacro, include it explicitly in platform_defs.h.in.Addresses the warnings being seen under Linux:
https://lore.kernel.org/all/20230710170243.GF11456@frogsfrogsfrogsSigned-off-by Kees Cook <[email protected]>--- include/platform_defs.h.in | 18 ++++++++++++++++++ libxfs/xfs_da_format.h | 38 ++++++++++++++++++++++++++++---------- libxfs/xfs_fs.h | 12 ++++++++++-- 3 files changed, 56 insertions(+), 12 deletions(-)include/platform_defs.h.in +7
-0@@ -134,6 +134,24 @@ static inline size_t __ab_c_size(size_t a, size_t b, size_t c) # define fallthrough do {} while (0) /* fallthrough */ #endif +#ifndef __DECLARE_FLEX_ARRAY +/** + * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union + * + * @type: The type of each flexible array element + * @name: The name of the flexible array member + *
include/platform_defs.h.in static inline size_t __ab_c_size(size_t a, size_t b, size_t c) · line 153
Quoted 10 lines
include/platform_defs.h.in +10
-0+ * In order to have a flexible array member in a union or alone in a + * struct, it needs to be wrapped in an anonymous struct with at least 1 + * named member, but that member can be empty. + */ +#define __DECLARE_FLEX_ARRAY(type, name) \ + struct { \ + struct { } __empty_ ## name; \ + type name[]; \ + } +#endif
Hmm. This is tempting, since the struct sizes stay the same, which avoids all the potential sizeof breakage problems and whatnot. I think the downside of doing it this way is that xfs_fs.h ships in xfslibs-dev but platform_defs.h does not, which means that random user programs can pick up a *user* of __DECLARE_FLEX_ARRAY from /usr/include/xfs/xfs_fs.h.
I guess this could be a problem for anyone who might (a) compile against xfs_fs.h from a newer xfslibs-dev package on a system with (b) old kernel headers that don't define __DECLARE_FLEX_ARRAY.
More on this at the bottom.
Quoted 9 lines
include/platform_defs.h.in +1
-0+ /* Only needed for the kernel. */ #define __initlibxfs/xfs_da_format.h +0
-0
libxfs/xfs_da_format.h xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp) · line 588
Quoted 9 lines
libxfs/xfs_da_format.h +6
-0@@ -580,18 +580,23 @@ xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)/* * Entries are packed toward the top as tight as possible. */ +struct xfs_attr_sf_entry { +uint8_t namelen;/* actual length of name (no NULL) */ +uint8_t valuelen;/* actual length of value (no NULL) */ +uint8_t flags;/* flags bits (see xfs_attr_leaf.h) */ +uint8_t nameval[];/* name & value bytes concatenated */ +};
(Yeah, these ought to be broken out...)
Quoted 94 lines
libxfs/xfs_da_format.h +22
-10+struct xfs_attr_shortform {struct xfs_attr_sf_hdr {/* constant-structure header block */ __be16 totsize;/* total bytes in shortform list */ __u8 count;/* count of active entries */ __u8 padding; } hdr; -struct xfs_attr_sf_entry { -uint8_t namelen;/* actual length of name (no NULL) */ -uint8_t valuelen;/* actual length of value (no NULL) */ -uint8_t flags;/* flags bits (see xfs_attr_leaf.h) */ -uint8_t nameval[];/* name & value bytes concatenated */ - } list[1 ];/* variable sized array */ +union { +struct xfs_attr_sf_entry legacy_padding; + __DECLARE_FLEX_ARRAY(struct xfs_attr_sf_entry, list); + }; }; typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */@@ -620,19 +625,29 @@ typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */typedef struct xfs_attr_leaf_name_local {__be16 valuelen;/* number of bytes in value */ __u8 namelen;/* length of name bytes */ -__u8 nameval[1 ];/* name/value bytes */ +union { +__u8 legacy_padding; + __DECLARE_FLEX_ARRAY(__u8, nameval); /* name/value bytes */ + }; }xfs_attr_leaf_name_local_t ;typedef struct xfs_attr_leaf_name_remote {__be32 valueblk;/* block number of value bytes */ __be32 valuelen;/* number of bytes in value */ __u8 namelen;/* length of name bytes */ -__u8 name[1 ];/* name bytes */ +union { +__u8 legacy_padding; + __DECLARE_FLEX_ARRAY(__u8, name); /* name bytes */ + }; }xfs_attr_leaf_name_remote_t ; typedef struct xfs_attr_leafblock { xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */ - xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */ + union { + xfs_attr_leaf_entry_t legacy_padding; + /* sorted on key, not name */ + __DECLARE_FLEX_ARRAY(xfs_attr_leaf_entry_t, entries); + }; /* * The rest of the block contains the following structures after the * leaf entries, growing from the bottom up. The variables are never@@ -664,7 +679,10 @@ struct xfs_attr3_leaf_hdr {struct xfs_attr3_leafblock {struct xfs_attr3_leaf_hdr hdr; -struct xfs_attr_leaf_entry entries[1 ]; +union { +struct xfs_attr_leaf_entry legacy_padding; + __DECLARE_FLEX_ARRAY(struct xfs_attr_leaf_entry, entries); + };/* * The rest of the block contains the following structures after the libxfs/xfs_fs.h +10
-2@@ -590,12 +590,20 @@ typedef struct xfs_attrlist_cursor {struct xfs_attrlist {__s32 al_count;/* number of entries in attrlist */ __s32 al_more;/* T/F: more attrs (do call again) */ -__s32 al_offset[1 ];/* byte offsets of attrs [var-sized] */ +union { +__s32 legacy_padding; +/* byte offsets of attrs [var-sized] */ + __DECLARE_FLEX_ARRAY(__s32, al_offset); + }; };struct xfs_attrlist_ent {/* data from attr_list() */ __u32 a_valuelen;/* number bytes in value of attr */ -char a_name[1 ];/* attr name (NULL terminated) */ +union { +char legacy_padding; +/* attr name (NULL terminated) */ + __DECLARE_FLEX_ARRAY(char, a_name); + }; }; typedef struct xfs_fsop_attrlist_handlereq {Signature · 1 line
2.34.1
Here's my version, where I go for a straight a[1] -> a[] conversion and let downstream attrlist ioctl users eat their lumps. What do you think of the excess-documentation approach?
--D
db/metadump.c +2 -2
@@ -1475,7 +1475,7 @@ process_attr_block( nlen = local->namelen; vlen =be16_to_cpu (local->valuelen); zlen =xfs_attr_leaf_entsize_local (nlen, vlen) - - (sizeof (xfs_attr_leaf_name_local_t) -1 + + (offsetof(struct xfs_attr_leaf_name_local , nameval) + nlen + vlen);if (zero_stale_data)memset (&local->nameval[nlen + vlen],0 , zlen);
@@ -1497,7 +1497,7 @@ process_attr_block(/* zero from end of name[] to next name start */ nlen = remote->namelen; zlen =xfs_attr_leaf_entsize_remote (nlen) - - (sizeof (xfs_attr_leaf_name_remote_t) -1 + + (offsetof(struct xfs_attr_leaf_name_remote , name) + nlen);if (zero_stale_data)memset (&remote->name[nlen],0 , zlen);
libxfs/xfs_da_format.h +74 -9
138 lines not on this page · raw message
libxfs/xfs_fs.h +5 -2
@@ -588,16 +588,19 @@ typedef struct xfs_attrlist_cursor {* * NOTE: struct xfs_attrlist must match struct attrlist defined in libattr, and * struct xfs_attrlist_ent must match struct attrlist_ent defined in libattr. +* +* WARNING: In Linux 6.5, al_offset and a_name were changed from array[1] to +* array[]. Anyone using sizeof is (already) broken! */ struct xfs_attrlist {__s32 al_count;/* number of entries in attrlist */ __s32 al_more;/* T/F: more attrs (do call again) */ -__s32 al_offset[1 ];/* byte offsets of attrs [var-sized] */ +__s32 al_offset[];/* byte offsets of attrs [var-sized] */ };struct xfs_attrlist_ent {/* data from attr_list() */ __u32 a_valuelen;/* number bytes in value of attr */ -char a_name[1 ];/* attr name (NULL terminated) */ +char a_name[];/* attr name (NULL terminated) */ }; typedef struct xfs_fsop_attrlist_handlereq {
Quoted 3 lines · Darrick J. Wong
Here's my version, where I go for a straight a[1] -> a[] conversion and let downstream attrlist ioctl users eat their lumps. What do you think of the excess-documentation approach?
I think this is roughtly the right thing, with one big caveat:
libxfs/xfs_da_format.h struct xfs_attr_shortform { · line 590
Quoted 1 line
libxfs/xfs_da_format.h +1
-0+/* In Linux 6.5 this flex array was changed from list[1] to list[]. */
For all the format headers there's no need for these comments. We've done this for various other structures that had the old one size arrays and never bothered.
Quoted 14 lines
libxfs/xfs_fs.h +3
-0@@ -588,16 +588,19 @@ typedef struct xfs_attrlist_cursor {* * NOTE: struct xfs_attrlist must match struct attrlist defined in libattr, and * struct xfs_attrlist_ent must match struct attrlist_ent defined in libattr. +* +* WARNING: In Linux 6.5, al_offset and a_name were changed from array[1] to +* array[]. Anyone using sizeof is (already) broken! */ struct xfs_attrlist {__s32 al_count;/* number of entries in attrlist */
libxfs/xfs_fs.h typedef struct xfs_attrlist_cursor { · line 604
Quoted 10 lines
libxfs/xfs_fs.h +2
-2__s32 al_more;/* T/F: more attrs (do call again) */ -__s32 al_offset[1 ];/* byte offsets of attrs [var-sized] */ +__s32 al_offset[];/* byte offsets of attrs [var-sized] */ };struct xfs_attrlist_ent {/* data from attr_list() */ __u32 a_valuelen;/* number bytes in value of attr */ -char a_name[1 ];/* attr name (NULL terminated) */ +char a_name[];/* attr name (NULL terminated) */ };
Now these are currently exposed in a xfsprogs headeer and IFF someone is using them it would break them in nasty ways. That being said, xfsprogs itself doesn't use them as it relies on identically layed out but differntly named structures from libattr.
So I think we should just move these two out of xfs_fs.h, because while they document a UAPI, they aren't actually used by userspace.
With that the need for any caution goes away and we can just fix the definition without any caveats.
Quoted 3 lines · Darrick J. Wong
Here's my version, where I go for a straight a[1] -> a[] conversion and let downstream attrlist ioctl users eat their lumps. What do you think of the excess-documentation approach?
This is fine by me, and I think it's much cleaner. Thanks for running with this!
-Kees
Signature · 1 line
Kees Cook
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Darrick J. Wong
Here's my version, where I go for a straight a[1] -> a[] conversion and let downstream attrlist ioctl users eat their lumps. What do you think of the excess-documentation approach?
Quoted 7 lines
I think this is roughtly the right thing, with one big caveat:
libxfs/xfs_da_format.h struct xfs_attr_shortform { · line 590
Quoted 1 line
libxfs/xfs_da_format.h +1
-0+/* In Linux 6.5 this flex array was changed from list[1] to list[]. */ For all the format headers there's no need for these comments. We've done this for various other structures that had the old one size arrays and never bothered.
<nod> Dave looked at an earlier version and wanted the comments for xfs_da_format.h to steer people at the entsize helpers. I more or less agree that it's overkill everywhere else though.
Quoted 30 lines
Quoted 14 lines
libxfs/xfs_fs.h +3
-0@@ -588,16 +588,19 @@ typedef struct xfs_attrlist_cursor {* * NOTE: struct xfs_attrlist must match struct attrlist defined in libattr, and * struct xfs_attrlist_ent must match struct attrlist_ent defined in libattr. +* +* WARNING: In Linux 6.5, al_offset and a_name were changed from array[1] to +* array[]. Anyone using sizeof is (already) broken! */ struct xfs_attrlist {__s32 al_count;/* number of entries in attrlist */ libxfs/xfs_fs.h typedef struct xfs_attrlist_cursor { · line 604
Quoted 10 lines
libxfs/xfs_fs.h +2
-2__s32 al_more;/* T/F: more attrs (do call again) */ -__s32 al_offset[1 ];/* byte offsets of attrs [var-sized] */ +__s32 al_offset[];/* byte offsets of attrs [var-sized] */ };struct xfs_attrlist_ent {/* data from attr_list() */ __u32 a_valuelen;/* number bytes in value of attr */ -char a_name[1 ];/* attr name (NULL terminated) */ +char a_name[];/* attr name (NULL terminated) */ };Now these are currently exposed in a xfsprogs headeer and IFF someone is using them it would break them in nasty ways. That being said, xfsprogs itself doesn't use them as it relies on identically layed out but differntly named structures from libattr.
Quoted 5 lines
So I think we should just move these two out of
xfs_fs.h, because while they document a UAPI, they aren't actually used by userspace.With that the need for any caution goes away and we can just fix the definition without any caveats.
So I looked at the libattr headers for Ubuntu 22.04 and saw this:
/*
* List the names and sizes of the values of all the attributes of an object.
* "Cursor" must be allocated and zeroed before the first call, it is used
* to maintain context between system calls if all the attribute names won't
* fit into the buffer on the first system call.
* The return value is -1 on error (w/errno set appropriately), 0 on success. */
extern int attr_list(const char *__path, char *__buffer, const int __buffersize,
int __flags, attrlist_cursor_t *__cursor)
__attribute__ ((deprecated ("Use listxattr or llistxattr instead")));
extern int attr_listf(int __fd, char *__buffer, const int __buffersize,
int __flags, attrlist_cursor_t *__cursor)
__attribute__ ((deprecated ("Use flistxattr instead")));I take that as a sign that they could drop all these xfs-specific APIs one day, which means we ought to keep them in xfs_fs.h.
--D
Quoted 8 lines · Darrick J. Wong
/*
* List the names and sizes of the values of all the attributes of an object.
* "Cursor" must be allocated and zeroed before the first call, it is used
* to maintain context between system calls if all the attribute names won't
* fit into the buffer on the first system call.
* The return value is -1 on error (w/errno set appropriately), 0 on success. */
extern int attr_list(const char*__path, char*__buffer, const int__buffersize,
Quoted 8 lines
int __flags, attrlist_cursor_t *__cursor) __attribute__ ((deprecated ("Use listxattr or llistxattr instead"))); extern int attr_listf(int __fd, char *__buffer, const int __buffersize, int __flags, attrlist_cursor_t *__cursor) __attribute__ ((deprecated ("Use flistxattr instead")));I take that as a sign that they could drop all these xfs-specific APIs one day, which means we ought to keep them in
xfs_fs.h.
Well...
These APIs you quoted are in fact internally mapped to the normal xattr syscalls by libattr, and have been since the Linux xattr syscalls were created. The only thing that actually uses the definitions in Linux is the magic attrlist by handle ioctl that exists only in XFS and which is exported through libhandle in xfsprogs. But the libhandle API is based on the attrlist_cursor from libattr and doesn't use the xfs_ kernel definitions.
(that struct attrlist/attrlist_ent in libattr have the same 1-sized array problem, but fortunately we don't need to solve that here..)