* [PATCH net 0/4] nfc: st95hf: fix SPI receive bounds, DMA safety, and teardown UAF
@ 2026-11-25 22:22 Elkin Cruz
2026-11-25 22:22 ` [PATCH net 1/4] nfc: st95hf: bound SPI receive length in st95hf_spi_recv_response() Elkin Cruz
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:22 UTC (permalink / raw)
To: David Heidelberg, Nathan Chancellor
Cc: Nick Desaulniers, Bill Wendling, Justin Stitt, oe-linux-nfc,
llvm, linux-kernel, irogers, Elkin Cruz
I am trying out new Clang static analysis checks for kernel bug
classes, and they flagged this code as possibly buggy. The findings
were triaged/fixed and commit descriptions were drafted with help of AI,
then checked against linux-next (not runtime-tested on hardware).
Thanks,
Elkin
Elkin Cruz (4):
nfc: st95hf: bound SPI receive length in st95hf_spi_recv_response()
nfc: st95hf: use DMA-safe buffers for SPI transfers
nfc: st95hf: validate async tag response frame lengths
nfc: st95hf: fix probe init order and remove teardown UAF
drivers/nfc/st95hf/core.c | 151 +++++++++++++++++++++-----------------
drivers/nfc/st95hf/spi.c | 75 +++++++++++--------
drivers/nfc/st95hf/spi.h | 13 +++-
3 files changed, 140 insertions(+), 99 deletions(-)
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 1/4] nfc: st95hf: bound SPI receive length in st95hf_spi_recv_response()
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
2026-11-25 22:22 ` [PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers Elkin Cruz
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:22 UTC (permalink / raw)
To: David Heidelberg, Nathan Chancellor
Cc: Nick Desaulniers, Bill Wendling, Justin Stitt, oe-linux-nfc,
llvm, linux-kernel, irogers, Elkin Cruz, stable
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers
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
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
3 siblings, 0 replies; 5+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:22 UTC (permalink / raw)
To: David Heidelberg, Nathan Chancellor
Cc: Nick Desaulniers, Bill Wendling, Justin Stitt, oe-linux-nfc,
llvm, linux-kernel, irogers, Elkin Cruz, stable
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 3/4] nfc: st95hf: validate async tag response frame lengths
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 ` [PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers Elkin Cruz
@ 2026-11-25 22:22 ` Elkin Cruz
2026-11-25 22:22 ` [PATCH net 4/4] nfc: st95hf: fix probe init order and remove teardown UAF Elkin Cruz
3 siblings, 0 replies; 5+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:22 UTC (permalink / raw)
To: David Heidelberg, Nathan Chancellor
Cc: Nick Desaulniers, Bill Wendling, Justin Stitt, oe-linux-nfc,
llvm, linux-kernel, irogers, Elkin Cruz, stable
st95hf_irq_thread_handler(), st95hf_error_handling(), and
st95hf_response_handler() index into skb_resp->data and subtract CRC
lengths in skb_trim() without verifying that the received frame is long
enough. Short responses from a tag or controller can underflow offsets
or read past the received frame.
Run st95hf_error_handling() before checking WTX_REQ_FROM_TAG, require
res_len >= 4 for WTX handling, and validate minimum frame lengths before
indexing CRC/RATS fields or trimming skb_resp.
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 | 27 +++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/drivers/nfc/st95hf/core.c b/drivers/nfc/st95hf/core.c
index d7ac7ede1259..ead0c342996c 100644
--- a/drivers/nfc/st95hf/core.c
+++ b/drivers/nfc/st95hf/core.c
@@ -667,6 +667,8 @@ static int st95hf_error_handling(struct st95hf_context *stcontext,
switch (stcontext->current_rf_tech) {
case NFC_DIGITAL_RF_TECH_106A:
if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) {
+ if (res_len < 3)
+ return -EIO;
error_byte = skb_resp->data[res_len - 3];
if (error_byte & ST95HF_NFCA_CRC_ERR_MASK) {
/* CRC error occurred */
@@ -678,6 +680,8 @@ static int st95hf_error_handling(struct st95hf_context *stcontext,
break;
case NFC_DIGITAL_RF_TECH_106B:
case NFC_DIGITAL_RF_TECH_ISO15693:
+ if (res_len < 1)
+ return -EIO;
error_byte = skb_resp->data[res_len - 1];
if (error_byte & ST95HF_NFCB_CRC_ERR_MASK) {
/* CRC error occurred */
@@ -714,7 +718,7 @@ static int st95hf_response_handler(struct st95hf_context *stcontext,
/* check if it is case of RATS request reply & FWI is present */
if (nfcddev->curr_protocol == NFC_PROTO_ISO14443 && cb_arg->rats &&
- (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) {
+ skb_len >= 4 && (skb_resp->data[1] & RATS_TB1_PRESENT_MASK)) {
if (skb_resp->data[1] & RATS_TA1_PRESENT_MASK)
stcontext->fwi =
(skb_resp->data[3] & TB1_FWI_MASK) >> 4;
@@ -736,13 +740,20 @@ static int st95hf_response_handler(struct st95hf_context *stcontext,
/* Remove CRC bytes only if received frames data has an eod (CRC) */
switch (stcontext->current_rf_tech) {
case NFC_DIGITAL_RF_TECH_106A:
- if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC)
+ if (stcontext->sendrcv_trflag == TRFLAG_NFCA_STD_FRAME_CRC) {
+ if (skb_len < 5)
+ return -EIO;
skb_trim(skb_resp, (skb_len - 5));
- else
+ } else {
+ if (skb_len < 3)
+ return -EIO;
skb_trim(skb_resp, (skb_len - 3));
+ }
break;
case NFC_DIGITAL_RF_TECH_106B:
case NFC_DIGITAL_RF_TECH_ISO15693:
+ if (skb_len < 3)
+ return -EIO;
skb_trim(skb_resp, (skb_len - 3));
break;
}
@@ -815,7 +826,11 @@ static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext)
goto end;
}
- if (skb_resp->data[2] == WTX_REQ_FROM_TAG) {
+ result = st95hf_error_handling(stcontext, skb_resp, res_len);
+ if (result)
+ goto end;
+
+ if (res_len >= 4 && skb_resp->data[2] == WTX_REQ_FROM_TAG) {
/* Request for new FWT from tag */
result = st95hf_handle_wtx(stcontext, true, skb_resp->data[3]);
if (result)
@@ -826,10 +841,6 @@ static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext)
return IRQ_HANDLED;
}
- result = st95hf_error_handling(stcontext, skb_resp, res_len);
- if (result)
- goto end;
-
result = st95hf_response_handler(stcontext, skb_resp, res_len);
if (result)
goto end;
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net 4/4] nfc: st95hf: fix probe init order and remove teardown UAF
2026-11-25 22:22 [PATCH net 0/4] nfc: st95hf: fix SPI receive bounds, DMA safety, and teardown UAF Elkin Cruz
` (2 preceding siblings ...)
2026-11-25 22:22 ` [PATCH net 3/4] nfc: st95hf: validate async tag response frame lengths Elkin Cruz
@ 2026-11-25 22:22 ` Elkin Cruz
3 siblings, 0 replies; 5+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:22 UTC (permalink / raw)
To: David Heidelberg, Nathan Chancellor
Cc: Nick Desaulniers, Bill Wendling, Justin Stitt, oe-linux-nfc,
llvm, linux-kernel, irogers, Elkin Cruz, stable
In st95hf_probe(), exchange_lock, rm_lock, and ddev/nfcdev are
initialized only after devm_request_threaded_irq(), leaving the IRQ
thread exposed to uninitialized synchronization primitives and a NULL
ddev if an interrupt arrives during probe. On teardown, st95hf_remove()
frees ddev while the threaded IRQ is still registered, and the end:
path in st95hf_irq_thread_handler() still calls complete_cb() after
nfcdev_free is set.
Initialize locks and allocate ddev before requesting the IRQ, skip
complete_cb() when nfcdev_free is set, and synchronize/free the IRQ via
devm_free_irq() before calling nfc_digital_free_device() in both
st95hf_probe() error paths and st95hf_remove().
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 | 118 +++++++++++++++++++-------------------
1 file changed, 60 insertions(+), 58 deletions(-)
diff --git a/drivers/nfc/st95hf/core.c b/drivers/nfc/st95hf/core.c
index ead0c342996c..f6384d9c4da8 100644
--- a/drivers/nfc/st95hf/core.c
+++ b/drivers/nfc/st95hf/core.c
@@ -869,9 +869,12 @@ static irqreturn_t st95hf_irq_thread_handler(int irq, void *st95hfcontext)
kfree_skb(skb_resp);
wtx = false;
cb_arg->rats = false;
- skb_resp = ERR_PTR(result);
- /* call of callback with error */
- cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg, skb_resp);
+ if (!stcontext->nfcdev_free) {
+ skb_resp = ERR_PTR(result);
+ /* call of callback with error */
+ cb_arg->complete_cb(stcontext->ddev, cb_arg->cb_usrarg,
+ skb_resp);
+ }
/* up the semaphore before returning */
up(&stcontext->exchange_lock);
mutex_unlock(&stcontext->rm_lock);
@@ -1076,10 +1079,9 @@ MODULE_DEVICE_TABLE(of, st95hf_spi_of_match);
static int st95hf_probe(struct spi_device *nfc_spi_dev)
{
struct device *dev = &nfc_spi_dev->dev;
- int ret;
-
- struct st95hf_context *st95context;
struct st95hf_spi_context *spicontext;
+ struct st95hf_context *st95context;
+ int ret;
nfc_info(&nfc_spi_dev->dev, "ST95HF driver probe called.\n");
@@ -1094,6 +1096,17 @@ static int st95hf_probe(struct spi_device *nfc_spi_dev)
st95context->fwi =
cmd_array[CMD_ISO14443A_PROTOCOL_SELECT].cmd_params[2];
+ init_completion(&spicontext->done);
+ mutex_init(&spicontext->spi_lock);
+ sema_init(&st95context->exchange_lock, 1);
+ mutex_init(&st95context->rm_lock);
+
+ /*
+ * Store spicontext in spi device object for using it in
+ * remove function
+ */
+ dev_set_drvdata(&nfc_spi_dev->dev, spicontext);
+
if (device_property_present(&nfc_spi_dev->dev, "st95hfvin")) {
st95context->st95hf_supply =
devm_regulator_get(&nfc_spi_dev->dev,
@@ -1110,15 +1123,6 @@ static int st95hf_probe(struct spi_device *nfc_spi_dev)
}
}
- init_completion(&spicontext->done);
- mutex_init(&spicontext->spi_lock);
-
- /*
- * Store spicontext in spi device object for using it in
- * remove function
- */
- dev_set_drvdata(&nfc_spi_dev->dev, spicontext);
-
st95context->enable_gpiod = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
if (IS_ERR(st95context->enable_gpiod)) {
ret = PTR_ERR(st95context->enable_gpiod);
@@ -1130,22 +1134,37 @@ static int st95hf_probe(struct spi_device *nfc_spi_dev)
if (ret)
goto err_disable_regulator;
- if (nfc_spi_dev->irq > 0) {
- if (devm_request_threaded_irq(&nfc_spi_dev->dev,
- nfc_spi_dev->irq,
- st95hf_irq_handler,
- st95hf_irq_thread_handler,
- IRQF_TRIGGER_FALLING,
- "st95hf",
- (void *)st95context) < 0) {
- dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n");
- ret = -EINVAL;
- goto err_disable_regulator;
- }
- } else {
+ /* create NFC dev object before requesting IRQ */
+ st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops,
+ ST95HF_SUPPORTED_PROT,
+ ST95HF_CAPABILITIES,
+ ST95HF_HEADROOM_LEN,
+ ST95HF_TAILROOM_LEN);
+ if (!st95context->ddev) {
+ ret = -ENOMEM;
+ goto err_disable_regulator;
+ }
+
+ st95context->nfcdev = st95context->ddev->nfc_dev;
+ nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev);
+ nfc_digital_set_drvdata(st95context->ddev, st95context);
+
+ if (nfc_spi_dev->irq <= 0) {
dev_err(&nfc_spi_dev->dev, "not a valid IRQ associated with ST95HF\n");
ret = -EINVAL;
- goto err_disable_regulator;
+ goto err_free_digital_device;
+ }
+
+ ret = devm_request_threaded_irq(&nfc_spi_dev->dev,
+ nfc_spi_dev->irq,
+ st95hf_irq_handler,
+ st95hf_irq_thread_handler,
+ IRQF_TRIGGER_FALLING,
+ "st95hf",
+ (void *)st95context);
+ if (ret < 0) {
+ dev_err(&nfc_spi_dev->dev, "err: irq request for st95hf is failed\n");
+ goto err_free_digital_device;
}
/*
@@ -1157,44 +1176,26 @@ static int st95hf_probe(struct spi_device *nfc_spi_dev)
ret = st95hf_send_spi_reset_sequence(st95context);
if (ret) {
dev_err(&nfc_spi_dev->dev, "err: spi_reset_sequence failed\n");
- goto err_disable_regulator;
+ goto err_free_irq;
}
/* call PowerOnReset sequence of ST95hf to activate it */
ret = st95hf_por_sequence(st95context);
if (ret) {
dev_err(&nfc_spi_dev->dev, "err: por seq failed for st95hf\n");
- goto err_disable_regulator;
- }
-
- /* create NFC dev object and register with NFC Subsystem */
- st95context->ddev = nfc_digital_allocate_device(&st95hf_nfc_digital_ops,
- ST95HF_SUPPORTED_PROT,
- ST95HF_CAPABILITIES,
- ST95HF_HEADROOM_LEN,
- ST95HF_TAILROOM_LEN);
- if (!st95context->ddev) {
- ret = -ENOMEM;
- goto err_disable_regulator;
+ goto err_free_irq;
}
- st95context->nfcdev = st95context->ddev->nfc_dev;
- nfc_digital_set_parent_dev(st95context->ddev, &nfc_spi_dev->dev);
-
- ret = nfc_digital_register_device(st95context->ddev);
+ ret = nfc_digital_register_device(st95context->ddev);
if (ret) {
dev_err(&st95context->nfcdev->dev, "st95hf registration failed\n");
- goto err_free_digital_device;
+ goto err_free_irq;
}
- /* store st95context in nfc device object */
- nfc_digital_set_drvdata(st95context->ddev, st95context);
-
- sema_init(&st95context->exchange_lock, 1);
- mutex_init(&st95context->rm_lock);
-
- return ret;
+ return 0;
+err_free_irq:
+ devm_free_irq(&nfc_spi_dev->dev, nfc_spi_dev->irq, st95context);
err_free_digital_device:
nfc_digital_free_device(st95context->ddev);
err_disable_regulator:
@@ -1206,18 +1207,16 @@ static int st95hf_probe(struct spi_device *nfc_spi_dev)
static void st95hf_remove(struct spi_device *nfc_spi_dev)
{
- int result = 0;
- unsigned char reset_cmd = ST95HF_COMMAND_RESET;
struct st95hf_spi_context *spictx = dev_get_drvdata(&nfc_spi_dev->dev);
-
struct st95hf_context *stcontext = container_of(spictx,
struct st95hf_context,
spicontext);
+ unsigned char reset_cmd = ST95HF_COMMAND_RESET;
+ int result = 0;
mutex_lock(&stcontext->rm_lock);
nfc_digital_unregister_device(stcontext->ddev);
- nfc_digital_free_device(stcontext->ddev);
stcontext->nfcdev_free = true;
mutex_unlock(&stcontext->rm_lock);
@@ -1227,6 +1226,9 @@ static void st95hf_remove(struct spi_device *nfc_spi_dev)
if (result == -EINTR)
dev_err(&spictx->spidev->dev, "sleep for semaphore interrupted by signal\n");
+ devm_free_irq(&nfc_spi_dev->dev, nfc_spi_dev->irq, stcontext);
+ nfc_digital_free_device(stcontext->ddev);
+
/* next reset the ST95HF controller */
result = st95hf_spi_send(&stcontext->spicontext,
&reset_cmd,
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 22:24 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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
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®