[PATCH net 4/4] nfc: st95hf: fix probe init order and remove teardown UAF

From: Elkin Cruz

Date: Wed Nov 25 2026 - 17:25:22 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
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