BPF iterator programs that can update sockmaps will be limited to iter/sockmap. Remove the update from the unix selftest. This reverts commit 997b8483d44c ("selftests/bpf: Extend bpf_iter_unix to attempt deadlocking"). Signed-off-by: Michal Luczaj --- tools/testing/selftests/bpf/progs/bpf_iter_unix.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_unix.c b/tools/testing/selftests/bpf/progs/bpf_iter_unix.c index 3061b148f338..90beada0386d 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter_unix.c +++ b/tools/testing/selftests/bpf/progs/bpf_iter_unix.c @@ -7,13 +7,6 @@ char _license[] SEC("license") = "GPL"; -SEC(".maps") struct { - __uint(type, BPF_MAP_TYPE_SOCKMAP); - __uint(max_entries, 1); - __type(key, __u32); - __type(value, __u64); -} sockmap; - static long sock_i_ino(const struct sock *sk) { const struct socket *sk_socket = sk->sk_socket; @@ -82,8 +75,5 @@ int dump_unix(struct bpf_iter__unix *ctx) BPF_SEQ_PRINTF(seq, "\n"); - /* Test for deadlock. */ - bpf_map_update_elem(&sockmap, &(int){0}, sk, 0); - return 0; } -- 2.56.0 Among tracing progs, allow only iter/sockmap to update sockmaps and sockhashes. Previously, any BPF_TRACE_ITER program could perform these updates, which caused locking issues in specific scenarios [1][2][3]. This tightening is part of the broader effort to lock down the BPF API [4]. [1]: commit 4d328dd69538 ("bpf, sockmap: Fix af_unix iter deadlock") [2]: commit 64c2f93fc325 ("bpf, sockmap: Take state lock for af_unix iter") [3]: https://msgid.link/87echn2fhd.fsf@cloudflare.com [4]: https://msgid.link/akRZ_e7DJh2aP-2i@john-p8 Reported-by: Sashiko Closes: https://msgid.link/20260626205814.BAC3C1F000E9@smtp.kernel.org Suggested-by: Jakub Sitnicki Signed-off-by: Michal Luczaj --- kernel/bpf/verifier.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1b6c0674ff6a..4dadfe9505a2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10487,6 +10487,7 @@ static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) { enum bpf_attach_type eatype = env->prog->expected_attach_type; enum bpf_prog_type type = resolve_prog_type(env->prog); + const char *afname = env->prog->aux->attach_func_name; if (func_id != BPF_FUNC_map_update_elem && func_id != BPF_FUNC_map_delete_elem) @@ -10497,7 +10498,8 @@ static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) */ switch (type) { case BPF_PROG_TYPE_TRACING: - if (eatype == BPF_TRACE_ITER) + if (eatype == BPF_TRACE_ITER && + !strcmp(afname + strlen(BPF_ITER_FUNC_PREFIX), "sockmap")) return true; break; case BPF_PROG_TYPE_SOCK_OPS: -- 2.56.0 Iterator-context sockmap updates are now restricted to iter/sockmap. Add a test that attempts an update from iter/unix and expects it to fail. Signed-off-by: Michal Luczaj --- .../testing/selftests/bpf/progs/verifier_sockmap_mutate.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c b/tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c index 20332a731d4e..1516fe1524bb 100644 --- a/tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c +++ b/tools/testing/selftests/bpf/progs/verifier_sockmap_mutate.c @@ -19,6 +19,12 @@ struct bpf_iter__sockmap { }; } __attribute__((preserve_access_index)); +struct bpf_iter__unix { + union { + struct sock *unix_sk; + }; +} __attribute__((preserve_access_index)); + struct { __uint(type, BPF_MAP_TYPE_SOCKHASH); __uint(max_entries, 1); @@ -113,6 +119,14 @@ int test_trace_iter(struct bpf_iter__sockmap *ctx) return 0; } +SEC("iter/unix") +__failure __msg("cannot update sockmap in this context") +int test_trace_iter_unix(struct bpf_iter__unix *ctx) +{ + test_sockmap_mutate(ctx->unix_sk); + return 0; +} + SEC("raw_tp/kfree") __failure __msg("cannot update sockmap in this context") int test_raw_tp_delete(const void *ctx __always_unused) -- 2.56.0 In preparation for plain-PTR_TO_BTF_ID removal: allow calling bpf helpers with sk provided by iter/sockmap. Suggested-by: Alexei Starovoitov Link: https://msgid.link/DM92JCFA4X4U.30MTDH0KCPTJ@gmail.com Signed-off-by: Michal Luczaj --- net/core/sock_map.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/core/sock_map.c b/net/core/sock_map.c index 057e2f3c0265..2a2a07372818 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -1989,7 +1989,7 @@ static struct bpf_iter_reg sock_map_iter_reg = { { offsetof(struct bpf_iter__sockmap, key), PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY }, { offsetof(struct bpf_iter__sockmap, sk), - PTR_TO_BTF_ID_OR_NULL }, + PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED }, }, }; -- 2.56.0