Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH net v2] can: j1939: reject an out-of-range data packet offset
@ 2026-11-24 18:17 Piyus Kedia
  2026-11-25 18:18 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-24 18:17 UTC (permalink / raw)
  To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
	Marc Kleine-Budde, linux-can, netdev
  Cc: kernel, linux-kernel, Kapil Vaswani, davem, edumazet, kuba,
	pabeni, stable

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@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@kernel.org/

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] can: j1939: reject an out-of-range data packet offset
  2026-11-24 18:17 [PATCH net v2] can: j1939: reject an out-of-range data packet offset Piyus Kedia
@ 2026-11-25 18:18 ` sashiko-bot
  2026-11-26 15:03   ` Piyus Kedia
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-11-25 18:18 UTC (permalink / raw)
  To: Piyus Kedia, linux-can
  Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
	Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] can: j1939: session leak in j1939_xtp_rx_dpo_one() on repeated
  invalid DPO
- [Medium] can: j1939: aborted session completed in
  j1939_session_completed() on EOMA
--

commit e965a3918b3f2f0306bb887a70504c5f042637e7
Author: Piyus Kedia <piyus@iiitd.ac.in>

can: j1939: reject an out-of-range data packet offset

This commit rejects out-of-range ETP data packet offsets in
j1939_xtp_rx_dpo_one() and looks up the completed receive message skb at
offset 0 in j1939_session_completed().

Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@kernel.org/

> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..225467843e428 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);

[Severity: Medium]
Can an incoming EOMA frame still complete a receive session that
j1939_xtp_rx_dpo_one() already cancelled?

After j1939_session_cancel() transitions the session to
J1939_SESSION_WAITING_ABORT and queues J1939_ERRQUEUE_RX_ABORT, the
session stays on priv->active_session_list until the abort frame or the
500 ms rxtimer deactivates it.  If an EOMA frame arrives during that
window, j1939_xtp_rx_eoma() finds the session and calls
j1939_xtp_rx_eoma_one() -> j1939_session_completed() without checking
session->state or session->err.  Because j1939_session_completed() now
fetches offset 0 instead of pkt.dpo * 7, it delivers the aborted
session's skb to receiving sockets via j1939_sk_recv() and queues a
second J1939_ERRQUEUE_RX_ABORT when the session is destroyed.

> @@ -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;
> +	}

[Severity: High]
Does a second out-of-range DPO frame leak the session and its netdev
references?

On the first invalid DPO, j1939_session_cancel() sets session->state to
J1939_SESSION_WAITING_ABORT and starts the 500 ms rxtimer:

net/can/j1939/transport.c:j1939_session_cancel() {
	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);
	}
}

If a second invalid DPO arrives while the session is still in
J1939_SESSION_WAITING_ABORT (for example, when the peer source address is
not local so the looped-back abort frame is not received locally, or when
back-to-back DPO frames are already queued), j1939_session_timers_cancel()
cancels the 500 ms rxtimer and j1939_session_cancel() does not restart it.
The session then stays on priv->active_session_list with no running
timers, leaking the session, its receive skb, and its netdev_hold()
references so unregister_netdevice() hangs after the socket closes.

Should this check for J1939_SESSION_WAITING_ABORT and call
j1939_session_deactivate_activate_next(session), the same way
j1939_xtp_rx_rts_session_active() does?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124181757.638280-1-piyus@iiitd.ac.in?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] can: j1939: reject an out-of-range data packet offset
  2026-11-25 18:18 ` sashiko-bot
@ 2026-11-26 15:03   ` Piyus Kedia
  0 siblings, 0 replies; 3+ messages in thread
From: Piyus Kedia @ 2026-11-26 15:03 UTC (permalink / raw)
  To: Robin van der Gracht, Oleksij Rempel, Marc Kleine-Budde,
	Oliver Hartkopp, Vincent Mailhol, linux-can
  Cc: kernel, netdev, linux-kernel, kapil, sashiko-reviews

Thank you - both findings are right, and I have read each mechanism in the
tree rather than reasoning from the patch.  v3 fixes both and is posted
separately.

[High] session leak in j1939_xtp_rx_dpo_one() on repeated invalid DPO

Correct, and the leak is as described.  j1939_session_cancel() only acts
below J1939_SESSION_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);
	}

so on a second out-of-range DPO it does nothing, while the
j1939_session_timers_cancel() in front of it has already cancelled the
500 ms timeout unconditionally.  The session is then on
priv->active_session_list in WAITING_ABORT with no timers, holding its
receive skb and its netdev references, and unregister_netdevice() waits
behind them.

v3 deactivates the session in exactly that case, as
j1939_xtp_rx_rts_session_active() already does for a second RTS.  It tests
the state before the cancel, so the first bad DPO still cancels and lets
the abort frame go out with its timeout running, and only the session that
was already dying is deactivated here.

[Medium] aborted session completed in j1939_session_completed() on EOMA

Correct.  j1939_xtp_rx_eoma_one() checks the PGN and the message size and
not session->state, so an EOMA arriving inside the abort window completes
the session.

One clarification on where it comes from: j1939_session_completed() has
always called j1939_sk_recv() on a receive session, so an aborted session's
data could reach the sockets before this series too.  What v2 changed is
the extent - looking the message up at offset 0 hands over the whole
reassembled message rather than one window - and the second
J1939_ERRQUEUE_RX_ABORT you describe is queued either way.  Since v2 made
the delivery whole, the state test belongs with it: v3 returns from
j1939_xtp_rx_eoma_one() when the session is at or past WAITING_ABORT and
leaves the session to the abort path.

Tested on 7.3.1 with KASAN.  The reproducer from the v1 posting still ends
with the session rejected and no fault, and net/can/j1939/transport.o
builds
clean.

pw-bot: cr

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-26 15:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 18:17 [PATCH net v2] can: j1939: reject an out-of-range data packet offset Piyus Kedia
2026-11-25 18:18 ` sashiko-bot
2026-11-26 15:03   ` Piyus Kedia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox