Skip to content

bpf: allow void* cast using bpf_rdonly_cast()

Eduard Zingerman bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 6 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4bpf: allow void* cast using bpf_rdonly_cast()kernel/bpf/verifier.c+60 -120 replies
2/4bpf: add bpf_features enumkernel/bpf/verifier.c+7 -02 replies
3/4selftests/bpf: allow tests from verifier.c not to drop CAP_SYS_ADMINtools/testing/selftests/bpf/prog_tests/verifier.c+11 -64 replies
4/4selftests/bpf: check operations on untrusted ro pointers to memtools/testing/selftests/bpf/prog_tests/verifier.c tools/testing/selftests/bpf/progs/verifier_mem_rdonly_untrusted.c+138 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next v1 0/4] bpf: allow void* cast using bpf_rdonly_cast()

At the moment pointers returned by bpf_rdonly_cast() have type "pointer to btf id", and only casts to structure types are allowed. Access to memory pointed to by returned pointers is done through BPF_PROBE_{MEM,MEMSX} instructions and does not produce errors on invalid memory access.

This patch-set extends bpf_rdonly_cast() to allow casts to an equivalent of 'void *', effectively replacing bpf_probe_read_kernel() calls in situations when access to individual bytes or integers is necessary.

The mechanism was suggested and explored by Andrii Nakryiko in [1].

To help with detecting support for this feature 'enum bpf_features' is added with intended usage like below:

if (bpf_core_enum_value_exists(enum bpf_features,

                                 BPF_FEAT_RDONLY_CAST_TO_VOID))
    ...

[1] https://github.com/anakryiko/linux/tree/bpf-mem-cast

Eduard Zingerman (4):
bpf: allow void* cast using bpf_rdonly_cast()
bpf: add bpf_features enum
selftests/bpf: allow tests from verifier.c not to drop CAP_SYS_ADMIN selftests/bpf: check operations on untrusted ro pointers to mem

 kernel/bpf/verifier.c                         |  79 ++++++++--
 .../selftests/bpf/prog_tests/verifier.c       |  19 ++-
 .../bpf/progs/verifier_mem_rdonly_untrusted.c | 136 ++++++++++++++++++
 3 files changed, 216 insertions(+), 18 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_mem_rdonly_untrusted.c
Signature · 1 line
2.47.1