nfc: nci: ignore unexpected CORE_RESET_NTF and CORE_RESET_RSP
Subsystems (4)
PATCH v3 1/1 nfc: nci: ignore unexpected CORE_RESET_NTF and CORE_RESET_RSP
Commit bcd684aace34 ("net/nfc/nci: Support NCI 2.x initial sequence") added handling of CORE_RESET_NTF in nci_core_reset_ntf_packet(). When received, it updates ndev->nci_ver, ndev->manufact_id, and ndev->manufact_specific_info, and calls nci_req_complete(ndev, NCI_STATUS_OK) to finish the pending reset request.
However, unlike other notification handlers in ntf.c (which validate ndev->state before completing requests), nci_core_reset_ntf_packet() does not check whether a core reset request is actually pending. If an unsolicited or delayed CORE_RESET_NTF arrives (e.g. after a reset command times out or from a misbehaving NFCC), it unconditionally:
1. Completes whatever request is currently in-flight (such as CORE_INIT, RF_DISCOVER, or CONN_CREATE) with NCI_STATUS_OK, leading to kernel state desynchronization.
2. Overwrites ndev->nci_ver and manufacturer info. Because ndev->nci_ver is used as a selector for subsequent packet formats and parsers (e.g., in nci_open_device() and nci_core_init_rsp_packet()), unexpectedly modifying it can cause protocol format confusion.
A similar issue exists in nci_core_reset_rsp_packet(): an unexpected or delayed response packet can prematurely complete an unrelated in-flight request.
Fix this by ensuring CORE_RESET_NTF and CORE_RESET_RSP are only processed by the core layer when a reset command is actively awaiting them:
- Set NCI_RESET_PENDING in nci_reset_req() when sending CORE_RESET_CMD.
- In nci_core_reset_rsp_packet(), ignore the response if NCI_RESET_PENDING is not set. If set, atomically clear the flag and complete the request on failure or for NCI 1.x (checking skb->len >= sizeof(*rsp)).
- In __nci_request(), ensure NCI_RESET_PENDING is cleared upon request completion, cancellation, or timeout.
- In nci_core_reset_ntf_packet(), fail any in-flight reset request if the controller reports an unrecoverable error (0x00), and only process command-triggered notifications (0x02). Non-command triggers (e.g. power-on 0x01 or proprietary triggers) and unexpected notifications return 0 without modifying core state, ensuring driver-specific notification handlers (such as fdp firmware patch handling) still receive them.
- In nci_core_reset_ntf_packet(), support notifications shorter than 9 bytes (e.g. NCI 2.0 frames with manufacturer_specific_len == 0), validating the fixed header length and safely bounding manufact_specific_info reads.
---
v3:
- In nci_core_reset_ntf_packet(), do not reject notifications shorter than 9 bytes with -EINVAL; instead allow valid short frames (e.g. 2-byte NCI 1.x or 5-byte NCI 2.0 with manufacturer_specific_len == 0), safely cap field reads, and return 0 so driver-registered hooks (such as fdp firmware patching) are not skipped (per Sashiko AI).
- Fix trigger semantics: in NCI, CORE_RESET_CMD trigger is 0x02 (NCI_RESET_TRIGGER_RESET_CMD). If controller reports an unrecoverable error (0x00, NCI_RESET_TRIGGER_ERROR) while reset is pending, fail the request instead of completing with OK; ignore power-on (0x01) and proprietary triggers at core layer (per Sashiko AI).
- In nci_core_reset_rsp_packet(), use test_and_clear_bit(NCI_RESET_PENDING) upon completing the request to consume the flag atomically (per Sashiko AI).
- Link to v2: https://lore.kernel.org/netdev/[email protected]/
v2:
- Do not abort the notification pipeline on unexpected CORE_RESET_NTF (return 0 instead of -EINVAL), preserving driver-level hooks (e.g. fdp firmware patching) per Simon Horman.
- Skip power-on notifications (trigger 0x01, identical in NCI 1.0 and 2.0) while gating both NCI 1.0 (0x00) and NCI 2.0 (0x02) command-triggered resets on NCI_RESET_PENDING alone.
- Check NCI_RESET_PENDING in nci_core_reset_rsp_packet() to avoid completing unrelated requests on unexpected responses.
- Explicitly check skb->len >= sizeof(*rsp) in nci_core_reset_rsp_packet() for NCI 1.x handling.
- Link to v1: https://lore.kernel.org/netdev/[email protected]/
include/net/nfc/nci.h | 5 ++++ include/net/nfc/nci_core.h | 1 + net/nfc/nci/core.c | 3 +++ net/nfc/nci/ntf.c | 48 +++++++++++++++++++++++++++++++++----- net/nfc/nci/rsp.c | 30 ++++++++++++++++++------ 5 files changed, 74 insertions(+), 13 deletions(-)
include/net/nfc/nci.h +5 -0
@@ -140,6 +140,11 @@#define NCI_RESET_TYPE_KEEP_CONFIG 0x00#define NCI_RESET_TYPE_RESET_CONFIG 0x01 +/* NCI Reset Triggers */ +#define NCI_RESET_TRIGGER_ERROR 0x00 +#define NCI_RESET_TRIGGER_POWER_ON 0x01 +#define NCI_RESET_TRIGGER_RESET_CMD 0x02 +/* NCI Static RF connection ID */ #define NCI_STATIC_RF_CONN_ID 0x00
include/net/nfc/nci_core.h +1 -0
@@ -31,6 +31,7 @@ enum nci_flag {NCI_DATA_EXCHANGE ,NCI_DATA_EXCHANGE_TO ,NCI_UNREG , + NCI_RESET_PENDING, };/* NCI device states */
net/nfc/nci/core.c +3 -0
@@ -134,6 +134,7 @@ static int __nci_request(struct nci_dev *ndev, } ndev->req_status = ndev->req_result =0 ; +clear_bit (NCI_RESET_PENDING , &ndev->flags);return rc; }
@@ -163,6 +164,8 @@ static void nci_reset_req(struct nci_dev *ndev, const void *opt) {struct nci_core_reset_cmd cmd; +set_bit (NCI_RESET_PENDING , &ndev->flags); + cmd.reset_type =NCI_RESET_TYPE_RESET_CONFIG ;nci_send_cmd (ndev,NCI_OP_CORE_RESET_CMD ,1 , &cmd); }
net/nfc/nci/ntf.c +42 -6
63 lines not on this page · raw message
net/nfc/nci/rsp.c +23 -7
@@ -32,16 +32,32 @@ static void nci_core_reset_rsp_packet(struct nci_dev *ndev,pr_debug ("status 0x%x\n" , rsp->status); -/* Handle NCI 1.x ver */ -if (skb->len !=1 ) { -if (rsp->status ==NCI_STATUS_OK ) { - ndev->nci_ver = rsp->nci_ver; -pr_debug ("nci_ver 0x%x, config_status 0x%x\n" , - rsp->nci_ver, rsp->config_status); - } +/* +* If no reset request is pending, ignore unexpected responses to avoid +* prematurely completing an unrelated request. +*/ +if (!test_bit (NCI_RESET_PENDING , &ndev->flags)) +return ; + +if (rsp->status !=NCI_STATUS_OK ) { +if (test_and_clear_bit (NCI_RESET_PENDING , &ndev->flags)) +nci_req_complete (ndev, rsp->status); +return ; + } + +/* Handle NCI 1.x ver: completed by RSP */ +if (skb->len >=sizeof (*rsp)) { +if (!test_and_clear_bit (NCI_RESET_PENDING , &ndev->flags)) +return ; + + ndev->nci_ver = rsp->nci_ver; +pr_debug ("nci_ver 0x%x, config_status 0x%x\n" , + rsp->nci_ver, rsp->config_status);nci_req_complete (ndev, rsp->status); } + +/* NCI 2.x ver: status is OK, wait for CORE_RESET_NTF to complete */ } static u8 nci_core_init_rsp_packet_v1(struct nci_dev *ndev,
Signature · 1 line
2.53.0
Quoted 62 lines · Yuchao Zhang
Commit
bcd684aace34("net/nfc/nci: Support NCI 2.x initial sequence") added handling ofCORE_RESET_NTFinnci_core_reset_ntf_packet(). When received, it updatesndev->nci_ver,ndev->manufact_id, andndev->manufact_specific_info, and callsnci_req_complete(ndev, NCI_STATUS_OK)to finish the pending reset request.However, unlike other notification handlers in
ntf.c(which validatendev->statebefore completing requests),nci_core_reset_ntf_packet()does not check whether a core reset request is actually pending. If an unsolicited or delayedCORE_RESET_NTFarrives (e.g. after a reset command times out or from a misbehaving NFCC), it unconditionally:
1. Completes whatever request is currently in-flight (such asCORE_INIT,RF_DISCOVER, orCONN_CREATE) withNCI_STATUS_OK, leading to kernel state desynchronization.
2. Overwritesndev->nci_verand manufacturer info. Becausendev->nci_veris used as a selector for subsequent packet formats and parsers (e.g., innci_open_device()andnci_core_init_rsp_packet()), unexpectedly modifying it can cause protocol format confusion.A similar issue exists in
nci_core_reset_rsp_packet(): an unexpected or delayed response packet can prematurely complete an unrelated in-flight request.Fix this by ensuring
CORE_RESET_NTFandCORE_RESET_RSPare only processed by the core layer when a reset command is actively awaiting them:
- SetNCI_RESET_PENDINGinnci_reset_req()when sendingCORE_RESET_CMD.
- Innci_core_reset_rsp_packet(), ignore the response ifNCI_RESET_PENDINGis not set. If set, atomically clear the flag and complete the request on failure or for NCI 1.x (checkingskb->len>=sizeof(*rsp)).
- In__nci_request(), ensureNCI_RESET_PENDINGis cleared upon request completion, cancellation, or timeout.
- Innci_core_reset_ntf_packet(), fail any in-flight reset request if the controller reports an unrecoverable error (0x00), and only process command-triggered notifications (0x02). Non-command triggers (e.g. power-on0x01or proprietary triggers) and unexpected notifications return 0 without modifying core state, ensuring driver-specific notification handlers (such as fdp firmware patch handling) still receive them.
- Innci_core_reset_ntf_packet(), support notifications shorter than 9 bytes (e.g. NCI 2.0 frames withmanufacturer_specific_len== 0), validating the fixed header length and safely boundingmanufact_specific_inforeads.Fixes bcd684aace34 ("net/nfc/nci: Support NCI 2.x initial sequence")Signed-off-by Yuchao Zhang <[email protected]>---
v3:
- Innci_core_reset_ntf_packet(), do not reject notifications shorter than 9 bytes with-EINVAL; instead allow valid short frames (e.g. 2-byte NCI 1.x or 5-byte NCI 2.0 withmanufacturer_specific_len== 0), safely cap field reads, and return 0 so driver-registered hooks (such as fdp firmware patching) are not skipped (per Sashiko AI).
- Fix trigger semantics: in NCI,CORE_RESET_CMDtrigger is0x02(NCI_RESET_TRIGGER_RESET_CMD). If controller reports an unrecoverable error (0x00,NCI_RESET_TRIGGER_ERROR) while reset is pending, fail the request instead of completing with OK; ignore power-on (0x01) and proprietary triggers at core layer (per Sashiko AI).
- Innci_core_reset_rsp_packet(), usetest_and_clear_bit(NCI_RESET_PENDING)upon completing the request to consume the flag atomically (per Sashiko AI).
- Link to v2: https://lore.kernel.org/netdev/[email protected]/
Quoted 12 lines
v2:
- Do not abort the notification pipeline on unexpectedCORE_RESET_NTF(return 0 instead of-EINVAL), preserving driver-level hooks (e.g. fdp firmware patching) per Simon Horman.
- Skip power-on notifications (trigger0x01, identical in NCI 1.0 and 2.0) while gating both NCI 1.0 (0x00) and NCI 2.0 (0x02) command-triggered resets onNCI_RESET_PENDINGalone.
- CheckNCI_RESET_PENDINGinnci_core_reset_rsp_packet()to avoid completing unrelated requests on unexpected responses.
- Explicitly checkskb->len>=sizeof(*rsp)innci_core_reset_rsp_packet()for NCI 1.x handling.
- Link to v1: https://lore.kernel.org/netdev/[email protected]/