linux-stable/Documentation/bpf
David Vernet 3f00c52393 bpf: Allow trusted pointers to be passed to KF_TRUSTED_ARGS kfuncs
Kfuncs currently support specifying the KF_TRUSTED_ARGS flag to signal
to the verifier that it should enforce that a BPF program passes it a
"safe", trusted pointer. Currently, "safe" means that the pointer is
either PTR_TO_CTX, or is refcounted. There may be cases, however, where
the kernel passes a BPF program a safe / trusted pointer to an object
that the BPF program wishes to use as a kptr, but because the object
does not yet have a ref_obj_id from the perspective of the verifier, the
program would be unable to pass it to a KF_ACQUIRE | KF_TRUSTED_ARGS
kfunc.

The solution is to expand the set of pointers that are considered
trusted according to KF_TRUSTED_ARGS, so that programs can invoke kfuncs
with these pointers without getting rejected by the verifier.

There is already a PTR_UNTRUSTED flag that is set in some scenarios,
such as when a BPF program reads a kptr directly from a map
without performing a bpf_kptr_xchg() call. These pointers of course can
and should be rejected by the verifier. Unfortunately, however,
PTR_UNTRUSTED does not cover all the cases for safety that need to
be addressed to adequately protect kfuncs. Specifically, pointers
obtained by a BPF program "walking" a struct are _not_ considered
PTR_UNTRUSTED according to BPF. For example, say that we were to add a
kfunc called bpf_task_acquire(), with KF_ACQUIRE | KF_TRUSTED_ARGS, to
acquire a struct task_struct *. If we only used PTR_UNTRUSTED to signal
that a task was unsafe to pass to a kfunc, the verifier would mistakenly
allow the following unsafe BPF program to be loaded:

SEC("tp_btf/task_newtask")
int BPF_PROG(unsafe_acquire_task,
             struct task_struct *task,
             u64 clone_flags)
{
        struct task_struct *acquired, *nested;

        nested = task->last_wakee;

        /* Would not be rejected by the verifier. */
        acquired = bpf_task_acquire(nested);
        if (!acquired)
                return 0;

        bpf_task_release(acquired);
        return 0;
}

To address this, this patch defines a new type flag called PTR_TRUSTED
which tracks whether a PTR_TO_BTF_ID pointer is safe to pass to a
KF_TRUSTED_ARGS kfunc or a BPF helper function. PTR_TRUSTED pointers are
passed directly from the kernel as a tracepoint or struct_ops callback
argument. Any nested pointer that is obtained from walking a PTR_TRUSTED
pointer is no longer PTR_TRUSTED. From the example above, the struct
task_struct *task argument is PTR_TRUSTED, but the 'nested' pointer
obtained from 'task->last_wakee' is not PTR_TRUSTED.

A subsequent patch will add kfuncs for storing a task kfunc as a kptr,
and then another patch will add selftests to validate.

Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20221120051004.3605026-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2022-11-20 09:16:21 -08:00
..
libbpf bpf, docs: Remove deprecated xsk libbpf APIs description 2022-07-08 14:18:50 +02:00
bpf_design_QA.rst bpf: Remove local kptr references in documentation 2022-11-14 21:52:45 -08:00
bpf_devel_QA.rst bpf/docs: Include blank lines between bullet points in bpf_devel_QA.rst 2022-11-17 16:26:42 +01:00
bpf_licensing.rst bpf: Document BPF licensing. 2021-09-22 23:13:13 +02:00
bpf_prog_run.rst Documentation/bpf: Add documentation for BPF_PROG_RUN 2022-03-09 14:19:22 -08:00
btf.rst docs/bpf: Update documentation for BTF_KIND_FUNC 2022-07-19 09:42:09 -07:00
clang-notes.rst bpf, docs: Add Clang note about BPF_ALU 2022-09-30 13:41:25 -07:00
classic_vs_extended.rst bpf, docs: Split the comparism to classic BPF from instruction-set.rst 2021-12-30 16:31:44 -08:00
drgn.rst bpf: Document bpf_inspect drgn tool 2020-03-26 00:08:36 +01:00
faq.rst bpf, docs: Fix ordering of bpf documentation 2021-11-17 23:24:01 +01:00
helpers.rst bpf, docs: Fix ordering of bpf documentation 2021-11-17 23:24:01 +01:00
index.rst bpf, docs: DEVMAPs and XDP_REDIRECT 2022-11-18 23:16:31 +01:00
instruction-set.rst Networking changes for 6.1. 2022-10-04 13:38:03 -07:00
kfuncs.rst bpf: Allow trusted pointers to be passed to KF_TRUSTED_ARGS kfuncs 2022-11-20 09:16:21 -08:00
linux-notes.rst bpf, docs: Delete misformatted table. 2022-10-01 08:51:26 -07:00
llvm_reloc.rst bpf, docs: Add llvm_reloc.rst to explain llvm bpf relocations 2021-05-28 22:12:04 +02:00
map_array.rst docs/bpf: Fix sample code in MAP_TYPE_ARRAY docs 2022-11-15 21:33:47 -08:00
map_cgroup_storage.rst Remove duplicate words inside documentation 2022-09-27 13:21:43 -06:00
map_cgrp_storage.rst docs/bpf: Add documentation for new cgroup local storage 2022-10-25 23:19:20 -07:00
map_cpumap.rst bpf, docs: Fixup cpumap sphinx >= 3.1 warning 2022-11-14 19:10:57 +01:00
map_devmap.rst bpf, docs: DEVMAPs and XDP_REDIRECT 2022-11-18 23:16:31 +01:00
map_hash.rst bpf, docs: document BPF_MAP_TYPE_HASH and variants 2022-07-19 10:38:03 -07:00
map_lpm_trie.rst docs/bpf: Document BPF_MAP_TYPE_LPM_TRIE map 2022-11-11 11:32:49 -08:00
map_of_maps.rst docs/bpf: Document BPF ARRAY_OF_MAPS and HASH_OF_MAPS 2022-11-11 11:32:54 -08:00
map_queue_stack.rst docs/bpf: Document BPF map types QUEUE and STACK 2022-11-11 11:34:39 -08:00
maps.rst bpf, docs: Reformat BPF maps page to be more readable 2022-10-20 18:53:34 -07:00
other.rst bpf, docs: Fix ordering of bpf documentation 2021-11-17 23:24:01 +01:00
prog_cgroup_sockopt.rst bpf: Document optval > PAGE_SIZE behavior for sockopt hooks 2020-06-17 10:54:05 -07:00
prog_cgroup_sysctl.rst bpf: Document BPF_PROG_TYPE_CGROUP_SYSCTL 2019-04-18 16:05:36 -07:00
prog_flow_dissector.rst bpf/flow_dissector: add mode to enforce global BPF flow dissector 2019-10-07 20:16:33 -07:00
prog_lsm.rst bpf, docs: Rename bpf_lsm.rst to prog_lsm.rst 2021-11-17 23:23:58 +01:00
prog_sk_lookup.rst bpf: sk_lookup: Add user documentation 2020-08-24 14:46:50 -07:00
programs.rst bpf, docs: Fix ordering of bpf documentation 2021-11-17 23:24:01 +01:00
redirect.rst bpf, docs: DEVMAPs and XDP_REDIRECT 2022-11-18 23:16:31 +01:00
ringbuf.rst docs/bpf: Remove source code links 2020-09-14 18:46:54 -07:00
s390.rst bpf: Add s390 testing documentation 2019-10-30 16:25:31 +01:00
syscall_api.rst bpf, docs: Fix ordering of bpf documentation 2021-11-17 23:24:01 +01:00
test_debug.rst bpf, docs: Fix ordering of bpf documentation 2021-11-17 23:24:01 +01:00
verifier.rst bpf, docs: Add a missing colon in verifier.rst 2022-02-28 18:20:35 +01:00