Skip to content

Commit e024bdc

Browse files
committed
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
Pablo Neira Ayuso says: ==================== The following patchset contains Netfilter/IPVS fixes for your net tree, they are: * Fix BUG_ON splat due to malformed TCP packets seen by synproxy, from Patrick McHardy. * Fix possible weight overflow in lblc and lblcr schedulers due to 32-bits arithmetics, from Simon Kirby. * Fix possible memory access race in the lblc and lblcr schedulers, introduced when it was converted to use RCU, two patches from Julian Anastasov. * Fix hard dependency on CPU 0 when reading per-cpu stats in the rate estimator, from Julian Anastasov. * Fix race that may lead to object use after release, when invoking ipvsadm -C && ipvsadm -R, introduced when adding RCU, from Julian Anastasov. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 1ed98ed + f4a87e7 commit e024bdc

13 files changed

Lines changed: 143 additions & 158 deletions

File tree

‎include/net/ip_vs.h‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,6 @@ struct ip_vs_dest_dst {
723723
struct rcu_head rcu_head;
724724
};
725725

726-
/* In grace period after removing */
727-
#define IP_VS_DEST_STATE_REMOVING 0x01
728726
/*
729727
* The real server destination forwarding entry
730728
* with ip address, port number, and so on.
@@ -742,7 +740,7 @@ struct ip_vs_dest {
742740

743741
atomic_t refcnt; /* reference counter */
744742
struct ip_vs_stats stats; /* statistics */
745-
unsigned long state; /* state flags */
743+
unsigned long idle_start; /* start time, jiffies */
746744

747745
/* connection counters and thresholds */
748746
atomic_t activeconns; /* active connections */
@@ -756,14 +754,13 @@ struct ip_vs_dest {
756754
struct ip_vs_dest_dst __rcu *dest_dst; /* cached dst info */
757755

758756
/* for virtual service */
759-
struct ip_vs_service *svc; /* service it belongs to */
757+
struct ip_vs_service __rcu *svc; /* service it belongs to */
760758
__u16 protocol; /* which protocol (TCP/UDP) */
761759
__be16 vport; /* virtual port number */
762760
union nf_inet_addr vaddr; /* virtual IP address */
763761
__u32 vfwmark; /* firewall mark of service */
764762

765763
struct list_head t_list; /* in dest_trash */
766-
struct rcu_head rcu_head;
767764
unsigned int in_rs_table:1; /* we are in rs_table */
768765
};
769766

@@ -1649,7 +1646,7 @@ static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
16491646
/* CONFIG_IP_VS_NFCT */
16501647
#endif
16511648

1652-
static inline unsigned int
1649+
static inline int
16531650
ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
16541651
{
16551652
/*

‎include/net/netfilter/nf_conntrack_synproxy.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct synproxy_options {
5656

5757
struct tcphdr;
5858
struct xt_synproxy_info;
59-
extern void synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
59+
extern bool synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
6060
const struct tcphdr *th,
6161
struct synproxy_options *opts);
6262
extern unsigned int synproxy_options_size(const struct synproxy_options *opts);

‎net/ipv4/netfilter/ipt_SYNPROXY.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
267267
if (th == NULL)
268268
return NF_DROP;
269269

270-
synproxy_parse_options(skb, par->thoff, th, &opts);
270+
if (!synproxy_parse_options(skb, par->thoff, th, &opts))
271+
return NF_DROP;
271272

272273
if (th->syn && !(th->ack || th->fin || th->rst)) {
273274
/* Initial SYN from client */
@@ -350,7 +351,8 @@ static unsigned int ipv4_synproxy_hook(unsigned int hooknum,
350351

351352
/* fall through */
352353
case TCP_CONNTRACK_SYN_SENT:
353-
synproxy_parse_options(skb, thoff, th, &opts);
354+
if (!synproxy_parse_options(skb, thoff, th, &opts))
355+
return NF_DROP;
354356

355357
if (!th->syn && th->ack &&
356358
CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
@@ -373,7 +375,9 @@ static unsigned int ipv4_synproxy_hook(unsigned int hooknum,
373375
if (!th->syn || !th->ack)
374376
break;
375377

376-
synproxy_parse_options(skb, thoff, th, &opts);
378+
if (!synproxy_parse_options(skb, thoff, th, &opts))
379+
return NF_DROP;
380+
377381
if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
378382
synproxy->tsoff = opts.tsval - synproxy->its;
379383

‎net/ipv6/netfilter/ip6t_SYNPROXY.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par)
282282
if (th == NULL)
283283
return NF_DROP;
284284

285-
synproxy_parse_options(skb, par->thoff, th, &opts);
285+
if (!synproxy_parse_options(skb, par->thoff, th, &opts))
286+
return NF_DROP;
286287

287288
if (th->syn && !(th->ack || th->fin || th->rst)) {
288289
/* Initial SYN from client */
@@ -372,7 +373,8 @@ static unsigned int ipv6_synproxy_hook(unsigned int hooknum,
372373

373374
/* fall through */
374375
case TCP_CONNTRACK_SYN_SENT:
375-
synproxy_parse_options(skb, thoff, th, &opts);
376+
if (!synproxy_parse_options(skb, thoff, th, &opts))
377+
return NF_DROP;
376378

377379
if (!th->syn && th->ack &&
378380
CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
@@ -395,7 +397,9 @@ static unsigned int ipv6_synproxy_hook(unsigned int hooknum,
395397
if (!th->syn || !th->ack)
396398
break;
397399

398-
synproxy_parse_options(skb, thoff, th, &opts);
400+
if (!synproxy_parse_options(skb, thoff, th, &opts))
401+
return NF_DROP;
402+
399403
if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
400404
synproxy->tsoff = opts.tsval - synproxy->its;
401405

‎net/netfilter/ipvs/ip_vs_core.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,22 @@ ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
116116

117117
if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
118118
struct ip_vs_cpu_stats *s;
119+
struct ip_vs_service *svc;
119120

120121
s = this_cpu_ptr(dest->stats.cpustats);
121122
s->ustats.inpkts++;
122123
u64_stats_update_begin(&s->syncp);
123124
s->ustats.inbytes += skb->len;
124125
u64_stats_update_end(&s->syncp);
125126

126-
s = this_cpu_ptr(dest->svc->stats.cpustats);
127+
rcu_read_lock();
128+
svc = rcu_dereference(dest->svc);
129+
s = this_cpu_ptr(svc->stats.cpustats);
127130
s->ustats.inpkts++;
128131
u64_stats_update_begin(&s->syncp);
129132
s->ustats.inbytes += skb->len;
130133
u64_stats_update_end(&s->syncp);
134+
rcu_read_unlock();
131135

132136
s = this_cpu_ptr(ipvs->tot_stats.cpustats);
133137
s->ustats.inpkts++;
@@ -146,18 +150,22 @@ ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
146150

147151
if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
148152
struct ip_vs_cpu_stats *s;
153+
struct ip_vs_service *svc;
149154

150155
s = this_cpu_ptr(dest->stats.cpustats);
151156
s->ustats.outpkts++;
152157
u64_stats_update_begin(&s->syncp);
153158
s->ustats.outbytes += skb->len;
154159
u64_stats_update_end(&s->syncp);
155160

156-
s = this_cpu_ptr(dest->svc->stats.cpustats);
161+
rcu_read_lock();
162+
svc = rcu_dereference(dest->svc);
163+
s = this_cpu_ptr(svc->stats.cpustats);
157164
s->ustats.outpkts++;
158165
u64_stats_update_begin(&s->syncp);
159166
s->ustats.outbytes += skb->len;
160167
u64_stats_update_end(&s->syncp);
168+
rcu_read_unlock();
161169

162170
s = this_cpu_ptr(ipvs->tot_stats.cpustats);
163171
s->ustats.outpkts++;

‎net/netfilter/ipvs/ip_vs_ctl.c‎

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ static inline void
460460
__ip_vs_bind_svc(struct ip_vs_dest *dest, struct ip_vs_service *svc)
461461
{
462462
atomic_inc(&svc->refcnt);
463-
dest->svc = svc;
463+
rcu_assign_pointer(dest->svc, svc);
464464
}
465465

466466
static void ip_vs_service_free(struct ip_vs_service *svc)
@@ -470,18 +470,25 @@ static void ip_vs_service_free(struct ip_vs_service *svc)
470470
kfree(svc);
471471
}
472472

473-
static void
474-
__ip_vs_unbind_svc(struct ip_vs_dest *dest)
473+
static void ip_vs_service_rcu_free(struct rcu_head *head)
475474
{
476-
struct ip_vs_service *svc = dest->svc;
475+
struct ip_vs_service *svc;
476+
477+
svc = container_of(head, struct ip_vs_service, rcu_head);
478+
ip_vs_service_free(svc);
479+
}
477480

478-
dest->svc = NULL;
481+
static void __ip_vs_svc_put(struct ip_vs_service *svc, bool do_delay)
482+
{
479483
if (atomic_dec_and_test(&svc->refcnt)) {
480484
IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
481485
svc->fwmark,
482486
IP_VS_DBG_ADDR(svc->af, &svc->addr),
483487
ntohs(svc->port));
484-
ip_vs_service_free(svc);
488+
if (do_delay)
489+
call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
490+
else
491+
ip_vs_service_free(svc);
485492
}
486493
}
487494

@@ -667,11 +674,6 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
667674
IP_VS_DBG_ADDR(svc->af, &dest->addr),
668675
ntohs(dest->port),
669676
atomic_read(&dest->refcnt));
670-
/* We can not reuse dest while in grace period
671-
* because conns still can use dest->svc
672-
*/
673-
if (test_bit(IP_VS_DEST_STATE_REMOVING, &dest->state))
674-
continue;
675677
if (dest->af == svc->af &&
676678
ip_vs_addr_equal(svc->af, &dest->addr, daddr) &&
677679
dest->port == dport &&
@@ -697,8 +699,10 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
697699

698700
static void ip_vs_dest_free(struct ip_vs_dest *dest)
699701
{
702+
struct ip_vs_service *svc = rcu_dereference_protected(dest->svc, 1);
703+
700704
__ip_vs_dst_cache_reset(dest);
701-
__ip_vs_unbind_svc(dest);
705+
__ip_vs_svc_put(svc, false);
702706
free_percpu(dest->stats.cpustats);
703707
kfree(dest);
704708
}
@@ -771,6 +775,7 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
771775
struct ip_vs_dest_user_kern *udest, int add)
772776
{
773777
struct netns_ipvs *ipvs = net_ipvs(svc->net);
778+
struct ip_vs_service *old_svc;
774779
struct ip_vs_scheduler *sched;
775780
int conn_flags;
776781

@@ -792,13 +797,14 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
792797
atomic_set(&dest->conn_flags, conn_flags);
793798

794799
/* bind the service */
795-
if (!dest->svc) {
800+
old_svc = rcu_dereference_protected(dest->svc, 1);
801+
if (!old_svc) {
796802
__ip_vs_bind_svc(dest, svc);
797803
} else {
798-
if (dest->svc != svc) {
799-
__ip_vs_unbind_svc(dest);
804+
if (old_svc != svc) {
800805
ip_vs_zero_stats(&dest->stats);
801806
__ip_vs_bind_svc(dest, svc);
807+
__ip_vs_svc_put(old_svc, true);
802808
}
803809
}
804810

@@ -998,16 +1004,6 @@ ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
9981004
return 0;
9991005
}
10001006

1001-
static void ip_vs_dest_wait_readers(struct rcu_head *head)
1002-
{
1003-
struct ip_vs_dest *dest = container_of(head, struct ip_vs_dest,
1004-
rcu_head);
1005-
1006-
/* End of grace period after unlinking */
1007-
clear_bit(IP_VS_DEST_STATE_REMOVING, &dest->state);
1008-
}
1009-
1010-
10111007
/*
10121008
* Delete a destination (must be already unlinked from the service)
10131009
*/
@@ -1023,20 +1019,16 @@ static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
10231019
*/
10241020
ip_vs_rs_unhash(dest);
10251021

1026-
if (!cleanup) {
1027-
set_bit(IP_VS_DEST_STATE_REMOVING, &dest->state);
1028-
call_rcu(&dest->rcu_head, ip_vs_dest_wait_readers);
1029-
}
1030-
10311022
spin_lock_bh(&ipvs->dest_trash_lock);
10321023
IP_VS_DBG_BUF(3, "Moving dest %s:%u into trash, dest->refcnt=%d\n",
10331024
IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
10341025
atomic_read(&dest->refcnt));
10351026
if (list_empty(&ipvs->dest_trash) && !cleanup)
10361027
mod_timer(&ipvs->dest_trash_timer,
1037-
jiffies + IP_VS_DEST_TRASH_PERIOD);
1028+
jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
10381029
/* dest lives in trash without reference */
10391030
list_add(&dest->t_list, &ipvs->dest_trash);
1031+
dest->idle_start = 0;
10401032
spin_unlock_bh(&ipvs->dest_trash_lock);
10411033
ip_vs_dest_put(dest);
10421034
}
@@ -1108,24 +1100,30 @@ static void ip_vs_dest_trash_expire(unsigned long data)
11081100
struct net *net = (struct net *) data;
11091101
struct netns_ipvs *ipvs = net_ipvs(net);
11101102
struct ip_vs_dest *dest, *next;
1103+
unsigned long now = jiffies;
11111104

11121105
spin_lock(&ipvs->dest_trash_lock);
11131106
list_for_each_entry_safe(dest, next, &ipvs->dest_trash, t_list) {
1114-
/* Skip if dest is in grace period */
1115-
if (test_bit(IP_VS_DEST_STATE_REMOVING, &dest->state))
1116-
continue;
11171107
if (atomic_read(&dest->refcnt) > 0)
11181108
continue;
1109+
if (dest->idle_start) {
1110+
if (time_before(now, dest->idle_start +
1111+
IP_VS_DEST_TRASH_PERIOD))
1112+
continue;
1113+
} else {
1114+
dest->idle_start = max(1UL, now);
1115+
continue;
1116+
}
11191117
IP_VS_DBG_BUF(3, "Removing destination %u/%s:%u from trash\n",
11201118
dest->vfwmark,
1121-
IP_VS_DBG_ADDR(dest->svc->af, &dest->addr),
1119+
IP_VS_DBG_ADDR(dest->af, &dest->addr),
11221120
ntohs(dest->port));
11231121
list_del(&dest->t_list);
11241122
ip_vs_dest_free(dest);
11251123
}
11261124
if (!list_empty(&ipvs->dest_trash))
11271125
mod_timer(&ipvs->dest_trash_timer,
1128-
jiffies + IP_VS_DEST_TRASH_PERIOD);
1126+
jiffies + (IP_VS_DEST_TRASH_PERIOD >> 1));
11291127
spin_unlock(&ipvs->dest_trash_lock);
11301128
}
11311129

@@ -1320,14 +1318,6 @@ ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
13201318
return ret;
13211319
}
13221320

1323-
static void ip_vs_service_rcu_free(struct rcu_head *head)
1324-
{
1325-
struct ip_vs_service *svc;
1326-
1327-
svc = container_of(head, struct ip_vs_service, rcu_head);
1328-
ip_vs_service_free(svc);
1329-
}
1330-
13311321
/*
13321322
* Delete a service from the service list
13331323
* - The service must be unlinked, unlocked and not referenced!
@@ -1376,13 +1366,7 @@ static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
13761366
/*
13771367
* Free the service if nobody refers to it
13781368
*/
1379-
if (atomic_dec_and_test(&svc->refcnt)) {
1380-
IP_VS_DBG_BUF(3, "Removing service %u/%s:%u\n",
1381-
svc->fwmark,
1382-
IP_VS_DBG_ADDR(svc->af, &svc->addr),
1383-
ntohs(svc->port));
1384-
call_rcu(&svc->rcu_head, ip_vs_service_rcu_free);
1385-
}
1369+
__ip_vs_svc_put(svc, true);
13861370

13871371
/* decrease the module use count */
13881372
ip_vs_use_count_dec();

‎net/netfilter/ipvs/ip_vs_est.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ static void ip_vs_read_cpu_stats(struct ip_vs_stats_user *sum,
5959
struct ip_vs_cpu_stats __percpu *stats)
6060
{
6161
int i;
62+
bool add = false;
6263

6364
for_each_possible_cpu(i) {
6465
struct ip_vs_cpu_stats *s = per_cpu_ptr(stats, i);
6566
unsigned int start;
6667
__u64 inbytes, outbytes;
67-
if (i) {
68+
if (add) {
6869
sum->conns += s->ustats.conns;
6970
sum->inpkts += s->ustats.inpkts;
7071
sum->outpkts += s->ustats.outpkts;
@@ -76,6 +77,7 @@ static void ip_vs_read_cpu_stats(struct ip_vs_stats_user *sum,
7677
sum->inbytes += inbytes;
7778
sum->outbytes += outbytes;
7879
} else {
80+
add = true;
7981
sum->conns = s->ustats.conns;
8082
sum->inpkts = s->ustats.inpkts;
8183
sum->outpkts = s->ustats.outpkts;

0 commit comments

Comments
 (0)