bpf: Add user memory kfuncs
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
- v3 Latest8 patches 5 repliesApplied 8/8
- v28 patches 10 repliesSuperseded
- v18 patches 9 repliesSuperseded
Changes in v3 from the cover letter
- use Alexei's change for checking the const [Aleexi]
- drop ia32 support/hack for vm_mmap [Alexei]
- remove leftover srcptr suffix in kfunc declaration
- drop extra verifier test for non-sleepable kprobe
Patches in v3 All 8 applied
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/8 | bpf: Honor const on generic kfunc memory argumentsDocumentation/ kernel/ | +25 | 3 replies | Applied |
| 2/8 | bpf: Add user memory kfuncsDocumentation/ kernel/ kernel/ | +129 | 2 replies | Applied |
| 3/8 | selftests/bpf: Add get_procmap_info helpertools/ tools/ | +34 | 0 replies | Applied |
| 4/8 | selftests/bpf: Test const kfunc memory argumentstools/ tools/ tools/ +2 more | +38 | 0 replies | Applied |
| 5/8 | selftests/bpf: Test user memory mmap kfuncstools/ tools/ | +154 | 0 replies | Applied |
| 6/8 | selftests/bpf: Test user memory page exectools/ tools/ | +97 | 0 replies | Applied |
| 7/8 | selftests/bpf: Test bpf_copy_to_user kfunctools/ tools/ | +123 | 0 replies | Applied |
| 8/8 | selftests/bpf: Test user memory kfunc errorstools/ tools/ | +76 | 0 replies | Applied |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v3 yet.
Cover letter [PATCHv3 bpf-next 0/8] bpf: Add user memory kfuncs
hi,
BPF programs have no supported interface for creating or removing private anonymous mappings in the current task. This limits use cases that need normal or executable memory in a live process, such as replacing a function or changing environment variables.
Although bpf_probe_write_user() can write to user memory, it disables page faults during the copy and therefore cannot handle memory that needs to be faulted in.
This series adds three sleepable kfuncs for those operations:
- bpf_user_anon_mmap(len, prot) creates a private anonymous mapping in the current task and returns its address. The requested protections can include execute permission.
- bpf_copy_to_user(dst, src, len, flags) copies data into the current task’s address space, handles page faults, and reports copy errors.
- bpf_user_munmap(addr, len) removes a mapping.
Together, the kfuncs provide a small interface for allocating, populating, and removing memory:
addr = bpf_user_anon_mmap(len, prot); ret = bpf_copy_to_user((void *)addr, src, len, flags); bpf_user_munmap(addr, len);
This interface lets a BPF program perform the operations in the current task’s address space without changing bpf_probe_write_user()’s fault behavior or introducing a separate mapping abstraction.
thanks,
jirka
rfc: https://lore.kernel.org/bpf/[email protected]/ v1: https://lore.kernel.org/bpf/[email protected]/ v2: https://lore.kernel.org/bpf/[email protected]/
v3 changes:
- use Alexei's change for checking the const [Aleexi]
- drop ia32 support/hack for vm_mmap [Alexei]
- remove leftover srcptr suffix in kfunc declaration
- drop extra verifier test for non-sleepable kprobe
v2 changes:
- infer read-only from constness of a pointer instead of srcptr argument suffix [Andrii]
- rename bpf_user_mmap to bpf_user_anon_mmap [Andrii]
- use arch_validate_prot to validate prot argument in mmap
- add the x86 compat mmap workaround
- assorted small changes
v1 changes:
- follow vm_mmap/vm_munmap design suggested by Alexei
- allow user space write only to current process, because we don't have remote task use case and it simplifies security checks
- many changes in tests
---
Jiri Olsa (8):
bpf: Honor const on generic kfunc memory arguments
bpf: Add user memory kfuncs selftests/bpf: Add get_procmap_info helper selftests/bpf: Test const kfunc memory arguments selftests/bpf: Test user memory mmap kfuncs selftests/bpf: Test user memory page exec selftests/bpf: Test bpf_copy_to_user kfunc selftests/bpf: Test user memory kfunc errors
Documentation/bpf/kfuncs.rst | 21 ++++ kernel/bpf/helpers.c | 107 ++++++++++++++++ kernel/bpf/syscall.c | 10 +- kernel/bpf/verifier.c | 24 +++- tools/testing/selftests/bpf/prog_tests/kfunc_call.c | 2 + tools/testing/selftests/bpf/prog_tests/user_mem.c | 349 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/kfunc_call_fail.c | 10 ++ tools/testing/selftests/bpf/progs/kfunc_call_test.c | 14 +++ tools/testing/selftests/bpf/progs/user_mem.c | 101 +++++++++++++++ tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 10 ++ tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h | 2 + tools/testing/selftests/bpf/trace_helpers.c | 38 +++++- tools/testing/selftests/bpf/trace_helpers.h | 1 + 13 files changed, 676 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/user_mem.c create mode 100644 tools/testing/selftests/bpf/progs/user_mem.c