mbox series

[bpf-next,v2,0/2] bpf: Add kfuncs for read-only string operations

Message ID cover.1727335530.git.vmalik@redhat.com (mailing list archive)
Headers show
Series bpf: Add kfuncs for read-only string operations | expand

Message

Viktor Malik Sept. 26, 2024, 7:29 a.m. UTC
Kernel contains highly optimised implementation of traditional string
operations. Expose them as kfuncs to allow BPF programs leverage the
kernel implementation instead of needing to reimplement the operations.

These will be very helpful to bpftrace as it now needs to implement all
the string operations in LLVM IR.

v1 -> v2:
- use bpf_probe_read_kernel_str instead of bpf_probe_read_str in
  selftests as the latter cannot be used on some arches (s390x)

Viktor Malik (2):
  bpf: Add kfuncs for read-only string operations
  selftests/bpf: Add tests for string kfuncs

 kernel/bpf/helpers.c                          |  66 ++++++
 .../selftests/bpf/prog_tests/string_kfuncs.c  |  37 +++
 .../selftests/bpf/progs/test_string_kfuncs.c  | 215 ++++++++++++++++++
 3 files changed, 318 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/string_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_string_kfuncs.c

Comments

Eduard Zingerman Sept. 27, 2024, 1:37 a.m. UTC | #1
On Thu, 2024-09-26 at 09:29 +0200, Viktor Malik wrote:
> Kernel contains highly optimised implementation of traditional string
> operations. Expose them as kfuncs to allow BPF programs leverage the
> kernel implementation instead of needing to reimplement the operations.
> 
> These will be very helpful to bpftrace as it now needs to implement all
> the string operations in LLVM IR.

Note that existing string related helpers take a pointer to a string
and it's maximal length, namely:
- bpf_strtol
- bpf_strtoul
- bpf_snprintf_btf
- bpf_strncmp

The unbounded variants that are being exposed in this patch-set
(like strcmp) are only safe to use if string is guaranteed to be null terminated.
Verifier does not check this property at the moment (idk how easy/hard
such analysis might be).

I'd suggest not to expose unbounded variants of string functions.

[...]
Viktor Malik Sept. 27, 2024, 7:12 a.m. UTC | #2
On 9/27/24 03:37, Eduard Zingerman wrote:
> On Thu, 2024-09-26 at 09:29 +0200, Viktor Malik wrote:
>> Kernel contains highly optimised implementation of traditional string
>> operations. Expose them as kfuncs to allow BPF programs leverage the
>> kernel implementation instead of needing to reimplement the operations.
>>
>> These will be very helpful to bpftrace as it now needs to implement all
>> the string operations in LLVM IR.
> 
> Note that existing string related helpers take a pointer to a string
> and it's maximal length, namely:
> - bpf_strtol
> - bpf_strtoul
> - bpf_snprintf_btf
> - bpf_strncmp
> 
> The unbounded variants that are being exposed in this patch-set
> (like strcmp) are only safe to use if string is guaranteed to be null terminated.
> Verifier does not check this property at the moment (idk how easy/hard
> such analysis might be).
> 
> I'd suggest not to expose unbounded variants of string functions.

That's a great point, thanks. Let me remove the unbounded variants for
now, until we add the null-byte check to the verifier. The bounded
variants will still be useful to bpftrace so I'd love to have them added.

Viktor

> 
> [...]
>