netfilter: synproxy: refresh tcphdr after skb_ensure_writable
Subsystems (2)
PATCH 1/1 netfilter: synproxy: refresh tcphdr after skb_ensure_writable
synproxy_tstamp_adjust() rewrites the TCP timestamp option in place and then patches the TCP checksum via inet_proto_csum_replace4() on the caller-supplied tcphdr pointer. Both ipv4_synproxy_hook() and ipv6_synproxy_hook() obtain that pointer with skb_header_pointer() before calling in, so it may either alias skb->head directly or point at the caller's on-stack _tcph buffer.
Between obtaining the pointer and using it, the function calls skb_ensure_writable(skb, optend), which on a cloned or non-linear skb invokes pskb_expand_head() and frees the old skb->head. After that point the cached th is stale:
caller (ipv[46]_synproxy_hook)
th = skb_header_pointer(skb, ..., &_tcph)
synproxy_tstamp_adjust(skb, protoff, th, ...)
skb_ensure_writable(skb, optend)
pskb_expand_head() /* kfree(old skb->head) */
...
inet_proto_csum_replace4(&th->check, ...)
/* writes into freed head, or
into the caller's stack copy
leaving the on-wire checksum
stale */The option bytes are written through skb->data and are fine; only the checksum update goes through th and so lands in the wrong place. The result is either a write into freed slab memory or a packet leaving with a checksum that does not match its payload.
Fix by re-deriving th from skb->data + protoff immediately after skb_ensure_writable() succeeds, so the subsequent checksum update targets the linear, writable header.
--- net/netfilter/nf_synproxy_core.c | 2 ++ 1 file changed, 2 insertions(+)
net/netfilter/nf_synproxy_core.c +2 -0
@@ -200,6 +200,8 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,if (skb_ensure_writable (skb, optend))return 0 ; + th = (struct tcphdr *)(skb->data + protoff); +while (optoff < optend) {unsigned char *op = skb->data + optoff;
Signature · 1 line
2.52.0
Quoted 29 lines · Chris Mason
synproxy_tstamp_adjust()rewrites the TCP timestamp option in place and then patches the TCP checksum viainet_proto_csum_replace4()on the caller-supplied tcphdr pointer. Bothipv4_synproxy_hook()andipv6_synproxy_hook()obtain that pointer withskb_header_pointer()before calling in, so it may either aliasskb->headdirectly or point at the caller's on-stack_tcphbuffer.Between obtaining the pointer and using it, the function calls
skb_ensure_writable(skb, optend), which on a cloned or non-linear skb invokespskb_expand_head()and frees the oldskb->head. After that point the cached th is stale:caller (ipv[46]_synproxy_hook) th = skb_header_pointer(skb, ..., &_tcph) synproxy_tstamp_adjust(skb, protoff, th, ...) skb_ensure_writable(skb, optend) pskb_expand_head() /* kfree(old skb->head) */ ... inet_proto_csum_replace4(&th->check, ...) /* writes into freed head, or into the caller's stack copy leaving the on-wire checksum stale */The option bytes are written through
skb->dataand are fine; only the checksum update goes through th and so lands in the wrong place. The result is either a write into freed slab memory or a packet leaving with a checksum that does not match its payload.
Quoted 7 lines
Fix by re-deriving th from
skb->data+ protoff immediately afterskb_ensure_writable()succeeds, so the subsequent checksum update targets the linear, writable header.Fixes 48b1de4c110a ("netfilter: add SYNPROXY core/target")Assisted-by kres (claude-opus-4-7)Signed-off-by Chris Mason <[email protected]>
Thanks, Sashiko flagged other issues that I am now addressing..