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.


Cleanup and sanity-checking of use of strncpy/strcpy (Coverity static analysis)
[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         strncpy(top->bin_file, top_arg.bin_file,256);
78         top->bin_file[255] = 0;
79         top->is_exec_hooked = 1;
80         DEBUG("top->bin_file is %s\n", top->bin_file);
81     } 
82
83     DEBUG("Palacios: Allocating %lu B of kernel memory for ELF binary data...\n", top->elf_size);
84     top->elf_data = palacios_alloc(top->elf_size);
85     if (!(top->elf_data)) {
86         ERROR("Palacios Error: could not allocate space for binary image\n");
87         palacios_free(top);
88         return -EFAULT;
89     }
90     memset(top->elf_data, 0, top->elf_size);
91
92     INFO("Palacios: Copying ELF image into kernel module...\n");
93     if (copy_from_user(top->elf_data, (void __user *)top_arg.elf_data, top->elf_size)) {
94         ERROR("Palacios: Error loading elf data\n");
95         palacios_free(top->elf_data);
96         palacios_free(top);
97         return -EFAULT;
98     }
99
100     if (register_top(top) < 0) {
101         ERROR("Cannot register top half\n");
102         palacios_free(top->elf_data);
103         palacios_free(top);
104         return -1;
105     }
106
107     INFO("Palacios: setting up inject code...\n");
108     if (v3_insert_code_inject(guest->v3_ctx, top->elf_data, top->elf_size, 
109                          top->bin_file, top->is_dyn, top->is_exec_hooked, top->func_offset) < 0) {
110         ERROR("Palacios Error: error setting up inject code\n");
111         palacios_free(top->elf_data);
112         palacios_free(top);
113         return -1;
114     }
115
116     INFO("Palacios: injection registration complete\n");
117     return 0;
118 }
119
120
121 static int init_code_inject (void) {
122     return 0;
123 }
124
125
126 static int deinit_code_inject (void) {
127     return 0;
128 }
129
130
131 static int guest_init_code_inject (struct v3_guest * guest, void ** vm_data) {
132     add_guest_ctrl(guest, V3_VM_TOPHALF_INJECT, vm_tophalf_inject, NULL);
133     return 0;
134 }
135
136
137 static int guest_deinit_code_inject (struct v3_guest * guest, void * vm_data) {
138     free_inject_data();
139     remove_guest_ctrl(guest, V3_VM_TOPHALF_INJECT);
140     return 0;
141 }
142
143
144 static struct linux_ext code_inject_ext = {
145     .name = "CODE_INJECT",
146     .init = init_code_inject,
147     .deinit = deinit_code_inject,
148     .guest_init = guest_init_code_inject,
149     .guest_deinit = guest_deinit_code_inject 
150 };
151
152 register_extension(&code_inject_ext);