From f89ebf07496b041cb21beda60ec3452fecf34d10 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Sun, 1 Nov 2026 19:05:16 +0000 Subject: bpf: Reject memory for kfunc projection type args struct __sk_buff and struct xdp_md are projection types of struct sk_buff and struct xdp_buff. A kfunc that takes a projection type casts the argument to the underlying type. When the projection type is the ctx type of the prog, the verifier accepts only the ctx. In other progs, process_arg_ptr_to_btf_id() accepts a pointer to the underlying type. But this function identifies the underlying type only by its name. A struct of the same name in prog BTF also matches. Its objects are memory of the prog. This test also ignores the offset of the register. Thus the kfunc uses memory of the prog, or the middle of an skb, as an skb. Accept the underlying type only from kernel BTF, and only at offset 0. A prog that passes an skb of the kernel sees no change. struct xdp_md has only scalar members. Thus in a prog with another ctx type, resolve_func_arg_type() changes ARG_PTR_TO_BTF_ID to memory, and the verifier accepts memory of the prog. Keep ARG_PTR_TO_BTF_ID for projection types. The first Fixes tag is for this change. That commit merged the conntrack kfunc sets of two prog types with different ctx types. Thus bpf_xdp_ct_{alloc,lookup}() can use memory of the prog as an xdp_buff. Fixes: a4703e318432 ("bpf: Switch to new kfunc flags infrastructure") Fixes: ec209ad86324 ("bpf: verifier: Relax caller requirements for kfunc projection type args") Signed-off-by: Chris Mason Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261101190535.1454146-9-alexei.starovoitov@gmail.com --- include/linux/btf.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/linux/btf.h b/include/linux/btf.h index a18254b7521b..87fad4076f0e 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -631,6 +631,7 @@ s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, struct module *owner); struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id); +bool btf_is_projection_type(const char *pname); bool btf_is_projection_of(const char *pname, const char *tname); bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, const struct btf_type *t, enum bpf_prog_type prog_type, -- cgit v1.2.3 From faea3cda41d3deb4f28e7970c556896b1aa8bd76 Mon Sep 17 00:00:00 2001 From: Alexei Starovoitov Date: Sun, 1 Nov 2026 19:05:19 +0000 Subject: bpf: Compare the arguments of an extension with those of its target An extension replaces its target, a global function of another prog. From BTF, btf_prepare_func_args() derives the kind of each argument, such as the context, memory of a given size or a kernel object. The verifier checks the callers with the BTF of the target and the extension with its own BTF. Only btf_check_func_type_match() compares the two prototypes. For a pointer to a struct, it compares only the struct names and assumes that the pointer is the context. That was true until the commit below added pointers to memory. Later the __arg_* tags added more kinds. Thus an extension can rely on more than the callers guarantee (size, not NULL, kind). Then what the verifier proved for it is false at run time. Keep the result of btf_prepare_func_args() in bpf_func_info_aux and compare the two results at load and on attach to another target. Return -EINVAL unless the kind and the BTF id are the same and the memory is not larger. The extension can also accept NULL where the callers never pass it. An extension with the prototype and the tags of its target loads as before. An argument that is the context only because of __arg_ctx needs the tag in the extension too. The tag __arg_untrusted or __arg_arena on the extension only is safe, but it is rejected because the kind differs. Fixes: e5069b9c23b3 ("bpf: Support pointers in global func args") Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261101190535.1454146-12-alexei.starovoitov@gmail.com --- include/linux/bpf.h | 10 ++++++++++ include/linux/bpf_verifier.h | 8 -------- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 1a8c3e514284..0b44bcb4f56e 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1752,12 +1752,22 @@ enum bpf_func_context { BPF_FUNC_CTX_CNT, }; +struct bpf_subprog_arg_info { + enum bpf_arg_type arg_type; + union { + u32 mem_size; + u32 btf_id; + }; +}; + struct bpf_func_info_aux { u16 linkage; bool unreliable; bool called[BPF_FUNC_CTX_CNT]; bool verified[BPF_FUNC_CTX_CNT]; bool called_with_pkt_ptrs; + /* what the callers of a global function are checked against */ + struct bpf_subprog_arg_info args[MAX_BPF_FUNC_REG_ARGS]; }; enum bpf_jit_poke_reason { diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 8852c173c53e..33dc3d402057 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -923,14 +923,6 @@ struct bpf_func_ptr { /* the subprog that a bpf_func_ptr pointed to was removed as dead code */ #define BPF_FUNC_PTR_DELETED ((u32)-1) -struct bpf_subprog_arg_info { - enum bpf_arg_type arg_type; - union { - u32 mem_size; - u32 btf_id; - }; -}; - enum priv_stack_mode { PRIV_STACK_UNKNOWN, NO_PRIV_STACK, -- cgit v1.2.3