* [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure
@ 2026-12-02 11:10 Lin Jiapeng
2026-12-03 6:45 ` Hangbin Liu
2026-12-03 11:27 ` netdev-bot+sashiko
0 siblings, 2 replies; 4+ messages in thread
From: Lin Jiapeng @ 2026-12-02 11:10 UTC (permalink / raw)
To: jv, andrew+netdev, davem, edumazet, kuba, pabeni, sfeldma, netdev
Cc: Lin Jiapeng, TencentOS Corvus AI
From: Lin Jiapeng <jiapenglin@tencent.com>
The 802.3ad enslave path initializes each slave's LACP port while
assembling the bond. TencentOS Corvus AI found that an injected enslave
failure can leave the port attached after its slave is freed, causing a
KASAN use-after-free in ad_agg_selection_logic().
The bind precedes upper-device publication. During NETDEV_CHANGEUPPER or
a later fallible step such as bond_sysfs_slave_add(), the periodic state
machine can link the port into a LAG. The error unwind then unlinks the
slave and frees ad_slave_info without unbinding the port, so later LAG
processing dereferences freed memory. 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
Call Trace:
ad_agg_selection_logic+0x1a0d/0x1fa0
bond_3ad_state_machine_handler+0x116d/0x5250
Adding only a late unbind is insufficient because a failed upper-link
notification has already unlisted the slave, leaving an unlisted-but-bound
interval before that unbind runs.
Bind the port only after upper-link success. On later failures, drain the
RX handler, unbind while the slave is still listed, unlink it, re-evaluate
the carrier, and rebuild the slave array after aggregator changes.
Upper-link failures never bind the port, so they neither race with an
unlisted bound port nor emit a spurious LACPDU. Earlier unpublished
failures keep their previous behaviour.
With CONFIG_BONDING, CONFIG_VETH, CONFIG_USER_NS and CONFIG_NET_NS enabled,
an unprivileged user enters user and network namespaces, creates an 802.3ad
bond and a veth pair, and attempts to enslave the peer. A root-loaded,
test-only kretprobe makes bond_sysfs_slave_add() return -ENOMEM. Repeating
the attempt lets the 802.3ad worker select the port before rollback; the
unfixed kernel reports the above KASAN use-after-free, while the fixed
kernel returns -ENOMEM without it and a later normal enslave/release
succeeds.
Fixes: 07699f9a7c8d ("bonding: add sysfs /slave dir for bond slave devices.")
Reported-by: TencentOS Corvus AI <corvus@tencent.com>
Signed-off-by: Lin Jiapeng <jiapenglin@tencent.com>
---
v3 -> v4:
- Rebase onto current net/main. No functional change.
- Revise the commit message to document the configuration and steps needed
to reproduce the bug.
v3: https://lore.kernel.org/netdev/20261126082257.46411-1-ljp1205831794@gmail.com/
v2: https://lore.kernel.org/netdev/20261124024319.52145-1-ljp1205831794@gmail.com/
v1: https://lore.kernel.org/netdev/20261113130454.63032-1-ljp1205831794@gmail.com/
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 042d7c75548f..950c04af762a 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 ||
@@ -2185,19 +2186,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:
@@ -2246,13 +2234,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
@@ -2357,11 +2362,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);
base-commit: 0d39bf81267861742b80d5f1c52e8a63e9ca0ca1
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure
2026-12-02 11:10 [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure Lin Jiapeng
@ 2026-12-03 6:45 ` Hangbin Liu
2026-12-03 8:29 ` Lin Jiapeng
2026-12-03 11:27 ` netdev-bot+sashiko
1 sibling, 1 reply; 4+ messages in thread
From: Hangbin Liu @ 2026-12-03 6:45 UTC (permalink / raw)
To: Lin Jiapeng
Cc: jv, andrew+netdev, davem, edumazet, kuba, pabeni, sfeldma, netdev,
Lin Jiapeng, TencentOS Corvus AI
Hi Jiapeng,
On Wed, Dec 02, 2026 at 07:10:42PM +0800, Lin Jiapeng wrote:
> From: Lin Jiapeng <jiapenglin@tencent.com>
>
> The 802.3ad enslave path initializes each slave's LACP port while
> assembling the bond. TencentOS Corvus AI found that an injected enslave
> failure can leave the port attached after its slave is freed, causing a
This is an injected enslave failure, and I see you are using a kretprobe
for testing, which means it's very hard to reproduce and not observed in
real-world use. Perhaps we should target this patch to net-next?
> KASAN use-after-free in ad_agg_selection_logic().
>
> The bind precedes upper-device publication. During NETDEV_CHANGEUPPER or
> a later fallible step such as bond_sysfs_slave_add(), the periodic state
> machine can link the port into a LAG. The error unwind then unlinks the
> slave and frees ad_slave_info without unbinding the port, so later LAG
> processing dereferences freed memory. 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
> Call Trace:
> ad_agg_selection_logic+0x1a0d/0x1fa0
> bond_3ad_state_machine_handler+0x116d/0x5250
> Adding only a late unbind is insufficient because a failed upper-link
> notification has already unlisted the slave, leaving an unlisted-but-bound
> interval before that unbind runs.
>
> Bind the port only after upper-link success. On later failures, drain the
> RX handler, unbind while the slave is still listed, unlink it, re-evaluate
> the carrier, and rebuild the slave array after aggregator changes.
> Upper-link failures never bind the port, so they neither race with an
> unlisted bound port nor emit a spurious LACPDU. Earlier unpublished
> failures keep their previous behaviour.
>
> With CONFIG_BONDING, CONFIG_VETH, CONFIG_USER_NS and CONFIG_NET_NS enabled,
> an unprivileged user enters user and network namespaces, creates an 802.3ad
> bond and a veth pair, and attempts to enslave the peer. A root-loaded,
> test-only kretprobe makes bond_sysfs_slave_add() return -ENOMEM. Repeating
> the attempt lets the 802.3ad worker select the port before rollback; the
> unfixed kernel reports the above KASAN use-after-free, while the fixed
> kernel returns -ENOMEM without it and a later normal enslave/release
> succeeds.
>
> Fixes: 07699f9a7c8d ("bonding: add sysfs /slave dir for bond slave devices.")
> Reported-by: TencentOS Corvus AI <corvus@tencent.com>
> Signed-off-by: Lin Jiapeng <jiapenglin@tencent.com>
> ---
> v3 -> v4:
> - Rebase onto current net/main. No functional change.
> - Revise the commit message to document the configuration and steps needed
> to reproduce the bug.
>
> v3: https://lore.kernel.org/netdev/20261126082257.46411-1-ljp1205831794@gmail.com/
> v2: https://lore.kernel.org/netdev/20261124024319.52145-1-ljp1205831794@gmail.com/
> v1: https://lore.kernel.org/netdev/20261113130454.63032-1-ljp1205831794@gmail.com/
> 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 042d7c75548f..950c04af762a 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 ||
> @@ -2185,19 +2186,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);
Here the ACTIVEBACKUP and 8023AD both only have the flag setting. Maybe just
case BOND_MODE_ACTIVEBACKUP:
case BOND_MODE_8023AD:
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:
> @@ -2246,13 +2234,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
> @@ -2357,11 +2362,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);
> + }
Do we need to update slave array here? The slave_cnt hasn't changed yet, no?
Thanks
Hangbin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure
2026-12-03 6:45 ` Hangbin Liu
@ 2026-12-03 8:29 ` Lin Jiapeng
0 siblings, 0 replies; 4+ messages in thread
From: Lin Jiapeng @ 2026-12-03 8:29 UTC (permalink / raw)
To: Hangbin Liu
Cc: jv, andrew+netdev, davem, edumazet, kuba, pabeni, sfeldma, netdev,
Lin Jiapeng, TencentOS Corvus AI
On 2026-12-03 2:45 p.m., Hangbin Liu wrote:
> Hi Jiapeng,
> On Wed, Dec 02, 2026 at 07:10:42PM +0800, Lin Jiapeng wrote:
>> From: Lin Jiapeng <jiapenglin@tencent.com>
>>
>> The 802.3ad enslave path initializes each slave's LACP port while
>> assembling the bond. TencentOS Corvus AI found that an injected enslave
>> failure can leave the port attached after its slave is freed, causing a
>
> This is an injected enslave failure, and I see you are using a kretprobe
> for testing, which means it's very hard to reproduce and not observed in
> real-world use. Perhaps we should target this patch to net-next?
>
>> KASAN use-after-free in ad_agg_selection_logic().
>>
>> The bind precedes upper-device publication. During NETDEV_CHANGEUPPER or
>> a later fallible step such as bond_sysfs_slave_add(), the periodic state
>> machine can link the port into a LAG. The error unwind then unlinks the
>> slave and frees ad_slave_info without unbinding the port, so later LAG
>> processing dereferences freed memory. 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
>> Call Trace:
>> ad_agg_selection_logic+0x1a0d/0x1fa0
>> bond_3ad_state_machine_handler+0x116d/0x5250
>> Adding only a late unbind is insufficient because a failed upper-link
>> notification has already unlisted the slave, leaving an unlisted-but-bound
>> interval before that unbind runs.
>>
>> Bind the port only after upper-link success. On later failures, drain the
>> RX handler, unbind while the slave is still listed, unlink it, re-evaluate
>> the carrier, and rebuild the slave array after aggregator changes.
>> Upper-link failures never bind the port, so they neither race with an
>> unlisted bound port nor emit a spurious LACPDU. Earlier unpublished
>> failures keep their previous behaviour.
>>
>> With CONFIG_BONDING, CONFIG_VETH, CONFIG_USER_NS and CONFIG_NET_NS enabled,
>> an unprivileged user enters user and network namespaces, creates an 802.3ad
>> bond and a veth pair, and attempts to enslave the peer. A root-loaded,
>> test-only kretprobe makes bond_sysfs_slave_add() return -ENOMEM. Repeating
>> the attempt lets the 802.3ad worker select the port before rollback; the
>> unfixed kernel reports the above KASAN use-after-free, while the fixed
>> kernel returns -ENOMEM without it and a later normal enslave/release
>> succeeds.
>>
>> Fixes: 07699f9a7c8d ("bonding: add sysfs /slave dir for bond slave devices.")
>> Reported-by: TencentOS Corvus AI <corvus@tencent.com>
>> Signed-off-by: Lin Jiapeng <jiapenglin@tencent.com>
>> ---
>> v3 -> v4:
>> - Rebase onto current net/main. No functional change.
>> - Revise the commit message to document the configuration and steps needed
>> to reproduce the bug.
>>
>> v3: https://lore.kernel.org/netdev/20261126082257.46411-1-ljp1205831794@gmail.com/
>> v2: https://lore.kernel.org/netdev/20261124024319.52145-1-ljp1205831794@gmail.com/
>> v1: https://lore.kernel.org/netdev/20261113130454.63032-1-ljp1205831794@gmail.com/
>> 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 042d7c75548f..950c04af762a 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 ||
>> @@ -2185,19 +2186,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);
>
> Here the ACTIVEBACKUP and 8023AD both only have the flag setting. Maybe just
>
> case BOND_MODE_ACTIVEBACKUP:
> case BOND_MODE_8023AD:
> 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:
>> @@ -2246,13 +2234,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
>> @@ -2357,11 +2362,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);
>> + }
>
> Do we need to update slave array here? The slave_cnt hasn't changed yet, no?
>
> Thanks
> Hangbin
Thanks for your review.
You are right about the trigger. In a realistic unprivileged setup,
reaching a suitable enslave failure seems to require memory pressure/OOM
or another bug, such as the issue fixed by b6743653d134. I agree that
net-next is a better target. I will also merge the ACTIVEBACKUP and
8023AD cases as you suggested.
Regarding the slave array, bond_3ad_unbind_slave() can invoke
ad_agg_selection_logic() when the removed port affects the active
aggregator, potentially selecting a different active aggregator even
though slave_cnt remains unchanged. Rebuilding the array afterwards
therefore seems to be a safer, conservative way to keep it consistent
with the LACP state, so I would prefer to keep the update.
Regards,
Jiapeng
pw-bot: cr
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure
2026-12-02 11:10 [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure Lin Jiapeng
2026-12-03 6:45 ` Hangbin Liu
@ 2026-12-03 11:27 ` netdev-bot+sashiko
1 sibling, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 11:27 UTC (permalink / raw)
To: ljp1205831794
Cc: jv, andrew+netdev, davem, edumazet, kuba, pabeni, sfeldma, netdev,
jiapenglin, corvus
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 1 · High: 0 · 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).
- [Critical] The patch moves bond_3ad_initialize() and
bond_3ad_bind_slave() in bond_enslave()…
- [Medium] bonding: slave_arr_work armed on DOWN bond device in
bond_enslave() rollback
--
bonding: unbind 802.3ad slaves on enslave failure
This commit moves bond_3ad_initialize() and bond_3ad_bind_slave() in
bond_enslave(). They now run only after bond_master_upper_dev_link()
succeeds. On later failures the error unwind now unbinds the 802.3ad
port, updates the carrier and rebuilds the slave array, which is meant
to fix a use-after-free in ad_agg_selection_logic().
> diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
> index 042d7c75548fa..950c04af762a4 100644
> --- a/drivers/net/bonding/bond_main.c
> +++ b/drivers/net/bonding/bond_main.c
[ ... ]
> @@ -2185,19 +2186,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:
> @@ -2246,13 +2234,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);
> + }
[Severity: Critical]
With this change, the bind now runs after two earlier steps.
netdev_rx_handler_register() has already made bond_handle_frame() live,
and bond_master_upper_dev_link() has already put the slave on
bond->dev->adj_list.lower.
Can the 802.3ad worker or the LACPDU RX path now see a half-built port
during a normal, successful enslave?
bond_3ad_bind_slave() runs without bond->mode_lock. It sets port->slave
before it sets up the aggregator:
drivers/net/bonding/bond_3ad.c:bond_3ad_bind_slave() {
...
port->slave = slave;
...
ad_initialize_agg(aggregator);
...
aggregator->slave = slave;
...
}
The only per-port readiness check in bond_3ad_state_machine_handler()
is:
port = &(SLAVE_AD_INFO(slave)->port);
if (!port->slave) {
net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
bond->dev->name);
goto re_arm;
}
Take the case of enslaving the first slave into an 802.3ad bond that is
already up. ad_work has been queued since bond_open(). Suppose the
worker runs after port->slave is stored but before aggregator->slave is
set. Could it take this path?
bond_3ad_state_machine_handler()
ad_rx_machine(NULL, port) /* AD_PORT_BEGIN -> INITIALIZE */
ad_port_selection_logic() /* picks the new slave's zeroed aggregator */
ad_agg_selection_logic(__get_first_agg(port), ...)
struct bonding *bond = agg->slave->bond; /* agg->slave is NULL */
This oops would happen while the worker holds
spin_lock_bh(&bond->mode_lock). Anything that takes mode_lock afterwards
would spin forever: LACPDU RX, bond_update_slave_arr() and unbind.
bond_3ad_set_carrier()->__get_active_agg() also does
aggregator->slave->bond.
Non-first slaves look exposed too. If every existing aggregator is in
use, the worker could link the new port into the new slave's aggregator
and mark it SELECTED. The later ad_initialize_agg() in the bind would
then clear lag_ports and num_of_ports. Wouldn't port->aggregator then
point at an aggregator that does not list the port, with no further
selection for that port?
There is also the window between the upper link and the bind, which
covers the NETDEV_CHANGEUPPER and NETDEV_CHANGELOWERSTATE notifier
chains. In that window every worker tick hits "Found an uninitialized
port" or "bond's first port is uninitialized" and jumps to re_arm. That
skips the state machines for every port in the bond.
During the same window, LACPDUs that reach the new slave through
bond_3ad_rx_indication() are dropped with "port of slave ... is
uninitialized".
Before this patch the bind finished before the slave was listed and
before the RX handler was registered, so these readers only ever saw a
fully initialized port. The commit message only looks at the
"unlisted-but-bound" interval.
Could the bind stay ahead of publication, with the upper-link failure
unwind handled another way? Alternatively, bond_3ad_initialize() and
bond_3ad_bind_slave() could run under bond->mode_lock with port->slave
set last. The readers would then skip an unbound port rather than abort
the whole tick.
> +
> 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;
> }
[ ... ]
> @@ -2357,11 +2362,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);
>
[Severity: Medium]
If bond_update_slave_arr() fails here, for example because
kzalloc_flex() returns NULL, this queues bond->slave_arr_work on
bond->wq. Nothing checks whether bond_dev is up at that point.
Is that safe when the failing enslave targets an 802.3ad bond that is
down? As far as I can see, the only place that cancels slave_arr_work
is bond_work_cancel_all(), and that is only called from bond_close().
If the bond is never opened and is later deleted, bond_close() never
runs, so nothing cancels the work.
bond_slave_arr_handler() also re-arms itself with a 1 jiffy delay when
rtnl_trylock() fails or when bond_update_slave_arr() fails again:
if (!rtnl_trylock())
goto err;
...
err:
bond_slave_arr_work_rearm(bond, 1);
RTNL can easily be held when the work first runs. The failing
bond_enslave() or a following unregister_netdevice() may still hold
it. If the item is sitting as a pending timer when bond_destructor()
calls destroy_workqueue(), the drain does not wait for it. That would
let free_netdev() free struct bonding while slave_arr_work.timer is
still armed, and delayed_work_timer_fn() would then use freed memory.
The handler could instead run after bond_uninit() has called
bond_set_slave_arr(bond, NULL, NULL). In that case it would install
new usable_slaves and all_slaves arrays that are never freed.
Is the re-arm needed here at all? On failure with a non-NULL
skipslave, bond_update_slave_arr() already calls bond_skip_slave() on
both existing arrays. That removes new_slave from them before
kobject_put() frees it. __bond_release_one() calls
bond_update_slave_arr(bond, slave) without re-arming the work.
Could the rollback do the same? Or should it re-arm only when
netif_running(bond_dev) is true?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202111120.99494-1-ljp1205831794%40gmail.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 11:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 11:10 [PATCH net v4] bonding: unbind 802.3ad slaves on enslave failure Lin Jiapeng
2026-12-03 6:45 ` Hangbin Liu
2026-12-03 8:29 ` Lin Jiapeng
2026-12-03 11:27 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox