2 * Linux interface for guest-context code injection
4 * (c) Kyle C. Hale 2011
9 #include <linux/uaccess.h>
10 #include <linux/vmalloc.h>
12 #include <linux/module.h>
14 #include <gears/code_inject.h>
18 #include "linux-exts.h"
19 #include "iface-code-inject.h"
22 /* eventually this should probably be a hash table,
23 * hashed on unique inject data
25 static struct top_half_data *top_map[MAX_INJ] = {[0 ... MAX_INJ - 1] = 0};
27 static int register_top(struct top_half_data *top) {
30 for (i = 0; i < MAX_INJ; i++) {
41 static void free_inject_data (void) {
44 for(i = 0; i < MAX_INJ; i++) {
46 palacios_free(top_map[i]->elf_data);
47 palacios_free(top_map[i]);
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;
58 top = palacios_alloc(sizeof(struct top_half_data));
60 ERROR("Palacios Error: could not allocate space for top half data\n");
63 memset(top, 0, sizeof(struct top_half_data));
65 INFO("Palacios: Loading ELF data...\n");
66 if (copy_from_user(&top_arg, (void __user *)arg, sizeof(struct top_half_data))) {
67 ERROR("palacios: error copying ELF from userspace\n");
72 top->elf_size = top_arg.elf_size;
73 top->func_offset = top_arg.func_offset;
74 top->is_dyn = top_arg.is_dyn;
76 /* we have a binary name */
77 if (top_arg.is_exec_hooked) {
78 strcpy(top->bin_file, top_arg.bin_file);
79 top->is_exec_hooked = 1;
80 DEBUG("top->bin_file is %s\n", top->bin_file);
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 (IS_ERR(top->elf_data)) {
86 ERROR("Palacios Error: could not allocate space for binary image\n");
90 memset(top->elf_data, 0, top->elf_size);
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);
100 if (register_top(top) < 0) {
101 ERROR("Cannot register top half\n");
102 palacios_free(top->elf_data);
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);
116 INFO("Palacios: injection registration complete\n");
121 static int init_code_inject (void) {
126 static int deinit_code_inject (void) {
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);
137 static int guest_deinit_code_inject (struct v3_guest * guest, void * vm_data) {
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
151 register_extension(&code_inject_ext);