File: vmware.c

package info (click to toggle)
imvirt 0.9.6-13
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,364 kB
  • sloc: perl: 2,778; sh: 802; ansic: 316; makefile: 61
file content (132 lines) | stat: -rw-r--r-- 3,173 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* imvirt / VMware detection code
 *
 * Authors:
 *   Thomas Liske <liske@ibh.de>
 *
 * Copyright Holder:
 *   2009 - 2012 (C) IBH IT-Service GmbH [http://www.ibh.de/]
 *
 * License:
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this package; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

/* This code tries to detect the VMware version using the VMware backdoor's
 * GETVERSION command (http://chitchat.at.infoseek.co.jp/vmware/backdoor.html#cmd0ah).
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "detect.h"

#define VMWARE_MAGIC 0x564d5868
#define VMWARE_PORT 0x5658

#define VMWARE_CMD_GETVERSION 0x0a

#if defined(__i386__) && defined(__PIC__)
#define VMWARE_CMD(eax, ebx, ecx, edx) \
    __asm__( \
	"xchgl %%ebx, %1;" \
	"inl (%%dx);" \
	"xchgl %%ebx, %1" \
	: "+a"(eax), "+r"(ebx), "+c"(ecx), "+d"(edx) \
    );
#else
#define VMWARE_CMD(eax, ebx, ecx, edx) \
    __asm__("inl (%%dx)" \
	: "+a"(eax), "+b"(ebx), "+c"(ecx), "+d"(edx) \
    );
#endif

static void vmware_cmd(uint32_t cmd, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) {
    *eax = VMWARE_MAGIC;
    *ebx = 0xffffffff;
    *ecx = cmd;
    *edx = VMWARE_PORT;

    VMWARE_CMD(*eax, *ebx, *ecx, *edx);
}

static void sigh(int signum) {
    exit(0);
}

static int do_vmware() {
    uint32_t eax, ebx, ecx, edx;
    struct sigaction sa;

    /* ignore SIGSEGV (VMWARE_CMD will cause a SEGV on none VMware systems) */
    sa.sa_handler = sigh;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGSEGV, &sa, NULL);

    vmware_cmd(VMWARE_CMD_GETVERSION, &eax, &ebx, &ecx, &edx);

    /* sanity check: maybe VMWARE_CMD did not SEGV if there was something
     * on the I/O port - test if EBX has been set to VMWARE_MAGIC */
    if ((uint32_t)-1 && ebx == VMWARE_MAGIC) {
	char *product;

	switch(ecx) {
	    case 0x01:
		product = "Express";
		break;
	    case 0x02:
		product = "ESX Server";
		break;
	    case 0x03:
		product = "GSX Server";
		break;
	    case 0x04:
		product = "Workstation";
		break;
	    default:
		product = "?";
		break;
	}

	printf("%s\n", product);

	return 1;
    }

    return 0;
}

int main(int argc, char **argv) {
    helper_main(argc, argv);

    pid_t pid = fork();
    switch (pid) {
    case 0:
	exit(do_vmware());
    case -1:
        return 0;
    }

    int status;
    waitpid(pid, &status, 0);
    if (WIFEXITED(status))
        return WEXITSTATUS(status);

    return 0;
}