diff mbox series

[bpf,v2,2/2] selftests/bpf: Add test for immediate spilled to stack

Message ID 20231101-fix-check-stack-write-v2-2-cb7c17b869b0@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Fix incorrect immediate spill | expand

Checks

Context Check Description
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-3 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-2 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-7 fail Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for aarch64-gcc / veristat
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers success CCed 17 of 17 maintainers
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: Avoid line continuations in quoted strings
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-13 fail Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-15 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-17 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-21 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-22 fail Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-llvm-16 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-llvm-16 / build / build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-llvm-16 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-llvm-16 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-16 / veristat
bpf/vmtest-bpf-VM_Test-28 fail Logs for x86_64-llvm-16 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-12 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-PR fail PR summary
bpf/vmtest-bpf-VM_Test-10 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc

Commit Message

Hao Sun Nov. 1, 2023, 7:33 a.m. UTC
Add a test to check if the verifier correctly reason about the sign
of an immediate spilled to stack by BPF_ST instruction.

Signed-off-by: Hao Sun <sunhao.th@gmail.com>
---
 tools/testing/selftests/bpf/verifier/bpf_st_mem.c | 32 +++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Eduard Zingerman Nov. 1, 2023, 11:05 a.m. UTC | #1
On Wed, 2023-11-01 at 08:33 +0100, Hao Sun wrote:
> Add a test to check if the verifier correctly reason about the sign
> of an immediate spilled to stack by BPF_ST instruction.
> 
> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> ---
>  tools/testing/selftests/bpf/verifier/bpf_st_mem.c | 32 +++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
> index 3af2501082b2..0ba23807c46c 100644
> --- a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
> +++ b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
> @@ -65,3 +65,35 @@
>  	.expected_attach_type = BPF_SK_LOOKUP,
>  	.runs = -1,
>  },
> +{
> +	"BPF_ST_MEM stack imm sign",
> +	/* Check if verifier correctly reasons about sign of an
> +	 * immediate spilled to stack by BPF_ST instruction.
> +	 *
> +	 *   fp[-8] = -44;
> +	 *   r0 = fp[-8];
> +	 *   if r0 s< 0 goto ret0;
> +	 *   r0 = -1;
> +	 *   exit;
> +	 * ret0:
> +	 *   r0 = 0;
> +	 *   exit;
> +	 */
> +	.insns = {
> +	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44),
> +	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
> +	BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2),
> +	BPF_MOV64_IMM(BPF_REG_0, -1),
> +	BPF_EXIT_INSN(),
> +	BPF_MOV64_IMM(BPF_REG_0, 0),
> +	BPF_EXIT_INSN(),
> +	},
> +	/* Use prog type that requires return value in range [0, 1] */
> +	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
> +	.expected_attach_type = BPF_SK_LOOKUP,
> +	.result = VERBOSE_ACCEPT,
> +	.runs = -1,
> +	.errstr = "0: (7a) *(u64 *)(r10 -8) = -44        ; R10=fp0 fp-8_w=-44\
> +	2: (c5) if r0 s< 0x0 goto pc+2\
> +	2: R0_w=-44",
> +},
> 

Please note that this test case fails on CI [0], full log below:

2023-11-01T07:49:51.2841702Z #116/p BPF_ST_MEM stack imm sign FAIL
2023-11-01T07:49:51.2843456Z Unexpected verifier log!
2023-11-01T07:49:51.2844968Z EXP: 2: R0_w=-44
2023-11-01T07:49:51.2845583Z RES:
2023-11-01T07:49:51.2846693Z func#0 @0
2023-11-01T07:49:51.2848932Z 0: R1=ctx(off=0,imm=0) R10=fp0
2023-11-01T07:49:51.2853045Z 0: (7a) *(u64 *)(r10 -8) = -44        ; R10=fp0 fp-8_w=-44
2023-11-01T07:49:51.2857391Z 1: (79) r0 = *(u64 *)(r10 -8)         ; R0_w=-44 R10=fp0 fp-8_w=-44
2023-11-01T07:49:51.2859127Z 2: (c5) if r0 s< 0x0 goto pc+2
2023-11-01T07:49:51.2862943Z mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1 
2023-11-01T07:49:51.2867511Z mark_precise: frame0: regs=r0 stack= before 1: (79) r0 = *(u64 *)(r10 -8)
2023-11-01T07:49:51.2872217Z mark_precise: frame0: regs= stack=-8 before 0: (7a) *(u64 *)(r10 -8) = -44
2023-11-01T07:49:51.2872816Z 5: R0_w=-44
2023-11-01T07:49:51.2875653Z 5: (b7) r0 = 0                        ; R0_w=0
2023-11-01T07:49:51.2876493Z 6: (95) exit

I suspect that after recent logging fixes instruction number printed
after jump changed and that's why test case no longer passes.

Note: you can check CI status for submitted patch-sets using link [1].

[0] https://github.com/kernel-patches/bpf/actions/runs/6717053909/job/18254330860
[1] https://patchwork.kernel.org/project/netdevbpf/list/
Hao Sun Nov. 1, 2023, 12:18 p.m. UTC | #2
On Wed, Nov 1, 2023 at 12:05 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Wed, 2023-11-01 at 08:33 +0100, Hao Sun wrote:
> > Add a test to check if the verifier correctly reason about the sign
> > of an immediate spilled to stack by BPF_ST instruction.
> >
> > Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> > ---
> >  tools/testing/selftests/bpf/verifier/bpf_st_mem.c | 32 +++++++++++++++++++++++
> >  1 file changed, 32 insertions(+)
> >
> > diff --git a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
> > index 3af2501082b2..0ba23807c46c 100644
> > --- a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
> > +++ b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
> > @@ -65,3 +65,35 @@
> >       .expected_attach_type = BPF_SK_LOOKUP,
> >       .runs = -1,
> >  },
> > +{
> > +     "BPF_ST_MEM stack imm sign",
> > +     /* Check if verifier correctly reasons about sign of an
> > +      * immediate spilled to stack by BPF_ST instruction.
> > +      *
> > +      *   fp[-8] = -44;
> > +      *   r0 = fp[-8];
> > +      *   if r0 s< 0 goto ret0;
> > +      *   r0 = -1;
> > +      *   exit;
> > +      * ret0:
> > +      *   r0 = 0;
> > +      *   exit;
> > +      */
> > +     .insns = {
> > +     BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44),
> > +     BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
> > +     BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2),
> > +     BPF_MOV64_IMM(BPF_REG_0, -1),
> > +     BPF_EXIT_INSN(),
> > +     BPF_MOV64_IMM(BPF_REG_0, 0),
> > +     BPF_EXIT_INSN(),
> > +     },
> > +     /* Use prog type that requires return value in range [0, 1] */
> > +     .prog_type = BPF_PROG_TYPE_SK_LOOKUP,
> > +     .expected_attach_type = BPF_SK_LOOKUP,
> > +     .result = VERBOSE_ACCEPT,
> > +     .runs = -1,
> > +     .errstr = "0: (7a) *(u64 *)(r10 -8) = -44        ; R10=fp0 fp-8_w=-44\
> > +     2: (c5) if r0 s< 0x0 goto pc+2\
> > +     2: R0_w=-44",
> > +},
> >
>
> Please note that this test case fails on CI [0], full log below:
>
> 2023-11-01T07:49:51.2841702Z #116/p BPF_ST_MEM stack imm sign FAIL
> 2023-11-01T07:49:51.2843456Z Unexpected verifier log!
> 2023-11-01T07:49:51.2844968Z EXP: 2: R0_w=-44
> 2023-11-01T07:49:51.2845583Z RES:
> 2023-11-01T07:49:51.2846693Z func#0 @0
> 2023-11-01T07:49:51.2848932Z 0: R1=ctx(off=0,imm=0) R10=fp0
> 2023-11-01T07:49:51.2853045Z 0: (7a) *(u64 *)(r10 -8) = -44        ; R10=fp0 fp-8_w=-44
> 2023-11-01T07:49:51.2857391Z 1: (79) r0 = *(u64 *)(r10 -8)         ; R0_w=-44 R10=fp0 fp-8_w=-44
> 2023-11-01T07:49:51.2859127Z 2: (c5) if r0 s< 0x0 goto pc+2
> 2023-11-01T07:49:51.2862943Z mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1
> 2023-11-01T07:49:51.2867511Z mark_precise: frame0: regs=r0 stack= before 1: (79) r0 = *(u64 *)(r10 -8)
> 2023-11-01T07:49:51.2872217Z mark_precise: frame0: regs= stack=-8 before 0: (7a) *(u64 *)(r10 -8) = -44
> 2023-11-01T07:49:51.2872816Z 5: R0_w=-44
> 2023-11-01T07:49:51.2875653Z 5: (b7) r0 = 0                        ; R0_w=0
> 2023-11-01T07:49:51.2876493Z 6: (95) exit
>
> I suspect that after recent logging fixes instruction number printed
> after jump changed and that's why test case no longer passes.
>

Yes, so I guess we can just drop the line number there, will send patch v3.

> Note: you can check CI status for submitted patch-sets using link [1].
>
> [0] https://github.com/kernel-patches/bpf/actions/runs/6717053909/job/18254330860
> [1] https://patchwork.kernel.org/project/netdevbpf/list/

Thanks.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
index 3af2501082b2..0ba23807c46c 100644
--- a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
+++ b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c
@@ -65,3 +65,35 @@ 
 	.expected_attach_type = BPF_SK_LOOKUP,
 	.runs = -1,
 },
+{
+	"BPF_ST_MEM stack imm sign",
+	/* Check if verifier correctly reasons about sign of an
+	 * immediate spilled to stack by BPF_ST instruction.
+	 *
+	 *   fp[-8] = -44;
+	 *   r0 = fp[-8];
+	 *   if r0 s< 0 goto ret0;
+	 *   r0 = -1;
+	 *   exit;
+	 * ret0:
+	 *   r0 = 0;
+	 *   exit;
+	 */
+	.insns = {
+	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44),
+	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
+	BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2),
+	BPF_MOV64_IMM(BPF_REG_0, -1),
+	BPF_EXIT_INSN(),
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	/* Use prog type that requires return value in range [0, 1] */
+	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
+	.expected_attach_type = BPF_SK_LOOKUP,
+	.result = VERBOSE_ACCEPT,
+	.runs = -1,
+	.errstr = "0: (7a) *(u64 *)(r10 -8) = -44        ; R10=fp0 fp-8_w=-44\
+	2: (c5) if r0 s< 0x0 goto pc+2\
+	2: R0_w=-44",
+},