Skip to content

Commit f10bbdd

Browse files
Guillaume Naultgregkh
authored andcommitted
netns: fix GFP flags in rtnl_net_notifyid()
[ Upstream commit d4e4fdf ] In rtnl_net_notifyid(), we certainly can't pass a null GFP flag to rtnl_notify(). A GFP_KERNEL flag would be fine in most circumstances, but there are a few paths calling rtnl_net_notifyid() from atomic context or from RCU critical sections. The later also precludes the use of gfp_any() as it wouldn't detect the RCU case. Also, the nlmsg_new() call is wrong too, as it uses GFP_KERNEL unconditionally. Therefore, we need to pass the GFP flags as parameter and propagate it through function calls until the proper flags can be determined. In most cases, GFP_KERNEL is fine. The exceptions are: * openvswitch: ovs_vport_cmd_get() and ovs_vport_cmd_dump() indirectly call rtnl_net_notifyid() from RCU critical section, * rtnetlink: rtmsg_ifinfo_build_skb() already receives GFP flags as parameter. Also, in ovs_vport_cmd_build_info(), let's change the GFP flags used by nlmsg_new(). The function is allowed to sleep, so better make the flags consistent with the ones used in the following ovs_vport_cmd_fill_info() call. Found by code inspection. Fixes: 9a96345 ("netns: notify netns id events") Signed-off-by: Guillaume Nault <gnault@redhat.com> Acked-by: Nicolas Dichtel <nicolas.dichtel@6wind.com> Acked-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 02ccce8 commit f10bbdd

5 files changed

Lines changed: 29 additions & 26 deletions

File tree

‎include/net/net_namespace.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static inline struct net *read_pnet(const possible_net_t *pnet)
333333
#define __net_initconst __initconst
334334
#endif
335335

336-
int peernet2id_alloc(struct net *net, struct net *peer);
336+
int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
337337
int peernet2id(struct net *net, struct net *peer);
338338
bool peernet_has_id(struct net *net, struct net *peer);
339339
struct net *get_net_ns_by_id(struct net *net, int id);

‎net/core/dev.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9411,7 +9411,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
94119411
call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
94129412
rcu_barrier();
94139413

9414-
new_nsid = peernet2id_alloc(dev_net(dev), net);
9414+
new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
94159415
/* If there is an ifindex conflict assign a new one */
94169416
if (__dev_get_by_index(net, dev->ifindex))
94179417
new_ifindex = dev_new_index(net);

‎net/core/net_namespace.c‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ static int __peernet2id(struct net *net, struct net *peer)
245245
return __peernet2id_alloc(net, peer, &no);
246246
}
247247

248-
static void rtnl_net_notifyid(struct net *net, int cmd, int id);
248+
static void rtnl_net_notifyid(struct net *net, int cmd, int id, gfp_t gfp);
249249
/* This function returns the id of a peer netns. If no id is assigned, one will
250250
* be allocated and returned.
251251
*/
252-
int peernet2id_alloc(struct net *net, struct net *peer)
252+
int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp)
253253
{
254254
bool alloc = false, alive = false;
255255
int id;
@@ -268,7 +268,7 @@ int peernet2id_alloc(struct net *net, struct net *peer)
268268
id = __peernet2id_alloc(net, peer, &alloc);
269269
spin_unlock_bh(&net->nsid_lock);
270270
if (alloc && id >= 0)
271-
rtnl_net_notifyid(net, RTM_NEWNSID, id);
271+
rtnl_net_notifyid(net, RTM_NEWNSID, id, gfp);
272272
if (alive)
273273
put_net(peer);
274274
return id;
@@ -532,7 +532,8 @@ static void unhash_nsid(struct net *net, struct net *last)
532532
idr_remove(&tmp->netns_ids, id);
533533
spin_unlock_bh(&tmp->nsid_lock);
534534
if (id >= 0)
535-
rtnl_net_notifyid(tmp, RTM_DELNSID, id);
535+
rtnl_net_notifyid(tmp, RTM_DELNSID, id,
536+
GFP_KERNEL);
536537
if (tmp == last)
537538
break;
538539
}
@@ -764,7 +765,7 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
764765
err = alloc_netid(net, peer, nsid);
765766
spin_unlock_bh(&net->nsid_lock);
766767
if (err >= 0) {
767-
rtnl_net_notifyid(net, RTM_NEWNSID, err);
768+
rtnl_net_notifyid(net, RTM_NEWNSID, err, GFP_KERNEL);
768769
err = 0;
769770
} else if (err == -ENOSPC && nsid >= 0) {
770771
err = -EEXIST;
@@ -1051,7 +1052,7 @@ static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
10511052
return err < 0 ? err : skb->len;
10521053
}
10531054

1054-
static void rtnl_net_notifyid(struct net *net, int cmd, int id)
1055+
static void rtnl_net_notifyid(struct net *net, int cmd, int id, gfp_t gfp)
10551056
{
10561057
struct net_fill_args fillargs = {
10571058
.cmd = cmd,
@@ -1060,15 +1061,15 @@ static void rtnl_net_notifyid(struct net *net, int cmd, int id)
10601061
struct sk_buff *msg;
10611062
int err = -ENOMEM;
10621063

1063-
msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
1064+
msg = nlmsg_new(rtnl_net_get_size(), gfp);
10641065
if (!msg)
10651066
goto out;
10661067

10671068
err = rtnl_net_fill(msg, &fillargs);
10681069
if (err < 0)
10691070
goto err_out;
10701071

1071-
rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
1072+
rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, gfp);
10721073
return;
10731074

10741075
err_out:

‎net/core/rtnetlink.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,15 +1523,15 @@ static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
15231523

15241524
static int rtnl_fill_link_netnsid(struct sk_buff *skb,
15251525
const struct net_device *dev,
1526-
struct net *src_net)
1526+
struct net *src_net, gfp_t gfp)
15271527
{
15281528
bool put_iflink = false;
15291529

15301530
if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
15311531
struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
15321532

15331533
if (!net_eq(dev_net(dev), link_net)) {
1534-
int id = peernet2id_alloc(src_net, link_net);
1534+
int id = peernet2id_alloc(src_net, link_net, gfp);
15351535

15361536
if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
15371537
return -EMSGSIZE;
@@ -1589,7 +1589,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
15891589
int type, u32 pid, u32 seq, u32 change,
15901590
unsigned int flags, u32 ext_filter_mask,
15911591
u32 event, int *new_nsid, int new_ifindex,
1592-
int tgt_netnsid)
1592+
int tgt_netnsid, gfp_t gfp)
15931593
{
15941594
struct ifinfomsg *ifm;
15951595
struct nlmsghdr *nlh;
@@ -1681,7 +1681,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
16811681
goto nla_put_failure;
16821682
}
16831683

1684-
if (rtnl_fill_link_netnsid(skb, dev, src_net))
1684+
if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
16851685
goto nla_put_failure;
16861686

16871687
if (new_nsid &&
@@ -2001,7 +2001,7 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
20012001
NETLINK_CB(cb->skb).portid,
20022002
nlh->nlmsg_seq, 0, flags,
20032003
ext_filter_mask, 0, NULL, 0,
2004-
netnsid);
2004+
netnsid, GFP_KERNEL);
20052005

20062006
if (err < 0) {
20072007
if (likely(skb->len))
@@ -3359,7 +3359,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
33593359
err = rtnl_fill_ifinfo(nskb, dev, net,
33603360
RTM_NEWLINK, NETLINK_CB(skb).portid,
33613361
nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3362-
0, NULL, 0, netnsid);
3362+
0, NULL, 0, netnsid, GFP_KERNEL);
33633363
if (err < 0) {
33643364
/* -EMSGSIZE implies BUG in if_nlmsg_size */
33653365
WARN_ON(err == -EMSGSIZE);
@@ -3471,7 +3471,7 @@ struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
34713471

34723472
err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
34733473
type, 0, 0, change, 0, 0, event,
3474-
new_nsid, new_ifindex, -1);
3474+
new_nsid, new_ifindex, -1, flags);
34753475
if (err < 0) {
34763476
/* -EMSGSIZE implies BUG in if_nlmsg_size() */
34773477
WARN_ON(err == -EMSGSIZE);

‎net/openvswitch/datapath.c‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ static struct genl_family dp_datapath_genl_family __ro_after_init = {
18501850
/* Called with ovs_mutex or RCU read lock. */
18511851
static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
18521852
struct net *net, u32 portid, u32 seq,
1853-
u32 flags, u8 cmd)
1853+
u32 flags, u8 cmd, gfp_t gfp)
18541854
{
18551855
struct ovs_header *ovs_header;
18561856
struct ovs_vport_stats vport_stats;
@@ -1871,7 +1871,7 @@ static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
18711871
goto nla_put_failure;
18721872

18731873
if (!net_eq(net, dev_net(vport->dev))) {
1874-
int id = peernet2id_alloc(net, dev_net(vport->dev));
1874+
int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
18751875

18761876
if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
18771877
goto nla_put_failure;
@@ -1912,11 +1912,12 @@ struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
19121912
struct sk_buff *skb;
19131913
int retval;
19141914

1915-
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1915+
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19161916
if (!skb)
19171917
return ERR_PTR(-ENOMEM);
19181918

1919-
retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
1919+
retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
1920+
GFP_KERNEL);
19201921
BUG_ON(retval < 0);
19211922

19221923
return skb;
@@ -2058,7 +2059,7 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
20582059

20592060
err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
20602061
info->snd_portid, info->snd_seq, 0,
2061-
OVS_VPORT_CMD_NEW);
2062+
OVS_VPORT_CMD_NEW, GFP_KERNEL);
20622063

20632064
new_headroom = netdev_get_fwd_headroom(vport->dev);
20642065

@@ -2119,7 +2120,7 @@ static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
21192120

21202121
err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
21212122
info->snd_portid, info->snd_seq, 0,
2122-
OVS_VPORT_CMD_SET);
2123+
OVS_VPORT_CMD_SET, GFP_KERNEL);
21232124
BUG_ON(err < 0);
21242125

21252126
ovs_unlock();
@@ -2159,7 +2160,7 @@ static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
21592160

21602161
err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
21612162
info->snd_portid, info->snd_seq, 0,
2162-
OVS_VPORT_CMD_DEL);
2163+
OVS_VPORT_CMD_DEL, GFP_KERNEL);
21632164
BUG_ON(err < 0);
21642165

21652166
/* the vport deletion may trigger dp headroom update */
@@ -2206,7 +2207,7 @@ static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
22062207
goto exit_unlock_free;
22072208
err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
22082209
info->snd_portid, info->snd_seq, 0,
2209-
OVS_VPORT_CMD_GET);
2210+
OVS_VPORT_CMD_GET, GFP_ATOMIC);
22102211
BUG_ON(err < 0);
22112212
rcu_read_unlock();
22122213

@@ -2242,7 +2243,8 @@ static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
22422243
NETLINK_CB(cb->skb).portid,
22432244
cb->nlh->nlmsg_seq,
22442245
NLM_F_MULTI,
2245-
OVS_VPORT_CMD_GET) < 0)
2246+
OVS_VPORT_CMD_GET,
2247+
GFP_ATOMIC) < 0)
22462248
goto out;
22472249

22482250
j++;

0 commit comments

Comments
 (0)