Skip to content

Commit 1d6119b

Browse files
edumazetdavem330
authored andcommitted
net: fix percpu memory leaks
This patch fixes following problems : 1) percpu_counter_init() can return an error, therefore init_frag_mem_limit() must propagate this error so that inet_frags_init_net() can do the same up to its callers. 2) If ip[46]_frags_ns_ctl_register() fail, we must unwind properly and free the percpu_counter. Without this fix, we leave freed object in percpu_counters global list (if CONFIG_HOTPLUG_CPU) leading to crashes. This bug was detected by KASAN and syzkaller tool (http://github.com/google/syzkaller) Fixes: 6d7b857 ("net: use lib/percpu_counter API for fragmentation mem accounting") Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Dmitry Vyukov <dvyukov@google.com> Cc: Hannes Frederic Sowa <hannes@stressinduktion.org> Cc: Jesper Dangaard Brouer <brouer@redhat.com> Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c451113 commit 1d6119b

File tree

6 files changed

+44
-24
lines changed

6 files changed

+44
-24
lines changed

include/net/inet_frag.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ struct inet_frags {
108108
int inet_frags_init(struct inet_frags *);
109109
void inet_frags_fini(struct inet_frags *);
110110

111-
void inet_frags_init_net(struct netns_frags *nf);
111+
static inline int inet_frags_init_net(struct netns_frags *nf)
112+
{
113+
return percpu_counter_init(&nf->mem, 0, GFP_KERNEL);
114+
}
115+
static inline void inet_frags_uninit_net(struct netns_frags *nf)
116+
{
117+
percpu_counter_destroy(&nf->mem);
118+
}
119+
112120
void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
113121

114122
void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
@@ -154,11 +162,6 @@ static inline void add_frag_mem_limit(struct netns_frags *nf, int i)
154162
__percpu_counter_add(&nf->mem, i, frag_percpu_counter_batch);
155163
}
156164

157-
static inline void init_frag_mem_limit(struct netns_frags *nf)
158-
{
159-
percpu_counter_init(&nf->mem, 0, GFP_KERNEL);
160-
}
161-
162165
static inline unsigned int sum_frag_mem_limit(struct netns_frags *nf)
163166
{
164167
unsigned int res;

net/ieee802154/6lowpan/reassembly.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,19 @@ static int __net_init lowpan_frags_init_net(struct net *net)
580580
{
581581
struct netns_ieee802154_lowpan *ieee802154_lowpan =
582582
net_ieee802154_lowpan(net);
583+
int res;
583584

584585
ieee802154_lowpan->frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
585586
ieee802154_lowpan->frags.low_thresh = IPV6_FRAG_LOW_THRESH;
586587
ieee802154_lowpan->frags.timeout = IPV6_FRAG_TIMEOUT;
587588

588-
inet_frags_init_net(&ieee802154_lowpan->frags);
589-
590-
return lowpan_frags_ns_sysctl_register(net);
589+
res = inet_frags_init_net(&ieee802154_lowpan->frags);
590+
if (res)
591+
return res;
592+
res = lowpan_frags_ns_sysctl_register(net);
593+
if (res)
594+
inet_frags_uninit_net(&ieee802154_lowpan->frags);
595+
return res;
591596
}
592597

593598
static void __net_exit lowpan_frags_exit_net(struct net *net)

net/ipv4/inet_fragment.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,6 @@ int inet_frags_init(struct inet_frags *f)
209209
}
210210
EXPORT_SYMBOL(inet_frags_init);
211211

212-
void inet_frags_init_net(struct netns_frags *nf)
213-
{
214-
init_frag_mem_limit(nf);
215-
}
216-
EXPORT_SYMBOL(inet_frags_init_net);
217-
218212
void inet_frags_fini(struct inet_frags *f)
219213
{
220214
cancel_work_sync(&f->frags_work);

net/ipv4/ip_fragment.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,8 @@ static void __init ip4_frags_ctl_register(void)
839839

840840
static int __net_init ipv4_frags_init_net(struct net *net)
841841
{
842+
int res;
843+
842844
/* Fragment cache limits.
843845
*
844846
* The fragment memory accounting code, (tries to) account for
@@ -862,9 +864,13 @@ static int __net_init ipv4_frags_init_net(struct net *net)
862864
*/
863865
net->ipv4.frags.timeout = IP_FRAG_TIME;
864866

865-
inet_frags_init_net(&net->ipv4.frags);
866-
867-
return ip4_frags_ns_ctl_register(net);
867+
res = inet_frags_init_net(&net->ipv4.frags);
868+
if (res)
869+
return res;
870+
res = ip4_frags_ns_ctl_register(net);
871+
if (res)
872+
inet_frags_uninit_net(&net->ipv4.frags);
873+
return res;
868874
}
869875

870876
static void __net_exit ipv4_frags_exit_net(struct net *net)

net/ipv6/netfilter/nf_conntrack_reasm.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,18 @@ EXPORT_SYMBOL_GPL(nf_ct_frag6_consume_orig);
648648

649649
static int nf_ct_net_init(struct net *net)
650650
{
651+
int res;
652+
651653
net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
652654
net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
653655
net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
654-
inet_frags_init_net(&net->nf_frag.frags);
655-
656-
return nf_ct_frag6_sysctl_register(net);
656+
res = inet_frags_init_net(&net->nf_frag.frags);
657+
if (res)
658+
return res;
659+
res = nf_ct_frag6_sysctl_register(net);
660+
if (res)
661+
inet_frags_uninit_net(&net->nf_frag.frags);
662+
return res;
657663
}
658664

659665
static void nf_ct_net_exit(struct net *net)

net/ipv6/reassembly.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,19 @@ static void ip6_frags_sysctl_unregister(void)
706706

707707
static int __net_init ipv6_frags_init_net(struct net *net)
708708
{
709+
int res;
710+
709711
net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
710712
net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
711713
net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
712714

713-
inet_frags_init_net(&net->ipv6.frags);
714-
715-
return ip6_frags_ns_sysctl_register(net);
715+
res = inet_frags_init_net(&net->ipv6.frags);
716+
if (res)
717+
return res;
718+
res = ip6_frags_ns_sysctl_register(net);
719+
if (res)
720+
inet_frags_uninit_net(&net->ipv6.frags);
721+
return res;
716722
}
717723

718724
static void __net_exit ipv6_frags_exit_net(struct net *net)

0 commit comments

Comments
 (0)