Merge branch 'main' of ssh://anscenter.ddns.net:2222/anscenter/ANSCORE

# Conflicts:
#	core/anslicensing/hwid.cpp
This commit is contained in:
2026-08-21 10:31:45 +10:00
7 changed files with 720 additions and 226 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;
}
+14
View File
@@ -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;
+83 -6
View File
@@ -21,9 +21,10 @@
#include <net/if.h>
#ifdef __linux__
# include <sys/ioctl.h>
# include <sys/stat.h> // stat() — physical-NIC test in linuxIsPhysicalNic
# include <netinet/in.h>
# include <unistd.h>
# include <string.h>
# include <string.h> // strncmp/strlen (same test)
// Match Linux to FreeBSD
# define AF_LINK AF_PACKET
@@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list<std::string>* propList)
propList->push_back(buf);
}
#ifdef __linux__
// A PHYSICAL network interface, i.e. one backed by a real hardware device.
//
// Why this exists: the hardware id is built from up to 6 enumerated MACs, and
// on Linux getifaddrs() also returns VIRTUAL interfaces — docker0 and the
// veth* pairs a container creates, tun*/wg* from a VPN, br-*/virbr* bridges.
// Container veths get RANDOM MACs and come and go with the containers, so an
// unfiltered enumeration can hand back a different set on every call: the id
// changes and an activated licence silently stops matching its machine. Boxes
// running Docker (the Coder feature) hit this routinely.
//
// The test mirrors what the Windows branch already does via WMI ("PNPDeviceId
// LIKE %PCI%" — physical adapters only). /sys/class/net/<name>/device is a
// symlink to the backing device and exists ONLY for hardware-backed
// interfaces; virtual ones have no such link. That is the kernel's own answer,
// so it stays correct as new virtual-interface flavours appear — unlike a name
// denylist, which is kept only as a cheap second line of defence.
static bool linuxIsPhysicalNic(const char* ifname)
{
if (ifname == NULL || ifname[0] == '\0') return false;
// Names that are virtual by construction. Not exhaustive on purpose — the
// /sys check below is the real test; this only catches oddities where /sys
// is unavailable (unusual containers, minimal namespaces).
static const char* kVirtualPrefixes[] = {
"lo", "docker", "veth", "br-", "virbr", "tun", "tap", "wg",
"vmnet", "vboxnet", "zt", "ppp", "dummy", "kube", "cni", "flannel",
"lxc", "lxd", "nerdctl", "podman", "cali", "weave"
};
for (const char* prefix : kVirtualPrefixes) {
if (strncmp(ifname, prefix, strlen(prefix)) == 0) return false;
}
std::string path = std::string("/sys/class/net/") + ifname + "/device";
struct stat st;
// stat() (not lstat) so the symlink is followed — we want "the target
// exists", i.e. a real backing device.
return stat(path.c_str(), &st) == 0;
}
#endif // __linux__
void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
// Head of the interface address linked list
@@ -99,6 +141,14 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
return;
}
#ifdef __linux__
// Collected separately so physical NICs can be preferred wholesale. Taking
// them inline would let a veth that merely appears EARLIER in the list
// consume one of the maxCount slots and push a real NIC out.
std::list<std::string> physical;
std::list<std::string> fallback;
#endif
while (current != NULL && maxCount > 0) {
const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr);
@@ -127,8 +177,18 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
char buf[18];
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
#ifdef __linux__
// Bucket now, choose after the walk (see the lists above).
// maxCount is NOT decremented here: the cap applies to the
// SELECTED set, not to whatever happened to be enumerated.
if (linuxIsPhysicalNic(current->ifa_name))
physical.push_back(buf);
else
fallback.push_back(buf);
#else
propList->push_back(buf);
maxCount--;
#endif
}
}
@@ -137,6 +197,21 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
freeifaddrs(ifap);
ifap = NULL;
#ifdef __linux__
// Physical NICs only — that is the whole point. FAIL-SAFE: if the filter
// found none (no /sys, an exotic NIC driver, a network namespace with only
// virtual links) fall back to the unfiltered set rather than returning
// nothing. An empty list would drop ALL MAC entropy from the id, and with
// the system-UUID and disk-serial slots already constant on Linux that
// would leave near-identical ids across different machines — far worse
// than an id that occasionally drifts.
std::list<std::string>& chosen = physical.empty() ? fallback : physical;
for (std::list<std::string>::const_iterator it = chosen.begin();
it != chosen.end() && maxCount > 0; ++it, --maxCount) {
propList->push_back(*it);
}
#endif
}
void GetHDDPropertyList(std::list<std::string>* propList, int maxCount)
@@ -220,11 +295,13 @@ const char* HardwareId::GetCurrentHardwareId()
WmiHelper wmi;
#endif
// 3 version bits + 9 x 16-bit hashes = 147 bits are written below, but only
// the first 16 bytes are base32-encoded into the HWID (the HDD fields are
// truncated by the 25-char format). The stream buffer must still hold every
// write: 125 bits (16 bytes) overflowed the allocation by 3 bytes.
BitStream3 bits(160);
// 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;