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 kfree(top_map[i]->elf_data);
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 = kmalloc(sizeof(struct top_half_data), GFP_KERNEL);
60 printk("Palacios Error: could not allocate space for top half data\n");
63 memset(top, 0, sizeof(struct top_half_data));
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");
71 top->elf_size = top_arg.elf_size;
72 top->func_offset = top_arg.func_offset;
73 top->is_dyn = top_arg.is_dyn;
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);
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");
88 memset(top->elf_data, 0, top->elf_size);
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");
96 if (register_top(top) < 0)
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");
106 printk("Palacios: injection registration complete\n");
111 static int init_code_inject (void) {
116 static int deinit_code_inject (void) {
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);
127 static int guest_deinit_code_inject (struct v3_guest * guest, void * vm_data) {
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
141 register_extension(&code_inject_ext);