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_env_inject.c
1 /* 
2  * V3 Environment Variable Injection Utility
3  * This code allows a user to inject environment variables into a process
4  * marked by a specific binary name in a running guest.
5  *
6  * (c) Kyle C. Hale, 2012
7  */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <sys/ioctl.h>
16
17 #include "iface-env-inject.h"
18 #include "v3_ctrl.h"
19
20
21 int main (int argc, char **argv) {
22         char *vm_dev, *env_file, *bin_name;
23         int err, bytes_read, num_strings;
24     struct stat t_stat;
25     struct env_data env;
26     char * strings[MAX_NUM_STRINGS];
27     char tmp_str[MAX_STRING_LEN];
28     int i = 0;
29     FILE * t_fd;
30
31         if (argc < 4) {
32                 v3_usage("<vm device> <env-file> <inject-point-exe>\n\n"
33                          "\tenv-file : file containing a list of new-line separated env vars\n\n"
34                          "\tinject-point-exe : if this is an exec-hooked inject, use this executable name\n");
35                          
36                 return -1;
37         }
38
39         vm_dev = argv[1];
40     env_file = argv[2];
41     bin_name = argv[3];
42
43         t_fd = fopen(env_file, "r");
44         if (!t_fd) {
45                 fprintf(stderr, "Error opening environment variable file: %s\n", env_file);
46                 return -1;
47         }
48
49     /* copy in the vars line by line */
50     while (fgets(tmp_str, MAX_STRING_LEN, t_fd) != NULL) {
51         int len = strlen(tmp_str) - 1;
52         if (tmp_str[len] == '\n')
53             tmp_str[len] = 0;
54         strings[i] = (char*)malloc(MAX_STRING_LEN);
55         if (!strings[i]) {
56                 fprintf(stderr, "Error allocating space for variable\n");
57                 return -1;
58         }
59         strcpy(strings[i], tmp_str);
60         i++;
61     }
62
63     env.num_strings = i;
64     printf("Found %d environment variables to inject\n", i);
65
66     env.strings = (char**) strings;
67
68     if (!bin_name) {
69         fprintf(stderr, "Error: no binary hook provided\n");
70         return -1;
71     }
72
73     strncpy(env.bin_name, bin_name, MAX_STRING_LEN);
74
75     printf("Transferring control to Palacios\n");
76         err = v3_vm_ioctl(vm_dev, V3_VM_ENV_INJECT, &env);
77         if (err < 0) {
78                 fprintf(stderr, "Error providing env var data to palacios\n");
79                 return -1;
80         }
81     
82         close(t_fd);
83         return 0;
84 }