Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added extension for syscall interception
[palacios.git] / palacios / src / extensions / ext_syscall_hijack.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2011, Kyle C. Hale <kh@u.norhtwestern.edu>
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Kyle C. Hale <kh@u.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm.h>
21 #include <palacios/vm_guest_mem.h>
22 #include <palacios/vm_guest.h>
23 #include <palacios/vmm_intr.h>
24 #include <palacios/vmm_decoder.h>
25 #include <palacios/vmm_string.h>
26 #include <palacios/vmm_shadow_paging.h>
27 #include <palacios/vmm_extensions.h>
28
29 #include <interfaces/syscall_hijack.h>
30 #include <interfaces/sw_intr.h>
31
32 #include "syscall_ref.h"
33
34 #ifndef V3_CONFIG_DEBUG_EXT_SYSCALL_HIJACK
35 #undef PrintDebug
36 #define PrintDebug(fmt, args...)
37 #endif
38
39 #define MAX_CHARS 256
40 #ifndef max
41     #define max(a, b) ( ((a) > (b)) ? (a) : (b) )
42 #endif
43
44 #define SYSCALL_INT_VECTOR 0x80
45
46
47 struct v3_syscall_hook {
48     int (*handler)(struct guest_info * core, uint_t syscall_nr, void * priv_data);
49     void * priv_data;
50 };
51
52 static struct v3_syscall_hook * syscall_hooks[512];
53
54
55 static int v3_syscall_handler (struct guest_info * core, uint8_t vector, void * priv_data) {
56  
57     uint_t syscall_nr = (uint_t) core->vm_regs.rax;
58     int err = 0;
59
60     struct v3_syscall_hook * hook = syscall_hooks[syscall_nr];
61     if (hook == NULL) {
62 #ifdef V3_CONFIG_EXT_SYSCALL_PASSTHROUGH
63         if (v3_hook_passthrough_syscall(core, syscall_nr) == -1) {
64             PrintDebug("Error hooking passthrough syscall\n");
65             return -1;
66         }
67         hook = syscall_hooks[syscall_nr];
68 #else
69         return v3_signal_swintr(core, vector);
70 #endif
71     }
72     
73     err = hook->handler(core, syscall_nr, hook->priv_data);
74     if (err == -1) {
75         PrintDebug("V3 Syscall Handler: Error in syscall hook\n");
76         return -1;
77     }
78
79     return 0;
80 }
81
82
83 static int init_syscall_hijack (struct v3_vm_info * vm, v3_cfg_tree_t * cfg, void ** priv_data) {
84
85     return 0;
86 }
87
88
89 static int init_syscall_hijack_core (struct guest_info * core, void * priv_data) {
90
91     v3_hook_swintr(core, SYSCALL_INT_VECTOR, v3_syscall_handler, NULL);
92     return 0;
93 }
94
95
96 static void print_arg (struct  guest_info * core, v3_reg_t reg, uint8_t argnum) {
97
98     addr_t hva;
99     int ret = 0;
100     
101     PrintDebug("\t ARG%d: INT - %ld\n", argnum, (long) reg);
102
103     if (core->mem_mode == PHYSICAL_MEM) {
104         ret = v3_gpa_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
105     }
106     else { 
107         ret = v3_gva_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
108     }
109
110     PrintDebug("\t       STR - ");
111     if (ret == -1) {
112         PrintDebug("\n");
113         return;
114     }
115         
116     uint32_t c = max(MAX_CHARS, 4096 - (hva % 4096));
117     int i = 0;
118     for (; i < c && *((char*)(hva + i)) != 0; i++) {
119         PrintDebug("%c", *((char*)(hva + i)));
120     }
121     PrintDebug("\n");
122 }
123
124
125 static void print_syscall (uint8_t is64, struct guest_info * core) {
126
127     if (is64) {
128         PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name64(core->vm_regs.rax));
129     } else {
130         PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name32(core->vm_regs.rax));
131     }
132
133     print_arg(core, core->vm_regs.rbx, 1);
134     print_arg(core, core->vm_regs.rcx, 2);
135     print_arg(core, core->vm_regs.rdx, 3);
136 }
137
138
139
140
141 static struct v3_extension_impl syscall_impl = {
142     .name = "syscall_intercept",
143     .init = init_syscall_hijack,
144     .deinit = NULL,
145     .core_init = init_syscall_hijack_core,
146     .core_deinit = NULL,
147     .on_entry = NULL,
148     .on_exit = NULL
149 };
150
151 register_extension(&syscall_impl);
152
153
154
155
156 static inline struct v3_syscall_hook * get_syscall_hook (struct guest_info * core, uint_t syscall_nr) {
157     return syscall_hooks[syscall_nr];
158
159
160
161 int v3_hook_syscall (struct guest_info * core,
162     uint_t syscall_nr,
163     int (*handler)(struct guest_info * core, uint_t syscall_nr, void * priv_data),
164     void * priv_data) 
165 {
166     struct v3_syscall_hook * hook = (struct v3_syscall_hook *)V3_Malloc(sizeof(struct v3_syscall_hook));
167
168     
169     if (hook == NULL) {
170         return -1;
171     }
172
173     if (get_syscall_hook(core, syscall_nr) != NULL) {
174         PrintError("System Call #%d already hooked\n", syscall_nr);
175         return -1;
176     }
177
178     hook->handler = handler;
179     hook->priv_data = priv_data;
180
181     syscall_hooks[syscall_nr] = hook;
182
183     return 0;
184 }
185
186
187 static int passthrough_syscall_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
188     print_syscall(0, core);
189     return 0;
190 }
191
192
193 int v3_hook_passthrough_syscall (struct guest_info * core, uint_t syscall_nr) {
194     
195     int rc = v3_hook_syscall(core, syscall_nr, passthrough_syscall_handler, NULL);
196
197     if (rc) {
198         PrintError("failed to hook syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
199         return -1;
200     } else {
201         PrintDebug("hooked syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
202         return 0;
203     }
204
205     /* shouldn't get here */
206     return 0;
207 }
208
209 /*
210 int v3_sysexecve_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
211     addr_t hva, key;
212     struct exec_hook * hook;
213     int ret;
214     
215     ret = v3_gva_to_hva(core, get_addr_linear(core, (addr_t)core->vm_regs.rbx, &(core->segments.ds)), &hva);
216     if (ret == -1) {
217         PrintDebug("Error translating file path in sysexecve handler\n");
218         return -1;
219     }
220
221     key = v3_hash_buffer((uchar_t*)hva, strlen((uchar_t*)hva));
222     if ((hook = (struct exec_hook*)v3_htable_search(core->exec_hooks.bin_table, key)) != NULL) {
223        if (hook->handler(core, NULL) == -1) {
224             PrintDebug("Error handling execve hook\n");
225             return -1;
226         }
227     } 
228         
229     return 0;
230 }
231
232 */