Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH can v5] can: j1939: reject an out-of-range data packet offset
@ 2026-11-30 10:52 Piyus Kedia
  2026-11-30 11:06 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-11-30 10:52 UTC (permalink / raw)
  To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
	Marc Kleine-Budde, linux-can
  Cc: kernel, linux-kernel, Kapil Vaswani

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 session completes with nothing to
hand to its sockets.

Reject an offset that is not below session->pkt.total and abort the
session, so the peer is told rather than left waiting.

Abort with J1939_XTP_ABORT_BAD_EDPO_OFFSET, which is the J1939-21 reason
for exactly this and reports EPROTO to local sockets.  Earlier revisions
used J1939_XTP_ABORT_FAULT, which puts "retransmit limit reached" on the
bus and ENOTRECOVERABLE on the error queue.

Deactivate the session rather than leave it to its abort timeout.  An ETP
sender does not wait for the abort, so the ETP.DT frames already on their
way arrive next, fail j1939_xtp_rx_dat_one()'s last_cmd check and reach
out_session_cancel, which cancels the timers and then calls
j1939_session_cancel() - a no-op once the state is WAITING_ABORT.  That
leaves the session on the active list with no timers running, holding its
queued skb and its netdev reference for good.
j1939_xtp_rx_rts_session_active() forces deactivation for the receiver for
the same reason.  Deactivating unconditionally also removes the need to
sample session->state before cancelling, which raced with
j1939_tp_rxtimer() setting WAITING_ABORT in between.

Ignore a DPO on a session that is already waiting for its abort.  Without
that, an in-range offset on such a session still resets its timeout and
queues an RX_DPO behind the RX_ABORT already queued, and the data packets
that follow are copied into a session nobody is going to read.

An EOMA can arrive for such a session too.  Completing it then would hand
its data to the receiving sockets and queue a second RX_ABORT, so
j1939_xtp_rx_eoma_one() leaves a cancelled session to the abort path.

Both of those two return without ending the session, so a peer can repeat
them for as long as the abort timeout lasts; their messages are therefore
rate-limited.  The out-of-range message is not, because that path tears the
session down and because its neighbours in j1939_xtp_rx_dat_one() print the
same class of peer-triggered protocol error unconditionally.

Two related fixes are deliberately not in here, so that this one stands on
its own and can be applied in any order:

 - the NULL skb this used to produce is handled as a mitigation for stable
   by "can: j1939: do not pass a NULL skb to the socket layer".
 - j1939_session_completed() still takes the message from the window the
   peer's last DPO named, because j1939_session_skb_get() returns
   j1939_session_skb_get_by_offset(session, session->pkt.dpo * 7).  With
   the check above the offset is in range, so there is an skb there, but
   for a multi-block ETP transfer it is not the start of the message.
   That is its own fix and follows separately.

This patch is the protocol fix and deliberately has no Cc: stable.

Found by AI-driven fuzzing built on top of syzkaller; the analysis was
then checked by hand against the code.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Link: https://lore.kernel.org/all/sashiko-outbox-240339@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>
---

This is v4's 2/2 split: the range check, which stands alone, and the
completion-path fix that was bundled with it, which does not.  Only this half
is being sent now.

It no longer depends on "can: j1939: do not pass a NULL skb to the socket
layer" <https://lore.kernel.org/all/20261130101232.2464732-1-piyus@iiitd.ac.in/>.
That patch and this one touch disjoint functions, so they can be applied in
either order.  v4's 2/2 did not apply without it, which is why it was a
series.

Changes in v5:
 - sent on its own instead of as 2/2 of a series, as asked
 - [PATCH can] rather than [PATCH net], as asked
 - the completion-path change (j1939_session_completed() looking the message
   up at offset 0) is split out into its own patch.  It is the only part that
   touched the same lines as the NULL-skb patch, and it is a different bug:
   the lookup is wrong for a multi-block ETP transfer even when the offset is
   in range
 - abort with J1939_XTP_ABORT_BAD_EDPO_OFFSET, not J1939_XTP_ABORT_FAULT
 - deactivate the session unconditionally after the cancel, instead of only
   when it was already dying.  This is what stops the ETP.DT frames that
   follow a rejected DPO from stranding the session, and it also removes the
   sample of session->state that raced with j1939_tp_rxtimer()
 - ignore a DPO on a session that is already waiting for its abort
 - rate-limit the two guard messages, which return without ending the
   session, so a peer cannot use them to flood the log

The last four come from the Sashiko AI review of v4 2/2
<https://lore.kernel.org/all/sashiko-outbox-240339@kernel.org/>, which was
right on all of them.

v4 series: https://lore.kernel.org/all/20261130033422.2143435-1-piyus@iiitd.ac.in/
v4 2/2:    https://lore.kernel.org/all/20261130033422.2143435-3-piyus@iiitd.ac.in/
v3:        https://lore.kernel.org/all/20261124181757.638280-1-piyus@iiitd.ac.in/

 net/can/j1939/transport.c | 51 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..37a253c89232 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1418,6 +1418,19 @@ j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
 
 	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.
+		 */
+		if (net_ratelimit())
+			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 +1836,50 @@ 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);
 
+	if (session->state >= J1939_SESSION_WAITING_ABORT) {
+		/* This session has been cancelled and is waiting for its abort
+		 * to go out.  Taking a new offset here would reset its timeout
+		 * and queue an RX_DPO behind the RX_ABORT already queued, and
+		 * the data packets that follow would be copied into a session
+		 * nobody is going to read.  Leave it to the abort path.
+		 */
+		if (net_ratelimit())
+			netdev_info(session->priv->ndev,
+				    "%s: 0x%p: DPO on a cancelled session\n",
+				    __func__, session);
+		return;
+	}
+
+	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_BAD_EDPO_OFFSET);
+		/* Force deactivation, as j1939_xtp_rx_rts_session_active() does
+		 * for the receiver.  The abort frame has already gone out, and
+		 * an ETP sender does not wait for it: the data packets that
+		 * follow this DPO reach j1939_xtp_rx_dat_one(), whose last_cmd
+		 * check takes them to out_session_cancel, which cancels the
+		 * timers and then calls j1939_session_cancel() - a no-op on a
+		 * session already in WAITING_ABORT.  That would strand this
+		 * session on the active list with no timers, holding 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);
 

base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
-- 
2.34.1


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

end of thread, other threads:[~2026-11-30 11:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 10:52 [PATCH can v5] can: j1939: reject an out-of-range data packet offset Piyus Kedia
2026-11-30 11:06 ` sashiko-bot

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