mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net: dsa: felix: fix NULL pointer dereference in felix_port_enable()
@ 2026-11-19 10:07 Aristo Chen
  2026-11-19 11:59 ` Vladimir Oltean
  0 siblings, 1 reply; 2+ messages in thread
From: Aristo Chen @ 2026-11-19 10:07 UTC (permalink / raw)
  To: Vladimir Oltean, UNGLinuxDriver, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: netdev, linux-kernel, Aristo Chen

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.

Fixes: eca70102cfb1 ("net: dsa: felix: add support for changing DSA master")
Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
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(-)

diff --git a/drivers/net/dsa/ocelot/felix.c b/drivers/net/dsa/ocelot/felix.c
index c99fd3d23bb3..76aabb8e2887 100644
--- a/drivers/net/dsa/ocelot/felix.c
+++ b/drivers/net/dsa/ocelot/felix.c
@@ -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;
 		}
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH net] net: dsa: felix: fix NULL pointer dereference in felix_port_enable()
  2026-11-19 10:07 [PATCH net] net: dsa: felix: fix NULL pointer dereference in felix_port_enable() Aristo Chen
@ 2026-11-19 11:59 ` Vladimir Oltean
  0 siblings, 0 replies; 2+ messages in thread
From: Vladimir Oltean @ 2026-11-19 11:59 UTC (permalink / raw)
  To: Aristo Chen
  Cc: UNGLinuxDriver, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, netdev, linux-kernel

Hi Aristo,

On Thu, Nov 19, 2026 at 10:07:10AM +0000, Aristo Chen wrote:
> DSA registers the user ports in dsa_tree_setup_ports() before it sets
> conduit->dsa_ptr in dsa_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.

> 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.

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}").

> 
> 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:

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)?

diff --git a/net/dsa/user.c b/net/dsa/user.c
index 041f9060c8ef..c68ed0389404 100644
--- a/net/dsa/user.c
+++ b/net/dsa/user.c
@@ -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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19 11:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 10:07 [PATCH net] net: dsa: felix: fix NULL pointer dereference in felix_port_enable() Aristo Chen
2026-11-19 11:59 ` Vladimir Oltean

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®