* 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