String operations are commonly used in programming and BPF programs are no exception. Since it is cumbersome to reimplement them over and over, this series introduce kfuncs which provide the most common operations. For now, we only limit ourselves to functions which do not copy memory since these usually introduce undefined behaviour in case the source/destination buffers overlap which would have to be prevented by the verifier. The kernel already contains implementations for all of these, however, it is not possible to use them from BPF context. The main reason is that the verifier is not able to check that it is safe to access the entire string and that the string is null-terminated and the function won't loop forever. Therefore, the operations are open-coded using __get_kernel_nofault instead of plain dereference to make them safe. The first patch of the series teaches the verifier to allow read-only memory to be passed to const arguments of kfuncs. With that change, the added kfuncs take strings in three forms: - string literals (i.e. pointers to read-only maps) - global variables (i.e. pointers to read-write maps) - stack-allocated buffers Note that currently, it is not possible to pass strings from the BPF program context (like function args) as the verifier doesn't treat them as neither PTR_TO_MEM nor PTR_TO_BTF_ID. Changes in v4 (all suggested by Andrii): - Open-code all the kfuncs, not just the unbounded variants. - Introduce `pagefault` lock guard to simplify the implementation - Return appropriate error codes (-E2BIG and -EFAULT) on failures - Const-ify all arguments and return values - Add negative test-cases Viktor Malik (4): bpf: Teach vefier to handle const ptrs as args to kfuncs uaccess: Define pagefault lock guard bpf: Add kfuncs for read-only string operations selftests/bpf: Add tests for string kfuncs include/linux/btf.h | 5 + include/linux/uaccess.h | 2 + kernel/bpf/helpers.c | 440 ++++++++++++++++++ kernel/bpf/verifier.c | 10 +- .../selftests/bpf/prog_tests/string_kfuncs.c | 65 +++ .../bpf/progs/string_kfuncs_failure1.c | 51 ++ .../bpf/progs/string_kfuncs_failure2.c | 24 + .../bpf/progs/string_kfuncs_success.c | 87 ++++ 8 files changed, 680 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/string_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/string_kfuncs_failure1.c create mode 100644 tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c create mode 100644 tools/testing/selftests/bpf/progs/string_kfuncs_success.c -- 2.49.0