* [PATCH net v2] can: j1939: reject an out-of-range data packet offset
@ 2026-11-24 18:17 Piyus Kedia
2026-11-28 19:27 ` netdev-bot+sashiko
0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-24 18:17 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
Marc Kleine-Budde, linux-can, netdev
Cc: kernel, linux-kernel, Kapil Vaswani, davem, edumazet, kuba,
pabeni, stable
j1939_session_completed() passes the skb from j1939_session_skb_get() to
j1939_sk_recv(), which dereferences it on its first statement:
se_skb = j1939_session_skb_get(session);
/* distribute among j1939 receivers */
j1939_sk_recv(session->priv, se_skb);
For a receive session that getter cannot fail by itself:
j1939_session_new()
queues one skb and j1939_session_fresh_new() sizes it to the whole message
with skb_put_zero(), and only j1939_session_skb_drop_old() and
j1939_session_destroy() ever remove one. What makes it return NULL is
where
it looks:
offset_start = session->pkt.dpo * 7;
return j1939_session_skb_get_by_offset(session, offset_start);
pkt.dpo is the ETP Data Packet Offset, and for a receive session the only
thing that writes it is j1939_xtp_rx_dpo_one(), which stores the peer's
24-bit value without comparing it to anything:
/* transmitted without problems */
session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
So a peer can name a window past the end of the message, and the lookup at
dpo * 7 then finds nothing. An ETP RTS, an ETP DPO with an offset at or
beyond pkt.total, and an EOMA reach the NULL dereference:
j1939_xtp_rx_eoma()
j1939_xtp_rx_eoma_one()
j1939_session_completed()
j1939_sk_recv()
j1939_sk_recv_one() <- skb->len on a NULL skb
An out-of-range offset has no other legitimate effect either: every data
packet that follows is rejected by the "packet > session->pkt.total" test
in
j1939_xtp_rx_dat_one(), which computes packet from the same dpo, so the
session can never complete with data.
Reject it where it arrives, and cancel the session as
j1939_xtp_rx_dat_one() does for a packet number it cannot place. The
session's own data packets are already checked against pkt.total; this is
the same check for the offset they are counted from.
Look the received message up at offset 0 in j1939_session_completed() as
well. The DPO window is where the next data packet belongs, not where the
message starts, and at completion it is the whole message that goes to the
sockets - for a receive session that is the single skb at offset 0, so the
lookup cannot fail there. The NULL test stays as a second guard: if it
ever fires now, the queue has lost an skb that this file never removes
while the session is alive, which would be a kernel bug and not something
a peer did.
What that guard does NOT do is tell anyone. The session is deactivated
with session->err still 0, so j1939_session_destroy() queues no
J1939_ERRQUEUE_RX_ABORT and a socket watching the error queue, having
already seen RX_RTS and RX_DPO, hears nothing more; no abort frame goes to
the peer either. That is deliberate: setting session->err here would write
a field that __j1939_session_cancel() writes under
active_session_list_lock, and the path is no longer reachable from the wire
once the offset is checked above.
Found by AI-driven fuzzing built on top of syzkaller, and reproduced on
mainline 7.4-rc3 and 7.3.1 with a 61-line program over two sockets: KASAN
reports a NULL pointer dereference in j1939_sk_recv_one(). The ETP DPO
trigger above is the one in that reproducer; an EOMA alone, with dpo still
0, finds the zero-filled skb and delivers it, which is a separate question
about partly-filled receive buffers and not a memory-safety one.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@kernel.org/
That link is the review this version answers, which is a reply inside the
thread of the posting it supersedes:
[PATCH net] can: j1939: do not pass a NULL session skb to the socket layer
The message-id of that mail is not recorded here - the send script writes no
log - so lore's thread view beside the review is where to find it.
Changes in v2, all from the review of v1:
- v1 described the wrong trigger. It said "an EOMA for a connection whose
data packets never arrived", and the review showed why that cannot be it:
j1939_session_new() queues one skb, j1939_session_fresh_new() sizes it to
the whole message with skb_put_zero(), and nothing removes it while the
session lives, so the queue is never empty for a receive session. What
fails is the lookup, because j1939_session_skb_get() looks at
pkt.dpo * 7 and pkt.dpo is whatever the peer's last ETP DPO carried. The
reproducer sends exactly that - an ETP RTS, an ETP DPO with offsets of
0xffffff, 0x100000, 0x010000 and 0x001000, then an EOMA - and its own
comments say so; the analysis simply did not reach the commit message.
- v2 fixes the cause instead of only the symptom, which is what the review
asked. j1939_xtp_rx_dpo_one() now rejects an offset that is not below
pkt.total and cancels the session, the same way j1939_xtp_rx_dat_one()
handles a packet number it cannot place. The data packets of a session
were already checked against pkt.total; this is the same check for the
offset they are counted from.
- j1939_session_completed() now looks the message up at offset 0 rather
than through the DPO window, the review's other suggestion. For a
receive session that is the single skb covering the whole message, so the
lookup cannot fail, and the NULL test stays only as a second guard.
- v1's commit message claimed "the two other callers of the getter in this
file already do" test the result, naming j1939_session_tx_dat() and
j1939_xtp_rx_dat_one(). Both call j1939_session_skb_get_by_offset(), not
j1939_session_skb_get(); the only other caller of the latter is
j1939_simple_txnext(), which does test it. That sentence is gone.
- The commit message now says what the guard does not do: the session is
deactivated with session->err still 0, so no J1939_ERRQUEUE_RX_ABORT is
queued and no abort frame is sent, which the review asked to have stated.
Measured, and not: the NULL dereference was reproduced on mainline 7.4-rc3
and on 7.3.1 by the 61-line program in this series' report thread, and the
KASAN splat is in j1939_sk_recv_one(). With v2 the same program is refused
at the DPO - the session is cancelled before an EOMA can complete it - and
no splat follows. Both hunks are compile-tested; checkpatch --strict
reports nothing. We have no second CAN interface here, so only the vcan
loopback path was exercised.
---
net/can/j1939/transport.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..225467843e42 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);
+ }
}
j1939_session_deactivate_activate_next(session);
@@ -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(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_FAULT);
+ 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.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net v2] can: j1939: reject an out-of-range data packet offset
[not found] <sashiko-outbox-233959@kernel.org>
@ 2026-11-26 15:03 ` Piyus Kedia
0 siblings, 0 replies; 3+ 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, sashiko-reviews
Thank you - both findings are right, and I have read each mechanism in the
tree rather than reasoning from the patch. v3 fixes both and is posted
separately.
[High] session leak in j1939_xtp_rx_dpo_one() on repeated invalid DPO
Correct, and the leak is as described. j1939_session_cancel() only acts
below J1939_SESSION_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);
}
so on a second out-of-range DPO it does nothing, while the
j1939_session_timers_cancel() in front of it has already cancelled the
500 ms timeout unconditionally. The session is then on
priv->active_session_list in WAITING_ABORT with no timers, holding its
receive skb and its netdev references, and unregister_netdevice() waits
behind them.
v3 deactivates the session in exactly that case, as
j1939_xtp_rx_rts_session_active() already does for a second RTS. It tests
the state before the cancel, so the first bad DPO still cancels and lets
the abort frame go out with its timeout running, and only the session that
was already dying is deactivated here.
[Medium] aborted session completed in j1939_session_completed() on EOMA
Correct. j1939_xtp_rx_eoma_one() checks the PGN and the message size and
not session->state, so an EOMA arriving inside the abort window completes
the session.
One clarification on where it comes from: j1939_session_completed() has
always called j1939_sk_recv() on a receive session, so an aborted session's
data could reach the sockets before this series too. What v2 changed is
the extent - looking the message up at offset 0 hands over the whole
reassembled message rather than one window - and the second
J1939_ERRQUEUE_RX_ABORT you describe is queued either way. Since v2 made
the delivery whole, the state test belongs with it: v3 returns from
j1939_xtp_rx_eoma_one() when the session is at or past WAITING_ABORT and
leaves the session to the abort path.
Tested on 7.3.1 with KASAN. The reproducer from the v1 posting still ends
with the session rejected and no fault, and net/can/j1939/transport.o
builds
clean.
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net v2] can: j1939: reject an out-of-range data packet offset
2026-11-24 18:17 [PATCH net v2] can: j1939: reject an out-of-range data packet offset Piyus Kedia
@ 2026-11-28 19:27 ` netdev-bot+sashiko
0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 19:27 UTC (permalink / raw)
To: piyus
Cc: robin, o.rempel, socketcan, mkl, linux-can, netdev, kernel,
linux-kernel, kapil, davem, edumazet, kuba, pabeni, stable
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · 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).
- [Medium] In net/can/j1939/transport.c, the new out-of-range check in
j1939_xtp_rx_dpo_one() cancels the session with J1939_XTP_ABORT_FAULT.
- [Medium] The new reject path in j1939_xtp_rx_dpo_one() calls
j1939_session_timers_cancel() and then j1939_session_cancel() without…
- [Low] The commit message's reason why rejecting an out-of-range DPO
loses nothing is wrong at the boundary.
--
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 pkt.total, and cancel the session when that happens. It also
changes j1939_session_completed() to look up the received message at
offset 0 instead of at the DPO window. The NULL test stays there as a
second guard.
Link: https://lore.kernel.org/all/179553126874.3135.14817947057284289051@kernel.org/
> An out-of-range offset has no other legitimate effect either: every data
> packet that follows is rejected by the "packet > session->pkt.total" test
> in
> j1939_xtp_rx_dat_one(), which computes packet from the same dpo, so the
> session can never complete with data.
[Severity: Low]
Does this hold at the boundary? j1939_xtp_rx_dat_one() does:
packet = (dat[0] - 1 + session->pkt.dpo);
if (packet > session->pkt.total ||
(session->pkt.rx + 1) > session->pkt.total) {
With dpo == total and dat[0] == 1, packet == total passes the > test. The
packet is only rejected later, when j1939_session_skb_get_by_offset()
returns NULL.
With dpo == total and dat[0] == 0 (the sequence number is never checked
for zero), packet works out to total - 1. That passes the test, the
lookup finds the message skb, and the payload is copied into the last
7-byte slot.
The new dpo >= session->pkt.total check closes both cases, so the code
itself is fine. Could the commit message describe the bounds check in
j1939_xtp_rx_dat_one() more precisely? Accepting sequence number 0 may
also be worth a separate look.
> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..225467843e428 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(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_FAULT);
[Severity: Medium]
Is J1939_XTP_ABORT_FAULT the right abort reason here? Its description in
enum j1939_xtp_abort is "Maximal retransmit request limit reached", and
j1939_xtp_abort_to_errno() maps it to ENOTRECOVERABLE.
The file already defines a code for this exact condition, and nothing in
net/can/j1939/ uses it yet:
J1939_XTP_ABORT_BAD_EDPO_OFFSET = 12,
/* Bad EDPO offset */
j1939_xtp_abort_to_errno() maps that one to EPROTO.
__j1939_session_cancel() passes the chosen code to the peer and to the
error queue:
session->err = j1939_xtp_abort_to_errno(priv, err);
...
j1939_xtp_tx_abort(priv, &session->skcb,
!session->transmission,
err, session->skcb.addr.pgn);
So the TP.Conn_Abort frame tells the peer reason 5 instead of 12. The
J1939_ERRQUEUE_RX_ABORT queued for this receive session reports
ENOTRECOVERABLE.
Documentation/networking/j1939.rst lists "Invalid PGN or offset in
EDPO/ECTS" under EPROTO. It keeps ENOTRECOVERABLE for the retransmit
limit. The DPO check in j1939_xtp_rx_cmd_bad_pgn() also uses the specific
J1939_XTP_ABORT_BAD_EDPO_PGN.
The commit message gives j1939_xtp_rx_dat_one() as the precedent. Its
FAULT, though, covers data packet numbers, not EDPO offsets. Would
J1939_XTP_ABORT_BAD_EDPO_OFFSET fit better here?
[Severity: Medium]
Can this path lose the abort timeout? j1939_xtp_rx_dpo() still finds
sessions in J1939_SESSION_WAITING_ABORT, because
j1939_session_get_by_addr_locked() matches only on address and direction.
Suppose a second out-of-range DPO arrives after the first one cancelled
the session, but before the echo of our abort frame is processed:
first bad DPO
j1939_session_timers_cancel()
j1939_session_cancel()
j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS)
__j1939_session_cancel()
session->state = J1939_SESSION_WAITING_ABORT
second bad DPO
j1939_session_timers_cancel() <- cancels the abort rxtimer
j1939_session_cancel()
state < J1939_SESSION_WAITING_ABORT is false, so no re-arm
j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT)
The WAITING_ABORT branch of j1939_tp_rxtimer() is the only timer-driven
cleanup for this session. j1939_xtp_tx_abort() ignores allocation and
can_send() failures, so the abort frame may never be sent or echoed. In
that case, does the session stay on active_session_list, holding its
references and receive skb? It would only go away on a new RTS from the
peer, a peer abort, or the interface going down.
Each such DPO also queues another J1939_ERRQUEUE_RX_ABORT, because that
call in j1939_session_cancel() is unconditional when session->sk is NULL.
j1939_xtp_rx_rts_session_active() already works around the same problem
for a second RTS:
} 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);
}
Before this patch, a DPO in this state went through
j1939_tp_set_rxtimeout(session, 750), which always re-armed the timer.
Does the new path need a similar state check or forced deactivation?
> + return;
> + }
> +
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124181757.638280-1-piyus%40iiitd.ac.in
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-28 19:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 18:17 [PATCH net v2] can: j1939: reject an out-of-range data packet offset Piyus Kedia
2026-11-28 19:27 ` netdev-bot+sashiko
[not found] <sashiko-outbox-233959@kernel.org>
2026-11-26 15:03 ` Piyus Kedia
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox