Skip to contentSkip to conversation

bpf: Replace 0-length arrays with flexible arrays

PATCH Applied Kees Cook 3 participants 3 messages Jan 5, 2023 – Jan 10, 2023

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 bpf: Replace 0-length arrays with flexible arrays

Zero-length arrays are deprecated[1]. Replace struct bpf_array's union of 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?) Detected with GCC 13, using -fstrict-flex-arrays=3:

arch/x86/net/bpf_jit_comp.c: In function 'bpf_tail_call_direct_fixup':
arch/x86/net/bpf_jit_comp.c:606:37: warning: array subscript <unknown> is outside array bounds of 'void *[0]' [-Warray-bounds=]

  606 |                 target = array->ptrs[poke->tail_call.key];
      |                          ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
In file included from include/linux/filter.h:9,
                 from arch/x86/net/bpf_jit_comp.c:9:

include/linux/bpf.h:1527:23: note: while referencing 'ptrs'
1527 | void *ptrs[0] __aligned(8);
| ^~~~

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

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
---
 include/linux/bpf.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/bpf.h +3 -3
index 3de24cfb7a3d..2131000f711e 100644
@@ -1523,9 +1523,9 @@ struct bpf_array {
 	u32 index_mask;
 	struct bpf_array_aux *aux;
 	union {
-		char value[0] __aligned(8);
-		void *ptrs[0] __aligned(8);
-		void __percpu *pptrs[0] __aligned(8);
+		DECLARE_FLEX_ARRAY(char, value) __aligned(8);
+		DECLARE_FLEX_ARRAY(void *, ptrs) __aligned(8);
+		DECLARE_FLEX_ARRAY(void __percpu *, pptrs) __aligned(8);
 	};
 };
 
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct bpf_array's union of 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?) Detected with GCC 13, using

Haven't looked closely, but these arrays should be produced somewhere around kernel/bpf/arraymap.c (fd_array).

Quoted 14 lines

-fstrict-flex-arrays=3:

arch/x86/net/bpf_jit_comp.c: In function 'bpf_tail_call_direct_fixup':
arch/x86/net/bpf_jit_comp.c:606:37: warning: array subscript <unknown> is outside array bounds of 'void *[0]' [-Warray-bounds=]

  606 |                 target = array->ptrs[poke->tail_call.key];
      |                          ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
In file included from include/linux/filter.h:9,
                 from arch/x86/net/bpf_jit_comp.c:9:

include/linux/bpf.h:1527:23: note: while referencing 'ptrs'
1527 | void *ptrs[0] __aligned(8);
| ^~~~

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

Quoted 14 lines
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Quoted 24 lines
---
 include/linux/bpf.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/bpf.h +3 -4
index 3de24cfb7a3d..2131000f711e 100644
@@ -1523,9 +1523,9 @@ struct bpf_array {
        u32 index_mask;
        struct bpf_array_aux *aux;
        union {
-               char value[0] __aligned(8);
-               void *ptrs[0] __aligned(8);
-               void __percpu *pptrs[0] __aligned(8);
+               DECLARE_FLEX_ARRAY(char, value) __aligned(8);
+               DECLARE_FLEX_ARRAY(void *, ptrs) __aligned(8);
+               DECLARE_FLEX_ARRAY(void __percpu *, pptrs) __aligned(8);
        };
 };
 
--

2.34.1

#2 lore raw
In reply to Kees Cook
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 11 lines · On Thu, 5 Jan 2023 11:26:47 -0800 you wrote:

Zero-length arrays are deprecated[1]. Replace struct bpf_array's union of 0-length arrays with flexible arrays. (How are the sizes of these arrays verified?) Detected with GCC 13, using -fstrict-flex-arrays=3:

arch/x86/net/bpf_jit_comp.c: In function 'bpf_tail_call_direct_fixup':
arch/x86/net/bpf_jit_comp.c:606:37: warning: array subscript <unknown> is outside array bounds of 'void *[0]' [-Warray-bounds=]

  606 |                 target = array->ptrs[poke->tail_call.key];
      |                          ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
In file included from include/linux/filter.h:9,
                 from arch/x86/net/bpf_jit_comp.c:9:
Quoted 5 lines

include/linux/bpf.h:1527:23: note: while referencing 'ptrs'
1527 | void *ptrs[0] __aligned(8);
| ^~~~

[...]

Here is the summary with links:
- bpf: Replace 0-length arrays with flexible arrays

    https://git.kernel.org/bpf/bpf-next/c/129d868ede1e

You are awesome, thank you!

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