netfilter: include: uapi: Use C99 flexible array member
Phil Sutter
reviewerPATCH v2 1/1 netfilter: include: uapi: Use C99 flexible array member
Recent versions of gcc started to complain about the old-style zero-length array as last member of various structs. For instance, while compiling iptables:
| In file included from /usr/include/string.h:495, | from libip4tc.c:15: | In function 'memcpy', | inlined from 'iptcc_compile_chain' at libiptc.c:1172:2, | inlined from 'iptcc_compile_table' at libiptc.c:1243:13, | inlined from 'iptc_commit' at libiptc.c:2572:8, | inlined from 'iptc_commit' at libiptc.c:2510:1: | /usr/include/bits/string_fortified.h:34:10: warning: writing 16 bytes into a region of size 0 [-Wstringop-overflow=] | 34 | return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest)); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | In file included from ../include/libiptc/libiptc.h:12, | from libip4tc.c:29: | libiptc.c: In function 'iptc_commit': | ../include/linux/netfilter_ipv4/ip_tables.h:202:19: note: at offset 0 to object 'entries' with size 0 declared here | 202 | struct ipt_entry entries[0]; | | ^~~~~~~
(Similar for libip6tc.c.)
Avoid this warning by declaring these fields as an ISO C99 flexible array member. This makes gcc aware of the intended use and enables sanity checking as described in:
https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
This patch is actually a follow-up on commit 6daf14140129d ("netfilter:
Replace zero-length array with flexible-array member") which seems to have missed a few spots. Like it, alignment attribute syntax is fixed where found in line with zero-length array fields.
---
Changes since v1:
- Fix up any zero-length arrays found via:
grep -r '\[0\]' include/uapi/linux/netfilter*.
- Perform alignment attribute syntax fixup just like 6daf14140129d does.
- Point at relationship with 6daf14140129d in commit message.
- Add Gustavo to Cc: for verification.
--- include/uapi/linux/netfilter/x_tables.h | 6 +++--- include/uapi/linux/netfilter_arp/arp_tables.h | 6 +++--- include/uapi/linux/netfilter_bridge/ebt_among.h | 2 +- include/uapi/linux/netfilter_bridge/ebtables.h | 10 +++++----- include/uapi/linux/netfilter_ipv4/ip_tables.h | 6 +++--- include/uapi/linux/netfilter_ipv6/ip6_tables.h | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-)
include/uapi/linux/netfilter/x_tables.h +3 -3
@@ -28,7 +28,7 @@ struct xt_entry_match {__u16 match_size; } u; -unsigned char data[0 ]; +unsigned char data[]; };struct xt_entry_target {
@@ -51,7 +51,7 @@ struct xt_entry_target {__u16 target_size; } u; -unsigned char data[0 ]; +unsigned char data[]; };#define XT_TARGET_INIT (__name, __size) \
@@ -119,7 +119,7 @@ struct xt_counters_info {unsigned int num_counters;/* The counters (actually `number' of these). */ -struct xt_counters counters[0 ]; +struct xt_counters counters[]; };#define XT_INV_PROTO 0x40/* Invert the sense of PROTO. */
include/uapi/linux/netfilter_arp/arp_tables.h +3 -3
@@ -109,7 +109,7 @@ struct arpt_entrystruct xt_counters counters;/* The matches (if any), then the target. */ -unsigned char elems[0 ]; +unsigned char elems[]; };/*
@@ -181,7 +181,7 @@ struct arpt_replace {struct xt_counters __user *counters;/* The entries (hang off end: not really an array). */ -struct arpt_entry entries[0 ]; +struct arpt_entry entries[]; };/* The argument to ARPT_SO_GET_ENTRIES. */
@@ -193,7 +193,7 @@ struct arpt_get_entries {unsigned int size;/* The entries. */ -struct arpt_entry entrytable[0 ]; +struct arpt_entry entrytable[]; };/* Helper functions */
include/uapi/linux/netfilter_bridge/ebt_among.h +1 -1
@@ -40,7 +40,7 @@ struct ebt_mac_wormhash_tuple {struct ebt_mac_wormhash {int table[257 ];int poolsize; -struct ebt_mac_wormhash_tuple pool[0 ]; +struct ebt_mac_wormhash_tuple pool[]; };#define ebt_mac_wormhash_size (x) ((x) ? sizeof(struct ebt_mac_wormhash) \
include/uapi/linux/netfilter_bridge/ebtables.h +5 -5
@@ -87,7 +87,7 @@ struct ebt_entries {/* nr. of entries */ unsigned int nentries;/* entry list */ - char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +char data[]__aligned(__alignof__(struct ebt_replace)) ; };/* used for the bitmask of struct ebt_entry */
@@ -129,7 +129,7 @@ struct ebt_entry_match { } u;/* size of data */ unsigned int match_size; - unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char data[]__aligned(__alignof__(struct ebt_replace)) ; };struct ebt_entry_watcher {
@@ -142,7 +142,7 @@ struct ebt_entry_watcher { } u;/* size of data */ unsigned int watcher_size; - unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char data[]__aligned(__alignof__(struct ebt_replace)) ; };struct ebt_entry_target {
@@ -155,7 +155,7 @@ struct ebt_entry_target { } u;/* size of data */ unsigned int target_size; - unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char data[]__aligned(__alignof__(struct ebt_replace)) ; };#define EBT_STANDARD_TARGET "standard"
@@ -188,7 +188,7 @@ struct ebt_entry {unsigned int target_offset;/* sizeof ebt_entry + matches + watchers + target */ unsigned int next_offset; - unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char elems[]__aligned(__alignof__(struct ebt_replace)) ; }; static __inline__ struct ebt_entry_target *
include/uapi/linux/netfilter_ipv4/ip_tables.h +3 -3
@@ -121,7 +121,7 @@ struct ipt_entry {struct xt_counters counters;/* The matches (if any), then the target. */ -unsigned char elems[0 ]; +unsigned char elems[]; };/*
@@ -203,7 +203,7 @@ struct ipt_replace {struct xt_counters __user *counters;/* The entries (hang off end: not really an array). */ -struct ipt_entry entries[0 ]; +struct ipt_entry entries[]; };/* The argument to IPT_SO_GET_ENTRIES. */
@@ -215,7 +215,7 @@ struct ipt_get_entries {unsigned int size;/* The entries. */ -struct ipt_entry entrytable[0 ]; +struct ipt_entry entrytable[]; };/* Helper functions */
include/uapi/linux/netfilter_ipv6/ip6_tables.h +3 -3
@@ -125,7 +125,7 @@ struct ip6t_entry {struct xt_counters counters;/* The matches (if any), then the target. */ -unsigned char elems[0 ]; +unsigned char elems[]; };/* Standard entry */
@@ -243,7 +243,7 @@ struct ip6t_replace {struct xt_counters __user *counters;/* The entries (hang off end: not really an array). */ -struct ip6t_entry entries[0 ]; +struct ip6t_entry entries[]; };/* The argument to IP6T_SO_GET_ENTRIES. */
@@ -255,7 +255,7 @@ struct ip6t_get_entries {unsigned int size;/* The entries. */ -struct ip6t_entry entrytable[0 ]; +struct ip6t_entry entrytable[]; };/* Helper functions */
Signature · 1 line
2.27.0
Hi,
Please, see this:
https://git.kernel.org/linus/1e6e9d0f4859ec698d55381ea26f4136eff3afe1
We are refraining from doing flexible-array conversions in UAPI, for now.
--
Gustavo
Quoted 238 lines · Phil Sutter
Recent versions of gcc started to complain about the old-style zero-length array as last member of various structs. For instance, while compiling iptables:
| In file included from /usr/include/string.h:495, | from libip4tc.c:15: | In function 'memcpy', | inlined from 'iptcc_compile_chain' at libiptc.c:1172:2, | inlined from 'iptcc_compile_table' at libiptc.c:1243:13, | inlined from 'iptc_commit' at libiptc.c:2572:8, | inlined from 'iptc_commit' at libiptc.c:2510:1: | /usr/include/bits/string_fortified.h:34:10: warning: writing 16 bytes into a region of size 0 [-Wstringop-overflow=] | 34 | return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest)); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | In file included from ../include/libiptc/libiptc.h:12, | from libip4tc.c:29: | libiptc.c: In function 'iptc_commit': | ../include/linux/netfilter_ipv4/ip_tables.h:202:19: note: at offset 0 to object 'entries' with size 0 declared here | 202 | struct ipt_entry entries[0]; | | ^~~~~~~(Similar for
libip6tc.c.)Avoid this warning by declaring these fields as an ISO C99 flexible array member. This makes gcc aware of the intended use and enables sanity checking as described in:
https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.htmlThis patch is actually a follow-up on commit
6daf14140129d("netfilter:
Replace zero-length array with flexible-array member") which seems to have missed a few spots. Like it, alignment attribute syntax is fixed where found in line with zero-length array fields.Cc Gustavo A. R. Silva <[email protected]>Signed-off-by Phil Sutter <[email protected]>---
Changes since v1:
- Fix up any zero-length arrays found via:
grep -r '\[0\]' include/uapi/linux/netfilter*.
- Perform alignment attribute syntax fixup just like6daf14140129ddoes.
- Point at relationship with6daf14140129din commit message.
- Add Gustavo to Cc: for verification.--- include/uapi/linux/netfilter/x_tables.h | 6 +++--- include/uapi/linux/netfilter_arp/arp_tables.h | 6 +++--- include/uapi/linux/netfilter_bridge/ebt_among.h | 2 +- include/uapi/linux/netfilter_bridge/ebtables.h | 10 +++++----- include/uapi/linux/netfilter_ipv4/ip_tables.h | 6 +++--- include/uapi/linux/netfilter_ipv6/ip6_tables.h | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-)include/uapi/linux/netfilter/x_tables.h +3
-3@@ -28,7 +28,7 @@ struct xt_entry_match {__u16 match_size; } u; -unsigned char data[0 ]; +unsigned char data[]; };struct xt_entry_target {@@ -51,7 +51,7 @@ struct xt_entry_target {__u16 target_size; } u; -unsigned char data[0 ]; +unsigned char data[]; };#define XT_TARGET_INIT (__name, __size) \@@ -119,7 +119,7 @@ struct xt_counters_info {unsigned int num_counters;/* The counters (actually `number' of these). */ -struct xt_counters counters[0 ]; +struct xt_counters counters[]; };#define XT_INV_PROTO 0x40/* Invert the sense of PROTO. */ include/uapi/linux/netfilter_arp/arp_tables.h +3
-3@@ -109,7 +109,7 @@ struct arpt_entrystruct xt_counters counters;/* The matches (if any), then the target. */ -unsigned char elems[0 ]; +unsigned char elems[]; };/* @@ -181,7 +181,7 @@ struct arpt_replace {struct xt_counters __user *counters;/* The entries (hang off end: not really an array). */ -struct arpt_entry entries[0 ]; +struct arpt_entry entries[]; };/* The argument to ARPT_SO_GET_ENTRIES. */ @@ -193,7 +193,7 @@ struct arpt_get_entries {unsigned int size;/* The entries. */ -struct arpt_entry entrytable[0 ]; +struct arpt_entry entrytable[]; };/* Helper functions */ include/uapi/linux/netfilter_bridge/ebt_among.h +1
-1@@ -40,7 +40,7 @@ struct ebt_mac_wormhash_tuple {struct ebt_mac_wormhash {int table[257 ];int poolsize; -struct ebt_mac_wormhash_tuple pool[0 ]; +struct ebt_mac_wormhash_tuple pool[]; };#define ebt_mac_wormhash_size (x) ((x) ? sizeof(struct ebt_mac_wormhash) \include/uapi/linux/netfilter_bridge/ebtables.h +5
-5@@ -87,7 +87,7 @@ struct ebt_entries {/* nr. of entries */ unsigned int nentries;/* entry list */ - char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +char data[]__aligned(__alignof__(struct ebt_replace)) ; };/* used for the bitmask of struct ebt_entry */ @@ -129,7 +129,7 @@ struct ebt_entry_match { } u;/* size of data */ unsigned int match_size; - unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char data[]__aligned(__alignof__(struct ebt_replace)) ; };struct ebt_entry_watcher {@@ -142,7 +142,7 @@ struct ebt_entry_watcher { } u;/* size of data */ unsigned int watcher_size; - unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char data[]__aligned(__alignof__(struct ebt_replace)) ; };struct ebt_entry_target {@@ -155,7 +155,7 @@ struct ebt_entry_target { } u;/* size of data */ unsigned int target_size; - unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char data[]__aligned(__alignof__(struct ebt_replace)) ; };#define EBT_STANDARD_TARGET "standard"@@ -188,7 +188,7 @@ struct ebt_entry {unsigned int target_offset;/* sizeof ebt_entry + matches + watchers + target */ unsigned int next_offset; - unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct ebt_replace)))); +unsigned char elems[]__aligned(__alignof__(struct ebt_replace)) ; }; static __inline__ struct ebt_entry_target *include/uapi/linux/netfilter_ipv4/ip_tables.h +3
-3@@ -121,7 +121,7 @@ struct ipt_entry {struct xt_counters counters;/* The matches (if any), then the target. */ -unsigned char elems[0 ]; +unsigned char elems[]; };/* @@ -203,7 +203,7 @@ struct ipt_replace {struct xt_counters __user *counters;/* The entries (hang off end: not really an array). */ -struct ipt_entry entries[0 ]; +struct ipt_entry entries[]; };/* The argument to IPT_SO_GET_ENTRIES. */ @@ -215,7 +215,7 @@ struct ipt_get_entries {unsigned int size;/* The entries. */ -struct ipt_entry entrytable[0 ]; +struct ipt_entry entrytable[]; };/* Helper functions */ include/uapi/linux/netfilter_ipv6/ip6_tables.h +3
-3@@ -125,7 +125,7 @@ struct ip6t_entry {struct xt_counters counters;/* The matches (if any), then the target. */ -unsigned char elems[0 ]; +unsigned char elems[]; };/* Standard entry */ @@ -243,7 +243,7 @@ struct ip6t_replace {struct xt_counters __user *counters;/* The entries (hang off end: not really an array). */ -struct ip6t_entry entries[0 ]; +struct ip6t_entry entries[]; };/* The argument to IP6T_SO_GET_ENTRIES. */ @@ -255,7 +255,7 @@ struct ip6t_get_entries {unsigned int size;/* The entries. */ -struct ip6t_entry entrytable[0 ]; +struct ip6t_entry entrytable[]; };/* Helper functions */
Phil Sutter
reviewerHi Gustavo,
Quoted 5 lines · Gustavo A. R. Silva
Please, see this:
https://git.kernel.org/linus/1e6e9d0f4859ec698d55381ea26f4136eff3afe1
We are refraining from doing flexible-array conversions in UAPI, for now.
Oh, thanks for clarifying. Still, gcc spits out warnings about it when compiling iptables. I see several options and their downsides:
- Convert iptables' copy of UAPI headers only and maintain the divergence in future.
- Compile iptables with -Wno-stringop-overflow, losing the checks in valid cases.
Not sure if we may just define that we know how structs in ip_tables.h and ip6_tables.h are (supposed to) being used and just change it anyway.
BTW: The commit above link points at mentions structs possibly being embedded as the rationale for keeping things as-is. Yet structs ipt_entry and ip6t_entry are already embedded into other structs within the same header file. Seems like gcc is fine with that as long as they occupy the last field. And that even if they are declared as zero-length array themselves, which means indexes can't be used to address individual items.
Cheers, Phil
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Phil,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on nf-next/master]
url: https://github.com/0day-ci/linux/commits/Phil-Sutter/netfilter-include-uapi-Use-C99-flexible-array-member/20200719-180355 base: https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master config: x86_64-randconfig-s022-20200719 (attached as .config) compiler: gcc-9 (Debian 9.3.0-14) 9.3.0 reproduce: # apt-get install sparse # sparse version: v0.6.2-49-g707c5017-dirty # save the attached .config to linux build tree make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from <command-line>:32:
>> ./usr/include/linux/netfilter_bridge/ebtables.h:90:14: error: expected ':', ',', ';', '}' or '__attribute__' before '__aligned'
90 | char data[] __aligned(__alignof__(struct ebt_replace));
| ^~~~~~~~~
./usr/include/linux/netfilter_bridge/ebtables.h:132:23: error: expected ':', ',', ';', '}' or '__attribute__' before '__aligned'
132 | unsigned char data[] __aligned(__alignof__(struct ebt_replace));
| ^~~~~~~~~
./usr/include/linux/netfilter_bridge/ebtables.h:145:23: error: expected ':', ',', ';', '}' or '__attribute__' before '__aligned'
145 | unsigned char data[] __aligned(__alignof__(struct ebt_replace));
| ^~~~~~~~~
./usr/include/linux/netfilter_bridge/ebtables.h:158:23: error: expected ':', ',', ';', '}' or '__attribute__' before '__aligned'
158 | unsigned char data[] __aligned(__alignof__(struct ebt_replace));
| ^~~~~~~~~
./usr/include/linux/netfilter_bridge/ebtables.h:191:24: error: expected ':', ',', ';', '}' or '__attribute__' before '__aligned'
191 | unsigned char elems[] __aligned(__alignof__(struct ebt_replace));
| ^~~~~~~~~---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Phil,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on nf-next/master]
url: https://github.com/0day-ci/linux/commits/Phil-Sutter/netfilter-include-uapi-Use-C99-flexible-array-member/20200719-180355 base: https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master config: x86_64-allyesconfig (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 3504acc33e8b9ddd7de71d75cdc329a14dbf58d9)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All error/warnings (new ones prefixed by >>):
In file included from <built-in>:1:
>> ./usr/include/linux/netfilter_bridge/ebtables.h:90:13: error: expected ';' at end of declaration list
char data[] __aligned(__alignof__(struct ebt_replace));
^
;
./usr/include/linux/netfilter_bridge/ebtables.h:132:22: error: expected ';' at end of declaration list
unsigned char data[] __aligned(__alignof__(struct ebt_replace));
^
;
./usr/include/linux/netfilter_bridge/ebtables.h:145:22: error: expected ';' at end of declaration list
unsigned char data[] __aligned(__alignof__(struct ebt_replace));
^
;
./usr/include/linux/netfilter_bridge/ebtables.h:158:22: error: expected ';' at end of declaration list
unsigned char data[] __aligned(__alignof__(struct ebt_replace));
^
;
>> ./usr/include/linux/netfilter_bridge/ebtables.h:163:26: warning: field 'target' with variable sized type 'struct ebt_entry_target' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct ebt_entry_target target;
^
./usr/include/linux/netfilter_bridge/ebtables.h:191:23: error: expected ';' at end of declaration list
unsigned char elems[] __aligned(__alignof__(struct ebt_replace));
^
; 1 warning and 5 errors generated.
--
In file included from <built-in>:1:
In file included from ./usr/include/linux/netfilter_ipv6/ip6_tables.h:24:
>> ./usr/include/linux/netfilter/x_tables.h:66:25: warning: field 'target' with variable sized type 'struct xt_entry_target' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct xt_entry_target target;
^
./usr/include/linux/netfilter/x_tables.h:71:25: warning: field 'target' with variable sized type 'struct xt_entry_target' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct xt_entry_target target;
^
In file included from <built-in>:1:
>> ./usr/include/linux/netfilter_ipv6/ip6_tables.h:131:20: warning: field 'entry' with variable sized type 'struct ip6t_entry' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct ip6t_entry entry;
^
./usr/include/linux/netfilter_ipv6/ip6_tables.h:136:20: warning: field 'entry' with variable sized type 'struct ip6t_entry' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct ip6t_entry entry;
^ 4 warnings generated.
--
In file included from <built-in>:1:
In file included from ./usr/include/linux/netfilter_arp/arpt_mangle.h:4:
In file included from ./usr/include/linux/netfilter_arp/arp_tables.h:18:
>> ./usr/include/linux/netfilter/x_tables.h:66:25: warning: field 'target' with variable sized type 'struct xt_entry_target' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct xt_entry_target target;
^
./usr/include/linux/netfilter/x_tables.h:71:25: warning: field 'target' with variable sized type 'struct xt_entry_target' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
struct xt_entry_target target;
^2 warnings generated.
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org