Skip to contentSkip to conversation

net: wwan: iosm: validate MUX DL lengths and offsets

PATCH New Fang Xieyan 2 participants 9 messages Nov 21 – Nov 25

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

v4 cover net: wwan: iosm: validate MUX DL lengths and offsets

The MUX downlink decoders trust several modem supplied lengths and offsets without bounding them against what actually arrived: the block size from the transfer descriptor, the ADGH datagram length, the ACBH block length, the command chain offsets and the declared parameter length. A truncated or enlarged DL block makes the driver read past the received data, hand the network stack an undeclared payload, or walk a command chain that loops or redecodes the same bytes. The aggregated datagram block path is already covered by commit 526b8ef54668 ("net:
wwan: iosm: bound device offsets in the MUX downlink decoder") and is not touched here.

This series adds those bounds. No command type, response or protocol semantics change, so a conforming modem behaves exactly as before. A parameter too short for the member its command type reads is dropped with -EMSGSIZE, which also ends an aggregated block at that command. Patch 4 is the prep change for patch 5; both carry Cc: stable, as patch 5 cannot be backported without patch 4.

cmd_len is not a parameter bound in either path. In an aggregated block the boundary is next_cmd_index, or the end of the received block for the last command. In MUX Lite there is one command per skb, so the received length is already everything that command owns; the flow control decoder treats a mismatching cmd_len as a legacy device, so the field is a compatibility flag, not a length bound.

Patch 3 bounds the ACBH command chain by the received length rather than block_length. Although the structure describes block_length as the length of the aggregated command block, the uplink encoder initializes it to the ACBH header size and does not update it as commands are appended, so it cannot delimit the received command chain. The field is sanity checked, while first_cmd_index and next_cmd_index are bounded against skb->len. The walk's array_index_nospec() call is removed: it bounded a byte offset by sizeof(struct mux_cmdh), zeroing every legal offset past that size. The offset indexes no array; array_index_nospec() is kept only for if_id, an actual array index.

Patch 2 relies on the ADGH header check from patch 1, patch 5 on the ACBH validation from patch 3 and the parameter pointer from patch 4. Every commit applies and builds individually, W=1 drivers/net/wwan/iosm/ is clean. Found by reviewing the DL decode path against the MUX protocol structures, not reproduced on a live modem: reaching it needs a device that truncates or enlarges its own downlink blocks. The series is based on net at 92e97d7cffc7.

Changes in v4:

- Patch 1 keeps the skb->data test as context again, so the series applies to the net tree, where its removal (66974df65a0b) has not landed. The added length check is unchanged and sits after the test; the two edits do not overlap and merge cleanly once the removal reaches net. Patches 2-5 are unchanged.

Changes in v3:

- Rebase onto net-next (9068e05d35f3) so the series applies on top of 66974df65a0b ("net: wwan: iosm: drop redundant skb->data check in MUX DL decode"). Patch 1 no longer anchors its signature-length check on the removed skb->data test; the check now sits directly before the signature read. This clears the v2 patchwork context failure at iosm_ipc_mux_codec.c:696 and the dependent context-hw gate failure.

- Patch 5 wraps the over-long continuation line in ipc_mux_dl_cmd_decode(): param_len now goes on its own line, clearing the checkpatch "line length of 82 exceeds 80 columns" warning.

- Cc Guanglei Zhu, whom get_maintainer.pl now lists for this file after his review of 66974df65a0b (the v2 cc_maintainers warning).

Changes in v2, all three points from the Sashiko AI review of v1:

- Patch 5 bounds the MUX Lite parameter by the received length alone, not by cmd_len: v1 dropped legacy FLOW_CTL (cmd_len 16..19) and OPEN_SESSION_RESP (16..27) even when the parameter bytes had arrived, defeating the backward compatibility branch in ipc_mux_dl_cmds_decode_process() that exists for exactly these (the protocol minimum for cmd_len is 0x10).

- Patch 2 rejects adgh_len == packet_offset too: the zero length clone reached ipc_wwan_receive(), which reads skb->data[0] without checking skb->len. mux_dl_process_dg() already rejects the same case for the aggregated path with dl_head_pad_len >= dg_len.

- Patch 4 gains Fixes: 9413491e20e1 and Cc: [email protected]: its by-value signature copied all twelve union bytes even for a four byte response, and patch 5 bounds the session responses, so the two are needed together. A prerequisite itself marked for stable and present in the series need not be listed, per stable-kernel-rules.rst.

v2:
https://lore.kernel.org/netdev/[email protected]/

v1:
https://lore.kernel.org/netdev/[email protected]/

Fang Xieyan (5):
net: wwan: iosm: validate the MUX DL block length
net: wwan: iosm: validate the ADGH datagram length
net: wwan: iosm: bound the ACBH command chain walk
net: wwan: iosm: pass the DL command parameter by pointer
net: wwan: iosm: validate the command parameter length

 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c | 186 ++++++++++++++++-----
 1 file changed, 141 insertions(+), 45 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/5 net: wwan: iosm: validate the MUX DL block length

The received length of a DL MUX block is supplied by the device and can be shorter than the header its signature advertises. ipc_mux_dl_decode() reads the four byte signature and then lets the decoders index their header structures without checking skb->len, so a short block makes the driver decode past the received data.

Check the signature before reading it, and check the header size of the ADGH, FCTH and CMDH branches before dispatching to their decoders. The ADB branch already validates its own headers, and the ACBH branch is covered by the bounds checks of its command chain walk. A block too short to carry its signature is dropped on the way to the single free.

Fixes 9413491e20e1 ("net: iosm: encode or decode datagram")
---
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +15 -0
index 0bbd41263cc2..5c33171ca701 100644
@@ -696,6 +696,11 @@ void ipc_mux_dl_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 	if (!skb->data)
 		return;
 
+	if (skb->len < sizeof(signature)) {
+		dev_err(ipc_mux->dev, "invalid MUX block length %u", skb->len);
+		goto free_skb;
+	}
+
 	/* Decode the MUX header type. */
 	signature = le32_to_cpup((__le32 *)skb->data);
 
@@ -704,15 +709,24 @@ void ipc_mux_dl_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 		mux_dl_adb_decode(ipc_mux, skb);
 		break;
 	case IOSM_AGGR_MUX_SIG_ADGH:
+		if (skb->len < sizeof(struct mux_adgh))
+			break;
+
 		ipc_mux_dl_adgh_decode(ipc_mux, skb);
 		break;
 	case MUX_SIG_FCTH:
+		if (skb->len < sizeof(struct ipc_mem_lite_gen_tbl))
+			break;
+
 		ipc_mux_dl_fcth_decode(ipc_mux, skb->data);
 		break;
 	case IOSM_AGGR_MUX_SIG_ACBH:	/* Aggregated Command Block Header */
 		ipc_mux_dl_acb_decode(ipc_mux, skb);
 		break;
 	case MUX_SIG_CMDH:
+		if (skb->len < offsetof(struct mux_lite_cmdh, param))
+			break;
+
 		ipc_mux_dl_cmd_decode(ipc_mux, skb);
 		break;
 
@@ -720,6 +734,7 @@ void ipc_mux_dl_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 		dev_err(ipc_mux->dev, "invalid ABH signature");
 	}
 
+free_skb:
 	ipc_pcie_kfree_skb(ipc_mux->pcie, skb);
 }
 
Signature · 1 line
2.50.1
#2 lore raw

PATCH v4 2/5 net: wwan: iosm: validate the ADGH datagram length

In reply to Fang Xieyan

ipc_mux_dl_adgh_decode() takes adgh->length from the device and hands adgh_len - packet_offset to the network stack as the payload size of the cloned skb, without bounding adgh_len. A length below the header plus its negotiated padding wraps the subtraction, skb_trim() keeps the clone unchanged and the stack gets the whole rest of the block. A length equal to packet_offset yields a zero length clone that ipc_wwan_receive() still reads skb->data[0] from.

Require the datagram length to exceed packet_offset and to fit into skb->len, and drop the block otherwise.

Fixes c34ca4f32c24 ("net: wwan: iosm: fix incorrect skb length")
---
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +9 -0
index 5c33171ca701..65421f6b4c91 100644
@@ -473,6 +473,15 @@ static void ipc_mux_dl_adgh_decode(struct iosm_mux *ipc_mux,
 	if_id += ipc_mux->wwan_q_offset;
 	adgh_len = le16_to_cpu(adgh->length);
 
+	/* The device supplied length bounds the datagram. It has to
+	 * exceed packet_offset and fit into the received block.
+	 */
+	if (adgh_len <= packet_offset || adgh_len > skb->len) {
+		dev_err(ipc_mux->dev, "invalid ADGH length %u, skb len %u",
+			adgh_len, skb->len);
+		return;
+	}
+
 	/* Pass the packet to the netif layer */
 	rc = ipc_mux_net_receive(ipc_mux, if_id, wwan, packet_offset,
 				 adgh->service_class, skb,
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH net v4 2/5] net: wwan: iosm: validate the ADGH datagram length

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…

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 drivers/net/wwan/iosm/iosm_ipc_mux_codec.h, the kernel-doc for struct mux_adgh describes @length as "Length (in bytes) of the…

--

net: wwan: iosm: validate the ADGH datagram length

This adds a bounds check on the device-supplied adgh->length in ipc_mux_dl_adgh_decode(). A datagram is now dropped if its length is not greater than packet_offset or is larger than skb->len. Before this, an underflowing payload length or a zero-length clone could be passed to the network stack.

Quoted 6 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +0 -0
index 5c33171ca7012..65421f6b4c91e 100644
@@ -473,6 +473,15 @@ static void ipc_mux_dl_adgh_decode(struct iosm_mux *ipc_mux,
 	if_id += ipc_mux->wwan_q_offset;

drivers/net/wwan/iosm/iosm_ipc_mux_codec.c static void ipc_mux_dl_adgh_decode(struct iosm_mux *ipc_mux, · line 483

Quoted 10 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +8 -0
 	adgh_len = le16_to_cpu(adgh->length);
 
+	/* The device supplied length bounds the datagram. It has to
+	 * exceed packet_offset and fit into the received block.
+	 */
+	if (adgh_len <= packet_offset || adgh_len > skb->len) {
+		dev_err(ipc_mux->dev, "invalid ADGH length %u, skb len %u",
+			adgh_len, skb->len);
+		return;
+	}

[Severity: Low]
This isn't a bug, but the new check disagrees with the kernel-doc for struct mux_adgh in iosm_ipc_mux_codec.h:

* @length: Length (in bytes) of the datagram header. This length
* shall include the header size. Min value: 0x10

dl_head_pad_len is only ever set to IPC_MEM_DL_ETH_OFFSET, in iosm_ipc_mux.c:

	ipc_mux->session[if_id].dl_head_pad_len = IPC_MEM_DL_ETH_OFFSET;

So pad_len is 0 and packet_offset is sizeof(struct mux_adgh), which is 0x10. That means adgh_len == 0x10, the documented minimum, is rejected here as invalid.

Dropping a header-only datagram looks correct, since a zero-length clone reaching ipc_wwan_receive() is what this patch fixes. The issue is that the header says 0x10 is valid while the decoder logs it with dev_err as an invalid length.

Firmware that sends header-only ADGH blocks according to the documented minimum would get one non-ratelimited dev_err per block on the RX path.

Should the kernel-doc be updated so the minimum requires a payload? Or should header-only datagrams be dropped silently, or with a ratelimited or lower-level message?

Quoted 4 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +1 -0
+
 	/* Pass the packet to the netif layer */
 	rc = ipc_mux_net_receive(ipc_mux, if_id, wwan, packet_offset,
 				 adgh->service_class, skb,
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121141850.73754-1-fangxy%40xiaopeng.com
#4 lore raw

PATCH v4 3/5 net: wwan: iosm: bound the ACBH command chain walk

In reply to Fang Xieyan

ipc_mux_dl_acb_decode() walks the command chain of a received Aggregated Command Block. first_cmd_index is passed through array_index_nospec() with sizeof(struct mux_cmdh) as its bound, which treats a byte offset as an array index and drops every command at or beyond that offset. next_cmd_index is not validated, so the walk can read past the end of the skb, loop on an earlier command, or decode the header just read as a second command.

Bound every command offset by the received length and require room for a complete command header. Validate the next index before processing the command carrying it: it has to advance past the current header and leave room for a complete next header, and only zero terminates the chain. The offsets then strictly increase and the walk terminates. The array_index_nospec() call is removed and block_length is only sanity checked.

Fixes 1f52d7b62285 ("net: wwan: iosm: Enable M.2 7360 WWAN card support")
---
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c | 46 ++++++++++++++++++----
 1 file changed, 38 insertions(+), 8 deletions(-)
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +38 -8
index 65421f6b4c91..a8448e273c02 100644
@@ -526,30 +526,60 @@ static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,
 /* Decode an aggregated command block. */
 static void ipc_mux_dl_acb_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 {
+	size_t hdr_len = offsetof(struct mux_cmdh, param);
 	struct mux_acbh *acbh;
 	struct mux_cmdh *cmdh;
-	u32 next_cmd_index;
+	u32 block_length, cmd_index, next_cmd_index;
 	u8 *block;
 	int size;
 
+	if (skb->len < sizeof(struct mux_acbh))
+		return;
+
 	acbh = (struct mux_acbh *)(skb->data);
 	block = (u8 *)(skb->data);
+	block_length = le32_to_cpu(acbh->block_length);
 
-	next_cmd_index = le32_to_cpu(acbh->first_cmd_index);
-	next_cmd_index = array_index_nospec(next_cmd_index,
-					    sizeof(struct mux_cmdh));
+	/*
+	 * The uplink encoder leaves block_length at the ACBH header size.
+	 * It cannot delimit the command chain, so use skb->len to bound
+	 * the command offsets.
+	 */
+	if (block_length < sizeof(struct mux_acbh) || block_length > skb->len)
+		return;
+
+	cmd_index = le32_to_cpu(acbh->first_cmd_index);
+
+	while (cmd_index != 0) {
+		/* Past the block header, and a full command header fits. */
+		if (cmd_index < sizeof(struct mux_acbh) ||
+		    cmd_index > skb->len ||
+		    hdr_len > skb->len - cmd_index)
+			break;
 
-	while (next_cmd_index != 0) {
-		cmdh = (struct mux_cmdh *)&block[next_cmd_index];
+		cmdh = (struct mux_cmdh *)&block[cmd_index];
 		next_cmd_index = le32_to_cpu(cmdh->next_cmd_index);
+
+		/*
+		 * The next index has to move past this header, stay inside
+		 * the received data and leave room for a header of its own.
+		 */
+		if (next_cmd_index != 0 &&
+		    (next_cmd_index <= cmd_index ||
+		     next_cmd_index - cmd_index < hdr_len ||
+		     next_cmd_index > skb->len ||
+		     skb->len - next_cmd_index < hdr_len))
+			break;
+
 		if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, cmdh->param,
 						       cmdh->command_type,
 						       cmdh->if_id,
 						       cmdh->transaction_id)) {
-			size = offsetof(struct mux_cmdh, param) +
-				sizeof(cmdh->param.flow_ctl);
+			size = hdr_len + sizeof(cmdh->param.flow_ctl);
 			ipc_mux_dl_acbcmd_decode(ipc_mux, cmdh, size);
 		}
+
+		cmd_index = next_cmd_index;
 	}
 }
 
Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH net v4 3/5] net: wwan: iosm: bound the ACBH command chain walk

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

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

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

- [Medium] ipc_mux_dl_acb_decode() (drivers/net/wwan/iosm/iosm_ipc_mux_codec.c:548-549) adds a new early…

--

net: wwan: iosm: bound the ACBH command chain walk

This patch rewrites the ACB command chain walk in ipc_mux_dl_acb_decode(). Every command offset is now bounded by skb->len, and next_cmd_index is validated before each command is processed. The array_index_nospec() clamp is removed and a sanity check on acbh->block_length is added.

Quoted 5 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +0 -0
index 65421f6b4c91e..a8448e273c021 100644
@@ -526,30 +526,60 @@ static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,

[ ... ]

Quoted 4 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +1 -0
 	acbh = (struct mux_acbh *)(skb->data);
 	block = (u8 *)(skb->data);
+	block_length = le32_to_cpu(acbh->block_length);
 

drivers/net/wwan/iosm/iosm_ipc_mux_codec.c static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux, · line 549

Quoted 10 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +7 -3
-	next_cmd_index = le32_to_cpu(acbh->first_cmd_index);
-	next_cmd_index = array_index_nospec(next_cmd_index,
-					    sizeof(struct mux_cmdh));
+	/*
+	 * The uplink encoder leaves block_length at the ACBH header size.
+	 * It cannot delimit the command chain, so use skb->len to bound
+	 * the command offsets.
+	 */
+	if (block_length < sizeof(struct mux_acbh) || block_length > skb->len)
+		return;

[Severity: Medium]
Could this new check end up dropping every downlink ACB?

Before this patch, ipc_mux_dl_acb_decode() never read acbh->block_length. The comment just above says the field cannot delimit the command chain. Every later bound uses skb->len, so the check does not seem to protect any of the reads.

The only in-tree code that writes this field is the uplink encoder, and it leaves the field at the header size:

drivers/net/wwan/iosm/iosm_ipc_mux_codec.c:ipc_mux_acb_init() {
    ...
	header->block_length = cpu_to_le32(sizeof(struct mux_acbh));
	header->first_cmd_index = header->block_length;
    ...
}

That value passes the check. However, nothing in the driver shows what the 7360 CP firmware writes to block_length in DL ACBs. If it writes 0, or any value outside [sizeof(struct mux_acbh), skb->len], the whole ACB is dropped without any message.

That would include MUX_CMD_OPEN_SESSION_RESP, so the blocking wait in ipc_mux_session_open_send() would time out:

drivers/net/wwan/iosm/iosm_ipc_mux.c:ipc_mux_session_open_send() {
    ...
	acb->wanted_response = MUX_CMD_OPEN_SESSION_RESP;
	if (ipc_mux_dl_acb_send_cmds(ipc_mux, MUX_CMD_OPEN_SESSION, if_id, 0,
				     &param, sizeof(param.open_session), true,
				 false) ||
	    acb->got_response != MUX_CMD_OPEN_SESSION_RESP) {
    ...
}

FLOW_CTL_ENABLE, FLOW_CTL_DISABLE and LINK_STATUS_REPORT commands would be dropped too. No FLOW_CTL_ACK would go back to the CP, and session->net_tx_stop and session->flow_ctl_mask would never be updated.

This patch is tagged for stable, and the block_length check is still present in the final state of the series. Would it be safer to drop the check, or at least log when it fires?

A smaller related point: the new next_cmd_index check later in the loop drops the current command even when its own header is valid:

	if (next_cmd_index != 0 &&
	    (next_cmd_index <= cmd_index ||
	     next_cmd_index - cmd_index < hdr_len ||
	     next_cmd_index > skb->len ||
	     skb->len - next_cmd_index < hdr_len))
		break;

None of the new rejection paths in ipc_mux_dl_acb_decode() log anything. The ADGH change in the same series does add a dev_err(). Would a ratelimited dev_err() on these paths help make a firmware mismatch visible?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121141850.73754-1-fangxy%40xiaopeng.com
#6 lore raw

PATCH v4 4/5 net: wwan: iosm: pass the DL command parameter by pointer

In reply to Fang Xieyan

ipc_mux_dl_cmdresps_decode_process() takes the twelve byte union mux_cmd_param by value, so every received command copies the whole union out of the skb even when its type only needs one member, reading past what arrived for a truncated response. The driver also builds its link status response inside the received block.

Take the parameter as a const pointer and keep the lite path response in a local variable; the aggregated path sends no response parameter. The session response path still copies the full parameter union here. The next patch adds the received parameter length check and restricts each response copy to its own member. This change is a prerequisite for that fix and is tagged for stable alongside it.

Fixes 9413491e20e1 ("net: iosm: encode or decode datagram")
---
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c | 29 ++++++++++------------
 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +13 -16
index a8448e273c02..3f93d7293785 100644
@@ -184,7 +184,7 @@ void ipc_mux_netif_tx_flowctrl(struct mux_session *session, int idx, bool on)
 }
 
 static int ipc_mux_dl_cmdresps_decode_process(struct iosm_mux *ipc_mux,
-					      union mux_cmd_param param,
+					      const union mux_cmd_param *param,
 					      __le32 command_type, u8 if_id,
 					      __le32 transaction_id)
 {
@@ -194,7 +194,7 @@ static int ipc_mux_dl_cmdresps_decode_process(struct iosm_mux *ipc_mux,
 	case MUX_CMD_OPEN_SESSION_RESP:
 	case MUX_CMD_CLOSE_SESSION_RESP:
 		/* Resume the control application. */
-		acb->got_param = param;
+		acb->got_param = *param;
 		break;
 
 	case MUX_LITE_CMD_FLOW_CTL_ACK:
@@ -318,7 +318,7 @@ static void ipc_mux_dl_cmd_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 	__le32 trans_id = cmdh->transaction_id;
 	int size;
 
-	if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, cmdh->param,
+	if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, &cmdh->param,
 					       cmdh->command_type, cmdh->if_id,
 					       cmdh->transaction_id)) {
 		/* Unable to decode command response indicates the cmd_type
@@ -333,14 +333,15 @@ static void ipc_mux_dl_cmd_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 			/* Decoded command may need a response. Give the
 			 * response according to the command type.
 			 */
+			union mux_cmd_param response = {};
 			union mux_cmd_param *mux_cmd = NULL;
 			size_t size = 0;
 			u32 cmd = MUX_LITE_CMD_LINK_STATUS_REPORT_RESP;
 
 			if (cmdh->command_type ==
 			    cpu_to_le32(MUX_LITE_CMD_LINK_STATUS_REPORT)) {
-				mux_cmd = &cmdh->param;
-				mux_cmd->link_status_resp.response =
+				mux_cmd = &response;
+				response.link_status_resp.response =
 					cpu_to_le32(MUX_CMD_RESP_SUCCESS);
 				/* response field is u32 */
 				size = sizeof(u32);
@@ -500,26 +501,22 @@ static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,
 	u32 fctl_dis = IOSM_AGGR_MUX_CMD_FLOW_CTL_DISABLE;
 	u32 fctl_ena = IOSM_AGGR_MUX_CMD_FLOW_CTL_ENABLE;
 	u32 fctl_ack = IOSM_AGGR_MUX_CMD_FLOW_CTL_ACK;
-	union mux_cmd_param *cmd_p = NULL;
 	u32 cmd = link_st;
 	u32 trans_id;
 
 	if (!ipc_mux_dl_cmds_decode_process(ipc_mux, &cmdh->param,
 					    cmdh->command_type, cmdh->if_id,
 					    cmdh->cmd_len, size)) {
-		size = 0;
-		if (cmdh->command_type == cpu_to_le32(link_st)) {
-			cmd_p = &cmdh->param;
-			cmd_p->link_status_resp.response = MUX_CMD_RESP_SUCCESS;
-		} else if ((cmdh->command_type == cpu_to_le32(fctl_ena)) ||
-				(cmdh->command_type == cpu_to_le32(fctl_dis))) {
+		if (cmdh->command_type == cpu_to_le32(fctl_ena) ||
+		    cmdh->command_type == cpu_to_le32(fctl_dis))
 			cmd = fctl_ack;
-		} else {
+		else if (cmdh->command_type != cpu_to_le32(link_st))
 			return;
-			}
+
 		trans_id = le32_to_cpu(cmdh->transaction_id);
+		/* No response parameter is sent in the aggregated protocol. */
 		ipc_mux_dl_acb_send_cmds(ipc_mux, cmd, cmdh->if_id,
-					 trans_id, cmd_p, size, false, true);
+					 trans_id, NULL, 0, false, true);
 	}
 }
 
@@ -571,7 +568,7 @@ static void ipc_mux_dl_acb_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 		     skb->len - next_cmd_index < hdr_len))
 			break;
 
-		if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, cmdh->param,
+		if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, &cmdh->param,
 						       cmdh->command_type,
 						       cmdh->if_id,
 						       cmdh->transaction_id)) {
Signature · 1 line
2.50.1
#7 lore raw

PATCH v4 5/5 net: wwan: iosm: validate the command parameter length

In reply to Fang Xieyan

Both command decoders read members of the parameter union without knowing how long the parameter actually is, so a truncated OPEN_SESSION_RESP copies bytes past the command into acb->got_param and resumes the session establishment waiting on it, and a short FLOW_CTL builds the flow control mask from whatever bytes followed.

Derive the parameter length from what the command owns: the bytes beyond the header in MUX Lite, the bytes up to the next command in an aggregated block, or up to the end of the block for the last one. Check the member each command type uses against that length and return -EMSGSIZE when it does not fit; the callers drop the command, and in an aggregated block the walk ends there. Unknown types still return -EINVAL and the walk continues. cmd_len is passed to the decoder unchanged.

Fixes 9413491e20e1 ("net: iosm: encode or decode datagram")
---
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c | 105 +++++++++++++++------
 1 file changed, 75 insertions(+), 30 deletions(-)
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +75 -30
index 3f93d7293785..b2bd53c5ba30 100644
@@ -185,6 +185,7 @@ void ipc_mux_netif_tx_flowctrl(struct mux_session *session, int idx, bool on)
 
 static int ipc_mux_dl_cmdresps_decode_process(struct iosm_mux *ipc_mux,
 					      const union mux_cmd_param *param,
+					      size_t param_len,
 					      __le32 command_type, u8 if_id,
 					      __le32 transaction_id)
 {
@@ -192,9 +193,18 @@ static int ipc_mux_dl_cmdresps_decode_process(struct iosm_mux *ipc_mux,
 
 	switch (le32_to_cpu(command_type)) {
 	case MUX_CMD_OPEN_SESSION_RESP:
-	case MUX_CMD_CLOSE_SESSION_RESP:
+		if (param_len < sizeof(param->open_session_resp))
+			return -EMSGSIZE;
+
 		/* Resume the control application. */
-		acb->got_param = *param;
+		acb->got_param.open_session_resp = param->open_session_resp;
+		break;
+
+	case MUX_CMD_CLOSE_SESSION_RESP:
+		if (param_len < sizeof(param->close_session_resp))
+			return -EMSGSIZE;
+
+		acb->got_param.close_session_resp = param->close_session_resp;
 		break;
 
 	case MUX_LITE_CMD_FLOW_CTL_ACK:
@@ -229,6 +239,7 @@ static int ipc_mux_dl_cmdresps_decode_process(struct iosm_mux *ipc_mux,
 
 static int ipc_mux_dl_cmds_decode_process(struct iosm_mux *ipc_mux,
 					  union mux_cmd_param *param,
+					  size_t param_len,
 					  __le32 command_type, u8 if_id,
 					  __le16 cmd_len, int size)
 {
@@ -242,6 +253,9 @@ static int ipc_mux_dl_cmds_decode_process(struct iosm_mux *ipc_mux,
 	case MUX_LITE_CMD_FLOW_CTL:
 	case IOSM_AGGR_MUX_CMD_FLOW_CTL_DISABLE:
 
+		if (param_len < sizeof(param->flow_ctl))
+			return -EMSGSIZE;
+
 		if (if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES) {
 			dev_err(ipc_mux->dev, "if_id [%d] not valid",
 				if_id);
@@ -315,18 +329,26 @@ static int ipc_mux_dl_cmds_decode_process(struct iosm_mux *ipc_mux,
 static void ipc_mux_dl_cmd_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 {
 	struct mux_lite_cmdh *cmdh = (struct mux_lite_cmdh *)skb->data;
+	size_t hdr_len = offsetof(struct mux_lite_cmdh, param);
+	size_t param_len = skb->len > hdr_len ? skb->len - hdr_len : 0;
 	__le32 trans_id = cmdh->transaction_id;
-	int size;
+	int ret, size;
+
+	ret = ipc_mux_dl_cmdresps_decode_process(ipc_mux, &cmdh->param,
+						 param_len,
+						 cmdh->command_type,
+						 cmdh->if_id,
+						 cmdh->transaction_id);
+	if (ret == -EMSGSIZE)
+		return;
 
-	if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, &cmdh->param,
-					       cmdh->command_type, cmdh->if_id,
-					       cmdh->transaction_id)) {
+	if (ret) {
 		/* Unable to decode command response indicates the cmd_type
 		 * may be a command instead of response. So try to decoding it.
 		 */
-		size = offsetof(struct mux_lite_cmdh, param) +
-				sizeof(cmdh->param.flow_ctl);
+		size = hdr_len + sizeof(cmdh->param.flow_ctl);
 		if (!ipc_mux_dl_cmds_decode_process(ipc_mux, &cmdh->param,
+						    param_len,
 						    cmdh->command_type,
 						    cmdh->if_id,
 						    cmdh->cmd_len, size)) {
@@ -494,8 +516,9 @@ static void ipc_mux_dl_adgh_decode(struct iosm_mux *ipc_mux,
 	ipc_mux->session[if_id].flush = 1;
 }
 
-static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,
-				     struct mux_cmdh *cmdh, int size)
+static int ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,
+				    struct mux_cmdh *cmdh, size_t param_len,
+				    int size)
 {
 	u32 link_st  = IOSM_AGGR_MUX_CMD_LINK_STATUS_REPORT_RESP;
 	u32 fctl_dis = IOSM_AGGR_MUX_CMD_FLOW_CTL_DISABLE;
@@ -503,32 +526,39 @@ static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,
 	u32 fctl_ack = IOSM_AGGR_MUX_CMD_FLOW_CTL_ACK;
 	u32 cmd = link_st;
 	u32 trans_id;
+	int ret;
 
-	if (!ipc_mux_dl_cmds_decode_process(ipc_mux, &cmdh->param,
-					    cmdh->command_type, cmdh->if_id,
-					    cmdh->cmd_len, size)) {
-		if (cmdh->command_type == cpu_to_le32(fctl_ena) ||
-		    cmdh->command_type == cpu_to_le32(fctl_dis))
-			cmd = fctl_ack;
-		else if (cmdh->command_type != cpu_to_le32(link_st))
-			return;
-
-		trans_id = le32_to_cpu(cmdh->transaction_id);
-		/* No response parameter is sent in the aggregated protocol. */
-		ipc_mux_dl_acb_send_cmds(ipc_mux, cmd, cmdh->if_id,
-					 trans_id, NULL, 0, false, true);
-	}
+	ret = ipc_mux_dl_cmds_decode_process(ipc_mux, &cmdh->param, param_len,
+					     cmdh->command_type, cmdh->if_id,
+					     cmdh->cmd_len, size);
+	if (ret)
+		return ret;
+
+	if (cmdh->command_type == cpu_to_le32(fctl_ena) ||
+	    cmdh->command_type == cpu_to_le32(fctl_dis))
+		cmd = fctl_ack;
+	else if (cmdh->command_type != cpu_to_le32(link_st))
+		return -EINVAL;
+
+	trans_id = le32_to_cpu(cmdh->transaction_id);
+
+	/* No response parameter is sent in the aggregated protocol. */
+	ipc_mux_dl_acb_send_cmds(ipc_mux, cmd, cmdh->if_id, trans_id,
+				 NULL, 0, false, true);
+
+	return 0;
 }
 
 /* Decode an aggregated command block. */
 static void ipc_mux_dl_acb_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 {
 	size_t hdr_len = offsetof(struct mux_cmdh, param);
+	size_t cmd_end, param_len;
 	struct mux_acbh *acbh;
 	struct mux_cmdh *cmdh;
 	u32 block_length, cmd_index, next_cmd_index;
 	u8 *block;
-	int size;
+	int ret, size;
 
 	if (skb->len < sizeof(struct mux_acbh))
 		return;
@@ -568,12 +598,27 @@ static void ipc_mux_dl_acb_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 		     skb->len - next_cmd_index < hdr_len))
 			break;
 
-		if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, &cmdh->param,
-						       cmdh->command_type,
-						       cmdh->if_id,
-						       cmdh->transaction_id)) {
+		/*
+		 * A command owns the bytes up to the next one, the last
+		 * one of the chain those up to the end of what arrived.
+		 */
+		cmd_end = next_cmd_index ? next_cmd_index : skb->len;
+		param_len = cmd_end - cmd_index - hdr_len;
+
+		ret = ipc_mux_dl_cmdresps_decode_process(ipc_mux, &cmdh->param,
+							 param_len,
+							 cmdh->command_type,
+							 cmdh->if_id,
+							 cmdh->transaction_id);
+		if (ret == -EMSGSIZE)
+			break;
+
+		if (ret) {
 			size = hdr_len + sizeof(cmdh->param.flow_ctl);
-			ipc_mux_dl_acbcmd_decode(ipc_mux, cmdh, size);
+			ret = ipc_mux_dl_acbcmd_decode(ipc_mux, cmdh, param_len,
+						       size);
+			if (ret == -EMSGSIZE)
+				break;
 		}
 
 		cmd_index = next_cmd_index;
Signature · 1 line
2.50.1
#8 lore raw

Re: [PATCH net v4 5/5] net: wwan: iosm: validate the command parameter length

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

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

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

- [Medium] The new parameter-length check never uses the device-supplied cmdh->cmd_len, which is what delimits a command in this wire format.

--

net: wwan: iosm: validate the command parameter length

This patch passes a parameter length to the iosm MUX command decoders. Each decoder checks it against the parameter union member its command type uses and returns -EMSGSIZE when the member does not fit. The length comes from skb->len for MUX Lite, and from next_cmd_index or skb->len for commands in an aggregated command block.

Quoted 4 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +0 -0
index 3f93d72937858..b2bd53c5ba30f 100644

[ ... ]

drivers/net/wwan/iosm/iosm_ipc_mux_codec.c static int ipc_mux_dl_cmds_decode_process(struct iosm_mux *ipc_mux, · line 333

Quoted 6 lines
drivers/net/wwan/iosm/iosm_ipc_mux_codec.c +2 -0
@@ -315,18 +329,26 @@ static int ipc_mux_dl_cmds_decode_process(struct iosm_mux *ipc_mux,
 static void ipc_mux_dl_cmd_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
 {
 	struct mux_lite_cmdh *cmdh = (struct mux_lite_cmdh *)skb->data;
+	size_t hdr_len = offsetof(struct mux_lite_cmdh, param);
+	size_t param_len = skb->len > hdr_len ? skb->len - hdr_len : 0;

[Severity: Medium]
Should param_len also be limited by cmdh->cmd_len here?

The kernel-doc for struct mux_lite_cmdh defines cmd_len as the length of the command:

* @cmd_len: Length (in bytes) of the command. This length shall
* include the header size. Minimum value: 0x10

The encoders, ipc_mux_lite_add_cmd() and ipc_mux_acb_add_cmd(), also set it to the header plus the parameter size, and skb_put() exactly that much:

	cmdh->cmd_len = cpu_to_le16(offsetof(struct mux_lite_cmdh, param) +
				    param_size);
	...
	skb_put(acb->skb, le16_to_cpu(cmdh->cmd_len));

Take an OPEN_SESSION_RESP that arrives with cmd_len = hdr_len + 4, so it carries only the response code, with more bytes after it in the received buffer. Would it still pass the new check in ipc_mux_dl_cmdresps_decode_process()?

	if (param_len < sizeof(param->open_session_resp))
		return -EMSGSIZE;

	/* Resume the control application. */
	acb->got_param.open_session_resp = param->open_session_resp;

The reads now stay within skb->len. But the bytes after the command would still be copied into acb->got_param. Then complete(&ipc_mux->channel->ul_sem) would wake ipc_mux_session_open(), which uses those bytes as the response and as ul_head_pad_len. A short FLOW_CTL would also have its mask read from bytes beyond cmd_len.

ipc_mux_dl_acb_decode() seems to have the same gap. Each aggregated command's extent comes from next_cmd_index or skb->len, not from its own cmd_len:

	cmd_end = next_cmd_index ? next_cmd_index : skb->len;
	param_len = cmd_end - cmd_index - hdr_len;

So padding before the next command counts as parameter. For the last command, the rest of the skb does too.

The commit message names this case as the bug being fixed:

"a truncated OPEN_SESSION_RESP copies bytes past the command into
acb->got_param"

It also says:

"cmd_len is passed to the decoder unchanged."

Would bounding the length by something like min(param_len, cmd_len - hdr_len), when cmd_len >= hdr_len, be needed to cover the truncated command case completely?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121141850.73754-1-fangxy%40xiaopeng.com