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=1795969543; b=hQ7UDGL04oxXTY5+zg2PHHszQMhF64RvX+Fd2ybAy3MExBcJBRiqEp9bXqEONEC1YdIi rw7moz8U+JjFgXQD3sAtEwGp+GHd0VFe0AWDWheLkyZzTYocbbFNIR9yhMFiwZl2R/f0a zxrTfEIpdiGl3/XklAib0AlcwjZkLtgg7lMyjEe8YA2qNnjZu8IiFz3frR5BDKzwf58bE FMgmfh9GcR6hTMbgDQrIcip20Zf5dBbLEyxtZjxgS1Ze3TozepA21RPN9M+hErGLnv+db 7KV2FZali0j0m5rZXtfPLBh/0pxnWLFTBlCBM73xjQ7KRgW6fiq6esRNTynoURyPdwQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795969543; h=Received:DKIM-Signature:Received:From:Subject:Date:Message-Id: MIME-Version:Content-Type:Content-Transfer-Encoding:X-B4-Tracking:To: Cc:X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=BsSqFHZDjr//Xtzkgz3/EZbuJNrKugyQ1cVjtR7tl6E=; b=e3Bof4XFOCx1qoF7XQgIoN94h6Qkc3y+pYBdMB4GfrGOMOBoBOQZ7vIJ/SQpayTEom34 HhJD5wV6qCu4C2knnl3E1rRRqPTCAn2/GlimMoLqR/bRp9fdwWdgR3loqlX9dM4/qa4QK bP3YURgrOQOXseAawu8bJ2OrR1isoc/XdcVvAC0fxERPiFFXDyPOTiJma2xr3uBNbmy46 woEXqJDnA+dd6hBoh1IY5naNB6SrY8p14w9vnE/pD5LNCyLHC5084x5xwuIyITlLK2iu/ avRxIXRvg58mIXEfO3bt2WfooNanfyBEg+Z4wYpmFbgj6PFT8c3rtE8msSaoPUvrH3Q== 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=LpjWATIr; 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 AEB85606A323; Sun, 29 Nov 2026 16:25:43 +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=LpjWATIr 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=1795969543; b=Qj1ccDicZJUqBnFRAqkr86ROZlmKseKdx6f1eVRFrbjK3Ldk/beiMslcCHHW1UhaPS+n ODWc3jyUQqE7Y7KlhdBsGbz+ysBWRkDGeXTEFhBbK7bJchQYsW7sruBzEA8ORn+aK4DOh aQhJQxnJZgnQ6I4Wimj5C3l5D8RCCollqmUjiCgDEvQ2bH39B9JiEFNiygMJJc0zUjJ2T PEWHaOKfRNNHpiBgrKl4Cxk6THHQavZi5SxcGIhmKwk/PInUVMddjU5k9k0f+TsMHdsir nv6P3OUnCUiUZRQoUomA98LQ+R4Mnul6a5aZxdYeuHXT98YtYz8n3unwiIKmMXxHqIw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795969543; h=DKIM-Signature:Received:From:Subject:Date:Message-Id:MIME-Version: Content-Type:Content-Transfer-Encoding:X-B4-Tracking:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=BsSqFHZDjr//Xtzkgz3/EZbuJNrKugyQ1cVjtR7tl6E=; b=CkmPIY6t6ao58yyh6uijdUuCl/or/f0Mh/RWNsU68JnKipErU9E1U10LAWOkNpm0RBcI Pz6a3Vk3+lF4CZjWbKIaV2pECy3ecepZ8cWaphMwqhnRXM4XEV7dJ+Fg4qMNh3wXmwJJa n1hQTd587tYtCwu7GGODlBe61kua5qwZEh6SUTg6bX/xzqgNrdHg25vA4jqNGhf3sd1sX B5gVo2AJRw610h57OMnrf2FYDiyYXpD2HBFU+WSEQW4Lk/NGRf414TOLKatwFe97FvkeF eZo5YufjiHY7p7SmHJXl0/ucGyTu8Mhsc0qhrsPPehAqE01+xyQSF3Ugd9Eg93CktuQ== 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 69D7D1F00923; 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=1795969542; bh=BsSqFHZDjr//Xtzkgz3/EZbuJNrKugyQ1cVjtR7tl6E=; h=From:Subject:Date:To:Cc:Reply-To; b=LpjWATIrLZaqpaIR5SDtRach+5jwqMhifeecpjHOFP4YGPIkQ+dwc0qCCfkbblzEJ 9GMETCtimwC1Oh2tkxADudW2I4acyrZCXSCFVHMggmHZxQCPnPhGmRe9bQN6s3vEZ9 yGkCvI1ScXZAzdiNayW8Vo3gWGrQXA+2kKdoiPN/+JG92MksPhQd1mhx8aKuUB+QXK CFlXjTmHb3ERxGa8kqyBWqWMFGv/3pSaLCq9aYjEPurhBUEcNX01fF0Ljx0eZR2VX7 LWgoMX3fO4Qk9ldVUWcPH3ktOK3VOfCdLmWVGjItMa3oBjS76INS1cvBUgtQdIQnXv NA4mkaYgfnbPg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id BEA4F4E516; Sun, 29 Nov 2026 16:25:35 +0000 (UTC) From: Joas Antonio dos Santos via B4 Relay Subject: [PATCH net v4 0/2] nfc: st21nfca: fix SE APDU request race condition Date: Sun, 29 Nov 2026 16:25:35 +0000 Message-Id: <20261129-st21nfca-v2-v4-0-7d18b104ad46@gmail.com> 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" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAP9RDGsC/22NQQrCMBBFryKzNpKZtjG68h7iIiSTNmBbSUpQS u9uyKqCMJv/P+/NColj4ATXwwqRc0hhnkpojwewg5l6FsGVDCRJIUot0kI4eWtEJuFb23mtkFt UUIhXZB/e1XaHiRd4lHIIaZnjp37IVKe/snJSkEP2znfGdfrWjyY8T3Yeqyc3Oxbpl20KazWeJ ZG6sMQ9u23bF0XWoUznAAAA 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=3074; i=joasantonio108@gmail.com; s=20261031; h=from:subject:message-id; bh=UHQEKDG+zDXlSglJZcRIuqsZukpW1gTKpNR53sSUBxk=; b=3LJSQlTVTyu7U4xVc462Xe8G2qGSexXZCgvEWvs9kOWWnyTeSRsOHjefvzdJ4gw/69CqPipx4 WQxW43rEH2iDVyIdYzkTjtfa6WSZc5e0hcoYa6z0bYz8waa0izH0L1Y 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 The BWI timeout work and the EVT_TRANSMIT_DATA handler can both complete the same SE APDU request. The callback frees its context, so the second completion is a use-after-free followed by a double free. 1/2: Add a spinlock around bwi_active, cb and cb_context in the paths that use them. 2/2: Use the lock to complete a request only once and to reject overlapping requests. The series is build-tested only (W=1, arm64); I do not have the hardware. Not addressed here, all older than this series and pointed out by the Sashiko reviews of v2 and v3: - st21nfca_se_deinit() can leave bwi_timer armed when info is freed (it runs before nfc_hci_unregister_device() and only deletes the timer when bwi_active is set), and it drops a pending request without completing it. The remove ordering needs its own patch. - EVT_TRANSMIT_DATA carries no request id, so a response that arrives after its request timed out can still be taken for the answer to the next request. - When nfc_hci_send_event() fails in st21nfca_hci_se_io() the request stays pending until the BWI timeout completes it with -ETIME. - drivers/nfc/st-nci/se.c has the same double completion. I will send a separate patch for it once the approach here is agreed on. --- Changes in v4: - 1/2: st21nfca_se_deinit() takes se_lock around its read and clear of bwi_active (Simon Horman). The teardown ordering itself is still left for a follow-up. - Link to v3: https://lore.kernel.org/r/20261112-st21nfca-v2-v3-0-c81702269e01@gmail.com Changes in v3: - 2/2: free cb_context when a request is rejected with -EBUSY; it was leaked (Sashiko). - 2/2: still send EVT_SE_END_OF_APDU_TRANSFER for a response that arrives after the timeout, as before the series; only the callback is skipped (Sashiko). - 1/2: comment on what se_lock protects (checkpatch), commit message no longer claims every access is serialized. - Rebased on v7.4-rc3+, target tree added to the subject. - Link to v2: https://lore.kernel.org/r/20261110-st21nfca-v2-v2-0-cf9102b314cb@gmail.com Changes in v2: - Use a spinlock instead of plain bwi_active flag checks (Simon Horman). - Split into two patches: locking, then the completion fix. - Set bwi_active before mod_timer(). - Copy cb/cb_context under the lock, call it after unlocking. - Reject overlapping se_io requests with -EBUSY. - Drop the Assisted-by trailer. - Link to v1: https://lore.kernel.org/r/179332800288.52644.8728400883396719965@gmail.com --- Joas Antonio dos Santos (2): nfc: st21nfca: add spinlock to serialize SE APDU request state nfc: st21nfca: prevent double completion of SE APDU request drivers/nfc/st21nfca/se.c | 48 ++++++++++++++++++++++++++++++++++++----- drivers/nfc/st21nfca/st21nfca.h | 2 ++ 2 files changed, 45 insertions(+), 5 deletions(-) --- base-commit: 9d66f26fefacebfeed15de31c1123451a17ff119 change-id: 20261108-st21nfca-v2-f4c5f861e416 Best regards, -- Joas Antonio dos Santos