From ee160c9193731d9fa758dcac3076ced650129254 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 3 Nov 2026 01:23:49 -0600 Subject: [PATCH] Correctly parse padded STUN attributes --- src/SimpleStun.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/SimpleStun.c b/src/SimpleStun.c index cac7d6b..aacdc18 100644 --- a/src/SimpleStun.c +++ b/src/SimpleStun.c @@ -214,23 +214,28 @@ int LiFindExternalAddressIP4(const char* stunServer, unsigned short stunPort, un attribute = (PSTUN_ATTRIBUTE_HEADER)(&resp.hdr + 1); bytesRead -= sizeof(resp.hdr); - while (bytesRead > (int)sizeof(*attribute)) { - if (bytesRead < (int)(sizeof(*attribute) + htons(attribute->length))) { - Limelog("STUN attribute out of bounds: %d\n", htons(attribute->length)); + while (bytesRead >= (int)sizeof(*attribute)) { + int attrLen = htons(attribute->length); + + if (bytesRead < (int)sizeof(*attribute) + attrLen) { + Limelog("STUN attribute out of bounds: %d\n", attrLen); err = -5; goto Exit; } // Mask off the comprehension bit else if ((htons(attribute->type) & 0x7FFF) != STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS) { + // Attributes are padded to 4 byte alignment + int attrPaddedLen = (attrLen + 3) & ~3; + // Continue searching if this wasn't our address - bytesRead -= sizeof(*attribute) + htons(attribute->length); - attribute = (PSTUN_ATTRIBUTE_HEADER)(((char*)attribute) + sizeof(*attribute) + htons(attribute->length)); + bytesRead -= sizeof(*attribute) + attrPaddedLen; + attribute = (PSTUN_ATTRIBUTE_HEADER)(((char*)attribute) + sizeof(*attribute) + attrPaddedLen); continue; } ipv4Attrib = (PSTUN_MAPPED_IPV4_ADDRESS_ATTRIBUTE)attribute; - if (htons(ipv4Attrib->hdr.length) != 8) { - Limelog("STUN address length mismatch: %d\n", htons(ipv4Attrib->hdr.length)); + if (attrLen != 8) { + Limelog("STUN address length mismatch: %d\n", attrLen); err = -5; goto Exit; }