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=1796380438; b=l71pwlwb4sq+MdIQGojty3CZgFzH+hCyTcOqgVqe8jIEDC+dtLV6uL3cfgpmq4VTpa0I pBQY+oc5kSCS/pRCS/OXS51oizKL9P6MjfWvFdAtHydgxKYLOQQWWYruRp3UQk0/mBl7g zcqpX4gMfpAe0YGbDHjRUMDEkdutAOBy8lKAdRw2d+x5nD5a2sHcj1KmTdSFdRcYgFe5h a0+qFEKV7D93Q6OASxzeiME5gGlRs1DWPu7qOxHlvIHgb704e5t4R40own7B4fi4fPHZL X035rBhtYWJukvlJ5Uw+FBinid/CxAgQeXrKFKKk0JePzD0VAwVhvueFOz3s+/Td91w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380438; 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=jeKl9XP+47kOkXfiS0frMuyU6o78p3IIIZsvQKiugA8=; b=auMkST6z/OsaBSD0qHK64bURnouKz556npo0sxpkod30QFRrLd1ThnvlF5nUZpCy+RCR NtbYdQcJe9w+HIbHZCSh1pckcrDW+YA01lFKEgO/BAelLIO462m1CZhwuP/jiuj+Ibgxr jAFFWbyG5DY6xZtBJKNVJbHkH3nVuOP8M9W4k4caOPFEWaTNu1UiX9GSdK4iS9FNXgvBG EHLJNQkzu1lfMN1ppV3lI8BPaF+8Gn3oGNwjWRVbsiP7vYKKH+rcsNxAPyXCRAyDvozc4 n0cZv22kZPVjY1EwtrAMLRn31SwD4llOYlWS4DWaR7K9qjJx5OVFdXHXBJLeeTJOS9w== 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=NL6Tr+WC; 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 437FF606A68B; Fri, 4 Dec 2026 10:33:58 +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=NL6Tr+WC Received: by smtp.kernel.org (Postfix) with ESMTPSA id B41781F000CE; Fri, 4 Dec 2026 10:33:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380438; bh=jeKl9XP+47kOkXfiS0frMuyU6o78p3IIIZsvQKiugA8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NL6Tr+WColIvEEIYWLhbTPr+kUIddzq9wbzNbLQPPOoKMADXlRhu0+qP3D99CXjHI RtqQVs0ObNGX5n20ms8DbpaJz3Zrv4MjAWz6KAOAig+AdDSjFWvV95dja7/1+tHfIw 9WAk/Ru30b2/oF0oCNs5p4CTO/vsm8xDraxBKQbs= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Sasha Levin Subject: [PATCH 7.3 274/742] bpf: Check that a program and a struct_ops map are for the same struct Date: Fri, 4 Dec 2026 19:02:18 +0900 Message-ID: <20261204100642.327967740@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 80184a7381c11eae0103f0c432e2adf6a5fd476f ] A map takes a program when the id of the struct and the index of the member are the same. Ids of types are not unique among modules. Compare BTF too. BPF_PROG_ASSOC_STRUCT_OPS takes any struct_ops map, and the callers of bpf_prog_get_assoc_struct_ops() take the result for their own struct. Pass the struct_ops that the caller wants, return NULL for another one. The callers are scx_prog_sched() of sched_ext and bpf_testmod, they change with the prototype. Fixes: f6be98d19985 ("bpf, net: switch to dynamic registration") Fixes: b5709f6d26d6 ("bpf: Support associating BPF program with struct_ops") Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261125032938.670542-10-alexei.starovoitov@gmail.com Signed-off-by: Sasha Levin --- include/linux/bpf.h | 6 ++++-- kernel/bpf/bpf_struct_ops.c | 8 ++++++-- kernel/sched/ext/internal.h | 5 ++++- tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 6 ++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 01d046553ec90..a5e49895d6901 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2307,7 +2307,8 @@ static inline void bpf_module_put(const void *data, struct module *owner) int bpf_struct_ops_link_create(union bpf_attr *attr); int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map); void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog); -void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux); +void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux, + const struct bpf_struct_ops *st_ops); u32 bpf_struct_ops_id(const void *kdata); int bpf_struct_ops_for_each_prog(const void *kdata, int (*cb)(struct bpf_prog *prog, void *data), @@ -2365,7 +2366,8 @@ static inline int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_ma static inline void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog) { } -static inline void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux) +static inline void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux, + const struct bpf_struct_ops *st_ops) { return NULL; } diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index d7c3030bc63b0..3b747eb52aa3a 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -844,6 +844,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, } if (prog->type != BPF_PROG_TYPE_STRUCT_OPS || + prog->aux->attach_btf != st_map->btf || prog->aux->attach_btf_id != st_ops_desc->type_id || prog->expected_attach_type != i) { bpf_prog_put(prog); @@ -1540,13 +1541,14 @@ void bpf_prog_disassoc_struct_ops(struct bpf_prog *prog) * Get a reference to the struct_ops struct (i.e., kdata) associated with a * program. Should only be called in BPF program context (e.g., in a kfunc). * - * If the returned pointer is not NULL, it must points to a valid struct_ops. + * If the returned pointer is not NULL, it must points to a valid @st_ops. * The struct_ops map is not guaranteed to be initialized nor attached. * Kernel struct_ops implementers are responsible for tracking and checking * the state of the struct_ops if the use case requires an initialized or * attached struct_ops. */ -void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux) +void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux, + const struct bpf_struct_ops *st_ops) { struct bpf_struct_ops_map *st_map; struct bpf_map *st_ops_assoc; @@ -1556,6 +1558,8 @@ void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux) return NULL; st_map = (struct bpf_struct_ops_map *)st_ops_assoc; + if (st_map->st_ops_desc->st_ops != st_ops) + return NULL; return &st_map->kvalue.data; } diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h index 2f35b98adb72b..974b4da74ab88 100644 --- a/kernel/sched/ext/internal.h +++ b/kernel/sched/ext/internal.h @@ -2520,6 +2520,8 @@ static inline bool scx_task_on_sched(struct scx_sched *sch, return rcu_access_pointer(p->scx.sched) == sch; } +static struct bpf_struct_ops bpf_sched_ext_ops, bpf_sched_ext_ops_cid; + /** * scx_prog_sched - Find scx_sched associated with a BPF prog * @aux: aux passed in from BPF to a kfunc @@ -2533,7 +2535,8 @@ static inline struct scx_sched *scx_prog_sched(const struct bpf_prog_aux *aux) struct sched_ext_ops *ops; struct scx_sched *sch, *root; - ops = bpf_prog_get_assoc_struct_ops(aux); + ops = bpf_prog_get_assoc_struct_ops(aux, &bpf_sched_ext_ops) ?: + bpf_prog_get_assoc_struct_ops(aux, &bpf_sched_ext_ops_cid); if (likely(ops)) { sch = rcu_dereference_all(ops->priv); if (sch && unlikely(READ_ONCE(sch->dead))) diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c index 9366a3c578f13..d507ba37731b8 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -2020,13 +2020,15 @@ int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) return ret; } +static struct bpf_struct_ops testmod_multi_st_ops; + /* Call test_1() of the associated struct_ops map */ int bpf_kfunc_multi_st_ops_test_1_assoc(struct st_ops_args *args, struct bpf_prog_aux *aux) { struct bpf_testmod_multi_st_ops *st_ops; int ret = -1; - st_ops = (struct bpf_testmod_multi_st_ops *)bpf_prog_get_assoc_struct_ops(aux); + st_ops = bpf_prog_get_assoc_struct_ops(aux, &testmod_multi_st_ops); if (st_ops) ret = st_ops->test_1(args); @@ -2104,7 +2106,7 @@ static struct bpf_testmod_multi_st_ops multi_st_ops_cfi_stubs = { .test_1 = bpf_testmod_multi_st_ops__test_1, }; -struct bpf_struct_ops testmod_multi_st_ops = { +static struct bpf_struct_ops testmod_multi_st_ops = { .verifier_ops = &bpf_testmod_verifier_ops, .init = multi_st_ops_init, .init_member = multi_st_ops_init_member, -- 2.53.0