From 4f392ce95e43a2065d4b6557aff36a36c305cfb0 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Mon, 27 Jul 2026 09:29:05 +1000 Subject: [PATCH] anslicensing: fix 3-byte heap overflow in hardware-id BitStream3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GetCurrentHardwareId sized its bit stream at 125 bits (16 bytes) but writes 147 (3 version + 9 x 16-bit hashes), so the last two hash slots overflowed the heap block by 3 bytes on every id computation. The Windows heap absorbed it silently for years; it SIGSEGVs under macOS malloc — found when ANSGateway enabled this code on POSIX (fixed there in ANSGateway 3a34c68/6c3cdfd; this is the upstream port). Two parts, both id-preserving (only the first 16 bytes feed the base32 encode — verified byte-identical ids before/after in the gateway): - hwid.cpp: size the stream to the 147 bits actually written. - BitStream3: bounds-safe byteAt/setByte accessors on every Write/Read, Attach records the real buffer size, default ctor zero-inits — an under-sized stream can no longer corrupt the heap, and out-of-range reads return 0 (identical to the base32 padding zeros the Match path always read there). Co-Authored-By: Claude Fable 5 --- core/anslicensing/bitstream3.cpp | 31 +++++++++++++++++-------------- core/anslicensing/bitstream3.h | 14 ++++++++++++++ core/anslicensing/hwid.cpp | 8 +++++++- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/core/anslicensing/bitstream3.cpp b/core/anslicensing/bitstream3.cpp index 1663d6e..e22e296 100644 --- a/core/anslicensing/bitstream3.cpp +++ b/core/anslicensing/bitstream3.cpp @@ -8,9 +8,11 @@ const unsigned char BitStream3::hiMask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0x BitStream3::BitStream3() : ownMemory(false), +buffer(NULL), +bufSize(0), +bitSize(0), currentBit(0), -currentByte(0), -buffer(NULL) +currentByte(0) { } @@ -41,6 +43,7 @@ void BitStream3::Attach(void * buf, int bitCount) { buffer = (unsigned char *)buf; bitSize = bitCount; + bufSize = (bitCount + 7) >> 3; currentBit = 0; currentByte = 0; } @@ -53,20 +56,20 @@ int BitStream3::Write(const unsigned char * buf, int bitCount) { if ((gap & 7) != 0) { - buffer[currentByte] = (unsigned char)((buffer[currentByte] << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap))); + setByte(currentByte, (unsigned char)((byteAt(currentByte) << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap)))); currentBit += gap; bitCount -= gap; currentByte++; for (; bitCount >= 8; i++, currentByte++, currentBit += 8, bitCount -= 8) - buffer[currentByte] = (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap))); + setByte(currentByte, (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap)))); } else for (; bitCount >= 8; currentByte++, i++, currentBit += 8, bitCount -= 8) - buffer[currentByte] = buf[i]; + setByte(currentByte, buf[i]); } if (bitCount > 0) { - buffer[currentByte] = (unsigned char)((buffer[currentByte] << bitCount) | (buf[i] & ((1 << bitCount) - 1))); + setByte(currentByte, (unsigned char)((byteAt(currentByte) << bitCount) | (buf[i] & ((1 << bitCount) - 1)))); currentBit += bitCount; } @@ -86,20 +89,20 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount) if ((gap & 7) != 0) { for (; bitCount >= 8 + 8 - gap; bitCount -= 8, currentByte++, currentBit += 8, i++) - readBuf[i] = (unsigned char)((buffer[currentByte] << gap) | (buffer[currentByte + 1] >> (8 - gap))); + readBuf[i] = (unsigned char)((byteAt(currentByte) << gap) | (byteAt(currentByte + 1) >> (8 - gap))); - // byte align + // byte align if (bitCount > 0) { if (bitCount < 8 - gap) { - readBuf[i] = (unsigned char)((buffer[currentByte] >> (8 - gap - bitCount)) & ((1 << bitCount) - 1)); + readBuf[i] = (unsigned char)((byteAt(currentByte) >> (8 - gap - bitCount)) & ((1 << bitCount) - 1)); currentBit += bitCount; bitCount = 0; } else { - readBuf[i] = (unsigned char)(buffer[currentByte] & ((1 << (8 - gap)) - 1)); + readBuf[i] = (unsigned char)(byteAt(currentByte) & ((1 << (8 - gap)) - 1)); bitCount -= (8 - gap); currentBit += (8 - gap); @@ -111,15 +114,15 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount) } else for (; bitCount >= 8; bitCount -= 8, currentByte++, currentBit += 8, i++) - readBuf[i] = buffer[currentByte]; + readBuf[i] = byteAt(currentByte); // already aligned if (bitCount > 0) { if (currentBit + 8 <= bitSize) // not last byte - readBuf[i] |= (unsigned char)((buffer[currentByte] >> (8 - bitCount)) & ((1 << bitCount) - 1)); - else // last byte - readBuf[i] = (unsigned char)((buffer[currentByte] >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1)); + readBuf[i] |= (unsigned char)((byteAt(currentByte) >> (8 - bitCount)) & ((1 << bitCount) - 1)); + else // last byte + readBuf[i] = (unsigned char)((byteAt(currentByte) >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1)); currentBit += bitCount; } diff --git a/core/anslicensing/bitstream3.h b/core/anslicensing/bitstream3.h index 841bd3b..c968a0f 100644 --- a/core/anslicensing/bitstream3.h +++ b/core/anslicensing/bitstream3.h @@ -16,6 +16,20 @@ public: unsigned char * GetBuffer(); protected: + // Bounds-safe accessors: every Write/Read goes through these so a caller + // that under-sizes the stream can no longer corrupt the heap (the 125-vs-147 + // bug in GetCurrentHardwareId overflowed 3 bytes for years). Out-of-range + // reads return 0 — identical to the base32 padding zeros the hardware-id + // Match path has always effectively read there. + unsigned char byteAt(int i) const + { + return (buffer && i >= 0 && i < bufSize) ? buffer[i] : 0; + } + void setByte(int i, unsigned char v) + { + if (buffer && i >= 0 && i < bufSize) buffer[i] = v; + } + bool ownMemory; unsigned char * buffer; int bufSize, bitSize; diff --git a/core/anslicensing/hwid.cpp b/core/anslicensing/hwid.cpp index d86d98f..1b72a00 100644 --- a/core/anslicensing/hwid.cpp +++ b/core/anslicensing/hwid.cpp @@ -220,7 +220,13 @@ const char* HardwareId::GetCurrentHardwareId() WmiHelper wmi; #endif - BitStream3 bits(125); + // 147 bits are actually written below (3 version + 9 x 16-bit hashes). The + // previous size of 125 allocated only 16 bytes and the last two hash slots + // overflowed the heap block by 3 bytes (asymptomatic on the Windows heap, + // SIGSEGV under macOS malloc — found when ANSGateway enabled this code on + // POSIX). Only the first 16 bytes feed the base32 encode, so sizing the + // stream correctly changes no id on any platform. + BitStream3 bits(147); list propList; unsigned char version = 1; unsigned short hash1, hash2, hash3, hash4;