mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v3] can: j1939: reject an out-of-range data packet offset
@ 2026-11-26 15:03 Piyus Kedia
  2026-11-27 16:04 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ 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

j1939_xtp_rx_dpo_one() takes the peer's data packet offset and stores it
without checking it against the session:

	session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);

j1939_session_skb_get_by_offset() then looks for a buffered skb at
pkt.dpo * 7, and j1939_xtp_rx_dat_one() copies into the session's skb at
that offset.  An offset past the session's packet count has no skb behind
it, so the lookup returns NULL and the copy and the completion run on it.

Reject an offset that is not below session->pkt.total, and look the
completed receive message up at offset 0 rather than at whatever window
the peer's last DPO named.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Co-developed-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
v3: addresses the review of v2
    (https://lore.kernel.org/r/sashiko-outbox-233959@kernel.org)

    - a second out-of-range DPO no longer leaks the session.  Both
      findings there are right.  j1939_session_cancel() does nothing once
      the state is WAITING_ABORT, so on a second bad DPO the
      j1939_session_timers_cancel() in front of it cancelled the 500 ms
      timeout and nothing restarted it: the session stayed on
      priv->active_session_list with no timers, holding its skb and its
      netdev references, and unregister_netdevice() hangs behind that.
      v3 deactivates the session in that case, the way
      j1939_xtp_rx_rts_session_active() already does for a second RTS.
      The first bad DPO still cancels and lets the abort go out, so the
      change is confined to the session that was already dying.

    - an EOMA no longer completes a cancelled session.
      j1939_xtp_rx_eoma_one() checks the PGN and the size but not the
      state, so an EOMA arriving inside the abort window completed the
      session: with v2 looking the message up at offset 0 that handed the
      aborted session's data to the receiving sockets and queued a second
      J1939_ERRQUEUE_RX_ABORT.  The delivery itself predates this series -
      j1939_session_completed() always called j1939_sk_recv() - but v2 is
      what made it reach a whole message, so the state test belongs here.

v2: rejected the offset instead of tolerating it, and looked the completed
    message up at offset 0.

    How this was found: an AI-driven fuzzing harness built on syzkaller,
    developed jointly by Kapil Vaswani and myself, driving ETP sessions at
    j1939 under KASAN on x86_64.  The analysis and this changelog were
    written with the assistance of a large language model and checked by
    hand against the tree; the reproducer and the KASAN report in the v1
    posting were produced by us.  Both v3 changes come from addressing an
    AI-generated review of v2 on the list, and each mechanism it described
    was read in the tree before being acted on.

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

 net/can/j1939/transport.c | 49 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 44 insertions(+), 5 deletions(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..54e7ae9a88c3 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1242,10 +1242,15 @@
 	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);
@@ -1418,6 +1423,18 @@
 
 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
 
+	if (session->state >= J1939_SESSION_WAITING_ABORT) {
+		/* This session has been cancelled and is waiting for its abort
+		 * to go out.  Completing it here would hand its data to the
+		 * receiving sockets and queue a second RX_ABORT, so leave it to
+		 * the abort path.
+		 */
+		netdev_info(session->priv->ndev,
+			    "%s: 0x%p: EOMA on a cancelled session\n",
+			    __func__, session);
+		return;
+	}
+
 	session->pkt.tx_acked = session->pkt.total;
 	j1939_session_timers_cancel(session);
 	/* transmitted without problems */
@@ -1823,14 +1840,36 @@
 				 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) {
+		bool was_dying = session->state >= J1939_SESSION_WAITING_ABORT;
+
+		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);
+		if (was_dying)
+			/* The cancel above is a no-op once the state is
+			 * WAITING_ABORT, so it does not restart the timeout
+			 * this function just cancelled.  Deactivate instead,
+			 * as j1939_xtp_rx_rts_session_active() does, or the
+			 * session stays on the active list with no timers and
+			 * holds its skb and netdev references for good.
+			 */
+			j1939_session_deactivate_activate_next(session);
+		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.43.0


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

end of thread, other threads:[~2026-11-27 16:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 15:03 [PATCH net v3] can: j1939: reject an out-of-range data packet offset Piyus Kedia
2026-11-27 16:04 ` netdev-bot+sashiko

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

all inboxes | Powered by JetHome®