Skip to contentSkip to conversation

bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch()

PATCH Superseded Jose Fernandez (Anthropic) 4 participants 6 messages Jul 17 – Jul 30

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch()

reqsk_queue_hash_req() publishes a TCP_NEW_SYN_RECV request_sock onto the ehash chain, drops the bucket lock, and only afterwards sets rsk_refcnt to 3.

Lockless readers such as __inet_lookup_established() handle this with refcount_inc_not_zero(), but bpf_iter_tcp_established_batch() uses plain sock_hold() while holding the bucket lock, on the assumption that the lock guarantees sk_refcnt > 0. That assumption does not hold for request_sock:

CPU 0 CPU 1
----- -----
tcp_conn_request()
reqsk_queue_hash_req()

    inet_ehash_insert(req)
     spin_lock(bucket)
     __sk_nulls_add_node_rcu(req)      // rsk_refcnt == 0
     spin_unlock(bucket)
                                       bpf_iter_tcp_established_batch()
                                        spin_lock(bucket)
                                        sock_hold(req)   <-- addition on 0
                                        spin_unlock(bucket)
    refcount_set(&req->rsk_refcnt, 3)  // clobbers saturated value

which surfaces as:

refcount_t: addition on 0; use-after-free.

  WARNING: lib/refcount.c:25 at refcount_warn_saturate+0x48/0x90, CPU#1
  Call Trace:
   bpf_iter_tcp_established_batch+0x14e/0x170
   bpf_iter_tcp_batch+0x53/0x200
   bpf_iter_tcp_seq_next+0x27/0x70
   bpf_seq_read+0x107/0x410
   vfs_read+0xb9/0x380

The iterator's stolen reference is lost when the publishing CPU's refcount_set() overwrites the count, leaving the socket one reference short. When the last legitimate owner drops its reference the reqsk is freed while still reachable, leading to use-after-free.

This reproduces in seconds with tcp_syncookies=0, a handful of threads doing connect()/close() to a local listener while others read an iter/tcp link in a tight loop.

Use refcount_inc_not_zero() and skip the socket on failure. A skipped socket is still part of the bucket, so keep counting it in expected. The reallocations are sized from expected, and a request sock whose refcount gets published while the lock is held across the last realloc must already have room.

A skipped socket is counted in expected but never batched, so end_sk can be short of expected on a batch that is actually complete. Decide completeness by whether the walk left any socket behind instead. The WARN after the locked realloc checks the same, replacing an end_sk == expected check that could not hold on that path since cdec67a489d4.

If every matching socket in a bucket is mid-init (refcount 0), end_sk stays 0. Advance to the next bucket rather than returning a batch entry that was never filled this round.

Fixes 04c7820b776f ("bpf: tcp: Bpf iter batching and lock_sock")
Assisted-by Claude:unspecified

---
Changes in v2:
- Count expected right after seq_sk_match() so the batch reallocations
are sized for the whole bucket, including request socks whose
refcount is not yet published (Kuniyuki)
- Signal batch completeness by the walk leaving no leftover socket
instead of end_sk == expected, and check the same condition in the
WARN after the locked reallocation
- Drop the Reviewed-by tags given the code changes
- Rebase onto bpf/master
- Link to v1: https://lore.kernel.org/bpf/[email protected]

The pre-existing double-put on the realloc failure path (raised in the v1 thread) will be addressed in a separate follow-up patch.

---
 net/ipv4/tcp_ipv4.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)
net/ipv4/tcp_ipv4.c +25 -19
index 209ef7522508..d8640d114c0d 100644
@@ -3073,24 +3073,24 @@ static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq,
 {
 	struct bpf_tcp_iter_state *iter = seq->private;
 	struct hlist_nulls_node *node;
-	unsigned int expected = 1;
-	struct sock *sk;
+	unsigned int expected = 0;
+	struct sock *sk = *start_sk;
 
-	sock_hold(*start_sk);
-	iter->batch[iter->end_sk++].sk = *start_sk;
-
-	sk = sk_nulls_next(*start_sk);
 	*start_sk = NULL;
 	sk_nulls_for_each_from(sk, node) {
-		if (seq_sk_match(seq, sk)) {
-			if (iter->end_sk < iter->max_sk) {
-				sock_hold(sk);
-				iter->batch[iter->end_sk++].sk = sk;
-			} else if (!*start_sk) {
-				/* Remember where we left off. */
-				*start_sk = sk;
-			}
-			expected++;
+		if (!seq_sk_match(seq, sk))
+			continue;
+		expected++;
+		if (iter->end_sk < iter->max_sk) {
+			/* reqsk_queue_hash_req() inserts with sk_refcnt == 0
+			 * and refcount_set()s it after the bucket lock drops.
+			 */
+			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+				continue;
+			iter->batch[iter->end_sk++].sk = sk;
+		} else if (!*start_sk) {
+			/* Remember where we left off. */
+			*start_sk = sk;
 		}
 	}
 
@@ -3128,12 +3128,14 @@ static struct sock *bpf_iter_tcp_batch(struct seq_file *seq)
 	struct sock *sk;
 	int err;
 
+again:
 	sk = bpf_iter_tcp_resume(seq);
 	if (!sk)
 		return NULL; /* Done */
 
 	expected = bpf_iter_fill_batch(seq, &sk);
-	if (likely(iter->end_sk == expected))
+	/* sk carries the first socket that did not fit in the batch. */
+	if (likely(!sk))
 		goto done;
 
 	/* Batch size was too small. */
@@ -3149,7 +3151,7 @@ static struct sock *bpf_iter_tcp_batch(struct seq_file *seq)
 		return NULL; /* Done */
 
 	expected = bpf_iter_fill_batch(seq, &sk);
-	if (likely(iter->end_sk == expected))
+	if (likely(!sk))
 		goto done;
 
 	/* Batch size was still too small. Hold onto the lock while we try
@@ -3162,10 +3164,14 @@ static struct sock *bpf_iter_tcp_batch(struct seq_file *seq)
 		return ERR_PTR(err);
 	}
 
-	expected = bpf_iter_fill_batch(seq, &sk);
-	WARN_ON_ONCE(iter->end_sk != expected);
+	bpf_iter_fill_batch(seq, &sk);
+	WARN_ON_ONCE(sk);
 done:
 	bpf_iter_tcp_unlock_bucket(seq);
+	if (unlikely(!iter->end_sk)) {
+		++iter->state.bucket;
+		goto again;
+	}
 	return iter->batch[0].sk;
 }
 

---
base-commit: 7cbd0c4cebe4c9f678d15e6b9ba975e1155a107f
change-id: 20260619-bpf-iter-tcp-refcnt-107d52b238da

Best regards,

Signature · 1 line
Jose Fernandez (Anthropic) <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Low] The patch introduces a multi-line comment that violates the BPF subsystem comment style guidelines.

Pre-existing issues:
- [High] Double-put and wild pointer dereference of batch sockets on the first bpf_iter_tcp_realloc_batch failure path. --

--- Patch [1]: [PATCH bpf v2] bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch() --- commit c52d637a13758169d9dce0d7c05be97965d399d3
Author: Jose Fernandez (Anthropic) <[email protected]>

bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch()

This commit fixes a use-after-free issue in the TCP iterator caused by request sockets being published to the ehash chain with a refcount of 0 before being updated. The fix uses refcount_inc_not_zero() to safely handle these sockets during batch iteration. It also refactors the batch completion logic to rely on whether any sockets were left behind during the walk.

Quoted 3 lines
net/ipv4/tcp_ipv4.c +0 -0
@@ -3073,24 +3073,24 @@ static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq,

[ ... ]

Quoted 7 lines
net/ipv4/tcp_ipv4.c +0 -6
 	sk_nulls_for_each_from(sk, node) {
-		if (seq_sk_match(seq, sk)) {
-			if (iter->end_sk < iter->max_sk) {
-				sock_hold(sk);
-				iter->batch[iter->end_sk++].sk = sk;
-			} else if (!*start_sk) {
-				/* Remember where we left off. */

net/ipv4/tcp_ipv4.c static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq, · line 3087

Quoted 10 lines
net/ipv4/tcp_ipv4.c +7 -3
-				*start_sk = sk;
-			}
-			expected++;
+		if (!seq_sk_match(seq, sk))
+			continue;
+		expected++;
+		if (iter->end_sk < iter->max_sk) {
+			/* reqsk_queue_hash_req() inserts with sk_refcnt == 0
+			 * and refcount_set()s it after the bucket lock drops.
+			 */

[Severity: Low]
Does this multi-line comment follow the BPF subsystem comment style? The BPF subsystem guide expects the opening block to be on its own line:

/*
* reqsk_queue_hash_req() inserts with sk_refcnt == 0
...

Quoted 2 lines
net/ipv4/tcp_ipv4.c +2 -0
+			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+				continue;

[ ... ]

Quoted 4 lines
net/ipv4/tcp_ipv4.c +0 -0
@@ -3128,12 +3128,14 @@ static struct sock *bpf_iter_tcp_batch(struct seq_file *seq)
 	struct sock *sk;
 	int err;
 

net/ipv4/tcp_ipv4.c static struct sock *bpf_iter_tcp_batch(struct seq_file *seq) · line 3139

Quoted 10 lines
net/ipv4/tcp_ipv4.c +3 -1
+again:
 	sk = bpf_iter_tcp_resume(seq);
 	if (!sk)
 		return NULL; /* Done */
 
 	expected = bpf_iter_fill_batch(seq, &sk);
-	if (likely(iter->end_sk == expected))
+	/* sk carries the first socket that did not fit in the batch. */
+	if (likely(!sk))
 		goto done;

[Severity: High]
This is a pre-existing issue, but I wanted to ask about the reallocation failure path that occurs just after this block in bpf_iter_tcp_batch().

If bpf_iter_tcp_realloc_batch() fails, the function calls bpf_iter_tcp_put_batch() and returns an error without clearing iter->end_sk.

When this happens, bpf_iter_tcp_put_batch() overwrites item->sk with a cookie:

net/ipv4/tcp_ipv4.c:bpf_iter_tcp_put_batch() {
    ...
    while (cur_sk < iter->end_sk) {
        ...
        sock_gen_put(item->sk);
        item->cookie = cookie;
    }
}

Because the seq framework subsequently calls bpf_iter_tcp_seq_stop(), it sees iter->cur_sk < iter->end_sk and calls bpf_iter_tcp_put_batch() a second time.

Could this lead to a wild pointer dereference when sock_gen_put() tries to use the cookie as a socket pointer during the second pass?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 94 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Low] The patch introduces a multi-line comment that violates the BPF subsystem comment style guidelines.

Pre-existing issues:
- [High] Double-put and wild pointer dereference of batch sockets on the first bpf_iter_tcp_realloc_batch failure path. --

--- Patch [1]: [PATCH bpf v2] bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch() --- commit c52d637a13758169d9dce0d7c05be97965d399d3
Author: Jose Fernandez (Anthropic) <[email protected]>

bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch()

This commit fixes a use-after-free issue in the TCP iterator caused by request sockets being published to the ehash chain with a refcount of 0 before being updated. The fix uses refcount_inc_not_zero() to safely handle these sockets during batch iteration. It also refactors the batch completion logic to rely on whether any sockets were left behind during the walk.

Quoted 3 lines
net/ipv4/tcp_ipv4.c +0 -0
@@ -3073,24 +3073,24 @@ static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq,

[ ... ]

Quoted 7 lines
net/ipv4/tcp_ipv4.c +0 -6
 	sk_nulls_for_each_from(sk, node) {
-		if (seq_sk_match(seq, sk)) {
-			if (iter->end_sk < iter->max_sk) {
-				sock_hold(sk);
-				iter->batch[iter->end_sk++].sk = sk;
-			} else if (!*start_sk) {
-				/* Remember where we left off. */

net/ipv4/tcp_ipv4.c static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq, · line 3087

Quoted 10 lines
net/ipv4/tcp_ipv4.c +7 -3
-				*start_sk = sk;
-			}
-			expected++;
+		if (!seq_sk_match(seq, sk))
+			continue;
+		expected++;
+		if (iter->end_sk < iter->max_sk) {
+			/* reqsk_queue_hash_req() inserts with sk_refcnt == 0
+			 * and refcount_set()s it after the bucket lock drops.
+			 */

[Severity: Low]
Does this multi-line comment follow the BPF subsystem comment style? The BPF subsystem guide expects the opening block to be on its own line:

/*
* reqsk_queue_hash_req() inserts with sk_refcnt == 0
...

Quoted 2 lines
net/ipv4/tcp_ipv4.c +2 -0
+			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+				continue;

[ ... ]

Quoted 4 lines
net/ipv4/tcp_ipv4.c +0 -0
@@ -3128,12 +3128,14 @@ static struct sock *bpf_iter_tcp_batch(struct seq_file *seq)
 	struct sock *sk;
 	int err;
 

net/ipv4/tcp_ipv4.c static struct sock *bpf_iter_tcp_batch(struct seq_file *seq) · line 3139

Quoted 10 lines
net/ipv4/tcp_ipv4.c +3 -1
+again:
 	sk = bpf_iter_tcp_resume(seq);
 	if (!sk)
 		return NULL; /* Done */
 
 	expected = bpf_iter_fill_batch(seq, &sk);
-	if (likely(iter->end_sk == expected))
+	/* sk carries the first socket that did not fit in the batch. */
+	if (likely(!sk))
 		goto done;

[Severity: High]
This is a pre-existing issue, but I wanted to ask about the reallocation failure path that occurs just after this block in bpf_iter_tcp_batch().

If bpf_iter_tcp_realloc_batch() fails, the function calls bpf_iter_tcp_put_batch() and returns an error without clearing iter->end_sk.

When this happens, bpf_iter_tcp_put_batch() overwrites item->sk with a cookie:

net/ipv4/tcp_ipv4.c:bpf_iter_tcp_put_batch() {
    ...
    while (cur_sk < iter->end_sk) {
        ...
        sock_gen_put(item->sk);
        item->cookie = cookie;
    }
}
Quoted 5 lines

Because the seq framework subsequently calls bpf_iter_tcp_seq_stop(), it sees iter->cur_sk < iter->end_sk and calls bpf_iter_tcp_put_batch() a second time.

Could this lead to a wild pointer dereference when sock_gen_put() tries to use the cookie as a socket pointer during the second pass?

Hi Kuniyuki,
Thoughts on the fix and Sashiko's concern? (The latter should be probably be fixed separately, if needed).

#3 lore raw
Quoted 4 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines · sashiko-bot

[Severity: Low]
Does this multi-line comment follow the BPF subsystem comment style? The BPF subsystem guide expects the opening block to be on its own line:

On the comment-style note, checkpatch accepts both block comment forms and the one used here matches the surrounding code in net/ipv4/tcp_ipv4.c, so I don't think the bot's flag applies. Either way is fine by me, whatever the maintainers prefer.

Quoted 7 lines
Quoted 3 lines

Pre-existing issues:
- [High] Double-put and wild pointer dereference of batch sockets on the first bpf_iter_tcp_realloc_batch failure path.

Hi Kuniyuki,
Thoughts on the fix and Sashiko's concern? (The latter should be probably be fixed separately, if needed).

The double-put is pre-existing and already fixed separately by 980a81345275 ("bpf: tcp: fix double sock release on batch realloc"), now in the bpf tree. The hunks are disjoint, so this series applies cleanly on top of it.

Thanks,
Jose

#4 lore raw
Quoted 16 lines · Jose Fernandez (Anthropic)
Quoted 4 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines · sashiko-bot

[Severity: Low]
Does this multi-line comment follow the BPF subsystem comment style? The BPF subsystem guide expects the opening block to be on its own line:

On the comment-style note, checkpatch accepts both block comment forms and the one used here matches the surrounding code in net/ipv4/tcp_ipv4.c, so I don't think the bot's flag applies. Either way is fine by me, whatever the maintainers prefer.

Quoted 4 lines
Quoted 3 lines

Pre-existing issues:
- [High] Double-put and wild pointer dereference of batch sockets on the first bpf_iter_tcp_realloc_batch failure path.

Quoted 8 lines
Quoted 3 lines

Hi Kuniyuki,
Thoughts on the fix and Sashiko's concern? (The latter should be probably be fixed separately, if needed).

The double-put is pre-existing and already fixed separately by 980a81345275 ("bpf: tcp: fix double sock release on batch realloc"), now in the bpf tree. The hunks are disjoint, so this series applies cleanly on top of it.

Sorry for the delay, I was on vacation for two weeks.

I'll review the patch.

Thanks

#5 lore raw
In reply to Jose Fernandez (Anthropic)
Quoted 51 lines · Jose Fernandez (Anthropic)

reqsk_queue_hash_req() publishes a TCP_NEW_SYN_RECV request_sock onto the ehash chain, drops the bucket lock, and only afterwards sets rsk_refcnt to 3.

Lockless readers such as __inet_lookup_established() handle this with refcount_inc_not_zero(), but bpf_iter_tcp_established_batch() uses plain sock_hold() while holding the bucket lock, on the assumption that the lock guarantees sk_refcnt > 0. That assumption does not hold for request_sock:

CPU 0 CPU 1
----- -----
tcp_conn_request()
reqsk_queue_hash_req()

    inet_ehash_insert(req)
     spin_lock(bucket)
     __sk_nulls_add_node_rcu(req)      // rsk_refcnt == 0
     spin_unlock(bucket)
                                       bpf_iter_tcp_established_batch()
                                        spin_lock(bucket)
                                        sock_hold(req)   <-- addition on 0
                                        spin_unlock(bucket)
    refcount_set(&req->rsk_refcnt, 3)  // clobbers saturated value

which surfaces as:

refcount_t: addition on 0; use-after-free.

  WARNING: lib/refcount.c:25 at refcount_warn_saturate+0x48/0x90, CPU#1
  Call Trace:
   bpf_iter_tcp_established_batch+0x14e/0x170
   bpf_iter_tcp_batch+0x53/0x200
   bpf_iter_tcp_seq_next+0x27/0x70
   bpf_seq_read+0x107/0x410
   vfs_read+0xb9/0x380

The iterator's stolen reference is lost when the publishing CPU's refcount_set() overwrites the count, leaving the socket one reference short. When the last legitimate owner drops its reference the reqsk is freed while still reachable, leading to use-after-free.

This reproduces in seconds with tcp_syncookies=0, a handful of threads doing connect()/close() to a local listener while others read an iter/tcp link in a tight loop.

Use refcount_inc_not_zero() and skip the socket on failure. A skipped socket is still part of the bucket, so keep counting it in expected. The reallocations are sized from expected, and a request sock whose refcount gets published while the lock is held across the last realloc must already have room.

Quoted 6 lines

A skipped socket is counted in expected but never batched, so end_sk can be short of expected on a batch that is actually complete. Decide completeness by whether the walk left any socket behind instead. The WARN after the locked realloc checks the same, replacing an end_sk == expected check that could not hold on that path since cdec67a489d4.

nit: please use the cacnonical format

commit cdec67a489d4 ("bpf: tcp: Make sure iter->batch always contains a full bucket snapshot")

Quoted 31 lines

If every matching socket in a bucket is mid-init (refcount 0), end_sk stays 0. Advance to the next bucket rather than returning a batch entry that was never filled this round.

Fixes 04c7820b776f ("bpf: tcp: Bpf iter batching and lock_sock")
Assisted-by Claude:unspecified

---
Changes in v2:
- Count expected right after seq_sk_match() so the batch reallocations
are sized for the whole bucket, including request socks whose
refcount is not yet published (Kuniyuki)
- Signal batch completeness by the walk leaving no leftover socket
instead of end_sk == expected, and check the same condition in the
WARN after the locked reallocation
- Drop the Reviewed-by tags given the code changes
- Rebase onto bpf/master
- Link to v1: https://lore.kernel.org/bpf/[email protected]

The pre-existing double-put on the realloc failure path (raised in the v1 thread) will be addressed in a separate follow-up patch.

---
 net/ipv4/tcp_ipv4.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)
net/ipv4/tcp_ipv4.c +0 -0
index 209ef7522508..d8640d114c0d 100644

net/ipv4/tcp_ipv4.c static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq, · line 3077

Quoted 7 lines
net/ipv4/tcp_ipv4.c +2 -2
@@ -3073,24 +3073,24 @@ static unsigned int bpf_iter_tcp_established_batch(struct seq_file *seq,
 {
        struct bpf_tcp_iter_state *iter = seq->private;
        struct hlist_nulls_node *node;
-       unsigned int expected = 1;
-       struct sock *sk;
+       unsigned int expected = 0;
+       struct sock *sk = *start_sk;

nit: please keep the reverse xmas tree order.

see: Documentation/process/maintainer-netdev.rst

Quoted 34 lines
net/ipv4/tcp_ipv4.c +13 -13
-       sock_hold(*start_sk);
-       iter->batch[iter->end_sk++].sk = *start_sk;
-
-       sk = sk_nulls_next(*start_sk);
        *start_sk = NULL;
        sk_nulls_for_each_from(sk, node) {
-               if (seq_sk_match(seq, sk)) {
-                       if (iter->end_sk < iter->max_sk) {
-                               sock_hold(sk);
-                               iter->batch[iter->end_sk++].sk = sk;
-                       } else if (!*start_sk) {
-                               /* Remember where we left off. */
-                               *start_sk = sk;
-                       }
-                       expected++;
+               if (!seq_sk_match(seq, sk))
+                       continue;
+               expected++;
+               if (iter->end_sk < iter->max_sk) {
+                       /* reqsk_queue_hash_req() inserts with sk_refcnt == 0
+                        * and refcount_set()s it after the bucket lock drops.
+                        */
+                       if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
+                               continue;
+                       iter->batch[iter->end_sk++].sk = sk;
+               } else if (!*start_sk) {
+                       /* Remember where we left off. */
+                       *start_sk = sk;
                }
        }
 
@@ -3128,12 +3128,14 @@ static struct sock *bpf_iter_tcp_batch(struct seq_file *seq)
        struct sock *sk;

net/ipv4/tcp_ipv4.c static struct sock *bpf_iter_tcp_batch(struct seq_file *seq) · line 3137

Quoted 10 lines
net/ipv4/tcp_ipv4.c +2 -1
        int err;
 
+again:
        sk = bpf_iter_tcp_resume(seq);
        if (!sk)
                return NULL; /* Done */
 
        expected = bpf_iter_fill_batch(seq, &sk);
-       if (likely(iter->end_sk == expected))
+       /* sk carries the first socket that did not fit in the batch. */

nit: please remove this comment since this is not always true, e.g. resume function does not find the old sk and moves to the next bucket.

The change itself looks good.

Thanks !