diff mbox series

[bpf-next,4/7] selftests/bpf: verify states_equal() maintains idmap across all frames

Message ID 20221209135733.28851-5-eddyz87@gmail.com (mailing list archive)
State Accepted
Commit 7d05794330877986f605c1618534d7478030f5b8
Delegated to: BPF
Headers show
Series stricter register ID checking in regsafe() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
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-15 success Logs for test_progs on aarch64 with llvm-16
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 success 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-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
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-36 success Logs for test_verifier on s390x 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-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
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 10 maintainers not CCed: linux-kselftest@vger.kernel.org kpsingh@kernel.org haoluo@google.com song@kernel.org martin.lau@linux.dev sdf@google.com john.fastabend@gmail.com shuah@kernel.org jolsa@kernel.org mykolal@fb.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
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: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 82 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 85 exceeds 80 columns WARNING: line length of 87 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns WARNING: line length of 94 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-9 success Logs for set-matrix

Commit Message

Eduard Zingerman Dec. 9, 2022, 1:57 p.m. UTC
A test case that would erroneously pass verification if
verifier.c:states_equal() maintains separate register ID mappings for
call frames.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 tools/testing/selftests/bpf/verifier/calls.c | 82 ++++++++++++++++++++
 1 file changed, 82 insertions(+)

Comments

Andrii Nakryiko Dec. 14, 2022, 12:35 a.m. UTC | #1
On Fri, Dec 9, 2022 at 5:58 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> A test case that would erroneously pass verification if
> verifier.c:states_equal() maintains separate register ID mappings for
> call frames.
>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> ---

It's so hard to read these tests. Moving forward, let's try adding new
verifier tests like this using __naked functions and embedded
assembly. With recent test loader changes ([0]), there isn't much
that's needed, except for a few simple examples to get us started and
perhaps __flags(BPF_F_TEST_STATE_FREQ) support. The upside is that
using maps or global variables from assembly is now possible and easy,
and doesn't require any custom loader support at all.


  [0] https://patchwork.kernel.org/project/netdevbpf/list/?series=702713&state=*


>  tools/testing/selftests/bpf/verifier/calls.c | 82 ++++++++++++++++++++
>  1 file changed, 82 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c
> index 3193915c5ee6..bcd15b26dcee 100644
> --- a/tools/testing/selftests/bpf/verifier/calls.c
> +++ b/tools/testing/selftests/bpf/verifier/calls.c
> @@ -2305,3 +2305,85 @@
>         .errstr = "!read_ok",
>         .result = REJECT,
>  },
> +/* Make sure that verifier.c:states_equal() considers IDs from all
> + * frames when building 'idmap' for check_ids().
> + */
> +{
> +       "calls: check_ids() across call boundary",
> +       .insns = {
> +       /* Function main() */
> +       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
> +       /* fp[-24] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
> +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> +       BPF_LD_MAP_FD(BPF_REG_1,
> +                     0),
> +       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
> +       BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -24),
> +       /* fp[-32] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
> +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> +       BPF_LD_MAP_FD(BPF_REG_1,
> +                     0),
> +       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
> +       BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -32),
> +       /* call foo(&fp[-24], &fp[-32])   ; both arguments have IDs in the current
> +        *                                ; stack frame
> +        */
> +       BPF_MOV64_REG(BPF_REG_1, BPF_REG_FP),
> +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -24),
> +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
> +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -32),
> +       BPF_CALL_REL(2),
> +       /* exit 0 */
> +       BPF_MOV64_IMM(BPF_REG_0, 0),
> +       BPF_EXIT_INSN(),
> +       /* Function foo()
> +        *
> +        * r9 = &frame[0].fp[-24]  ; save arguments in the callee saved registers,
> +        * r8 = &frame[0].fp[-32]  ; arguments are pointers to pointers to map value
> +        */
> +       BPF_MOV64_REG(BPF_REG_9, BPF_REG_1),
> +       BPF_MOV64_REG(BPF_REG_8, BPF_REG_2),
> +       /* r7 = ktime_get_ns() */
> +       BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
> +       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
> +       /* r6 = ktime_get_ns() */
> +       BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
> +       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
> +       /* if r6 > r7 goto +1      ; no new information about the state is derived from
> +        *                         ; this check, thus produced verifier states differ
> +        *                         ; only in 'insn_idx'
> +        * r9 = r8
> +        */
> +       BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_7, 1),
> +       BPF_MOV64_REG(BPF_REG_9, BPF_REG_8),
> +       /* r9 = *r9                ; verifier get's to this point via two paths:
> +        *                         ; (I) one including r9 = r8, verified first;
> +        *                         ; (II) one excluding r9 = r8, verified next.
> +        *                         ; After load of *r9 to r9 the frame[0].fp[-24].id == r9.id.
> +        *                         ; Suppose that checkpoint is created here via path (I).
> +        *                         ; When verifying via (II) the r9.id must be compared against
> +        *                         ; frame[0].fp[-24].id, otherwise (I) and (II) would be
> +        *                         ; incorrectly deemed equivalent.
> +        * if r9 == 0 goto <exit>
> +        */
> +       BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_9, 0),
> +       BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 0, 1),
> +       /* r8 = *r8                ; read map value via r8, this is not safe
> +        * r0 = *r8                ; because r8 might be not equal to r9.
> +        */
> +       BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_8, 0),
> +       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_8, 0),
> +       /* exit 0 */
> +       BPF_MOV64_IMM(BPF_REG_0, 0),
> +       BPF_EXIT_INSN(),
> +       },
> +       .flags = BPF_F_TEST_STATE_FREQ,
> +       .fixup_map_hash_8b = { 3, 9 },
> +       .result = REJECT,
> +       .errstr = "R8 invalid mem access 'map_value_or_null'",
> +       .result_unpriv = REJECT,
> +       .errstr_unpriv = "",
> +       .prog_type = BPF_PROG_TYPE_CGROUP_SKB,
> +},
> --
> 2.34.1
>
Eduard Zingerman Dec. 14, 2022, 4:38 p.m. UTC | #2
On Tue, 2022-12-13 at 16:35 -0800, Andrii Nakryiko wrote:
> On Fri, Dec 9, 2022 at 5:58 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
> > 
> > A test case that would erroneously pass verification if
> > verifier.c:states_equal() maintains separate register ID mappings for
> > call frames.
> > 
> > Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> > ---
> 
> It's so hard to read these tests. Moving forward, let's try adding new
> verifier tests like this using __naked functions and embedded
> assembly. With recent test loader changes ([0]), there isn't much
> that's needed, except for a few simple examples to get us started and
> perhaps __flags(BPF_F_TEST_STATE_FREQ) support. The upside is that
> using maps or global variables from assembly is now possible and easy,
> and doesn't require any custom loader support at all.
> 
> 
>   [0] https://patchwork.kernel.org/project/netdevbpf/list/?series=702713&state=*
> 
> 

This is very nice, I'll try to use it for the next patch-set.
How do you think it should look like for test_verifier kind of tests?
The easiest way would be to just add new BPF sources under progs/
and have some prog_tests/verifier.c like this:

int test_verifier()
  ...
  RUN_TESTS(array_access),
  RUN_TESTS(scalar_ids)
  ...

Thus reusing the build mechanics for skeletons etc.
However, it seems to break current logical separation
between "unit" tests in test_verifier and "functional"
tests in test_progs. But this may be ok.


> >  tools/testing/selftests/bpf/verifier/calls.c | 82 ++++++++++++++++++++
> >  1 file changed, 82 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c
> > index 3193915c5ee6..bcd15b26dcee 100644
> > --- a/tools/testing/selftests/bpf/verifier/calls.c
> > +++ b/tools/testing/selftests/bpf/verifier/calls.c
> > @@ -2305,3 +2305,85 @@
> >         .errstr = "!read_ok",
> >         .result = REJECT,
> >  },
> > +/* Make sure that verifier.c:states_equal() considers IDs from all
> > + * frames when building 'idmap' for check_ids().
> > + */
> > +{
> > +       "calls: check_ids() across call boundary",
> > +       .insns = {
> > +       /* Function main() */
> > +       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
> > +       /* fp[-24] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
> > +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> > +       BPF_LD_MAP_FD(BPF_REG_1,
> > +                     0),
> > +       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
> > +       BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -24),
> > +       /* fp[-32] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
> > +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> > +       BPF_LD_MAP_FD(BPF_REG_1,
> > +                     0),
> > +       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
> > +       BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -32),
> > +       /* call foo(&fp[-24], &fp[-32])   ; both arguments have IDs in the current
> > +        *                                ; stack frame
> > +        */
> > +       BPF_MOV64_REG(BPF_REG_1, BPF_REG_FP),
> > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -24),
> > +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
> > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -32),
> > +       BPF_CALL_REL(2),
> > +       /* exit 0 */
> > +       BPF_MOV64_IMM(BPF_REG_0, 0),
> > +       BPF_EXIT_INSN(),
> > +       /* Function foo()
> > +        *
> > +        * r9 = &frame[0].fp[-24]  ; save arguments in the callee saved registers,
> > +        * r8 = &frame[0].fp[-32]  ; arguments are pointers to pointers to map value
> > +        */
> > +       BPF_MOV64_REG(BPF_REG_9, BPF_REG_1),
> > +       BPF_MOV64_REG(BPF_REG_8, BPF_REG_2),
> > +       /* r7 = ktime_get_ns() */
> > +       BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
> > +       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
> > +       /* r6 = ktime_get_ns() */
> > +       BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
> > +       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
> > +       /* if r6 > r7 goto +1      ; no new information about the state is derived from
> > +        *                         ; this check, thus produced verifier states differ
> > +        *                         ; only in 'insn_idx'
> > +        * r9 = r8
> > +        */
> > +       BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_7, 1),
> > +       BPF_MOV64_REG(BPF_REG_9, BPF_REG_8),
> > +       /* r9 = *r9                ; verifier get's to this point via two paths:
> > +        *                         ; (I) one including r9 = r8, verified first;
> > +        *                         ; (II) one excluding r9 = r8, verified next.
> > +        *                         ; After load of *r9 to r9 the frame[0].fp[-24].id == r9.id.
> > +        *                         ; Suppose that checkpoint is created here via path (I).
> > +        *                         ; When verifying via (II) the r9.id must be compared against
> > +        *                         ; frame[0].fp[-24].id, otherwise (I) and (II) would be
> > +        *                         ; incorrectly deemed equivalent.
> > +        * if r9 == 0 goto <exit>
> > +        */
> > +       BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_9, 0),
> > +       BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 0, 1),
> > +       /* r8 = *r8                ; read map value via r8, this is not safe
> > +        * r0 = *r8                ; because r8 might be not equal to r9.
> > +        */
> > +       BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_8, 0),
> > +       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_8, 0),
> > +       /* exit 0 */
> > +       BPF_MOV64_IMM(BPF_REG_0, 0),
> > +       BPF_EXIT_INSN(),
> > +       },
> > +       .flags = BPF_F_TEST_STATE_FREQ,
> > +       .fixup_map_hash_8b = { 3, 9 },
> > +       .result = REJECT,
> > +       .errstr = "R8 invalid mem access 'map_value_or_null'",
> > +       .result_unpriv = REJECT,
> > +       .errstr_unpriv = "",
> > +       .prog_type = BPF_PROG_TYPE_CGROUP_SKB,
> > +},
> > --
> > 2.34.1
> >
Andrii Nakryiko Dec. 14, 2022, 5:10 p.m. UTC | #3
On Wed, Dec 14, 2022 at 8:38 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> On Tue, 2022-12-13 at 16:35 -0800, Andrii Nakryiko wrote:
> > On Fri, Dec 9, 2022 at 5:58 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
> > >
> > > A test case that would erroneously pass verification if
> > > verifier.c:states_equal() maintains separate register ID mappings for
> > > call frames.
> > >
> > > Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> > > ---
> >
> > It's so hard to read these tests. Moving forward, let's try adding new
> > verifier tests like this using __naked functions and embedded
> > assembly. With recent test loader changes ([0]), there isn't much
> > that's needed, except for a few simple examples to get us started and
> > perhaps __flags(BPF_F_TEST_STATE_FREQ) support. The upside is that
> > using maps or global variables from assembly is now possible and easy,
> > and doesn't require any custom loader support at all.
> >
> >
> >   [0] https://patchwork.kernel.org/project/netdevbpf/list/?series=702713&state=*
> >
> >
>
> This is very nice, I'll try to use it for the next patch-set.
> How do you think it should look like for test_verifier kind of tests?
> The easiest way would be to just add new BPF sources under progs/
> and have some prog_tests/verifier.c like this:
>
> int test_verifier()
>   ...
>   RUN_TESTS(array_access),
>   RUN_TESTS(scalar_ids)
>   ...
>
> Thus reusing the build mechanics for skeletons etc.
> However, it seems to break current logical separation
> between "unit" tests in test_verifier and "functional"
> tests in test_progs. But this may be ok.

Yes, reusing skeletons and stuff, of course. But I wouldn't
necessarily make all of them as part of a single test_verifier test.
I'd probably have multiple tests with logically grouped sets of tests.

The interesting part is whether we can somehow automatically convert
macro-based test_verifier tests to this new embedded asm :) At least
most of them, but it's not clear how much work that would be, so I
just mentioned the possibility. I don't think we should manually
rewrite 1000+ tests, of course.

>
>
> > >  tools/testing/selftests/bpf/verifier/calls.c | 82 ++++++++++++++++++++
> > >  1 file changed, 82 insertions(+)
> > >
> > > diff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c
> > > index 3193915c5ee6..bcd15b26dcee 100644
> > > --- a/tools/testing/selftests/bpf/verifier/calls.c
> > > +++ b/tools/testing/selftests/bpf/verifier/calls.c
> > > @@ -2305,3 +2305,85 @@
> > >         .errstr = "!read_ok",
> > >         .result = REJECT,
> > >  },
> > > +/* Make sure that verifier.c:states_equal() considers IDs from all
> > > + * frames when building 'idmap' for check_ids().
> > > + */
> > > +{
> > > +       "calls: check_ids() across call boundary",
> > > +       .insns = {
> > > +       /* Function main() */
> > > +       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
> > > +       /* fp[-24] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
> > > +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> > > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> > > +       BPF_LD_MAP_FD(BPF_REG_1,
> > > +                     0),
> > > +       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
> > > +       BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -24),
> > > +       /* fp[-32] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
> > > +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> > > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> > > +       BPF_LD_MAP_FD(BPF_REG_1,
> > > +                     0),
> > > +       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
> > > +       BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -32),
> > > +       /* call foo(&fp[-24], &fp[-32])   ; both arguments have IDs in the current
> > > +        *                                ; stack frame
> > > +        */
> > > +       BPF_MOV64_REG(BPF_REG_1, BPF_REG_FP),
> > > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -24),
> > > +       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
> > > +       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -32),
> > > +       BPF_CALL_REL(2),
> > > +       /* exit 0 */
> > > +       BPF_MOV64_IMM(BPF_REG_0, 0),
> > > +       BPF_EXIT_INSN(),
> > > +       /* Function foo()
> > > +        *
> > > +        * r9 = &frame[0].fp[-24]  ; save arguments in the callee saved registers,
> > > +        * r8 = &frame[0].fp[-32]  ; arguments are pointers to pointers to map value
> > > +        */
> > > +       BPF_MOV64_REG(BPF_REG_9, BPF_REG_1),
> > > +       BPF_MOV64_REG(BPF_REG_8, BPF_REG_2),
> > > +       /* r7 = ktime_get_ns() */
> > > +       BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
> > > +       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
> > > +       /* r6 = ktime_get_ns() */
> > > +       BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
> > > +       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
> > > +       /* if r6 > r7 goto +1      ; no new information about the state is derived from
> > > +        *                         ; this check, thus produced verifier states differ
> > > +        *                         ; only in 'insn_idx'
> > > +        * r9 = r8
> > > +        */
> > > +       BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_7, 1),
> > > +       BPF_MOV64_REG(BPF_REG_9, BPF_REG_8),
> > > +       /* r9 = *r9                ; verifier get's to this point via two paths:
> > > +        *                         ; (I) one including r9 = r8, verified first;
> > > +        *                         ; (II) one excluding r9 = r8, verified next.
> > > +        *                         ; After load of *r9 to r9 the frame[0].fp[-24].id == r9.id.
> > > +        *                         ; Suppose that checkpoint is created here via path (I).
> > > +        *                         ; When verifying via (II) the r9.id must be compared against
> > > +        *                         ; frame[0].fp[-24].id, otherwise (I) and (II) would be
> > > +        *                         ; incorrectly deemed equivalent.
> > > +        * if r9 == 0 goto <exit>
> > > +        */
> > > +       BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_9, 0),
> > > +       BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 0, 1),
> > > +       /* r8 = *r8                ; read map value via r8, this is not safe
> > > +        * r0 = *r8                ; because r8 might be not equal to r9.
> > > +        */
> > > +       BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_8, 0),
> > > +       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_8, 0),
> > > +       /* exit 0 */
> > > +       BPF_MOV64_IMM(BPF_REG_0, 0),
> > > +       BPF_EXIT_INSN(),
> > > +       },
> > > +       .flags = BPF_F_TEST_STATE_FREQ,
> > > +       .fixup_map_hash_8b = { 3, 9 },
> > > +       .result = REJECT,
> > > +       .errstr = "R8 invalid mem access 'map_value_or_null'",
> > > +       .result_unpriv = REJECT,
> > > +       .errstr_unpriv = "",
> > > +       .prog_type = BPF_PROG_TYPE_CGROUP_SKB,
> > > +},
> > > --
> > > 2.34.1
> > >
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/verifier/calls.c b/tools/testing/selftests/bpf/verifier/calls.c
index 3193915c5ee6..bcd15b26dcee 100644
--- a/tools/testing/selftests/bpf/verifier/calls.c
+++ b/tools/testing/selftests/bpf/verifier/calls.c
@@ -2305,3 +2305,85 @@ 
 	.errstr = "!read_ok",
 	.result = REJECT,
 },
+/* Make sure that verifier.c:states_equal() considers IDs from all
+ * frames when building 'idmap' for check_ids().
+ */
+{
+	"calls: check_ids() across call boundary",
+	.insns = {
+	/* Function main() */
+	BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
+	/* fp[-24] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
+	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+	BPF_LD_MAP_FD(BPF_REG_1,
+		      0),
+	BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+	BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -24),
+	/* fp[-32] = map_lookup_elem(...) ; get a MAP_VALUE_PTR_OR_NULL with some ID */
+	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+	BPF_LD_MAP_FD(BPF_REG_1,
+		      0),
+	BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+	BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_0, -32),
+	/* call foo(&fp[-24], &fp[-32])   ; both arguments have IDs in the current
+	 *                                ; stack frame
+	 */
+	BPF_MOV64_REG(BPF_REG_1, BPF_REG_FP),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -24),
+	BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -32),
+	BPF_CALL_REL(2),
+	/* exit 0 */
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	/* Function foo()
+	 *
+	 * r9 = &frame[0].fp[-24]  ; save arguments in the callee saved registers,
+	 * r8 = &frame[0].fp[-32]  ; arguments are pointers to pointers to map value
+	 */
+	BPF_MOV64_REG(BPF_REG_9, BPF_REG_1),
+	BPF_MOV64_REG(BPF_REG_8, BPF_REG_2),
+	/* r7 = ktime_get_ns() */
+	BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
+	BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+	/* r6 = ktime_get_ns() */
+	BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
+	BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+	/* if r6 > r7 goto +1      ; no new information about the state is derived from
+	 *                         ; this check, thus produced verifier states differ
+	 *                         ; only in 'insn_idx'
+	 * r9 = r8
+	 */
+	BPF_JMP_REG(BPF_JGT, BPF_REG_6, BPF_REG_7, 1),
+	BPF_MOV64_REG(BPF_REG_9, BPF_REG_8),
+	/* r9 = *r9                ; verifier get's to this point via two paths:
+	 *                         ; (I) one including r9 = r8, verified first;
+	 *                         ; (II) one excluding r9 = r8, verified next.
+	 *                         ; After load of *r9 to r9 the frame[0].fp[-24].id == r9.id.
+	 *                         ; Suppose that checkpoint is created here via path (I).
+	 *                         ; When verifying via (II) the r9.id must be compared against
+	 *                         ; frame[0].fp[-24].id, otherwise (I) and (II) would be
+	 *                         ; incorrectly deemed equivalent.
+	 * if r9 == 0 goto <exit>
+	 */
+	BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_9, 0),
+	BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 0, 1),
+	/* r8 = *r8                ; read map value via r8, this is not safe
+	 * r0 = *r8                ; because r8 might be not equal to r9.
+	 */
+	BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_8, 0),
+	BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_8, 0),
+	/* exit 0 */
+	BPF_MOV64_IMM(BPF_REG_0, 0),
+	BPF_EXIT_INSN(),
+	},
+	.flags = BPF_F_TEST_STATE_FREQ,
+	.fixup_map_hash_8b = { 3, 9 },
+	.result = REJECT,
+	.errstr = "R8 invalid mem access 'map_value_or_null'",
+	.result_unpriv = REJECT,
+	.errstr_unpriv = "",
+	.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
+},