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.


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