Skip to content

Commit 21ee543

Browse files
mkubecekdavem330
authored andcommitted
xfrm: fix race between netns cleanup and state expire notification
The xfrm_user module registers its pernet init/exit after xfrm itself so that its net exit function xfrm_user_net_exit() is executed before xfrm_net_exit() which calls xfrm_state_fini() to cleanup the SA's (xfrm states). This opens a window between zeroing net->xfrm.nlsk pointer and deleting all xfrm_state instances which may access it (via the timer). If an xfrm state expires in this window, xfrm_exp_state_notify() will pass null pointer as socket to nlmsg_multicast(). As the notifications are called inside rcu_read_lock() block, it is sufficient to retrieve the nlsk socket with rcu_dereference() and check the it for null. Signed-off-by: Michal Kubecek <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1299b3c commit 21ee543

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

net/xfrm/xfrm_user.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,20 @@ static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
955955
return skb;
956956
}
957957

958+
/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
959+
* Must be called with RCU read lock.
960+
*/
961+
static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
962+
u32 pid, unsigned int group)
963+
{
964+
struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
965+
966+
if (nlsk)
967+
return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
968+
else
969+
return -1;
970+
}
971+
958972
static inline size_t xfrm_spdinfo_msgsize(void)
959973
{
960974
return NLMSG_ALIGN(4)
@@ -2265,7 +2279,7 @@ static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
22652279
if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
22662280
BUG();
22672281

2268-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2282+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
22692283
}
22702284
#else
22712285
static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
@@ -2456,7 +2470,7 @@ static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
24562470
return -EMSGSIZE;
24572471
}
24582472

2459-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2473+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
24602474
}
24612475

24622476
static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
@@ -2471,7 +2485,7 @@ static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event
24712485
if (build_aevent(skb, x, c) < 0)
24722486
BUG();
24732487

2474-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2488+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
24752489
}
24762490

24772491
static int xfrm_notify_sa_flush(const struct km_event *c)
@@ -2497,7 +2511,7 @@ static int xfrm_notify_sa_flush(const struct km_event *c)
24972511

24982512
nlmsg_end(skb, nlh);
24992513

2500-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2514+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
25012515
}
25022516

25032517
static inline size_t xfrm_sa_len(struct xfrm_state *x)
@@ -2584,7 +2598,7 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
25842598

25852599
nlmsg_end(skb, nlh);
25862600

2587-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2601+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
25882602

25892603
out_free_skb:
25902604
kfree_skb(skb);
@@ -2675,7 +2689,7 @@ static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
26752689
if (build_acquire(skb, x, xt, xp) < 0)
26762690
BUG();
26772691

2678-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2692+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
26792693
}
26802694

26812695
/* User gives us xfrm_user_policy_info followed by an array of 0
@@ -2789,7 +2803,7 @@ static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct
27892803
if (build_polexpire(skb, xp, dir, c) < 0)
27902804
BUG();
27912805

2792-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2806+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
27932807
}
27942808

27952809
static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
@@ -2851,7 +2865,7 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_e
28512865

28522866
nlmsg_end(skb, nlh);
28532867

2854-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2868+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
28552869

28562870
out_free_skb:
28572871
kfree_skb(skb);
@@ -2879,7 +2893,7 @@ static int xfrm_notify_policy_flush(const struct km_event *c)
28792893

28802894
nlmsg_end(skb, nlh);
28812895

2882-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2896+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
28832897

28842898
out_free_skb:
28852899
kfree_skb(skb);
@@ -2948,7 +2962,7 @@ static int xfrm_send_report(struct net *net, u8 proto,
29482962
if (build_report(skb, proto, sel, addr) < 0)
29492963
BUG();
29502964

2951-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2965+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
29522966
}
29532967

29542968
static inline size_t xfrm_mapping_msgsize(void)
@@ -3000,7 +3014,7 @@ static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
30003014
if (build_mapping(skb, x, ipaddr, sport) < 0)
30013015
BUG();
30023016

3003-
return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
3017+
return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
30043018
}
30053019

30063020
static bool xfrm_is_alive(const struct km_event *c)

0 commit comments

Comments
 (0)