netfilter: synproxy: misc fixes about synproxy core
Subsystems (2)
v3 cover netfilter: synproxy: misc fixes about synproxy core
This series fixes several long standing issues during synproxy timestamp adjustment and concurrent hook registration. From ignored error handling to unaligned memory access. Most of this are not issues impacting real setups as they would have been reported before.
FWIW; I am sending these fixes as separated patches because they are addressing independent issues.
Fernando Fernandez Mancera (5):
netfilter: synproxy: drop packets if timestamp adjustment fails
netfilter: synproxy: adjust duplicate timestamp options
netfilter: synproxy: fix unaligned memory access in timestamp
adjustment
netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
netfilter: synproxy: add mutex to guard hook reference counting
net/netfilter/nf_synproxy_core.c | 76 +++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 25 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/5 netfilter: synproxy: drop packets if timestamp adjustment fails
If a packet was malformed or if skb_ensure_writable() failed, the synproxy_tstamp_adjust() function returned 0 indicating an error but it was ignored on the callers.
Make the function return a boolean instead to clarify the result and drop the packet if synproxy_tstamp_adjust() failed due to ENOMEM from skb_ensure_writable(). In addition, if there are malformed options, skip the tstamp update but do not drop the packet as that should be done by the policy directly.
--- net/netfilter/nf_synproxy_core.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
net/netfilter/nf_synproxy_core.c +13 -9
@@ -182,7 +182,7 @@ synproxy_check_timestamp_cookie(struct synproxy_options *opts) opts->options |= opts->tsecr & (1 <<5 ) ?NF_SYNPROXY_OPT_ECN :0 ; } -static unsigned int +static bool synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, struct tcphdr *th, struct nf_conn *ct, enum ip_conntrack_info ctinfo,
@@ -192,20 +192,20 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,__be32 *ptr, old;if (synproxy->tsoff ==0 ) -return 1 ; +return true; optoff = protoff +sizeof (struct tcphdr ); optend = protoff + th->doff *4 ;if (skb_ensure_writable (skb, optend)) -return 0 ; +return false;while (optoff < optend) {unsigned char *op = skb->data + optoff;switch (op[0 ]) {case TCPOPT_EOL : -return 1 ; +return true;case TCPOPT_NOP : optoff++;continue ;
@@ -213,7 +213,7 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,if (optoff +1 == optend || optoff + op[1 ] > optend || op[1 ] <2 ) -return 0 ; +return true;if (op[0 ] ==TCPOPT_TIMESTAMP && op[1 ] ==TCPOLEN_TIMESTAMP ) {if (CTINFO2DIR (ctinfo) ==IP_CT_DIR_REPLY ) {
@@ -229,12 +229,12 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, }inet_proto_csum_replace4 (&th->check, skb, old, *ptr, false); -return 1 ; +return true; } optoff += op[1 ]; } } -return 1 ; +return true; }#ifdef CONFIG_PROC_FS
@@ -745,7 +745,9 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,break ; } -synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy); +if (!synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy)) +return NF_DROP_REASON (skb,SKB_DROP_REASON_NETFILTER_DROP ,ENOMEM ); +return NF_ACCEPT ; }EXPORT_SYMBOL_GPL (ipv4_synproxy_hook);
@@ -1168,7 +1170,9 @@ ipv6_synproxy_hook(void *priv, struct sk_buff *skb,break ; } -synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy); +if (!synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy)) +return NF_DROP_REASON (skb,SKB_DROP_REASON_NETFILTER_DROP ,ENOMEM ); +return NF_ACCEPT ; }EXPORT_SYMBOL_GPL (ipv6_synproxy_hook);
Signature · 1 line
2.53.0
PATCH v3 2/5 netfilter: synproxy: adjust duplicate timestamp options
In reply to Fernando Fernandez ManceraRFC 9293 does not mention anything about duplicated options and each networking stack handles it in their own way. Currently, Linux kernel is processing options sequentially and in case of duplicated timestamp options, the value from the latest one overrides the others.
As SYNPROXY is modifying only the first timestamp option found, a packet can reach the backend server and it might parse the wrong timestamp value. Let's just continue parsing the following options and in case a duplicated timestamp is found, adjust it too.
--- net/netfilter/nf_synproxy_core.c | 1 - 1 file changed, 1 deletion(-)
net/netfilter/nf_synproxy_core.c +0 -1
@@ -229,7 +229,6 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, }inet_proto_csum_replace4 (&th->check, skb, old, *ptr, false); -return true; } optoff += op[1 ]; }
Signature · 1 line
2.53.0
PATCH v3 3/5 netfilter: synproxy: fix unaligned memory access in timestamp adjustment
In reply to Fernando Fernandez ManceraUse get_unaligned_be32() and put_unaligned_be32() to safely read and write the timestamp fields. This prevents performance degradation due to unaligned memory access or even a crash on strict alignment architectures.
This follows the implementation of timestamp parsing in the networking stack at tcp_parse_options() and synproxy_parse_options().
--- net/netfilter/nf_synproxy_core.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
net/netfilter/nf_synproxy_core.c +9 -10
@@ -189,7 +189,7 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, const struct nf_conn_synproxy *synproxy) {unsigned int optoff, optend; -__be32 *ptr, old; +u32 new, old;if (synproxy->tsoff ==0 )return true;
@@ -217,18 +217,17 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,if (op[0 ] ==TCPOPT_TIMESTAMP && op[1 ] ==TCPOLEN_TIMESTAMP ) {if (CTINFO2DIR (ctinfo) ==IP_CT_DIR_REPLY ) { - ptr = (__be32 *)&op[2 ]; - old = *ptr; - *ptr =htonl (ntohl (*ptr) - - synproxy->tsoff); + old =get_unaligned_be32 (&op[2 ]); + new = old - synproxy->tsoff; +put_unaligned_be32 (new, &op[2 ]); }else { - ptr = (__be32 *)&op[6 ]; - old = *ptr; - *ptr =htonl (ntohl (*ptr) + - synproxy->tsoff); + old =get_unaligned_be32 (&op[6 ]); + new = old + synproxy->tsoff; +put_unaligned_be32 (new, &op[6 ]); }inet_proto_csum_replace4 (&th->check, skb, - old, *ptr, false); +cpu_to_be32 (old), +cpu_to_be32 (new), false); } optoff += op[1 ]; }
Signature · 1 line
2.53.0
PATCH v3 4/5 netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
In reply to Fernando Fernandez Manceranf_ct_seqadj_init() is called without holding the ct lock. This can race with nf_ct_seq_adjust() when a connection is in CLOSE state due to an RST or connection reopening. In addition for SYN_RECV state, concurrent processing of packets can trigger nf_ct_seq_adjust() too. These situations create a read/write data race.
Fix this by wrapping the nf_ct_seqadj_init() calls in the synproxy hooks with locking.
--- net/netfilter/nf_synproxy_core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
net/netfilter/nf_synproxy_core.c +12 -0
@@ -669,8 +669,10 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,switch (state->state) {case TCP_CONNTRACK_CLOSE :if (th->rst &&CTINFO2DIR (ctinfo) !=IP_CT_DIR_ORIGINAL ) { +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo, synproxy->isn -ntohl (th->seq) +1 ); +spin_unlock_bh (&ct->lock);break ; }
@@ -682,7 +684,9 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,* adjustments, they will get initialized once the connection is * reestablished. */ +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo,0 ); +spin_unlock_bh (&ct->lock); synproxy->tsoff =0 ;this_cpu_inc (snet->stats->conn_reopened); fallthrough;
@@ -731,7 +735,9 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,swap (opts.tsval, opts.tsecr);synproxy_send_server_ack (net, state, skb, th, &opts); +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo, synproxy->isn -ntohl (th->seq)); +spin_unlock_bh (&ct->lock);nf_conntrack_event_cache (IPCT_SEQADJ , ct);swap (opts.tsval, opts.tsecr);
@@ -1094,8 +1100,10 @@ ipv6_synproxy_hook(void *priv, struct sk_buff *skb,switch (state->state) {case TCP_CONNTRACK_CLOSE :if (th->rst &&CTINFO2DIR (ctinfo) !=IP_CT_DIR_ORIGINAL ) { +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo, synproxy->isn -ntohl (th->seq) +1 ); +spin_unlock_bh (&ct->lock);break ; }
@@ -1107,7 +1115,9 @@ ipv6_synproxy_hook(void *priv, struct sk_buff *skb,* adjustments, they will get initialized once the connection is * reestablished. */ +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo,0 ); +spin_unlock_bh (&ct->lock); synproxy->tsoff =0 ;this_cpu_inc (snet->stats->conn_reopened); fallthrough;
@@ -1156,7 +1166,9 @@ ipv6_synproxy_hook(void *priv, struct sk_buff *skb,swap (opts.tsval, opts.tsecr);synproxy_send_server_ack_ipv6 (net, state, skb, th, &opts); +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo, synproxy->isn -ntohl (th->seq)); +spin_unlock_bh (&ct->lock);nf_conntrack_event_cache (IPCT_SEQADJ , ct);swap (opts.tsval, opts.tsecr);
Signature · 1 line
2.53.0
Pablo Neira Ayuso
maintainerRe: [PATCH 4/5 nf-next v3] netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
Hi Fernando,
linux$ git grep nf_ct_seqadj_init
net/netfilter/nf_conntrack_seqadj.c:int nf_ct_seqadj_init(struct nf_conn *ct, enum ip_conntrack_info ctinfo, net/netfilter/nf_conntrack_seqadj.c:EXPORT_SYMBOL_GPL(nf_ct_seqadj_init);
net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, 0); net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq)); net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, 0); net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
net/netfilter/nf_synproxy_core.c ipv4_synproxy_hook(void *priv, struct sk_buff *skb, · line 676
Quoted 13 lines · Fernando Fernandez Mancera
net/netfilter/nf_synproxy_core.c +2
-0@@ -669,8 +669,10 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,switch (state->state) {case TCP_CONNTRACK_CLOSE :if (th->rst &&CTINFO2DIR (ctinfo) !=IP_CT_DIR_ORIGINAL ) { +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo, synproxy->isn -ntohl (th->seq) +1 ); +spin_unlock_bh (&ct->lock);break ;
Maybe add the spin_lock to nf_ct_seqadj_init() given synproxy is the only user of this function?
Quoted 2 lines
net/netfilter/nf_synproxy_core.c +0
-0}
Re: [PATCH 4/5 nf-next v3] netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
Quoted 13 lines · Pablo Neira Ayuso
Hi Fernando,
linux$ git grep
nf_ct_seqadj_init
net/netfilter/nf_conntrack_seqadj.c:int nf_ct_seqadj_init(structnf_conn*ct, enumip_conntrack_infoctinfo, net/netfilter/nf_conntrack_seqadj.c:EXPORT_SYMBOL_GPL(nf_ct_seqadj_init);net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, 0); net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq)); net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, 0); net/netfilter/nf_synproxy_core.c: nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));On Tue, May 26, 2026 at 04:18:37PM +0200, Fernando Fernandez Mancera wrote:
Quoted 17 lines
net/netfilter/nf_synproxy_core.c ipv4_synproxy_hook(void *priv, struct sk_buff *skb, · line 676
Quoted 13 lines
net/netfilter/nf_synproxy_core.c +2
-0@@ -669,8 +669,10 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,switch (state->state) {case TCP_CONNTRACK_CLOSE :if (th->rst &&CTINFO2DIR (ctinfo) !=IP_CT_DIR_ORIGINAL ) { +spin_lock_bh (&ct->lock);nf_ct_seqadj_init (ct, ctinfo, synproxy->isn -ntohl (th->seq) +1 ); +spin_unlock_bh (&ct->lock);break ;Maybe add the
spin_locktonf_ct_seqadj_init()given synproxy is the only user of this function?
Thanks Pablo, makes a lot of sense.
Quoted 3 lines
Quoted 2 lines
net/netfilter/nf_synproxy_core.c +0
-0}
PATCH v3 5/5 netfilter: synproxy: add mutex to guard hook reference counting
In reply to Fernando Fernandez ManceraAs the synproxy infrastructure register netfilter hooks on-demand when a user adds the first iptables target or nftables expression, if done concurrently they can race each other.
Introduce a mutex to serialize the refcount control blocks access from both frontends. While a per namespace mutex might be more efficient, it is not needed for target/expression like SYNPROXY.
--- net/netfilter/nf_synproxy_core.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
net/netfilter/nf_synproxy_core.c +18 -6
@@ -22,6 +22,8 @@#include <net/netfilter/nf_conntrack_zones.h> #include <net/netfilter/nf_synproxy.h> +static DEFINE_MUTEX (synproxy_mutex); +unsigned int synproxy_net_id;EXPORT_SYMBOL_GPL (synproxy_net_id);
@@ -773,26 +775,31 @@ static const struct nf_hook_ops ipv4_synproxy_ops[] = {int nf_synproxy_ipv4_init (struct synproxy_net *snet,struct net *net) { -int err; +int err =0 ; +mutex_lock (&synproxy_mutex);if (snet->hook_ref4 ==0 ) { err =nf_register_net_hooks (net, ipv4_synproxy_ops,ARRAY_SIZE (ipv4_synproxy_ops));if (err) -return err; + goto out; } snet->hook_ref4++; -return 0 ; +out: +mutex_unlock (&synproxy_mutex); +return err; }EXPORT_SYMBOL_GPL (nf_synproxy_ipv4_init);void nf_synproxy_ipv4_fini (struct synproxy_net *snet,struct net *net) { +mutex_lock (&synproxy_mutex); snet->hook_ref4--;if (snet->hook_ref4 ==0 )nf_unregister_net_hooks (net, ipv4_synproxy_ops,ARRAY_SIZE (ipv4_synproxy_ops)); +mutex_unlock (&synproxy_mutex); }EXPORT_SYMBOL_GPL (nf_synproxy_ipv4_fini);
@@ -1205,27 +1212,32 @@ static const struct nf_hook_ops ipv6_synproxy_ops[] = {int nf_synproxy_ipv6_init (struct synproxy_net *snet,struct net *net) { -int err; +int err =0 ; +mutex_lock (&synproxy_mutex);if (snet->hook_ref6 ==0 ) { err =nf_register_net_hooks (net, ipv6_synproxy_ops,ARRAY_SIZE (ipv6_synproxy_ops));if (err) -return err; + goto out; } snet->hook_ref6++; -return 0 ; +out: +mutex_unlock (&synproxy_mutex); +return err; }EXPORT_SYMBOL_GPL (nf_synproxy_ipv6_init);void nf_synproxy_ipv6_fini (struct synproxy_net *snet,struct net *net) { +mutex_lock (&synproxy_mutex); snet->hook_ref6--;if (snet->hook_ref6 ==0 )nf_unregister_net_hooks (net, ipv6_synproxy_ops,ARRAY_SIZE (ipv6_synproxy_ops)); +mutex_unlock (&synproxy_mutex); }EXPORT_SYMBOL_GPL (nf_synproxy_ipv6_fini);#endif /* CONFIG_IPV6 */
Signature · 1 line
2.53.0