Skip to contentSkip to conversation

nfc: llcp: fix OOB read and u8 offset wrap in TLV parsers

PATCH Applied Muhammad Bilal 2 participants 2 messages Jun 22 – Jun 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nfc: llcp: fix OOB read and u8 offset wrap in TLV parsers

nfc_llcp_parse_gb_tlv() and nfc_llcp_parse_connection_tlv() contain three related bugs in their TLV parsing loops:

1. 'offset' is declared u8 but tlv_array_len is u16. When TLV data advances offset past 255 it silently wraps to zero, causing infinite loops or double-processing of buffer data.

2. Before reading tlv[0] (type) and tlv[1] (length) there is no check that offset+2 <= tlv_array_len. A truncated TLV causes an OOB read of one byte past the buffer end.

3. After reading the length field, the value bytes are accessed without checking offset+2+length <= tlv_array_len. A crafted length=0xFF on a short buffer causes up to 255 bytes of OOB read past the buffer end.

Both functions are reachable without authentication via nfc_llcp_set_remote_gb() which feeds remote LLCP general bytes directly into nfc_llcp_parse_gb_tlv() with no additional validation.

Fix all three issues by widening offset from u8 to u16 and adding bounds checks for both the TLV header and value field before each access.

Fixes 3df40eb3a2ea ("nfc: constify several pointers to u8, char and sk_buff")

---

Notes:
v2:
- Rebased onto current nfc/for-next.
- Dropped the previous nfc_llcp_recv_snl() fix since equivalent checks were merged by commit ed85d4cbbfaa ("nfc: llcp: bound SNL TLV parsing to the skb and add length checks").
- Retain only the fixes for u8 offset wraparound and missing TLV bounds checks in nfc_llcp_parse_gb_tlv() and nfc_llcp_parse_connection_tlv().
- Reject invalid TLVs silently with -EINVAL; dropped the v1 pr_err() logging, which was reachable from a remote peer.

 net/nfc/llcp_commands.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/nfc/llcp_commands.c +16 -2
index 291f26facbf3a..ca89fe967d6a2 100644
@@ -193,7 +193,8 @@ int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
 			  const u8 *tlv_array, u16 tlv_array_len)
 {
 	const u8 *tlv = tlv_array;
-	u8 type, length, offset = 0;
+	u8 type, length;
+	u16 offset = 0;
 
 	pr_debug("TLV array length %d\n", tlv_array_len);
 
@@ -201,9 +202,15 @@ int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
 		return -ENODEV;
 
 	while (offset < tlv_array_len) {
+		if (offset + 2 > tlv_array_len)
+			return -EINVAL;
+
 		type = tlv[0];
 		length = tlv[1];
 
+		if (offset + 2 + length > tlv_array_len)
+			return -EINVAL;
+
 		pr_debug("type 0x%x length %d\n", type, length);
 
 		switch (type) {
@@ -243,7 +250,8 @@ int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
 				  const u8 *tlv_array, u16 tlv_array_len)
 {
 	const u8 *tlv = tlv_array;
-	u8 type, length, offset = 0;
+	u8 type, length;
+	u16 offset = 0;
 
 	pr_debug("TLV array length %d\n", tlv_array_len);
 
@@ -251,9 +259,15 @@ int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
 		return -ENOTCONN;
 
 	while (offset < tlv_array_len) {
+		if (offset + 2 > tlv_array_len)
+			return -EINVAL;
+
 		type = tlv[0];
 		length = tlv[1];
 
+		if (offset + 2 + length > tlv_array_len)
+			return -EINVAL;
+
 		pr_debug("type 0x%x length %d\n", type, length);
 
 		switch (type) {

base-commit: ed85d4cbbfaa4e630c5aa0d607348b42620d976b

Signature · 1 line
2.54.0
maintainer Applied
#1 lore raw
Quoted 1 line · Muhammad Bilal

nfc: llcp: fix OOB read and u8 offset wrap in TLV parsers

Applied, thanks!

[1/1] nfc: llcp: fix OOB read and u8 offset wrap in TLV parsers

      commit: cb682b66aba4bc3f4dd4d7ae5612f49ad75b2087

Best regards,

Signature · 1 line
David Heidelberg <[email protected]>