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=1795969548; b=5Xh/dxd4PLg3sZHnN+7F9N+5sBAff61YXsSKgCLKH996RyWXaVR3U1Nd5p99xPLoKwes TMKHfcGyNTdNHczC5Hn7mcBnKbjk39GJ0a3if3wvoSy9Iu4ibKuyQ+x0CK3rxyxZmqJKg icFuI3+N8+qhh+3+V0dIajcy5eKFl8q+wTybpQziBD9u+RaLOTuRZpMJjju8AglEXPn+c uXYUOsSV7E7V8l4PTYQ71X8UcDmv9knZ2/HVpSSqbdnzdpAIszpW2PwQONtpkBdZKQQzW 6qv7eHSd0r+hXveoCRQWZwwW2T7VYcnn++7YYeW8wxKyexcnkeT5I9sEZ6TmWFAl0Eg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795969548; 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=MErQwMYEiCHiZD2cgtQJvIswWMmVcXIutqdVABBz72U=; b=tqXm7FUGrrCjjq/C201nRVgMNElZggei0vfVmzt2o+kGOrPDeux+xXD/Z2HVxxOXYMVS n8z3NasJK4qgY1tX/2C+dmYe6yDhCAL27ow/aiS/jI6ipXr0v7Vfkxnqrz9SQlYJHqwrA +RWOrpKbwbRN5t+zY72ih3w2gatjLijZ2bbcMSVZJVoPKA9Ng1cuxF8mQXfdRvzY+sbCV F79N1mxpel9BDRjIF+xs4lj2Sr/iaptCGlcG5y+x4w1O9Al80PDzhmlYJNdFLPhwB2kUt jNfR6IAWKdk+NIjsrQR35j2W3AaxnNjt/Q6QKNXQtACq8POJAPNODNSZCWo0aiLehoQ== 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=XevIHUuM; 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 42B3D606A31C; Sun, 29 Nov 2026 16:25:48 +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=XevIHUuM 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=1795969547; b=yQz8irXbRY1/dlLMdOAUkdjf6SulbhjgF6qucTaIZeBwqwfnxbG6PPNGMiIbQY7l6XNp v+1Fic2KCiygObCeWjqBrTLhfTsy6R8I/6ix4uIOe1reS19rkDITEGg5KB8J3OHvRJ1s9 63KC5VKd3YIyz/YbKbxW9Rr+cDqUvctv3zptDxKYikfWd+PVOnEwAjAoU4ujaY5PchX0T eWA1tuyVpR2k5IpDhAU/ViPJ8/XqCKpqxHjQGTrE2ORwnke+ejgo1EBxc+WCDoZlhhjMx tll3FfZpQQqMWLK/3MCqbNWubKYmo5YRNji1YbtZLEmDzM11SMaGZQiPHyvbhmqVa1A== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795969547; 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=MErQwMYEiCHiZD2cgtQJvIswWMmVcXIutqdVABBz72U=; b=TDO1l7mx6/gIHYhdEiCwe0mW+f/meZOOSyxmJuGzvx7wuLRVA7qZpbVkalqBIZCnDs1+ Kf6dzRNRfnLWc5WaU6RZqWYSNnTpRMOg8c/3SwhB0HtPhre0raMJrTuBLdrh2T9nRgff0 48WftUy7wKfPrxmm0bekn4v2tcei2Kco93O/GAN54dN8a31xfUmiRW7RVZdBL7s3fXtFM XTZqCd9Q16NMQSDqSrCjjMEXXfxb523dO+kBIdTP8v6TlF9sEf1McSRxC7KT1EKJqgyKa /zRwyAMDZp/9WHr0psG/SiL6HD5TRz5y/b69SIf43/Rsjta1b06Q6IvqshESGgQ4toA== 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 84D871F00925; 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=1795969547; bh=MErQwMYEiCHiZD2cgtQJvIswWMmVcXIutqdVABBz72U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=XevIHUuMDXixHDEDGBBSQKwHeBZ92HEuZoLYm8jKTF5wncclBKTNbzcozyWM806RS v2+qzjZva+L+vo36V9ueDoE53heO4K8LZ5/G4SLJggk8Jrc8ZsBM4Z+a+48SZSe6+3 +6I+Iq6nZ/+JElOdsbqF3Tbgk/0oHbULxomsgm1icpIcfSnbWQ5SqNplyjtRwNbdD7 rlNDicUu7gCc9CX/7XHuxCMM8ueXcY6+bkP7KQinO0RbnKB7aGUup22apX3IncnnOq GuXfI0qJqTi9/cIAqSx6kx0cy07pBQ4o7dtpxHfCnqMdDJOTjK4V1IZgh93GoghniV Idw7g2XiqvWvA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 11D034E51A; Sun, 29 Nov 2026 16:25:36 +0000 (UTC) From: Joas Antonio dos Santos via B4 Relay Date: Sun, 29 Nov 2026 16:25:37 +0000 Subject: [PATCH net v4 2/2] nfc: st21nfca: prevent double completion of SE APDU request 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-2-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=3220; i=joasantonio108@gmail.com; s=20261031; h=from:subject:message-id; bh=pD6itiUASKDLB1kcs4yFpVu4eTLyxU6GaTSomgb5dQE=; b=NmvA1Yfcp9pUiEPmNSdAhTNb0QW/4tPj5+GT4cnYxZqjdh279jLHvZBm7SFho/fY9kUTRw79E Ofu0NiXAOpaCYOqSwqsRqoNcvGvrKE6DqC2D90X1KOY2YWBnQz3RcZ2 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 Use the spinlock added in the previous commit to prevent completing an SE APDU request more than once. Without this, the timeout work handler (st21nfca_se_wt_work) and the EVT_TRANSMIT_DATA event handler can both invoke the callback for the same request. The callback frees its context, so the second call is a use-after-free followed by a double free. Check bwi_active under the lock in both completion paths and only complete a request that is still pending. A response that arrives after the timeout still gets its EVT_SE_END_OF_APDU_TRANSFER, as before, but no callback. Also reject a new SE APDU request in st21nfca_hci_se_io() with -EBUSY while one is in progress, instead of overwriting the callback and context of the pending request. The callback is not called in that case, so free the context like the -ENODEV path does; nfc_se_io() does not free it once the driver has been called. 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 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c index 3726b00b7580..33882f873909 100644 --- a/drivers/nfc/st21nfca/se.c +++ b/drivers/nfc/st21nfca/se.c @@ -228,6 +228,12 @@ 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); + if (info->se_info.bwi_active) { + spin_unlock(&info->se_info.se_lock); + /* The callback is not called, see the comment below */ + kfree(cb_context); + return -EBUSY; + } info->se_info.cb = cb; info->se_info.cb_context = cb_context; info->se_info.bwi_active = true; @@ -269,6 +275,10 @@ static void st21nfca_se_wt_work(struct work_struct *work) se_info.timeout_work); spin_lock(&info->se_info.se_lock); + if (!info->se_info.bwi_active) { + spin_unlock(&info->se_info.se_lock); + return; + } info->se_info.bwi_active = false; cb = info->se_info.cb; cb_context = info->se_info.cb_context; @@ -383,6 +393,7 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, u8 event, struct sk_buff *skb) { int r = 0; + bool active; se_io_cb_t cb; void *cb_context; struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); @@ -395,6 +406,7 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, cancel_work_sync(&info->se_info.timeout_work); spin_lock(&info->se_info.se_lock); + active = info->se_info.bwi_active; info->se_info.bwi_active = false; cb = info->se_info.cb; cb_context = info->se_info.cb_context; @@ -405,7 +417,9 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, if (r < 0) goto exit; - cb(cb_context, skb->data, skb->len, 0); + /* The request may have been completed by the timeout already */ + if (active) + cb(cb_context, skb->data, skb->len, 0); break; case ST21NFCA_EVT_WTX_REQUEST: mod_timer(&info->se_info.bwi_timer, jiffies + -- 2.43.0