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 ability to hook execution of arbitrary binary files. Added ability to add arbit...
[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     if (hook == NULL) {
129         return -1;
130     }
131
132     if (get_syscall_hook(core, syscall_nr) != NULL) {
133         PrintError("System Call #%d already hooked\n", syscall_nr);
134         return -1;
135     }
136
137     hook->handler = handler;
138     hook->priv_data = priv_data;
139
140     core->sc_hook_map.syscall_hooks[syscall_nr] = hook;
141
142     return 0;
143 }
144
145
146 static int passthrough_syscall_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
147     print_syscall(0, core);
148     return 0;
149 }
150
151
152 int v3_hook_passthrough_syscall (struct guest_info * core, uint_t syscall_nr) {
153     
154     int rc = v3_hook_syscall(core, syscall_nr, passthrough_syscall_handler, NULL);
155
156     if (rc) {
157         PrintError("failed to hook syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
158         return -1;
159     } else {
160         PrintDebug("hooked syscall 0x%x for passthrough (guest=0x%p)\n", syscall_nr, (void *)core);
161         return 0;
162     }
163
164     /* shouldn't get here */
165     return 0;
166 }
167
168
169 int v3_sysopen_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
170
171     return 0;
172 }
173
174
175 int v3_sysmount_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
176
177     return 0;
178 }
179
180 int v3_sysexecve_handler (struct guest_info * core, uint_t syscall_nr, void * priv_data) {
181     addr_t hva, key;
182     struct exec_hook * hook;
183     int ret;
184     
185     ret = v3_gva_to_hva(core, get_addr_linear(core, (addr_t)core->vm_regs.rbx, &(core->segments.ds)), &hva);
186     if (ret == -1) {
187         PrintDebug("Error translating file path in sysexecve handler\n");
188         return -1;
189     }
190
191     key = v3_hash_buffer((uchar_t*)hva, strlen((uchar_t*)hva));
192     if ((hook = (struct exec_hook*)v3_htable_search(core->exec_hooks.bin_table, key)) != NULL) {
193        if (hook->handler(core, NULL) == -1) {
194             PrintDebug("Error handling execve hook\n");
195             return -1;
196         }
197     } 
198         
199     return 0;
200 }
201