Netdev List
 help / color / mirror / Atom feed
From: Shubham Antil <shubham@octane.security>
To: netdev@vger.kernel.org
Cc: Simon Horman <horms@kernel.org>,
	gio@octane.security, David Heidelberg <david@ixit.cz>,
	oe-linux-nfc@lists.linux.dev,
	Shubham Antil <shubham@octane.security>
Subject: [PATCH net v9 1/2] nfc: nci: uart: protect the driver registry
Date: Mon, 30 Nov 2026 09:30:44 +0530	[thread overview]
Message-ID: <20261130040045.41928-2-shubham@octane.security> (raw)
In-Reply-To: <20261130040045.41928-1-shubham@octane.security>

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


  reply	other threads:[~2026-11-30  4:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130040045.41928-2-shubham@octane.security \
    --to=shubham@octane.security \
    --cc=david@ixit.cz \
    --cc=gio@octane.security \
    --cc=horms@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=oe-linux-nfc@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox