mirror of https://lore.kernel.org/lkml/
 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-12-02  0:22 ` netdev-bot+sashiko
  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	[flat|nested] 2+ messages in thread

* Re: [PATCH can v5] can: j1939: reject an out-of-range data packet offset
  2026-11-30 10:52 [PATCH can v5] can: j1939: reject an out-of-range data packet offset Piyus Kedia
@ 2026-12-02  0:22 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-12-02  0:22 UTC (permalink / raw)
  To: piyus
  Cc: robin, o.rempel, socketcan, mkl, linux-can, kernel, linux-kernel,
	kapil, kuba

Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 5

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 new WAITING_ABORT guard in j1939_xtp_rx_dpo_one() (line
  1846) turns a sequence that used to clean itself up into a permanently…
- [Medium] In j1939_xtp_rx_dpo_one() (net/can/j1939/transport.c), the
  out-of-range DPO branch leaves the abort rxtimer armed on a session it…
- [Low] The new range check in j1939_xtp_rx_dpo_one() also runs on
  transmit sessions, and the commit message does not say so.
- [Low] The commit message says that, even with the range check,
  j1939_session_completed() takes the message from the window named by…
- [Low] The new comment in j1939_xtp_rx_eoma_one() (line 1421) and the
  commit message give, as a reason to leave a cancelled session to the…
- [Low] Both new guards, j1939_xtp_rx_dpo_one() line 1846 and
  j1939_xtp_rx_eoma_one() line 1421, test `session->state >=…
- [Low] The new `session->state >= J1939_SESSION_WAITING_ABORT` guards in
  j1939_xtp_rx_dpo_one() (line 1846) and j1939_xtp_rx_eoma_one() (line…

--

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

This patch makes j1939_xtp_rx_dpo_one() reject an ETP DPO whose offset is
not below session->pkt.total. Such a session is aborted with
J1939_XTP_ABORT_BAD_EDPO_OFFSET and then force-deactivated. The patch also
makes j1939_xtp_rx_dpo_one() and j1939_xtp_rx_eoma_one() ignore frames for
sessions that are already waiting for their abort.

Link: https://lore.kernel.org/all/sashiko-outbox-240339@kernel.org/

>  - 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.

[Severity: Low]
Is this accurate for the receive path?

j1939_session_completed() only calls j1939_session_skb_get() when
!session->transmission. A receive session has one skb that covers the
whole message. It is allocated in j1939_session_fresh_new():

    skb = alloc_skb(size, GFP_ATOMIC);
    ...
    skb_put_zero(skb, size);

That skb's skcb is copied from the RTS. j1939_can_recv() zeroes the RTS cb,
so skcb->offset is 0. j1939_session_skb_queue() is only called from the tx
path in socket.c, so a receive session never holds more than that one skb.

With dpo < pkt.total, dpo * 7 is below the message size. That means
j1939_session_skb_get_by_offset() always returns the single skb, and it
starts at message offset 0.

So once this check is in, doesn't multi-block ETP completion already
deliver the whole message? If so, the promised follow-up has nothing to
fix.

> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..37a253c892328 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.
> +		 */

[Severity: Low]
Is the "queue a second RX_ABORT" part of this reasoning right?

j1939_session_completed() doesn't queue RX_ABORT itself. The second one
comes from j1939_session_destroy() when the last reference drops, because
j1939_session_cancel() has already set session->err:

    } else if (session->err) {
            j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
    }

The abort path this defers to (j1939_tp_rxtimer() in WAITING_ABORT ->
j1939_session_deactivate_activate_next()) also reaches
j1939_session_destroy() with err set. It queues the same second RX_ABORT.
So does the new out-of-range DPO path in j1939_xtp_rx_dpo_one().

Only the "hand its data to the receiving sockets" half seems to apply.
Could the comment and the commit message be adjusted?

> +		if (net_ratelimit())
> +			netdev_info(session->priv->ndev,
> +				    "%s: 0x%p: EOMA on a cancelled session\n",
> +				    __func__, session);
> +		return;
> +	}

[Severity: Low]
This guard, and the matching one in j1939_xtp_rx_dpo_one(), tests
>= J1939_SESSION_WAITING_ABORT. In enum j1939_session_state, both
J1939_SESSION_ACTIVE_MAX and J1939_SESSION_DONE sort above WAITING_ABORT.

j1939_session_deactivate_locked() sets DONE on any deactivation, including
normal completion. A DPO or EOMA handler holds a reference taken under the
list lock. It can therefore see a session that another CPU deactivated
after the lookup.

In that case the comment and the "cancelled session" log messages say the
session was cancelled when it was not. Returning early is still correct for
DONE. Should the comment and log text describe the broader condition?

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

[Severity: High]
Can this early return strand a receive session permanently?

Take an ETP receive session where the peer's DPO arrives just after the
1250 ms rx timeout. By then j1939_tp_rxtimer() has moved the session to
WAITING_ABORT and armed the 500 ms abort timer. last_cmd is still
J1939_ETP_CMD_CTS, or J1939_ETP_CMD_RTS if no CTS went out.

With this patch the DPO returns here. last_cmd is not updated and the timer
is left as it is. WAITING_ABORT sessions stay on active_session_list, so the
ETP.DT frames that follow still find the session. In j1939_xtp_rx_dat_one()
the stale last_cmd hits the default case:

    switch (session->last_cmd) {
    case 0xff:
            break;
    case J1939_ETP_CMD_DPO:
            if (skcb->addr.type == J1939_ETP)
                    break;
            ...
    default:
            ...
            goto out_session_cancel;

and then:

 out_session_cancel:
    kfree_skb(se_skb);
    j1939_session_timers_cancel(session);
    j1939_session_cancel(session, abort);

j1939_session_timers_cancel() cancels the abort timer, which was the only
thing left that would deactivate the session. j1939_session_cancel() does
nothing in WAITING_ABORT.

The session then sits on the active list with no timers. It holds its
receive skb, its netdev reference and a priv reference. The new EOMA guard
also removes completion as a way out. The commit message names this
stranding as the problem the forced deactivation is meant to prevent.

Before this patch, the same DPO set last_cmd to DPO and re-armed the
rxtimer. The DTs then passed the switch, and j1939_tp_rxtimer() eventually
deactivated the session.

Receive sessions have sk == NULL, so j1939_sk_release() won't match them.
Once the last j1939 socket on the interface closes, __j1939_rx_release()
unregisters the CAN receiver and clears the priv. After that, neither RX
nor j1939_netdev_notify() can reach the session. Could
unregister_netdevice() then wait forever on that netdev reference?

The out_session_cancel pattern in j1939_xtp_rx_dat_one() and
j1939_xtp_rx_cts_one() was already there, but this guard adds a new route
into it. Would it help to force deactivation here, or to handle
WAITING_ABORT in out_session_cancel or j1939_session_cancel()?

[Severity: Low]
This state check, and the one in j1939_xtp_rx_eoma_one(), reads
session->state without priv->active_session_list_lock. Every writer of
session->state holds that lock. One of them is j1939_tp_rxtimer(), an
HRTIMER_MODE_REL_SOFT timer that can run on another CPU while this RX
softirq is in progress:

    j1939_session_list_lock(session->priv);
    if (session->state >= J1939_SESSION_ACTIVE &&
        session->state < J1939_SESSION_ACTIVE_MAX) {
            ...
            __j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT);
    }
    j1939_session_list_unlock(session->priv);

Suppose the guard sees ACTIVE and j1939_tp_rxtimer() cancels the session
right after. The DPO path still sets pkt.dpo and last_cmd. It then replaces
the abort timer with j1939_tp_set_rxtimeout(session, 750) and queues RX_DPO
after RX_ABORT. On the EOMA side, j1939_session_completed() can still
deliver the data of a session that has just been aborted.

These are the outcomes the guards are meant to prevent. The commit message
also lists, as a benefit, removing a state sample that "raced with
j1939_tp_rxtimer() setting WAITING_ABORT in between".

Would reading the state and acting on it under j1939_session_list_lock()
close this window?

> +
> +	dpo = j1939_etp_ctl_to_packet(dat);
> +	if (dpo >= session->pkt.total) {

[Severity: Low]
This check also runs on transmit sessions, because j1939_tp_cmd_recv()
passes the local node's own echoed DPO through
j1939_xtp_rx_dpo(priv, skb, true). Is that intended?

The transmitter takes its offset from j1939_session_tx_dpo():

    session->pkt.dpo = session->pkt.tx_acked;

and tx_acked comes from the peer's CTS in j1939_xtp_rx_cts_one(), with no
check against pkt.total:

    /* set packet counters only when not CTS(0) */
    session->pkt.tx_acked = pkt - 1;

So a peer CTS with a next-packet value beyond pkt.total makes the local
node send an out-of-range DPO. When that DPO loops back, this check aborts
the transmit session with J1939_XTP_ABORT_BAD_EDPO_OFFSET and the sending
socket gets EPROTO.

The abort reason on the bus then blames the DPO the local node sent. The
real fault is the peer's CTS, which J1939_XTP_ABORT_ECTS_TOO_BIG would
describe.

The missing CTS validation predates this patch. The transmit-side behaviour
is new, though, and the commit message doesn't mention it. Should it?

> +		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

[ ... ]

> +		j1939_session_deactivate_activate_next(session);
> +		return;
> +	}

[Severity: Medium]
Does this leave the abort rxtimer armed on a session that has already been
deactivated?

For an ACTIVE session, j1939_session_cancel() arms the abort timer and
takes a session reference:

    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);
    }

j1939_session_deactivate_activate_next() then sets the state to DONE and
removes the session from the active list. It does not cancel that timer.
j1939_xtp_rx_rts_session_active(), which the comment cites, calls
j1939_session_timers_cancel() again before its forced deactivation.

Because the session is no longer on the active list,
j1939_cancel_active_session() on NETDEV_DOWN or NETDEV_UNREGISTER cannot
cancel the timer. The receive skb, netdev and priv references stay held for
up to J1939_XTP_ABORT_TIMEOUT_MS.

When the timer fires, the state is DONE rather than WAITING_ABORT, so
j1939_tp_rxtimer() takes its final else branch. Nothing is re-armed, but
this still runs:

    if (!session->sk)
            j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);

j1939_session_destroy() then queues one more because session->err is set.
Doesn't that make three RX_ABORT notifications for one rejected DPO?

Would calling j1939_session_rxtimer_cancel() between j1939_session_cancel()
and the forced deactivation fix both effects?

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

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130105233.2512536-1-piyus%40iiitd.ac.in

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

end of thread, other threads:[~2026-12-02  0:22 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-12-02  0:22 ` 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®