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 1/4] nfc: st95hf: bound SPI receive length in st95hf_spi_recv_response()
Date: Wed, 25 Nov 2026 22:22:56 +0000 [thread overview]
Message-ID: <20261125222300.1438398-2-elkin@google.com> (raw)
In-Reply-To: <20261125222300.1438398-1-elkin@google.com>
st95hf_spi_recv_response() computes up to 1025 bytes of response length
from the 2-byte SPI header and reads the payload directly into
receivebuff without checking the destination buffer capacity. A
malformed or unexpected response overflows the 2-byte stack buffer in
st95hf_send_recv_cmd() or the 280-byte skb buffer in
st95hf_irq_thread_handler().
Pass the destination buffer capacity max_len to
st95hf_spi_recv_response() and reject lengths exceeding max_len with
-EMSGSIZE after deasserting chip-select via an empty trailing transfer.
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/core.c | 6 ++++--
drivers/nfc/st95hf/spi.c | 31 ++++++++++++++++++++++---------
drivers/nfc/st95hf/spi.h | 3 ++-
3 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/drivers/nfc/st95hf/core.c b/drivers/nfc/st95hf/core.c
index dcc9e53815ca..d7ac7ede1259 100644
--- a/drivers/nfc/st95hf/core.c
+++ b/drivers/nfc/st95hf/core.c
@@ -283,7 +283,8 @@ static int st95hf_send_recv_cmd(struct st95hf_context *st95context,
unsigned char st95hf_response_arr[2];
ret = st95hf_spi_recv_response(&st95context->spicontext,
- st95hf_response_arr);
+ st95hf_response_arr,
+ sizeof(st95hf_response_arr));
if (ret < 0) {
dev_err(dev, "spi error from st95hf_spi_recv_response(), err = 0x%x\n",
ret);
@@ -800,7 +801,8 @@ static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext)
mutex_lock(&stcontext->rm_lock);
res_len = st95hf_spi_recv_response(&stcontext->spicontext,
- skb_resp->data);
+ skb_resp->data,
+ MAX_RESPONSE_BUFFER_SIZE);
if (res_len < 0) {
dev_err(spidevice, "TISR spi response err = 0x%x\n", res_len);
result = res_len;
diff --git a/drivers/nfc/st95hf/spi.c b/drivers/nfc/st95hf/spi.c
index ffaf2789c406..a6599d65a3bf 100644
--- a/drivers/nfc/st95hf/spi.c
+++ b/drivers/nfc/st95hf/spi.c
@@ -66,21 +66,22 @@ EXPORT_SYMBOL_GPL(st95hf_spi_send);
/* Function to Receive command Response */
int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
- unsigned char *receivebuff)
+ unsigned char *receivebuff,
+ size_t max_len)
{
- int len = 0;
- struct spi_transfer tx_takedata;
- struct spi_message m;
- struct spi_device *spidev = spicontext->spidev;
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,},
+ { .tx_buf = &readdata_cmd, .len = 1 },
+ { .rx_buf = receivebuff, .len = 2, .cs_change = 1 },
};
-
+ struct spi_message m;
+ int len = 0;
int ret = 0;
- memset(&tx_takedata, 0x0, sizeof(struct spi_transfer));
+ if (max_len < 2)
+ return -EINVAL;
mutex_lock(&spicontext->spi_lock);
@@ -106,7 +107,19 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
else
len += receivebuff[1];
+ if (len > max_len) {
+ dev_err(&spidev->dev, "spi_recv_resp, invalid response len = %d\n",
+ len);
+ memset(&tx_takedata, 0x0, sizeof(struct spi_transfer));
+ spi_message_init(&m);
+ spi_message_add_tail(&tx_takedata, &m);
+ spi_sync(spidev, &m);
+ mutex_unlock(&spicontext->spi_lock);
+ return -EMSGSIZE;
+ }
+
/* 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.len = len - 2;
diff --git a/drivers/nfc/st95hf/spi.h b/drivers/nfc/st95hf/spi.h
index 3ab678734c17..840f55f82899 100644
--- a/drivers/nfc/st95hf/spi.h
+++ b/drivers/nfc/st95hf/spi.h
@@ -45,7 +45,8 @@ int st95hf_spi_send(struct st95hf_spi_context *spicontext,
enum req_type reqtype);
int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
- unsigned char *receivebuff);
+ unsigned char *receivebuff,
+ size_t max_len);
int st95hf_spi_recv_echo_res(struct st95hf_spi_context *spicontext,
unsigned char *receivebuff);
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-25 22:23 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 ` Elkin Cruz [this message]
2026-11-25 22:22 ` [PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers Elkin Cruz
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-2-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®