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.


1262d772d1db80139dcdb43305efc0a708d2ec95
[palacios.git] / linux_module / iface-code-inject.c
1 /* 
2  * Linux interface for guest-context code injection
3  *
4  * (c) Kyle C. Hale 2011
5  *
6  */
7
8 #include <linux/elf.h>
9 #include <linux/uaccess.h>
10
11 #include <linux/module.h>
12
13 #include <gears/code_inject.h>
14
15 #include "palacios.h"
16 #include "vm.h"
17 #include "linux-exts.h"
18 #include "iface-code-inject.h"
19
20
21 /* eventually this should probably be a hash table,
22  * hashed on unique inject data 
23  */
24 static struct top_half_data *top_map[MAX_INJ] = {[0 ... MAX_INJ - 1] = 0};
25
26 static int register_top(struct top_half_data *top) {
27     int i;
28
29     for (i = 0; i < MAX_INJ; i++) {
30         if (!top_map[i]) {
31             top_map[i] = top;
32             return i;
33         }
34     }
35
36     return -1;
37 }
38
39
40 static void free_inject_data (void) {
41     int i;
42
43     for(i = 0; i < MAX_INJ; i++) {
44         if (top_map[i]) {
45             palacios_free(top_map[i]->elf_data);
46             palacios_free(top_map[i]);
47         }
48     }
49 }
50
51
52
53 static int vm_tophalf_inject (struct v3_guest * guest, unsigned int cmd, unsigned long arg, void * priv_data) {
54     struct top_half_data top_arg;
55     struct top_half_data * top;
56
57     top = palacios_alloc(sizeof(struct top_half_data));
58     if (!top) {
59         ERROR("Palacios Error: could not allocate space for top half data\n");
60         return -EFAULT;
61     }
62     memset(top, 0, sizeof(struct top_half_data));
63     
64     INFO("Palacios: Loading ELF data...\n");
65     if (copy_from_user(&top_arg, (void __user *)arg, sizeof(struct top_half_data))) {
66         ERROR("palacios: error copying ELF from userspace\n");
67         palacios_free(top);
68         return -EFAULT;
69     }
70
71     top->elf_size = top_arg.elf_size;
72     top->func_offset = top_arg.func_offset;
73     top->is_dyn = top_arg.is_dyn;
74
75     /* we have a binary name */
76     if (top_arg.is_exec_hooked) {
77         strcpy(top->bin_file, top_arg.bin_file);
78         top->is_exec_hooked = 1;
79         DEBUG("top->bin_file is %s\n", top->bin_file);
80     } 
81
82     DEBUG("Palacios: Allocating %lu B of kernel memory for ELF binary data...\n", top->elf_size);
83     top->elf_data = palacios_alloc(top->elf_size);
84     if (!(top->elf_data)) {
85         ERROR("Palacios Error: could not allocate space for binary image\n");
86         palacios_free(top);
87         return -EFAULT;
88     }
89     memset(top->elf_data, 0, top->elf_size);
90
91     INFO("Palacios: Copying ELF image into kernel module...\n");
92     if (copy_from_user(top->elf_data, (void __user *)top_arg.elf_data, top->elf_size)) {
93         ERROR("Palacios: Error loading elf data\n");
94         palacios_free(top->elf_data);
95         palacios_free(top);
96         return -EFAULT;
97     }
98
99     if (register_top(top) < 0) {
100         ERROR("Cannot register top half\n");
101         palacios_free(top->elf_data);
102         palacios_free(top);
103         return -1;
104     }
105
106     INFO("Palacios: setting up inject code...\n");
107     if (v3_insert_code_inject(guest->v3_ctx, top->elf_data, top->elf_size, 
108                          top->bin_file, top->is_dyn, top->is_exec_hooked, top->func_offset) < 0) {
109         ERROR("Palacios Error: error setting up inject code\n");
110         palacios_free(top->elf_data);
111         palacios_free(top);
112         return -1;
113     }
114
115     INFO("Palacios: injection registration complete\n");
116     return 0;
117 }
118
119
120 static int init_code_inject (void) {
121     return 0;
122 }
123
124
125 static int deinit_code_inject (void) {
126     return 0;
127 }
128
129
130 static int guest_init_code_inject (struct v3_guest * guest, void ** vm_data) {
131     add_guest_ctrl(guest, V3_VM_TOPHALF_INJECT, vm_tophalf_inject, NULL);
132     return 0;
133 }
134
135
136 static int guest_deinit_code_inject (struct v3_guest * guest, void * vm_data) {
137     free_inject_data();
138     remove_guest_ctrl(guest, V3_VM_TOPHALF_INJECT);
139     return 0;
140 }
141
142
143 static struct linux_ext code_inject_ext = {
144     .name = "CODE_INJECT",
145     .init = init_code_inject,
146     .deinit = deinit_code_inject,
147     .guest_init = guest_init_code_inject,
148     .guest_deinit = guest_deinit_code_inject 
149 };
150
151 register_extension(&code_inject_ext);