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.


cosmetic changes
[palacios.git] / palacios / src / palacios / vmm_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_syscall_hijack.h>
28 #include <palacios/vmm_linux_syscall_map.h>
29 #include <palacios/vmm_process_environment.h>
30 #include <palacios/vmm_execve_hook.h>
31
32
33 #ifndef CONFIG_DEBUG_SYSCALL_HIJACK
34 #undef PrintDebug
35 #define PrintDebug(fmt, args...)
36 #endif
37
38 #define MAX_CHARS 256
39 #ifndef max
40     #define max(a, b) ( ((a) > (b)) ? (a) : (b) )
41 #endif
42
43
44
45 static void print_arg (struct  guest_info * core, v3_reg_t reg, uint8_t argnum) {
46
47     addr_t hva;
48     int ret = 0;
49     
50     PrintDebug("\t ARG%d: INT - %ld\n", argnum, (long) reg);
51
52     if (core->mem_mode == PHYSICAL_MEM) {
53         ret = v3_gpa_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
54     }
55     else { 
56         ret = v3_gva_to_hva(core, get_addr_linear(core, reg, &(core->segments.ds)), &hva);
57     }
58
59     PrintDebug("\t       STR - ");
60     if (ret == -1) {
61         PrintDebug("\n");
62         return;
63     }
64         
65     uint32_t c = max(MAX_CHARS, 4096 - (hva % 4096));
66     int i = 0;
67     for (; i < c && *((char*)(hva + i)) != 0; i++) {
68         PrintDebug("%c", *((char*)(hva + i)));
69     }
70     PrintDebug("\n");
71 }
72
73
74 static void print_syscall (uint8_t is64, struct guest_info * core) {
75
76     if (is64) {
77         PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name64(core->vm_regs.rax));
78     } else {
79         PrintDebug("Syscall #%ld: \"%s\"\n", (long)core->vm_regs.rax, get_linux_syscall_name32(core->vm_regs.rax));
80     }
81
82     print_arg(core, core->vm_regs.rbx, 1);
83     print_arg(core, core->vm_regs.rcx, 2);
84     print_arg(core, core->vm_regs.rdx, 3);
85 }
86
87
88 int v3_syscall_handler (struct guest_info * core, uint8_t vector, void * priv_data) {
89  
90     uint_t syscall_nr = (uint_t) core->vm_regs.rax;
91     int err = 0;
92
93     struct v3_syscall_hook * hook = core->sc_hook_map.syscall_hooks[syscall_nr];
94     if (hook == NULL) {
95 #ifdef CONFIG_SYSCALL_PASSTHROUGH
96         if (v3_hook_passthrough_syscall(core, syscall_nr) == -1) {
97             PrintDebug("Error hooking passthrough syscall\n");
98             return -1;
99         }
100         hook = core->sc_hook_map.syscall_hooks[syscall_nr];
101 #else
102         return v3_signal_swintr(core, vector);
103 #endif
104     }
105     
106     err = hook->handler(core, syscall_nr, hook->priv_data);
107     if (err == -1) {
108         PrintDebug("V3 Syscall Handler: Error in syscall hook\n");
109         return -1;
110     }
111
112     return 0;
113 }
114
115
116 static inline struct v3_syscall_hook * get_syscall_hook (struct guest_info * core, uint_t syscall_nr) {
117     return core->sc_hook_map.syscall_hooks[syscall_nr];
118
119
120
121 int v3_hook_syscall (struct guest_info * core,
122     uint_t syscall_nr,
123     int (*handler)(struct guest_info * core, uint_t syscall_nr, void * priv_data),
124     void * priv_data) 
125 {
126     struct v3_syscall_hook * hook = (struct v3_syscall_hook *)V3_Malloc(sizeof(struct v3_syscall_hook));
127
128     // TODO: ensure that software interrupts & SYSENTER are hooked
129     
130     if (hook == NULL) {
131         return -1;
132     }
133
134     if (get_syscall_hook(core, syscall_nr) != NULL) {
135         PrintError("System Call #%d already hooked\n", syscall_nr);
136         return -1;
137     }
138
139     hook->handler = handler;
140     hook->priv_data = priv_data;
141
142     core->sc_hook_map.syscall_hooks[syscall_nr] = hook;
143
144     return 0;
145 }
146
147
148 static int passthrough_syscall_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
149     print_syscall(0, core);
150     return 0;
151 }
152
153
154 int v3_hook_passthrough_syscall (struct guest_info * core, uint_t syscall_nr) {
155     
156     int rc = v3_hook_syscall(core, syscall_nr, passthrough_syscall_handler, NULL);
157
158     if (rc) {
159         PrintError("failed to hook syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
160         return -1;
161     } else {
162         PrintDebug("hooked syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
163         return 0;
164     }
165
166     /* shouldn't get here */
167     return 0;
168 }
169
170
171 int v3_sysopen_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
172
173     return 0;
174 }
175
176
177 int v3_sysmount_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
178
179     return 0;
180 }
181
182 int v3_sysexecve_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
183     addr_t hva, key;
184     struct exec_hook * hook;
185     int ret;
186     
187     ret = v3_gva_to_hva(core, get_addr_linear(core, (addr_t)core->vm_regs.rbx, &(core->segments.ds)), &hva);
188     if (ret == -1) {
189         PrintDebug("Error translating file path in sysexecve handler\n");
190         return -1;
191     }
192
193     key = v3_hash_buffer((uchar_t*)hva, strlen((uchar_t*)hva));
194     if ((hook = (struct exec_hook*)v3_htable_search(core->exec_hooks.bin_table, key)) != NULL) {
195        if (hook->handler(core, NULL) == -1) {
196             PrintDebug("Error handling execve hook\n");
197             return -1;
198         }
199     } 
200         
201     return 0;
202 }
203