syzbot reported recursive locking with an HSR device whose slave is a bridge, and that bridge has the master of a second HSR device as a port. The first device transmits its supervision frames with its seqnr_lock held; the bridge floods them to the second HSR master, whose hsr_dev_xmit() takes the seqnr_lock of that device: WARNING: possible recursive locking detected ... hsr_dev_xmit+0x18a/0x210 net/hsr/hsr_device.c:234 ... br_dev_xmit+0x1202/0x1b40 ... hsr_forward_skb+0x171c/0x2ac0 net/hsr/hsr_forward.c:728 send_hsr_supervision_frame+0x63b/0xcc0 net/hsr/hsr_device.c:351 hsr_announce+0x1f8/0x3a0 net/hsr/hsr_device.c:408 Interlink RX forwards to the slaves with the same lock held. A chain of two devices does not deadlock, but such a port makes no sense: an HSR ring and the two PRP LANs are attached to the node directly. Refuse bridge and OVS masters and bond and team masters (also without ports, since ports can be added later) as slave or interlink, and a macvlan or MACsec device when an HSR/PRP, bridge, bond or team master is below it; on an ordinary NIC or veth such a device stays valid. This also refuses one on a DSA user port whose conduit is a bond or team, since it can gain ports later too. A macvlan takes its GRO setting from its lower device, so it is accepted only if GRO is off there when it is attached. The lower devices of other ports are not checked, because a DSA user port is an upper of its conduit and can offload HSR. VLAN devices are already refused. The check runs at attach; later changes below a stacked device are not rechecked. Not covered are a tunnel whose route leads back through the HSR master, a tc or BPF redirect to it, other stacked devices, and a software device without an identifying flag such as batman-adv. Narrowing seqnr_lock, planned for net-next, addresses that class as a whole. Also refuse a device whose RX handler is in use or not supported (IFF_NO_RX_HANDLER: ipvlan, VRF), or that already has a master, such as a VRF port, with an extack message and the same error codes. All ports are checked before any of them is changed. Some of these devices, such as a macvlan lower, ipvlan or VRF, used to fail only in netdev_rx_handler_register(), after the previous patch had dropped their GRO request; a refused device now keeps its state, and so do the ports given with it. A bridge can no longer be used directly as the interlink; connect it through a veth pair. Reported-by: syzbot+fbf74291c3b7e753b481@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=fbf74291c3b7e753b481 Fixes: 06afd2c31d33 ("hsr: Synchronize sending frames to have always incremented outgoing seq nr.") Fixes: 430d67bdcb04 ("net: hsr: Use the seqnr lock for frames received via interlink port.") Signed-off-by: Xin Xie --- net/hsr/hsr_device.c | 7 ++++ net/hsr/hsr_slave.c | 79 ++++++++++++++++++++++++++++++++++++++++++-- net/hsr/hsr_slave.h | 2 ++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c index fc512204600c..90ee55eb3c19 100644 --- a/net/hsr/hsr_device.c +++ b/net/hsr/hsr_device.c @@ -758,6 +758,13 @@ int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2], INIT_LIST_HEAD(&hsr->proxy_node_db); spin_lock_init(&hsr->list_lock); + /* Refuse a bad port before any port is changed. The same checks + * in hsr_add_port() remain as a safety net. + */ + res = hsr_check_ports(slave, interlink, extack); + if (res) + return res; + eth_hw_addr_set(hsr_dev, slave[0]->dev_addr); /* initialize protocol specific functions */ diff --git a/net/hsr/hsr_slave.c b/net/hsr/hsr_slave.c index b7765a4bb992..666220b24dee 100644 --- a/net/hsr/hsr_slave.c +++ b/net/hsr/hsr_slave.c @@ -10,6 +10,7 @@ #include "hsr_slave.h" #include #include +#include #include #include #include "hsr_main.h" @@ -97,6 +98,16 @@ bool hsr_port_exists(const struct net_device *dev) return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame; } +/* A lower device that forwards into other ports or into a second + * HSR/PRP device; called under RTNL by the lower-device walk. + */ +static int hsr_lower_forwards(struct net_device *lower, + struct netdev_nested_priv *priv) +{ + return is_hsr_master(lower) || netif_is_any_bridge_master(lower) || + netif_is_lag_master(lower); +} + static int hsr_check_dev_ok(struct net_device *dev, struct netlink_ext_ack *extack) { @@ -120,6 +131,27 @@ static int hsr_check_dev_ok(struct net_device *dev, return -EINVAL; } + /* The RX handler is taken by devices such as bridge/bond/team + * ports, macvlan and ipvlan lowers and HSR ports, or unsupported + * (IFF_NO_RX_HANDLER). Refuse before any state change, so a + * failed attach leaves the device as it was. + */ + if (dev->priv_flags & IFF_NO_RX_HANDLER) { + NL_SET_ERR_MSG_MOD(extack, + "This device does not support an RX handler."); + return -EINVAL; + } + if (netdev_is_rx_handler_busy(dev)) { + NL_SET_ERR_MSG_MOD(extack, + "RX handler busy: device is already a lower of another device."); + return -EBUSY; + } + /* A master without an RX handler on its ports, such as VRF. */ + if (netdev_master_upper_dev_get(dev)) { + NL_SET_ERR_MSG_MOD(extack, "Device already has a master."); + return -EBUSY; + } + if (is_vlan_dev(dev)) { NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver."); return -EINVAL; @@ -131,13 +163,56 @@ static int hsr_check_dev_ok(struct net_device *dev, return -EOPNOTSUPP; } - /* HSR over bonded devices has not been tested, but I'm not sure it - * won't work... + /* Forwarding devices feed HSR frames into another device's + * transmit path and can nest seqnr_lock through a second HSR + * device. A bridge, bond, team or OVS master makes no sense as a + * ring or LAN port and is refused by type. A macvlan or MACsec + * device is refused if such a master or an HSR/PRP master is + * below it. Other devices keep their lower devices unchecked: + * DSA user ports are uppers of their conduit. + */ + if (netif_is_any_bridge_master(dev) || netif_is_lag_master(dev)) { + NL_SET_ERR_MSG_MOD(extack, + "Cannot use a bridge, bond, team or OVS master as HSR port."); + return -EINVAL; + } + if ((netif_is_macvlan(dev) || netif_is_macsec(dev)) && + netdev_walk_all_lower_dev(dev, hsr_lower_forwards, NULL)) { + NL_SET_ERR_MSG_MOD(extack, + "Cannot use a macvlan or MACsec device stacked on an HSR, bridge, bond, team or OVS master as HSR port."); + return -EINVAL; + } + /* A macvlan takes its GRO setting from the lower device, so the + * member role cannot turn it off. Refuse at attach, before any + * state change, if the lower device runs GRO; it can still turn + * GRO on later. */ + if (netif_is_macvlan(dev) && + (macvlan_dev_real_dev(dev)->features & NETIF_F_GRO)) { + NL_SET_ERR_MSG_MOD(extack, + "macvlan inherits GRO from its lower device; turn GRO off on the lower device."); + return -EBUSY; + } return 0; } +/* Check every port before any of them is changed, so that a refused + * port does not leave an earlier one modified. + */ +int hsr_check_ports(struct net_device *slave[2], struct net_device *interlink, + struct netlink_ext_ack *extack) +{ + int res; + + res = hsr_check_dev_ok(slave[0], extack); + if (!res) + res = hsr_check_dev_ok(slave[1], extack); + if (!res && interlink) + res = hsr_check_dev_ok(interlink, extack); + return res; +} + /* Member role and feature policy. * Setting the role makes __netdev_update_features() clear GRO * and configurable GRO_HW from the request and before diff --git a/net/hsr/hsr_slave.h b/net/hsr/hsr_slave.h index edc4612bb009..e19c779b500d 100644 --- a/net/hsr/hsr_slave.h +++ b/net/hsr/hsr_slave.h @@ -14,6 +14,8 @@ #include #include "hsr_main.h" +int hsr_check_ports(struct net_device *slave[2], struct net_device *interlink, + struct netlink_ext_ack *extack); int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev, enum hsr_port_type pt, struct netlink_ext_ack *extack); void hsr_del_port(struct hsr_port *port); -- 2.53.0