* [PATCH net v4 1/2] can: j1939: do not pass a NULL skb to the socket layer
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 ` 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 7:47 ` [PATCH net v4 0/2] can: j1939: the NULL session skb, split in two Oleksij Rempel
2 siblings, 0 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-11-30 3:34 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
Marc Kleine-Budde, linux-can
Cc: kernel, linux-kernel, Kapil Vaswani
Check for a NULL skb in j1939_session_completed() and end the session
with EPROTO, so that sockets reading the error queue get an RX abort.
Print a rate-limited warning, because a peer can cause the missing skb.
A peer can send an ETP DPO at or beyond the end of the message. When
the session completes after that, j1939_session_completed() finds no
skb and passes NULL to j1939_sk_recv(), which dereferences it. The
dereference happens in the receive softirq, so it is fatal:
BUG: KASAN: null-ptr-deref in j1939_sk_recv+0xd4/0x3c0
Read of size 8 at addr 0000000000000018 by task repro/9528
<IRQ>
j1939_sk_recv+0xd4/0x3c0
j1939_xtp_rx_eoma+0x207/0x300
j1939_tp_recv+0x64c/0xa10
j1939_can_recv+0x51a/0x640
can_receive+0x19e/0x2a0
__netif_receive_skb+0x120/0x270
process_backlog+0x427/0xd50
net_rx_action+0x3f7/0x870
handle_softirqs+0x1e5/0x6a0
</IRQ>
netif_rx+0x89/0xa0
can_send+0x71f/0x7e0
raw_sendmsg+0x9ed/0xab0
Kernel panic - not syncing: Fatal exception in interrupt
This is only a mitigation, and it leaves the transport protocol alone
on purpose. The DPO is still not checked when it arrives, and no abort
frame is sent to the peer. These checks follow as a separate patch.
Found by AI-driven fuzzing built on top of syzkaller; the analysis was
then checked by hand against the code. Measured on current net.git main
with KASAN and kasan_multi_shot=1, over one vcan pair, with the same
reproducer each time: without this patch the report above and the panic;
with it no report and no panic, and the new warning fires once per round
of the reproducer, so the path is still being reached and is now handled.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Suggested-by: Oleksij Rempel <o.rempel@pengutronix.de>
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>
---
net/can/j1939/transport.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..fa30273df326 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1243,9 +1243,17 @@ static void j1939_session_completed(struct j1939_session *session)
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);
+ if (!se_skb) {
+ if (net_ratelimit())
+ netdev_warn(session->priv->ndev,
+ "%s: 0x%p: no skb found for completed session\n",
+ __func__, session);
+ session->err = EPROTO;
+ } else {
+ /* distribute among j1939 receivers */
+ j1939_sk_recv(session->priv, se_skb);
+ consume_skb(se_skb);
+ }
}
j1939_session_deactivate_activate_next(session);
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net v4 2/2] can: j1939: reject an out-of-range data packet offset
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 ` Piyus Kedia
2026-11-30 7:47 ` [PATCH net v4 0/2] can: j1939: the NULL session skb, split in two Oleksij Rempel
2 siblings, 0 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-11-30 3:34 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
Marc Kleine-Budde, linux-can
Cc: kernel, linux-kernel, Kapil Vaswani
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.
Reject an offset that is not below session->pkt.total and abort the
session, so the peer is told rather than left waiting, and complete a
receive session from offset 0 rather than from whatever window the last
DPO named.
Two consequences of aborting here, both handled:
- an EOMA can still arrive for a session already waiting for its abort
to go out. Completing it then would hand its data to the receiving
sockets and queue a second RX_ABORT, so j1939_xtp_rx_eoma_one() now
leaves a cancelled session to the abort path.
- j1939_session_cancel() is a no-op once the state is WAITING_ABORT, so
on a second out-of-range DPO it would not restart the timeout cancelled
just above. Deactivate the session instead, as
j1939_xtp_rx_rts_session_active() does, or it stays on the active list
with no timers and holds its skb and netdev references for good.
The NULL skb this used to produce is handled on its own, as a mitigation
for stable, by "can: j1939: do not pass a NULL skb to the socket layer".
This patch is the protocol fix and deliberately has no Cc: stable.
Found by AI-driven fuzzing built on top of syzkaller; the analysis was
then checked by hand against the code. Measured on current net.git main
with KASAN, over one vcan pair, on top of the mitigation: the offset is
refused when it arrives ("data packet offset 16777215 is not below the
1171 packet(s) of this session"), the mitigation's NULL-skb branch is
then never reached at all, and the EOMA guard above does fire for the
cancelled sessions. No KASAN report and no panic.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
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>
---
net/can/j1939/transport.c | 41 +++++++++++++++++++++++++++++++++++++--
1 file changed, 39 insertions(+), 2 deletions(-)
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index fa30273df326..187a7e6849eb 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1242,7 +1242,10 @@ static void j1939_session_completed(struct j1939_session *session)
struct sk_buff *se_skb;
if (!session->transmission) {
- se_skb = j1939_session_skb_get(session);
+ /* 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) {
if (net_ratelimit())
netdev_warn(session->priv->ndev,
@@ -1426,6 +1429,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) {
+ /* 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 */
@@ -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);
+ j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
+ 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;
+ }
+
/* 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 [flat|nested] 5+ messages in thread* Re: [PATCH net v4 0/2] can: j1939: the NULL session skb, split in two
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 7:47 ` Oleksij Rempel
2026-11-30 10:57 ` Piyus Kedia
2 siblings, 1 reply; 5+ messages in thread
From: Oleksij Rempel @ 2026-11-30 7:47 UTC (permalink / raw)
To: Piyus Kedia
Cc: Robin van der Gracht, Oliver Hartkopp, Marc Kleine-Budde,
linux-can, kernel, linux-kernel, Kapil Vaswani
Hi Piyus,
On Mon, Nov 30, 2026 at 09:04:20AM +0530, Piyus Kedia wrote:
> Posted as a series rather than as two independent patches because 2/2 does
> not apply without 1/2 - sent separately, the second one fails to apply for
> everyone and for the bots. If you would rather have 1/2 alone first and
> 2/2 after it lands, say so and I will resend that way.
Yes, please send 1/2 alone first.
Please use "can" instead of "net" in the subject prefix, for example
[PATCH can v5].
When you resend 1/2, you can add my Acked-by.
Regards,
Oleksij
^ permalink raw reply [flat|nested] 5+ messages in thread