manifest.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #define _GNU_SOURCE 1
  4. #ifndef __GNUC__
  5. #define __GNUC__ 1
  6. #endif
  7. #ifdef IN_PAL
  8. # include "pal_defs.h"
  9. # include "pal_linux_defs.h"
  10. # include "pal.h"
  11. # include "pal_internal.h"
  12. # include "pal_linux.h"
  13. # include "pal_debug.h"
  14. # include "pal_error.h"
  15. #else
  16. # include "internal.h"
  17. #endif
  18. #include "graphene.h"
  19. #include "pal_security.h"
  20. #include "api.h"
  21. #include <sys/socket.h>
  22. #include <linux/unistd.h>
  23. #include <asm/fcntl.h>
  24. #include <asm/mman.h>
  25. #include <asm/errno.h>
  26. static inline int is_file_uri (const char * uri)
  27. {
  28. return uri[0] == 'f' && uri[1] == 'i' && uri[2] == 'l' && uri[3] == 'e' &&
  29. uri[4] == ':';
  30. }
  31. static inline const char * file_uri_to_path (const char * uri, int len)
  32. {
  33. char * path;
  34. if (len == 5) {
  35. path = malloc(2);
  36. if (!path)
  37. return NULL;
  38. path[0] = '.';
  39. path[1] = 0;
  40. return path;
  41. }
  42. path = malloc(len - 4);
  43. if (!path)
  44. return NULL;
  45. memcpy(path, uri + 5, len - 4);
  46. return path;
  47. }
  48. static const char * __get_path (struct config_store * config, const char * key)
  49. {
  50. char uri[CONFIG_MAX];
  51. if (get_config(config, key, uri, CONFIG_MAX) <= 0 ||
  52. !is_file_uri(uri))
  53. return NULL;
  54. return file_uri_to_path(uri, strlen(uri));
  55. }
  56. #define PRELOAD_MAX 16
  57. int get_preload_paths (struct config_store * config, const char *** paths)
  58. {
  59. char cfgbuf[CONFIG_MAX];
  60. if (get_config(config, "loader.preload", cfgbuf, CONFIG_MAX) <= 0)
  61. return 0;
  62. const char * p = cfgbuf, * n;
  63. const char * preload_paths[PRELOAD_MAX];
  64. int npreload = 0;
  65. while (*p && npreload < PRELOAD_MAX) {
  66. for (n = p ; *n && *n != ',' ; n++);
  67. if (!is_file_uri(p))
  68. goto next;
  69. if (!(preload_paths[npreload++] = file_uri_to_path(p, n - p)))
  70. return -ENOMEM;
  71. next:
  72. p = *n ? n + 1 : n;
  73. }
  74. *paths = malloc(sizeof(const char *) * npreload);
  75. if (!(*paths))
  76. return -ENOMEM;
  77. memcpy((*paths), preload_paths, sizeof(const char *) * npreload);
  78. return npreload;
  79. }
  80. int get_fs_paths (struct config_store * config, const char *** paths)
  81. {
  82. const char * root_path = __get_path(config, "fs.mount.root.uri");
  83. if (!root_path)
  84. return 0;
  85. char keys[CONFIG_MAX];
  86. int nkeys;
  87. if ((nkeys = get_config_entries(config, "fs.mount.other", keys,
  88. CONFIG_MAX)) < 0)
  89. nkeys = 0;
  90. *paths = malloc(sizeof(const char *) * (1 + nkeys));
  91. if (!(*paths))
  92. return -ENOMEM;
  93. (*paths)[0] = root_path;
  94. int npaths = 1;
  95. if (!nkeys)
  96. goto out;
  97. char key[CONFIG_MAX], * k = keys, * n;
  98. memcpy(key, "fs.mount.other.", 15);
  99. for (int i = 0 ; i < nkeys ; i++) {
  100. for (n = k ; *n ; n++);
  101. int len = n - k;
  102. memcpy(key + 15, k, len);
  103. memcpy(key + 15 + len, ".uri", 5);
  104. const char * path = __get_path(config, key);
  105. if (path)
  106. (*paths)[npaths++] = path;
  107. k = n + 1;
  108. }
  109. out:
  110. return npaths;
  111. }
  112. int get_net_rules (struct config_store * config,
  113. struct graphene_net_rule ** net_rules,
  114. int * nbind_rules)
  115. {
  116. char binds[CONFIG_MAX], peers[CONFIG_MAX];
  117. int nbinds, npeers;
  118. int nrules = 0;
  119. if ((nbinds = get_config_entries(config, "net.allow_bind", binds,
  120. CONFIG_MAX)) < 0)
  121. return 0;
  122. if ((npeers = get_config_entries(config, "net.allow_peer", peers,
  123. CONFIG_MAX)) < 0)
  124. return 0;
  125. struct graphene_net_rule * rules =
  126. malloc(sizeof(struct graphene_net_rule) * (nbinds + npeers));
  127. if (!rules)
  128. return -ENOMEM;
  129. for (int t = 0 ; t < 2 ; t ++) {
  130. char key[CONFIG_MAX], * k, * n;
  131. int nadded;
  132. if (t == 0) {
  133. if (!nbinds)
  134. continue;
  135. k = binds;
  136. nadded = nbinds;
  137. memcpy(key, "net.allow_bind.", 15);
  138. } else {
  139. if (!npeers)
  140. continue;
  141. k = peers;
  142. nadded = npeers;
  143. memcpy(key, "net.allow_peer.", 15);
  144. }
  145. for (int i = 0 ; i < nadded ; i++) {
  146. struct graphene_net_rule * r = &rules[nrules];
  147. char cfgbuf[CONFIG_MAX];
  148. for (n = k ; *n ; n++);
  149. int len = n - k;
  150. memcpy(key + 15, k, len + 1);
  151. key[15 + len] = 0;
  152. int cfglen = get_config(config, key, cfgbuf, CONFIG_MAX);
  153. if (cfglen <= 0)
  154. goto next;
  155. char * c = cfgbuf, * end = cfgbuf + cfglen;
  156. char * addr = c, * num;
  157. int addrlen;
  158. r->family = AF_INET;
  159. if (*c == '[') {
  160. r->family = AF_INET6;
  161. addr++;
  162. for ( ; c < end && *c != ']' ; c++);
  163. if (c == end)
  164. goto next;
  165. addrlen = c - addr;
  166. c++;
  167. if (c == end || *c != ':')
  168. goto next;
  169. } else {
  170. for ( ; c < end && *c != ':' ; c++);
  171. if (c == end)
  172. goto next;
  173. addrlen = c - addr;
  174. }
  175. c++;
  176. if (c == end)
  177. goto next;
  178. num = c;
  179. for ( ; c < end && *c >= '0' && *c <= '9' ; c++);
  180. if (c == num)
  181. goto next;
  182. r->addr.port_end = r->addr.port_begin = atoi(num);
  183. if (c < end && *c == '-') {
  184. num = (++c);
  185. for ( ; c < end && *c >= '0' && *c <= '9' ; c++);
  186. if (c == num)
  187. goto next;
  188. r->addr.port_end = atoi(num);
  189. }
  190. if (r->family == AF_INET) {
  191. if (!inet_pton4(addr, addrlen, &r->addr.addr))
  192. goto next;
  193. } else {
  194. if (!inet_pton6(addr, addrlen, &r->addr.addr))
  195. goto next;
  196. }
  197. nrules++;
  198. next:
  199. k = n + 1;
  200. }
  201. if (t == 0)
  202. *nbind_rules = nrules;
  203. }
  204. *net_rules = rules;
  205. return nrules;
  206. }
  207. int ioctl_set_graphene (struct config_store * config, int ndefault,
  208. const struct graphene_user_policy * default_policies)
  209. {
  210. int ro = GRAPHENE_FS_READ, rw = ro | GRAPHENE_FS_WRITE;
  211. int ret = 0;
  212. const char ** preload_paths = NULL;
  213. const char ** fs_paths = NULL;
  214. struct graphene_net_rule * net_rules = NULL;
  215. int npreload = 0, nfs = 0, net = 0, bind_rules = 0;
  216. int fd = -1;
  217. int n = 0;
  218. npreload = get_preload_paths(config, &preload_paths);
  219. if (npreload < 0) {
  220. ret = npreload;
  221. goto out;
  222. }
  223. nfs = get_fs_paths(config, &fs_paths);
  224. if (nfs < 0) {
  225. ret = nfs;
  226. goto out;
  227. }
  228. net = get_net_rules(config, &net_rules, &bind_rules);
  229. if (net < 0) {
  230. ret = net;
  231. goto out;
  232. }
  233. struct graphene_policies * p =
  234. __alloca(sizeof(struct graphene_policies) +
  235. sizeof(struct graphene_user_policy) *
  236. (ndefault + npreload + nfs + net));
  237. memcpy(&p->policies[n], default_policies,
  238. sizeof(struct graphene_user_policy) * ndefault);
  239. n += ndefault;
  240. for (int i = 0 ; i < npreload ; i++) {
  241. p->policies[n].type = GRAPHENE_FS_PATH | ro;
  242. p->policies[n].value = preload_paths[i];
  243. n++;
  244. }
  245. for (int i = 0 ; i < nfs ; i++) {
  246. p->policies[n].type = GRAPHENE_FS_PATH | GRAPHENE_FS_RECURSIVE | rw;
  247. p->policies[n].value = fs_paths[i];
  248. n++;
  249. }
  250. for (int i = 0 ; i < net ; i++) {
  251. p->policies[n].type = GRAPHENE_NET_RULE;
  252. if (i < bind_rules)
  253. p->policies[n].type |= GRAPHENE_NET_BIND;
  254. p->policies[n].value = &net_rules[i];
  255. n++;
  256. }
  257. p->npolicies = n;
  258. fd = INLINE_SYSCALL(open, 3, GRAPHENE_FILE, O_RDONLY, 0);
  259. if (IS_ERR(fd)) {
  260. ret = -ERRNO(fd);
  261. goto out;
  262. }
  263. ret = INLINE_SYSCALL(ioctl, 3, fd, GRAPHENE_SET_TASK, p);
  264. ret = IS_ERR(ret) ? -ERRNO(ret) : 0;
  265. out:
  266. if (fd != -1)
  267. INLINE_SYSCALL(close, 1, fd);
  268. if (preload_paths) {
  269. for (int i = 0 ; i < npreload ; i++)
  270. free((void *) preload_paths[i]);
  271. free(preload_paths);
  272. }
  273. if (fs_paths) {
  274. for (int i = 0 ; i < nfs ; i++)
  275. free((void *) fs_paths[i]);
  276. free(fs_paths);
  277. }
  278. if (net_rules)
  279. free(net_rules);
  280. return ret;
  281. }