Skip to contentSkip to conversation

ieee802154: keep the original error after a netns rollback

PATCH Applied Andrey Misiurov 5 participants 5 messages Nov 20 – Dec 1

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ieee802154: keep the original error after a netns rollback

cfg802154_switch_netns() moves all of a phy's netdevs into a new netns. If one move fails, it jumps to the errout label and moves the already-moved netdevs back to the old netns.

That rollback loop overwrites the same 'err' variable with the result of each move-back. The move-backs succeed, so 'err' ends up 0 and the function returns 0 -- success -- even though nothing moved and the phy stayed in the old netns. nl802154_wpan_phy_netns() then tells userspace the move worked.

Give the rollback loop its own 'err' so the original error survives and is returned.

Fixes 66e5c2672cd1 ("ieee802154: add netns support")

---
v2: shadow 'err' inside the rollback loop instead of adding a separate
variable next to it (Miquel Raynal).

Found during a backport review.

On debian13 6.12.88, "iwpan phy phy0 set netns name T" reported success (returned 0) while phy0 and its wpan0 interface stayed in the original namespace; internally the move had failed with -EEXIST, but the function returned 0 anyway. With the fix the same command correctly fails with "File exists".

 net/ieee802154/core.c | 2 ++
 1 file changed, 2 insertions(+)
net/ieee802154/core.c +2 -0
@@ -255,6 +255,8 @@ errout:
 	list_for_each_entry_continue_reverse(wpan_dev,
 					     &rdev->wpan_dev_list,
 					     list) {
+		int err;
+
 		if (!wpan_dev->netdev)
 			continue;
 		wpan_dev->netdev->netns_immutable = false;
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Andrey Misiurov

cfg802154_switch_netns() moves all of a phy's netdevs into a new netns. If one move fails, it jumps to the errout label and moves the already-moved netdevs back to the old netns.

That rollback loop overwrites the same 'err' variable with the result of each move-back. The move-backs succeed, so 'err' ends up 0 and the function returns 0 -- success -- even though nothing moved and the phy stayed in the old netns. nl802154_wpan_phy_netns() then tells userspace the move worked.

Quoted 5 lines

Give the rollback loop its own 'err' so the original error survives and is returned.

Fixes 66e5c2672cd1 ("ieee802154: add netns support")
#2 lore raw
In reply to Andrey Misiurov
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 (2) · Medium: 0 · Low: 1

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 rollback loop in cfg802154_switch_netns() (net/ieee802154/core.c, lines 255-267) walks rdev->wpan_dev_list…
- [Low] The commit message suggests that any failed move in cfg802154_switch_netns() ends with 'err' overwritten to 0.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it sits on the exact path the commit message describes (-EEXIST on the forward move).
- [High] This problem wasn't introduced by this patch, but cfg802154_pernet_exit() (net/ieee802154/core.c, around line 361) calls…

--

ieee802154: keep the original error after a netns rollback

This patch shadows 'err' inside the rollback loop of cfg802154_switch_netns(). A failed netns switch then returns the original error instead of the result of the last move-back. Without it, a failed switch can be reported to userspace as a success through nl802154_wpan_phy_netns().

Quoted 4 lines

That rollback loop overwrites the same 'err' variable with the result of each move-back. The move-backs succeed, so 'err' ends up 0 and the function returns 0 -- success -- even though nothing moved and the phy stayed in the old netns.

[Severity: Low]
This isn't a bug, but could the commit message say more precisely when the overwrite happens?

Suppose the first entry in rdev->wpan_dev_list that has a netdev is the one that fails. list_for_each_entry_continue_reverse() starts at list_prev_entry(pos), which is the list head. The rollback body never runs, so the unpatched code already returns the original error in that case.

The overwrite only happens in two cases:

- at least one earlier netdev moved before a later one failed, or
- every netdev moved and then device_rename() failed.

The message doesn't mention the device_rename() path, which this patch also fixes.

The reproduction note below the --- line ("phy0 and its wpan0 interface", -EEXIST internally, 0 returned) only fits if phy0 had another interface that moved before wpan0 failed.

Would stating these conditions help backporters decide whether they are affected?

net/ieee802154/core.c int cfg802154_switch_netns(struct cfg802154_registered_device *rdev, · line 257

Quoted 8 lines
net/ieee802154/core.c +0 -0
index c0b8712018a16..f471744175182 100644
@@ -255,6 +255,8 @@ int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
 	list_for_each_entry_continue_reverse(wpan_dev,
 					     &rdev->wpan_dev_list,
 					     list) {

[Severity: High]
Does this rollback really move all of the already-moved netdevs back?

Each dev_change_net_namespace() call runs the NETDEV_UNREGISTER and then the NETDEV_REGISTER notifier. cfg802154_netdev_notifier_call() changes rdev->wpan_dev_list while that happens:

net/ieee802154/core.c:cfg802154_netdev_notifier_call() {
    ...
	case NETDEV_REGISTER:
		...
		list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
    ...
	case NETDEV_UNREGISTER:
		...
		if (!list_empty(&wpan_dev->list)) {
			list_del_rcu(&wpan_dev->list);
		...
		INIT_LIST_HEAD(&wpan_dev->list);
    ...
}

So every successful move puts the moved wpan_dev at the head of the list. Take a list of [A, B, C] where C fails:

forward loop: move A -> [A, B, C]
move B -> [B, A, C]
A again, net_eq() no-op
C fails, break with pos = C

rollback: pos = prev(C) = A
move A back -> [A, B, C]
prev(A) is the list head, loop ends

B is never moved back. On the device_rename() failure path pos is the list head, so only prev(head) is moved back.

With this patch the function returns the original error (for example -EEXIST from an altname clash in the target netns). Userspace is told the switch failed, but B stays in the new netns while the phy stays in the old one.

The commit message says the error path "moves the already-moved netdevs back to the old netns" and that "nothing moved". That doesn't seem to hold when two or more netdevs had already moved.

The stranded netdev also keeps netns_immutable = true. When its netns is torn down, default_device_exit_net() skips it. default_device_exit_batch() then unregisters it with unregister_netdevice_queue(), because mac802154 netdevs have no rtnl_link_ops.

That path bypasses ieee802154_if_remove(), so sdata->list stays on local->interfaces after the netdev and its sdata are freed. Can that then be dereferenced in __ieee802154_rx_handle_packet():

	list_for_each_entry_rcu(sdata, &local->interfaces, list) {

or by the list_del_rcu() in ieee802154_remove_interfaces()?

net/ieee802154/core.c errout: · line 262

Quoted 5 lines
net/ieee802154/core.c +2 -0
+		int err;
+
 		if (!wpan_dev->netdev)
 			continue;
 		wpan_dev->netdev->netns_immutable = false;

[Severity: High]
This isn't a bug introduced by this patch, but it is on the same -EEXIST forward-move path the commit message describes. Is the netns_immutable flag of the failing netdev ever restored?

In the forward loop of cfg802154_switch_netns():

		wpan_dev->netdev->netns_immutable = false;
		err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
		if (err) {
			WARN_ON(err && err != -ENOMEM);
			break;
		}
		wpan_dev->netdev->netns_immutable = true;

The break skips the restore. The rollback starts at list_prev_entry() of the failing entry, so it never visits that netdev either. That leaves the only guard in __dev_change_net_namespace() disabled:

	if (dev->netns_immutable) {
		NL_SET_ERR_MSG(extack, "The interface netns is immutable");
		goto out;
	}

A later "ip link set wpanX netns Y" can then move the netdev away from its phy. NETDEV_REGISTER sets the flag back to true in Y. When Y is destroyed, the same default_device_exit_batch() path described above unregisters the netdev without ieee802154_if_remove(). That leaves a freed sdata on local->interfaces.

cfg80211_switch_wdev_netns() in net/wireless/core.c always restores the flag, whatever the result:

	wdev->netdev->netns_immutable = false;
	err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
	wdev->netdev->netns_immutable = true;

Should the 802.15.4 version also set the flag back before the break?

[Severity: High]
This is a pre-existing issue, but what happens if cfg802154_switch_netns() fails when it is called from cfg802154_pernet_exit()?

net/ieee802154/core.c:cfg802154_pernet_exit() {
    ...
		if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
			cfg802154_switch_netns(rdev, &init_net);
    ...
}

The return value is ignored. Altname clashes don't apply during teardown, because __dev_change_net_namespace() destroys conflicting altnames when !check_net(net_old). The trigger here is an allocation failure in dev_change_net_namespace() or device_rename().

On such a failure the errout path does:

	net = wpan_phy_net(&rdev->wpan_phy);

That is the dying netns. The rolled-back netdevs are moved back into a namespace that is about to be freed, possibly only some of them given the list reordering above. wpan_phy_net_set() is never called, so the phy keeps pointing at the freed struct net.

The immutable netdevs left in the dying netns are then unregistered by default_device_exit_batch() without ieee802154_if_remove(). That leaves dangling sdata entries on local->interfaces, which ieee802154_rx() and ieee802154_remove_interfaces() later use.

cfg80211_pernet_exit() handles this with force mode and a WARN_ON():

			WARN_ON(__cfg80211_switch_netns(rdev, &init_net, true));

Force mode unregisters the interfaces that cannot be moved. Is a similar fallback needed for 802.15.4?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120065750.3274661-1-amisiurov%40cloudlinux.com
maintainer Applied
#3 lore raw
In reply to Andrey Misiurov

Hello Andrey Misiurov.

Quoted 10 lines · Andrey Misiurov

cfg802154_switch_netns() moves all of a phy's netdevs into a new netns. If one move fails, it jumps to the errout label and moves the already-moved netdevs back to the old netns.

That rollback loop overwrites the same 'err' variable with the result of each move-back. The move-backs succeed, so 'err' ends up 0 and the function returns 0 -- success -- even though nothing moved and the phy stayed in the old netns. nl802154_wpan_phy_netns() then tells userspace the move worked.

[...]

Applied to wpan/wpan.git, thanks!

[1/1] ieee802154: keep the original error after a netns rollback

      https://git.kernel.org/wpan/wpan/c/006dfe9c1d68

regards,
Stefan Schmidt

#4 lore raw
In reply to Andrey Misiurov
This patch was applied to netdev/net.git (main) by Stefan Schmidt <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Stefan Schmidt <[email protected]>:

Quoted 10 lines · On Fri, 20 Nov 2026 07:57:49 +0100 you wrote:

cfg802154_switch_netns() moves all of a phy's netdevs into a new netns. If one move fails, it jumps to the errout label and moves the already-moved netdevs back to the old netns.

That rollback loop overwrites the same 'err' variable with the result of each move-back. The move-backs succeed, so 'err' ends up 0 and the function returns 0 -- success -- even though nothing moved and the phy stayed in the old netns. nl802154_wpan_phy_netns() then tells userspace the move worked.

[...]

Here is the summary with links:
- [net,v2] ieee802154: keep the original error after a netns rollback

    https://git.kernel.org/netdev/net/c/006dfe9c1d68

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html