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.


Avoid strict-aliasing related issues when compiling with optimization
[palacios.git] / linux_usr / v3_top_inject.c
1 /* 
2  * V3 Top Half Injection Utility
3  * This code allows a user to inject a "top half" of code into a running guest. 
4  * The "bottom half" (a hypercall handler) is inserted using another utility.
5  *
6  * (c) Kyle C. Hale, 2011
7  */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <malloc.h>
12 #include <string.h>
13 #include <elf.h>
14 #include <fcntl.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18
19 #include "iface-code-inject.h"
20 #include "v3_ctrl.h"
21
22 #define __ELF_INJECT_CLASS 32
23
24 #define ElfW(type)      _ElfW (Elf, __ELF_INJECT_CLASS, type)
25 #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
26 #define _ElfW_1(e,w,t)  e##w##t
27
28
29 /* look for PT_DYNAMIC to see if it's dynamically linked */
30 static int is_dynamic (ElfW(Ehdr) * ehdr) {
31     int i;
32     ElfW(Phdr) * phdr = (ElfW(Phdr)*)((char*)ehdr + ehdr->e_phoff);
33     ElfW(Phdr) * phdr_cursor;
34
35     phdr_cursor = phdr;
36
37     for (i = 0; i < ehdr->e_phnum; i++, phdr_cursor++) {
38         if (phdr_cursor->p_type == PT_DYNAMIC)
39             return 1;
40     }
41
42     return 0;
43 }
44
45
46 int main (int argc, char **argv) {
47         char *vm_dev = NULL;
48         char *top_half = NULL;
49     char *bin_file = NULL;
50         int t_fd, err, bytes_read, entry;
51     struct stat t_stat;
52     struct top_half_data elf;
53     ElfW(Ehdr) * elf_hdr;
54
55         if (argc < 4 || argc > 5) {
56                 // TODO: add a better explanation here
57                 v3_usage("/dev/v3-vm<N> <inject-code> <code-entry-offset> [inject-point-exe]\n\n"
58                           "\tinject-code       : the binary file to be injected\n\n"
59                           "\tcode-entry-offset : offset in the binary to .text\n\n"
60                           "\tinject-point-exe  : if exec-hooked injection is used, use this exec name\n");
61                 return -1;
62         }
63
64         vm_dev = argv[1];
65     top_half = argv[2];
66     entry = strtol(argv[3], NULL, 0);
67     if (argv[4]) 
68         bin_file = argv[4];
69     
70
71         t_fd = open(top_half, O_RDONLY);
72         if (t_fd == -1) {
73                 fprintf(stderr, "Error opening top half .o file: %s\n", top_half);
74                 return -1;
75         }
76
77     if (fstat(t_fd, &t_stat) < 0) {
78         fprintf(stderr, "Error: could not stat ELF binary file %s\n", top_half);
79         return -1;
80     }
81
82     memset(&elf, 0, sizeof(struct top_half_data));
83
84     elf.elf_size = t_stat.st_size;
85
86     if (bin_file) {
87         strcpy(elf.bin_file, bin_file);
88         elf.is_exec_hooked = 1;
89     } else {
90         elf.is_exec_hooked = 0;
91     }
92
93         
94     /* read in the ELF */
95     elf.elf_data = malloc(elf.elf_size);
96     if (!elf.elf_data) {
97         fprintf(stderr, "Error allocating memory for ELF data\n");
98         return -1;
99     }
100
101     printf("Loading ELF binary...\n");
102     if ((bytes_read = read(t_fd, elf.elf_data, elf.elf_size)) < 0) {
103         fprintf(stderr, "Error loading ELF binary %s\n", top_half);
104         return -1;
105     }
106
107     printf("Loaded. %d Bytes of data read\n", bytes_read);
108     elf_hdr = (ElfW(Ehdr)*)elf.elf_data;
109
110     /* set the entry point */
111     elf.func_offset = entry;
112
113     /* check the ELF magic nr to make sure this is a valid ELF */
114     if (elf_hdr->e_ident[EI_MAG0] != 0x7f ||
115         elf_hdr->e_ident[EI_MAG1] != 'E'  ||
116         elf_hdr->e_ident[EI_MAG2] != 'L'  ||
117         elf_hdr->e_ident[EI_MAG3] != 'F') {
118             
119         fprintf(stderr, "Error: Invalid ELF binary %s\n", top_half);
120         return -1;
121     }
122
123     /* make sure the ELF is an actual executable file */
124     if (elf_hdr->e_type != ET_EXEC) {
125         fprintf(stderr, "Error: ELF must be an executable file %s\n", top_half);
126         return -1;
127     }
128
129     /* is it a dynamically linked executable? */
130     elf.is_dyn = is_dynamic(elf_hdr);
131
132
133     printf("Transferring control to Palacios\n");
134         err = v3_vm_ioctl(vm_dev, V3_VM_TOPHALF_INJECT, &elf);
135         if (err < 0) {
136                 fprintf(stderr, "Error providing top half to palacios: %s\n", top_half);
137                 return -1;
138         }
139     
140     free(elf.elf_data);
141         close(t_fd);
142
143         return 0;
144 }