[PATCH bpf 03/11] bpf, sockmap: Fix strparser reinit after the parser link is removed
From: Jiayuan Chen
Date: Sat Nov 21 2026 - 05:34:07 EST
A socket can sit in several sockmaps at once, so a psock can outlive
the link that brought the stream parser. When that link goes away,
sock_map_del_link() can only call sk_psock_stop_strp(), which marks the
strparser stopped. strp_done(), which cancels the message timer and the
work and frees the partial skb, needs lock_sock() and sync cancels and
is only run from sk_psock_destroy(). With another link holding the
psock, destroy never comes and the strparser stays in that half state.
Adding the socket to another map with a parser then goes through
sk_psock_init_strp() again, because progs.stream_parser and
saved_data_ready were both cleared. strp_init() memsets the strparser
and reinits its work and timer while the old timer may still be on the
timer wheel and the old work may still be queued. The partial skb is
leaked. Once the timer fires, __run_timers() unlinks a timer_list that
no longer matches the wheel and delayed_work_timer_fn() dereferences
the zeroed wq:
BUG: KASAN: slab-use-after-free in __run_timers+0xa7d/0xab0
Write of size 8 at addr ffff888001ed40e0 by task systemd-journal/148
Call Trace:
<IRQ>
__run_timers+0xa7d/0xab0
run_timer_base+0x107/0x190
run_timer_softirq+0x1a/0x40
handle_softirqs+0x251/0xa70
__irq_exit_rcu+0x1a8/0x260
irq_exit_rcu+0xe/0x30
sysvec_apic_timer_interrupt+0xc2/0x110
</IRQ>
Allocated by task 756:
sk_psock_init+0x187/0x850
sock_map_link+0x7d8/0xcf0
sock_map_update_common+0xbd/0x6c0
sock_map_update_elem_sys+0x324/0x460
Freed by task 684:
sk_psock_destroy+0x847/0xb30
process_one_work+0x93a/0x1b60
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000038
KASAN: null-ptr-deref in range [0x00000000000001c0-0x00000000000001c7]
RIP: 0010:__queue_work+0x94/0x1850
Call Trace:
<IRQ>
delayed_work_timer_fn+0x5d/0xa0
call_timer_fn+0x17d/0x510
__run_timers+0x4e5/0xab0
run_timer_base+0x107/0x190
run_timer_softirq+0x1a/0x40
</IRQ>
Finish the strparser when the parser link is removed instead of waiting
for the psock to die. sock_map_del_link() cannot sleep, so take a psock
reference and queue sk_psock_destroy() on psock->rwork with
SK_PSOCK_STRP_DONE_PENDING set. The work then only runs strp_done(),
clears SK_PSOCK_RX_STRP_ENABLED and drops the reference. The work holds
the psock, so the normal destroy cannot be queued before it finishes,
and the final put may requeue rwork for the real destroy from inside
the handler.
sock_map_link() refuses to attach a parser while
SK_PSOCK_RX_STRP_ENABLED is still set, under sk_callback_lock together
with the bit updates. sock_map_update_elem_sys() flushes the pending
work before taking the socket lock, so userspace can remove a parser and
attach a new one back to back without seeing -EBUSY.
BPF programs updating a sockmap (sockops helpers, iterator, sk_lookup
and sk_reuseport programs) cannot wait and may get a transient -EBUSY
if they add a socket to a parser map right after its parser link was
removed. Before this change the same sequence reinitialized a live
strparser, so this is the better outcome.
The psock now also lives a little longer after the last link to a
parser map is removed, until the work has run. A TLS upgrade attempted
in that window is rejected like for any socket with a psock.
Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")
Signed-off-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
---
include/linux/skmsg.h | 6 ++++++
net/core/skmsg.c | 24 ++++++++++++++++++++++++
net/core/sock_map.c | 39 ++++++++++++++++++++++++++++++++++++++-
3 files changed, 68 insertions(+), 1 deletion(-)
diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index f0e27b4b929f1..276692e060d53 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -68,6 +68,7 @@ struct sk_psock_progs {
enum sk_psock_state_bits {
SK_PSOCK_TX_ENABLED,
SK_PSOCK_RX_STRP_ENABLED,
+ SK_PSOCK_STRP_DONE_PENDING,
};
struct sk_psock_link {
@@ -459,6 +460,7 @@ void sk_psock_stop(struct sk_psock *psock);
int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
+void sk_psock_queue_strp_done(struct sk_psock *psock);
#else
static inline int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
{
@@ -472,6 +474,10 @@ static inline void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
static inline void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
{
}
+
+static inline void sk_psock_queue_strp_done(struct sk_psock *psock)
+{
+}
#endif
void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock);
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index 16f84dcb5acd9..43dc128841e98 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -879,6 +879,18 @@ static void sk_psock_destroy(struct work_struct *work)
{
struct sk_psock *psock = container_of(to_rcu_work(work),
struct sk_psock, rwork);
+
+ if (sk_psock_test_state(psock, SK_PSOCK_STRP_DONE_PENDING)) {
+ /* Parser link gone, other links keep the psock alive */
+ sk_psock_done_strp(psock);
+ write_lock_bh(&psock->sk->sk_callback_lock);
+ sk_psock_clear_state(psock, SK_PSOCK_RX_STRP_ENABLED);
+ sk_psock_clear_state(psock, SK_PSOCK_STRP_DONE_PENDING);
+ write_unlock_bh(&psock->sk->sk_callback_lock);
+ sk_psock_put(psock->sk, psock);
+ return;
+ }
+
/* No sk_callback_lock since already detached. */
sk_psock_done_strp(psock);
@@ -1208,6 +1220,18 @@ void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
strp_stop(&psock->strp);
}
+/* Called with sk_callback_lock held while a link still holds the psock */
+void sk_psock_queue_strp_done(struct sk_psock *psock)
+{
+ if (!sk_psock_test_state(psock, SK_PSOCK_RX_STRP_ENABLED) ||
+ test_and_set_bit(SK_PSOCK_STRP_DONE_PENDING, &psock->state))
+ return;
+
+ refcount_inc(&psock->refcnt);
+ INIT_WORK(&psock->rwork.work, sk_psock_destroy);
+ schedule_work(&psock->rwork.work);
+}
+
static void sk_psock_done_strp(struct sk_psock *psock)
{
/* Parser has been stopped */
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 282c6a4b2b05f..6747b3cc1cac1 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -115,6 +115,33 @@ int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
return ret;
}
+/*
+ * Wait for the strparser of a removed parser link to be torn down, so that
+ * a new parser can be attached right after the old one was removed.
+ */
+static void sock_map_strp_done_wait(struct bpf_map *map, struct sock *sk)
+{
+ struct sk_psock *psock;
+
+ if (!READ_ONCE(sock_map_progs(map)->stream_parser))
+ return;
+
+ rcu_read_lock();
+ read_lock_bh(&sk->sk_callback_lock);
+ psock = sk_psock(sk);
+ if (!psock ||
+ !sk_psock_test_state(psock, SK_PSOCK_STRP_DONE_PENDING) ||
+ !refcount_inc_not_zero(&psock->refcnt))
+ psock = NULL;
+ read_unlock_bh(&sk->sk_callback_lock);
+ rcu_read_unlock();
+ if (!psock)
+ return;
+
+ flush_work(&psock->rwork.work);
+ sk_psock_put(sk, psock);
+}
+
static void sock_map_sk_acquire(struct sock *sk)
__acquires(&sk->sk_lock.slock)
{
@@ -169,8 +196,10 @@ static void sock_map_del_link(struct sock *sk,
write_lock_bh(&sk->sk_callback_lock);
if (psock->saved_data_ready) {
- if (strp_stop)
+ if (strp_stop) {
sk_psock_stop_strp(sk, psock);
+ sk_psock_queue_strp_done(psock);
+ }
if (verdict_stop)
sk_psock_stop_verdict(sk, psock);
@@ -297,6 +326,13 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
}
write_lock_bh(&sk->sk_callback_lock);
+ if (stream_parser &&
+ sk_psock_test_state(psock, SK_PSOCK_RX_STRP_ENABLED)) {
+ write_unlock_bh(&sk->sk_callback_lock);
+ sk_psock_put(sk, psock);
+ ret = -EBUSY;
+ goto out_progs;
+ }
if (msg_parser)
psock_set_prog(&psock->progs.msg_parser, msg_parser);
if (stream_parser)
@@ -597,6 +633,7 @@ int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
goto out;
}
+ sock_map_strp_done_wait(map, sk);
sock_map_sk_acquire(sk);
if (!sock_map_sk_state_allowed(sk))
ret = -EOPNOTSUPP;
--
2.43.0