diff --git a/core/anslicensing/hwid.cpp b/core/anslicensing/hwid.cpp index 1b72a00..349d960 100644 --- a/core/anslicensing/hwid.cpp +++ b/core/anslicensing/hwid.cpp @@ -21,9 +21,10 @@ #include #ifdef __linux__ # include +# include // stat() — physical-NIC test in linuxIsPhysicalNic # include # include -# include +# include // strncmp/strlen (same test) // Match Linux to FreeBSD # define AF_LINK AF_PACKET @@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list* 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//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* propList, int maxCount) { // Head of the interface address linked list @@ -99,6 +141,14 @@ void GetMACPropertyList(std::list* 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 physical; + std::list fallback; +#endif + while (current != NULL && maxCount > 0) { const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr); @@ -127,8 +177,18 @@ void GetMACPropertyList(std::list* 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* 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& chosen = physical.empty() ? fallback : physical; + for (std::list::const_iterator it = chosen.begin(); + it != chosen.end() && maxCount > 0; ++it, --maxCount) { + propList->push_back(*it); + } +#endif } void GetHDDPropertyList(std::list* propList, int maxCount)