anslicensing: fix 3-byte heap overflow in hardware-id BitStream3

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 <noreply@anthropic.com>
This commit is contained in:
2026-07-27 09:29:05 +10:00
co-authored by Claude Fable 5
parent ac79e51d17
commit 4f392ce95e
3 changed files with 38 additions and 15 deletions
+17 -14
View File
@@ -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;
}