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_register_gm.c
1 /* 
2  * V3 Guarded Module registration utility
3  *
4  * This code allows a user to register a 
5  * guest driver module to be guarded upon
6  * injection into a guest.  *
7  * (c) Kyle C. Hale, 2012
8  */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <malloc.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19
20 #include "v3_ctrl.h"
21 #include "iface-guard-mods.h"
22 #include "cJSON.h"
23
24 #define SET_PRIV(x, i) ((x) |= 1U << (i))
25
26
27 /* Parse text to JSON, then render back to text, and print! */
28 static void 
29 populate_gm (char * filename, struct v3_guard_mod * m)
30 {
31     int fd, i, nents, nrets;
32     char * data;
33     struct stat stats;
34     cJSON *json, *tmp, *tmp2, *ep, *sub_ep;
35     
36         fd = open(filename, O_RDONLY);
37     if (fd < 0) {
38         fprintf(stderr, "Error opening file: %s\n", filename);
39         exit(0);
40     }
41
42     if (fstat(fd, &stats) == -1) {
43         fprintf(stderr, "Error stating file: %s\n", filename);
44         exit(0);
45     }
46
47         data = malloc(stats.st_size);
48     v3_read_file(fd, stats.st_size, data);
49     close(fd);
50
51         json = cJSON_Parse(data);
52
53         if (!json) {
54         fprintf(stderr, "Error before: [%s]\n",cJSON_GetErrorPtr());
55         goto out;
56     } 
57
58     m->name = cJSON_Print(cJSON_GetObjectItem(json, "module_name"));
59     m->content_hash = cJSON_Print(cJSON_GetObjectItem(json, "content_hash"));
60
61     tmp = cJSON_GetObjectItem(json, "size");
62     m->text_size = tmp->valueint;
63
64     tmp = cJSON_GetObjectItem(json, "hcall_offset");
65     m->hcall_offset = tmp->valueint;
66
67     /* extract all the valid entry points */
68     tmp = cJSON_GetObjectItem(json, "entry_points");
69     nents = cJSON_GetArraySize(tmp);
70
71     tmp2 = cJSON_GetObjectItem(json, "ret_points");
72     nrets = cJSON_GetArraySize(tmp2);
73
74
75     m->num_entries = nents + nrets;
76     printf("num entries: %d, nents: %d, nrets: %d\n", m->num_entries, nents, nrets);
77     m->entry_points = malloc(sizeof(struct v3_entry_point)*m->num_entries);
78
79     for (i = 0; i < nents; i++) {
80         ep = cJSON_GetArrayItem(tmp, i);
81         sub_ep = cJSON_GetArrayItem(ep, 0);
82
83         m->entry_points[i].name = cJSON_Print(sub_ep);
84         m->entry_points[i].is_ret = 0;
85
86         sub_ep = cJSON_GetArrayItem(ep, 1);
87         m->entry_points[i].offset = sub_ep->valueint;
88     }
89
90
91     for (i = nents; i < m->num_entries; i++) {
92         ep = cJSON_GetArrayItem(tmp2, i - nents);
93         sub_ep = cJSON_GetArrayItem(ep, 0);
94
95         m->entry_points[i].name = cJSON_Print(sub_ep);
96         m->entry_points[i].is_ret = 1;
97
98         sub_ep = cJSON_GetArrayItem(ep, 1);
99         m->entry_points[i].offset = sub_ep->valueint;
100     }
101
102     tmp = cJSON_GetObjectItem(json, "privileges");
103     m->num_privs = cJSON_GetArraySize(tmp);
104     m->priv_array = malloc(sizeof(char*)*m->num_privs);
105     if (!m->priv_array) {
106         fprintf(stderr, "Problem allocating privilege array in userspace\n");
107         goto out;
108     }
109
110     for (i = 0; i < m->num_privs; i++) {
111         ep = cJSON_GetArrayItem(tmp, i);
112         m->priv_array[i] = cJSON_Print(ep);
113     }
114
115 out:
116     cJSON_Delete(json);
117         free(data);
118 }
119
120
121 int main (int argc, char **argv) {
122     struct v3_guard_mod mod; 
123     char *dev_file, *json_file;
124     uint64_t ret;
125
126     if (argc < 2) {
127         v3_usage("<vm-device> <json>\n");
128         return -1;
129     }
130
131     dev_file  = argv[1];
132     json_file = argv[2];
133
134     populate_gm(json_file, &mod);
135
136     printf("Registering guarded module: %s, size: %d, offset: %d\n", mod.name, mod.text_size, mod.hcall_offset);
137
138     mod.id = 0;
139
140     ret = v3_vm_ioctl(dev_file, V3_VM_REGISTER_MOD, &mod);
141
142     if (ret < 0) {
143         fprintf(stderr, "Problem registering module\n");
144         return -1;
145     }
146     
147     if (!mod.id) {
148         fprintf(stderr, "Could not register guarded module\n");
149     } else {
150         printf("Module successfully registered [0x%llx]\n", mod.id);
151     }
152
153         return 0;
154 }