can: j1939: fix null-ptr-deref in j1939_session_completed()
Subsystems (6)
PATCH 1/1 can: j1939: fix null-ptr-deref in j1939_session_completed()
j1939_session_completed() looks up the receive skb using the session's current data packet offset and passes the result to j1939_sk_recv() without checking it. If no queued skb covers that offset, the lookup returns NULL and receive-side processing can dereference it.
An inconsistent ETP session offset can leave the receive buffer lookup unsatisfied when the session completes. The DPO receive path does not validate the offset against the receive buffer, so completion cannot assume that the lookup always succeeds.
Check the returned skb before distributing the message and releasing the lookup reference. Keep session deactivation and activation of the next queued session unconditional, so a missing receive buffer does not leave the completed session active. This also matches the NULL handling already used by j1939_simple_txnext().
AI assistance was used to analyze the bug and prepare this patch.
---
Notes (j1939-null-submit):
Based on mainline commit 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4. The affected transport.c and socket.c are unchanged from the original local report baseline daf677c2c6449011ee695d55b48b5b2977a36f88.
Validation:
- Built net/can/j1939/ with W=1 on x86_64, CONFIG_CAN_J1939=m, CONFIG_KASAN=y and CONFIG_WERROR=y; no build warnings or errors.
- A temporary userspace harness executed the extracted completion function with stubbed dependencies. It checked missing-buffer receive, normal receive reference balancing, and transmit completion. The old function passed NULL to the delivery stub; the patched function did not, while retaining the completion-helper call.
- This revision has not been boot-tested or run against a CAN interface. The harness is not an end-to-end kernel regression test.
AI assistance (OpenAI coding assistant) was used for source analysis, commit-message preparation, and build and isolated-function checks. No packet-level reproducer or triggering sequence is included.
net/can/j1939/transport.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/can/j1939/transport.c +4 -2
@@ -1244,8 +1244,10 @@ 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); + } }j1939_session_deactivate_activate_next (session);
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
[email protected]
AI reviewThank 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 · 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] The patch fixes the symptom (the NULL deref in j1939_session_completed()) but leaves in place the root cause that the…
- [Low] The commit message understates how reachable the bug is.
--
can: j1939: fix null-ptr-deref in j1939_session_completed()
This patch adds a NULL check in j1939_session_completed() on the skb returned by j1939_session_skb_get(). The check runs before the skb is passed to j1939_sk_recv() and consume_skb(). Session deactivation and activation of the next queued session still happen unconditionally.
Quoted 4 lines
An inconsistent ETP session offset can leave the receive buffer lookup unsatisfied when the session completes. The DPO receive path does not validate the offset against the receive buffer, so completion cannot assume that the lookup always succeeds.
[Severity: Low]
Could the commit message say more precisely how this is reached? As far as I can tell, any node on the CAN bus can trigger the NULL dereference in the unpatched code:
1. Start a valid ETP transfer to a local address that has a bound J1939 socket.
2. Send all the data packets.
3. Send an ETP.CM_DPO with dpo * 7 >= total_message_size. This can be sent before the receiver's txtimer sends EOMA, or after EOMA is sent but before its echo comes back.
The DPO is stored without any check:
j1939_tp_cmd_recv()
j1939_xtp_rx_dpo(priv, skb, false)
j1939_xtp_rx_dpo_one()
session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);Then the EOMA echo completes the session:
j1939_tp_cmd_recv()
j1939_xtp_rx_eoma(priv, skb, false)
j1939_xtp_rx_eoma_one()
j1939_session_completed()
j1939_sk_recv(priv, NULL)
j1939_sk_recv_one()
if (oskb->sk == &jsk->sk)A matching socket must exist, because j1939_session_tx_eoma() won't send EOMA otherwise. So the loop body in j1939_sk_recv() always runs.
This is a remotely triggerable crash. Would it help stable backporters and security triage if the commit message said so and included the trigger sequence?
Quoted 7 lines
net/can/j1939/transport.c +0
-0@@ -1244,8 +1244,10 @@ static void j1939_session_completed(struct j1939_session *session)if (!session->transmission) { se_skb =j1939_session_skb_get (session);
net/can/j1939/transport.c static void j1939_session_completed(struct j1939_session *session) · line 1253
Quoted 10 lines
net/can/j1939/transport.c +4
-2/* 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); + } }j1939_session_deactivate_activate_next (session);
[Severity: High]
When se_skb is NULL here, doesn't this drop a fully received message without any notice and then complete the session as if it succeeded?
session->err is still 0 on this path. j1939_session_destroy() only queues RX_ABORT when err is set:
net/can/j1939/transport.c:j1939_session_destroy() {
...
} else if (session->err) {
j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
}
...
}That has these effects:
- Errqueue listeners that already got J1939_ERRQUEUE_RX_RTS and J1939_ERRQUEUE_RX_DPO never get a final event.
- Documentation/networking/j1939.rst describes J1939_EE_INFO_RX_ABORT as the event for an aborted reception, and it is never sent here.
- The peer has already been sent EOMA and thinks the transfer succeeded.
- The only trace is a netdev_dbg() in j1939_session_skb_get_by_offset().
The other RX lookup site treats the same condition as a fault and cancels the session:
net/can/j1939/transport.c:j1939_xtp_rx_dat_one() {
...
se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
if (!se_skb) {
netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__,
session);
goto out_session_cancel;
}
...
}The root cause named in the commit message is still in place. j1939_xtp_rx_dpo_one() stores the peer's DPO without checking it against pkt.total, total_message_size or the CTS window:
/* transmitted without problems */ session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
J1939_XTP_ABORT_BAD_EDPO_OFFSET and J1939_XTP_ABORT_EDPO_OUTOF_CTS are defined, but nothing sends them.
Also, j1939_session_skb_get() looks up the RX skb using pkt.dpo * 7:
offset_start = session->pkt.dpo * 7; return j1939_session_skb_get_by_offset(session, offset_start);
An RX session only has a single skb, allocated in j1939_session_fresh_new() and covering [0, total_message_size). So this lookup only fails when the peer has sent an out-of-range DPO.
Would it be better to validate the DPO in j1939_xtp_rx_dpo_one() and abort with J1939_XTP_ABORT_BAD_EDPO_OFFSET? Another option is to look up the RX skb without using dpo.
If the NULL check stays, could this branch at least set session->err, or cancel the session with an abort? That way the failure is reported instead of the session completing as a success.
The commit message also doesn't mention this change in behaviour.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117103516.535910-1-fuchen.dust%40gmail.com