From: Hangbin Liu <redacted>
bond_enslave() currently runs the fallible XDP setup
(dev_xdp_propagate()) after the slave has already been published:
slave_cnt is incremented, ns maddrs are added and the active slave
may have been selected. If XDP setup then fails, the error path
jumps to err_sysfs_del and leaks:
- the promiscuity/allmulti increments and synced addresses applied
to the slave earlier in the function, which are only undone on
an immediate allmulti failure,
- the ns maddrs added just before the failure, which no enslave
error path ever removes.
Move dev_xdp_propagate() to the last fallible step, right after the
promiscuity/allmulti/address-sync block and before
WRITE_ONCE(bond->slave_cnt), so that no handled fallible call
follows the publication of the slave and a failure can no longer
leak slave_cnt or ns maddrs.
bond_xdp_set_features() is moved along with this reordering so XDP
features are updated before the slave is published. Route the XDP
failure through a new err_set_xdp label that undoes the promiscuity,
allmulti and address-sync state, mirroring what __bond_release_one()
does.
Signed-off-by: Hangbin Liu <redacted>
---
drivers/net/bonding/bond_main.c | 52 +++++++++++++++++++++++++----------------
1 file changed, 32 insertions(+), 20 deletions(-)
diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 340af4c3fa20..80b6c18ee9c2 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -2313,22 +2313,11 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
}
}
- WRITE_ONCE(bond->slave_cnt, bond->slave_cnt + 1);
- netdev_compute_master_upper_features(bond->dev, true);
- bond_set_carrier(bond);
-
- /* Needs to be called before bond_select_active_slave(), which will
- * remove the maddrs if the slave is selected as active slave.
+ /* XDP capability and prog-count were already validated above.
+ * Setup runs as the last fallible step before the slave is
+ * published, so no undo of this block can ever be needed and
+ * a failure here cannot leak slave_cnt or ns maddrs.
*/
- bond_slave_ns_maddrs_add(bond, new_slave);
-
- if (bond_uses_primary(bond)) {
- block_netpoll_tx();
- bond_select_active_slave(bond);
- unblock_netpoll_tx();
- }
-
- /* XDP capability and prog-count were already validated above */
if (bond->xdp_prog) {
struct netdev_bpf xdp = {
.command = XDP_SETUP_PROG,@@ -2341,19 +2330,35 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
if (res < 0) {
/* ndo_bpf() sets extack error message */
slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
- goto err_dec_slave_cnt;
+ goto err_set_xdp;
}
if (bond->xdp_prog)
bpf_prog_inc(bond->xdp_prog);
}
+ bond_xdp_set_features(bond_dev);
+
+ /* Must called before bond_update_slave_arr() or bond_select_active_slave() */
+ WRITE_ONCE(bond->slave_cnt, bond->slave_cnt + 1);
+ netdev_compute_master_upper_features(bond->dev, true);
+ bond_set_carrier(bond);
+
+ /* Needs to be called before bond_select_active_slave(), which will
+ * remove the maddrs if the slave is selected as active slave.
+ */
+ bond_slave_ns_maddrs_add(bond, new_slave);
+
+ if (bond_uses_primary(bond)) {
+ block_netpoll_tx();
+ bond_select_active_slave(bond);
+ unblock_netpoll_tx();
+ }
+
/* broadcast mode uses the all_slaves to loop through slaves. */
if (bond_mode_can_use_xmit_hash(bond) ||
BOND_MODE(bond) == BOND_MODE_BROADCAST)
bond_update_slave_arr(bond, NULL);
- bond_xdp_set_features(bond_dev);
-
slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
bond_is_active_slave(new_slave) ? "an active" : "a backup",
new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");@@ -2363,8 +2368,15 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
return 0;
/* Undo stages on error */
-err_dec_slave_cnt:
- WRITE_ONCE(bond->slave_cnt, bond->slave_cnt - 1);
+err_set_xdp:
+ if (!bond_uses_primary(bond)) {
+ if (bond_dev->flags & IFF_PROMISC)
+ dev_set_promiscuity(slave_dev, -1);
+ if (bond_dev->flags & IFF_ALLMULTI)
+ dev_set_allmulti(slave_dev, -1);
+ if (bond_dev->flags & IFF_UP)
+ bond_hw_addr_flush(bond_dev, slave_dev);
+ }
err_sysfs_del:
bond_sysfs_slave_del(new_slave);
--
2.56.0