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
+7 -1
View File
@@ -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<string> propList;
unsigned char version = 1;
unsigned short hash1, hash2, hash3, hash4;