mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aristo Chen <aristo.chen@canonical.com>
To: Vladimir Oltean <vladimir.oltean@nxp.com>,
	UNGLinuxDriver@microchip.com, Andrew Lunn <andrew@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Aristo Chen <aristo.chen@canonical.com>
Subject: [PATCH net] net: dsa: felix: fix NULL pointer dereference in felix_port_enable()
Date: Thu, 19 Nov 2026 10:07:10 +0000	[thread overview]
Message-ID: <20261119100710.129379-1-aristo.chen@canonical.com> (raw)

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


             reply	other threads:[~2026-11-19 10:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 10:07 Aristo Chen [this message]
2026-11-19 11:59 ` Vladimir Oltean

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119100710.129379-1-aristo.chen@canonical.com \
    --to=aristo.chen@canonical.com \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=vladimir.oltean@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®