* [PATCH net v3] can: j1939: reject an out-of-range data packet offset
@ 2026-11-26 15:03 Piyus Kedia
2026-11-27 16:04 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-11-26 15:03 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Marc Kleine-Budde,
Oliver Hartkopp, Vincent Mailhol, linux-can
Cc: kernel, netdev, linux-kernel, kapil
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 copy and the completion run on it.
Reject an offset that is not below session->pkt.total, and look the
completed receive message up at offset 0 rather than at whatever window
the peer's last DPO named.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.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>
---
v3: addresses the review of v2
(https://lore.kernel.org/r/sashiko-outbox-233959@kernel.org)
- a second out-of-range DPO no longer leaks the session. Both
findings there are right. j1939_session_cancel() does nothing once
the state is WAITING_ABORT, so on a second bad DPO the
j1939_session_timers_cancel() in front of it cancelled the 500 ms
timeout and nothing restarted it: the session stayed on
priv->active_session_list with no timers, holding its skb and its
netdev references, and unregister_netdevice() hangs behind that.
v3 deactivates the session in that case, the way
j1939_xtp_rx_rts_session_active() already does for a second RTS.
The first bad DPO still cancels and lets the abort go out, so the
change is confined to the session that was already dying.
- an EOMA no longer completes a cancelled session.
j1939_xtp_rx_eoma_one() checks the PGN and the size but not the
state, so an EOMA arriving inside the abort window completed the
session: with v2 looking the message up at offset 0 that handed the
aborted session's data to the receiving sockets and queued a second
J1939_ERRQUEUE_RX_ABORT. The delivery itself predates this series -
j1939_session_completed() always called j1939_sk_recv() - but v2 is
what made it reach a whole message, so the state test belongs here.
v2: rejected the offset instead of tolerating it, and looked the completed
message up at offset 0.
How this was found: an AI-driven fuzzing harness built on syzkaller,
developed jointly by Kapil Vaswani and myself, driving ETP sessions at
j1939 under KASAN on x86_64. The analysis and this changelog were
written with the assistance of a large language model and checked by
hand against the tree; the reproducer and the KASAN report in the v1
posting were produced by us. Both v3 changes come from addressing an
AI-generated review of v2 on the list, and each mechanism it described
was read in the tree before being acted on.
Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@kernel.org/
net/can/j1939/transport.c | 49 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 44 insertions(+), 5 deletions(-)
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..54e7ae9a88c3 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1242,10 +1242,15 @@
struct sk_buff *se_skb;
if (!session->transmission) {
- se_skb = j1939_session_skb_get(session);
- /* distribute among j1939 receivers */
- j1939_sk_recv(session->priv, se_skb);
- consume_skb(se_skb);
+ /* the whole message, not the window the peer's last DPO
+ * happened to name
+ */
+ se_skb = j1939_session_skb_get_by_offset(session, 0);
+ if (se_skb) {
+ /* distribute among j1939 receivers */
+ j1939_sk_recv(session->priv, se_skb);
+ consume_skb(se_skb);
+ }
}
j1939_session_deactivate_activate_next(session);
@@ -1418,6 +1423,18 @@
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.
+ */
+ 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 +1840,36 @@
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);
+ j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
+ if (was_dying)
+ /* The cancel above is a no-op once the state is
+ * WAITING_ABORT, so it does not restart the timeout
+ * this function just cancelled. 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;
+ }
+
/* 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);
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH net v3] can: j1939: reject an out-of-range data packet offset
2026-11-26 15:03 [PATCH net v3] can: j1939: reject an out-of-range data packet offset Piyus Kedia
@ 2026-11-27 16:04 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 16:04 UTC (permalink / raw)
To: piyus
Cc: robin, o.rempel, mkl, socketcan, mailhol, linux-can, kernel,
netdev, linux-kernel, kapil, kuba
Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 2 · Low: 4
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 v3 notes say the patch fixes a session leak, but it fixes
only one path into it, and the patch's own new cancel makes another…
- [Medium] The new check in j1939_xtp_rx_eoma_one() stops a cancelled
session from being completed, but only on the EOMA path.
- [Medium] j1939_xtp_rx_dpo_one() reads `was_dying = session->state >=
J1939_SESSION_WAITING_ABORT` (transport.c:1852) without the…
- [Low] The new `session->state >= J1939_SESSION_WAITING_ABORT` check in
j1939_xtp_rx_eoma_one() (transport.c:1426) reads the state without…
- [Low] Both new comments are wrong about RX_ABORT.
- [Low] The changelog says that for an out-of-range offset "the lookup
returns NULL and the copy and the completion run on it".
- [Low] The main changelog, above '---', describes only the DPO range
check and the offset-0 lookup.
--
can: j1939: reject an out-of-range data packet offset
This patch rejects an ETP DPO whose data packet offset is not below
session->pkt.total. The session is cancelled, and it is deactivated if it
was already waiting for its abort. The patch also makes
j1939_session_completed() look the receive message up at offset 0, and
makes j1939_xtp_rx_eoma_one() ignore an EOMA on a session in WAITING_ABORT.
Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@kernel.org/
> 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 copy and the completion run on it.
[Severity: Low]
Is the part about the copy accurate? j1939_xtp_rx_dat_one() already checks
the bounds and checks for NULL before the memcpy:
se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
if (!se_skb) {
netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__,
session);
goto out_session_cancel;
}
The NULL only seems to reach the old j1939_session_completed(), which passed
the result of j1939_session_skb_get() straight to j1939_sk_recv().
Could the changelog also say how completion is reached after a bad DPO? That
would be an EOMA with sa equal to our address, either the echo of our own
EOMA or a spoofed one.
> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..54e7ae9a88c3e 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c
> @@ -1242,10 +1242,15 @@ static void j1939_session_completed(struct j1939_session *session)
> struct sk_buff *se_skb;
>
> if (!session->transmission) {
> - se_skb = j1939_session_skb_get(session);
> - /* distribute among j1939 receivers */
> - j1939_sk_recv(session->priv, se_skb);
> - consume_skb(se_skb);
> + /* the whole message, not the window the peer's last DPO
> + * happened to name
> + */
> + se_skb = j1939_session_skb_get_by_offset(session, 0);
> + if (se_skb) {
> + /* distribute among j1939 receivers */
> + j1939_sk_recv(session->priv, se_skb);
> + consume_skb(se_skb);
> + }
> }
[Severity: Medium]
The EOMA hunk below stops a session in WAITING_ABORT from being completed.
However, j1939_xtp_rx_dat_one() also calls j1939_session_completed(), and
its BAM final branch has no state check:
if (final) {
j1939_session_timers_cancel(session);
j1939_session_completed(session);
Can a cancelled BAM receive session still deliver its data here?
Take a BAM followed by one malformed DAT (skb->len != 8) or one with a bad
sequence number. That cancels the session: the state becomes WAITING_ABORT
and J1939_ERRQUEUE_RX_ABORT is queued.
j1939_xtp_rx_dat_one() never checks the state, and last_cmd (BAM or 0xff)
still passes the last_cmd switch. So the in-order DATs that arrive inside
the 500 ms abort window keep raising pkt.rx.
When pkt.rx reaches pkt.total, the abort timer is cancelled and the message
goes to j1939_sk_recv(). The sockets would see RX_ABORT, then the data, then
another RX_ABORT from j1939_session_destroy().
Would a state check in j1939_session_completed(), or under the list lock at
deactivation, cover both callers?
> @@ -1418,6 +1423,18 @@ 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) {
[Severity: Low]
This check also applies to transmit sessions. Before this patch, a TX
session in WAITING_ABORT that got the peer's EOMA was completed and
deactivated at once. Now it waits for its abort echo or for the 500 ms
abort timeout.
The changelog above the '---' only describes the DPO range check and the
offset-0 lookup. Two changes are explained only in the version notes, which
git am drops: this EOMA check, and the forced deactivation on a second bad
DPO. Could both be described in the permanent changelog?
The version notes also say:
The delivery itself predates this series -
j1939_session_completed() always called j1939_sk_recv() - but v2 is
what made it reach a whole message, so the state test belongs here.
Is that right? Before the series, j1939_session_skb_get() looked up offset
pkt.dpo * 7. A receive session's skb_queue holds a single skb at offset 0
sized total_message_size (see j1939_session_new()).
So any in-range dpo, including 0, already returned the whole message. An
EOMA on a session cancelled for any other reason already delivered it. That
would make this hunk a separate fix that matters for stable on its own.
> + /* 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.
> + */
> + 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: Low]
The state check above reads session->state without active_session_list_lock.
What happens if j1939_tp_rxtimer() or j1939_tp_txtimer() cancels the session
on another CPU after the check but before the
j1939_session_timers_cancel() and j1939_session_completed() calls here?
That cancel sets WAITING_ABORT, sends an abort and queues
J1939_ERRQUEUE_RX_ABORT. This CPU then cancels the new abort timer and
completes the session, so j1939_session_completed() still hands the message
to the sockets.
Does the check need to happen under the list lock, together with the state
change (deactivating the session or marking it as completing)? The message
would then be delivered only if that change succeeded.
> @@ -1823,14 +1840,36 @@ static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
[ ... ]
> + dpo = j1939_etp_ctl_to_packet(dat);
> + if (dpo >= session->pkt.total) {
> + bool was_dying = session->state >= J1939_SESSION_WAITING_ABORT;
[Severity: Medium]
was_dying is read without the list lock and before the timers are
cancelled. Can a timer callback move the session from ACTIVE to
WAITING_ABORT in between?
For example, j1939_tp_rxtimer() can fire on another CPU for the 750 ms DPO
timeout or the 1250 ms CTS timeout. Under the lock it re-arms the same
rxtimer:
j1939_session_get(session);
hrtimer_start(&session->rxtimer,
ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS),
HRTIMER_MODE_REL_SOFT);
__j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT);
j1939_tp_txtimer() can do the same on errors, through j1939_session_cancel().
Back here, j1939_session_rxtimer_cancel() waits for the callback and then
cancels the abort timer it just armed. j1939_session_cancel() sees
WAITING_ABORT and arms nothing. The stale was_dying == false then skips the
deactivation.
Would the receive session be left on the active list with no timer?
The comment below cites j1939_xtp_rx_rts_session_active() as its model, and
that function checks session->state after the cancel. Could this decision
also be made after the cancel? For example, j1939_session_cancel() could
report whether it armed the abort timer.
> +
> + 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_FAULT);
> + if (was_dying)
> + /* The cancel above is a no-op once the state is
> + * WAITING_ABORT, so it does not restart the timeout
[Severity: Low]
Is the cancel really a no-op here? j1939_session_cancel() queues RX_ABORT
outside its state guard:
j1939_session_list_unlock(session->priv);
if (!session->sk)
j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
So on the was_dying path every matching socket gets another RX_ABORT.
j1939_session_destroy() then queues one more, because session->err is set.
The new comment in j1939_xtp_rx_eoma_one() has a similar problem:
* 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.
The abort path also ends in j1939_session_destroy(). That function queues
RX_ABORT whenever !transmission && session->err, on the completion path as
well. Should both comments be corrected?
> + * this function just cancelled. 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]
This covers a second bad DPO. j1939_xtp_rx_dat_one() runs the same sequence
without any deactivation:
out_session_cancel:
kfree_skb(se_skb);
j1939_session_timers_cancel(session);
j1939_session_cancel(session, abort);
j1939_session_put(session);
j1939_xtp_rx_cts_one() does the same:
out_session_cancel:
j1939_session_timers_cancel(session);
j1939_session_cancel(session, err);
Can the DAT path be reached right after this rejection? Take an ETP receive
session after the echo of our own CTS, with last_cmd ==
J1939_ETP_CMD_CTS:
j1939_xtp_rx_dpo_one() /* dpo >= pkt.total */
j1939_session_cancel() /* WAITING_ABORT, 500 ms timer armed,
last_cmd unchanged */
Then the peer sends its next DAT:
j1939_xtp_rx_dat_one()
switch (session->last_cmd) -> default: goto out_session_cancel
j1939_session_timers_cancel() /* kills the abort timer */
j1939_session_cancel() /* already WAITING_ABORT, arms nothing */
A malformed DAT (skb->len != 8) gets there as well.
The echo of our own abort does not clean this up. It carries only
J1939_ECU_LOCAL_SRC, so j1939_tp_cmd_recv() calls
j1939_xtp_rx_abort(priv, skb, true). That only matches sessions with
transmission == true.
When the last bound socket closes, __j1939_rx_release() calls
j1939_priv_set(ndev, NULL), and j1939_netdev_notify() can no longer find
priv.
Would the session then keep its skb, its priv reference and its
netdev_hold() for good, with unregister_netdevice() waiting on it?
The underlying sequence existed before this patch: two bad DATs also
strand a session. The new DPO rejection makes it reachable with the
follow-up frame a sender would normally send. j1939_session_cancel() is
where the re-arm is skipped:
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);
}
Would it be better for j1939_session_cancel() itself to deactivate the
session, or re-arm the abort timer, when the session is already in
WAITING_ABORT? That would cover every caller and replace the was_dying
special case here and the similar one in j1939_xtp_rx_rts_session_active().
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126150339.2110025-1-piyus%40iiitd.ac.in
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 16:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 15:03 [PATCH net v3] can: j1939: reject an out-of-range data packet offset Piyus Kedia
2026-11-27 16:04 ` 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®