From: Lin Jiapeng The 802.3ad enslave path binds a slave's port before the slave is published in the bond's lower list. The periodic state machine can select the port and attach it to a LAG while enslavement is still in progress, including while NETDEV_CHANGEUPPER notifiers are running. If enslavement fails, rollback can free ad_slave_info without unbinding its port and leave later LAG processing with a dangling pointer. This affects both a failed upper-link notification and later failures such as bond_sysfs_slave_add(). A test-only kretprobe forces the latter to return -ENOMEM, after which KASAN reports: BUG: KASAN: slab-use-after-free in ad_agg_selection_logic+0x1a0d/0x1fa0 Read of size 1 by task kworker/u16:0/13 Freed by task 110: kfree slave_kobj_release kobject_put bond_enslave Bind the port only after bond_master_upper_dev_link() has succeeded, so an upper-link failure never binds anything and cannot race with the state machine on a port the notifier rollback has already unlisted. On failures after publication, drain the RX handler, unbind while the slave is still listed, then unlink, re-evaluate the carrier the way __bond_release_one() does, and rebuild the slave array after aggregator changes. Earlier unpublished failures keep their previous behaviour. Fixes: 07699f9a7c8d ("bonding: add sysfs /slave dir for bond slave devices.") Reported-by: TencentOS Corvus AI Signed-off-by: Lin Jiapeng Link: https://lore.kernel.org/netdev/20261124024319.52145-1-ljp1205831794@gmail.com/ --- v2 -> v3: - Bind the 802.3ad port only after the upper link has been established, so an upper-link failure has nothing to unbind, cannot race with the state machine on an unlisted port, and no longer sends a spurious LACPDU on pre-adjacency failures. - Re-evaluate the carrier with bond_set_carrier() in the rollback, the same way __bond_release_one() does. - Collapse the error unwind into a single err_unregister block gated by upper_linked; err_upper_unlink is gone (Nikolay Aleksandrov). - Add Signed-off-by. drivers/net/bonding/bond_main.c | 46 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c index e9f5e753f677..c0c6fbcc8745 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -1892,6 +1892,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, const struct net_device_ops *slave_ops = slave_dev->netdev_ops; struct slave *new_slave = NULL, *prev_slave; struct sockaddr_storage ss; + bool upper_linked = false; int res = 0, i; if (slave_dev->type == ARPHRD_CAN) { @@ -2182,19 +2183,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, * aggregator */ bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); - /* if this is the first slave */ - if (!prev_slave) { - SLAVE_AD_INFO(new_slave)->id = 1; - /* Initialize AD with the number of times that the AD timer is called in 1 second - * can be called only after the mac address of the bond is set - */ - bond_3ad_initialize(bond); - } else { - SLAVE_AD_INFO(new_slave)->id = - SLAVE_AD_INFO(prev_slave)->id + 1; - } - - bond_3ad_bind_slave(new_slave); break; case BOND_MODE_TLB: case BOND_MODE_ALB: @@ -2243,13 +2231,30 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res); goto err_unregister; } + upper_linked = true; bond_lower_state_changed(new_slave); + if (BOND_MODE(bond) == BOND_MODE_8023AD) { + if (!prev_slave) { + SLAVE_AD_INFO(new_slave)->id = 1; + /* Initialize AD with the number of times that the AD + * timer is called in 1 second; can be called only after + * the mac address of the bond is set + */ + bond_3ad_initialize(bond); + } else { + SLAVE_AD_INFO(new_slave)->id = + SLAVE_AD_INFO(prev_slave)->id + 1; + } + + bond_3ad_bind_slave(new_slave); + } + res = bond_sysfs_slave_add(new_slave); if (res) { slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res); - goto err_upper_unlink; + goto err_unregister; } /* If the mode uses primary, then the following is handled by @@ -2354,11 +2359,18 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, err_sysfs_del: bond_sysfs_slave_del(new_slave); -err_upper_unlink: - bond_upper_dev_unlink(bond, new_slave); - err_unregister: netdev_rx_handler_unregister(slave_dev); + if (upper_linked) { + if (BOND_MODE(bond) == BOND_MODE_8023AD) + bond_3ad_unbind_slave(new_slave); + bond_upper_dev_unlink(bond, new_slave); + if (BOND_MODE(bond) == BOND_MODE_8023AD) { + bond_set_carrier(bond); + if (bond_update_slave_arr(bond, new_slave)) + bond_slave_arr_work_rearm(bond, 0); + } + } err_detach: vlan_vids_del_by_dev(slave_dev, bond_dev); -- 2.55.0