mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Elkin Cruz <elkin@google.com>
To: David Heidelberg <david@ixit.cz>, Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	 Justin Stitt <justinstitt@google.com>,
	oe-linux-nfc@lists.linux.dev, llvm@lists.linux.dev,
	 linux-kernel@vger.kernel.org, irogers@google.com,
	 Elkin Cruz <elkin@google.com>,
	stable@vger.kernel.org
Subject: [PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers
Date: Wed, 25 Nov 2026 22:22:57 +0000	[thread overview]
Message-ID: <20261125222300.1438398-3-elkin@google.com> (raw)
In-Reply-To: <20261125222300.1438398-1-elkin@google.com>

st95hf_spi_send(), st95hf_spi_recv_response(), and
st95hf_spi_recv_echo_res() pass stack-allocated buffers from core.c and
spi.c directly to spi_sync(), violating kernel DMA safety requirements
on DMA-capable SPI controllers and CONFIG_VMAP_STACK kernels.

Use kmemdup() in st95hf_spi_send(), spi_write_then_read() in
st95hf_spi_recv_echo_res(), and DMA-cacheline-isolated cmd_buf and
rx_buf members (via __dma_from_device_group_begin/end) in
struct st95hf_spi_context for st95hf_spi_recv_response().

This issue was found while trying out new Clang static analysis checks for
kernel bug classes. The finding was triaged and fixed, and this commit
description was drafted with the help of AI, then verified by code
inspection and compile-tested against linux-next (not reproduced and not
runtime-tested on hardware).

Fixes: cab47333f0f7 ("NFC: Add STMicroelectronics ST95HF driver")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---
 drivers/nfc/st95hf/spi.c | 52 +++++++++++++++++++++-------------------
 drivers/nfc/st95hf/spi.h | 10 ++++++++
 2 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/drivers/nfc/st95hf/spi.c b/drivers/nfc/st95hf/spi.c
index a6599d65a3bf..8bd7eccc34d3 100644
--- a/drivers/nfc/st95hf/spi.c
+++ b/drivers/nfc/st95hf/spi.c
@@ -14,13 +14,18 @@ int st95hf_spi_send(struct st95hf_spi_context *spicontext,
 		    int datalen,
 		    enum req_type reqtype)
 {
-	struct spi_message m;
-	int result = 0;
 	struct spi_device *spidev = spicontext->spidev;
 	struct spi_transfer tx_transfer = {
-		.tx_buf = buffertx,
 		.len = datalen,
 	};
+	unsigned char *tx_buf;
+	struct spi_message m;
+	int result = 0;
+
+	tx_buf = kmemdup(buffertx, datalen, GFP_KERNEL);
+	if (!tx_buf)
+		return -ENOMEM;
+	tx_transfer.tx_buf = tx_buf;
 
 	mutex_lock(&spicontext->spi_lock);
 
@@ -35,6 +40,7 @@ int st95hf_spi_send(struct st95hf_spi_context *spicontext,
 	spi_message_add_tail(&tx_transfer, &m);
 
 	result = spi_sync(spidev, &m);
+	kfree(tx_buf);
 	if (result) {
 		dev_err(&spidev->dev, "error: sending cmd to st95hf using SPI = %d\n",
 			result);
@@ -69,12 +75,11 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
 			     unsigned char *receivebuff,
 			     size_t max_len)
 {
-	unsigned char readdata_cmd = ST95HF_COMMAND_RECEIVE;
 	struct spi_device *spidev = spicontext->spidev;
 	struct spi_transfer tx_takedata;
 	struct spi_transfer t[2] = {
-		{ .tx_buf = &readdata_cmd, .len = 1 },
-		{ .rx_buf = receivebuff, .len = 2, .cs_change = 1 },
+		{ .len = 1 },
+		{ .len = 2, .cs_change = 1 },
 	};
 	struct spi_message m;
 	int len = 0;
@@ -85,6 +90,10 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
 
 	mutex_lock(&spicontext->spi_lock);
 
+	spicontext->cmd_buf = ST95HF_COMMAND_RECEIVE;
+	t[0].tx_buf = &spicontext->cmd_buf;
+	t[1].rx_buf = spicontext->rx_buf;
+
 	/* First spi transfer to know the length of valid data */
 	spi_message_init(&m);
 	spi_message_add_tail(&t[0], &m);
@@ -102,12 +111,13 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
 	len = 2;
 
 	/* Support of long frame */
-	if (receivebuff[0] & 0x60)
-		len += (((receivebuff[0] & 0x60) >> 5) << 8) | receivebuff[1];
+	if (spicontext->rx_buf[0] & 0x60)
+		len += (((spicontext->rx_buf[0] & 0x60) >> 5) << 8) |
+		       spicontext->rx_buf[1];
 	else
-		len += receivebuff[1];
+		len += spicontext->rx_buf[1];
 
-	if (len > max_len) {
+	if (len > max_len || len > ST95HF_MAX_RESPONSE_BUFFER_SIZE) {
 		dev_err(&spidev->dev, "spi_recv_resp, invalid response len = %d\n",
 			len);
 		memset(&tx_takedata, 0x0, sizeof(struct spi_transfer));
@@ -120,21 +130,23 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
 
 	/* Now make a transfer to read only relevant bytes */
 	memset(&tx_takedata, 0x0, sizeof(struct spi_transfer));
-	tx_takedata.rx_buf = &receivebuff[2];
+	tx_takedata.rx_buf = &spicontext->rx_buf[2];
 	tx_takedata.len = len - 2;
 
 	spi_message_init(&m);
 	spi_message_add_tail(&tx_takedata, &m);
 
 	ret = spi_sync(spidev, &m);
-
-	mutex_unlock(&spicontext->spi_lock);
 	if (ret) {
 		dev_err(&spidev->dev, "spi_recv_resp, data read error = %d\n",
 			ret);
+		mutex_unlock(&spicontext->spi_lock);
 		return ret;
 	}
 
+	memcpy(receivebuff, spicontext->rx_buf, len);
+	mutex_unlock(&spicontext->spi_lock);
+
 	return len;
 }
 EXPORT_SYMBOL_GPL(st95hf_spi_recv_response);
@@ -143,21 +155,11 @@ int st95hf_spi_recv_echo_res(struct st95hf_spi_context *spicontext,
 			     unsigned char *receivebuff)
 {
 	unsigned char readdata_cmd = ST95HF_COMMAND_RECEIVE;
-	struct spi_transfer t[2] = {
-		{.tx_buf = &readdata_cmd, .len = 1,},
-		{.rx_buf = receivebuff, .len = 1,},
-	};
-	struct spi_message m;
 	struct spi_device *spidev = spicontext->spidev;
-	int ret = 0;
+	int ret;
 
 	mutex_lock(&spicontext->spi_lock);
-
-	spi_message_init(&m);
-	spi_message_add_tail(&t[0], &m);
-	spi_message_add_tail(&t[1], &m);
-	ret = spi_sync(spidev, &m);
-
+	ret = spi_write_then_read(spidev, &readdata_cmd, 1, receivebuff, 1);
 	mutex_unlock(&spicontext->spi_lock);
 
 	if (ret)
diff --git a/drivers/nfc/st95hf/spi.h b/drivers/nfc/st95hf/spi.h
index 840f55f82899..6fbace4412d9 100644
--- a/drivers/nfc/st95hf/spi.h
+++ b/drivers/nfc/st95hf/spi.h
@@ -9,6 +9,7 @@
 #ifndef __LINUX_ST95HF_SPI_H
 #define __LINUX_ST95HF_SPI_H
 
+#include <linux/dma-mapping.h>
 #include <linux/spi/spi.h>
 
 /* Basic ST95HF SPI CMDs */
@@ -17,6 +18,7 @@
 #define ST95HF_COMMAND_RECEIVE	0x2
 
 #define ST95HF_RESET_CMD_LEN	0x1
+#define ST95HF_MAX_RESPONSE_BUFFER_SIZE	280
 
 /*
  * structure to contain st95hf spi communication specific information.
@@ -25,12 +27,20 @@
  * @done: completion structure to wait for st95hf response
  *	for synchronous calls.
  * @spi_lock: mutex to allow only one spi transfer at a time.
+ * @cmd_buf: DMA-safe buffer for command byte.
+ * @rx_buf: DMA-safe buffer for response transfers.
  */
 struct st95hf_spi_context {
 	bool req_issync;
 	struct spi_device *spidev;
 	struct completion done;
 	struct mutex spi_lock;
+	__dma_from_device_group_begin(cmd);
+	u8 cmd_buf;
+	__dma_from_device_group_end(cmd);
+	__dma_from_device_group_begin(rx);
+	u8 rx_buf[ST95HF_MAX_RESPONSE_BUFFER_SIZE];
+	__dma_from_device_group_end(rx);
 };
 
 /* flag to differentiate synchronous & asynchronous spi request */
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-25 22:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 22:22 [PATCH net 0/4] nfc: st95hf: fix SPI receive bounds, DMA safety, and teardown UAF Elkin Cruz
2026-11-25 22:22 ` [PATCH net 1/4] nfc: st95hf: bound SPI receive length in st95hf_spi_recv_response() Elkin Cruz
2026-11-25 22:22 ` Elkin Cruz [this message]
2026-11-25 22:22 ` [PATCH net 3/4] nfc: st95hf: validate async tag response frame lengths Elkin Cruz
2026-11-25 22:22 ` [PATCH net 4/4] nfc: st95hf: fix probe init order and remove teardown UAF Elkin Cruz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125222300.1438398-3-elkin@google.com \
    --to=elkin@google.com \
    --cc=david@ixit.cz \
    --cc=irogers@google.com \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=oe-linux-nfc@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®