Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Piyus Kedia" <piyus@iiitd.ac.in>, linux-can@vger.kernel.org
Cc: "Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH net v4 2/2] can: j1939: reject an out-of-range data packet offset
Date: Mon, 30 Nov 2026 03:51:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-240339@kernel.org> (raw)
In-Reply-To: <20261130033422.2143435-3-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] can: j1939: session and netdev leak in j1939_xtp_rx_dat_one()
  after bad DPO
- [Medium] can: j1939: j1939_xtp_rx_dpo_one() processes DPO on
  WAITING_ABORT session
- [Medium] can: j1939: TOCTOU race on was_dying in
  j1939_xtp_rx_dpo_one() leaks session
- [Medium] can: j1939: wrong abort code used for bad DPO in
  j1939_xtp_rx_dpo_one()
--

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

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

This commit validates the ETP data packet offset against
session->pkt.total in j1939_xtp_rx_dpo_one(), ignores EOMA frames on
cancelled sessions, and fetches the completed receive skb from offset 0.

> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index fa30273df3263..187a7e6849eb6 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c

[ ... ]

> @@ -1831,14 +1846,36 @@ 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) {
> +		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);

[Severity: Medium]
Can reading session->state into was_dying before cancelling the timers race
with j1939_tp_rxtimer() and leak the session?

CPU0                                  CPU1
j1939_xtp_rx_dpo_one()
  was_dying = session->state >= ...
    (false)
  netdev_info(...)
                                      j1939_tp_rxtimer()
                                        j1939_session_list_lock()
                                        hrtimer_start(&session->rxtimer, ...)
                                        __j1939_session_cancel()
                                          session->state =
                                            J1939_SESSION_WAITING_ABORT
                                        j1939_session_list_unlock()
  j1939_session_timers_cancel()
  j1939_session_cancel() (no-op)
  if (was_dying) (false)              <-- session stranded with no timers

j1939_session_timers_cancel() cancels the abort rxtimer started by CPU1,
j1939_session_cancel() sees J1939_SESSION_WAITING_ABORT and does not
restart it, and was_dying is stale so
j1939_session_deactivate_activate_next() is skipped, leaving the session on
active_session_list with its skb and netdev references held forever.

> +		j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);

[Severity: Medium]
Should this pass J1939_XTP_ABORT_BAD_EDPO_OFFSET instead of
J1939_XTP_ABORT_FAULT?

transport.c defines J1939_XTP_ABORT_BAD_EDPO_OFFSET (reason 12, mapped to
EPROTO) specifically for a bad EDPO offset, whereas J1939_XTP_ABORT_FAULT
sends SAE J1939-21 abort reason 5 (retransmit limit reached) on the bus and
reports ENOTRECOVERABLE to local sockets.

> +		if (was_dying)
> +			/* j1939_session_cancel() is a no-op once the state is
> +			 * WAITING_ABORT, so it does not restart the timeout
> +			 * cancelled just above.  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;
> +	}

[Severity: High]
Does rejecting an out-of-range DPO still leak the session and its netdev
reference when the ETP.DT data packet that follows it arrives?

In ETP, the sender transmits ETP.DT frames right after ETP.CM_DPO without
waiting for an acknowledgment.  When the first out-of-range DPO is rejected
here, session->state becomes J1939_SESSION_WAITING_ABORT with a 500 ms
abort rxtimer and session->last_cmd remains at RTS or CTS.  When the
following ETP.DT frame reaches j1939_xtp_rx_dat_one(), the last_cmd check
fails and jumps to out_session_cancel:

net/can/j1939/transport.c:j1939_xtp_rx_dat_one() {
	...
 out_session_cancel:
	kfree_skb(se_skb);
	j1939_session_timers_cancel(session);
	j1939_session_cancel(session, abort);
	j1939_session_put(session);
}

j1939_session_timers_cancel() cancels the 500 ms abort rxtimer and
j1939_session_cancel() is a no-op because session->state is already
J1939_SESSION_WAITING_ABORT, stranding the session on active_session_list
with no timers and leaking its queued skb and netdev reference.  Also, if a
prior block had left session->last_cmd at 0xff, j1939_xtp_rx_dat_one() does
not check for J1939_SESSION_WAITING_ABORT and continues copying ETP.DT
payload into the previous DPO window and rearming txtimer on the cancelled
session.

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

[Severity: Medium]
Does j1939_xtp_rx_dpo_one() still accept in-range DPOs and queue duplicate
abort notifications when session->state is already
J1939_SESSION_WAITING_ABORT?

Because session->state >= J1939_SESSION_WAITING_ABORT is only checked
inside the dpo >= session->pkt.total branch, an in-range DPO on a cancelled
session still updates session->pkt.dpo and session->last_cmd, resets
rxtimer to 750 ms, queues J1939_ERRQUEUE_RX_DPO after
J1939_ERRQUEUE_RX_ABORT, and lets subsequent ETP.DT frames write into the
session skb and schedule txtimer.  Even when dpo >= session->pkt.total on
an already-cancelled session, calling j1939_session_cancel() still reaches
j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT) outside the state guard
and queues a duplicate RX_ABORT on the socket error queue.

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

  reply	other threads:[~2026-11-30  3:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  3:34 [PATCH net v4 0/2] can: j1939: the NULL session skb, split in two Piyus Kedia
2026-11-30  3:34 ` [PATCH net v4 1/2] can: j1939: do not pass a NULL skb to the socket layer Piyus Kedia
2026-11-30  3:34 ` [PATCH net v4 2/2] can: j1939: reject an out-of-range data packet offset Piyus Kedia
2026-11-30  3:51   ` sashiko-bot [this message]
2026-11-30  7:47 ` [PATCH net v4 0/2] can: j1939: the NULL session skb, split in two Oleksij Rempel

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=sashiko-outbox-240339@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=piyus@iiitd.ac.in \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /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