cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

check_glibc_kernelversion.c (6207B)


      1/*
      2 * Check the lz insn.
      3 */
      4
      5#include <stdio.h>
      6#include <stdlib.h>
      7#include <stdint.h>
      8#include "sys.h"
      9
     10#define __LINUX_KERNEL_VERSION 131584
     11
     12#define DL_SYSDEP_OSCHECK(FATAL) \
     13  do {                                                                        \
     14    /* Test whether the kernel is new enough.  This test is only              \
     15       performed if the library is not compiled to run on all                 \
     16       kernels.  */                                                           \
     17    if (__LINUX_KERNEL_VERSION > 0)                                           \
     18      {                                                                       \
     19        char bufmem[64];                                                      \
     20        char *buf = bufmem;                                                   \
     21        unsigned int version;                                                 \
     22        int parts;                                                            \
     23        char *cp;                                                             \
     24        struct utsname uts;                                                   \
     25                                                                              \
     26        /* Try the uname syscall */                                           \
     27        if (__uname (&uts))                                                   \
     28          {                                                                   \
     29            /* This was not successful.  Now try reading the /proc            \
     30               filesystem.  */                                                \
     31            ssize_t reslen;                                                   \
     32            int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);         \
     33            if (fd == -1                                                      \
     34                || (reslen = __read (fd, bufmem, sizeof (bufmem))) <= 0)      \
     35              /* This also didn't work.  We give up since we cannot           \
     36                 make sure the library can actually work.  */                 \
     37              FATAL ("FATAL: cannot determine library version\n");            \
     38            __close (fd);                                                     \
     39            buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';          \
     40          }                                                                   \
     41        else                                                                  \
     42          buf = uts.release;                                                  \
     43                                                                              \
     44        /* Now convert it into a number.  The string consists of at most      \
     45           three parts.  */                                                   \
     46        version = 0;                                                          \
     47        parts = 0;                                                            \
     48        cp = buf;                                                             \
     49        while ((*cp >= '0') && (*cp <= '9'))                                  \
     50          {                                                                   \
     51            unsigned int here = *cp++ - '0';                                  \
     52                                                                              \
     53            while ((*cp >= '0') && (*cp <= '9'))                              \
     54              {                                                               \
     55                here *= 10;                                                   \
     56                here += *cp++ - '0';                                          \
     57              }                                                               \
     58                                                                              \
     59            ++parts;                                                          \
     60            version <<= 8;                                                    \
     61            version |= here;                                                  \
     62                                                                              \
     63            if (*cp++ != '.')                                                 \
     64              /* Another part following?  */                                  \
     65              break;                                                          \
     66          }                                                                   \
     67                                                                              \
     68        if (parts < 3)                                                        \
     69          version <<= 8 * (3 - parts);                                        \
     70                                                                              \
     71        /* Now we can test with the required version.  */                     \
     72        if (version < __LINUX_KERNEL_VERSION)                                 \
     73          /* Not sufficient.  */                                               \
     74          FATAL ("FATAL: kernel too old\n");                                  \
     75                                                                              \
     76        _dl_osversion = version;                                              \
     77      }                                                                       \
     78  } while (0)
     79
     80int main(void)
     81{
     82        char bufmem[64] = "2.6.22";
     83        char *buf = bufmem;
     84        unsigned int version;
     85        int parts;
     86        char *cp;
     87
     88        version = 0;
     89        parts = 0;
     90        cp = buf;
     91        while ((*cp >= '0') && (*cp <= '9'))
     92          {
     93            unsigned int here = *cp++ - '0';
     94
     95            while ((*cp >= '0') && (*cp <= '9'))
     96              {
     97                here *= 10;
     98                here += *cp++ - '0';
     99              }
    100
    101            ++parts;
    102            version <<= 8;
    103            version |= here;
    104
    105            if (*cp++ != '.')
    106              /* Another part following?  */
    107              break;
    108          }
    109
    110        if (parts < 3)
    111          version <<= 8 * (3 - parts);
    112        if (version < __LINUX_KERNEL_VERSION)
    113                err();
    114        pass();
    115        exit(0);
    116}