netfilter: nfnetlink_queue: keep socket ownership when expanding skb
Subsystems (2)
PATCH v2 1/1 netfilter: nfnetlink_queue: keep socket ownership when expanding skb
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.
---
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(-)
net/netfilter/nfnetlink_queue.c +5 -10
@@ -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))
Signature · 1 line
2.56.0