xfrm interface: bug fix on changelink
Subsystems (3)
cover xfrm interface: bug fix on changelink
Here are two bug fix seen by code review. The first one avoids a corruption of existing xfrm interfaces and the second is a minor fix of an error message.
include/net/xfrm.h | 1 - net/xfrm/xfrm_interface.c | 20 ++++++-------------- 2 files changed, 6 insertions(+), 15 deletions(-)
Regards,
Nicolas
PATCH 1/2 xfrm interface: avoid corruption on changelink
The new parameters must not be stored in the netdev_priv() before validation, it may corrupt the interface. Note also that if data is NULL, only a memset() is done.
$ ip link add xfrm1 type xfrm dev lo if_id 1
$ ip link add xfrm2 type xfrm dev lo if_id 2
$ ip link set xfrm1 type xfrm dev lo if_id 2
RTNETLINK answers: File exists
$ ip -d link list dev xfrm1
5: xfrm1@lo: <NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/none 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 minmtu 68 maxmtu 1500
xfrm if_id 0x2 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535=> "if_id 0x2"
--- net/xfrm/xfrm_interface.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/xfrm/xfrm_interface.c +5 -5
@@ -671,12 +671,12 @@ static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],struct nlattr *data[], struct netlink_ext_ack *extack) { - struct xfrm_if *xi = netdev_priv(dev); struct net *net = dev_net(dev); +struct xfrm_if_parms p; +struct xfrm_if *xi; -xfrmi_netlink_parms (data, &xi->p); - - xi =xfrmi_locate (net, &xi->p); +xfrmi_netlink_parms (data, &p); + xi =xfrmi_locate (net, &p);if (!xi) { xi =netdev_priv (dev); }else {
@@ -684,7 +684,7 @@ static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],return -EEXIST ; } -return xfrmi_update (xi, &xi->p); +return xfrmi_update (xi, &p); }static size_t xfrmi_get_size (const struct net_device *dev)
Signature · 1 line
2.21.0
PATCH 2/2 xfrm interface: ifname may be wrong in logs
In reply to Nicolas DichtelThe ifname is copied when the interface is created, but is never updated later. In fact, this property is used only in one error message, where the netdevice pointer is available, thus let's use it.
--- include/net/xfrm.h | 1 - net/xfrm/xfrm_interface.c | 10 +--------- 2 files changed, 1 insertion(+), 10 deletions(-)
include/net/xfrm.h +0 -1
@@ -988,7 +988,6 @@ static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)void xfrm_dst_ifdown (struct dst_entry *dst,struct net_device *dev);struct xfrm_if_parms { -char name[IFNAMSIZ ];/* name of XFRM device */ int link;/* ifindex of underlying L2 interface */ u32 if_id;/* interface identifyer */ };
net/xfrm/xfrm_interface.c +1 -9
@@ -145,8 +145,6 @@ static int xfrmi_create(struct net_device *dev)if (err <0 ) goto out; -strcpy (xi->p.name, dev->name); -dev_hold (dev);xfrmi_link (xfrmn, xi);
@@ -294,7 +292,7 @@ xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)if (tdev == dev) { stats->collisions++;net_warn_ratelimited ("%s: Local routing loop detected!\n" , - xi->p.name); + dev->name); goto tx_err_dst_release; }
@@ -638,12 +636,6 @@ static int xfrmi_newlink(struct net *src_net, struct net_device *dev,int err;xfrmi_netlink_parms (data, &p); - -if (!tb[IFLA_IFNAME ]) -return -EINVAL ; - -nla_strlcpy (p.name, tb[IFLA_IFNAME ],IFNAMSIZ ); - xi =xfrmi_locate (net, &p);if (xi)return -EEXIST ;
Signature · 1 line
2.21.0
Le 10/07/2019 à 09:45, Nicolas Dichtel a écrit :
Quoted 7 lines
Here are two bug fix seen by code review. The first one avoids a corruption of existing xfrm interfaces and the second is a minor fix of an error message.
include/net/xfrm.h | 1 - net/xfrm/xfrm_interface.c | 20 ++++++-------------- 2 files changed, 6 insertions(+), 15 deletions(-)
Please, drop this series, I will resend it.
Regards,
Nicolas
v2 cover xfrm interface: bugs fixes
Here is a bunch of bugs fixes. Some have been seen by code review and some when playing with x-netns.
The details are in each patch.
v1 -> v2:
- add patch #3 and #4
include/net/xfrm.h | 2 -- net/xfrm/xfrm_interface.c | 56 +++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 33 deletions(-)
Regards,
Nicolas
PATCH v2 1/4 xfrm interface: avoid corruption on changelink
The new parameters must not be stored in the netdev_priv() before validation, it may corrupt the interface. Note also that if data is NULL, only a memset() is done.
$ ip link add xfrm1 type xfrm dev lo if_id 1
$ ip link add xfrm2 type xfrm dev lo if_id 2
$ ip link set xfrm1 type xfrm dev lo if_id 2
RTNETLINK answers: File exists
$ ip -d link list dev xfrm1
5: xfrm1@lo: <NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/none 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 minmtu 68 maxmtu 1500
xfrm if_id 0x2 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535=> "if_id 0x2"
--- net/xfrm/xfrm_interface.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/xfrm/xfrm_interface.c +5 -5
@@ -671,12 +671,12 @@ static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],struct nlattr *data[], struct netlink_ext_ack *extack) { - struct xfrm_if *xi = netdev_priv(dev); struct net *net = dev_net(dev); +struct xfrm_if_parms p; +struct xfrm_if *xi; -xfrmi_netlink_parms (data, &xi->p); - - xi =xfrmi_locate (net, &xi->p); +xfrmi_netlink_parms (data, &p); + xi =xfrmi_locate (net, &p);if (!xi) { xi =netdev_priv (dev); }else {
@@ -684,7 +684,7 @@ static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],return -EEXIST ; } -return xfrmi_update (xi, &xi->p); +return xfrmi_update (xi, &p); }static size_t xfrmi_get_size (const struct net_device *dev)
Signature · 1 line
2.21.0
PATCH v2 2/4 xfrm interface: ifname may be wrong in logs
In reply to Nicolas DichtelThe ifname is copied when the interface is created, but is never updated later. In fact, this property is used only in one error message, where the netdevice pointer is available, thus let's use it.
--- include/net/xfrm.h | 1 - net/xfrm/xfrm_interface.c | 10 +--------- 2 files changed, 1 insertion(+), 10 deletions(-)
include/net/xfrm.h +0 -1
@@ -983,7 +983,6 @@ static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)void xfrm_dst_ifdown (struct dst_entry *dst,struct net_device *dev);struct xfrm_if_parms { -char name[IFNAMSIZ ];/* name of XFRM device */ int link;/* ifindex of underlying L2 interface */ u32 if_id;/* interface identifyer */ };
net/xfrm/xfrm_interface.c +1 -9
@@ -145,8 +145,6 @@ static int xfrmi_create(struct net_device *dev)if (err <0 ) goto out; -strcpy (xi->p.name, dev->name); -dev_hold (dev);xfrmi_link (xfrmn, xi);
@@ -294,7 +292,7 @@ xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)if (tdev == dev) { stats->collisions++;net_warn_ratelimited ("%s: Local routing loop detected!\n" , - xi->p.name); + dev->name); goto tx_err_dst_release; }
@@ -638,12 +636,6 @@ static int xfrmi_newlink(struct net *src_net, struct net_device *dev,int err;xfrmi_netlink_parms (data, &p); - -if (!tb[IFLA_IFNAME ]) -return -EINVAL ; - -nla_strlcpy (p.name, tb[IFLA_IFNAME ],IFNAMSIZ ); - xi =xfrmi_locate (net, &p);if (xi)return -EEXIST ;
Signature · 1 line
2.21.0
PATCH v2 3/4 xfrm interface: fix list corruption for x-netns
In reply to Nicolas Dichteldev_net(dev) is the netns of the device and xi->net is the link netns, where the device has been linked.changelink() must operate in the link netns to avoid a corruption of the xfrm lists.
Note that xi->net and dev_net(xi->physdev) are always the same.
Before the patch, the xfrmi lists may be corrupted and can later trigger a kernel panic.
--- net/xfrm/xfrm_interface.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/xfrm/xfrm_interface.c +4 -4
@@ -503,7 +503,7 @@ static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)static int xfrmi_update (struct xfrm_if *xi,struct xfrm_if_parms *p) { -struct net *net =dev_net (xi->dev); +struct net *net = xi->net;struct xfrmi_net *xfrmn =net_generic (net, xfrmi_net_id);int err;
@@ -663,9 +663,9 @@ static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],struct nlattr *data[], struct netlink_ext_ack *extack) { - struct net *net = dev_net(dev); + struct xfrm_if *xi = netdev_priv(dev); +struct net *net = xi->net;struct xfrm_if_parms p; -struct xfrm_if *xi;xfrmi_netlink_parms (data, &p); xi =xfrmi_locate (net, &p);
@@ -707,7 +707,7 @@ static struct net *xfrmi_get_link_net(const struct net_device *dev) {struct xfrm_if *xi =netdev_priv (dev); -return dev_net (xi->phydev); +return xi->net; }static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX +1 ] = {
Signature · 1 line
2.21.0
PATCH v2 4/4 xfrm interface: fix management of phydev
In reply to Nicolas DichtelWith the current implementation, phydev cannot be removed:
$ ip link add dummy type dummy $ ip link add xfrm1 type xfrm dev dummy if_id 1 $ ip l d dummy kernel:[77938.465445] unregister_netdevice: waiting for dummy to become free. Usage count = 1
Manage it like in ip tunnels, ie just keep the ifindex. Not that the side effect, is that the phydev is now optional.
--- include/net/xfrm.h | 1 - net/xfrm/xfrm_interface.c | 32 +++++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-)
include/net/xfrm.h +0 -1
@@ -990,7 +990,6 @@ struct xfrm_if_parms {struct xfrm_if {struct xfrm_if __rcu *next;/* next interface in list */ struct net_device *dev;/* virtual device associated with interface */ -struct net_device *phydev;/* physical device */ struct net *net;/* netns for packet i/o */ struct xfrm_if_parms p;/* interface parms */
net/xfrm/xfrm_interface.c +17 -15
@@ -175,7 +175,6 @@ static void xfrmi_dev_uninit(struct net_device *dev)struct xfrmi_net *xfrmn =net_generic (xi->net, xfrmi_net_id);xfrmi_unlink (xfrmn, xi); -dev_put (xi->phydev);dev_put (dev); }
@@ -362,7 +361,7 @@ static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev) goto tx_err; } - fl.flowi_oif = xi->phydev->ifindex; + fl.flowi_oif = xi->p.link; ret =xfrmi_xmit2 (skb, dev, &fl);if (ret <0 )
@@ -548,7 +547,7 @@ static int xfrmi_get_iflink(const struct net_device *dev) {struct xfrm_if *xi =netdev_priv (dev); -return xi->phydev->ifindex; +return xi->p.link; }
@@ -574,12 +573,14 @@ static void xfrmi_dev_setup(struct net_device *dev) dev->needs_free_netdev = true; dev->priv_destructor = xfrmi_dev_free;netif_keep_dst (dev); + +eth_broadcast_addr (dev->broadcast); }static int xfrmi_dev_init (struct net_device *dev) {struct xfrm_if *xi =netdev_priv (dev); -struct net_device *phydev = xi->phydev; +struct net_device *phydev =__dev_get_by_index (xi->net, xi->p.link);int err; dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
@@ -594,13 +595,19 @@ static int xfrmi_dev_init(struct net_device *dev) dev->features |=NETIF_F_LLTX ; - dev->needed_headroom = phydev->needed_headroom; - dev->needed_tailroom = phydev->needed_tailroom; +if (phydev) { + dev->needed_headroom = phydev->needed_headroom; + dev->needed_tailroom = phydev->needed_tailroom; -if (is_zero_ether_addr (dev->dev_addr)) -eth_hw_addr_inherit (dev, phydev); -if (is_zero_ether_addr (dev->broadcast)) -memcpy (dev->broadcast, phydev->broadcast, dev->addr_len); +if (is_zero_ether_addr (dev->dev_addr)) +eth_hw_addr_inherit (dev, phydev); +if (is_zero_ether_addr (dev->broadcast)) +memcpy (dev->broadcast, phydev->broadcast, + dev->addr_len); + }else { +eth_hw_addr_random (dev); +eth_broadcast_addr (dev->broadcast); + }return 0 ; }
@@ -644,13 +651,8 @@ static int xfrmi_newlink(struct net *src_net, struct net_device *dev, xi->p = p; xi->net = net; xi->dev = dev; - xi->phydev =dev_get_by_index (net, p.link); -if (!xi->phydev) -return -ENODEV ; err =xfrmi_create (dev); -if (err <0 ) -dev_put (xi->phydev);return err; }
Signature · 1 line
2.21.0
Steffen Klassert
maintainerRe: [PATCH ipsec v2 0/4] xfrm interface: bugs fixes
In reply to Nicolas DichtelQuoted 7 lines · Nicolas Dichtel
Here is a bunch of bugs fixes. Some have been seen by code review and some when playing with x-netns.
The details are in each patch.v1 -> v2:
- add patch #3 and #4
Series applied, thanks Nicolas!