bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Fri, 17 Dec 2021 01:50:24 +0000 (07:20 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 19 Dec 2021 02:11:47 +0000 (18:11 -0800)
commit3363bd0cfbb80dfcd25003cd3815b0ad8b68d0ff
treeca95bb58bbcff2f9b46f49f7f69d7deca5275916
parent7f16d2aa4089a882b6a9899876ac455036a8c34d
bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support

Allow passing PTR_TO_CTX, if the kfunc expects a matching struct type,
and punt to PTR_TO_MEM block if reg->type does not fall in one of
PTR_TO_BTF_ID or PTR_TO_SOCK* types. This will be used by future commits
to get access to XDP and TC PTR_TO_CTX, and pass various data (flags,
l4proto, netns_id, etc.) encoded in opts struct passed as pointer to
kfunc.

For PTR_TO_MEM support, arguments are currently limited to pointer to
scalar, or pointer to struct composed of scalars. This is done so that
unsafe scenarios (like passing PTR_TO_MEM where PTR_TO_BTF_ID of
in-kernel valid structure is expected, which may have pointers) are
avoided. Since the argument checking happens basd on argument register
type, it is not easy to ascertain what the expected type is. In the
future, support for PTR_TO_MEM for kfunc can be extended to serve other
usecases. The struct type whose pointer is passed in may have maximum
nesting depth of 4, all recursively composed of scalars or struct with
scalars.

Future commits will add negative tests that check whether these
restrictions imposed for kfunc arguments are duly rejected by BPF
verifier or not.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211217015031.1278167-4-memxor@gmail.com
kernel/bpf/btf.c