Skip to contentSkip to conversation

bpf, sockmap: Fix self-redirect copied_seq double-counting

PATCH Applied Geliang Tang 5 participants 5 messages Sep 8 – Sep 14

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpf, sockmap: Fix self-redirect copied_seq double-counting

From: Geliang Tang <[email protected]>

When a BPF stream_verdict program redirects an skb back to the same socket (self-redirect with BPF_F_INGRESS), sk_psock_verdict_apply() calls tcp_eat_skb() which advances tcp_sk->copied_seq. However, the skb is then delivered to the socket's psock ingress queue and later read by tcp_bpf_recvmsg_parser(), which also advances copied_seq via the copied_from_self accounting path. This double-counting causes copied_seq to advance by 2x the actual data length, triggering:

  TCP recvmsg seq # bug 2: copied BF2E806, seq BF2E7FD, \
			   rcvnxt BF2E806, fl 0
  WARNING: net/ipv4/tcp.c:2745 at tcp_recvmsg_locked+0x72b/0x2640
  Call Trace:
   tcp_recvmsg+0x10a/0x500
   sock_recvmsg+0x168/0x1d0
   __sys_recvfrom+0x19a/0x2a0
   __x64_sys_recvfrom+0xe4/0x1f0
   do_syscall_64+0xf7/0x530
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

cleanup rbuf bug: copied BF2E806 seq BF2E806 rcvnxt BF2E806

  WARNING: net/ipv4/tcp.c:1609 at tcp_cleanup_rbuf+0xf2/0x1c0
  Call Trace:
   tcp_recvmsg_locked+0x8d1/0x2640
   tcp_recvmsg+0x10a/0x500
   sock_recvmsg+0x168/0x1d0
   __sys_recvfrom+0x19a/0x2a0
   __x64_sys_recvfrom+0xe4/0x1f0
   do_syscall_64+0xf7/0x530
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fix this by converting self-redirect verdict to __SK_PASS at the beginning of sk_psock_verdict_apply(). This bypasses the __SK_REDIRECT case entirely (which calls sk_psock_eat_skb), letting the __SK_PASS path queue the skb to the psock ingress queue. The data is then read via tcp_bpf_recvmsg_parser(), which advances copied_seq exactly once through copied_from_self. Cross-socket redirects continue through __SK_REDIRECT with sk_psock_eat_skb() unchanged.

Fixes e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq")

---
v3:
- add "bpf" prefix.

v2:
- fixup the verdict as Jakub and Jiayuan suggested.
- https://patchwork.kernel.org/project/netdevbpf/patch/1d2370f4c81f10834b8dd77524924575c629a464.1788591198.git.tanggeliang@kylinos.cn/

v1:
- https://patchwork.kernel.org/project/netdevbpf/patch/b840c35fdfdf36e9fddedfa645b12699bc51aa34.1787968065.git.tanggeliang@kylinos.cn/

---
 net/core/skmsg.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skmsg.c +4 -0
index 2521b643fa05..df385a5a961e 100644
@@ -1000,6 +1000,10 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
 	int err = 0;
 	u32 len, off;
 
+	if (verdict == __SK_REDIRECT && skb_bpf_ingress(skb) &&
+	    skb_bpf_redirect_fetch(skb) == psock->sk)
+		verdict = __SK_PASS;
+
 	switch (verdict) {
 	case __SK_PASS:
 		err = -EIO;
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Inconsistent TCP sequence accounting (copied_seq) for BPF stream_verdict self-redirects depending…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Inconsistent TCP sequence accounting (copied_seq) for BPF stream_verdict self-redirects depending on ingress queue state. --

commit 1c84437e57670b82a168813bb40bf5ab64fc2722
Author: Geliang Tang <[email protected]>

bpf, sockmap: Fix self-redirect copied_seq double-counting

This commit attempts to fix a copied_seq double-counting issue by converting a self-redirect verdict to __SK_PASS in sk_psock_verdict_apply(). This bypasses the __SK_REDIRECT case entirely and lets the __SK_PASS path queue the skb to the psock ingress queue.

net/core/skmsg.c static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, · line 1006

Quoted 12 lines
net/core/skmsg.c +4 -0
index 2521b643fa05d..df385a5a961e8 100644
@@ -1000,6 +1000,10 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
 	int err = 0;
 	u32 len, off;
 
+	if (verdict == __SK_REDIRECT && skb_bpf_ingress(skb) &&
+	    skb_bpf_redirect_fetch(skb) == psock->sk)
+		verdict = __SK_PASS;
+

[Severity: High]
Does converting this to __SK_PASS introduce inconsistent TCP sequence accounting (copied_seq) depending on the ingress queue state?

When the queue is empty, sk_psock_verdict_apply() calls sk_psock_skb_ingress_self() directly:

net/core/skmsg.c:sk_psock_verdict_apply() {
    ...
    if (skb_queue_empty(&psock->ingress_skb)) {
        ...
        err = sk_psock_skb_ingress_self(psock, skb, off, len, false);
    ...
}

This successfully creates a message with msg->sk = sk, which allows tcp_bpf_recvmsg_parser() to correctly advance copied_seq.

However, if the ingress queue is not empty, the skb is queued for the backlog worker. When backlog processing later reaches sk_psock_skb_ingress():

net/core/skmsg.c:sk_psock_skb_ingress() {
    ...
    if (unlikely(skb->sk == sk))
        return sk_psock_skb_ingress_self(psock, skb, off, len, true);
    msg = sk_psock_create_ingress_msg(sk, skb);
    ...
}

Since sk_psock_strp_read() sets skb->sk = NULL before verdict application, this check fails. The resulting msg has a NULL sk, bypassing copied_seq advancement entirely.

Could this failure to advance copied_seq lead to TCP window exhaustion and cause the connection to stall (a Denial of Service for the socket), essentially replacing the 2x over-counting bug with a 0x under-counting bug?

Quoted 3 lines
net/core/skmsg.c +0 -0
 	switch (verdict) {
 	case __SK_PASS:
 		err = -EIO;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/1a8e797a1b26e2f695aaac22ac644c2862f63466.1788858299.git.tanggeliang@kylinos.cn?part=1
Reviewed-by on v3 1/1
#2 lore raw
In reply to Geliang Tang
Quoted 42 lines · Geliang Tang

From: Geliang Tang <[email protected]>

When a BPF stream_verdict program redirects an skb back to the same socket (self-redirect with BPF_F_INGRESS), sk_psock_verdict_apply() calls tcp_eat_skb() which advances tcp_sk->copied_seq. However, the skb is then delivered to the socket's psock ingress queue and later read by tcp_bpf_recvmsg_parser(), which also advances copied_seq via the copied_from_self accounting path. This double-counting causes copied_seq to advance by 2x the actual data length, triggering:

  TCP recvmsg seq # bug 2: copied BF2E806, seq BF2E7FD, \
                           rcvnxt BF2E806, fl 0
  WARNING: net/ipv4/tcp.c:2745 at tcp_recvmsg_locked+0x72b/0x2640
  Call Trace:
   tcp_recvmsg+0x10a/0x500
   sock_recvmsg+0x168/0x1d0
   __sys_recvfrom+0x19a/0x2a0
   __x64_sys_recvfrom+0xe4/0x1f0
   do_syscall_64+0xf7/0x530
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

cleanup rbuf bug: copied BF2E806 seq BF2E806 rcvnxt BF2E806

  WARNING: net/ipv4/tcp.c:1609 at tcp_cleanup_rbuf+0xf2/0x1c0
  Call Trace:
   tcp_recvmsg_locked+0x8d1/0x2640
   tcp_recvmsg+0x10a/0x500
   sock_recvmsg+0x168/0x1d0
   __sys_recvfrom+0x19a/0x2a0
   __x64_sys_recvfrom+0xe4/0x1f0
   do_syscall_64+0xf7/0x530
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fix this by converting self-redirect verdict to __SK_PASS at the beginning of sk_psock_verdict_apply(). This bypasses the __SK_REDIRECT case entirely (which calls sk_psock_eat_skb), letting the __SK_PASS path queue the skb to the psock ingress queue. The data is then read via tcp_bpf_recvmsg_parser(), which advances copied_seq exactly once through copied_from_self. Cross-socket redirects continue through __SK_REDIRECT with sk_psock_eat_skb() unchanged.

Quoted 4 lines
Fixes e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq")

Bot is wrong, in the sync path the whole ingress_bytes adjustment is so that a later recvmsg will advance copied_seq by the amount of bytes we have in psock after reading it. In the async path we just account for the data immediately
in copied_seq and do not try to delay the accounting.

Quoted 33 lines

---
v3:
- add "bpf" prefix.

v2:
- fixup the verdict as Jakub and Jiayuan suggested.
- https://patchwork.kernel.org/project/netdevbpf/patch/1d2370f4c81f10834b8dd77524924575c629a464.1788591198.git.tanggeliang@kylinos.cn/

v1:
- https://patchwork.kernel.org/project/netdevbpf/patch/b840c35fdfdf36e9fddedfa645b12699bc51aa34.1787968065.git.tanggeliang@kylinos.cn/

---
 net/core/skmsg.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skmsg.c +4 -1
index 2521b643fa05..df385a5a961e 100644
@@ -1000,6 +1000,10 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
        int err = 0;
        u32 len, off;
 
+       if (verdict == __SK_REDIRECT && skb_bpf_ingress(skb) &&
+           skb_bpf_redirect_fetch(skb) == psock->sk)
+               verdict = __SK_PASS;
+
        switch (verdict) {
        case __SK_PASS:
                err = -EIO;
--

2.53.0

maintainer Reviewed-by on v3 1/1
#3 lore raw
In reply to Geliang Tang
Quoted 41 lines · Geliang Tang

From: Geliang Tang <[email protected]>

When a BPF stream_verdict program redirects an skb back to the same socket (self-redirect with BPF_F_INGRESS), sk_psock_verdict_apply() calls tcp_eat_skb() which advances tcp_sk->copied_seq. However, the skb is then delivered to the socket's psock ingress queue and later read by tcp_bpf_recvmsg_parser(), which also advances copied_seq via the copied_from_self accounting path. This double-counting causes copied_seq to advance by 2x the actual data length, triggering:

   TCP recvmsg seq # bug 2: copied BF2E806, seq BF2E7FD, \
			   rcvnxt BF2E806, fl 0
   WARNING: net/ipv4/tcp.c:2745 at tcp_recvmsg_locked+0x72b/0x2640
   Call Trace:
    tcp_recvmsg+0x10a/0x500
    sock_recvmsg+0x168/0x1d0
    __sys_recvfrom+0x19a/0x2a0
    __x64_sys_recvfrom+0xe4/0x1f0
    do_syscall_64+0xf7/0x530
    entry_SYSCALL_64_after_hwframe+0x77/0x7f

   cleanup rbuf bug: copied BF2E806 seq BF2E806 rcvnxt BF2E806
   WARNING: net/ipv4/tcp.c:1609 at tcp_cleanup_rbuf+0xf2/0x1c0
   Call Trace:
    tcp_recvmsg_locked+0x8d1/0x2640
    tcp_recvmsg+0x10a/0x500
    sock_recvmsg+0x168/0x1d0
    __sys_recvfrom+0x19a/0x2a0
    __x64_sys_recvfrom+0xe4/0x1f0
    do_syscall_64+0xf7/0x530
    entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fix this by converting self-redirect verdict to __SK_PASS at the beginning of sk_psock_verdict_apply(). This bypasses the __SK_REDIRECT case entirely (which calls sk_psock_eat_skb), letting the __SK_PASS path queue the skb to the psock ingress queue. The data is then read via tcp_bpf_recvmsg_parser(), which advances copied_seq exactly once through copied_from_self. Cross-socket redirects continue through __SK_REDIRECT with sk_psock_eat_skb() unchanged.

Quoted 4 lines
Fixes e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq")

All AI reviews from BPF-CI and Sashiko go beyond the original meaning of this patch.

Quoted 29 lines

---
v3:
- add "bpf" prefix.

v2:
- fixup the verdict as Jakub and Jiayuan suggested.
- https://patchwork.kernel.org/project/netdevbpf/patch/1d2370f4c81f10834b8dd77524924575c629a464.1788591198.git.tanggeliang@kylinos.cn/

v1:
- https://patchwork.kernel.org/project/netdevbpf/patch/b840c35fdfdf36e9fddedfa645b12699bc51aa34.1787968065.git.tanggeliang@kylinos.cn/

---
  net/core/skmsg.c | 4 ++++
  1 file changed, 4 insertions(+)
net/core/skmsg.c +4 -0
index 2521b643fa05..df385a5a961e 100644
@@ -1000,6 +1000,10 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
  	int err = 0;
  	u32 len, off;
 
+	if (verdict == __SK_REDIRECT && skb_bpf_ingress(skb) &&
+	    skb_bpf_redirect_fetch(skb) == psock->sk)
+		verdict = __SK_PASS;
+
  	switch (verdict) {
  	case __SK_PASS:
  		err = -EIO;
#4 lore raw
In reply to Geliang Tang
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Tue, 8 Sep 2026 17:08:32 +0800 you wrote:

From: Geliang Tang <[email protected]>

When a BPF stream_verdict program redirects an skb back to the same socket (self-redirect with BPF_F_INGRESS), sk_psock_verdict_apply() calls tcp_eat_skb() which advances tcp_sk->copied_seq. However, the skb is then delivered to the socket's psock ingress queue and later read by tcp_bpf_recvmsg_parser(), which also advances copied_seq via the copied_from_self accounting path. This double-counting causes copied_seq to advance by 2x the actual data length, triggering:

[...]

Here is the summary with links:
- [bpf,v3] bpf, sockmap: Fix self-redirect copied_seq double-counting

    https://git.kernel.org/bpf/bpf/c/490a83d6386e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html