From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795969550; b=YP//sW27WNvp0PSBxMOrT/HYRCtH1uPornunL+LDsV1o3Z602X6yAWeSABEFzEaAG/kg GCq/vMx1CBVgTv4RM5FhZYaPatAUJbuh4tADnhPtpUXPsL7qOOuBGBg8Cqnn4kewB1+bz Q2t31blT7+g4REPcfszRdpAzNkzn1opm7Zqb8h6bLE0yt7LrbmP9KeXsqrntRzmm0QCnu vNrn90vu1U2J4a8P9TGqzATgpRIUpKCbjbMz9Hfy0iCswa58p+VKoQCeE24J3/2VgqGSa HMW1QVsdf2MIrhaXrXBFwtuN50mmdzdQ52Xbu4B1hI1JcnUG24ukZs7T9smdUnqdSRw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795969550; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=GGTeOpfB0bRU/K/WI/sbeIkZL0ylP6pdL2Y/sTzvdos=; b=2rrT/pJqcwgQg2dGlHqqupUrW49wGbTWRPZoYSt7ZpyNp3GGfwFvnUABt/1Iz33qVmny ullL7+iipM/0Cd7AmCGHKh8CH1poldRSkV9RlqiVEni4faE3eBiqXFBMxmDcpFTQoMPDl YG0awmi3sfI8H+17HumU0YFvC9A6hyGEih2FBVBSFIDgx7M2SO8e/ua34EMjOny0hwyku Z+fyreuy6loalT6zYVlBe2z15Ve25hM6pHLeB8zQZbQ9UmQf7pkJ48zD3+lTPc+RcwUJ3 2QsFx3qnvIhDj/9ZCqkKWBlpb+M6PXGWGpZFlnxjcYLwcW1bMC5YlDL9WNYr67dPHiw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VUnllU32; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18A48606A32E; Sun, 29 Nov 2026 16:25:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VUnllU32 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795969549; b=f/yIaOTzHgFm7Kdu8NNQdk/9jk5HpIYuRolMv2P6iTVy3J3yj7gEn/wNxuPGvzF2KnoZ 6c7x4VjfxSS2L2I00VR6E4s57JymGauZzg1TgOCFFYJr0dyNAGImwIe7nGNAqH7hBvpp1 Oa/+r3yr4l48Frrm9Y63Witn+MF4/Wic3FGKOtS9gsu0HeMrM0kArZC348aYDToWMNC6H gYOL2M44UexkmSZQ/HkpBi/xZ98IZW3+Puuqwx2wIP9g19Y0c0At9G9CNo7g6ahLMfKCE Vb0DpMIQJU9aNuKK/Dzw/UgxaVRCSr13N24SGmuXs7Xmdgw+sYMESHXJqpEI872n5JQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795969549; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=GGTeOpfB0bRU/K/WI/sbeIkZL0ylP6pdL2Y/sTzvdos=; b=MInr/06qVikJMEwpXV/j0iYckge5yfPaEIYBol/ei8ePpZGUB+C4DC8ohsgxGORVhez+ NT9K2oJ0EUeMsQpEKghqa91yc/iqpf4YcknRAE67yAeKSl8y18kbicHYhZqH2QbwqOUWG oIuEQ/UIfhDzanakqY35e7nRa/3p5xaWd0mS4ISRy8bynToUsUNPkpnjzTWznM0esB8M9 hYEGKejz1f/hprcED2/qyZJ2SsHoaVivn8C6apRNO8o+cZhTLGTUlQLLyb4Xabk/YTbsE U8Me3J928w1Kp/1yIxyYQdiY6cP75M27qPzwI4QhIYuaCI/DxtN+iCCVz+Fi8Bg4aSw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 6A20B1F00924; Sun, 29 Nov 2026 16:25:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795969549; bh=GGTeOpfB0bRU/K/WI/sbeIkZL0ylP6pdL2Y/sTzvdos=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=VUnllU32uhsYMzYQVhY5rXf0UHzMcNp/kWeQljKEgEjmejmu6Tpj1avG0oBCDFQvj 1NOHghk7O5YNbywScZJRdQL2asRrFVpLPSgvvtCDDpBdKb1X0DpLur3iZkd2rM9Bwj D8QSGui0WpkgTVojMDEHqbvnf5Z6ubVrJkbqTRVSUcMzdzcEZnZivMuKfhshVM7bSZ SHD4uE3RjUjznbqKM7LJ/Lhna7lhzIxBE/aQb5mjJsacKIN+kfnM6xGm2BsH8LOVaS PS7GBEyeXsFooulEBy8wubtk1nTUKzsNNw6OmQN3/U9324QILzwLGV/LlGfqHV6Ifr fcp2M895II25w== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id E383D4E515; Sun, 29 Nov 2026 16:25:35 +0000 (UTC) From: Joas Antonio dos Santos via B4 Relay Date: Sun, 29 Nov 2026 16:25:36 +0000 Subject: [PATCH net v4 1/2] nfc: st21nfca: add spinlock to serialize SE APDU request state X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261129-st21nfca-v2-v4-1-7d18b104ad46@gmail.com> References: <20261129-st21nfca-v2-v4-0-7d18b104ad46@gmail.com> In-Reply-To: <20261129-st21nfca-v2-v4-0-7d18b104ad46@gmail.com> To: david@ixit.cz, krzk@kernel.org, joasantonio108@gmail.com Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, horms@kernel.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795969535; l=5941; i=joasantonio108@gmail.com; s=20261031; h=from:subject:message-id; bh=VyAkI0vjT6uxoWHGTO521PjBdyHDMMy5BVLzkNSbe5E=; b=qkM8qekbsMkfxRHgejpYCo+SxliPnOkHaVe1Xnd2PuyXzwl6zo9vUpu6EoRnyhObxHn/KMsQQ 1+v1cfTi/LOClwYtNFm8H9qBhCyLxhS50j+I/M4lpYlB+X+8EbwULcF X-Developer-Key: i=joasantonio108@gmail.com; a=ed25519; pk=ZxAznJT/FcPPdzLrDDFJQm4LLjjHVGBBqDSG3TXS23w= X-Endpoint-Received: by B4 Relay for joasantonio108@gmail.com/20261031 with auth_id=1371 X-Original-From: Joas Antonio dos Santos Reply-To: joasantonio108@gmail.com Content-Transfer-Encoding: 7bit From: Joas Antonio dos Santos The bwi_active flag, callback pointer (cb), and callback context (cb_context) in st21nfca_se_info are accessed concurrently from multiple contexts without synchronization: - st21nfca_hci_se_io() sets them from the netlink path - st21nfca_se_wt_work() reads/clears them from the timeout workqueue - st21nfca_apdu_reader_event_received() reads/clears them from the HCI rx path - st21nfca_se_deinit() reads/clears bwi_active on device removal Add a spinlock (se_lock) and take it around the accesses in these paths. The callback pointer and context are copied under the lock and invoked after releasing it, since both the callback and nfc_hci_send_event() may sleep. Also move the bwi_active = true assignment before mod_timer() so that the flag is always set before the timeout handler could observe it. st21nfca_se_deinit() only gets the lock around its read and clear of bwi_active. The teardown ordering has problems of its own which need a separate fix. Found by code review. Fixes: 2130fb97fecf ("NFC: st21nfca: Adding support for secure element") Cc: Simon Horman Signed-off-by: Joas Antonio dos Santos --- drivers/nfc/st21nfca/se.c | 34 +++++++++++++++++++++++++++++----- drivers/nfc/st21nfca/st21nfca.h | 2 ++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c index 7154bc1d644a..3726b00b7580 100644 --- a/drivers/nfc/st21nfca/se.c +++ b/drivers/nfc/st21nfca/se.c @@ -227,11 +227,13 @@ int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx, switch (se_idx) { case ST21NFCA_ESE_HOST_ID: + spin_lock(&info->se_info.se_lock); info->se_info.cb = cb; info->se_info.cb_context = cb_context; + info->se_info.bwi_active = true; + spin_unlock(&info->se_info.se_lock); mod_timer(&info->se_info.bwi_timer, jiffies + msecs_to_jiffies(info->se_info.wt_timeout)); - info->se_info.bwi_active = true; return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE, ST21NFCA_EVT_TRANSMIT_DATA, apdu, apdu_length); @@ -260,11 +262,17 @@ static void st21nfca_se_wt_work(struct work_struct *work) */ /* hardware reset managed through VCC_UICC_OUT power supply */ u8 param = 0x01; + se_io_cb_t cb; + void *cb_context; struct st21nfca_hci_info *info = container_of(work, struct st21nfca_hci_info, se_info.timeout_work); + spin_lock(&info->se_info.se_lock); info->se_info.bwi_active = false; + cb = info->se_info.cb; + cb_context = info->se_info.cb_context; + spin_unlock(&info->se_info.se_lock); if (!info->se_info.xch_error) { info->se_info.xch_error = true; @@ -275,7 +283,7 @@ static void st21nfca_se_wt_work(struct work_struct *work) nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_EVT_SE_HARD_RESET, ¶m, 1); } - info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME); + cb(cb_context, NULL, 0, -ETIME); } static void st21nfca_se_wt_timeout(struct timer_list *t) @@ -375,6 +383,8 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, u8 event, struct sk_buff *skb) { int r = 0; + se_io_cb_t cb; + void *cb_context; struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); pr_debug("apdu reader gate event: %x\n", event); @@ -383,14 +393,19 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, case ST21NFCA_EVT_TRANSMIT_DATA: timer_delete_sync(&info->se_info.bwi_timer); cancel_work_sync(&info->se_info.timeout_work); + + spin_lock(&info->se_info.se_lock); info->se_info.bwi_active = false; + cb = info->se_info.cb; + cb_context = info->se_info.cb_context; + spin_unlock(&info->se_info.se_lock); + r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0); if (r < 0) goto exit; - info->se_info.cb(info->se_info.cb_context, - skb->data, skb->len, 0); + cb(cb_context, skb->data, skb->len, 0); break; case ST21NFCA_EVT_WTX_REQUEST: mod_timer(&info->se_info.bwi_timer, jiffies + @@ -416,6 +431,7 @@ void st21nfca_se_init(struct nfc_hci_dev *hdev) /* initialize timers */ timer_setup(&info->se_info.bwi_timer, st21nfca_se_wt_timeout, 0); info->se_info.bwi_active = false; + spin_lock_init(&info->se_info.se_lock); timer_setup(&info->se_info.se_active_timer, st21nfca_se_activation_timeout, 0); @@ -434,14 +450,22 @@ EXPORT_SYMBOL(st21nfca_se_init); void st21nfca_se_deinit(struct nfc_hci_dev *hdev) { struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); + bool bwi_active; + + spin_lock(&info->se_info.se_lock); + bwi_active = info->se_info.bwi_active; + spin_unlock(&info->se_info.se_lock); - if (info->se_info.bwi_active) + if (bwi_active) timer_delete_sync(&info->se_info.bwi_timer); if (info->se_info.se_active) timer_delete_sync(&info->se_info.se_active_timer); cancel_work_sync(&info->se_info.timeout_work); + + spin_lock(&info->se_info.se_lock); info->se_info.bwi_active = false; + spin_unlock(&info->se_info.se_lock); info->se_info.se_active = false; } EXPORT_SYMBOL(st21nfca_se_deinit); diff --git a/drivers/nfc/st21nfca/st21nfca.h b/drivers/nfc/st21nfca/st21nfca.h index ae6771cc9894..03e41fadb5ed 100644 --- a/drivers/nfc/st21nfca/st21nfca.h +++ b/drivers/nfc/st21nfca/st21nfca.h @@ -6,6 +6,7 @@ #ifndef __LOCAL_ST21NFCA_H_ #define __LOCAL_ST21NFCA_H_ +#include #include #include #include @@ -131,6 +132,7 @@ struct st21nfca_se_info { struct timer_list bwi_timer; int wt_timeout; /* in msecs */ bool bwi_active; + spinlock_t se_lock; /* protects bwi_active, cb and cb_context */ struct timer_list se_active_timer; bool se_active; -- 2.43.0