From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
Jakub Sitnicki <jakub@cloudflare.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Willem de Bruijn <willemb@google.com>,
Neal Cardwell <ncardwell@google.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Shuah Khan <shuah@kernel.org>, Sechang Lim <rhkrqnwk98@gmail.com>,
Michal Luczaj <mhal@rbox.co>,
Mattia Meleleo <mattia.meleleo@coralogix.com>,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH bpf 08/11] bpf, sockmap: Fix socket lock taken on a child before accept()
Date: Sat, 21 Nov 2026 18:30:09 +0800 [thread overview]
Message-ID: <20261121103222.88209-9-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20261121103222.88209-1-jiayuan.chen@linux.dev>
A sockops prog can add a child that is not accepted yet to a sockmap at
BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB. An sk_msg redirect to this child,
ingress or egress, takes lock_sock() on it. If the listener is closed at
that time, inet_csk_listen_stop() hits:
WARN_ON(sock_owned_by_user(child));
WARNING: net/ipv4/inet_connection_sock.c:1492 at inet_csk_listen_stop+0x7ba/0xc40
Call Trace:
<TASK>
__tcp_close+0xb64/0xee0
tcp_close+0x24/0xf0
inet_release+0x10a/0x240
__sock_release+0xaf/0x270
sock_close+0x18/0x30
__fput+0x360/0xa90
fput_close_sync+0xe6/0x1d0
__x64_sys_close+0x80/0xe0
x64_sys_call+0x2680/0x27e0
do_syscall_64+0xe5/0x610
entry_SYSCALL_64_after_hwframe+0x76/0x7e
</TASK>
Commit 4aeaad1c536e ("bpf, sockmap: Check the target of an egress
redirect before each send") already refuses an egress redirect to such a
child, but only after lock_sock(). Refuse the ingress redirect too, and
do it before taking the lock: a child has no sk_socket until accept()
has taken it off the queue.
sock_map_free() and sock_hash_free() also lock the sockets in the map.
They can't refuse, so unlink such a child without the lock, as a delete
from the map already does.
Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")
Fixes: 7e81a3530206 ("bpf: Sockmap, ensure sock lock held during tear down")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/core/sock_map.c | 27 +++++++++++++++++++++++----
net/ipv4/tcp_bpf.c | 10 +++++++++-
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 6fc1d29ac6ed5..4a053846bd60e 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -386,6 +386,19 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
return ret;
}
+/*
+ * The listener frees a child that nobody has accepted yet without waiting
+ * for the owner of its socket lock. Unlink such a child without the lock,
+ * as a delete does.
+ */
+static bool sock_map_free_lock_sk(struct sock *sk)
+{
+ if (!READ_ONCE(sk->sk_socket))
+ return false;
+ lock_sock(sk);
+ return true;
+}
+
static void sock_map_free(struct bpf_map *map)
{
struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
@@ -402,12 +415,15 @@ static void sock_map_free(struct bpf_map *map)
sk = xchg(psk, NULL);
if (sk) {
+ bool locked;
+
sock_hold(sk);
- lock_sock(sk);
+ locked = sock_map_free_lock_sk(sk);
rcu_read_lock();
sock_map_unref(sk, psk);
rcu_read_unlock();
- release_sock(sk);
+ if (locked)
+ release_sock(sk);
sock_put(sk);
}
}
@@ -1228,12 +1244,15 @@ static void sock_hash_free(struct bpf_map *map)
* link to sockhash.
*/
hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
+ bool locked;
+
hlist_del(&elem->node);
- lock_sock(elem->sk);
+ locked = sock_map_free_lock_sk(elem->sk);
rcu_read_lock();
sock_map_unref(elem->sk, elem);
rcu_read_unlock();
- release_sock(elem->sk);
+ if (locked)
+ release_sock(elem->sk);
sock_put(elem->sk);
sock_hash_free_elem(htab, elem);
}
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 9715716518c49..da9caccfede79 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -188,9 +188,17 @@ static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
struct sk_msg *msg, u32 bytes, int flags)
{
- struct sk_psock *psock = sk_psock_get(sk);
+ struct sk_psock *psock;
int ret;
+ /*
+ * The listener frees a child that nobody has accepted yet without
+ * waiting for the owner of its socket lock.
+ */
+ if (!READ_ONCE(sk->sk_socket))
+ return sock_flag(sk, SOCK_DEAD) ? -EPIPE : -EINVAL;
+
+ psock = sk_psock_get(sk);
if (unlikely(!psock))
return -EPIPE;
--
2.43.0
next prev parent reply other threads:[~2026-11-21 10:36 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 10:30 [PATCH bpf 00/11] fix misc sockmap issues Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 01/11] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 02/11] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 03/11] bpf, sockmap: Fix strparser reinit after the parser link is removed Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 04/11] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 05/11] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
2026-11-21 10:53 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 06/11] bpf, sockmap: Fix reconnect with a psock by rejecting disconnect Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 07/11] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-11-21 10:30 ` Jiayuan Chen [this message]
2026-11-21 10:56 ` [PATCH bpf 08/11] bpf, sockmap: Fix socket lock taken on a child before accept() sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 09/11] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 10/11] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 11/11] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261121103222.88209-9-jiayuan.chen@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=jakub@cloudflare.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mattia.meleleo@coralogix.com \
--cc=memxor@gmail.com \
--cc=mhal@rbox.co \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rhkrqnwk98@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.