net: dsa: felix: fix NULL pointer dereference in felix_port_enable()
Subsystems (4)
PATCH 1/1 net: dsa: felix: fix NULL pointer dereference in felix_port_enable()
DSA registers the user ports in dsa_tree_setup_ports() before it sets conduit->dsa_ptr in dsa_tree_setup_conduit(). On teardown, it clears conduit->dsa_ptr in dsa_tree_teardown_conduit() before dsa_tree_teardown_ports() removes the user ports. RTNL is not held across either pair of steps, so user space can open a user port while conduit->dsa_ptr is NULL.
In NPI mode, felix_port_enable() calls felix_cpu_port_for_conduit(). For a physical conduit, that function reads the CPU port index through conduit->dsa_ptr without checking for NULL. This caused an oops on an LS1028A RDB when systemd-networkd tried to open a user port while the switch was being probed again at runtime:
[ 166.722423] Unable to handle kernel access to user memory outside uaccess routines at virtual address 0000000000000028 [ 166.816908] Internal error: Oops: 0000000096000004 [#1] SMP [ 166.961766] pc : felix_cpu_port_for_conduit.isra.0+0x90/0xb8 [mscc_felix_dsa_lib] [ 166.969289] lr : felix_port_enable+0x120/0x1a0 [mscc_felix_dsa_lib]
The oops left RTNL locked. Later operations that waited for RTNL hung, and the board had to be power cycled.
NPI mode uses one CPU port for the whole switch. Changing the conduit of a user port can move the NPI port to another CPU port, and the check in felix_port_enable() keeps user ports that still use the old CPU port down.
For a physical conduit, compare dp->cpu_dp->index with the NPI port instead. DSA sets dp->cpu_dp before it registers the user port and clears it only after it removes the user port, so the check keeps working while conduit->dsa_ptr is NULL. As long as the physical conduit is set up, conduit->dsa_ptr and dp->cpu_dp point to the same CPU port, so the check gives the same result as before. LAG conduits keep using felix_cpu_port_for_conduit(), which does not read dsa_ptr for them.
---
The crash was seen on a 7.0-based distribution kernel. I applied this patch to that kernel and probed the switch again at runtime 117 times on an LS1028A RDB, with systemd-networkd bringing up the user ports. Each time, three user ports were opened before the conduit was set up. All of them came up, and there was no crash. The check also still
works: after one port was moved to the other conduit, bringing up a port that still used the old conduit failed with "Multiple conduits are not allowed". I also unbound the switch 10 times while bringing up user ports that were down. 22 of those ports came up after the conduit was torn down and before they were removed, with no crash. On net, this patch is only compile-tested.
drivers/net/dsa/ocelot/felix.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/net/dsa/ocelot/felix.c +12 -1
@@ -1239,8 +1239,19 @@ static int felix_port_enable(struct dsa_switch *ds, int port,if (ocelot->npi >=0 ) {struct net_device *conduit =dsa_port_to_conduit (dp); +int cpu; -if (felix_cpu_port_for_conduit (ds, conduit) != ocelot->npi) { +/* Do not use conduit->dsa_ptr for a physical conduit. It is +* NULL before DSA sets up the conduit and after it tears it +* down, and opening a user port can reach this check in both +* cases. +*/ +if (netif_is_lag_master (conduit)) + cpu =felix_cpu_port_for_conduit (ds, conduit); +else + cpu = dp->cpu_dp->index; + +if (cpu != ocelot->npi) {dev_err (ds->dev,"Multiple conduits are not allowed\n" );return -EINVAL ; }
Signature · 1 line
2.53.0
Vladimir Oltean
maintainerHi Aristo,
Quoted 2 lines · Aristo Chen
DSA registers the user ports in
dsa_tree_setup_ports()before it setsconduit->dsa_ptrindsa_tree_setup_conduit().
I wish I had seen this when writing commit 11fd667dac31 ("net: dsa:
setup master before ports"), because I later had to revert it in commit 762c2998c9625f642f0d23da7d3f7e4f90665fdf due to some unforeseen MTU handling complications. Had I realized the conduit->dsa_ptr implications, I would have tried harder to solve the MTU stuff.
Quoted 5 lines
On teardown, it clears
conduit->dsa_ptrindsa_tree_teardown_conduit()beforedsa_tree_teardown_ports()removes the user ports. RTNL is not held across either pair of steps, so user space can open a user port whileconduit->dsa_ptris NULL.
Correct, even though conduit->dsa_ptr itself *is* assigned under rtnl_lock() since commit c146f9bc195a ("net: dsa: hold rtnl_mutex when calling dsa_master_{setup,teardown}").
Quoted 6 lines
In NPI mode,
felix_port_enable()callsfelix_cpu_port_for_conduit(). For a physical conduit, that function reads the CPU port index throughconduit->dsa_ptrwithout checking for NULL. This caused an oops on an LS1028A RDB when systemd-networkd tried to open a user port while the switch was being probed again at runtime:
Yeah, I think this isn't the only problem. conduit->dsa_ptr is accessed from the data path as well, so IMO patching place after place is just playing the whack-a-mole game.
I realize reapplying commit 11fd667dac31 ("net: dsa: setup master before ports") for stable kernels introduces a high regression risk, but what about this change instead (not even compile-tested)?
net/dsa/user.c +3 -0
@@ -403,6 +403,9 @@ static int dsa_user_open(struct net_device *dev)struct dsa_port *dp =dsa_user_to_port (dev);int err; +if (!conduit->dsa_ptr) +return -ENETDOWN ; + err =dev_open (conduit,NULL );if (err <0 ) {netdev_err (dev,"failed to open conduit %s\n" , conduit->name);
I know what you're thinking - if we refuse bringing the interface up altogether, won't that regress user space and be even worse than risking race conditions with dsa_conduit_setup() assigning dev->dsa_ptr fast enough?
And the reason why I think this alternative change has a chance is because of DSA history, where bringing a user port up had as prerequisite to bring the conduit up first. I removed that dependency in commit 9d5ef190e561 ("net: dsa: automatically bring up DSA master when opening user port"), but user space still has code to deal with this for older kernels. See:
https://github.com/systemd/systemd/blob/v262.2/src/network/networkd-setlink.c#L1081-L1082
and I'm hoping other network managers than systemd-networkd have similar code as well.
So I'm just proposing to play dumb for a while in stable kernels, return the -ENETDOWN that used to be established practice, and buy some time to do a more proper reordering of the setup/teardown in net-next, which gives us the guarantee that we need that conduit->dsa_ptr is a valid pointer for the entire registered lifetime of the user ports.
Hi Vladimir,
Thanks for the quick review and the suggestion!
Quoted 53 lines · Vladimir Oltean
Hi Aristo,
Quoted 2 lines · Aristo Chen
DSA registers the user ports in
dsa_tree_setup_ports()before it setsconduit->dsa_ptrindsa_tree_setup_conduit().I wish I had seen this when writing commit
11fd667dac31("net: dsa:
setup master before ports"), because I later had to revert it in commit762c2998c9625f642f0d23da7d3f7e4f90665fdfdue to some unforeseen MTU handling complications. Had I realized theconduit->dsa_ptrimplications, I would have tried harder to solve the MTU stuff.Quoted 5 lines
On teardown, it clears
conduit->dsa_ptrindsa_tree_teardown_conduit()beforedsa_tree_teardown_ports()removes the user ports. RTNL is not held across either pair of steps, so user space can open a user port whileconduit->dsa_ptris NULL.Correct, even though
conduit->dsa_ptritself *is* assigned underrtnl_lock()since commitc146f9bc195a("net: dsa: holdrtnl_mutexwhen calling dsa_master_{setup,teardown}").Quoted 6 lines
In NPI mode,
felix_port_enable()callsfelix_cpu_port_for_conduit(). For a physical conduit, that function reads the CPU port index throughconduit->dsa_ptrwithout checking for NULL. This caused an oops on an LS1028A RDB when systemd-networkd tried to open a user port while the switch was being probed again at runtime:Yeah, I think this isn't the only problem.
conduit->dsa_ptris accessed from the data path as well, so IMO patching place after place is just playing the whack-a-mole game.I realize reapplying commit
11fd667dac31("net: dsa: setup master before ports") for stable kernels introduces a high regression risk, but what about this change instead (not even compile-tested)?net/dsa/user.c +3
-0@@ -403,6 +403,9 @@ static int dsa_user_open(struct net_device *dev)struct dsa_port *dp =dsa_user_to_port (dev);int err; +if (!conduit->dsa_ptr) +return -ENETDOWN ; + err =dev_open (conduit,NULL );if (err <0 ) {netdev_err (dev,"failed to open conduit %s\n" , conduit->name);
Quoted 3 lines
I know what you're thinking - if we refuse bringing the interface up altogether, won't that regress user space and be even worse than risking race conditions with
dsa_conduit_setup()assigningdev->dsa_ptrfast enough?
I tested your change on the LS1028A RDB, on a 7.0-based kernel without the felix patch, with systemd 259. It does prevent the crash. The conduit was already up in all 10 runtime probes, and I saw no oops. In 10 unbinds, 28 opens during teardown failed with -ENETDOWN, and the other 2 found the port already gone.
But networkd did not bring the refused ports back. In all 17 runtime probes where only networkd tried to bring up the ports, swp0-swp2 stayed down until I ran "networkctl reconfigure". In the first seven, they were still "off (configuring)" after two minutes. Only swp3 came up on its own.
In four debug runs, networkd logged two -ENETDOWN "retrying" messages per affected port. Before it retries, networkd checks that the conduit is up, but not that DSA has set it up. In link_up_or_down_handler(), an -ENETDOWN reply to the activation request makes link_up_dsa_slave() queue an "up" request. If that request gets -ENETDOWN too, its handler tries to queue another one while the failed request is still in the queue, and request_new() drops the new one as a duplicate. That would explain the two messages and why the ports stayed down. The handler and the duplicate check look the same in v262.2, but I only tested 259.
So the core change can leave a port down if an early open used to succeed and user space does not retry after DSA sets up the conduit. In these tests, networkd did not retry after setup. For drivers whose early opens work today, that would be a new regression.
I agree that the packet path still needs a proper lifetime fix. The felix patch only fixes the crash I reproduced. For a physical conduit, it checks the user port's assigned CPU port against the NPI port without reading dsa_ptr, and it leaves the LAG check unchanged. I also checked the open path and the other drivers' .port_enable callbacks, and did not find another place that dereferences conduit->dsa_ptr without checking it. On the same board, all ports came up in 117 runtime probes with the felix patch, and there was no oops in 10 teardown runs.
Would you take the felix patch for net, to fix this crash without making early opens fail in the core? The setup and teardown order could then be fixed in net-next.
Quoted 11 lines
And the reason why I think this alternative change has a chance is because of DSA history, where bringing a user port up had as prerequisite to bring the conduit up first. I removed that dependency in commit
9d5ef190e561("net: dsa: automatically bring up DSA master when opening user port"), but user space still has code to deal with this for older kernels. See:
https://github.com/systemd/systemd/blob/v262.2/src/network/networkd-setlink.c#L1081-L1082
and I'm hoping other network managers than systemd-networkd have similar code as well.
Quoted 5 lines
So I'm just proposing to play dumb for a while in stable kernels, return the
-ENETDOWNthat used to be established practice, and buy some time to do a more proper reordering of the setup/teardown in net-next, which gives us the guarantee that we need thatconduit->dsa_ptris a valid pointer for the entire registered lifetime of the user ports.
Thanks,
Aristo
Quoted 35 lines · Aristo Chen
DSA registers the user ports in
dsa_tree_setup_ports()before it setsconduit->dsa_ptrindsa_tree_setup_conduit(). On teardown, it clearsconduit->dsa_ptrindsa_tree_teardown_conduit()beforedsa_tree_teardown_ports()removes the user ports. RTNL is not held across either pair of steps, so user space can open a user port whileconduit->dsa_ptris NULL.In NPI mode,
felix_port_enable()callsfelix_cpu_port_for_conduit(). For a physical conduit, that function reads the CPU port index throughconduit->dsa_ptrwithout checking for NULL. This caused an oops on an LS1028A RDB when systemd-networkd tried to open a user port while the switch was being probed again at runtime:[ 166.722423] Unable to handle kernel access to user memory outside uaccess routines at virtual address 0000000000000028 [ 166.816908] Internal error: Oops: 0000000096000004 [#1] SMP [ 166.961766] pc : felix_cpu_port_for_conduit.isra.0+0x90/0xb8 [mscc_felix_dsa_lib] [ 166.969289] lr : felix_port_enable+0x120/0x1a0 [mscc_felix_dsa_lib]The oops left RTNL locked. Later operations that waited for RTNL hung, and the board had to be power cycled.
NPI mode uses one CPU port for the whole switch. Changing the conduit of a user port can move the NPI port to another CPU port, and the check in
felix_port_enable()keeps user ports that still use the old CPU port down.For a physical conduit, compare
dp->cpu_dp->indexwith the NPI port instead. DSA setsdp->cpu_dpbefore it registers the user port and clears it only after it removes the user port, so the check keeps working whileconduit->dsa_ptris NULL. As long as the physical conduit is set up,conduit->dsa_ptranddp->cpu_dppoint to the same CPU port, so the check gives the same result as before. LAG conduits keep usingfelix_cpu_port_for_conduit(), which does not readdsa_ptrfor them.
Quoted 3 lines
Fixes eca70102cfb1 ("net: dsa: felix: add support for changing DSA master")Signed-off-by Aristo Chen <[email protected]>---
As discussed, this is a patch that treats the symptom and not the cause. It should only be a stopgap for net. The larger problem of conduit->dsa_ptr being NULL while the user ports are open still remains unaddressed.
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 8 lines · On Thu, 19 Nov 2026 10:07:10 +0000 you wrote:
DSA registers the user ports in
dsa_tree_setup_ports()before it setsconduit->dsa_ptrindsa_tree_setup_conduit(). On teardown, it clearsconduit->dsa_ptrindsa_tree_teardown_conduit()beforedsa_tree_teardown_ports()removes the user ports. RTNL is not held across either pair of steps, so user space can open a user port whileconduit->dsa_ptris NULL.[...]
Here is the summary with links:
- [net] net: dsa: felix: fix NULL pointer dereference in felix_port_enable()
https://git.kernel.org/netdev/net/c/469b61e4be88
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html