mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [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; 4+ 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] 4+ messages in thread

end of thread, other threads:[~2026-11-24 18:18 UTC | newest]

Thread overview: 4+ 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
2026-11-24 18:18     ` Piyus Kedia

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®