Getting the MAChine Serial Number and CPU Id Using C/C++ in Linux

Getting the machine serial number and CPU ID using C/C++ in Linux

Here is what the Linux kernel seems to use:

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));
}

which one then can use as e.g.:

#include <stdio.h>

int main(int argc, char **argv)
{
unsigned eax, ebx, ecx, edx;

eax = 1; /* processor info and feature bits */
native_cpuid(&eax, &ebx, &ecx, &edx);

printf("stepping %d\n", eax & 0xF);
printf("model %d\n", (eax >> 4) & 0xF);
printf("family %d\n", (eax >> 8) & 0xF);
printf("processor type %d\n", (eax >> 12) & 0x3);
printf("extended model %d\n", (eax >> 16) & 0xF);
printf("extended family %d\n", (eax >> 20) & 0xFF);

/* EDIT */
eax = 3; /* processor serial number */
native_cpuid(&eax, &ebx, &ecx, &edx);

/** see the CPUID Wikipedia article on which models return the serial
number in which registers. The example here is for
Pentium III */
printf("serial number 0x%08x%08x\n", edx, ecx);

}

Where a good reference on how to use the CPUID instruction is in this Wikipedia article.

EDIT The Wikipedia article says that the serial number was introduced with the Pentium III but was not anymore implemented in later models due to privacy concerns. On a Linux system you can check for the presence of this feature (PSN bit) by doing:

grep -i --color psn /proc/cpuinfo

if this does not show anything, your system does not support a processor serial number.

System call to get serial number of machine in Linux

Here is an excerpt of a blog post by Lennart Poettering about IDs in general. It is about unique IDs, not necessarily about unique IDs in relation with security:

  • /sys/class/dmi/id/product_uuid: The main board product UUID, as set by the
    board manufacturer and encoded in the
    BIOS DMI information. It may be used
    to identify a mainboard and only the
    mainboard. It changes when the user
    replaces the main board. Also, often
    enough BIOS manufacturers write bogus
    serials into it. In addition, it is
    x86-specific. Access for unprivileged
    users is forbidden. Hence it is of
    little general use.

  • CPUID/EAX=3 CPU serial number: A CPU UUID, as set by the CPU manufacturer
    and encoded on the CPU chip. It may be
    used to identify a CPU and only a CPU.
    It changes when the user replaces the
    CPU. Also, most modern CPUs don't
    implement this feature anymore, and
    older computers tend to disable this
    option by default, controllable via a
    BIOS Setup option. In addition, it is
    x86-specific. Hence this too is of
    little general use.

So /sys/class/dmi/id/product_uuid seems like a good candidate for your validation daemon but means that your validation code needs to be run as a privileged user. The full blog post is really a valuable read!



Related Topics



Leave a reply



Submit