From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796030110; b=WdLXwBlqTrzfH2EIPvJTm1IfZ1DsbfNqFXt3Z4eZUZuimj2Bw74+wt0LIfBsW16yvHBg 0ulMbk60zmKdAbSmWARIXU8ycnj0btEaj26Qi+JJBcTAbvhPMhUKXh7F/p5OVi90Z2GUs IFvs+MqvM1pKzngY8RmmqfQtKEZKOMUPVsdkbpfXjvA0T/RPNGQPc7T+9K++80ROVk2l4 m2N+dbEN4IsPIx/ZuDL7OMXDJapy/9ZoeFi3CJlJZzhjhzJg1EnYWn/uaY5bE0bgtA+UK porFtQ94OPAHmBD64izirjuupJnyPWjoIL+744mAVSHNeLt5i4HulC6G5selNl8u0sw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796030110; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=8xKXiOZcMmWsAraf2roxIggEM+IQ/GkzBEaB23aTTDc=; b=mwiqku7FzK+2neHahwA3Jy2alTElLQwP5oB+ZjfuowKfgF+VnxntVrvgm5O0LPOXvw0O Zclex60pUhNK9GJs1gInnZb0kI+9RC44oE/uN/KUY+GyGwVg5j+BKik5zuTxFOSjeW/9h gefev1sq0hPANIrWUFNeZp8PBej1eUmf1mi08ytsre1XrVfA9gxW1IPwptLcs3QwVpXcf N+ToEmv5RalD7Tzj5e97v4j2zlBb/bVKtothW/F6x76iQ+ypVooYnL1p8K1rHywJGZU3y uXPVEVGWns2gXCqnjnGOf2uamD6eJMRjrZNng0iEMwv4RRjV0XzHhzDJwLGv8ifv38A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=octane.security; spf=pass smtp.mailfrom=octane.security; dkim=pass header.d=octane.security header.i=@octane.security header.a=rsa-sha256 header.s=google header.b=dMobRTGK; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2F67606A329 for ; Mon, 30 Nov 2026 09:15:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F2F67606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=octane.security Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=octane.security Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=octane.security header.i=@octane.security header.a=rsa-sha256 header.s=google header.b=dMobRTGK Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-3282db206d3so1738197eec.0 for ; Mon, 30 Nov 2026 01:15:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=octane.security; s=google; t=1796030109; x=1796634909; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=8xKXiOZcMmWsAraf2roxIggEM+IQ/GkzBEaB23aTTDc=; b=dMobRTGKK9NIuC2QBNowAthSWIQMoOCccivzy65ge1ZtIyKBVICLjWlEcTRf24ULv7 ZKjlMtzyN9E6VfRvwq4d7mhYE1+2/9kr/+6Mt0tDoS4+5KyznEQlBDS6VpSK7oe/NhCe WAnkXTdrkUZjZFhAbrbEEGY6OaVozd0vDwovIpKRf7uYNyV3nYVVa3YAnbwwOCbDNVvN fIpgobjnxGdDl01dA122yNXK7vYkfszUt7lAX63Csn6Exk1VePo/nFMNOtu2iT5FE7vo UCH+f9Kj1Dmgt/diBMu4U0ExOwnFTDGxMa9ujwmRpX2VUf3r3XgLCPCvqj1b2wXubioI SXrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796030109; x=1796634909; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=8xKXiOZcMmWsAraf2roxIggEM+IQ/GkzBEaB23aTTDc=; b=BuuIqo322nHNZ5tIwNE0Qa9Bdm1dJenhuP0V/+L3KD811RnlYk8XscY4Z7UmynjeIj u61ArQOQ1EqXr0e6A50AkvU8z5s3UUYV41UlBx81ruqXH2yZqFCHjCbpQPt5wlMuFphw 9qzVwYgUaGKpx3akZ78KbEB6zmM6rWMtCCVQIX579pr6cPkDcJaHLoHisVrbBThr1ZXA 4A3MKkF/PSHC2u4+QUvefSNMTi9QVmGKdXUbNhoYuHhBDMuo7HTPvSxyl712jS8+Aaqg icqinFHufIryBSJR5ltIBDqyZgNCaKxhrRwT7K1CVw9SE1z/3I9Kwc2uWDDKCPaLLkqx zyCw== X-Gm-Message-State: APuU4Z9EeS187x7s/FyV0ul40bwNiATCQEpM502ZJWtsD5A7Ldl0tElb VWerrPi3Vf9twBQMsA59DYaQNfz5qWnf94uWI0sELuBdijOJlSvxd2Gt526tPbevvHKVgHpmduP 8XOkJ X-Gm-Gg: AZFdTZe0rqyKODpDS7l8gIgXBStLCoHuhCmqJXL8QZFmkMCFI1hJW6OGJ9w/BA0dgFN YF9Tx7nP073GhOToT24cpay4p0biXAa5JIYcgEEkQIvtjHkL7ycEcLWW7NuUB9AncQLUs/WEb/r Pb6M399OSfb4uBOx0zJqUxhU80Au0Tlq2dEAZzrALYY2D/qs7uBpFnay2xqHMQwMwRnd8Yxk19k HkKIrwWxvVr1JHKNR8WXdo9cjkQC+6YfWitR40wdf4/0o+0ruc/VqTJ13Bb1Y6D4t6SpA1TKVSm x/C0K8trI6D1XsTIxrkxdYVt/TJmEMn2pfxDMTpfSITFNyWx1zGrSQR63gxvDTdIfHpAmKY67ah kRfA2qjryIPlb6R8GNQkNJY83xAIWhnkls7P4n0zPXQPPoPPS+opwKBS4fkYrIiv7bU/rABLiBn v3c3FsHY7PB8MnqsFS6zHTP5NztnyABYvhImM5+1cavNFC8rNaBvl39doSueNhc3Q0kid1OUf5a X4tY8jQEKK8MKm7atepH7meedhnciv/xuaY+VkrZEcP4mzK/xH68b34sm+Abr9ZGbvRM9CiYemr k7zJIiCf8oRySk+ywzQBUSSy4Z3ONf7c2LxP0fmyA5pPd1GTy6cSA64eQ2PJfxS/e+yRmsk8lvd iEhe1A8haLSz9vQ9JzLuYEKDuCz7DSbSImnEzW6EkcASx/WKi X-Received: by 2002:a05:7300:f68e:b0:383:d3d1:f31e with SMTP id 5a478bee46e88-3856e94760dmr31345962eec.6.1796030108874; Mon, 30 Nov 2026 01:15:08 -0800 (PST) Received: from localhost.localdomain ([103.47.169.47]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-386ffc4ab65sm13869349eec.22.2026.11.30.01.15.06 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Nov 2026 01:15:08 -0800 (PST) From: Shubham Antil To: netdev@vger.kernel.org Cc: Simon Horman , gio@octane.security, David Heidelberg , oe-linux-nfc@lists.linux.dev Subject: [PATCH net v9 2/4] nfc: nci: coordinate transport shutdown in the core Date: Mon, 30 Nov 2026 14:44:54 +0530 Message-ID: <20261130091456.62379-3-shubham@octane.security> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261130091456.62379-1-shubham@octane.security> References: <20261130091456.62379-1-shubham@octane.security> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit nci_close_device() can cancel command work while an NCI response already owned by the receive worker makes another command runnable. The close path also resets the controller without first asking the transport whether queued bytes preserve frame boundaries. Close command admission, drain pre-close receive work, and cancel the command dependency chain before transport shutdown. Add ready and flush lifecycle callbacks so drivers can publish transport state before registration and preserve framing before the close-time reset. Reopen admission only for the reset and driver close callback, then drain any response and cancel every command left by an interrupted request. Publish NCI_UP before clearing NCI_INIT after a successful open. Receive callbacks can therefore never observe both states clear during the transition. Discovered during an AI-assisted source audit using OpenAI Codex, gpt-5.6-sol. The implicated close and reset transitions were exercised by repeated open, down, reopen, and unregister cycles and by an incomplete-RX close. No separate pre-fix failure was isolated for this command dependency race. Patch development and test planning used the same assistant. Confirmed with repeated pty-backed NCI UART open, command, reset, and hangup cycles. No physical NFC controller was available. Fixes: 9961127d4bce ("NFC: nci: add generic uart support") Assisted-by: LLM (OpenAI Codex, gpt-5.6-sol) Signed-off-by: Shubham Antil --- include/net/nfc/nci_core.h | 10 +++++ net/nfc/nci/core.c | 83 +++++++++++++++++++++++++++++--------- net/nfc/nci/rsp.c | 3 ++ 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/include/net/nfc/nci_core.h b/include/net/nfc/nci_core.h index 664d5058e..43b38aeab 100644 --- a/include/net/nfc/nci_core.h +++ b/include/net/nfc/nci_core.h @@ -28,6 +28,7 @@ enum nci_flag { NCI_INIT, NCI_UP, + NCI_CLOSING, NCI_DATA_EXCHANGE, NCI_DATA_EXCHANGE_TO, NCI_UNREG, @@ -67,6 +68,15 @@ struct nci_ops { int (*open)(struct nci_dev *ndev); int (*close)(struct nci_dev *ndev); int (*send)(struct nci_dev *ndev, struct sk_buff *skb); + /* Publish transport callbacks after core setup, before registration. */ + void (*ready)(struct nci_dev *ndev); + /* + * Flush transport-owned frames before the close-time reset. This may + * sleep and is called with req_lock held after command producers have + * stopped. Return an error if framing cannot be preserved; the core + * will then skip the reset exchange. + */ + int (*flush)(struct nci_dev *ndev); int (*setup)(struct nci_dev *ndev); int (*post_setup)(struct nci_dev *ndev); int (*fw_download)(struct nci_dev *ndev, const char *firmware_name); diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c index 73e3a9647..13fb8c3bc 100644 --- a/net/nfc/nci/core.c +++ b/net/nfc/nci/core.c @@ -530,14 +530,14 @@ static int nci_open_device(struct nci_dev *ndev) msecs_to_jiffies(NCI_INIT_TIMEOUT)); } - clear_bit(NCI_INIT, &ndev->flags); - if (!rc) { set_bit(NCI_UP, &ndev->flags); + clear_bit(NCI_INIT, &ndev->flags); nci_clear_target_list(ndev); atomic_set(&ndev->state, NCI_IDLE); } else { /* Init failed, cleanup */ + clear_bit(NCI_INIT, &ndev->flags); skb_queue_purge(&ndev->cmd_q); skb_queue_purge(&ndev->rx_q); skb_queue_purge(&ndev->tx_q); @@ -553,6 +553,8 @@ static int nci_open_device(struct nci_dev *ndev) static int nci_close_device(struct nci_dev *ndev) { + int flush_rc = 0; + nci_req_cancel(ndev, ENODEV); /* This mutex needs to be held as a barrier for @@ -574,33 +576,76 @@ static int nci_close_device(struct nci_dev *ndev) mutex_unlock(&ndev->req_lock); return 0; } + set_bit(NCI_CLOSING, &ndev->flags); /* Drop RX and TX queues */ skb_queue_purge(&ndev->rx_q); skb_queue_purge(&ndev->tx_q); - /* Flush TX wq, RX wq flush can't be under the lock */ + /* + * An RX worker may already own a response which can make another + * command runnable. No new frame is admitted after NCI_UP was + * cleared. Drop req_lock so an old RX worker can finish a request, + * then cancel every command it made runnable. + */ + mutex_unlock(&ndev->req_lock); + flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); + + /* Flush TX wq after the last pre-close RX work has completed. */ flush_workqueue(ndev->tx_wq); - /* Reset device */ + /* Stop command producers before flushing the transport backlog. */ + timer_delete_sync(&ndev->cmd_timer); skb_queue_purge(&ndev->cmd_q); - atomic_set(&ndev->cmd_cnt, 1); + flush_workqueue(ndev->cmd_wq); + timer_delete_sync(&ndev->cmd_timer); + flush_workqueue(ndev->cmd_wq); + if (ndev->ops->flush) + flush_rc = ndev->ops->flush(ndev); + + if (!flush_rc) { + /* Reset device only when the transport preserved framing. */ + atomic_set(&ndev->cmd_cnt, 1); + + set_bit(NCI_INIT, &ndev->flags); + clear_bit(NCI_CLOSING, &ndev->flags); + __nci_request(ndev, nci_reset_req, (void *)0, + msecs_to_jiffies(NCI_RESET_TIMEOUT)); + set_bit(NCI_CLOSING, &ndev->flags); + mutex_unlock(&ndev->req_lock); + flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); - set_bit(NCI_INIT, &ndev->flags); - __nci_request(ndev, nci_reset_req, (void *)0, - msecs_to_jiffies(NCI_RESET_TIMEOUT)); + /* + * A signal can end the request before cmd_work dequeues its skb. + * Remove that command before admitting a driver close callback, + * some of which issue their own synchronous NCI command. + */ + timer_delete_sync(&ndev->cmd_timer); + atomic_set(&ndev->cmd_cnt, 0); + cancel_work_sync(&ndev->cmd_work); + timer_delete_sync(&ndev->cmd_timer); + skb_queue_purge(&ndev->cmd_q); + atomic_set(&ndev->cmd_cnt, 1); + clear_bit(NCI_CLOSING, &ndev->flags); + } - /* After this point our queues are empty - * rx work may be running but will see that NCI_UP was cleared - */ + /* Let driver close callbacks complete their final command exchange. */ ndev->ops->close(ndev); + set_bit(NCI_CLOSING, &ndev->flags); clear_bit(NCI_INIT, &ndev->flags); - - /* Flush cmd and tx wq */ - flush_workqueue(ndev->cmd_wq); + mutex_unlock(&ndev->req_lock); + flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); timer_delete_sync(&ndev->cmd_timer); + atomic_set(&ndev->cmd_cnt, 0); + cancel_work_sync(&ndev->cmd_work); + timer_delete_sync(&ndev->cmd_timer); + skb_queue_purge(&ndev->cmd_q); + timer_delete_sync(&ndev->data_timer); if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) @@ -612,9 +657,6 @@ static int nci_close_device(struct nci_dev *ndev) mutex_unlock(&ndev->req_lock); - /* rx_work may take req_lock via nci_deactivate_target */ - flush_workqueue(ndev->rx_wq); - return 0; } @@ -1254,8 +1296,6 @@ int nci_register_device(struct nci_dev *ndev) struct device *dev = &ndev->nfc_dev->dev; char name[32]; - ndev->flags = 0; - INIT_WORK(&ndev->cmd_work, nci_cmd_work); snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); ndev->cmd_wq = create_singlethread_workqueue(name); @@ -1289,6 +1329,8 @@ int nci_register_device(struct nci_dev *ndev) mutex_init(&ndev->req_lock); INIT_LIST_HEAD(&ndev->conn_info_list); + if (ndev->ops->ready) + ndev->ops->ready(ndev); rc = nfc_register_device(ndev->nfc_dev); if (rc) @@ -1320,7 +1362,6 @@ void nci_unregister_device(struct nci_dev *ndev) struct nci_conn_info *conn_info, *n; nfc_unregister_rfkill(ndev->nfc_dev); - /* This set_bit is not protected with specialized barrier, * However, it is fine because the mutex_lock(&ndev->req_lock); * in nci_close_device() will help to emit one. @@ -1614,6 +1655,8 @@ static void nci_cmd_work(struct work_struct *work) struct sk_buff *skb; pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); + if (test_bit(NCI_CLOSING, &ndev->flags)) + return; /* Send queued command */ if (atomic_read(&ndev->cmd_cnt)) { diff --git a/net/nfc/nci/rsp.c b/net/nfc/nci/rsp.c index b0ab4f5ac..da71bf4ec 100644 --- a/net/nfc/nci/rsp.c +++ b/net/nfc/nci/rsp.c @@ -458,6 +458,9 @@ void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb) kfree_skb(skb); /* trigger the next cmd */ + if (test_bit(NCI_CLOSING, &ndev->flags)) + return; + atomic_set(&ndev->cmd_cnt, 1); if (!skb_queue_empty(&ndev->cmd_q)) queue_work(ndev->cmd_wq, &ndev->cmd_work); -- 2.54.0