[PATCH net 1/4] nfc: st95hf: bound SPI receive length in st95hf_spi_recv_response()
From: Elkin Cruz
Date: Wed Nov 25 2026 - 17:24:21 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
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