bpf/verifier: allow kfunc to return an allocated mem
authorBenjamin Tissoires <benjamin.tissoires@redhat.com>
Tue, 6 Sep 2022 15:13:02 +0000 (17:13 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 7 Sep 2022 18:05:17 +0000 (11:05 -0700)
commiteb1f7f71c126c8fd50ea81af98f97c4b581ea4ae
tree8eec02763efd316fb6daf76680acbc9bc72cf2e0
parentf9b348185f4d684cc19e6bd9b87904823d5aa5ed
bpf/verifier: allow kfunc to return an allocated mem

For drivers (outside of network), the incoming data is not statically
defined in a struct. Most of the time the data buffer is kzalloc-ed
and thus we can not rely on eBPF and BTF to explore the data.

This commit allows to return an arbitrary memory, previously allocated by
the driver.
An interesting extra point is that the kfunc can mark the exported
memory region as read only or read/write.

So, when a kfunc is not returning a pointer to a struct but to a plain
type, we can consider it is a valid allocated memory assuming that:
- one of the arguments is either called rdonly_buf_size or
  rdwr_buf_size
- and this argument is a const from the caller point of view

We can then use this parameter as the size of the allocated memory.

The memory is either read-only or read-write based on the name
of the size parameter.

Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Link: https://lore.kernel.org/r/20220906151303.2780789-7-benjamin.tissoires@redhat.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
include/linux/bpf_verifier.h
include/linux/btf.h
kernel/bpf/btf.c
kernel/bpf/verifier.c