Skip to content

bpf: additional use-cases for untrusted PTR_TO_MEM

Eduard Zingerman bpf 2 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest8 patches 0 repliesApplied 8/8
  2. v18 patches 25 repliesSuperseded

Changes in v2 from the cover letter

- Added safety check in btf_prepare_func_args() to ensure that only struct or primitive types could be combined with __arg_untrusted (Alexei).
- Removed unnecessary 'continue' btf_check_func_arg_match() (Alexei).
- Added test cases for __arg_untrusted pointers to enum and __arg_untrusted combined with non-kernel type (Kumar).
- Added acks from Kumar.

Patches in v2 All 8 applied

#Subject and filesSizeRepliesApplied
1/8bpf: make makr_btf_ld_reg return error for unexpected reg typeskernel/bpf/verifier.c+39 -200 repliesApplied
2/8bpf: rdonly_untrusted_mem for btf id walk pointer leafskernel/bpf/btf.c kernel/bpf/verifier.c tools/testing/selftests/bpf/prog_tests/linked_list.c+12 -10 repliesApplied
3/8selftests/bpf: ptr_to_btf_id struct walk ending with primitive pointertools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c+31 -00 repliesApplied
4/8bpf: attribute __arg_untrusted for global function parameterskernel/bpf/btf.c kernel/bpf/verifier.c+39 -50 repliesApplied
5/8libbpf: __arg_untrusted in bpf_helpers.htools/lib/bpf/bpf_helpers.h+1 -00 repliesApplied
6/8selftests/bpf: test cases for __arg_untrustedtools/testing/selftests/bpf/progs/verifier_global_ptr_args.c+81 -00 repliesApplied
7/8bpf: support for void/primitive __arg_untrusted global func paramsinclude/linux/btf.h kernel/bpf/btf.c kernel/bpf/verifier.c+19 -40 repliesApplied
8/8selftests/bpf: tests for __arg_untrusted void * global func paramstools/testing/selftests/bpf/progs/verifier_global_ptr_args.c+53 -00 repliesApplied

Reviews trailers per patch

All 8 patches have a Reviewed-by or Acked-by.

PatchKumar Kartikeya Dwivedi
1/8A-b
2/8A-b
3/8A-b
4/8A-b
5/8A-b
6/8A-b
7/8A-b
8/8A-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/8] bpf: additional use-cases for untrusted PTR_TO_MEM

This patch set introduces two usability enhancements leveraging untrusted pointers to mem:
- When reading a pointer field from a PTR_TO_BTF_ID, the resulting value is now assumed to be PTR_TO_MEM|MEM_RDONLY|PTR_UNTRUSTED instead of SCALAR_VALUE, provided the pointer points to a primitive type.
- __arg_untrusted attribute for global function parameters, allowed for pointer arguments of both structural and primitive types:
- For structural types, the attribute produces PTR_TO_BTF_ID|PTR_UNTRUSTED.
- For primitive types, it yields PTR_TO_MEM|MEM_RDONLY|PTR_UNTRUSTED.

Here are examples enabled by the series:

  struct foo {
    int *arr;
  };
  ...
  p = bpf_core_cast(..., struct foo);
  bpf_for(i, 0, ...) {
    ... p->arr[i] ...       // load at any offset is allowed
  }

  int memcmp(void *a __arg_untrusted, void *b __arg_untrusted, size_t n) {
    bpf_for(i, 0, n)
      if (a[i] - b[i])      // load at any offset is allowed
        return ...;
    return 0;
  }

The patch-set was inspired by Anrii's series [1]. The goal of that series was to define a generic global glob_match function, capable to accept any pointer type:

  __weak int glob_match(const char *pat, const char *str);

  char filename_glob[32];

  void foo(...) {
    ...
    task = bpf_get_current_task_btf();
    filename = task->mm->exe_file->f_path.dentry->d_name.name;
    ... match_glob(filename_glob,  // pointer to map value
                   filename) ...   // scalar
  }

At the moment, there is no straightforward way to express such a function. This patch-set makes it possible to define it as follows:

__weak int glob_match(const char *pat __arg_untrusted,

                        const char *str __arg_untrusted);

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

Changelog:
v1: https://lore.kernel.org/bpf/[email protected]/
v1 -> v2:
- Added safety check in btf_prepare_func_args() to ensure that only struct or primitive types could be combined with __arg_untrusted (Alexei).
- Removed unnecessary 'continue' btf_check_func_arg_match() (Alexei).
- Added test cases for __arg_untrusted pointers to enum and __arg_untrusted combined with non-kernel type (Kumar).
- Added acks from Kumar.

Eduard Zingerman (8):
bpf: make makr_btf_ld_reg return error for unexpected reg types
bpf: rdonly_untrusted_mem for btf id walk pointer leafs selftests/bpf: ptr_to_btf_id struct walk ending with primitive pointer
bpf: attribute __arg_untrusted for global function parameters
libbpf: __arg_untrusted in bpf_helpers.h
selftests/bpf: test cases for __arg_untrusted
bpf: support for void/primitive __arg_untrusted global func params selftests/bpf: tests for __arg_untrusted void * global func params

 include/linux/btf.h                           |   1 +
 kernel/bpf/btf.c                              |  57 +++++++-
 kernel/bpf/verifier.c                         |  77 +++++++---
 tools/lib/bpf/bpf_helpers.h                   |   1 +
 .../selftests/bpf/prog_tests/linked_list.c    |   2 +-
 .../bpf/progs/mem_rdonly_untrusted.c          |  31 ++++
 .../bpf/progs/verifier_global_ptr_args.c      | 134 ++++++++++++++++++
 7 files changed, 274 insertions(+), 29 deletions(-)
Signature · 1 line
2.49.0