2 * V3 Guarded Module registration utility
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
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
21 #include "iface-guard-mods.h"
24 #define SET_PRIV(x, i) ((x) |= 1U << (i))
27 /* Parse text to JSON, then render back to text, and print! */
29 populate_gm (char * filename, struct v3_guard_mod * m)
31 int fd, i, nents, nrets;
34 cJSON *json, *tmp, *tmp2, *ep, *sub_ep;
36 fd = open(filename, O_RDONLY);
38 fprintf(stderr, "Error opening file: %s\n", filename);
42 if (fstat(fd, &stats) == -1) {
43 fprintf(stderr, "Error stating file: %s\n", filename);
47 data = malloc(stats.st_size);
48 v3_read_file(fd, stats.st_size, data);
51 json = cJSON_Parse(data);
54 fprintf(stderr, "Error before: [%s]\n",cJSON_GetErrorPtr());
58 m->name = cJSON_Print(cJSON_GetObjectItem(json, "module_name"));
59 m->content_hash = cJSON_Print(cJSON_GetObjectItem(json, "content_hash"));
61 tmp = cJSON_GetObjectItem(json, "size");
62 m->text_size = tmp->valueint;
64 tmp = cJSON_GetObjectItem(json, "hcall_offset");
65 m->hcall_offset = tmp->valueint;
67 /* extract all the valid entry points */
68 tmp = cJSON_GetObjectItem(json, "entry_points");
69 nents = cJSON_GetArraySize(tmp);
71 tmp2 = cJSON_GetObjectItem(json, "ret_points");
72 nrets = cJSON_GetArraySize(tmp2);
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);
79 for (i = 0; i < nents; i++) {
80 ep = cJSON_GetArrayItem(tmp, i);
81 sub_ep = cJSON_GetArrayItem(ep, 0);
83 m->entry_points[i].name = cJSON_Print(sub_ep);
84 m->entry_points[i].is_ret = 0;
86 sub_ep = cJSON_GetArrayItem(ep, 1);
87 m->entry_points[i].offset = sub_ep->valueint;
91 for (i = nents; i < m->num_entries; i++) {
92 ep = cJSON_GetArrayItem(tmp2, i - nents);
93 sub_ep = cJSON_GetArrayItem(ep, 0);
95 m->entry_points[i].name = cJSON_Print(sub_ep);
96 m->entry_points[i].is_ret = 1;
98 sub_ep = cJSON_GetArrayItem(ep, 1);
99 m->entry_points[i].offset = sub_ep->valueint;
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");
110 for (i = 0; i < m->num_privs; i++) {
111 ep = cJSON_GetArrayItem(tmp, i);
112 m->priv_array[i] = cJSON_Print(ep);
121 int main (int argc, char **argv) {
122 struct v3_guard_mod mod;
123 char *dev_file, *json_file;
127 v3_usage("<vm-device> <json>\n");
134 populate_gm(json_file, &mod);
136 printf("Registering guarded module: %s, size: %d, offset: %d\n", mod.name, mod.text_size, mod.hcall_offset);
140 ret = v3_vm_ioctl(dev_file, V3_VM_REGISTER_MOD, &mod);
143 fprintf(stderr, "Problem registering module\n");
148 fprintf(stderr, "Could not register guarded module\n");
150 printf("Module successfully registered [0x%llx]\n", mod.id);