Skip to content

Introduce composable bpf types

Hao Luo bpf 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest posting 2/29 patches 4 repliesIn review
  2. v2 RFC posting 1/29 patches 10 repliesSuperseded

Changes in v2 from the cover letter

- use %u to print base_type(type) instead of %lu. (Andrii, patch 3/9)
- improve reg_type_str() by appending '_or_null' and prepending 'rdonly_'. use preallocated buffer in 'bpf_env'.
- unified handling of the previous XXX_OR_NULL in adjust_ptr_min_max_vals (Andrii, patch 4/9)
- move PTR_TO_MAP_KEY up to PTR_TO_MAP_VALUE so that we don't have to change to drivers that assume the numeric values of bpf_reg. (patch 4/9)
- reintroduce the typo from previous commits in fixes tags (Andrii, patch 7/9)
- extensive comments on the reason behind folding flags in check_reg_type (Andrii, patch 8/9)

Patches in v2 posting 2/2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9bpf: Introduce composable reg, ret and arg types.include/linux/bpf.h include/linux/bpf_verifier.h+55 -00 replies
2/9bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULLinclude/linux/bpf.h kernel/bpf/verifier.c+23 -310 replies
3/9bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULLinclude/linux/bpf.h kernel/bpf/helpers.c kernel/bpf/verifier.c+39 -340 replies
4/9bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULLinclude/linux/bpf.h include/linux/bpf_verifier.h kernel/bpf/btf.c +4 more+148 -1891 reply
5/9bpf: Introduce MEM_RDONLY flaginclude/linux/bpf.h kernel/bpf/btf.c kernel/bpf/map_iter.c +3 more+60 -430 replies
6/9bpf: Convert PTR_TO_MEM_OR_NULL to composable types.include/linux/bpf.h kernel/bpf/btf.c kernel/bpf/verifier.c+2 -30 replies
7/9bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.kernel/bpf/helpers.c kernel/bpf/verifier.c+28 -61 reply
8/9bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.include/linux/bpf.h kernel/bpf/btf.c kernel/bpf/cgroup.c +6 more+73 -572 replies
9/9bpf/selftests: Test PTR_TO_RDONLY_MEMtools/testing/selftests/bpf/prog_tests/ksyms_btf.c tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c+43 -00 replies

Reviews trailers per patch

1 of 9 patches has a Reviewed-by or Acked-by.

PatchAndrii Nakryiko
1/9
2/9
3/9
4/9
5/9
6/9
7/9
8/9
9/9A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH bpf-next v2 0/9] Introduce composable bpf types

This patch set consists of two changes:

- a cleanup of arg_type, ret_type and reg_type which try to make those types composable. (patch 1/9 - patch 6/9)
- a bug fix that prevents bpf programs from writing kernel memory. (patch 7/9 - patch 9/9)

The purpose of the cleanup is to find a scalable way to express type nullness and read-onliness. This patchset introduces two flags that can be applied on all three types: PTR_MAYBE_NULL and MEM_RDONLY. Previous types such as ARG_XXX_OR_NULL can now be written as

ARG_XXX | PTR_MAYBE_NULL

Similarly, PTR_TO_RDONLY_BUF is now "PTR_TO_BUF | MEM_RDONLY".

Flags can be composed, as ARGs can be both MEM_RDONLY and MAYBE_NULL.

ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY

Based on this new composable types, patch 7/9 applies MEM_RDONLY on PTR_TO_MEM, in order to tag the returned memory from per_cpu_ptr as read-only. Therefore fixing a previous bug that one can leverage per_cpu_ptr to modify kernel memory within BPF programs.

Patch 8/9 generalizes the use of MEM_RDONLY further by tagging a set of helper arguments ARG_PTR_TO_MEM with MEM_RDONLY. Some helper functions may override their arguments, such as bpf_d_path, bpf_snprintf. In this patch, we narrow the ARG_PTR_TO_MEM to be compatible with only a subset of memory types. This prevents these helpers from writing read-only memories. For the helpers that do not write its arguments, we add tag MEM_RDONLY to allow taking a RDONLY memory as argument.

Changes since v1:
- use %u to print base_type(type) instead of %lu. (Andrii, patch 3/9)
- improve reg_type_str() by appending '_or_null' and prepending 'rdonly_'. use preallocated buffer in 'bpf_env'.
- unified handling of the previous XXX_OR_NULL in adjust_ptr_min_max_vals (Andrii, patch 4/9)
- move PTR_TO_MAP_KEY up to PTR_TO_MAP_VALUE so that we don't have to change to drivers that assume the numeric values of bpf_reg. (patch 4/9)
- reintroduce the typo from previous commits in fixes tags (Andrii, patch 7/9)
- extensive comments on the reason behind folding flags in check_reg_type (Andrii, patch 8/9)

Changes since RFC v2:
- renamed BPF_BASE_TYPE to a more succinct name base_type and move its definition to bpf_verifier.h. Same for BPF_TYPE_FLAG. (Alexei)
- made checking MEM_RDONLY in check_reg_type() universal (Alexei)
- ran through majority of test_progs and fixed bugs in RFC v2:
- fixed incorrect BPF_BASE_TYPE_MASK. The high bit of GENMASK should be BITS - 1, rather than BITS. patch 1/9.
- fixed incorrect conditions when checking ARG_PTR_TO_MAP_VALUE in

     check_func_arg(). See patch 2/9.

- fixed a bug where PTR_TO_BTF_ID may be combined with MEM_RDONLY, causing the check in check_mem_access() to fall through to the 'else' branch. See check_helper_call() in patch 7/9.
- fixed build failure on netronome driver. Entries in bpf_reg_type have been ordered. patch 4/9.
- fixed build warnings of using '%d' to print base_type. patch 4/9
- unify arg_type_may_be_null() and reg_type_may_be_null() into a single type_may_be_null().

Previous versions:

v1:
https://lwn.net/Articles/877938/

RFC v2:
https://lwn.net/Articles/877171/

RFC v1:
https://lore.kernel.org/bpf/[email protected]/T/
https://lore.kernel.org/bpf/[email protected]/T/

Hao Luo (9):
bpf: Introduce composable reg, ret and arg types.
bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL
bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL
bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
bpf: Introduce MEM_RDONLY flag
bpf: Convert PTR_TO_MEM_OR_NULL to composable types.
bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem. bpf/selftests: Test PTR_TO_RDONLY_MEM

 include/linux/bpf.h                           | 101 +++-
 include/linux/bpf_verifier.h                  |  17 +
 kernel/bpf/btf.c                              |  12 +-
 kernel/bpf/cgroup.c                           |   2 +-
 kernel/bpf/helpers.c                          |  12 +-
 kernel/bpf/map_iter.c                         |   4 +-
 kernel/bpf/ringbuf.c                          |   2 +-
 kernel/bpf/syscall.c                          |   2 +-
 kernel/bpf/verifier.c                         | 491 +++++++++---------
 kernel/trace/bpf_trace.c                      |  26 +-
 net/core/bpf_sk_storage.c                     |   2 +-
 net/core/filter.c                             |  64 +--
 net/core/sock_map.c                           |   2 +-
 .../selftests/bpf/prog_tests/ksyms_btf.c      |  14 +
 .../bpf/progs/test_ksyms_btf_write_check.c    |  29 ++
 15 files changed, 444 insertions(+), 336 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog