]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
bpf: Add kfuncs for read-only string operations
authorViktor Malik <vmalik@redhat.com>
Thu, 26 Jun 2025 06:08:29 +0000 (08:08 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 26 Jun 2025 16:44:45 +0000 (09:44 -0700)
commite91370550f1fe6fa3b02e8bf9762e3dc0a02fcad
treea7a78119632e9e3a046e132448266e62bbc24989
parent3a95a561f2763e3854e207de3ea821e795a1f1e0
bpf: Add kfuncs for read-only string operations

String operations are commonly used so this exposes the most common ones
to BPF programs. For now, we limit ourselves to operations which do not
copy memory around.

Unfortunately, most in-kernel implementations assume that strings are
%NUL-terminated, which is not necessarily true, and therefore we cannot
use them directly in the BPF context. Instead, we open-code them using
__get_kernel_nofault instead of plain dereference to make them safe and
limit the strings length to XATTR_SIZE_MAX to make sure the functions
terminate. When __get_kernel_nofault fails, functions return -EFAULT.
Similarly, when the size bound is reached, the functions return -E2BIG.
In addition, we return -ERANGE when the passed strings are outside of
the kernel address space.

Note that thanks to these dynamic safety checks, no other constraints
are put on the kfunc args (they are marked with the "__ign" suffix to
skip any verifier checks for them).

All of the functions return integers, including functions which normally
(in kernel or libc) return pointers to the strings. The reason is that
since the strings are generally treated as unsafe, the pointers couldn't
be dereferenced anyways. So, instead, we return an index to the string
and let user decide what to do with it. This also nicely fits with
returning various error codes when necessary (see above).

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Viktor Malik <vmalik@redhat.com>
Link: https://lore.kernel.org/r/4b008a6212852c1b056a413f86e3efddac73551c.1750917800.git.vmalik@redhat.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/helpers.c