-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvm_test.c
208 lines (182 loc) · 5.59 KB
/
kvm_test.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/kvm.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
/**
* You can find the KVM documentation here :
*
* https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt
*
* a snapshot of this file is available here : kvm-api.txt
*
**/
int main(int argc, char* argv[]) {
const uint8_t code[] = {
0xb8, 0x2a, 0x00, 0x00, 0x00,
0xf4,
};
//func to read file size, thanks stackoverflow
off_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0) {
return st.st_size;
}
return -1;
}
//Create the fd for the driver
int devkvm_fd = open("/dev/kvm", O_RDWR | O_CLOEXEC);
//Check that it is not negative
if(devkvm_fd < 0) {
return 1;
}
int ret = ioctl(devkvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY);
if(ret == -1) {
close(devkvm_fd);
return 1;
}
//Get system kvm version for the lulz
int kvm_api_version = ioctl(devkvm_fd, KVM_GET_API_VERSION, NULL);
printf("version %d\n", kvm_api_version);
// create vm, see doc section 4.2 KVM_CREATE_VM
int vm_fd = ioctl(devkvm_fd, KVM_CREATE_VM, 0);
if(vm_fd == -1) {
printf("error create vm");
return 1;
}
// create vcpu, see doc section 4.7 KVM_CREATE_VCPU
int vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0);
if(vm_fd == -1) {
printf("error create vcpu");
return 1;
}
//get xsave to try and identify wtf it is
/*struct kvm_xsave xsave;
int xsave_r = ioctl(vcpu_fd, KVM_GET_XSAVE, xsave);
for(int i = 0; i < 1024; i++) {
printf("%d\n", xsave.region[i]);
}*/
//xsave appears to be a mix of fixed and nondeterministic
//memory allocation
//from file
/*int mem_fd;
mem_fd = open("hw", O_RDWR);
//File size
size_t mem_size = 0x8000;
//size_t mem_size = (size_t)fsize("hw");
size_t prg_size = (size_t)fsize("hw");
printf("mem size=%u\n", mem_size);
printf("prg size=%u\n", prg_size);
__u64 *memory = mmap(NULL, mem_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, mem_fd, 0);
if(memory == MAP_FAILED) {
printf("error mem map");
close(vcpu_fd);
close(devkvm_fd);
return 1;
}*/
//test: from scratch
//works
size_t mem_size = 4096;
__u64 *memory = mmap(NULL, mem_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
memcpy(memory, code, sizeof(code));
//Tell kvm about this memory space
struct kvm_userspace_memory_region region = {
.slot = 0,
.guest_phys_addr = mem_size,
.memory_size = mem_size,
.userspace_addr = memory,
};
int memset_rc = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, ®ion);
if(memset_rc == -1) {
printf("error set mem region");
}
//ssize_t s = write(STDOUT_FILENO, memory, 1024);
// Now, we have to set BOTH RIP and CS to the beginning of the previous segment (i.e. they have to take the value of "memory" or 0, not sure)
//Set code segment (CS)
//Code segment orchestrates jumps, set to 0 to start at beginning of guest memory
struct kvm_sregs vcpu_sregs;
int get_sregs_sc = ioctl(vcpu_fd, KVM_GET_SREGS, &vcpu_sregs);
if(get_sregs_sc == -1) {
printf("can't get sregs");
}
vcpu_sregs.cs.base = 0;
vcpu_sregs.cs.selector = 0;
int set_sregs_sc = ioctl(vcpu_fd, KVM_SET_SREGS, &vcpu_sregs);
if(set_sregs_sc == -1) {
printf("can't set sregs");
}
//set instruction pointer (RIP) to "memory" variable, where guest memory begins
struct kvm_regs vcpu_regs;
//pass pointer to created object. if you make a pointer to struct, enjoy your segfault (out of experience)
/*int get_regs_sc = ioctl(vcpu_fd, KVM_GET_REGS, &vcpu_regs);
if(get_regs_sc == -1) {
printf("can't get regs");
}*/
vcpu_regs.rip = memory;
vcpu_regs.rax = 3;
vcpu_regs.rbx = 0;
vcpu_regs.rflags = 0x2;
int set_regs_sc = ioctl(vcpu_fd, KVM_SET_REGS, &vcpu_regs);
if(set_regs_sc == -1) {
printf("can't set regs");
}
// get KVM_RUN implicit parameter block for interrupt handling (this is documented in doc section 4.10 KVM_RUN)
// CAUTION! Contrary to all that might seem logical, this is a SYSTEM (/dev/kvm/ fd) ioctl and not a VCPU one
int mmap_size = ioctl(devkvm_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
printf("KVM_VCPU_MMAP_SIZE = %d\n", mmap_size);
//Size problem: -1
struct kvm_run *kvm_run_parameters = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu_fd, 0);
if (kvm_run_parameters<0) {
printf("%u\n", errno);
}
printf("ready to run\n");
//See doc section 4.10 KVM_RUN
//Apparently, code has to run in a loop
int run_ret;
int run_flag = 1;
while (run_flag) {
run_ret = ioctl(vcpu_fd, KVM_RUN, NULL);
if(run_ret == -1) {
err(1, "KVM_RUN");
break;
}
switch (kvm_run_parameters->exit_reason) {
case KVM_EXIT_HLT:
printf("Exit: hlt\n");
puts("KVM_EXIT_HLT");
run_flag = 0;
break;
default:
//Exit reason 9 is "KVM_EXIT_FAIL_ENTRY"
printf("oh shit exit reason = %x\n", kvm_run_parameters->exit_reason);
run_flag = 0;
break;
}
}
//Read memory
//ssize_t s = write(STDOUT_FILENO, memory, mem_size);
printf("get regs\n");
int get_regs_sc = ioctl(vcpu_fd, KVM_GET_REGS, &vcpu_regs);
printf("Reading register RAX:\n");
printf("%lld\n", vcpu_regs.rax);
//more info on the KVM_EXIT_FAIL_ENTRY can be found in this struct
//apparently 80000021 is invalid guest state
printf("Failure entry: %x\n", kvm_run_parameters->fail_entry.hardware_entry_failure_reason);
//Dispose of borrowed memory
//TODO: P sure we forgot shit
munmap(kvm_run_parameters, mmap_size);
munmap(memory, mem_size);
//Close all open resources
close(vcpu_fd);
close(vm_fd);
close(devkvm_fd);
return 0;
}