Skip to contentSkip to conversation

nfc: nci: validate packet length when parsing NCI 2.x RF interfaces

PATCH In review Zijing Yin 3 participants 3 messages Jun 11 – Jun 21

Subsystems (4)
lore
Share & tools
lore

Zijing Yin

author
#0 lore raw

PATCH 1/1 nfc: nci: validate packet length when parsing NCI 2.x RF interfaces

nci_core_init_rsp_packet_v2() parses the variable-length list of supported RF interfaces carried in an NCI 2.x CORE_INIT_RSP without ever validating the controller-supplied lengths against the size of the received packet.

Each list entry is a (RF interface, RF extension count, RF extensions[]) tuple. The loop walks the list using the per-entry extension count (rf_extension_cnt, up to 255) taken straight from the packet, so a malformed CORE_INIT_RSP can advance the read pointer far past the end of the skb data buffer. The stored interface count is clamped to NCI_MAX_SUPPORTED_RF_INTERFACES so the write side is bounded, but the read side runs off the end of the buffer.

A malformed CORE_INIT_RSP from the controller, also reachable from user space through the virtual NCI device (CONFIG_NFC_VIRTUAL_NCI) once the device has entered NCI 2.x mode, therefore makes the parser read past the end of the response buffer while walking the interface list, copying the out-of-bounds bytes into ndev->supported_rf_interfaces[].

Reject responses shorter than the fixed part of the structure, and make sure each interface entry and its extension bytes lie within the received packet before dereferencing them. A truncated or malformed list is treated as a syntax error, which fails the CORE_INIT request instead of reading out of bounds.

Fixes bcd684aace34 ("net/nfc/nci: Support NCI 2.x initial sequence")
---
 net/nfc/nci/rsp.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
net/nfc/nci/rsp.c +17 -1
index 9eeb86282..152b5f57e 100644
@@ -87,7 +87,8 @@ static u8 nci_core_init_rsp_packet_v2(struct nci_dev *ndev,
 				      const struct sk_buff *skb)
 {
 	const struct nci_core_init_rsp_nci_ver2 *rsp = (void *)skb->data;
-	const u8 *supported_rf_interface = rsp->supported_rf_interfaces;
+	const u8 *skb_end = skb->data + skb->len;
+	const u8 *supported_rf_interface;
 	u8 rf_interface_idx = 0;
 	u8 rf_extension_cnt = 0;
 
@@ -96,6 +97,11 @@ static u8 nci_core_init_rsp_packet_v2(struct nci_dev *ndev,
 	if (rsp->status != NCI_STATUS_OK)
 		return rsp->status;
 
+	if (skb->len < sizeof(*rsp))
+		return NCI_STATUS_SYNTAX_ERROR;
+
+	supported_rf_interface = rsp->supported_rf_interfaces;
+
 	ndev->nfcc_features = __le32_to_cpu(rsp->nfcc_features);
 	ndev->num_supported_rf_interfaces = rsp->num_supported_rf_interfaces;
 
@@ -104,10 +110,20 @@ static u8 nci_core_init_rsp_packet_v2(struct nci_dev *ndev,
 		    NCI_MAX_SUPPORTED_RF_INTERFACES);
 
 	while (rf_interface_idx < ndev->num_supported_rf_interfaces) {
+		/* The supported RF interfaces are a variable-length list of
+		 * (interface, extension count, extensions[]) tuples supplied by
+		 * the NFCC; bail out if its lengths would take us past the end
+		 * of the received packet.
+		 */
+		if (skb_end - supported_rf_interface < 2)
+			return NCI_STATUS_SYNTAX_ERROR;
+
 		ndev->supported_rf_interfaces[rf_interface_idx++] = *supported_rf_interface++;
 
 		/* skip rf extension parameters */
 		rf_extension_cnt = *supported_rf_interface++;
+		if (skb_end - supported_rf_interface < rf_extension_cnt)
+			return NCI_STATUS_SYNTAX_ERROR;
 		supported_rf_interface += rf_extension_cnt;
 	}
 
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Zijing Yin

nci_core_init_rsp_packet_v2() parses the variable-length list of supported RF interfaces carried in an NCI 2.x CORE_INIT_RSP without ever validating the controller-supplied lengths against the size of the received packet.

Each list entry is a (RF interface, RF extension count, RF extensions[]) tuple. The loop walks the list using the per-entry extension count (rf_extension_cnt, up to 255) taken straight from the packet, so a malformed CORE_INIT_RSP can advance the read pointer far past the end of the skb data buffer. The stored interface count is clamped to NCI_MAX_SUPPORTED_RF_INTERFACES so the write side is bounded, but the read side runs off the end of the buffer.

A malformed CORE_INIT_RSP from the controller, also reachable from user space through the virtual NCI device (CONFIG_NFC_VIRTUAL_NCI) once the device has entered NCI 2.x mode, therefore makes the parser read past the end of the response buffer while walking the interface list, copying the out-of-bounds bytes into ndev->supported_rf_interfaces[].

Reject responses shorter than the fixed part of the structure, and make sure each interface entry and its extension bytes lie within the received packet before dereferencing them. A truncated or malformed list is treated as a syntax error, which fails the CORE_INIT request instead of reading out of bounds.

Quoted 3 lines
Fixes bcd684aace34 ("net/nfc/nci: Support NCI 2.x initial sequence")

FTR, there is an AI-generated review of this patch available on sashiko.dev However, I believe that the issue flagged there can be considered in the context of possible follow-up rather than effecting the progress of this patch.

maintainer
#2 lore raw
In reply to Zijing Yin
Quoted 25 lines · Zijing Yin

nci_core_init_rsp_packet_v2() parses the variable-length list of supported RF interfaces carried in an NCI 2.x CORE_INIT_RSP without ever validating the controller-supplied lengths against the size of the received packet.

Each list entry is a (RF interface, RF extension count, RF extensions[]) tuple. The loop walks the list using the per-entry extension count (rf_extension_cnt, up to 255) taken straight from the packet, so a malformed CORE_INIT_RSP can advance the read pointer far past the end of the skb data buffer. The stored interface count is clamped to NCI_MAX_SUPPORTED_RF_INTERFACES so the write side is bounded, but the read side runs off the end of the buffer.

A malformed CORE_INIT_RSP from the controller, also reachable from user space through the virtual NCI device (CONFIG_NFC_VIRTUAL_NCI) once the device has entered NCI 2.x mode, therefore makes the parser read past the end of the response buffer while walking the interface list, copying the out-of-bounds bytes into ndev->supported_rf_interfaces[].

Reject responses shorter than the fixed part of the structure, and make sure each interface entry and its extension bytes lie within the received packet before dereferencing them. A truncated or malformed list is treated as a syntax error, which fails the CORE_INIT request instead of reading out of bounds.

Quoted 7 lines
Fixes bcd684aace34 ("net/nfc/nci: Support NCI 2.x initial sequence")
---
  net/nfc/nci/rsp.c | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)

Hello Zijing,

in meanwhile [1] got merged earlier patch, which also seems to address the issue.

Could you review that the issue is now fixed for you?

The NFC tree is here [2].

Thank you and sorry for the late reply
David

[1] https://lore.kernel.org/all/[email protected]/
[2] https://codeberg.org/linux-nfc/linux