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.


Linux implementation of Gears interfaces
[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 #include <linux/vmalloc.h>
11
12 #include <linux/module.h>
13
14 #include <gears/code_inject.h>
15
16 #include "palacios.h"
17 #include "vm.h"
18 #include "linux-exts.h"
19 #include "iface-code-inject.h"
20
21
22 /* eventually this should probably be a hash table,
23  * hashed on unique inject data 
24  */
25 static struct top_half_data *top_map[MAX_INJ] = {[0 ... MAX_INJ - 1] = 0};
26
27 static int register_top(struct top_half_data *top) {
28     int i;
29
30     for (i = 0; i < MAX_INJ; i++) {
31         if (!top_map[i]) {
32             top_map[i] = top;
33             return i;
34         }
35     }
36
37     return -1;
38 }
39
40
41 static void free_inject_data (void) {
42     int i;
43
44     for(i = 0; i < MAX_INJ; i++) {
45         if (top_map[i]) {
46             kfree(top_map[i]->elf_data);
47             kfree(top_map[i]);
48         }
49     }
50 }
51
52
53
54 static int vm_tophalf_inject (struct v3_guest * guest, unsigned int cmd, unsigned long arg, void * priv_data) {
55     struct top_half_data top_arg;
56     struct top_half_data * top;
57
58     top = kmalloc(sizeof(struct top_half_data), GFP_KERNEL);
59     if (IS_ERR(top)) {
60         printk("Palacios Error: could not allocate space for top half data\n");
61         return -EFAULT;
62     }
63     memset(top, 0, sizeof(struct top_half_data));
64     
65     printk("Palacios: Loading ELF data...\n");
66     if (copy_from_user(&top_arg, (void __user *)arg, sizeof(struct top_half_data))) {
67         printk("palacios: error copying ELF from userspace\n");
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         printk("top->bin_file is %s\n", top->bin_file);
80     } 
81
82     printk("Palacios: Allocating %lu B of kernel memory for ELF binary data...\n", top->elf_size);
83     top->elf_data = kmalloc(top->elf_size, GFP_KERNEL);
84     if (IS_ERR(top->elf_data)) {
85         printk("Palacios Error: could not allocate space for binary image\n");
86         return -EFAULT;
87     }
88     memset(top->elf_data, 0, top->elf_size);
89
90     printk("Palacios: Copying ELF image into kernel module...\n");
91     if (copy_from_user(top->elf_data, (void __user *)top_arg.elf_data, top->elf_size)) {
92         printk("Palacios: Error loading elf data\n");
93         return -EFAULT;
94     }
95
96     if (register_top(top) < 0) 
97         return -1;
98     
99     printk("Palacios: setting up inject code...\n");
100     if (v3_insert_code_inject(guest->v3_ctx, top->elf_data, top->elf_size, 
101                          top->bin_file, top->is_dyn, top->is_exec_hooked, top->func_offset) < 0) {
102         printk("Palacios Error: error setting up inject code\n");
103         return -1;
104     }
105
106     printk("Palacios: injection registration complete\n");
107     return 0;
108 }
109
110
111 static int init_code_inject (void) {
112     return 0;
113 }
114
115
116 static int deinit_code_inject (void) {
117     return 0;
118 }
119
120
121 static int guest_init_code_inject (struct v3_guest * guest, void ** vm_data) {
122     add_guest_ctrl(guest, V3_VM_TOPHALF_INJECT, vm_tophalf_inject, NULL);
123     return 0;
124 }
125
126
127 static int guest_deinit_code_inject (struct v3_guest * guest, void * vm_data) {
128     free_inject_data();
129     return 0;
130 }
131
132
133 static struct linux_ext code_inject_ext = {
134     .name = "CODE_INJECT",
135     .init = init_code_inject,
136     .deinit = deinit_code_inject,
137     .guest_init = guest_init_code_inject,
138     .guest_deinit = guest_deinit_code_inject 
139 };
140
141 register_extension(&code_inject_ext);