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 --- 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 #include +#include #include #include #include @@ -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 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 --- 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