* [BUG] can: j1939: NULL dereference when a peer completes a session with no data queued
@ 2026-11-23 14:36 Piyus Kedia
2026-11-23 14:36 ` [PATCH net] can: j1939: do not pass a NULL session skb to the socket layer Piyus Kedia
0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-23 14:36 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
Marc Kleine-Budde, linux-can, netdev
Cc: kernel, linux-kernel, Kapil Vaswani
Hello,
j1939_session_completed() passes the session's skb to the socket layer
without checking that there is one. A peer that completes a transport
session whose data packets never arrived makes j1939_sk_recv_one()
dereference NULL.
Reproduced on net.git main at af6737b90668 ("net: af_key: fix use-after-free
in dump_sp()"), 7.4.0-rc3-01164, with KASAN. A patch follows in this thread.
1. Root cause
net/can/j1939/transport.c, j1939_session_completed():
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);
}
j1939_session_skb_get() resolves the session's current offset through
j1939_session_skb_get_by_offset(), which returns NULL when no queued skb
covers it:
struct sk_buff *skb = NULL;
...
if (!skb)
netdev_dbg(...);
return skb;
j1939_sk_recv() then walks the bound sockets and j1939_sk_recv_one()
dereferences the skb on its first statement:
static void j1939_sk_recv_one(struct j1939_sock *jsk, struct sk_buff *oskb)
{
const struct j1939_sk_buff_cb *oskcb = j1939_skb_to_cb(oskb);
...
if (oskb->sk == &jsk->sk)
sk_buff::sk is at offset 0x18 on x86_64, which is the faulting address in
the report below. consume_skb() tolerates NULL, so the dereference is the
only fault on this path.
The same getter is checked by both of its other callers in this file -
j1939_session_tx_dat() returns -ENOBUFS, and j1939_xtp_rx_dat_one() warns and
cancels the session - so this looks like an omission rather than an
invariant.
2. How a peer reaches it
By sending an End Of Message Acknowledgement for a session with nothing
queued, so that the session is completed before any data packet arrives:
j1939_can_recv() net/can/j1939/main.c:108
j1939_tp_recv() net/can/j1939/transport.c:2201
j1939_tp_cmd_recv() net/can/j1939/transport.c:2153
j1939_xtp_rx_eoma() net/can/j1939/transport.c:1439
j1939_xtp_rx_eoma_one() :1424
j1939_session_completed() :1247
Everything on that path is driven by received CAN frames, so on a shared bus
this is reachable by any node; locally it needs only the ability to send on a
CAN interface (the reproducer uses vcan).
3. KASAN report
net.git main, 7.4.0-rc3-01164-gaf6737b90668, KASAN (full log attached as
kasan-j1939-null-session-skb-mainline-7.4-rc3.txt):
BUG: KASAN: null-ptr-deref in j1939_sk_recv+0xd4/0x3c0
Read of size 8 at addr 0000000000000018 by task x/9440
j1939_sk_recv
j1939_xtp_rx_eoma
j1939_tp_recv
j1939_can_recv
j1939_sk_recv_one() and j1939_session_completed() are inlined in that build.
On 7.3.1, where net/ is built without inlining, the same reproducer names
every frame separately (kasan-j1939-null-session-skb-7.3.1.txt):
BUG: KASAN: null-ptr-deref in j1939_sk_recv_one+0x34/0x170
net/can/j1939/socket.c:318
j1939_sk_recv_one net/can/j1939/socket.c:318
j1939_sk_recv net/can/j1939/socket.c:363
j1939_session_completed net/can/j1939/transport.c:1247
j1939_xtp_rx_eoma_one net/can/j1939/transport.c:1424
4. Impact
A NULL dereference in softirq context, from the CAN receive path: an oops
that kills the receiving context and, with panic_on_oops, the machine. It is
a denial of service, not a corruption - address 0x18 is not mappable on any
supported configuration.
net/can/j1939/socket.c and transport.c are byte-identical between 7.3.1 and
af6737b90668, so nothing in between changes this path.
5. Reproducer
j1939_eoma_null_session_skb.c in this directory (sent off-list on request;
see the note at the end). It brings up vcan, binds a J1939 socket, and
sends the connection-management frames that complete a session with no data.
It faults within a second on both trees. CONFIG_CAN, CONFIG_CAN_J1939,
CONFIG_CAN_VCAN and KASAN are required.
6. Suggested fix
Test the skb before using it, in the shape the file's other callers already
use:
+ if (se_skb) {
/* distribute among j1939 receivers */
- j1939_sk_recv(session->priv, se_skb);
- consume_skb(se_skb);
+ j1939_sk_recv(session->priv, se_skb);
+ consume_skb(se_skb);
+ }
Deliberately without a netdev_warn(): a session completed with no data
queued is the peer's protocol error, not a kernel one, and a warning on this
path would let a peer flood the log.
Cancelling the session rather than dropping the completion may be the more
correct protocol response. We left that alone so the patch stays a fix for
the dereference; say the word if you would rather have the session cancelled.
The patch follows in this thread. Checkpatch is clean.
7. What was and was not checked
Checked: the fault reproduces on net.git main under KASAN; the patched kernel
is clean on the same reproducer; ordinary J1939 transport sessions (a
multi-packet transfer over vcan, RTS/CTS/DT/EOMA) still complete and deliver
with the patch applied; both j1939 files are byte-identical between 7.3.1 and
af6737b90668.
Not checked: real CAN hardware - all of this is vcan under QEMU. We have not
tried this from a second node on a physical bus.
This was found by AI-driven fuzzing built on top of syzkaller; its analysis
was then checked by hand against the code and every claim above was
reproduced.
Kapil Vaswani
Piyus Kedia
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH net] can: j1939: do not pass a NULL session skb to the socket layer
2026-11-23 14:36 [BUG] can: j1939: NULL dereference when a peer completes a session with no data queued Piyus Kedia
@ 2026-11-23 14:36 ` Piyus Kedia
2026-11-24 14:41 ` netdev-bot+sashiko
0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-23 14:36 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
Marc Kleine-Budde, linux-can, netdev
Cc: kernel, linux-kernel, Kapil Vaswani, Piyus Kedia, stable
j1939_session_completed() hands the session's skb to the socket layer
without checking that there is one:
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);
}
j1939_session_skb_get() returns the skb covering the session's current
offset, or NULL when the queue holds none, and j1939_sk_recv_one() then
dereferences it on its first statement:
if (oskb->sk == &jsk->sk)
sk_buff::sk sits at offset 0x18, which is the address in the report below.
consume_skb() tolerates NULL, so the dereference is the only fault.
A peer reaches this by completing a transport session that has no data
queued - an EOMA for a connection whose data packets never arrived:
BUG: KASAN: null-ptr-deref in j1939_sk_recv_one+0x34/0x170
Read of size 8 at addr 0000000000000018 by task syz-executor
j1939_sk_recv_one net/can/j1939/socket.c:318
j1939_sk_recv net/can/j1939/socket.c:363
j1939_session_completed net/can/j1939/transport.c:1247
j1939_xtp_rx_eoma_one net/can/j1939/transport.c:1424
j1939_xtp_rx_eoma net/can/j1939/transport.c:1439
j1939_tp_cmd_recv net/can/j1939/transport.c:2153
j1939_tp_recv net/can/j1939/transport.c:2201
j1939_can_recv net/can/j1939/main.c:108
Test the skb before using it, as the two other callers of the getter in
this file already do (j1939_session_tx_dat() returns -ENOBUFS, and
j1939_xtp_rx_dat_one() warns and cancels the session). Nothing is warned
about here: a session completed with no data queued is a peer's protocol
error rather than a kernel one, and a netdev_warn() on this path would let
a peer flood the log.
Cancelling the session instead of dropping the completion may be the more
correct protocol behaviour; that is left alone deliberately, so this stays
a fix for the dereference.
How this was found: by AI-driven fuzzing built on top of syzkaller. Its
analysis was checked by hand against the code, the fault was reproduced on
net.git main under KASAN, and the patched kernel is clean on the same
reproducer with ordinary J1939 transport sessions unaffected.
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>
---
net/can/j1939/transport.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..af54e6a6bd71 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1243,9 +1243,11 @@ 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) {
+ /* 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 related [flat|nested] 3+ messages in thread
* Re: [PATCH net] can: j1939: do not pass a NULL session skb to the socket layer
2026-11-23 14:36 ` [PATCH net] can: j1939: do not pass a NULL session skb to the socket layer Piyus Kedia
@ 2026-11-24 14:41 ` netdev-bot+sashiko
0 siblings, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 14:41 UTC (permalink / raw)
To: piyus
Cc: robin, o.rempel, socketcan, mkl, linux-can, netdev, kernel,
linux-kernel, kapil, stable, kuba
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · 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] The commit message gives the wrong cause for the NULL, and the
real cause is not fixed.
- [Low] When se_skb is NULL, the new branch in j1939_session_completed()
still ends the receive session as a success.
--
can: j1939: do not pass a NULL session skb to the socket layer
This patch adds a NULL check on the skb returned by
j1939_session_skb_get() in j1939_session_completed(). A receive session
that completes without a matching skb no longer passes NULL to
j1939_sk_recv(). The session is still deactivated as before.
> j1939_session_skb_get() returns the skb covering the session's current
> offset, or NULL when the queue holds none, and j1939_sk_recv_one() then
> dereferences it on its first statement:
[ ... ]
> A peer reaches this by completing a transport session that has no data
> queued - an EOMA for a connection whose data packets never arrived:
[ ... ]
> Test the skb before using it, as the two other callers of the getter in
> this file already do (j1939_session_tx_dat() returns -ENOBUFS, and
> j1939_xtp_rx_dat_one() warns and cancels the session).
[Severity: Medium]
Is this the actual trigger? For a receive session, the queue does not
seem able to be empty while the session is alive.
j1939_xtp_rx_rts_session_new()->j1939_session_fresh_new()->
j1939_session_new() queues one skb:
net/can/j1939/transport.c:j1939_session_new() {
...
skb_queue_head_init(&session->skb_queue);
skb_queue_tail(&session->skb_queue, skb_get(skb));
...
}
j1939_session_fresh_new() then sizes that skb to the full message:
/* alloc data area */
skb_put_zero(skb, size);
Only j1939_session_skb_drop_old() and j1939_session_destroy() remove skbs
from the queue. drop_old() returns early when fewer than two are queued.
j1939_session_skb_get() looks up the skb by the DPO window, not by
offset 0:
offset_start = session->pkt.dpo * 7;
return j1939_session_skb_get_by_offset(session, offset_start);
So for a receive session it returns NULL only when pkt.dpo * 7 is at or
past total_message_size.
For a receive session, pkt.dpo is only written in j1939_xtp_rx_dpo_one().
That function stores the 24-bit value from the peer without checking it
against pkt.total:
/* transmitted without problems */
session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
j1939_xtp_rx_dat_one() does reject packet > session->pkt.total before it
does its lookup.
That means the splat needs an ETP RTS, then an ETP DPO with an
out-of-range offset, then an EOMA. An EOMA alone with dpo == 0 finds the
zero-filled skb. It then delivers zero or partly filled data to the
matching sockets, both before and after this patch.
Could the commit message describe the DPO trigger instead?
Would it also make sense to fix the cause? j1939_xtp_rx_dpo_one() could
reject or cancel the session when the DPO is >= pkt.total. Another option
is to fetch the RX message at offset 0 in j1939_session_completed(). The
NULL check would then stay as a second guard.
One more point: 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 j1939_session_skb_get() appears to be
j1939_simple_txnext().
> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..af54e6a6bd714 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c
> @@ -1243,9 +1243,11 @@ 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) {
> + /* distribute among j1939 receivers */
> + j1939_sk_recv(session->priv, se_skb);
> + consume_skb(se_skb);
> + }
> }
>
> j1939_session_deactivate_activate_next(session);
[Severity: Low]
When se_skb is NULL, the receive session still ends as a success. Is
user space told anything at all?
j1939_session_deactivate_locked() sets session->state to
J1939_SESSION_DONE and leaves session->err at 0. j1939_session_destroy()
only queues an abort for a receive session when err is set:
} else if (session->err) {
j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
}
Sockets using the error queue have already seen J1939_ERRQUEUE_RX_RTS and
J1939_ERRQUEUE_RX_DPO for this session. After that they get neither the
data nor J1939_ERRQUEUE_RX_ABORT.
The peer gets no abort frame either, because j1939_session_cancel() is
never called on this path.
j1939_xtp_rx_dat_one() handles a NULL lookup differently. It jumps to
out_session_cancel, which sets err, sends an abort, and later queues
RX_ABORT.
The commit message says cancelling was left out on purpose. Could it also
mention that error-queue users are left with a transfer that never
completes or aborts?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123143625.4021586-2-piyus%40iiitd.ac.in
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 14:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 14:36 [BUG] can: j1939: NULL dereference when a peer completes a session with no data queued Piyus Kedia
2026-11-23 14:36 ` [PATCH net] can: j1939: do not pass a NULL session skb to the socket layer Piyus Kedia
2026-11-24 14:41 ` 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