Skip to contentSkip to conversation

ethtool: Replace 0-length array with flexible array

PATCH Applied Kees Cook 6 participants 11 messages Jan 6, 2023 – Jan 7, 2023

Subsystems (4)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v3 1/1 ethtool: Replace 0-length array with flexible array

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>

---
v3: don't use helper (vincent)
v2: https://lore.kernel.org/lkml/[email protected]

---
 include/uapi/linux/ethtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/ethtool.h +1 -1
index 58e587ba0450..3135fa0ba9a4 100644
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
 		__u32			rule_cnt;
 		__u32			rss_context;
 	};
-	__u32				rule_locs[0];
+	__u32				rule_locs[];
 };
 
 
Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 14 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

Quoted 4 lines
 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Side comment, this link does not mention the __DECLARE_FLEX_ARRAY(). It could be good to add a reference to the helper here. But of course, this is not a criticism of this patch.

Quoted 4 lines
Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Quoted 8 lines
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>
Quoted 2 lines

---
v3: don't use helper (vincent)

You may want to double check your other patches as well. At least this one is also using the helper when not needed:

https://lore.kernel.org/netdev/[email protected]/T/#u

Quoted 21 lines

v2: https://lore.kernel.org/lkml/[email protected]

---
 include/uapi/linux/ethtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/ethtool.h +1 -2
index 58e587ba0450..3135fa0ba9a4 100644
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
                __u32                   rule_cnt;
                __u32                   rss_context;
        };
-       __u32                           rule_locs[0];
+       __u32                           rule_locs[];
 };
 
 
--

2.34.1

#3 lore raw
In reply to Kees Cook
Quoted 31 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>
Quoted 2 lines

---
v3: don't use helper (vincent)

v1: https://lore.kernel.org/all/[email protected]

                                               ^^^^^
Quoted 1 line

v2: https://lore.kernel.org/lkml/[email protected]

                                                  ^^^^^
v3: https://lore.kernel.org/netdev/[email protected]
                                                  ^^^^

Seriously... :)

Quoted 16 lines

v2: https://lore.kernel.org/lkml/[email protected]

---
 include/uapi/linux/ethtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/ethtool.h +1 -1
index 58e587ba0450..3135fa0ba9a4 100644
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
                __u32                   rule_cnt;
                __u32                   rss_context;
        };
-       __u32                           rule_locs[0];
+       __u32                           rule_locs[];
 };
author
#4 lore raw
Quoted 35 lines · Vincent MAILHOL
Quoted 31 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>
Quoted 2 lines

---
v3: don't use helper (vincent)

Quoted 8 lines

v1: https://lore.kernel.org/all/[email protected]

                                               ^^^^^
Quoted 1 line

v2: https://lore.kernel.org/lkml/[email protected]

                                                  ^^^^^
v3: https://lore.kernel.org/netdev/[email protected]
                                                  ^^^^

Seriously... :)

Hurray! Someone noticed and it's not even April yet. :) *celebrate*

-Kees

Signature · 1 line
Kees Cook
#5 lore raw
In reply to Kees Cook
Quoted 43 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>

---
v3: don't use helper (vincent)
v2: https://lore.kernel.org/lkml/[email protected]

---
 include/uapi/linux/ethtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/ethtool.h +0 -0
index 58e587ba0450..3135fa0ba9a4 100644

include/uapi/linux/ethtool.h struct ethtool_rxnfc { · line 1186

Quoted 5 lines
include/uapi/linux/ethtool.h +1 -1
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
                __u32                   rule_cnt;
                __u32                   rss_context;
        };
-       __u32                           rule_locs[0];
+       __u32                           rule_locs[];

Stupid question: Is this syntax allowed in UAPI headers despite not being part of standard C90 or C++? Are we relying on all C/C++ compilers for pre-C99 having gcc/clang extensions?

#6 lore raw
Quoted 39 lines · Jann Horn
Quoted 38 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>

---
v3: don't use helper (vincent)
v2: https://lore.kernel.org/lkml/[email protected]

---
 include/uapi/linux/ethtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

include/uapi/linux/ethtool.h struct ethtool_rxnfc { · line 1186

Quoted 10 lines
include/uapi/linux/ethtool.h +1 -1
index 58e587ba0450..3135fa0ba9a4 100644
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
                __u32                   rule_cnt;
                __u32                   rss_context;
        };
-       __u32                           rule_locs[0];
+       __u32                           rule_locs[];

Stupid question: Is this syntax allowed in UAPI headers despite not being part of standard C90 or C++? Are we relying on all C/C++ compilers for pre-C99 having gcc/clang extensions?

The [0] isn't part of the C90 standard either. So having to choose between [0] and [], the latter is the most portable nowadays.

If I do a bit of speleology, I can see that C99 flexible array members were used as early as v2.6.19 (released in November 2006):

https://elixir.bootlin.com/linux/v2.6.19/source/include/linux/usb/audio.h#L36

This is prior to the include/linux and include/uapi/linux split, but believe me, this usb/audio.h file is indeed part of the uapi. So, yes, using C99 flexible array members in the UAPI is de facto allowed because it was used for the last 16 years.

An interesting sub question would be:

What are the minimum compiler requirements to build a program using
the Linux UAPI?

And, after research, I could not find the answer. The requirements to build the kernel are well documented:

https://docs.kernel.org/process/changes.html#changes

But no clue for the uapi. I guess that at one point in 2006, people decided that it was time to set the minimum requirement to C99. Maybe this matches the end of life of the latest pre-C99 GCC version? The detailed answer must be hidden somewhere on lkml.

Yours sincerely,
Vincent Mailhol

Reviewed-by on v3 1/1
#9 lore raw
In reply to Kees Cook
Quoted 19 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Quoted 12 lines
Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc kernel test robot <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Sean Anderson <[email protected]>
Cc Alexandru Tachici <[email protected]>
Cc Amit Cohen <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Vincent Mailhol <[email protected]>

Thanks!
--
Gustavo

Quoted 23 lines

---
v3: don't use helper (vincent)
v2: https://lore.kernel.org/lkml/[email protected]

---
 include/uapi/linux/ethtool.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/ethtool.h +1 -1
index 58e587ba0450..3135fa0ba9a4 100644
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
 		__u32			rule_cnt;
 		__u32			rss_context;
 	};
-	__u32				rule_locs[0];
+	__u32				rule_locs[];
 };
 
 
Signature · 1 line
2.34.1
author
#2 lore raw
In reply to Vincent MAILHOL
Quoted 6 lines · Vincent MAILHOL
Quoted 1 line · Kees Cook

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Side comment, this link does not mention the __DECLARE_FLEX_ARRAY(). It could be good to add a reference to the helper here. But of course, this is not a criticism of this patch.

Good point! I've sent a patch for this now.

Quoted 4 lines

You may want to double check your other patches as well. At least this one is also using the helper when not needed:

https://lore.kernel.org/netdev/[email protected]/T/#u

That one does, actually, need it since otherwise the flex array would be "alone in a struct" which is the other case that C99 irrationally disallows.

-Kees

Signature · 1 line
Kees Cook
author
#7 lore raw

Re: minimum compiler for Linux UAPI (was Re: [PATCH v3] ethtool: Replace 0-length array with flexible array)

In reply to Vincent MAILHOL
Quoted 34 lines · Vincent MAILHOL
Quoted 4 lines · Jann Horn
Quoted 3 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

[...]

Quoted 6 lines
Quoted 6 lines
include/uapi/linux/ethtool.h +0 -0
index 58e587ba0450..3135fa0ba9a4 100644
@@ -1183,7 +1183,7 @@ struct ethtool_rxnfc {
                __u32                   rule_cnt;
Quoted 8 lines

include/uapi/linux/ethtool.h struct ethtool_rxnfc { · line 1186

Quoted 4 lines
include/uapi/linux/ethtool.h +1 -1
                __u32                   rss_context;
        };
-       __u32                           rule_locs[0];
+       __u32                           rule_locs[];

Stupid question: Is this syntax allowed in UAPI headers despite not being part of standard C90 or C++? Are we relying on all C/C++ compilers for pre-C99 having gcc/clang extensions?

The [0] isn't part of the C90 standard either. So having to choose between [0] and [], the latter is the most portable nowadays.

If I do a bit of speleology, I can see that C99 flexible array members were used as early as v2.6.19 (released in November 2006):

https://elixir.bootlin.com/linux/v2.6.19/source/include/linux/usb/audio.h#L36

This is prior to the include/linux and include/uapi/linux split, but believe me, this usb/audio.h file is indeed part of the uapi. So, yes, using C99 flexible array members in the UAPI is de facto allowed because it was used for the last 16 years.

Quoted 4 lines

An interesting sub question would be:

What are the minimum compiler requirements to build a program using
the Linux UAPI?

You're right -- we haven't explicitly documented this. C99 seems like the defacto minimum, though.

Quoted 9 lines

And, after research, I could not find the answer. The requirements to build the kernel are well documented:

https://docs.kernel.org/process/changes.html#changes

But no clue for the uapi. I guess that at one point in 2006, people decided that it was time to set the minimum requirement to C99. Maybe this matches the end of life of the latest pre-C99 GCC version? The detailed answer must be hidden somewhere on lkml.

I would make the argument that the requirements for building Linux UAPI should match that of building the kernel...

Signature · 1 line
Kees Cook
#8 lore raw

Re: minimum compiler for Linux UAPI (was Re: [PATCH v3] ethtool: Replace 0-length array with flexible array)

Quoted 14 lines · Kees Cook
Quoted 4 lines · Vincent MAILHOL

On Fri. 6 Jan 2023 at 22:19, Jann Horn <[email protected]> wrote:

What are the minimum compiler requirements to build a program using
the Linux UAPI?

You're right -- we haven't explicitly documented this. C99 seems like the defacto minimum, though.

Quoted 5 lines

And, after research, I could not find the answer. The requirements to build the kernel are well documented:

https://docs.kernel.org/process/changes.html#changes

Quoted 7 lines
Quoted 4 lines

But no clue for the uapi. I guess that at one point in 2006, people decided that it was time to set the minimum requirement to C99. Maybe this matches the end of life of the latest pre-C99 GCC version? The detailed answer must be hidden somewhere on lkml.

I would make the argument that the requirements for building Linux UAPI should match that of building the kernel...

I think it's a bit more nuanced than that: glibc does not require C99 but does include some kernel headers from generic library headers, so I would not make the assumption that it's always safe to use C99 features. On the other hand, Linux specific device drivers whose header is only really used from one application are free to make assumptions about the toolchain.

Arnd

#10 lore raw
In reply to Kees Cook
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 14 lines · On Thu, 5 Jan 2023 20:28:48 -0800 you wrote:

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

net/ethtool/common.c: In function 'ethtool_get_max_rxnfc_channel':
net/ethtool/common.c:558:55: warning: array subscript i is outside array bounds of '__u32[0]' {aka 'unsigned int[]'} [-Warray-bounds=]
558 | .fs.location = info->rule_locs[i],

      |                                        ~~~~~~~~~~~~~~~^~~

In file included from include/linux/ethtool.h:19,
from include/uapi/linux/ethtool_netlink.h:12, from include/linux/ethtool_netlink.h:6, from net/ethtool/common.c:3:
include/uapi/linux/ethtool.h:1186:41: note: while referencing 'rule_locs'

Quoted 4 lines
 1186 |         __u32                           rule_locs[0];
      |                                         ^~~~~~~~~

[...]

Here is the summary with links:
- [v3] ethtool: Replace 0-length array with flexible array

    https://git.kernel.org/netdev/net-next/c/b466a25c930f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html