[PATCH net v2] can: j1939: reject an out-of-range data packet offset

From: Piyus Kedia

Date: Tue Nov 24 2026 - 13:18:32 EST


j1939_session_completed() passes the skb from j1939_session_skb_get() to
j1939_sk_recv(), which dereferences it on its first statement:

se_skb = j1939_session_skb_get(session);
/* distribute among j1939 receivers */
j1939_sk_recv(session->priv, se_skb);

For a receive session that getter cannot fail by itself:
j1939_session_new()
queues one skb and j1939_session_fresh_new() sizes it to the whole message
with skb_put_zero(), and only j1939_session_skb_drop_old() and
j1939_session_destroy() ever remove one. What makes it return NULL is
where
it looks:

offset_start = session->pkt.dpo * 7;
return j1939_session_skb_get_by_offset(session, offset_start);

pkt.dpo is the ETP Data Packet Offset, and for a receive session the only
thing that writes it is j1939_xtp_rx_dpo_one(), which stores the peer's
24-bit value without comparing it to anything:

/* transmitted without problems */
session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);

So a peer can name a window past the end of the message, and the lookup at
dpo * 7 then finds nothing. An ETP RTS, an ETP DPO with an offset at or
beyond pkt.total, and an EOMA reach the NULL dereference:

j1939_xtp_rx_eoma()
j1939_xtp_rx_eoma_one()
j1939_session_completed()
j1939_sk_recv()
j1939_sk_recv_one() <- skb->len on a NULL skb

An out-of-range offset has no other legitimate effect either: every data
packet that follows is rejected by the "packet > session->pkt.total" test
in
j1939_xtp_rx_dat_one(), which computes packet from the same dpo, so the
session can never complete with data.

Reject it where it arrives, and cancel the session as
j1939_xtp_rx_dat_one() does for a packet number it cannot place. The
session's own data packets are already checked against pkt.total; this is
the same check for the offset they are counted from.

Look the received message up at offset 0 in j1939_session_completed() as
well. The DPO window is where the next data packet belongs, not where the
message starts, and at completion it is the whole message that goes to the
sockets - for a receive session that is the single skb at offset 0, so the
lookup cannot fail there. The NULL test stays as a second guard: if it
ever fires now, the queue has lost an skb that this file never removes
while the session is alive, which would be a kernel bug and not something
a peer did.

What that guard does NOT do is tell anyone. The session is deactivated
with session->err still 0, so j1939_session_destroy() queues no
J1939_ERRQUEUE_RX_ABORT and a socket watching the error queue, having
already seen RX_RTS and RX_DPO, hears nothing more; no abort frame goes to
the peer either. That is deliberate: setting session->err here would write
a field that __j1939_session_cancel() writes under
active_session_list_lock, and the path is no longer reachable from the wire
once the offset is checked above.

Found by AI-driven fuzzing built on top of syzkaller, and reproduced on
mainline 7.4-rc3 and 7.3.1 with a 61-line program over two sockets: KASAN
reports a NULL pointer dereference in j1939_sk_recv_one(). The ETP DPO
trigger above is the one in that reproducer; an EOMA alone, with dpo still
0, finds the zero-filled skb and delivers it, which is a separate question
about partly-filled receive buffers and not a memory-safety one.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@xxxxxxxxxx/

That link is the review this version answers, which is a reply inside the
thread of the posting it supersedes:

[PATCH net] can: j1939: do not pass a NULL session skb to the socket layer

The message-id of that mail is not recorded here - the send script writes no
log - so lore's thread view beside the review is where to find it.

Changes in v2, all from the review of v1:

- v1 described the wrong trigger. It said "an EOMA for a connection whose
data packets never arrived", and the review showed why that cannot be it:
j1939_session_new() queues one skb, j1939_session_fresh_new() sizes it to
the whole message with skb_put_zero(), and nothing removes it while the
session lives, so the queue is never empty for a receive session. What
fails is the lookup, because j1939_session_skb_get() looks at
pkt.dpo * 7 and pkt.dpo is whatever the peer's last ETP DPO carried. The
reproducer sends exactly that - an ETP RTS, an ETP DPO with offsets of
0xffffff, 0x100000, 0x010000 and 0x001000, then an EOMA - and its own
comments say so; the analysis simply did not reach the commit message.

- v2 fixes the cause instead of only the symptom, which is what the review
asked. j1939_xtp_rx_dpo_one() now rejects an offset that is not below
pkt.total and cancels the session, the same way j1939_xtp_rx_dat_one()
handles a packet number it cannot place. The data packets of a session
were already checked against pkt.total; this is the same check for the
offset they are counted from.

- j1939_session_completed() now looks the message up at offset 0 rather
than through the DPO window, the review's other suggestion. For a
receive session that is the single skb covering the whole message, so the
lookup cannot fail, and the NULL test stays only as a second guard.

- v1's commit message claimed "the two other callers of the getter in this
file already do" test the result, naming j1939_session_tx_dat() and
j1939_xtp_rx_dat_one(). Both call j1939_session_skb_get_by_offset(), not
j1939_session_skb_get(); the only other caller of the latter is
j1939_simple_txnext(), which does test it. That sentence is gone.

- The commit message now says what the guard does not do: the session is
deactivated with session->err still 0, so no J1939_ERRQUEUE_RX_ABORT is
queued and no abort frame is sent, which the review asked to have stated.

Measured, and not: the NULL dereference was reproduced on mainline 7.4-rc3
and on 7.3.1 by the 61-line program in this series' report thread, and the
KASAN splat is in j1939_sk_recv_one(). With v2 the same program is refused
at the DPO - the session is cancelled before an EOMA can complete it - and
no splat follows. Both hunks are compile-tested; checkpatch --strict
reports nothing. We have no second CAN interface here, so only the vcan
loopback path was exercised.
---
net/can/j1939/transport.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..225467843e42 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1242,10 +1242,15 @@ static void j1939_session_completed(struct j1939_session *session)
struct sk_buff *se_skb;

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);
+ /* the whole message, not the window the peer's last DPO
+ * happened to name
+ */
+ se_skb = j1939_session_skb_get_by_offset(session, 0);
+ if (se_skb) {
+ /* distribute among j1939 receivers */
+ j1939_sk_recv(session->priv, se_skb);
+ consume_skb(se_skb);
+ }
}

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(dat);
+ if (dpo >= session->pkt.total) {
+ netdev_info(session->priv->ndev,
+ "%s: 0x%p: data packet offset %u is not below the %u packet(s) of this session\n",
+ __func__, session, dpo, session->pkt.total);
+ j1939_session_timers_cancel(session);
+ j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
+ 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);

--
2.34.1