bpf: Enable annotating trusted nested pointers
authorDavid Vernet <void@manifault.com>
Fri, 20 Jan 2023 19:25:15 +0000 (13:25 -0600)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 25 Jan 2023 04:15:01 +0000 (20:15 -0800)
commit57539b1c0ac2dcccbe64a7675ff466be009c040f
tree6a1adf6f58fa9a8910ca352da02dd7a7a14e1ef8
parent0eb9d19e2201068260e439a5c96dc85f9f3722a2
bpf: Enable annotating trusted nested pointers

In kfuncs, a "trusted" pointer is a pointer that the kfunc can assume is
safe, and which the verifier will allow to be passed to a
KF_TRUSTED_ARGS kfunc. Currently, a KF_TRUSTED_ARGS kfunc disallows any
pointer to be passed at a nonzero offset, but sometimes this is in fact
safe if the "nested" pointer's lifetime is inherited from its parent.
For example, the const cpumask_t *cpus_ptr field in a struct task_struct
will remain valid until the task itself is destroyed, and thus would
also be safe to pass to a KF_TRUSTED_ARGS kfunc.

While it would be conceptually simple to enable this by using BTF tags,
gcc unfortunately does not yet support this. In the interim, this patch
enables support for this by using a type-naming convention. A new
BTF_TYPE_SAFE_NESTED macro is defined in verifier.c which allows a
developer to specify the nested fields of a type which are considered
trusted if its parent is also trusted. The verifier is also updated to
account for this. A patch with selftests will be added in a follow-on
change, along with documentation for this feature.

Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230120192523.3650503-2-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
kernel/bpf/btf.c
kernel/bpf/verifier.c