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 can v5] can: j1939: reject an out-of-range data packet offset
Date: Mon, 30 Nov 2026 11:06:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-240910@kernel.org> (raw)
In-Reply-To: <20261130105233.2512536-1-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] can: j1939: uncancelled abort rxtimer in j1939_xtp_rx_dpo_one()
- [Medium] can: j1939: unlocked state check races with
  j1939_tp_rxtimer()
--

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

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

This commit validates the data packet offset in j1939_xtp_rx_dpo_one(),
aborts and deactivates the session when the offset is out of range, and
ignores EOMA and DPO frames arriving on sessions already waiting to abort.

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

> 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.
> +		 */
> +		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 */

[Severity: Medium]
Can checking session->state before cancelling session->rxtimer race with
j1939_tp_rxtimer() here and in j1939_xtp_rx_dpo_one()?

CPU0                                  CPU1
j1939_xtp_rx_eoma_one()
  if (session->state >=
      J1939_SESSION_WAITING_ABORT)
    /* false: state is ACTIVE */
                                      j1939_tp_rxtimer()
                                        j1939_session_list_lock(...)
                                        hrtimer_start(&session->rxtimer, ...)
                                        __j1939_session_cancel(session,
                                          J1939_XTP_ABORT_TIMEOUT)
                                          session->err = EHOSTUNREACH
                                          session->state =
                                            J1939_SESSION_WAITING_ABORT
                                        j1939_session_list_unlock(...)
                                        j1939_sk_errqueue(session,
                                          J1939_ERRQUEUE_RX_ABORT)
  j1939_session_timers_cancel(session)
  j1939_session_completed(session)    <-- completes aborted session

When j1939_tp_rxtimer() fires between the unlocked state check and
j1939_session_timers_cancel(), j1939_xtp_rx_eoma_one() cancels the newly
armed abort timer and completes the timed-out session, handing the data to
receiving sockets and queuing a second abort notification when
j1939_session_destroy() sees session->err set.  The same window exists in
j1939_xtp_rx_dpo_one() before j1939_tp_set_rxtimeout(), where an in-range
DPO re-arms rxtimer to 750 ms and queues J1939_ERRQUEUE_RX_DPO after
J1939_ERRQUEUE_RX_ABORT.

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

[Severity: High]
Does calling j1939_session_cancel() after j1939_session_timers_cancel()
leave session->rxtimer armed on the deactivated session?

Because session->state is J1939_SESSION_ACTIVE here, j1939_session_cancel()
starts session->rxtimer for J1939_XTP_ABORT_TIMEOUT_MS:

net/can/j1939/transport.c:j1939_session_cancel() {
	...
	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 unlinks the session from
priv->active_session_list and sets session->state to J1939_SESSION_DONE
while session->rxtimer is still running.  Neither the looped-back abort
frame in j1939_xtp_rx_abort_one() nor teardown in
j1939_cancel_active_session() can find the unlinked session to cancel the
timer, and when session->rxtimer expires 500 ms later, j1939_tp_rxtimer()
sees J1939_SESSION_DONE instead of J1939_SESSION_WAITING_ABORT, enters the
else branch, and queues a duplicate J1939_ERRQUEUE_RX_ABORT before final
session destruction queues another.

Cancelling the timers after j1939_session_cancel() (as
j1939_xtp_rx_rts_session_active() does before deactivating) would avoid
leaving the abort timer active.

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

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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=sashiko-outbox-240910@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