net: dsa: qca8k: fix the MIB autocast and management frame handlers
v3 cover net: dsa: qca8k: fix the MIB autocast and management frame handlers
The netdev AI review of the MIB statistics series [1] found bugs in the qca8k handlers of the Ethernet management and MIB autocast frames, and its review of v1 of this series [2] found two more. None of them has been seen in production, so as Andrew asked for the related MTU series [3], this goes to net-next rather than net. All four keep their Fixes tags.
1. qca8k_bulk_read() falls back to MDIO when the management path is not available, but stores register i at val_buf + i with val_buf a void pointer, so every multi-register read is garbage: the FDB dump and the MDB add/del write the wrong port mask back into the ATU.
2. An ethtool MIB request over Ethernet (autocast) that times out leaves the caller's buffer published; late frames write into it after ethtool has freed it, and extra frames underflow the frame counter. v2 also handles the frames of an earlier autocast arriving during the next request.
3. The autocast handler reads up to 164 bytes of counters without checking the frame length.
4. The management ack handler trusts the frame for both the length and the layout: up to 32 bytes into a 16-byte buffer, read from skb->data without checking that the frame has them.
Changes in v3:
- Rebase on current net-next; the code is unchanged.
- 1/4: the description said that the last three bytes of a three-register read are never written; it is the last six (Alexey Immoreev) [5].
Changes in v2:
- Drop "let regmap serialize register access" (v1 1/3): Yongzhao Chen carries it, unchanged, as 1/2 of his net-next v8 "preserve port state across MTU changes" [4]; this series does not depend on it.
- New 1/4: the MDIO fallback of qca8k_bulk_read() (review of v1 1/3).
- 2/4: poll MIB_BUSY before the next autocast, as qca8k_mib_init() does after a flush, and track the ports seen as a bitmap instead of a countdown (review of v1 2/3). The description now says that a frame of the previous autocast still queued on the host can serve the next request, with counters one autocast old, instead of claiming that every late frame is dropped.
- 3/4 unchanged (v1 3/3).
- New 4/4: bound the data of a management ack frame (review of v1 3/3).
- Target net-next; describe the LLM assistance here and in the commit messages instead of an Assisted-by tag.
Testing: the affected objects build with W=1 on arm64 on this base. The hardware test is the one of v2, the code being the same: on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000), kernel 6.18.60 with these four patches, under tag_qca with the management conduit on port 0, counted on the conduit: one ethtool -S costs 3 management frames up and 10 down (the MIB_BUSY poll, the cast, the 7 autocast frames and acks); with the request timeout forced to 0 by a test-only module parameter it costs 50 up and 57 down, i.e. the register fallback over management frames plus the 7 late autocast frames, which the handler drops. The counters stayed monotonic over 2000 such reads and over 5x200 reads under iperf3 in both directions while toggling the timeout, with no memory growth and no splat. Under a tagger without management frames (qca-8021q, OpenWrt) the MDIO fallback of 1/4 was checked by the FDB dump.
LLM assistance: Claude Code (Claude Opus 5.5 for v1, Claude Fable 5.1 for v2, Claude Fable 5.2 for the v3 rebase) drafted the code and the descriptions from my analysis and the review findings, and helped work out the race in 2/4. I reviewed every line, built the series and ran the tests above. The review findings themselves came from Sashiko, the netdev AI review bot.
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
[3] https://lore.kernel.org/netdev/[email protected]/
[4] https://lore.kernel.org/netdev/[email protected]/
[5] https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
Stanislaw Pal (4):
net: dsa: qca8k: read each register into its own word over MDIO
net: dsa: qca8k: drop MIB autocast frames once the request is over
net: dsa: qca8k: check the length of MIB autocast frames
net: dsa: qca8k: bound the data of a management ack frame
drivers/net/dsa/qca/qca8k-8xxx.c | 81 ++++++++++++++++++++++++++---- drivers/net/dsa/qca/qca8k-common.c | 2 +- drivers/net/dsa/qca/qca8k.h | 9 ++-- 3 files changed, 77 insertions(+), 15 deletions(-)
base-commit: d6816d9a93b66a12bf01e10037e377f3333ad6cc
Signature · 1 line
2.43.0
PATCH v3 1/4 net: dsa: qca8k: read each register into its own word over MDIO
qca8k_bulk_read() is the regmap read op. When the Ethernet management path is not available it falls back to reading the registers one by one over MDIO, but it stores register i at val_buf + i with val_buf a void
pointer: the words land one byte apart and overwrite each other, and the last six bytes of a three-register read are never written.
Every multi-register read over MDIO is garbage then. qca8k_fdb_read() reads the three ATU registers this way, so the FDB dump shows MAC addresses made of the VID and the aging field, the search reads a wrong status and port mask for the entry it found, and qca8k_port_mdb_add() and qca8k_port_mdb_del() write that port mask back into the ATU. With tag_qca the management frames carry these reads and the fallback is rarely taken; it is taken every time when the management conduit is down, when a management read fails, and on boards whose CPU port is not port 0, where qca8k_conduit_change() never sets up the management conduit.
Store each register in its own u32.
The bug was found while bringing up a tagger without management frames; an LLM assistant (Claude, claude-fable-5-1) helped write the fix and this description. I reviewed both and tested the fix on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000), where bridge fdb show returns the right entries with it and garbage without.
--- drivers/net/dsa/qca/qca8k-8xxx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dsa/qca/qca8k-8xxx.c +1 -1
@@ -519,7 +519,7 @@ qca8k_bulk_read(void *ctx, const void *reg_buf, size_t reg_len,/* loop count times and increment reg of 4 */ for (i =0 ; i < count; i++, reg +=sizeof (u32)) { - ret =qca8k_read_mii (priv, reg, val_buf + i); + ret =qca8k_read_mii (priv, reg, (u32 *)val_buf + i);if (ret <0 )return ret; }
Signature · 1 line
2.43.0
PATCH v3 2/4 net: dsa: qca8k: drop MIB autocast frames once the request is over
In reply to Stanislaw Palqca8k_get_ethtool_stats_eth() points mib_eth_data at the caller's ethtool buffer, asks the switch for an autocast and waits 5 ms for the frames. On a timeout it returns with the request still published, and the caller falls back to register reads; ethtool_get_stats() then frees the buffer. The autocast handler runs from the conduit's receive path without the request's mutex, so a frame that arrives late still writes the requested port's counters through mib_eth_data->data into freed memory, and frames beyond the seven expected ones underflow port_parsed. ETHTOOL_GSTATS does not need CAP_NET_ADMIN.
Protect the request with a spinlock that the handler takes, retire it on the way out, and let the handler retire it when the last frame completes it, so that frames arriving after that are dropped without touching data.
An autocast frame carries no identity of the request it answers. Once a request has timed out and the next one is published, the switch may still be sending the earlier autocast, and its frames count for the new request. Wait for MIB_BUSY to clear before asking for the next autocast, as qca8k_mib_init() does after a flush, so that at most the frames already on their way to the host can be taken for the new request, and track the ports seen as a bitmap instead of a countdown, so that a second frame of a port cannot complete the request before the other ports have reported. The requested port's counters then come from the newest frame of that port, which is at most one autocast behind - the same as a register read racing the hardware.
An LLM assistant (Claude, claude-fable-5-1) helped analyse the race and write the change and this description; I reviewed both. Built with W=1 on arm64; tested on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000) with tag_qca, with the request timeout forced to 0 to exercise the late frame path.
--- drivers/net/dsa/qca/qca8k-8xxx.c | 47 ++++++++++++++++++++++++++---- drivers/net/dsa/qca/qca8k-common.c | 2 +- drivers/net/dsa/qca/qca8k.h | 6 ++-- 3 files changed, 47 insertions(+), 8 deletions(-)
drivers/net/dsa/qca/qca8k-8xxx.c +42 -5
@@ -1676,10 +1676,18 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *sk mib_ethhdr = (struct mib_ethhdr *)skb_mac_header (skb); mib_eth_data = &priv->mib_eth_data; +spin_lock (&mib_eth_data->lock); + +/* A late frame of a request that has already returned */ +if (!mib_eth_data->data) + goto unlock; +/* The switch autocast every port. Ignore other packet and * parse only the requested one. */ port =FIELD_GET (QCA_HDR_RECV_SOURCE_PORT ,ntohs (mib_ethhdr->hdr)); +if (port >=QCA8K_NUM_PORTS ) + goto unlock;if (port != mib_eth_data->req_port) goto exit;
@@ -1704,9 +1712,17 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *sk } exit: -/* Complete on receiving all the mib packet */ -if (refcount_dec_and_test (&mib_eth_data->port_parsed)) +/* Complete once a frame of every port has arrived. A second frame +* of a port, left over from an earlier autocast, cannot end the +* request early. +*/ + mib_eth_data->ports_seen |=BIT (port); +if (mib_eth_data->ports_seen ==GENMASK (QCA8K_NUM_PORTS -1 ,0 )) { + mib_eth_data->data =NULL ;complete (&mib_eth_data->rw_done); + } +unlock: +spin_unlock (&mib_eth_data->lock); } static int
@@ -1721,13 +1737,25 @@ qca8k_get_ethtool_stats_eth(struct dsa_switch *ds, int port, u64 *data)mutex_lock (&mib_eth_data->mutex); +mutex_lock (&priv->reg_mutex); + +/* A request that timed out may have left the switch still sending +* its autocast. Let it finish before asking for another one, so +* that as few of its frames as possible are taken for ours. +*/ + ret =qca8k_busy_wait (priv,QCA8K_REG_MIB ,QCA8K_MIB_BUSY ); +if (ret) { +mutex_unlock (&priv->reg_mutex); + goto out; + } +reinit_completion (&mib_eth_data->rw_done); +spin_lock_bh (&mib_eth_data->lock); mib_eth_data->req_port = dp->index; mib_eth_data->data = data; -refcount_set (&mib_eth_data->port_parsed,QCA8K_NUM_PORTS ); - -mutex_lock (&priv->reg_mutex); + mib_eth_data->ports_seen =0 ; +spin_unlock_bh (&mib_eth_data->lock);/* Send mib autocast request */ ret = regmap_update_bits(priv->regmap, QCA8K_REG_MIB,
@@ -1743,6 +1771,14 @@ qca8k_get_ethtool_stats_eth(struct dsa_switch *ds, int port, u64 *data) ret =wait_for_completion_timeout (&mib_eth_data->rw_done,QCA8K_ETHERNET_TIMEOUT ); exit: +/* On a timeout the frames can still arrive after the caller has +* freed data: retire the request so that the handler drops them. +*/ +spin_lock_bh (&mib_eth_data->lock); + mib_eth_data->data =NULL ; +spin_unlock_bh (&mib_eth_data->lock); + +out:mutex_unlock (&mib_eth_data->mutex);return ret;
@@ -2120,6 +2156,7 @@ qca8k_sw_probe(struct mdio_device *mdiodev)init_completion (&priv->mgmt_eth_data.rw_done);mutex_init (&priv->mib_eth_data.mutex); +spin_lock_init (&priv->mib_eth_data.lock);init_completion (&priv->mib_eth_data.rw_done); priv->ds->dev = &mdiodev->dev;
drivers/net/dsa/qca/qca8k-common.c +1 -1
@@ -101,7 +101,7 @@ const struct regmap_access_table qca8k_readable_table = { .n_yes_ranges = ARRAY_SIZE(qca8k_readable_ranges), }; -static int qca8k_busy_wait (struct qca8k_priv *priv,u32 reg,u32 mask) +int qca8k_busy_wait (struct qca8k_priv *priv,u32 reg,u32 mask) {u32 val;
drivers/net/dsa/qca/qca8k.h +4 -2
@@ -402,9 +402,10 @@ struct qca8k_mgmt_eth_data {struct qca8k_mib_eth_data {struct completion rw_done;struct mutex mutex;/* Process one command at time */ -refcount_t port_parsed;/* Counter to track parsed port */ +spinlock_t lock;/* Protects the request against the rx handler */ +unsigned long ports_seen;/* Ports whose frame arrived, under lock */ u8 req_port; -u64 *data;/* pointer to ethtool data */ +u64 *data;/* pointer to ethtool data, NULL with no request pending */ };struct qca8k_ports_config {
@@ -501,6 +502,7 @@ static inline u32 qca8k_port_to_phy(int port)/* Common setup function */ extern const struct qca8k_mib_desc ar8327_mib[];extern const struct regmap_access_table qca8k_readable_table; +int qca8k_busy_wait (struct qca8k_priv *priv,u32 reg,u32 mask);int qca8k_mib_init (struct qca8k_priv *priv);void qca8k_port_set_status (struct qca8k_priv *priv,int port,int enable);int qca8k_read_switch_id (struct qca8k_priv *priv);
Signature · 1 line
2.43.0
PATCH v3 3/4 net: dsa: qca8k: check the length of MIB autocast frames
In reply to Stanislaw Palqca_tag_rcv() makes only the 2 byte tag linear before it hands a MIB frame to qca8k_mib_autocast_handler(). The handler then reads the counters that follow the Ethernet header, up to 164 bytes on QCA833x, straight from skb->data, without checking the frame length. A short or non-linear frame makes it read past the packet data.
Pull the counters into the linear area before reading them. A frame that is too short is left uncounted, so the request times out and the caller reads the counters from the registers instead.
An LLM assistant (Claude, claude-fable-5-1) helped write the change and this description; I reviewed both and built them with W=1 on arm64.
--- drivers/net/dsa/qca/qca8k-8xxx.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/net/dsa/qca/qca8k-8xxx.c +11 -0
@@ -1669,6 +1669,7 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *skstruct qca8k_priv *priv = ds->priv;const struct qca8k_mib_desc *mib;struct mib_ethhdr *mib_ethhdr; +unsigned int len =0 ;__le32 *data2;u8 port;int i;
@@ -1691,6 +1692,16 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *skif (port != mib_eth_data->req_port) goto exit; +/* Past the first 3, the counters follow the header. Leave a short +* frame uncounted, so that the request times out and the caller +* reads the registers instead. +*/ +for (i =3 ; i < priv->info->mib_count; i++) + len += ar8327_mib[i].size *sizeof (u32); +if (!pskb_may_pull (skb, len)) + goto unlock; + + mib_ethhdr = (struct mib_ethhdr *)skb_mac_header (skb); data2 = (__le32 *)skb->data;for (i =0 ; i < priv->info->mib_count; i++) {
Signature · 1 line
2.43.0
PATCH v3 4/4 net: dsa: qca8k: bound the data of a management ack frame
In reply to Stanislaw Palqca8k_rw_reg_ack_handler() answers a management read by copying the data that follows the Ethernet header into mgmt_eth_data->data. It trusts the frame for both the length and the layout: the length field of the frame allows up to 32 bytes while the buffer holds 16, so a frame announcing more than three registers writes past the buffer into the fields that follow it in qca8k_priv; and qca_tag_rcv() made only the 2 byte tag linear, so the copy reads skb->data without checking that the frame has the bytes it announces. The handler runs for every ack frame, whatever its sequence number.
Size the buffer to what a management frame can carry, and pull the announced data into the linear area before reading it. A frame that is too short is left unacknowledged, so the read fails and the caller falls back to MDIO.
An LLM assistant (Claude, claude-fable-5-1) helped write the change and this description; I reviewed both and built them with W=1 on arm64. The management path was exercised on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000) with tag_qca.
--- drivers/net/dsa/qca/qca8k-8xxx.c | 21 ++++++++++++++++----- drivers/net/dsa/qca/qca8k.h | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-)
drivers/net/dsa/qca/qca8k-8xxx.c +16 -5
@@ -170,6 +170,7 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb)struct qca8k_mgmt_eth_data *mgmt_eth_data;struct qca8k_priv *priv = ds->priv;struct qca_mgmt_ethhdr *mgmt_ethhdr; +int data_len =0 ;u32 command;u8 len, cmd;int i;
@@ -190,6 +191,17 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb)/* We can ignore odd value, we always round up them in the alloc function. */ len *=sizeof (u16); +if (cmd ==MDIO_READ && len >QCA_HDR_MGMT_DATA1_LEN ) + data_len =min_t (int,QCA_HDR_MGMT_DATA2_LEN , + len -QCA_HDR_MGMT_DATA1_LEN ); + +/* The tagger made only the tag linear. Leave a frame that is too +* short for the data it announces unacknowledged: the read fails +* and the caller falls back to MDIO. +*/ +if (data_len && !pskb_may_pull (skb, data_len)) + goto complete; +/* Make sure the seq match the requested packet */ if (get_unaligned_le32 (&mgmt_ethhdr->seq) == mgmt_eth_data->seq) mgmt_eth_data->ack = true;
@@ -199,13 +211,11 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) *val =get_unaligned_le32 (&mgmt_ethhdr->mdio_data); -/* Get the rest of the 12 byte of data. -* The read/write function will extract the requested data. +/* Get the rest of the data. +* The read function extracts what it asked for. */ -if (len >QCA_HDR_MGMT_DATA1_LEN ) { +if (data_len) {__le32 *data2 = (__le32 *)skb->data; -int data_len =min_t (int,QCA_HDR_MGMT_DATA2_LEN , - len -QCA_HDR_MGMT_DATA1_LEN ); val++;
@@ -217,6 +227,7 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) } } +complete:complete (&mgmt_eth_data->rw_done); }
drivers/net/dsa/qca/qca8k.h +2 -1
@@ -396,7 +396,8 @@ struct qca8k_mgmt_eth_data {struct mutex mutex;/* Enforce one mdio read/write at time */ bool ack;u32 seq; -u32 data[4 ]; +/* The data a management frame can carry */ +u32 data[(QCA_HDR_MGMT_DATA1_LEN +QCA_HDR_MGMT_DATA2_LEN ) /sizeof (u32)]; };struct qca8k_mib_eth_data {
Signature · 1 line
2.43.0
Re: [PATCH net-next v3 4/4] net: dsa: qca8k: bound the data of a management ack frame
drivers/net/dsa/qca/qca8k-8xxx.c static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) · line 206
Quoted 5 lines · Stanislaw Pal
drivers/net/dsa/qca/qca8k-8xxx.c +3
-0+if (data_len && !pskb_may_pull (skb, data_len)) + goto complete; +/* Make sure the seq match the requested packet */ if (get_unaligned_le32 (&mgmt_ethhdr->seq) == mgmt_eth_data->seq)
This is wrong: mgmt_ethhdr is taken from skb_mac_header() before the pull, and pskb_may_pull() can move the head, so ->seq and ->mdio_data may then be read from the old one. 3/4 takes mib_ethhdr again after its pull; this one has to do the same. I will send v4 with that, the rest unchanged, once v3 has been on the list for a day.
pw-bot: cr
[email protected]
AI reviewRe: [PATCH net-next v3 2/4] net: dsa: qca8k: drop MIB autocast frames once the request is over
In reply to Stanislaw PalThank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] In qca8k_get_ethtool_stats_eth() (drivers/net/dsa/qca/qca8k-8xxx.c), the patch now calls…
--
net: dsa: qca8k: drop MIB autocast frames once the request is over
This patch adds a spinlock shared by qca8k_get_ethtool_stats_eth() and the rx handler to protect the MIB autocast request. The request is retired (data set to NULL) on exit or once every port has reported, so late frames no longer write into the freed ethtool buffer. It also replaces the port_parsed refcount with a ports_seen bitmap, and waits for MIB_BUSY to clear before sending a new autocast request.
Quoted 4 lines
drivers/net/dsa/qca/qca8k-8xxx.c +0
-0
[ ... ]
Quoted 5 lines
drivers/net/dsa/qca/qca8k-8xxx.c +1
-0@@ -1721,13 +1737,25 @@ qca8k_get_ethtool_stats_eth(struct dsa_switch *ds, int port, u64 *data)mutex_lock (&mib_eth_data->mutex); +mutex_lock (&priv->reg_mutex);
drivers/net/dsa/qca/qca8k-8xxx.c qca8k_get_ethtool_stats_eth(struct dsa_switch *ds, int port, u64 *data) · line 1750
Quoted 10 lines
drivers/net/dsa/qca/qca8k-8xxx.c +10
-0+ +/* A request that timed out may have left the switch still sending +* its autocast. Let it finish before asking for another one, so +* that as few of its frames as possible are taken for ours. +*/ + ret =qca8k_busy_wait (priv,QCA8K_REG_MIB ,QCA8K_MIB_BUSY ); +if (ret) { +mutex_unlock (&priv->reg_mutex); + goto out; + }
[Severity: Low]
Is it known when the switch clears MIB_BUSY for the QCA8K_MIB_CAST function?
The commit message says:
Wait for MIB_BUSY to clear before asking for the next autocast, as qca8k_mib_init() does after a flush, so that at most the frames already on their way to the host can be taken for the new request
That is only true if BUSY stays set until the switch has sent every autocast frame. The only other user, qca8k_mib_init(), waits on BUSY after QCA8K_MIB_FLUSH, and a flush sends no frames. Neither the driver nor commit 5c957c7ca78c says how BUSY behaves for CAST.
Suppose BUSY clears as soon as the counters are captured. Could all seven frames of a timed-out autocast then arrive after the new request is published, and fill ports_seen on their own?
There is also a cost when BUSY never clears, for example if the MIB engine is wedged or the CPU port cannot transmit. qca8k_busy_wait() polls with sleep_us = 0 and a QCA8K_BUSY_WAIT_TIMEOUT of 2000 ms:
return regmap_read_poll_timeout(priv->regmap, reg, val, !(val & mask), 0,
QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC);Here it runs with both mib_eth_data->mutex and priv->reg_mutex held. Would each ETHTOOL_GSTATS call then poll back to back for up to 2 s before falling back to register reads? Other reg_mutex users would be blocked for that time. Before this patch, the same case cost one 5 ms completion timeout per call.
Quoted 11 lines
drivers/net/dsa/qca/qca8k-8xxx.c +3
-3reinit_completion (&mib_eth_data->rw_done); +spin_lock_bh (&mib_eth_data->lock); mib_eth_data->req_port = dp->index; mib_eth_data->data = data; -refcount_set (&mib_eth_data->port_parsed,QCA8K_NUM_PORTS ); - -mutex_lock (&priv->reg_mutex); + mib_eth_data->ports_seen =0 ; +spin_unlock_bh (&mib_eth_data->lock);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118104104.26684-1-kuncy7%40gmail.com
[email protected]
AI reviewRe: [PATCH net-next v3 4/4] net: dsa: qca8k: bound the data of a management ack frame
In reply to Stanislaw PalThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 2
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] Use-after-free introduced by the new pskb_may_pull() in qca8k_rw_reg_ack_handler() (drivers/net/dsa/qca/qca8k-8xxx.c).
- [Low] The commit message says that with the old u32 data[4] a frame "announcing more than three registers writes past the buffer".
- [Low] The new comment in qca8k_rw_reg_ack_handler(), "The tagger made only the tag linear", and the matching commit-message line…
--
net: dsa: qca8k: bound the data of a management ack frame
This patch resizes mgmt_eth_data->data to the largest payload a management frame can carry. Before qca8k_rw_reg_ack_handler() copies the announced DATA2 payload, it now pulls that payload into the linear area with pskb_may_pull(). A frame that is too short for its announced length is left unacknowledged.
Quoted 6 lines
qca8k_rw_reg_ack_handler()answers a management read by copying the data that follows the Ethernet header intomgmt_eth_data->data. It trusts the frame for both the length and the layout: the length field of the frame allows up to 32 bytes while the buffer holds 16, so a frame announcing more than three registers writes past the buffer into the fields that follow it inqca8k_priv; andqca_tag_rcv()made only
[Severity: Low]
Is the threshold here off by one? With the old u32 data[4], data[0] is filled from mdio_data, and the loop then writes data_len / 4 more words:
for (i = sizeof(u32); i <= data_len; i += sizeof(u32)) {For len = 16 (four registers), data_len is 12, so three more words are written and data[0..3] is filled exactly. len = 18 also gives three words. Only at len = 20 (five registers) does data_len = 16 reach data[4].
So does the old overflow start at more than four registers, not more than three?
Quoted 4 lines
drivers/net/dsa/qca/qca8k-8xxx.c +0
-0
[ ... ]
drivers/net/dsa/qca/qca8k-8xxx.c static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) · line 198
Quoted 9 lines
drivers/net/dsa/qca/qca8k-8xxx.c +5
-0@@ -190,6 +191,17 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb)/* We can ignore odd value, we always round up them in the alloc function. */ len *=sizeof (u16); +if (cmd ==MDIO_READ && len >QCA_HDR_MGMT_DATA1_LEN ) + data_len =min_t (int,QCA_HDR_MGMT_DATA2_LEN , + len -QCA_HDR_MGMT_DATA1_LEN ); + +/* The tagger made only the tag linear. Leave a frame that is too
[Severity: Low]
Is this comment accurate? qca_tag_rcv() in net/dsa/tag_qca.c does:
if (unlikely(!pskb_may_pull(skb, QCA_HDR_LEN))) {
...
phdr = dsa_etype_header_pos_rx(skb);That pull makes QCA_HDR_LEN bytes linear starting at skb->data. For a management ack those are the first two bytes of the DATA2 payload. The tag itself is read at skb->data - 2, inside the MAC header, which was already linear.
The commit message uses the same wording: "qca_tag_rcv() made only the 2 byte tag linear".
DATA2 still needs a pull, so that conclusion holds. Could the comment and changelog say which bytes the tagger actually guarantees?
drivers/net/dsa/qca/qca8k-8xxx.c static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) · line 207
Quoted 9 lines
drivers/net/dsa/qca/qca8k-8xxx.c +6
-0+* short for the data it announces unacknowledged: the read fails +* and the caller falls back to MDIO. +*/ +if (data_len && !pskb_may_pull (skb, data_len)) + goto complete; +/* Make sure the seq match the requested packet */ if (get_unaligned_le32 (&mgmt_ethhdr->seq) == mgmt_eth_data->seq) mgmt_eth_data->ack = true;
[Severity: High]
Can this new pskb_may_pull() leave mgmt_ethhdr pointing into a freed skb head? The function caches the pointer before the pull:
mgmt_ethhdr = (struct qca_mgmt_ethhdr *)skb_mac_header(skb);
data_len can be up to 28 bytes. If it is larger than skb_headlen(skb) and the head lacks tailroom, the call chain is:
pskb_may_pull()
__pskb_pull_tail()
pskb_expand_head()pskb_expand_head() allocates a new head and frees the old one. dsa_switch_rcv() has already run skb_unclone_full(), so no clone keeps the old head alive.
mgmt_ethhdr is never reloaded after the pull. The read of mgmt_ethhdr->seq here, and the read of mgmt_ethhdr->mdio_data below, would then come from the freed buffer.
A garbage seq could set mgmt_eth_data->ack = true. The garbage mdio_data would land in mgmt_eth_data->data[0], which qca8k_read_eth() returns as the register value.
The kernel-doc for __pskb_pull_tail() says "All the pointers pointing into skb header may change and must be reloaded after call to this function". The sibling handler qca8k_mib_autocast_handler() in this file reloads its pointer after the pull:
if (!pskb_may_pull(skb, len))
goto unlock;
mib_ethhdr = (struct mib_ethhdr *)skb_mac_header(skb);Should mgmt_ethhdr be re-read after a successful pull here? The other option is to read seq and mdio_data into locals before the pull, as is already done for command.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118104104.26684-1-kuncy7%40gmail.com