Skip to contentSkip to conversation

netfilter: nf_queue: do not queue BPF syncookie request sockets

PATCH In review Weiming Shi 3 participants 3 messages Nov 29 – Nov 30

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

PATCH 1/1 netfilter: nf_queue: do not queue BPF syncookie request sockets

bpf_sk_assign_tcp_reqsk() allocates a request socket with inet_reqsk_alloc(ops, sk, false), stores the listener in req->rsk_listener without taking a reference, and attaches it to the skb with skb->destructor = sock_pfree. Because neither the request socket (rsk_refcnt == 0) nor req->rsk_listener is refcounted, the listener pointer is only valid within the ingress RCU read-side critical section.

__nf_queue() lets the skb outlive that critical section while waiting for a userspace verdict. Its prefetched-socket check only handles !sk_is_refcounted(sk), which is false for TCP_NEW_SYN_RECV because sk_fullsock() is false for request sockets, and nf_queue_entry_get_refs() only pins state->sk (NULL at prerouting). If the listener is closed and its RCU grace period elapses while the packet is queued, reinjection returns the freed req->rsk_listener from skb_steal_sock() into inet_steal_sock() and tcp_v[46]_rcv():

  BUG: KASAN: slab-use-after-free in tcp_v4_rcv (include/net/sock.h:2295
   include/net/inet_hashtables.h:458 include/net/inet_hashtables.h:496
   net/ipv4/tcp_ipv4.c:2121)
  Read of size 1 at addr ffff888025542812 by task exploit/194
  Call Trace:
   tcp_v4_rcv (net/ipv4/tcp_ipv4.c:2121)
   ip_protocol_deliver_rcu (net/ipv4/ip_input.c:207)
   ip_local_deliver_finish (net/ipv4/ip_input.c:241)
   ip_local_deliver (net/ipv4/ip_input.c:262)
   ip_rcv_finish (net/ipv4/ip_input.c:492)
   nfqnl_reinject (net/netfilter/nfnetlink_queue.c:544)
   nfqnl_recv_verdict (net/netfilter/nfnetlink_queue.c:1905)
   ...
   __x64_sys_sendto (net/socket.c:2286)

Reject prefetched unrefcounted TCP_NEW_SYN_RECV request sockets in __nf_queue() with -ENOTCONN before the !sk_is_refcounted() check so nf_queue() drops the skb. Calling skb_orphan() instead would discard the validated request socket and cause tcp_v[46]_rcv() to fail cookie_tcp_check() and send a spurious RST on reinjection. Matching on !refcount_read(&sk->sk_refcnt) instead of inet_reqsk(sk)->syncookie also avoids a data race against reqsk_timer_handler() updating num_timeout in the same bitfield byte on hashed request sockets.

Fixes e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")
Assisted-by LLM

---
Note: this conflicts with "[PATCH RFC bpf-next 1/4] net: track references owned by prefetched socket assignments" ([email protected]), which rewrites the skb_sk_is_prefetched() block in __nf_queue() (patch 2/4 of that series, [email protected], addresses the sibling skb_do_redirect() escape). This check needs to stay outside the !sk_is_refcounted(skb->sk) branch. If pinning req->rsk_listener on struct nf_queue_entry is preferred over dropping these packets, happy to send a v2 with that approach instead.

 net/netfilter/nf_queue.c | 4 ++++
 1 file changed, 4 insertions(+)
net/netfilter/nf_queue.c +4 -0
index 166d05e994ac..ee9d2ee49855 100644
@@ -198,6 +198,10 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
 	if (skb_sk_is_prefetched(skb)) {
 		struct sock *sk = skb->sk;
 
+		if (READ_ONCE(sk->sk_state) == TCP_NEW_SYN_RECV &&
+		    !refcount_read(&sk->sk_refcnt))
+			return -ENOTCONN;
+
 		if (!sk_is_refcounted(sk)) {
 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
 				return -ENOTCONN;
Signature · 1 line
2.56.1
#1 lore raw
Quoted 32 lines · Weiming Shi

bpf_sk_assign_tcp_reqsk() allocates a request socket with inet_reqsk_alloc(ops, sk, false), stores the listener in req->rsk_listener without taking a reference, and attaches it to the skb with skb->destructor = sock_pfree. Because neither the request socket (rsk_refcnt == 0) nor req->rsk_listener is refcounted, the listener pointer is only valid within the ingress RCU read-side critical section.

__nf_queue() lets the skb outlive that critical section while waiting for a userspace verdict. Its prefetched-socket check only handles !sk_is_refcounted(sk), which is false for TCP_NEW_SYN_RECV because sk_fullsock() is false for request sockets, and nf_queue_entry_get_refs() only pins state->sk (NULL at prerouting). If the listener is closed and its RCU grace period elapses while the packet is queued, reinjection returns the freed req->rsk_listener from skb_steal_sock() into inet_steal_sock() and tcp_v[46]_rcv():

  BUG: KASAN: slab-use-after-free in tcp_v4_rcv (include/net/sock.h:2295
   include/net/inet_hashtables.h:458 include/net/inet_hashtables.h:496
   net/ipv4/tcp_ipv4.c:2121)
  Read of size 1 at addr ffff888025542812 by task exploit/194
  Call Trace:
   tcp_v4_rcv (net/ipv4/tcp_ipv4.c:2121)
   ip_protocol_deliver_rcu (net/ipv4/ip_input.c:207)
   ip_local_deliver_finish (net/ipv4/ip_input.c:241)
   ip_local_deliver (net/ipv4/ip_input.c:262)
   ip_rcv_finish (net/ipv4/ip_input.c:492)
   nfqnl_reinject (net/netfilter/nfnetlink_queue.c:544)
   nfqnl_recv_verdict (net/netfilter/nfnetlink_queue.c:1905)
   ...
   __x64_sys_sendto (net/socket.c:2286)
Quoted 8 lines

Reject prefetched unrefcounted TCP_NEW_SYN_RECV request sockets in __nf_queue() with -ENOTCONN before the !sk_is_refcounted() check so nf_queue() drops the skb. Calling skb_orphan() instead would discard the validated request socket and cause tcp_v[46]_rcv() to fail cookie_tcp_check() and send a spurious RST on reinjection. Matching on !refcount_read(&sk->sk_refcnt) instead of inet_reqsk(sk)->syncookie also avoids a data race against reqsk_timer_handler() updating num_timeout in the same bitfield byte on hashed request sockets.

Cc Nikolay.

I have seen working on this. I am not sure if this will require more checks in more spots (and more follow up incremental patches), which would be better to avoid.

Quoted 36 lines
Fixes e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")
Assisted-by LLM

---
Note: this conflicts with "[PATCH RFC bpf-next 1/4] net: track references owned by prefetched socket assignments" ([email protected]), which rewrites the skb_sk_is_prefetched() block in __nf_queue() (patch 2/4 of that series, [email protected], addresses the sibling skb_do_redirect() escape). This check needs to stay outside the !sk_is_refcounted(skb->sk) branch. If pinning req->rsk_listener on struct nf_queue_entry is preferred over dropping these packets, happy to send a v2 with that approach instead.

 net/netfilter/nf_queue.c | 4 ++++
 1 file changed, 4 insertions(+)
net/netfilter/nf_queue.c +4 -0
index 166d05e994ac..ee9d2ee49855 100644
@@ -198,6 +198,10 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
 	if (skb_sk_is_prefetched(skb)) {
 		struct sock *sk = skb->sk;
 
+		if (READ_ONCE(sk->sk_state) == TCP_NEW_SYN_RECV &&
+		    !refcount_read(&sk->sk_refcnt))
+			return -ENOTCONN;
+
 		if (!sk_is_refcounted(sk)) {
 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
 				return -ENOTCONN;
Signature · 1 line
2.56.1
#2 lore raw
In reply to Weiming Shi
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.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In net/netfilter/nf_queue.c, __nf_queue() now returns -ENOTCONN for every prefetched TCP_NEW_SYN_RECV socket with refcnt 0,…
- [Medium] The commit message says only that nf_queue() 'drops the skb', and argues this beats skb_orphan() because skb_orphan() would cause a…

--

netfilter: nf_queue: do not queue BPF syncookie request sockets

This patch makes __nf_queue() reject skbs that carry a prefetched, unrefcounted TCP_NEW_SYN_RECV request socket, the kind created by bpf_sk_assign_tcp_reqsk(). It returns -ENOTCONN so that nf_queue() drops the skb. The goal is to stop the skb from outliving the ingress RCU section while req->rsk_listener is not pinned.

net/netfilter/nf_queue.c static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state, · line 204

Quoted 12 lines
net/netfilter/nf_queue.c +4 -0
index bd603e3a99a59..54d902169065e 100644
@@ -198,6 +198,10 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
 	if (skb_sk_is_prefetched(skb)) {
 		struct sock *sk = skb->sk;
 
+		if (READ_ONCE(sk->sk_state) == TCP_NEW_SYN_RECV &&
+		    !refcount_read(&sk->sk_refcnt))
+			return -ENOTCONN;
+

[Severity: Medium]
Does this early return also drop packets that nfnetlink_queue would have passed through in the same RCU section?

The check runs before qh->outfn(), so nfnetlink_queue never sees these skbs. Two configurations look affected.

The first is --queue-bypass with no daemon bound to the queue. nfqnl_enqueue_packet() used to return -ESRCH here:

net/netfilter/nfnetlink_queue.c:nfqnl_enqueue_packet() {
    ...
	queue = instance_lookup(q, queuenum);
	if (!queue)
		return -ESRCH;
    ...
}

nf_queue() treats only that case as a bypass:

net/netfilter/nf_queue.c:nf_queue() {
    ...
	if (ret < 0) {
		if (ret == -ESRCH &&
		    (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
			return 1;
		kfree_skb(skb);
	}
    ...
}

The second is NFQA_CFG_F_FAIL_OPEN when the queue is full or the unicast fails. In that case __nfqnl_enqueue_packet() reinjects synchronously:

	if (failopen)
		nfqnl_reinject(entry, NF_ACCEPT);

In both cases the skb stayed inside the current RCU read-side section, so the stale req->rsk_listener could not be hit.

With this patch __nf_queue() returns -ENOTCONN, which nf_queue() does not treat as a bypass, so it calls kfree_skb(). Will BPF SYN cookie ACKs then be silently dropped in exactly the setups that are configured to fail open?

[Severity: Medium]
Does the commit message describe how wide the effect of this check is? It says:

Reject prefetched unrefcounted TCP_NEW_SYN_RECV request sockets in __nf_queue() with -ENOTCONN before the !sk_is_refcounted() check so nf_queue() drops the skb.

bpf_sk_assign_tcp_reqsk() allocates a new unrefcounted request socket on every call:

net/core/filter.c:bpf_sk_assign_tcp_reqsk() {
    ...
	req = inet_reqsk_alloc(ops, sk, false);
    ...
	req->rsk_listener = sk;
	req->syncookie = 1;
    ...
	skb->sk = req_to_sk(req);
	skb->destructor = sock_pfree;
    ...
}

As a result, every cookie ACK the BPF program validates (including retransmissions) matches the new check. If an NFQUEUE verdict applies on the ingress path, the packet is dropped, so the client sees an established connection that the server never accepts.

Before this patch the combination failed only when the listener closed while a packet was queued. After it, bpf_sk_assign_tcp_reqsk() and NFQUEUE look like they cannot work together at all.

Could the commit message state this? The other approach, pinning req->rsk_listener on struct nf_queue_entry the way the existing refcount_inc_not_zero()/sock_edemux handling pins prefetched sockets, appears only below the cut line. Would that approach be preferable here, since it keeps this combination working?

Quoted 3 lines
net/netfilter/nf_queue.c +0 -0
 		if (!sk_is_refcounted(sk)) {
 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
 				return -ENOTCONN;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129161228.4185092-2-bestswngs%40gmail.com