Message ID | 20241129063112.763095-1-xiyou.wangcong@gmail.com (mailing list archive) |
---|---|
State | New |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | [net] rtnetlink: catch error pointer for rtnl_link_get_net() | expand |
From: Cong Wang <xiyou.wangcong@gmail.com> Date: Thu, 28 Nov 2024 22:31:12 -0800 > From: Cong Wang <cong.wang@bytedance.com> > > Currently all callers of rtnl_link_get_net() assume that it always > returns a valid netns pointer, because I assume it's always tested in rtnl_add_peer_net()... > when rtnl_link_get_net_ifla() fails, > it uses 'src_net' as a fallback. > > This is not true, because rtnl_link_get_net_ifla() isn't called if (!data || !data[ops->peer_type]), so the correct fix is: ---8<--- diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index dd142f444659..c1f4aaa40823 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -3815,6 +3815,10 @@ static int rtnl_add_peer_net(struct rtnl_nets *rtnl_nets, struct net *net; int err; + net = rtnl_link_get_net_ifla(tb); + if (IS_ERR(net)) + return PTR_ERR(net); + if (!data || !data[ops->peer_type]) return 0; @@ -3828,9 +3832,6 @@ static int rtnl_add_peer_net(struct rtnl_nets *rtnl_nets, return err; } - net = rtnl_link_get_net_ifla(tb); - if (IS_ERR(net)) - return PTR_ERR(net); if (net) rtnl_nets_add(rtnl_nets, net); ---8<--- > because rtnl_link_get_net_ifla() can return an > error pointer too, we need to handle this error case and propagate > the error code to its callers. > > Add a comment to better document its return value.
diff --git a/drivers/net/can/vxcan.c b/drivers/net/can/vxcan.c index da7c72105fb6..6d03a5314034 100644 --- a/drivers/net/can/vxcan.c +++ b/drivers/net/can/vxcan.c @@ -204,6 +204,9 @@ static int vxcan_newlink(struct net *net, struct net_device *dev, } peer_net = rtnl_link_get_net(net, tbp); + if (IS_ERR(peer_net)) + return PTR_ERR(peer_net); + peer = rtnl_create_link(peer_net, ifname, name_assign_type, &vxcan_link_ops, tbp, extack); if (IS_ERR(peer)) { diff --git a/drivers/net/netkit.c b/drivers/net/netkit.c index bb07725d1c72..44fe99a82ac3 100644 --- a/drivers/net/netkit.c +++ b/drivers/net/netkit.c @@ -386,6 +386,9 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev, return -EOPNOTSUPP; net = rtnl_link_get_net(src_net, tbp); + if (IS_ERR(net)) + return PTR_ERR(net); + peer = rtnl_create_link(net, ifname, ifname_assign_type, &netkit_link_ops, tbp, extack); if (IS_ERR(peer)) { diff --git a/drivers/net/veth.c b/drivers/net/veth.c index 0d6d0d749d44..3a42a982c638 100644 --- a/drivers/net/veth.c +++ b/drivers/net/veth.c @@ -1801,6 +1801,9 @@ static int veth_newlink(struct net *src_net, struct net_device *dev, } net = rtnl_link_get_net(src_net, tbp); + if (IS_ERR(net)) + return PTR_ERR(net); + peer = rtnl_create_link(net, ifname, name_assign_type, &veth_link_ops, tbp, extack); if (IS_ERR(peer)) { diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index dd142f444659..6a4363276117 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -2527,6 +2527,18 @@ static struct net *rtnl_link_get_net_ifla(struct nlattr *tb[]) return net; } +/** + * rtnl_link_get_net - Get the network namespace from the netlink attributes + * or just @src_net. + * + * @src_net: the source network namespace + * @tb: the netlink attributes + * + * Returns: + * The network namespace specified in the netlink attributes, + * in case of error, an error pointer is returned. + * Or, @src_net if no netns attributes were passed. + */ struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) { struct net *net = rtnl_link_get_net_ifla(tb);