From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380525; b=W12/6LW9wDXmppTna6ePXp02o6YjGJ2nYiMI4vg+UGfac7fMZa0wUafbIYNm4qgDb40f 69QdlpC1C7lcI/KaKTU2t3Jbkk0fvxyK2in2dtQVvFoTX+qUQVoMHDLf4G2cND+3cE73A fZLdtZY5rw51G8WDJQJUuUR91S3+9Lateir8zk5qZBNA8w1v6+Sl44ItuYIN+F2eVOZ/0 jvbCiDJA4nie6eu1uCCefdfimcL+ISNhlNUEKR+1w/0Fm9m9nBIqmPu4bV8xSZTa65jqI T1CEhc2V7kxjxWB/lxanhiJyB4GZ77xZF0yRFRborITPrF8DElNdgtMl/j4ow/s9Rbg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380525; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=A1HBOqFm1p8XeBJ968yGrc/YgTLexlsfQwyyqnjogIg=; b=mjZgt79pkXaKUXzpHlfO4Xmsoicbk/4P6fiBheIdURfA85E2dhIvRdLGWKIXMqAkrBmC FpnNLMrAopfTSFRfAsT2xHzcQB1iIMhxMdrueIsvuPsrUMDPUuh/XxpRkHad2VMsIi/EY e2w/vmtLJMopnh0xGxLHa1yjxAkwOygjbvVHUKakdJQoRapDNb5bFPd7z0pp/pUh/nc/P eCuAQXevL/J4AiHzEMoaF+7LWCyUPAmSRooarexvuH4l7REKmvWs1wZ+ngATmRCTqTcw5 MGqSlJZXIHwxZjTHDiRews/mA46IqoK5tzNSDEiKjxePJVThnnaUfzxMGnQ+2KVZadQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=RkZi+jYq; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2048606A681; Fri, 4 Dec 2026 10:35:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=RkZi+jYq Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D8A91F00897; Fri, 4 Dec 2026 10:35:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380524; bh=A1HBOqFm1p8XeBJ968yGrc/YgTLexlsfQwyyqnjogIg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RkZi+jYqkv66RPnpqZYr4ZoNrYgFnoB3KJNZq7DATdKXbvTqg63cq9kqX1wW+NpZ1 saTYA3/pANhSpgOJ0wu8PqFSuuwKit+vu1UtATmJxQd3RQv8QChqmnXPcMysVbk22d cg9W6vMoz6PYCVQ++l//81LdghNG++R/wOTOk+CI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Anton Protopopov , Sasha Levin Subject: [PATCH 7.3 270/742] bpf: Check that an extension fits the hook of its target Date: Fri, 4 Dec 2026 19:02:14 +0900 Message-ID: <20261204100642.228268501@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit f44d0e6a1d684167d4fc58d313077ba21ad6f301 ] An extension runs in the hook of its target, and the checks for the hook do not see it. Tracing, LSM and struct_ops programs are checked against the BTF of their hook. An extension takes the type of its target, but it has no attach_btf and no st_ops, and attach_btf_id and the prototype are those of the function that it replaces: - tp_btf: it can read more arguments than the tracepoint has. - LSM: the range of bpf_get_retval() and bpf_set_retval(), and the helpers that only some hooks have, are looked up by a wrong id. - tp_btf and struct_ops: the load can oops. fentry, fexit, fsession and iterators cannot be extended already. Refuse the rest. freplace of global functions of tp_btf, fmod_ret and LSM programs goes away. Targets of other types load with any expected_attach_type. Do not take their extension for tp_btf or for an iterator. Some properties of a program are looked at when it is attached or run. Nobody looks at those of an extension: - bpf_get_local_storage() returns the storage of the attached program, the extension is verified against its own map. - bpf_override_return() needs a function that allows error injection. - skb->cb is saved, cleared and restored only for programs that access it. Ask for the storage map of the target, at load and at attach. Refuse the attach when the target does not call bpf_override_return() itself, or does not access skb->cb. A tail call counts as an access to skb->cb. Fixes: be8704ff07d2 ("bpf: Introduce dynamic program extensions") Signed-off-by: Alexei Starovoitov Acked-by: Anton Protopopov Link: https://patch.msgid.link/20261125032938.670542-6-alexei.starovoitov@gmail.com Signed-off-by: Sasha Levin --- kernel/bpf/syscall.c | 18 ++++++++++++++++++ kernel/bpf/verifier.c | 38 +++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 1836cf9403fe2..12f51650fe6b7 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3830,6 +3830,24 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, err = -EINVAL; goto out_unlock; } + /* Where the return can be overridden is checked for the target only */ + if (prog->type == BPF_PROG_TYPE_EXT && + prog->kprobe_override && !tgt_prog->kprobe_override) { + err = -EINVAL; + goto out_unlock; + } + /* bpf_prog_run_save_cb() and bpf_prog_run_clear_cb() look at the target only */ + if (prog->type == BPF_PROG_TYPE_EXT && + (tgt_prog->type == BPF_PROG_TYPE_SOCKET_FILTER || + tgt_prog->type == BPF_PROG_TYPE_CGROUP_SKB || + tgt_prog->type == BPF_PROG_TYPE_LWT_IN || + tgt_prog->type == BPF_PROG_TYPE_LWT_OUT || + tgt_prog->type == BPF_PROG_TYPE_LWT_XMIT || + tgt_prog->type == BPF_PROG_TYPE_LWT_SEG6LOCAL) && + prog->cb_access && !tgt_prog->cb_access) { + err = -EINVAL; + goto out_unlock; + } /* * The size of these contexts depends on what the program is attached * to, or run with. That is compared with max_ctx_offset of the program, diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8b80c25cd3695..74a5277c5b352 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20380,6 +20380,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, } if (tgt_prog) { struct bpf_prog_aux *aux = tgt_prog->aux; + enum bpf_cgroup_storage_type stype; bool tgt_changes_pkt_data; bool tgt_might_sleep; @@ -20426,6 +20427,16 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, "Extension programs should be JITed\n"); return -EINVAL; } + /* bpf_get_local_storage() returns the storage of the target */ + for_each_cgroup_storage_type(stype) { + struct bpf_map *storage = prog->aux->cgroup_storage[stype]; + + if (storage && storage != aux->cgroup_storage[stype]) { + bpf_log(log, + "Extension program has another cgroup storage than original\n"); + return -EINVAL; + } + } /* * How much of the buffer of the tracepoint is used * is checked for the program that is attached to it. @@ -20476,16 +20487,14 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, bpf_log(log, "Cannot recursively attach\n"); return -EINVAL; } - if (tgt_prog->type == BPF_PROG_TYPE_TRACING && - prog_extension && - (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || - tgt_prog->expected_attach_type == BPF_TRACE_FEXIT || - tgt_prog->expected_attach_type == BPF_TRACE_FENTRY_MULTI || - tgt_prog->expected_attach_type == BPF_TRACE_FEXIT_MULTI || - tgt_prog->expected_attach_type == BPF_TRACE_FSESSION || - tgt_prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI)) { - /* Program extensions can extend all program types - * except fentry/fexit. The reason is the following. + if (prog_extension && + (tgt_prog->type == BPF_PROG_TYPE_TRACING || + tgt_prog->type == BPF_PROG_TYPE_LSM || + tgt_prog->type == BPF_PROG_TYPE_STRUCT_OPS)) { + /* + * These types are checked against the BTF of their hook, + * and an extension has the BTF of its target instead. + * There is one more reason for fentry/fexit. * The fentry/fexit programs are used for performance * analysis, stats and can be attached to any program * type. When extension program is replacing XDP function @@ -20498,7 +20507,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, * beyond reasonable stack size. Hence extending fentry * is not allowed. */ - bpf_log(log, "Cannot extend fentry/fexit/fsession\n"); + bpf_log(log, "Cannot extend tracing, LSM and struct_ops programs\n"); return -EINVAL; } } else { @@ -20825,10 +20834,13 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; } - if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { + /* An extension has expected_attach_type of its target, it can be any value */ + if (prog->type != BPF_PROG_TYPE_EXT && + prog->expected_attach_type == BPF_TRACE_RAW_TP) { prog->aux->attach_btf_trace = true; return 0; - } else if (prog->expected_attach_type == BPF_TRACE_ITER) { + } else if (prog->type != BPF_PROG_TYPE_EXT && + prog->expected_attach_type == BPF_TRACE_ITER) { return bpf_iter_prog_supported(prog); } -- 2.53.0