Skip to contentSkip to conversation

tty: n_gsm: fix gsm_queue() UAF and add a base regression test

PATCH Superseded Weiming Shi 3 participants 9 messages Jun 16 – Jun 30

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover tty: n_gsm: fix gsm_queue() UAF and add a base regression test

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Changes since v1:
- Fix the UAF by reference-pinning instead of a NULL check in the handlers; no gsm->mutex in the data path (Greg, Daniel).
- Pin every DLCI the dispatch touches, not just the addressed one:
MSC/RLS/PN operate on gsm->dlci[k] named in the payload.
- Add a base selftest (patch 2), as Greg asked.

Verification (KASAN, panic_on_warn=1): the originally reported splat is the gsm_control_reply() / CMD_TEST path (see the Link in patch 1). A reproducer targeting the MSC handler crashes the unpatched kernel and survives 270 race rounds on v2. The selftest passes on both the clean and patched kernel (pass:3 fail:0 skip:0).

Weiming Shi (2):
tty: n_gsm: fix use-after-free in gsm_queue() control frame dispatch
selftests: tty: add base regression test for n_gsm line discipline

 drivers/tty/n_gsm.c                          | 105 +++++-
 tools/testing/selftests/tty/.gitignore       |   1 +
 tools/testing/selftests/tty/Makefile         |   2 +-
 tools/testing/selftests/tty/config           |   1 +
 tools/testing/selftests/tty/tty_n_gsm_test.c | 344 +++++++++++++++++++
 5 files changed, 443 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/tty/tty_n_gsm_test.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 tty: n_gsm: fix use-after-free in gsm_queue() control frame dispatch

The receive worker (flush_to_ldisc -> gsmld_receive_buf -> gsm0_receive/ gsm1_receive -> gsm_queue) reads gsm->dlci[address] and dispatches the frame via dlci->data() without holding gsm->mutex. The control handlers reached through dlci->data() then re-read gsm->dlci[]: gsm_control_reply() re-reads gsm->dlci[0], while gsm_control_modem() (MSC), gsm_control_rls() (RLS) and gsm_control_negotiation() (PN) re-read gsm->dlci[addr] for the DLCI named in the command - a different channel from the one the frame was addressed to.

Concurrently GSMIOC_SETCONF -> gsm_config() -> gsm_cleanup_mux() takes gsm->mutex and releases every DLCI via gsm_dlci_release() -> dlci_put(). When the last reference is dropped the destructor gsm_dlci_free() clears gsm->dlci[addr] and frees the object. If the worker dereferences one of those DLCIs while it is being freed, it touches freed memory.

A peer that drives DLCI 0 control frames (e.g. CMD_TEST) while the mux owner reconfigures the line discipline with GSMIOC_SETCONF can therefore trigger a use-after-free:

  BUG: KASAN: slab-use-after-free in gsm_control_reply.isra.0
  Read of size 8 at addr ffff888029ae9000 by task kworker/u16:2/46
  Workqueue: events_unbound flush_to_ldisc
  Call Trace:
   gsm_control_reply.isra.0 (drivers/tty/n_gsm.c:1494)
   gsm_dlci_command (drivers/tty/n_gsm.c:2477)
   gsmld_receive_buf (drivers/tty/n_gsm.c:3616)
   tty_ldisc_receive_buf (drivers/tty/tty_buffer.c:398)
   tty_port_default_receive_buf (drivers/tty/tty_port.c:37)
   flush_to_ldisc (drivers/tty/tty_buffer.c:502)
   process_one_work
   worker_thread
   kthread

  Freed by task 5110:
   kfree
   gsm_cleanup_mux (drivers/tty/n_gsm.c:3161)
   gsmld_ioctl (drivers/tty/n_gsm.c:3415)
   tty_ioctl

Pin each DLCI across the dereference with its existing tty_port reference. gsm_dlci_open_get() looks gsm->dlci[addr] up under gsm->mutex and, if present, takes a dlci_get() reference before dropping the mutex; the caller releases it with gsm_dlci_unget() once it is done. While the reference is held the kref cannot reach zero, so gsm_dlci_free() cannot
run: the object stays live and gsm->dlci[addr] is not cleared. gsm_queue() pins the addressed DLCI for the UI/UIH dispatch, and gsm_control_modem(), gsm_control_rls() and gsm_control_negotiation() each pin the DLCI they operate on.

The reference is taken only under the mutex, around the lookup; the mutex is released before dlci->data() and before the data-path work (gsm_process_modem(), tty_flip_buffer_push(), gsm_data_queue(), ...), so the receive/transmit path is not serialised by gsm->mutex and its timing is unaffected.

Because a pinned DLCI can outlive the gsm_cleanup_mux() that released it, a subsequent GSMIOC_SETCONF may re-create a DLCI at the same address before the worker drops its reference. Make gsm_dlci_free() clear the slot only if it still points at the DLCI being freed, so the late destructor cannot wipe a freshly installed DLCI:

	cmpxchg(&dlci->gsm->dlci[dlci->addr], dlci, NULL);

Attaching the n_gsm line discipline requires CAP_NET_ADMIN (gsmld_open() uses capable(), not ns_capable()), so this is a local denial of service for a privileged mux owner whose control channel is driven by an untrusted peer on the serial link while it reconfigures; harden the receive path regardless.

---
 drivers/tty/n_gsm.c | 105 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 9 deletions(-)
drivers/tty/n_gsm.c +96 -9
index c13e050de..e1ab3a08f 100644
@@ -453,6 +453,8 @@ static const u8 gsm_fcs8[256] = {
 #define GOOD_FCS	0xCF
 
 static void gsm_dlci_close(struct gsm_dlci *dlci);
+static struct gsm_dlci *gsm_dlci_open_get(struct gsm_mux *gsm, unsigned int addr);
+static void gsm_dlci_unget(struct gsm_dlci *dlci);
 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk);
 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
@@ -1694,10 +1696,8 @@ static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
 		return;
 
 	addr >>= 1;
-	/* Closed port, or invalid ? */
-	if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
+	if (addr == 0)
 		return;
-	dlci = gsm->dlci[addr];
 
 	/* Must be at least one byte following the EA */
 	if ((cl - len) < 1)
@@ -1711,12 +1711,23 @@ static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
 	if (len < 1)
 		return;
 
+	/*
+	 * Pin the addressed DLCI across the dereference: a concurrent
+	 * GSMIOC_SETCONF -> gsm_cleanup_mux() can free it otherwise. Pinning
+	 * (not gsm->mutex over the whole handler) keeps the data path lock
+	 * free.
+	 */
+	dlci = gsm_dlci_open_get(gsm, addr);
+	if (dlci == NULL)
+		return;
+
 	tty = tty_port_tty_get(&dlci->port);
 	gsm_process_modem(tty, dlci, modem, cl);
 	if (tty) {
 		tty_wakeup(tty);
 		tty_kref_put(tty);
 	}
+	gsm_dlci_unget(dlci);
 	gsm_control_reply(gsm, CMD_MSC, data, clen);
 }
 
@@ -1746,15 +1757,26 @@ static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
 	/* Invalid DLCI? */
 	params = (struct gsm_dlci_param_bits *)data;
 	addr = FIELD_GET(PN_D_FIELD_DLCI, params->d_bits);
-	if (addr == 0 || addr >= NUM_DLCI || !gsm->dlci[addr]) {
+	if (addr == 0) {
+		gsm->open_error++;
+		return;
+	}
+
+	/*
+	 * Pin the addressed DLCI across the negotiation; see gsm_control_modem()
+	 * for why. Unlike MSC/RLS this DLCI need not be open, so pin first and
+	 * check the state afterwards.
+	 */
+	dlci = gsm_dlci_open_get(gsm, addr);
+	if (dlci == NULL) {
 		gsm->open_error++;
 		return;
 	}
-	dlci = gsm->dlci[addr];
 
 	/* Too late for parameter negotiation? */
 	if ((!cr && dlci->state == DLCI_OPENING) || dlci->state == DLCI_OPEN) {
 		gsm->open_error++;
+		gsm_dlci_unget(dlci);
 		return;
 	}
 
@@ -1765,6 +1787,7 @@ static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
 			pr_info("%s PN failed\n", __func__);
 		gsm->open_error++;
 		gsm_dlci_close(dlci);
+		gsm_dlci_unget(dlci);
 		return;
 	}
 
@@ -1785,6 +1808,7 @@ static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
 			pr_info("%s PN in invalid state\n", __func__);
 		gsm->open_error++;
 	}
+	gsm_dlci_unget(dlci);
 }
 
 /**
@@ -1800,6 +1824,7 @@ static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
 
 static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
 {
+	struct gsm_dlci *dlci;
 	struct tty_port *port;
 	unsigned int addr = 0;
 	u8 bits;
@@ -1816,15 +1841,21 @@ static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
 	if (len <= 0)
 		return;
 	addr >>= 1;
-	/* Closed port, or invalid ? */
-	if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
+	if (addr == 0)
 		return;
 	/* No error ? */
 	bits = *dp;
 	if ((bits & 1) == 0)
 		return;
 
-	port = &gsm->dlci[addr]->port;
+	/*
+	 * Pin the addressed DLCI across the dereference; see gsm_control_modem()
+	 * for why. gsm_cleanup_mux() can free it concurrently otherwise.
+	 */
+	dlci = gsm_dlci_open_get(gsm, addr);
+	if (dlci == NULL)
+		return;
+	port = &dlci->port;
 
 	if (bits & 2)
 		tty_insert_flip_char(port, 0, TTY_OVERRUN);
@@ -1835,6 +1866,7 @@ static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
 
 	tty_flip_buffer_push(port);
 
+	gsm_dlci_unget(dlci);
 	gsm_control_reply(gsm, CMD_RLS, data, clen);
 }
 
@@ -2694,7 +2726,14 @@ static void gsm_dlci_free(struct tty_port *port)
 	struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
 
 	timer_shutdown_sync(&dlci->t1);
-	dlci->gsm->dlci[dlci->addr] = NULL;
+	/*
+	 * Only clear the slot if it still points at us. A receive worker can
+	 * pin this DLCI across gsm_queue() dispatch with dlci_get(); if a
+	 * concurrent GSMIOC_SETCONF tears the mux down and re-creates a DLCI
+	 * at the same address before the worker drops its reference, the slot
+	 * already refers to the new DLCI and must not be cleared here.
+	 */
+	cmpxchg(&dlci->gsm->dlci[dlci->addr], dlci, NULL);
 	kfifo_free(&dlci->fifo);
 	while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
 		dev_kfree_skb(dlci->skb);
@@ -2711,6 +2750,42 @@ static inline void dlci_put(struct gsm_dlci *dlci)
 	tty_port_put(&dlci->port);
 }
 
+/**
+ *	gsm_dlci_open_get	-	look up a DLCI and pin it
+ *	@gsm: GSM mux
+ *	@addr: DLCI address
+ *
+ *	Look up gsm->dlci[addr] under gsm->mutex and, if present, take a
+ *	tty_port reference so it cannot be freed while a control-frame handler
+ *	dereferences it. A concurrent GSMIOC_SETCONF -> gsm_cleanup_mux()
+ *	releases DLCIs under the same mutex, so the lookup and the pin are
+ *	atomic with respect to the teardown. Returns the pinned DLCI or NULL.
+ *	The caller must release it with gsm_dlci_unget(). Callers that require
+ *	a particular state must check dlci->state themselves.
+ */
+static struct gsm_dlci *gsm_dlci_open_get(struct gsm_mux *gsm, unsigned int addr)
+{
+	struct gsm_dlci *dlci;
+
+	if (addr >= NUM_DLCI)
+		return NULL;
+	mutex_lock(&gsm->mutex);
+	dlci = gsm->dlci[addr];
+	if (dlci != NULL)
+		dlci_get(dlci);
+	mutex_unlock(&gsm->mutex);
+	return dlci;
+}
+
+/**
+ *	gsm_dlci_unget		-	drop a reference from gsm_dlci_open_get()
+ *	@dlci: DLCI to release
+ */
+static void gsm_dlci_unget(struct gsm_dlci *dlci)
+{
+	dlci_put(dlci);
+}
+
 static void gsm_destroy_network(struct gsm_dlci *dlci);
 
 /**
@@ -2839,11 +2914,23 @@ static void gsm_queue(struct gsm_mux *gsm)
 	case UI|PF:
 	case UIH:
 	case UIH|PF:
+		/*
+		 * Pin the DLCI so a concurrent gsm_cleanup_mux() cannot free
+		 * it while dlci->data() and the handlers it reaches use it.
+		 * The mutex is dropped before the dispatch, so the data path
+		 * is not serialised.
+		 */
+		mutex_lock(&gsm->mutex);
+		dlci = gsm->dlci[address];
 		if (dlci == NULL || dlci->state != DLCI_OPEN) {
+			mutex_unlock(&gsm->mutex);
 			gsm_response(gsm, address, DM|PF);
 			return;
 		}
+		dlci_get(dlci);
+		mutex_unlock(&gsm->mutex);
 		dlci->data(dlci, gsm->buf, gsm->len);
+		dlci_put(dlci);
 		break;
 	default:
 		goto invalid;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 selftests: tty: add base regression test for n_gsm line discipline

In reply to Weiming Shi

n_gsm has no selftest coverage. Add a base functional regression test that drives the line discipline over a local pty pair, so no real serial hardware or modem is required, and exercises the GSM 07.10 / 3GPP TS 27.010 basic-option mux from userspace.

The test attaches N_GSM to the pty master, then:

- basic: brings up the mux (SETCONF, initiator side), drives the
DLCI 0 control channel SABM/UA handshake and tears it down.
- getconf: round-trips GSMIOC_GETCONF/GSMIOC_SETCONF and checks the
configuration is preserved.
- data_dlci: opens a data DLCI (DLCI 1) via the SABM/UA exchange and verifies the responder side answers, covering the control -> data DLCI path.

Frames are encoded by hand against 3GPP TS 27.010 (address EA/C-R/DLCI bits, SABM/UA/UIH control fields, the reversed CRC-8 FCS) with the clause numbers referenced in the comments, so the test doubles as a small, readable description of the on-wire format.

It is a functional/regression test, not a race reproducer: it gives the subsystem a green baseline to catch behavioural regressions, including in the gsm_queue() control-frame dispatch path.

Wire it into the tty selftest Makefile, add CONFIG_N_GSM=y to the config fragment, and ignore the built binary. The test SKIPs cleanly when N_GSM is not built, /dev/ptmx is missing, or it lacks the capability to attach the ldisc.

Assisted-by Claude:claude-opus-4-8
---
 tools/testing/selftests/tty/.gitignore       |   1 +
 tools/testing/selftests/tty/Makefile         |   2 +-
 tools/testing/selftests/tty/config           |   1 +
 tools/testing/selftests/tty/tty_n_gsm_test.c | 344 +++++++++++++++++++
 4 files changed, 347 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/tty/tty_n_gsm_test.c
tools/testing/selftests/tty/.gitignore +1 -0
index 2453685d2..e3fcee15e 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 tty_tiocsti_test
 tty_tstamp_update
+tty_n_gsm_test
tools/testing/selftests/tty/Makefile +1 -1
index 7f6fbe5a0..ae546d0d4 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 CFLAGS = -O2 -Wall
-TEST_GEN_PROGS := tty_tstamp_update tty_tiocsti_test
+TEST_GEN_PROGS := tty_tstamp_update tty_tiocsti_test tty_n_gsm_test
 LDLIBS += -lcap
 
 include ../lib.mk
tools/testing/selftests/tty/config +1 -0
index c6373aba6..66a5ffc9e 100644
@@ -1 +1,2 @@
 CONFIG_LEGACY_TIOCSTI=y
+CONFIG_N_GSM=y
tools/testing/selftests/tty/tty_n_gsm_test.c new file +344 -0

344 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw
In reply to Weiming Shi
Quoted 10 lines · Weiming Shi

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Cool, but wow, that's complex for something that will never actually happen in a real device :)

So do we want to add that complexity? if so, why?

Ideally Daniel can verfiy this change is ok as they are the only known user here.

And thanks for the test patch, but that's just a functional test, while great to have, and not one that can actually mimic a real device with its timing constraints, right?

thanks,

greg k-h

#4 lore raw
Quoted 20 lines · Greg Kroah-Hartman
Quoted 10 lines · Weiming Shi

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Cool, but wow, that's complex for something that will never actually happen in a real device :)

So do we want to add that complexity? if so, why?

Ideally Daniel can verfiy this change is ok as they are the only known user here.

Quoted 7 lines

And thanks for the test patch, but that's just a functional test, while great to have, and not one that can actually mimic a real device with its timing constraints, right?

thanks,

greg k-h

Hi,

The complexity is just the cmpxchg() in gsm_dlci_free(). The actual UAF fix is only the tty_port pin (dlci_get/put) around the dispatch, which doesn't touch any fast path. The cmpxchg() only guards the teardown+recreate case Daniel mentioned, and isn't needed for the UAF.

So should I drop it and respin a v3 with the pin only? The use-after-free is fully fixed either way.

Daniel, does the pin approach look right to you?

And yes, the test is functional only (pty bring-up/teardown, no timing). It's the base regression test, not a race reproducer.

Thanks,
Weiming Shi

#5 lore raw
In reply to Weiming Shi
Quoted 24 lines

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Changes since v1:
- Fix the UAF by reference-pinning instead of a NULL check in the handlers; no gsm->mutex in the data path (Greg, Daniel).
- Pin every DLCI the dispatch touches, not just the addressed one:
MSC/RLS/PN operate on gsm->dlci[k] named in the payload.
- Add a base selftest (patch 2), as Greg asked.

Verification (KASAN, panic_on_warn=1): the originally reported splat is the gsm_control_reply() / CMD_TEST path (see the Link in patch 1). A reproducer targeting the MSC handler crashes the unpatched kernel and survives 270 race rounds on v2. The selftest passes on both the clean and patched kernel (pass:3 fail:0 skip:0).

Quoted 3 lines

Weiming Shi (2):
tty: n_gsm: fix use-after-free in gsm_queue() control frame dispatch
selftests: tty: add base regression test for n_gsm line discipline

So now there is a race on device node level. We have the old virtual gsmttys still waiting for the current pending operations to finish while the reconfiguration of the ldisc triggers a recreation of these. How is this handled?

PATCH 1/2:

Review

Quoted 4 lines
(quoted diff) +1 -2
-	if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
+	if (addr == 0)
 		return;
-	dlci = gsm->dlci[addr];

Control octets can be transmitted on non-control DLCIs in convergence layer type 2. You do not guard against invalid DLCIs anymore. Same for parameter negotiation and RLS.

Quoted 5 lines

This whole worker separation was added later, not in e1eaea46bb40. Maybe worth updating to the corresponding commit.
Also, the code comments have a strong AI flavor but AI use was only disclosed for PATCH 2/2.

Best regards,
Daniel Starke

#6 lore raw
Quoted 25 lines · Starke, Daniel <[email protected]> 于2026年6月17日周三 15:26写道:
Quoted 25 lines

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Changes since v1:
- Fix the UAF by reference-pinning instead of a NULL check in the handlers; no gsm->mutex in the data path (Greg, Daniel).
- Pin every DLCI the dispatch touches, not just the addressed one:
MSC/RLS/PN operate on gsm->dlci[k] named in the payload.
- Add a base selftest (patch 2), as Greg asked.

Verification (KASAN, panic_on_warn=1): the originally reported splat is the gsm_control_reply() / CMD_TEST path (see the Link in patch 1). A reproducer targeting the MSC handler crashes the unpatched kernel and survives 270 race rounds on v2. The selftest passes on both the clean and patched kernel (pass:3 fail:0 skip:0).

Quoted 9 lines
Quoted 3 lines

Weiming Shi (2):
tty: n_gsm: fix use-after-free in gsm_queue() control frame dispatch
selftests: tty: add base regression test for n_gsm line discipline

So now there is a race on device node level. We have the old virtual gsmttys still waiting for the current pending operations to finish while the reconfiguration of the ldisc triggers a recreation of these. How is this handled?

A pending gsmtty operation stays bound to the old object. gsmtty_install() takes a dlci_get() and the gsmtty ops use tty->driver_data, not gsm->dlci[], so an open fd keeps its own reference and never reaches the recreated dlci (a fresh gsm_dlci_alloc()). On teardown gsm_dlci_release() does tty_vhangup(), which unblocks the pending ops, and sets DLCI_CLOSED, which every gsmtty op checks first.

This is the existing tty_port refcount, and the receive path here uses the same dlci_get/put, so I don't think the pin adds a device-node race. But you know this hardware better than I do; if there's a path I'm missing, point me at it.

Quoted 11 lines

PATCH 1/2:

Review

Quoted 4 lines
(quoted diff) +1 -2
-     if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
+     if (addr == 0)
              return;
-     dlci = gsm->dlci[addr];

Control octets can be transmitted on non-control DLCIs in convergence layer type 2. You do not guard against invalid DLCIs anymore. Same for parameter negotiation and RLS.

The checks aren't dropped, just folded into gsm_dlci_open_get(), which returns NULL for addr >= NUM_DLCI and for an empty slot. But hiding them in the lookup makes the diff read like the guard is gone, which was a bad call. For v4, maybe we should keep the addr checks at the call sites and et the helper only do the locked lookup and pin, so the guard stays visible where the DLCI is used? Does that match what you'd expect?

Quoted 8 lines
Quoted 5 lines

This whole worker separation was added later, not in e1eaea46bb40. Maybe worth updating to the corresponding commit.

I'll find the commit that moved the eceive side onto the worker and point Fixes: at that.

Quoted 2 lines

Also, the code comments have a strong AI flavor but AI use was only disclosed for PATCH 2/2.

Sorry, I missed that tag. v4 will add

Thanks for the careful review.

Quoted 3 lines

Best regards,
Daniel Starke

#7 lore raw
Quoted 40 lines
Quoted 24 lines
Quoted 24 lines

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Changes since v1:
- Fix the UAF by reference-pinning instead of a NULL check in the handlers; no gsm->mutex in the data path (Greg, Daniel).
- Pin every DLCI the dispatch touches, not just the addressed one:
MSC/RLS/PN operate on gsm->dlci[k] named in the payload.
- Add a base selftest (patch 2), as Greg asked.

Verification (KASAN, panic_on_warn=1): the originally reported splat is the gsm_control_reply() / CMD_TEST path (see the Link in patch 1). A reproducer targeting the MSC handler crashes the unpatched kernel and survives 270 race rounds on v2. The selftest passes on both the clean and patched kernel (pass:3 fail:0 skip:0).

Quoted 9 lines
Quoted 3 lines

Weiming Shi (2):
tty: n_gsm: fix use-after-free in gsm_queue() control frame dispatch
selftests: tty: add base regression test for n_gsm line discipline

So now there is a race on device node level. We have the old virtual gsmttys still waiting for the current pending operations to finish while the reconfiguration of the ldisc triggers a recreation of these. How is this handled?

A pending gsmtty operation stays bound to the old object. gsmtty_install() takes a dlci_get() and the gsmtty ops use tty->driver_data, not gsm->dlci[], so an open fd keeps its own reference and never reaches the recreated dlci (a fresh gsm_dlci_alloc()). On teardown gsm_dlci_release() does tty_vhangup(), which unblocks the pending ops, and sets DLCI_CLOSED, which every gsmtty op checks first.

Quoted 4 lines

This is the existing tty_port refcount, and the receive path here uses the same dlci_get/put, so I don't think the pin adds a device-node race. But you know this hardware better than I do; if there's a path I'm missing, point me at it.

Sounds right. Thank you for the explanation. My tty internals understanding is rather shallow.

Quoted 12 lines
Quoted 11 lines

PATCH 1/2:

Review

Quoted 4 lines
(quoted diff) +1 -2
-     if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
+     if (addr == 0)
              return;
-     dlci = gsm->dlci[addr];

Control octets can be transmitted on non-control DLCIs in convergence layer type 2. You do not guard against invalid DLCIs anymore. Same for parameter negotiation and RLS.

Quoted 6 lines

The checks aren't dropped, just folded into gsm_dlci_open_get(), which returns NULL for addr >= NUM_DLCI and for an empty slot. But hiding them in the lookup makes the diff read like the guard is gone, which was a bad call. For v4, maybe we should keep the addr checks at the call sites and et the helper only do the locked lookup and pin, so the guard stays visible where the DLCI is used? Does that match what you'd expect?

Let's revisit the code for gsm_control_modem for example:

	len = gsm_read_ea_val(&addr, data, cl);
	if (len < 1)
		return;

	addr >>= 1;
	/* Closed port, or invalid ? */
	if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
		return;
	dlci = gsm->dlci[addr];

Without the addr >= NUM_DLCI part you access the dlci array at a possible out of bounds index if the received data was broken. Let us keep input data validation at system boundaries. You also risk dereferencing a null pointer if the requested DLCI has not been allocated, i.e. opened, yet. Hence, both checks should be kept in place.

Best regards,
Daniel Starke

#8 lore raw
Quoted 78 lines · Starke, Daniel <[email protected]> 于2026年6月19日周五 15:14写道:
Quoted 41 lines
Quoted 25 lines
Quoted 25 lines

The receive worker walks gsm->dlci[] without gsm->mutex while a concurrent GSMIOC_SETCONF -> gsm_cleanup_mux() frees the DLCIs, so the control handlers can dereference a freed gsm_dlci. v1's NULL check only narrowed the window; v2 fixes the use-after-free itself.

The fix pins each DLCI the dispatch dereferences with its existing tty_port reference (option 2), so the data path stays lock-free. See the patch 1 commit message for details, including why the late destructor uses cmpxchg() so it cannot wipe a re-created mux (Daniel's teardown concern).

Changes since v1:
- Fix the UAF by reference-pinning instead of a NULL check in the handlers; no gsm->mutex in the data path (Greg, Daniel).
- Pin every DLCI the dispatch touches, not just the addressed one:
MSC/RLS/PN operate on gsm->dlci[k] named in the payload.
- Add a base selftest (patch 2), as Greg asked.

Verification (KASAN, panic_on_warn=1): the originally reported splat is the gsm_control_reply() / CMD_TEST path (see the Link in patch 1). A reproducer targeting the MSC handler crashes the unpatched kernel and survives 270 race rounds on v2. The selftest passes on both the clean and patched kernel (pass:3 fail:0 skip:0).

Quoted 9 lines
Quoted 3 lines

Weiming Shi (2):
tty: n_gsm: fix use-after-free in gsm_queue() control frame dispatch
selftests: tty: add base regression test for n_gsm line discipline

So now there is a race on device node level. We have the old virtual gsmttys still waiting for the current pending operations to finish while the reconfiguration of the ldisc triggers a recreation of these. How is this handled?

A pending gsmtty operation stays bound to the old object. gsmtty_install() takes a dlci_get() and the gsmtty ops use tty->driver_data, not gsm->dlci[], so an open fd keeps its own reference and never reaches the recreated dlci (a fresh gsm_dlci_alloc()). On teardown gsm_dlci_release() does tty_vhangup(), which unblocks the pending ops, and sets DLCI_CLOSED, which every gsmtty op checks first.

Quoted 4 lines

This is the existing tty_port refcount, and the receive path here uses the same dlci_get/put, so I don't think the pin adds a device-node race. But you know this hardware better than I do; if there's a path I'm missing, point me at it.

Sounds right. Thank you for the explanation. My tty internals understanding is rather shallow.

Quoted 12 lines
Quoted 11 lines

PATCH 1/2:

Review

Quoted 4 lines
(quoted diff) +1 -2
-     if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
+     if (addr == 0)
              return;
-     dlci = gsm->dlci[addr];

Control octets can be transmitted on non-control DLCIs in convergence layer type 2. You do not guard against invalid DLCIs anymore. Same for parameter negotiation and RLS.

Quoted 6 lines

The checks aren't dropped, just folded into gsm_dlci_open_get(), which returns NULL for addr >= NUM_DLCI and for an empty slot. But hiding them in the lookup makes the diff read like the guard is gone, which was a bad call. For v4, maybe we should keep the addr checks at the call sites and et the helper only do the locked lookup and pin, so the guard stays visible where the DLCI is used? Does that match what you'd expect?

Let's revisit the code for gsm_control_modem for example:

        len = gsm_read_ea_val(&addr, data, cl);
        if (len < 1)
                return;

        addr >>= 1;
        /* Closed port, or invalid ? */
        if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
                return;
        dlci = gsm->dlci[addr];
Quoted 8 lines

Without the addr >= NUM_DLCI part you access the dlci array at a possible out of bounds index if the received data was broken. Let us keep input data validation at system boundaries. You also risk dereferencing a null pointer if the requested DLCI has not been allocated, i.e. opened, yet. Hence, both checks should be kept in place.

Best regards,
Daniel Starke

HI,
v3 has been sent. If there are any issues, please let me know.

Best regards,
Weiming Shi