Linux CAN drivers development
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: fuchen.dust@gmail.com
Cc: robin@protonic.nl, o.rempel@pengutronix.de, mkl@pengutronix.de,
	socketcan@hartkopp.net, kernel@pengutronix.de,
	linux-can@vger.kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org, stable@vger.kernel.org, kuba@kernel.org
Subject: Re: [PATCH net v2] can: j1939: fix null-ptr-deref in j1939_session_completed()
Date: Tue, 24 Nov 2026 00:09:55 +0000	[thread overview]
Message-ID: <179547899509.3135.186731755576873205@kernel.org> (raw)
In-Reply-To: <20261123000916.181439-1-fuchen.dust@gmail.com>

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

Critical: 0 · High: 1 (1) · Medium: 0 · Low: 1

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] A peer can leave a receive session permanently stranded by
  sending two bad DPOs.
- [Low] The commit message says a bogus DPO can be sent "right before the
  last data packet of an otherwise valid transfer".

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch adds
  another caller.

--

can: j1939: fix null-ptr-deref in j1939_session_completed()

This patch makes j1939_xtp_rx_dpo_one() reject an ETP.CM_DPO on a receive
session when the packet number is >= session->pkt.total. It cancels the
session with J1939_XTP_ABORT_BAD_EDPO_OFFSET. It also adds a NULL check
on the reassembled skb in j1939_session_completed() and sets session->err
on that path.

> Any node on the CAN bus can send such a DPO, for example right before
> the last data packet of an otherwise valid transfer, so this is
> remotely triggerable.

[Severity: Low]
Is this reproduction sequence accurate? Once a DPO with packet number
D >= pkt.total is stored, the next data packet goes through
j1939_xtp_rx_dat_one():

	packet = (dat[0] - 1 + session->pkt.dpo);
	if (packet > session->pkt.total ||
	    (session->pkt.rx + 1) > session->pkt.total) {
		...
		goto out_session_cancel;
	}

	se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
	if (!se_skb) {
		...
		goto out_session_cancel;
	}

For any sequence byte dat[0] >= 1, packet is >= pkt.total. If packet is
greater than total, the bound check aborts. If packet equals total, the
offset lookup returns NULL, because the RX skb is only len bytes long.

Either way the session is cancelled and pkt.rx never reaches pkt.total.
No EOMA is sent, and j1939_session_completed() is never called.

As far as I can tell, only two sequences reach the NULL dereference on
the unpatched code:

- The bad DPO arrives after the final data packet but before the
  loopback echo of the receiver's own EOMA reaches
  j1939_xtp_rx_eoma_one()->j1939_session_completed().

- A DPO with D == pkt.total is followed by a data packet with sequence
  byte 0. Then packet == total - 1 and pkt.rx reaches total.

The test in the notes (DPO packet 256 for a 256 packet transfer) seems
to match the second sequence. The new dpo >= session->pkt.total check
blocks both, so only the description seems to be off.

Could the commit message describe a sequence that actually triggers the
crash?

> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..d3644c8239a4b 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c

[ ... ]

> @@ -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(skb->data);
> +	/* a receive session keeps the whole message in a single skb,
> +	 * reject a DPO which does not point into it
> +	 */
> +	if (!session->transmission && dpo >= session->pkt.total) {
> +		j1939_session_timers_cancel(session);
> +		j1939_session_cancel(session, J1939_XTP_ABORT_BAD_EDPO_OFFSET);
> +		return;
> +	}

[Severity: High]
Can a peer strand a receive session by sending two bad DPOs in a row?

j1939_session_cancel() only re-arms the rxtimer while the session is
not yet in 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);
	}

The sequence would be:

1. The first DPO with packet number >= pkt.total moves the session to
   WAITING_ABORT and arms the 500 ms abort rxtimer.

2. A second bad DPO arrives within 500 ms.
   j1939_session_get_by_addr_locked() does not filter on state, and the
   PGN is unchanged, so j1939_xtp_rx_cmd_bad_pgn() passes.

3. j1939_session_timers_cancel() cancels the pending abort rxtimer and
   drops its reference.

4. j1939_session_cancel() does nothing except queue another RX_ABORT.

Only the WAITING_ABORT branch of j1939_tp_rxtimer() deactivates such a
session, and that timer is now gone. Receive sessions have no sk, so
socket release does not clean them up. The local abort echo carries a
local SA, so it is only dispatched to transmitter sessions.

The session would then stay on active_session_list. It keeps its kref,
its reassembly skb (sized by the peer's RTS, up to tp_max_packet_size),
its netdev tracker and its priv reference. These are only released on
NETDEV_DOWN/UNREGISTER or when an RTS with the same PGN arrives on the
same tuple. An RTS with a different PGN on the same (sa, da) is refused
with BUSY.

Before this patch, a DPO in WAITING_ABORT always went through
j1939_tp_set_rxtimeout(session, 750), which re-armed the timer.

The RTS path already guards against this in
j1939_xtp_rx_rts_session_active():

		} else if (session->state == J1939_SESSION_WAITING_ABORT) {
			/* Force deactivation for the receiver.
			 * If we rely on the timer starting in j1939_session_cancel,
			 * a second RTS call here will cancel that timer and fail
			 * to restart it because the state is already WAITING_ABORT.
			 */
			j1939_session_deactivate_activate_next(session);
		}

Would it make sense to return early here when session->state >=
J1939_SESSION_WAITING_ABORT? Another option is to force
j1939_session_deactivate_activate_next() in that state, as the RTS path
does.

The out_session_cancel path of j1939_xtp_rx_dat_one() has the same
timers_cancel plus cancel pattern, for example with two data frames
whose length is not 8. That copy is pre-existing.

[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
However, the patch adds another caller of j1939_session_cancel(), which
holds the session list lock across a synchronous hrtimer cancel:

j1939_session_cancel()
  j1939_session_list_lock(session->priv)   /* spin_lock_bh */
  j1939_tp_set_rxtimeout()
    j1939_session_rxtimer_cancel()
      hrtimer_cancel(&session->rxtimer)    /* waits for running callback */

The rxtimer callback j1939_tp_rxtimer() takes the same lock in its
non-WAITING_ABORT, non-SIMPLE branch:

	} else {
		j1939_session_list_lock(session->priv);
		if (session->state >= J1939_SESSION_ACTIVE &&
		    session->state < J1939_SESSION_ACTIVE_MAX) {

Suppose the rxtimer fires on CPU B while CPU A is inside
j1939_session_cancel() with the session still ACTIVE. Can B then spin
on the list lock while A spins in hrtimer_cancel() waiting for B's
callback to finish?

The more realistic trigger is the existing
j1939_tp_txtimer()->j1939_session_cancel() path, which does not cancel
the rxtimer first. At this new call site, j1939_session_timers_cancel()
runs just before, so the window is very narrow. The rxtimer would have
to be re-armed from another CPU and expire almost immediately, and all
rxtimeouts are at least 250 ms.

Would it be better to cancel and re-arm the timers outside the list
lock?

> +
>  	/* 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);

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123000916.181439-1-fuchen.dust%40gmail.com

      reply	other threads:[~2026-11-24  0:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  0:09 [PATCH net v2] can: j1939: fix null-ptr-deref in j1939_session_completed() Xue Boyang
2026-11-24  0:09 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179547899509.3135.186731755576873205@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=fuchen.dust@gmail.com \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=robin@protonic.nl \
    --cc=socketcan@hartkopp.net \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox