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