diff mbox series

bpf: Replace 0-length arrays with flexible arrays

Message ID 20230105192646.never.154-kees@kernel.org (mailing list archive)
State Accepted
Commit 129d868ede1ea835ac97f0a9f2cf3f4ed00d8ea7
Delegated to: BPF
Headers show
Series bpf: Replace 0-length arrays with flexible arrays | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch, async
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 fail Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-16

Commit Message

Kees Cook Jan. 5, 2023, 7:26 p.m. UTC
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 <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yhs@fb.com>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Stanislav Fomichev <sdf@google.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: bpf@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/linux/bpf.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Stanislav Fomichev Jan. 5, 2023, 7:56 p.m. UTC | #1
On Thu, Jan 5, 2023 at 11:26 AM Kees Cook <keescook@chromium.org> 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

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

> -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 <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: John Fastabend <john.fastabend@gmail.com>
> Cc: Andrii Nakryiko <andrii@kernel.org>
> Cc: Martin KaFai Lau <martin.lau@linux.dev>
> Cc: Song Liu <song@kernel.org>
> Cc: Yonghong Song <yhs@fb.com>
> Cc: KP Singh <kpsingh@kernel.org>
> Cc: Stanislav Fomichev <sdf@google.com>
> Cc: Hao Luo <haoluo@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> Cc: bpf@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>

Acked-by: Stanislav Fomichev <sdf@google.com>

> ---
>  include/linux/bpf.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 3de24cfb7a3d..2131000f711e 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -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
>
patchwork-bot+netdevbpf@kernel.org Jan. 10, 2023, 10:50 p.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

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:
> 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!
diff mbox series

Patch

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 3de24cfb7a3d..2131000f711e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -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);
 	};
 };