From: Viktor Malik <vmalik@redhat.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>, Viktor Malik <vmalik@redhat.com>
Subject: [PATCH bpf-next v4 0/4] bpf: Add kfuncs for read-only string operations
Date: Wed, 7 May 2025 08:40:35 +0200 [thread overview]
Message-ID: <cover.1746598898.git.vmalik@redhat.com> (raw)
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
next reply other threads:[~2025-05-07 6:41 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-07 6:40 Viktor Malik [this message]
2025-05-07 6:40 ` [PATCH bpf-next v4 1/4] bpf: Teach vefier to handle const ptrs as args to kfuncs Viktor Malik
2025-05-08 9:08 ` Matt Bobrowski
2025-05-09 16:20 ` Alexei Starovoitov
2025-05-13 6:48 ` Matt Bobrowski
2025-05-13 7:54 ` Viktor Malik
2025-05-13 14:58 ` Alexei Starovoitov
2025-05-13 7:58 ` Viktor Malik
2025-05-07 6:40 ` [PATCH bpf-next v4 2/4] uaccess: Define pagefault lock guard Viktor Malik
2025-05-08 10:00 ` Matt Bobrowski
2025-05-09 18:20 ` Andrii Nakryiko
2025-05-13 6:55 ` Matt Bobrowski
2025-05-07 6:40 ` [PATCH bpf-next v4 3/4] bpf: Add kfuncs for read-only string operations Viktor Malik
2025-05-08 9:41 ` Matt Bobrowski
2025-05-09 16:39 ` Alexei Starovoitov
2025-05-28 9:05 ` Viktor Malik
2025-05-15 12:32 ` Viktor Malik
2025-05-09 18:20 ` Andrii Nakryiko
2025-05-09 21:37 ` Eduard Zingerman
2025-05-09 22:03 ` Andrii Nakryiko
2025-05-15 12:27 ` Viktor Malik
2025-05-15 17:17 ` Andrii Nakryiko
2025-05-16 5:59 ` Viktor Malik
2025-05-16 15:50 ` Andrii Nakryiko
2025-05-07 6:40 ` [PATCH bpf-next v4 4/4] selftests/bpf: Add tests for string kfuncs Viktor Malik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cover.1746598898.git.vmalik@redhat.com \
--to=vmalik@redhat.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.