Skip to content

bpf: make reg_not_null() true for CONST_PTR_TO_MAP #9042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

kernel-patches-daemon-bpf[bot]
Copy link

Pull request for series with
subject: bpf: make reg_not_null() true for CONST_PTR_TO_MAP
version: 2
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=968449

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: cd2e103
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968449
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: cd2e103
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968739
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 7fdaba9
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968739
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 919319b
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968739
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 97744b4
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968739
version: 3

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: a570f38
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968739
version: 3

theihor added 3 commits June 5, 2025 13:57
When reg->type is CONST_PTR_TO_MAP, it can not be null. However the
verifier explores the branches under rX == 0 in check_cond_jmp_op()
even if reg->type is CONST_PTR_TO_MAP, because it was not checked for
in reg_not_null().

Fix this by adding CONST_PTR_TO_MAP to the set of types that are
considered non nullable in reg_not_null().

An old "unpriv: cmp map pointer with zero" selftest fails with this
change, because now early out correctly triggers in
check_cond_jmp_op(), making the verification to pass.

In practice verifier may allow pointer to null comparison in unpriv,
since in many cases the relevant branch and comparison op are removed
as dead code. So change the expected test result to __success_unpriv.

Signed-off-by: Ihor Solodrai <[email protected]>
Acked-by: Andrii Nakryiko <[email protected]>
Add a test for CONST_PTR_TO_MAP comparison with a non-0 constant. A
BPF program with this code must not pass verification in unpriv.

Signed-off-by: Ihor Solodrai <[email protected]>
A test requires the following to happen:
  * CONST_PTR_TO_MAP value is put on the stack
  * then this value is checked for null
  * the code in the null branch fails verification

I was able to achieve this by using a stack allocated array of maps,
populated with values from a global map. This is the first test case:
map_ptr_is_never_null.

The second test case (map_ptr_is_never_null_rb) involves an array of
ringbufs and attempts to recreate a common coding pattern [1].

[1] https://lore.kernel.org/bpf/CAEf4BzZNU0gX_sQ8k8JaLe1e+Veth3Rk=4x7MDhv=hQxvO8EDw@mail.gmail.com/

Suggested-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Ihor Solodrai <[email protected]>
Acked-by: Andrii Nakryiko <[email protected]>
@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 64a064c
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=968739
version: 3

@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the series/966056=>bpf-next branch from ff5d226 to ca2c2d4 Compare June 5, 2025 20:57
@kernel-patches-daemon-bpf
Copy link
Author

At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=968739 expired. Closing PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant