Netdev List
 help / color / mirror / Atom feed
* [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes
@ 2026-11-30  4:00 Shubham Antil
  2026-11-30  4:00 ` [PATCH net v9 1/2] nfc: nci: uart: protect the driver registry Shubham Antil
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Shubham Antil @ 2026-11-30  4:00 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc, Shubham Antil

The v8 series combined several independent NFC fixes.  This submission
contains only the NCI UART driver-selection fixes and splits the registry
lifetime change from the per-line-discipline selection change.

The first patch protects the transport registry and pins a transport
module before copying its template.  The second patch serializes the
existing allocate, open, and publish sequence with a sleepable mutex and
keeps failed selections retryable.

The issues were found during an AI-assisted source audit using OpenAI
Codex, gpt-5.6-sol.  Patch development and test planning used the same
assistant.

Each mail patch was applied independently to the base below on the Linux
test host.  The affected NFC objects built after every patch with W=1 and
sparse.  No physical Marvell controller was available.

Changes since v8:

  - send the driver-selection fixes as an independent patch set
  - separate registry lifetime protection from selection serialization
  - retain late publication and use a small selection mutex
  - add the requested AI-assistance trailer

Shubham Antil (2):
  nfc: nci: uart: protect the driver registry
  nfc: nci: uart: serialize driver selection

 net/nfc/nci/uart.c | 61 +++++++++++++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 20 deletions(-)


base-commit: 30c71476d842b094651bd7712f6c8ee8716147d0
-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net v9 1/2] nfc: nci: uart: protect the driver registry
  2026-11-30  4:00 [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes Shubham Antil
@ 2026-11-30  4:00 ` Shubham Antil
  2026-11-30  4:00 ` [PATCH net v9 2/2] nfc: nci: uart: serialize driver selection Shubham Antil
  2026-11-30  4:03 ` [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes netdev-bot+sinfo
  2 siblings, 0 replies; 4+ messages in thread
From: Shubham Antil @ 2026-11-30  4:00 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc, Shubham Antil

nci_uart_set_driver() reads a transport template before pinning its
module, while unregister can remove that template concurrently.
Registration also indexes the global table without validating a negative
enum value.

Serialize lookup, registration, and removal with a mutex. Pin the
transport module while the template is protected, copy it only after the
pin succeeds, and validate the driver index before accessing the table.
Drop the module reference if transport open fails.

Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. Patch development and test planning used the same assistant.

Fixes: 9961127d4bce ("NFC: nci: add generic uart support")
Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol)
Signed-off-by: Shubham Antil <shubham@octane.security>
---
 net/nfc/nci/uart.c | 49 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/net/nfc/nci/uart.c b/net/nfc/nci/uart.c
index aa20e8603..ee3d26ee6 100644
--- a/net/nfc/nci/uart.c
+++ b/net/nfc/nci/uart.c
@@ -13,6 +13,7 @@
 
 #include <linux/kernel.h>
 #include <linux/init.h>
+#include <linux/mutex.h>
 #include <linux/types.h>
 #include <linux/fcntl.h>
 #include <linux/interrupt.h>
@@ -35,6 +36,7 @@
 #define NCI_UART_TX_WAKEUP	2
 
 static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
+static DEFINE_MUTEX(nci_uart_drivers_lock);
 
 static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
 {
@@ -105,19 +107,26 @@ static void nci_uart_write_work(struct work_struct *work)
 static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
 {
 	struct nci_uart *nu = NULL;
+	struct nci_uart *template;
 	int ret;
 
 	if (driver >= NCI_UART_DRIVER_MAX)
 		return -EINVAL;
 
-	if (!nci_uart_drivers[driver])
-		return -ENOENT;
-
 	nu = kzalloc_obj(*nu);
 	if (!nu)
 		return -ENOMEM;
 
-	memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
+	mutex_lock(&nci_uart_drivers_lock);
+	template = nci_uart_drivers[driver];
+	if (!template || !try_module_get(template->owner)) {
+		mutex_unlock(&nci_uart_drivers_lock);
+		kfree(nu);
+		return -ENOENT;
+	}
+	memcpy(nu, template, sizeof(*nu));
+	mutex_unlock(&nci_uart_drivers_lock);
+
 	nu->tty = tty;
 	skb_queue_head_init(&nu->tx_q);
 	INIT_WORK(&nu->write_work, nci_uart_write_work);
@@ -125,12 +134,9 @@ static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
 
 	ret = nu->ops.open(nu);
 	if (ret) {
+		module_put(nu->owner);
 		kfree(nu);
 		return ret;
-	} else if (!try_module_get(nu->owner)) {
-		nu->ops.close(nu);
-		kfree(nu);
-		return -ENOENT;
 	}
 	tty->disc_data = nu;
 
@@ -370,33 +376,46 @@ static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
 
 int nci_uart_register(struct nci_uart *nu)
 {
+	int ret = 0;
+
 	if (!nu || !nu->ops.open ||
-	    !nu->ops.recv || !nu->ops.close)
+	    !nu->ops.recv || !nu->ops.close ||
+	    (unsigned int)nu->driver >= NCI_UART_DRIVER_MAX)
 		return -EINVAL;
 
-	/* Set the send callback */
-	nu->ops.send = nci_uart_send;
-
 	/* Add this driver in the driver list */
+	mutex_lock(&nci_uart_drivers_lock);
 	if (nci_uart_drivers[nu->driver]) {
 		pr_err("driver %d is already registered\n", nu->driver);
-		return -EBUSY;
+		ret = -EBUSY;
+		goto unlock;
 	}
+
+	/* Set the send callback */
+	nu->ops.send = nci_uart_send;
 	nci_uart_drivers[nu->driver] = nu;
 
 	pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
 
-	return 0;
+unlock:
+	mutex_unlock(&nci_uart_drivers_lock);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(nci_uart_register);
 
 void nci_uart_unregister(struct nci_uart *nu)
 {
+	mutex_lock(&nci_uart_drivers_lock);
+
 	pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
 		nu->driver);
 
 	/* Remove this driver from the driver list */
-	nci_uart_drivers[nu->driver] = NULL;
+	if ((unsigned int)nu->driver < NCI_UART_DRIVER_MAX &&
+	    nci_uart_drivers[nu->driver] == nu)
+		nci_uart_drivers[nu->driver] = NULL;
+
+	mutex_unlock(&nci_uart_drivers_lock);
 }
 EXPORT_SYMBOL_GPL(nci_uart_unregister);
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH net v9 2/2] nfc: nci: uart: serialize driver selection
  2026-11-30  4:00 [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes Shubham Antil
  2026-11-30  4:00 ` [PATCH net v9 1/2] nfc: nci: uart: protect the driver registry Shubham Antil
@ 2026-11-30  4:00 ` Shubham Antil
  2026-11-30  4:03 ` [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes netdev-bot+sinfo
  2 siblings, 0 replies; 4+ messages in thread
From: Shubham Antil @ 2026-11-30  4:00 UTC (permalink / raw)
  To: netdev; +Cc: Simon Horman, gio, David Heidelberg, oe-linux-nfc, Shubham Antil

NCIUARTSETDRIVER assumes that tty->disc_data remains NULL until a
transport has been selected. Two concurrent callers can therefore both
allocate and register an NCI device for the same line discipline.

Serialize the allocate, open, and publish sequence with a sleepable mutex.
Recheck tty->disc_data while holding the mutex and keep publication after
transport open succeeds, so a failed selection remains retryable without
exposing a partially initialized context.

Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. Patch development and test planning used the same assistant.
Confirmed with a pty-backed user-space NCI controller and concurrent
NCIUARTSETDRIVER ioctls.

Fixes: 9961127d4bce ("NFC: nci: add generic uart support")
Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol)
Signed-off-by: Shubham Antil <shubham@octane.security>
---
 net/nfc/nci/uart.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/net/nfc/nci/uart.c b/net/nfc/nci/uart.c
index ee3d26ee6..bc2016e62 100644
--- a/net/nfc/nci/uart.c
+++ b/net/nfc/nci/uart.c
@@ -37,6 +37,7 @@
 
 static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
 static DEFINE_MUTEX(nci_uart_drivers_lock);
+static DEFINE_MUTEX(nci_uart_selection_lock);
 
 static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
 {
@@ -331,16 +332,17 @@ static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
 static int nci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
 			      unsigned long arg)
 {
-	struct nci_uart *nu = tty->disc_data;
 	int err = 0;
 
 	switch (cmd) {
 	case NCIUARTSETDRIVER:
-		if (!nu)
-			return nci_uart_set_driver(tty, (unsigned int)arg);
+		mutex_lock(&nci_uart_selection_lock);
+		if (tty->disc_data)
+			err = -EBUSY;
 		else
-			return -EBUSY;
-		break;
+			err = nci_uart_set_driver(tty, (unsigned int)arg);
+		mutex_unlock(&nci_uart_selection_lock);
+		return err;
 	default:
 		err = n_tty_ioctl_helper(tty, cmd, arg);
 		break;
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes
  2026-11-30  4:00 [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes Shubham Antil
  2026-11-30  4:00 ` [PATCH net v9 1/2] nfc: nci: uart: protect the driver registry Shubham Antil
  2026-11-30  4:00 ` [PATCH net v9 2/2] nfc: nci: uart: serialize driver selection Shubham Antil
@ 2026-11-30  4:03 ` netdev-bot+sinfo
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-11-30  4:03 UTC (permalink / raw)
  To: Shubham Antil; +Cc: netdev, Simon Horman, gio, David Heidelberg, oe-linux-nfc

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30  4:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  4:00 [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes Shubham Antil
2026-11-30  4:00 ` [PATCH net v9 1/2] nfc: nci: uart: protect the driver registry Shubham Antil
2026-11-30  4:00 ` [PATCH net v9 2/2] nfc: nci: uart: serialize driver selection Shubham Antil
2026-11-30  4:03 ` [PATCH net v9 0/2] nfc: nci: uart: split driver selection fixes netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox