ipvs: read conn fields during sync atomically
Subsystems (3)
Julian Anastasov
maintainerv3 cover ipvs: read conn fields during sync atomically
Hello,
The following are two patches that ensure atomic access
for the connection fields in the backup and master servers.
The first patch makes sure the connection fields such as
flags, sync_conn_opt (seqs), state, old_state and timeout are updated atomically in the backup server.
As for the master server, the second patch takes snapshot
of the flags and seqs under lock before sending. This is to ensure we send valid seqs if their presence is marked in the flags we send.
Changes in v2:
* add patch 1
* patch 2: read seqs with flags under lock
Changes in v3:
* as v1 of patch 2 is applied, make v3 relative to the applied commit and rename it
Link to v1:
https://lore.kernel.org/all/[email protected]/
Link to v2:
https://lore.kernel.org/all/[email protected]/
Julian Anastasov (2):
ipvs: update flags and seqs in backup server under lock
ipvs: read flags and seqs in master atomically
net/netfilter/ipvs/ip_vs_sync.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-)
Signature · 1 line
2.56.0
Julian Anastasov
maintainerPATCH v3 1/2 ipvs: update flags and seqs in backup server under lock
Sashiko reports that we modify the conn flags and seq values in the backup server without holding cp->lock. The same is true for other fields such as state, old_state and timeout which are modified by protocols under lock.
Extend the locked region in ip_vs_proc_conn() to update the fields atomically.
--- net/netfilter/ipvs/ip_vs_sync.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
net/netfilter/ipvs/ip_vs_sync.c +6 -3
@@ -850,6 +850,7 @@ static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *pa unsigned long timeout, __u32 fwmark, struct ip_vs_sync_conn_options *opt) { + struct ip_vs_conn *cp_new = NULL;struct ip_vs_dest *dest;struct ip_vs_conn *cp;
@@ -891,9 +892,6 @@ static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *pa flags &=IP_VS_CONN_F_BACKUP_UPD_MASK ; flags |= cp->flags & ~IP_VS_CONN_F_BACKUP_UPD_MASK ; cp->flags = flags; -spin_unlock_bh (&cp->lock); -if (!dest) -ip_vs_try_bind_dest (cp); }else {/* * Find the appropriate destination for the connection.
@@ -920,6 +918,8 @@ static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *pa }if (!(flags &IP_VS_CONN_F_TEMPLATE ))kfree (param->pe_data); + cp_new = cp; +spin_lock_bh (&cp->lock); }if (opt) {
@@ -951,6 +951,9 @@ static void ip_vs_proc_conn(struct netns_ipvs *ipvs, struct ip_vs_conn_param *paelse cp->timeout = (3 *60 *HZ ); } +spin_unlock_bh (&cp->lock); +if (!dest && !cp_new) +ip_vs_try_bind_dest (cp);ip_vs_conn_put (cp); }
Signature · 1 line
2.56.0
Julian Anastasov
maintainerPATCH v3 2/2 ipvs: read flags and seqs in master atomically
In reply to Julian AnastasovAfter we fixed the double read of cp->flags in the master with commit 3e3fe49e8678 ("ipvs: fix buffer overflow when sending sync messages") Sashiko reports that we should also read the cp->flags and the seqs atomically. So if the SEQ_MASK is set, re-read the flags and the seqs under cp->lock to provide a snapshot when sending the sync message.
--- net/netfilter/ipvs/ip_vs_sync.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
net/netfilter/ipvs/ip_vs_sync.c +20 -3
@@ -539,6 +539,7 @@ static int ip_vs_sync_conn_needed(struct netns_ipvs *ipvs,static void ip_vs_sync_conn_v0 (struct netns_ipvs *ipvs,struct ip_vs_conn *cp,int pkts) { +struct ip_vs_sync_conn_options opts;struct ip_vs_sync_mesg_v0 *m;struct ip_vs_sync_conn_v0 *s;struct ip_vs_sync_buff *buff;
@@ -557,6 +558,14 @@ static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,if (!ip_vs_sync_conn_needed (ipvs, cp, pkts))return ; +if (flags &IP_VS_CONN_F_SEQ_MASK ) { +/* Take atomic snapshot of flags and seqs */ +spin_lock_bh (&cp->lock); + flags = cp->flags; +memcpy (&opts, &cp->sync_conn_opt,sizeof (opts)); +spin_unlock_bh (&cp->lock); + } +spin_lock_bh (&ipvs->sync_buff_lock);if (!(ipvs->sync_state &IP_VS_STATE_MASTER )) {spin_unlock_bh (&ipvs->sync_buff_lock);
@@ -604,7 +613,7 @@ static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,if (seq_mask) {struct ip_vs_sync_conn_options *opt = (struct ip_vs_sync_conn_options *)&s[1 ]; -memcpy (opt, &cp->sync_conn_opt,sizeof (*opt)); +memcpy (opt, &opts,sizeof (*opt)); } m->nr_conns++;
@@ -630,6 +639,7 @@ static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,*/ void ip_vs_sync_conn (struct netns_ipvs *ipvs,struct ip_vs_conn *cp,int pkts) { +struct ip_vs_sync_conn_options opts;struct ip_vs_sync_mesg *m;union ip_vs_sync_conn *s;struct ip_vs_sync_buff *buff;
@@ -651,6 +661,13 @@ void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts)if (!ip_vs_sync_conn_needed (ipvs, cp, pkts)) goto control; flags =READ_ONCE (cp->flags); +if (flags &IP_VS_CONN_F_SEQ_MASK ) { +/* Take atomic snapshot of flags and seqs */ +spin_lock_bh (&cp->lock); + flags = cp->flags; +memcpy (&opts, &cp->sync_conn_opt,sizeof (opts)); +spin_unlock_bh (&cp->lock); + }/* Sanity checks */ pe_name_len =0 ;
@@ -752,9 +769,9 @@ void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts)if (seq_mask) { *(p++) =IPVS_OPT_SEQ_DATA ; *(p++) =sizeof (struct ip_vs_sync_conn_options ); -hton_seq (&cp->in_seq, (struct ip_vs_seq *)p); +hton_seq (&opts.in_seq, (struct ip_vs_seq *)p); p +=sizeof (struct ip_vs_seq ); -hton_seq (&cp->out_seq, (struct ip_vs_seq *)p); +hton_seq (&opts.out_seq, (struct ip_vs_seq *)p); p +=sizeof (struct ip_vs_seq ); }/* Handle pe data */
Signature · 1 line
2.56.0