[PATCH net 3/4] nfc: st95hf: validate async tag response frame lengths
From: Elkin Cruz
Date: Wed Nov 25 2026 - 17:24:48 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
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