diff mbox series

[nf,v2] netfilter: nfnetlink_queue: keep socket ownership when expanding skb

Message ID 20261103165726.6170-1-fmancera@suse.de
State New
Headers show
Series [nf,v2] netfilter: nfnetlink_queue: keep socket ownership when expanding skb | expand

Commit Message

Fernando Fernandez Mancera Nov. 3, 2026, 4:57 p.m. UTC
nfqnl_mangle() replaces the skb with skb_copy_expand() when a verdict
grows a packet beyond its tailroom. skb_copy_expand() does not copy
skb->sk, so the replacement is unowned and freeing the original drops
its socket memory charge.

The expanded packet is no longer associated with the process that sent
it. Therefore xt_owner or xt_cgroup and other socket based matches in
later rules no longer matches and a later NFQUEUE reports no uid/gid. In
addition, the expanded packet's memory is not counted towards the
sender's SO_SNDBUF limit, so the sender can keep producing them while
they sit in a queue.

skb_copy_expand() was introduced to avoid the SKB_LINEAR_ASSERT in
skb_put() on non-linear skbs. Address that directly instead, linearize
the skb, then grow its head in place with pskb_expand_head(), which
preserves the socket association. While linearize + pskb_expand_head is
expensive, this is a really slow-path, so this should not be a problem.

Please note that the additional head space is not added to skb->truesize
nor to sk_wmem_alloc, so the socket is charged only for the size the skb
had when it was sent. Fixing that would require wmem adjustments that
depend on the skb destructor type, which is not worth the complexity
here as expanding payload requires CAP_NET_ADMIN and is rare for locally
generated packets.

Fixes: e2b58a67b91d ("[NETFILTER]: {ip,ip6,nfnetlink}_queue: fix SKB_LINEAR_ASSERT when mangling packet data")
Reported-by: VEGA <vega@nebusec.ai>
Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
---
Note: Florian this is my best attempt to fix the socket ownership issue
and almost the memory issue completely. If you think a complete fix is
necessary I would de-prioritize this as it isn't worth it. Anyway, for
the ownership this should be enough.
---
 net/netfilter/nfnetlink_queue.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index 90d887546f7d..1c9fd5f70694 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -1435,8 +1435,6 @@  static bool nfqnl_validate_write(const void *data, unsigned int data_len,
 static int
 nfqnl_mangle(void *data, unsigned int data_len, struct nf_queue_entry *e, int diff)
 {
-	struct sk_buff *nskb;
-
 	if (e->state.net->user_ns != &init_user_ns)
 		return -EPERM;
 
@@ -1454,14 +1452,11 @@  nfqnl_mangle(void *data, unsigned int data_len, struct nf_queue_entry *e, int di
 	} else if (diff > 0) {
 		if (data_len > 0xFFFF)
 			return -EINVAL;
-		if (diff > skb_tailroom(e->skb)) {
-			nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
-					       diff, GFP_ATOMIC);
-			if (!nskb)
-				return -ENOMEM;
-			kfree_skb(e->skb);
-			e->skb = nskb;
-		}
+		if (skb_linearize(e->skb))
+			return -ENOMEM;
+		if (diff > skb_tailroom(e->skb) &&
+		    pskb_expand_head(e->skb, 0, diff - skb_tailroom(e->skb), GFP_ATOMIC))
+			return -ENOMEM;
 		skb_put(e->skb, diff);
 	}
 	if (skb_ensure_writable(e->skb, data_len))