Skip to contentSkip to conversation

can: j1939: fix null-ptr-deref in j1939_session_completed()

PATCH New Xue Boyang 2 participants 2 messages Nov 23 – Nov 24

Subsystems (6)
lore
Share & tools
lore

Xue Boyang

author
#0 lore raw

PATCH v2 1/1 can: j1939: fix null-ptr-deref in j1939_session_completed()

j1939_session_completed() distributes the reassembled message of a receive session. It looks the message up at the offset named by the last ETP.CM_DPO of the peer, session->pkt.dpo * 7, and passes the result to j1939_sk_recv() without checking it.

j1939_xtp_rx_dpo_one() stores that DPO without any check. A receive session keeps the whole message in a single skb, so a DPO with a packet number >= session->pkt.total makes the lookup return NULL. When the transfer then completes, j1939_sk_recv() dereferences the NULL pointer and the kernel crashes in the receive path.

Any node on the CAN bus can send such a DPO, for example right before the last data packet of an otherwise valid transfer, so this is remotely triggerable. On a system with the vcan and J1939 modules available, an unprivileged local user can reach it from inside a user namespace as well.

Reject the DPO when it is received, and keep a backstop for the lookup:

- Cancel a receive session with J1939_XTP_ABORT_BAD_EDPO_OFFSET when the DPO has a packet number >= session->pkt.total. That abort code existed but was never sent. Transmitter sessions are not affected, they only see the echo of their own DPO.
- Keep the NULL check in j1939_session_completed() and set session->err on that path, so that an unexpected lookup failure is reported as J1939_EE_INFO_RX_ABORT instead of ending as a success.

AI assistance was used to analyze the bug, and to prepare and test this patch.

Fixes 9d71dd0c7009 ("can: add support of SAE J1939 protocol")

---

Notes (j1939-v2-submit):
Based on mainline commit 4843f6873f7c4835e62f2c73ba0fd538e57a2f22.

Changes since v1, following the Sashiko review:
v1: https://lore.kernel.org/linux-can/[email protected]
review: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117103516.535910-1-fuchen.dust%40gmail.com

- v1 only added the NULL check. v2 also rejects the DPO in j1939_xtp_rx_dpo_one() and reports a failed lookup via session->err.
- The commit message now states the impact and the reachability.
- This patch supersedes my 2-patch series from September, which had the same NULL check and a DPO check limited to dpo > pkt.total:
https://lore.kernel.org/linux-can/[email protected]

Testing, on v7.4-rc3 based kernels with CONFIG_KASAN=y, with raw CAN frames injected on a vcan interface:

- Unpatched: an ETP receive session given a DPO with packet number 256 for a 256 packet transfer, followed by the last data packet, causes "KASAN: null-ptr-deref in range [0x18-0x1f]" in j1939_sk_recv() and a panic. The same happens when an unprivileged user triggers it from inside a user namespace, with the modules auto-loaded on demand.
- Patched: the kernel answers with ETP.CM_ABORT (reason 12), the socket gets J1939_EE_INFO_RX_ABORT, nothing is delivered and the kernel keeps running. Same for packet numbers 257 and 65535.
- A valid transfer whose last DPO has packet number 255 completes on both kernels, the 1786 byte message arrives intact.
- net/can/j1939/ builds without warnings with W=1.

The reproducer is not included. It is available on request.

 net/can/j1939/transport.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
net/can/j1939/transport.c +19 -3
index 8fcfd13e5e6f8..d3644c8239a4b 100644
@@ -1244,8 +1244,13 @@ static void j1939_session_completed(struct j1939_session *session)
 	if (!session->transmission) {
 		se_skb = j1939_session_skb_get(session);
 		/* distribute among j1939 receivers */
-		j1939_sk_recv(session->priv, se_skb);
-		consume_skb(se_skb);
+		if (se_skb) {
+			j1939_sk_recv(session->priv, se_skb);
+			consume_skb(se_skb);
+		} else {
+			/* should not happen, report it instead of a success */
+			session->err = EPROTO;
+		}
 	}
 
 	j1939_session_deactivate_activate_next(session);
@@ -1823,14 +1828,25 @@ static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
 				 struct sk_buff *skb)
 {
 	const u8 *dat = skb->data;
+	unsigned int dpo;
 
 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
 		return;
 
 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
 
+	dpo = j1939_etp_ctl_to_packet(skb->data);
+	/* a receive session keeps the whole message in a single skb,
+	 * reject a DPO which does not point into it
+	 */
+	if (!session->transmission && dpo >= session->pkt.total) {
+		j1939_session_timers_cancel(session);
+		j1939_session_cancel(session, J1939_XTP_ABORT_BAD_EDPO_OFFSET);
+		return;
+	}
+
 	/* transmitted without problems */
-	session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
+	session->pkt.dpo = dpo;
 	session->last_cmd = dat[0];
 	j1939_tp_set_rxtimeout(session, 750);
 

base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22

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

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

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

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] A peer can leave a receive session permanently stranded by sending two bad DPOs.
- [Low] The commit message says a bogus DPO can be sent "right before the last data packet of an otherwise valid transfer".

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch adds another caller.

--

can: j1939: fix null-ptr-deref in j1939_session_completed()

This patch makes j1939_xtp_rx_dpo_one() reject an ETP.CM_DPO on a receive session when the packet number is >= session->pkt.total. It cancels the session with J1939_XTP_ABORT_BAD_EDPO_OFFSET. It also adds a NULL check on the reassembled skb in j1939_session_completed() and sets session->err on that path.

Quoted 3 lines

Any node on the CAN bus can send such a DPO, for example right before the last data packet of an otherwise valid transfer, so this is remotely triggerable.

[Severity: Low]
Is this reproduction sequence accurate? Once a DPO with packet number D >= pkt.total is stored, the next data packet goes through j1939_xtp_rx_dat_one():

	packet = (dat[0] - 1 + session->pkt.dpo);
	if (packet > session->pkt.total ||
	    (session->pkt.rx + 1) > session->pkt.total) {
		...
		goto out_session_cancel;
	}

	se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
	if (!se_skb) {
		...
		goto out_session_cancel;
	}

For any sequence byte dat[0] >= 1, packet is >= pkt.total. If packet is greater than total, the bound check aborts. If packet equals total, the offset lookup returns NULL, because the RX skb is only len bytes long.

Either way the session is cancelled and pkt.rx never reaches pkt.total. No EOMA is sent, and j1939_session_completed() is never called.

As far as I can tell, only two sequences reach the NULL dereference on the unpatched code:

- The bad DPO arrives after the final data packet but before the loopback echo of the receiver's own EOMA reaches j1939_xtp_rx_eoma_one()->j1939_session_completed().

- A DPO with D == pkt.total is followed by a data packet with sequence byte 0. Then packet == total - 1 and pkt.rx reaches total.

The test in the notes (DPO packet 256 for a 256 packet transfer) seems to match the second sequence. The new dpo >= session->pkt.total check blocks both, so only the description seems to be off.

Could the commit message describe a sequence that actually triggers the crash?

Quoted 4 lines
net/can/j1939/transport.c +0 -0
index 8fcfd13e5e6f8..d3644c8239a4b 100644

[ ... ]

Quoted 10 lines
net/can/j1939/transport.c +1 -0
@@ -1823,14 +1828,25 @@ static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
 				 struct sk_buff *skb)
 {
 	const u8 *dat = skb->data;
+	unsigned int dpo;
 
 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
 		return;
 
 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);

net/can/j1939/transport.c static void j1939_xtp_rx_dpo_one(struct j1939_session *session, · line 1846

Quoted 10 lines
net/can/j1939/transport.c +9 -0
 
+	dpo = j1939_etp_ctl_to_packet(skb->data);
+	/* a receive session keeps the whole message in a single skb,
+	 * reject a DPO which does not point into it
+	 */
+	if (!session->transmission && dpo >= session->pkt.total) {
+		j1939_session_timers_cancel(session);
+		j1939_session_cancel(session, J1939_XTP_ABORT_BAD_EDPO_OFFSET);
+		return;
+	}

[Severity: High]
Can a peer strand a receive session by sending two bad DPOs in a row?

j1939_session_cancel() only re-arms the rxtimer while the session is not yet in WAITING_ABORT:

	if (session->state >= J1939_SESSION_ACTIVE &&
	    session->state < J1939_SESSION_WAITING_ABORT) {
		j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS);
		__j1939_session_cancel(session, err);
	}

The sequence would be:

1. The first DPO with packet number >= pkt.total moves the session to WAITING_ABORT and arms the 500 ms abort rxtimer.

2. A second bad DPO arrives within 500 ms. j1939_session_get_by_addr_locked() does not filter on state, and the PGN is unchanged, so j1939_xtp_rx_cmd_bad_pgn() passes.

3. j1939_session_timers_cancel() cancels the pending abort rxtimer and drops its reference.

4. j1939_session_cancel() does nothing except queue another RX_ABORT.

Only the WAITING_ABORT branch of j1939_tp_rxtimer() deactivates such a session, and that timer is now gone. Receive sessions have no sk, so socket release does not clean them up. The local abort echo carries a local SA, so it is only dispatched to transmitter sessions.

The session would then stay on active_session_list. It keeps its kref, its reassembly skb (sized by the peer's RTS, up to tp_max_packet_size), its netdev tracker and its priv reference. These are only released on NETDEV_DOWN/UNREGISTER or when an RTS with the same PGN arrives on the same tuple. An RTS with a different PGN on the same (sa, da) is refused with BUSY.

Before this patch, a DPO in WAITING_ABORT always went through j1939_tp_set_rxtimeout(session, 750), which re-armed the timer.

The RTS path already guards against this in
j1939_xtp_rx_rts_session_active():

		} else if (session->state == J1939_SESSION_WAITING_ABORT) {
			/* Force deactivation for the receiver.
			 * If we rely on the timer starting in j1939_session_cancel,
			 * a second RTS call here will cancel that timer and fail
			 * to restart it because the state is already WAITING_ABORT.
			 */
			j1939_session_deactivate_activate_next(session);
		}

Would it make sense to return early here when session->state >= J1939_SESSION_WAITING_ABORT? Another option is to force j1939_session_deactivate_activate_next() in that state, as the RTS path does.

The out_session_cancel path of j1939_xtp_rx_dat_one() has the same timers_cancel plus cancel pattern, for example with two data frames whose length is not 8. That copy is pre-existing.

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. However, the patch adds another caller of j1939_session_cancel(), which holds the session list lock across a synchronous hrtimer cancel:

j1939_session_cancel()
j1939_session_list_lock(session->priv) /* spin_lock_bh */
j1939_tp_set_rxtimeout()

    j1939_session_rxtimer_cancel()
      hrtimer_cancel(&session->rxtimer)    /* waits for running callback */

The rxtimer callback j1939_tp_rxtimer() takes the same lock in its non-WAITING_ABORT, non-SIMPLE branch:

	} else {
		j1939_session_list_lock(session->priv);
		if (session->state >= J1939_SESSION_ACTIVE &&
		    session->state < J1939_SESSION_ACTIVE_MAX) {

Suppose the rxtimer fires on CPU B while CPU A is inside j1939_session_cancel() with the session still ACTIVE. Can B then spin on the list lock while A spins in hrtimer_cancel() waiting for B's callback to finish?

The more realistic trigger is the existing j1939_tp_txtimer()->j1939_session_cancel() path, which does not cancel the rxtimer first. At this new call site, j1939_session_timers_cancel() runs just before, so the window is very narrow. The rxtimer would have to be re-armed from another CPU and expire almost immediately, and all rxtimeouts are at least 250 ms.

Would it be better to cancel and re-arm the timers outside the list lock?

Quoted 6 lines
net/can/j1939/transport.c +2 -1
+
 	/* transmitted without problems */
-	session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
+	session->pkt.dpo = dpo;
 	session->last_cmd = dat[0];
 	j1939_tp_set_rxtimeout(session, 750);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123000916.181439-1-fuchen.dust%40gmail.com