bonding: unbind 802.3ad slaves on enslave failure
Subsystems (3)
PATCH 1/1 bonding: unbind 802.3ad slaves on enslave failure
From: Lin Jiapeng <[email protected]>
The 802.3ad enslave path binds a slave to an aggregator before publishing it in the bond's slave list. This lets the periodic state machine select the port and attach it to a LAG while enslavement is in progress.
If a later step fails, the rollback removes the slave and frees its ad_slave_info without unbinding its port. A port selected in the meantime can remain linked from another live slave's aggregator, so later LAG processing accesses freed memory. A test-only kretprobe forces bond_sysfs_slave_add() to return -ENOMEM, after which KASAN reports:
BUG: KASAN: slab-use-after-free in ad_agg_selection_logic+0x1a0d/0x1fa0
Read of size 1 at addr ff11000109be946e by task kworker/u16:0/13
Freed by task 110:
kasan_save_stack+0x30/0x50
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x70
__kasan_slab_free+0x47/0x70
kfree+0x1b3/0x550
slave_kobj_release+0xb6/0xd0
kobject_put+0x17e/0x300
bond_enslave+0x1acc/0x5090Call bond_3ad_unbind_slave() in the common rollback for published slaves before unlinking the slave from the bond. This detaches the port under mode_lock and preserves the invalidate-drain-free ordering used by normal release. Successful enslavement and earlier unpublished failures are unchanged.
--- drivers/net/bonding/bond_main.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/net/bonding/bond_main.c +2 -0
@@ -2355,6 +2355,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,bond_sysfs_slave_del (new_slave); err_upper_unlink: +if (BOND_MODE (bond) ==BOND_MODE_8023AD ) +bond_3ad_unbind_slave (new_slave);bond_upper_dev_unlink (bond, new_slave); err_unregister:
Signature · 1 line
2.55.0
Hi Jiapeng,
Quoted 52 lines · Lin Jiapeng
From: Lin Jiapeng <[email protected]>
The 802.3ad enslave path binds a slave to an aggregator before publishing it in the bond's slave list. This lets the periodic state machine select the port and attach it to a LAG while enslavement is in progress.
If a later step fails, the rollback removes the slave and frees its
ad_slave_infowithout unbinding its port. A port selected in the meantime can remain linked from another live slave's aggregator, so later LAG processing accesses freed memory. A test-only kretprobe forcesbond_sysfs_slave_add()to return-ENOMEM, after which KASAN reports:BUG: KASAN: slab-use-after-free in ad_agg_selection_logic+0x1a0d/0x1fa0 Read of size 1 at addr ff11000109be946e by task kworker/u16:0/13 Freed by task 110: kasan_save_stack+0x30/0x50 kasan_save_track+0x14/0x30 kasan_save_free_info+0x3b/0x70 __kasan_slab_free+0x47/0x70 kfree+0x1b3/0x550 slave_kobj_release+0xb6/0xd0 kobject_put+0x17e/0x300 bond_enslave+0x1acc/0x5090Call
bond_3ad_unbind_slave()in the common rollback for published slaves before unlinking the slave from the bond. This detaches the port undermode_lockand preserves the invalidate-drain-free ordering used by normal release. Successful enslavement and earlier unpublished failures are unchanged.Fixes 07699f9a7c8d ("bonding: add sysfs /slave dir for bond slave devices.")Reported-by TencentOS Corvus AI <[email protected]>Signed-off-by Lin Jiapeng <[email protected]>--- drivers/net/bonding/bond_main.c | 2 ++ 1 file changed, 2 insertions(+)drivers/net/bonding/bond_main.c +2
-0@@ -2355,6 +2355,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,bond_sysfs_slave_del (new_slave); err_upper_unlink: +if (BOND_MODE (bond) ==BOND_MODE_8023AD ) +bond_3ad_unbind_slave (new_slave);bond_upper_dev_unlink (bond, new_slave); err_unregister:Signature · 1 line
2.55.0
Thanks for your fix. I’m also working on correcting some error rollback issues in bond_enslave, including the 3ad unbind problem. My patch still needs further adjustments[1].
Just FYI, I’ll leave it up to the maintainer to decide whether to apply your fix first.
[1] https://lore.kernel.org/all/[email protected]/
Thanks
Hangbin
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 2 (1) · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix is incomplete: a failed bond_master_upper_dev_link() skips the new unbind and leaves the same use-after-free reachable.
- [High] The new bond_3ad_unbind_slave() call can cause a NULL pointer dereference in the RX softirq path.
- [Medium] The new caller breaks bond_3ad_unbind_slave()'s 'caller updates array' contract.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same publish-then-fail window leaves TLB/ALB hash entries pointing at the…
--
bonding: unbind 802.3ad slaves on enslave failure
This commit adds a bond_3ad_unbind_slave() call at the err_upper_unlink label of bond_enslave() in 802.3ad mode. If the state machine attached the new slave's port to a LAG during enslavement, the port is now detached before the slave is freed. This addresses a KASAN slab-use-after-free in ad_agg_selection_logic() seen when bond_sysfs_slave_add() fails.
drivers/net/bonding/bond_main.c int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, · line 2359
Quoted 10 lines
drivers/net/bonding/bond_main.c +2
-0@@ -2355,6 +2355,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,bond_sysfs_slave_del (new_slave); err_upper_unlink: +if (BOND_MODE (bond) ==BOND_MODE_8023AD ) +bond_3ad_unbind_slave (new_slave);
[Severity: High]
Can this unbind race with the 802.3ad receive path and cause a NULL pointer dereference in softirq context?
At err_upper_unlink, bond_handle_frame() is still registered as the rx_handler for slave_dev. netdev_rx_handler_unregister() drains in-flight handlers through synchronize_net(), but it only runs later, at err_unregister.
bond_3ad_rx_indication() checks port->slave without holding mode_lock:
drivers/net/bonding/bond_3ad.c:bond_3ad_rx_indication() {
...
port = &(SLAVE_AD_INFO(slave)->port);
if (!port->slave) {
...
return ret;
}
...
spin_lock(&slave->bond->mode_lock);
ad_rx_machine(lacpdu, port);
...
ad_marker_info_received(marker, port);
...
}Both callees dereference port->slave right away:
drivers/net/bonding/bond_3ad.c:ad_rx_machine() {
...
if (lacpdu) {
atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx);
...
}drivers/net/bonding/bond_3ad.c:ad_marker_info_received() {
...
atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx);
...
}bond_3ad_unbind_slave() sets port->slave = NULL under mode_lock. For a LACPDU, the RX CPU can pass the check, spin on mode_lock while the unbind runs, and then call ad_rx_machine() after port->slave is already NULL. The marker handlers don't take mode_lock at all.
__bond_release_one() avoids this by draining RX first:
/* unregister rx_handler early so bond_handle_frame wouldn't be called * for this slave anymore. */ netdev_rx_handler_unregister(slave_dev); if (BOND_MODE(bond) == BOND_MODE_8023AD) bond_3ad_unbind_slave(slave);
The commit message says this "preserves the invalidate-drain-free ordering used by normal release". Here, though, port->slave is cleared before the drain. Should the unbind come after netdev_rx_handler_unregister()?
One deterministic way to reach this rollback is the XDP check in bond_enslave(). With an XDP program on the bond and one already loaded on the slave, the code jumps to err_dec_slave_cnt and falls through to err_upper_unlink.
[Severity: Medium]
Does this rollback leave bond->usable_slaves stale?
bond_3ad_unbind_slave() discards the update_slave_arr result from ad_agg_selection_logic() and expects the caller to rebuild the array:
bool dummy_slave_update; /* Ignore this value as caller updates array */
__bond_release_one() does this by calling bond_update_slave_arr() after the unbind and unlink. In bond_enslave(), bond_update_slave_arr() is only called on the success path, so nothing in the rollback rebuilds the array.
The unbind can reselect the active aggregator, for example when the new port was already in the active aggregator. In that case ad_agg_selection_logic() can disable the old aggregator's ports and enable the new one's.
Wouldn't bond_3ad_xor_xmit() then keep using the old usable_slaves until some other event forces a rebuild?
drivers/net/bonding/bond_main.c int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, · line 2362
Quoted 3 lines
drivers/net/bonding/bond_main.c +0
-0bond_upper_dev_unlink (bond, new_slave); err_unregister:
[Severity: High]
Should the bond_master_upper_dev_link() failure path also unbind the port?
bond_3ad_bind_slave() runs well before the upper link, but a failure in the upper link jumps past the new unbind:
res = bond_master_upper_dev_link(bond, new_slave, extack);
if (res) {
slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
goto err_unregister;
}The slave can be visible to the 3ad state machine during that failed call. __netdev_upper_dev_link() first calls __netdev_adjacent_dev_link_neighbour(), which adds slave_dev to the list walked by bond_for_each_slave_rcu(). Only after that does it send NETDEV_CHANGEUPPER, and if a notifier fails it rolls the adjacency back.
CHANGEUPPER handlers can fail, and they can sleep. For example, dsa_user_changeupper() -> dsa_port_lag_join() can return -ENOMEM from dsa_port_lag_create(), a driver error from DSA_NOTIFIER_LAG_JOIN, or an error from dsa_port_bridge_join().
bond_3ad_state_machine_handler() only takes mode_lock and RCU. During that window it can run ad_port_selection_logic() on the new port and link it into another live slave's aggregator. The rollback then reaches kobject_put(&new_slave->kobj) and frees ad_info while lag_ports or port->aggregator still point into it.
Isn't that the same use-after-free in ad_agg_selection_logic() that this commit fixes for the sysfs failure?
The commit message says "earlier unpublished failures are unchanged", but this failure publishes the slave and then rolls it back. Could the unbind cover every failure after bond_3ad_bind_slave(), placed after the rx_handler drain?
[Severity: High]
This isn't a bug introduced by this patch, but can the same publish-then-fail window leave TLB/ALB hash entries pointing at the freed slave?
In balance-tlb/alb mode, bond_enslave() calls bond_alb_init_slave() and bond_set_active_slave(new_slave). Once the upper link publishes the slave, bond_slave_can_tx(new_slave) can be true, and a concurrent transmit can store the slave in the TX hash table:
bond_xmit_tlb_slave_get()
tlb_choose_channel()
__tlb_choose_channel()
tlb_get_least_loaded_slave()drivers/net/bonding/bond_alb.c:__tlb_choose_channel() {
...
assigned_slave = hash_table[hash_index].tx_slave;
if (!assigned_slave) {
assigned_slave = tlb_get_least_loaded_slave(bond);
if (assigned_slave) {
...
hash_table[hash_index].tx_slave = assigned_slave;
...
}Normal release clears these references in bond_alb_deinit_slave(), through tlb_clear_slave() and rlb_clear_slave(). The enslave rollback does nothing equivalent before kobject_put(&new_slave->kobj) frees the slave. bond_alb_monitor() only rebalances slaves that are still on the list.
A later packet with the same hash would get the freed slave back from __tlb_choose_channel(). bond_do_alb_xmit() would then use it:
if (tx_slave && bond_slave_can_tx(tx_slave)) {
...
return bond_dev_queue_xmit(bond, skb, tx_slave->dev);In TLB/ALB, two failures can happen after the slave is published:bond_sysfs_slave_add() failing (goto err_upper_unlink) and a failing NETDEV_CHANGEUPPER notifier (goto err_unregister). Should the rollback also call bond_alb_deinit_slave() for these modes?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113130454.63032-1-ljp1205831794%40gmail.com
Quoted 204 lines · On 2026-11-17 9:43 p.m., [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 2 (1) · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix is incomplete: a failed
bond_master_upper_dev_link()skips the new unbind and leaves the same use-after-free reachable.
- [High] The newbond_3ad_unbind_slave()call can cause a NULL pointer dereference in the RX softirq path.
- [Medium] The new caller breaks bond_3ad_unbind_slave()'s 'caller updates array' contract.Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same publish-then-fail window leaves TLB/ALB hash entries pointing at the…--
bonding: unbind 802.3ad slaves on enslave failure
This commit adds a
bond_3ad_unbind_slave()call at theerr_upper_unlinklabel ofbond_enslave()in 802.3ad mode. If the state machine attached the new slave's port to a LAG during enslavement, the port is now detached before the slave is freed. This addresses a KASAN slab-use-after-free inad_agg_selection_logic()seen whenbond_sysfs_slave_add()fails.drivers/net/bonding/bond_main.c int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, · line 2359
Quoted 10 lines
drivers/net/bonding/bond_main.c +2
-0@@ -2355,6 +2355,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,bond_sysfs_slave_del (new_slave); err_upper_unlink: +if (BOND_MODE (bond) ==BOND_MODE_8023AD ) +bond_3ad_unbind_slave (new_slave);[Severity: High]
Can this unbind race with the 802.3ad receive path and cause a NULL pointer dereference in softirq context?At
err_upper_unlink,bond_handle_frame()is still registered as therx_handlerforslave_dev.netdev_rx_handler_unregister()drains in-flight handlers throughsynchronize_net(), but it only runs later, aterr_unregister.
bond_3ad_rx_indication()checksport->slavewithout holdingmode_lock:drivers/net/bonding/bond_3ad.c:bond_3ad_rx_indication() { ... port = &(SLAVE_AD_INFO(slave)->port); if (!port->slave) { ... return ret; } ... spin_lock(&slave->bond->mode_lock); ad_rx_machine(lacpdu, port); ... ad_marker_info_received(marker, port); ... }Both callees dereference
port->slaveright away:drivers/net/bonding/bond_3ad.c:ad_rx_machine() { ... if (lacpdu) { atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx); ... }drivers/net/bonding/bond_3ad.c:ad_marker_info_received() { ... atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx); ... }
bond_3ad_unbind_slave()setsport->slave= NULL undermode_lock. For a LACPDU, the RX CPU can pass the check, spin onmode_lockwhile the unbind runs, and then callad_rx_machine()afterport->slaveis already NULL. The marker handlers don't takemode_lockat all.
__bond_release_one()avoids this by draining RX first:/* unregister rx_handler early so bond_handle_frame wouldn't be called * for this slave anymore. */ netdev_rx_handler_unregister(slave_dev); if (BOND_MODE(bond) == BOND_MODE_8023AD) bond_3ad_unbind_slave(slave);The commit message says this "preserves the invalidate-drain-free ordering used by normal release". Here, though,
port->slaveis cleared before the drain. Should the unbind come afternetdev_rx_handler_unregister()?One deterministic way to reach this rollback is the XDP check in
bond_enslave(). With an XDP program on the bond and one already loaded on the slave, the code jumps toerr_dec_slave_cntand falls through toerr_upper_unlink.[Severity: Medium]
Does this rollback leavebond->usable_slavesstale?
bond_3ad_unbind_slave()discards theupdate_slave_arrresult fromad_agg_selection_logic()and expects the caller to rebuild the array:bool
dummy_slave_update; /* Ignore this value as caller updates array */
__bond_release_one()does this by callingbond_update_slave_arr()after the unbind and unlink. Inbond_enslave(),bond_update_slave_arr()is only called on the success path, so nothing in the rollback rebuilds the array.The unbind can reselect the active aggregator, for example when the new port was already in the active aggregator. In that case
ad_agg_selection_logic()can disable the old aggregator's ports and enable the new one's.Wouldn't
bond_3ad_xor_xmit()then keep using the oldusable_slavesuntil some other event forces a rebuild?drivers/net/bonding/bond_main.c int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev, · line 2362
Quoted 3 lines
drivers/net/bonding/bond_main.c +0
-0bond_upper_dev_unlink (bond, new_slave); err_unregister:[Severity: High]
Should thebond_master_upper_dev_link()failure path also unbind the port?
bond_3ad_bind_slave()runs well before the upper link, but a failure in the upper link jumps past the new unbind:res = bond_master_upper_dev_link(bond, new_slave, extack); if (res) { slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res); goto err_unregister; }The slave can be visible to the 3ad state machine during that failed call.
__netdev_upper_dev_link()first calls__netdev_adjacent_dev_link_neighbour(), which addsslave_devto the list walked bybond_for_each_slave_rcu(). Only after that does it sendNETDEV_CHANGEUPPER, and if a notifier fails it rolls the adjacency back.CHANGEUPPER handlers can fail, and they can sleep. For example,
dsa_user_changeupper()->dsa_port_lag_join()can return-ENOMEMfromdsa_port_lag_create(), a driver error fromDSA_NOTIFIER_LAG_JOIN, or an error fromdsa_port_bridge_join().
bond_3ad_state_machine_handler()only takesmode_lockand RCU. During that window it can runad_port_selection_logic()on the new port and link it into another live slave's aggregator. The rollback then reacheskobject_put(&new_slave->kobj)and freesad_infowhilelag_portsorport->aggregatorstill point into it.Isn't that the same use-after-free in
ad_agg_selection_logic()that this commit fixes for the sysfs failure?The commit message says "earlier unpublished failures are unchanged", but this failure publishes the slave and then rolls it back. Could the unbind cover every failure after
bond_3ad_bind_slave(), placed after therx_handlerdrain?[Severity: High]
This isn't a bug introduced by this patch, but can the same publish-then-fail window leave TLB/ALB hash entries pointing at the freed slave?In balance-tlb/alb mode,
bond_enslave()callsbond_alb_init_slave()andbond_set_active_slave(new_slave). Once the upper link publishes the slave,bond_slave_can_tx(new_slave)can be true, and a concurrent transmit can store the slave in the TX hash table:
bond_xmit_tlb_slave_get()
tlb_choose_channel()__tlb_choose_channel() tlb_get_least_loaded_slave()drivers/net/bonding/bond_alb.c:__tlb_choose_channel() { ... assigned_slave = hash_table[hash_index].tx_slave; if (!assigned_slave) { assigned_slave = tlb_get_least_loaded_slave(bond); if (assigned_slave) { ... hash_table[hash_index].tx_slave = assigned_slave; ... }Normal release clears these references in
bond_alb_deinit_slave(), throughtlb_clear_slave()andrlb_clear_slave(). The enslave rollback does nothing equivalent beforekobject_put(&new_slave->kobj)frees the slave.bond_alb_monitor()only rebalances slaves that are still on the list.A later packet with the same hash would get the freed slave back from
__tlb_choose_channel().bond_do_alb_xmit()would then use it:
Quoted 9 lines
if (tx_slave && bond_slave_can_tx(tx_slave)) { ... return bond_dev_queue_xmit(bond, skb, tx_slave->dev);In TLB/ALB, two failures can happen after the slave is published:
bond_sysfs_slave_add()failing (gotoerr_upper_unlink) and a failingNETDEV_CHANGEUPPERnotifier (gotoerr_unregister). Should the rollback also callbond_alb_deinit_slave()for these modes?
I agree with the two High issues. In v2, I will drain the RX handler before unbinding and also cover the NETDEV_CHANGEUPPER failure path, without repeating upper-dev unlink. I will add the slave-array update where needed.
The array issue appears to be a consistency problem rather than a UAF, since slave_arr_work may already rebuild it.
The TLB/ALB issue is not directly related to this patch. If confirmed by further validation, it can be addressed with a separate patch.
pw-bot: cr