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 3/4] nfc: st95hf: validate async tag response frame lengths
Date: Wed, 25 Nov 2026 22:22:58 +0000 [thread overview]
Message-ID: <20261125222300.1438398-4-elkin@google.com> (raw)
In-Reply-To: <20261125222300.1438398-1-elkin@google.com>
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
next prev 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 ` [PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers Elkin Cruz
2026-11-25 22:22 ` Elkin Cruz [this message]
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-4-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®