manifest.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 strpartcmp_static(uri, "file:");
  29. }
  30. static inline const char * file_uri_to_path (const char * uri, int len)
  31. {
  32. int prefix_len = static_strlen("file:");
  33. char * path;
  34. if (len == prefix_len) {
  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 - prefix_len + 1);
  43. if (!path)
  44. return NULL;
  45. memcpy(path, uri + prefix_len, len - prefix_len + 1);
  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. char *keys;
  83. int nkeys;
  84. keys = __alloca(get_config_entries_size(config, "fs.mount"));
  85. if ((nkeys = get_config_entries(config, "fs.mount", keys)) < 0)
  86. nkeys = 0;
  87. *paths = malloc(sizeof(const char *) * (1 + nkeys));
  88. if (!(*paths))
  89. return -ENOMEM;
  90. (*paths)[0] = ".";
  91. int npaths = 1;
  92. if (!nkeys)
  93. goto out;
  94. char key[CONFIG_MAX], * k = keys, * n;
  95. char * tmp;
  96. tmp = strcpy_static(key, "fs.mount.", CONFIG_MAX);
  97. for (int i = 0 ; i < nkeys ; i++) {
  98. for (n = k ; *n ; n++);
  99. int len = n - k;
  100. memcpy(tmp, k, len);
  101. strcpy_static(tmp + len, ".uri", (key + CONFIG_MAX) - (tmp + len));
  102. const char * path = __get_path(config, key);
  103. if (path)
  104. (*paths)[npaths++] = path;
  105. k = n + 1;
  106. }
  107. out:
  108. return npaths;
  109. }
  110. int get_net_rules (struct config_store * config,
  111. struct graphene_net_rule ** net_rules,
  112. int * nbind_rules)
  113. {
  114. char *binds, *peers;
  115. int nbinds, npeers;
  116. int nrules = 0;
  117. binds = __alloca(get_config_entries_size(config, "net.allow_bind"));
  118. if ((nbinds = get_config_entries(config, "net.allow_bind", binds)) < 0)
  119. return 0;
  120. peers = __alloca(get_config_entries_size(config, "net.allow_peer"));
  121. if ((npeers = get_config_entries(config, "net.allow_peer", peers)) < 0)
  122. return 0;
  123. struct graphene_net_rule * rules =
  124. malloc(sizeof(struct graphene_net_rule) * (nbinds + npeers));
  125. if (!rules)
  126. return -ENOMEM;
  127. for (int t = 0 ; t < 2 ; t ++) {
  128. char key[CONFIG_MAX], * k, * n;
  129. char * tmp;
  130. int nadded;
  131. if (t == 0) {
  132. if (!nbinds)
  133. continue;
  134. k = binds;
  135. nadded = nbinds;
  136. tmp = strcpy_static(key, "net.allow_bind.", CONFIG_MAX);
  137. } else {
  138. if (!npeers)
  139. continue;
  140. k = peers;
  141. nadded = npeers;
  142. tmp = strcpy_static(key, "net.allow_peer.", CONFIG_MAX);
  143. }
  144. for (int i = 0 ; i < nadded ; i++) {
  145. struct graphene_net_rule * r = &rules[nrules];
  146. char cfgbuf[CONFIG_MAX];
  147. for (n = k ; *n ; n++);
  148. int len = n - k;
  149. memcpy(tmp, k, len + 1);
  150. tmp[len] = 0;
  151. int cfglen = get_config(config, key, cfgbuf, CONFIG_MAX);
  152. if (cfglen <= 0)
  153. goto next;
  154. char * c = cfgbuf, * end = cfgbuf + cfglen;
  155. char * addr = c, * num;
  156. int addrlen;
  157. r->family = AF_INET;
  158. if (*c == '[') {
  159. r->family = AF_INET6;
  160. addr++;
  161. for ( ; c < end && *c != ']' ; c++);
  162. if (c == end)
  163. goto next;
  164. addrlen = c - addr;
  165. c++;
  166. if (c == end || *c != ':')
  167. goto next;
  168. } else {
  169. for ( ; c < end && *c != ':' ; c++);
  170. if (c == end)
  171. goto next;
  172. addrlen = c - addr;
  173. }
  174. c++;
  175. if (c == end)
  176. goto next;
  177. num = c;
  178. for ( ; c < end && *c >= '0' && *c <= '9' ; c++);
  179. if (c == num)
  180. goto next;
  181. r->addr.port_end = r->addr.port_begin = atoi(num);
  182. if (c < end && *c == '-') {
  183. num = (++c);
  184. for ( ; c < end && *c >= '0' && *c <= '9' ; c++);
  185. if (c == num)
  186. goto next;
  187. r->addr.port_end = atoi(num);
  188. }
  189. if (r->family == AF_INET) {
  190. if (!inet_pton4(addr, addrlen, &r->addr.addr))
  191. goto next;
  192. } else {
  193. if (!inet_pton6(addr, addrlen, &r->addr.addr))
  194. goto next;
  195. }
  196. nrules++;
  197. next:
  198. k = n + 1;
  199. }
  200. if (t == 0)
  201. *nbind_rules = nrules;
  202. }
  203. *net_rules = rules;
  204. return nrules;
  205. }
  206. int ioctl_set_graphene (struct config_store * config, int ndefault,
  207. const struct graphene_user_policy * default_policies)
  208. {
  209. int ro = GRAPHENE_FS_READ, rw = ro | GRAPHENE_FS_WRITE;
  210. int ret = 0;
  211. const char ** preload_paths = NULL;
  212. const char ** fs_paths = NULL;
  213. struct graphene_net_rule * net_rules = NULL;
  214. int npreload = 0, nfs = 0, net = 0, bind_rules = 0;
  215. int fd = -1;
  216. int n = 0;
  217. npreload = get_preload_paths(config, &preload_paths);
  218. if (npreload < 0) {
  219. ret = npreload;
  220. goto out;
  221. }
  222. nfs = get_fs_paths(config, &fs_paths);
  223. if (nfs < 0) {
  224. ret = nfs;
  225. goto out;
  226. }
  227. net = get_net_rules(config, &net_rules, &bind_rules);
  228. if (net < 0) {
  229. ret = net;
  230. goto out;
  231. }
  232. struct graphene_policies * p =
  233. __alloca(sizeof(struct graphene_policies) +
  234. sizeof(struct graphene_user_policy) *
  235. (ndefault + npreload + nfs + net));
  236. memcpy(&p->policies[n], default_policies,
  237. sizeof(struct graphene_user_policy) * ndefault);
  238. n += ndefault;
  239. for (int i = 0 ; i < npreload ; i++) {
  240. p->policies[n].type = GRAPHENE_FS_PATH | ro;
  241. p->policies[n].value = preload_paths[i];
  242. n++;
  243. }
  244. for (int i = 0 ; i < nfs ; i++) {
  245. p->policies[n].type = GRAPHENE_FS_PATH | GRAPHENE_FS_RECURSIVE | rw;
  246. p->policies[n].value = fs_paths[i];
  247. n++;
  248. }
  249. for (int i = 0 ; i < net ; i++) {
  250. p->policies[n].type = GRAPHENE_NET_RULE;
  251. if (i < bind_rules)
  252. p->policies[n].type |= GRAPHENE_NET_BIND;
  253. p->policies[n].value = &net_rules[i];
  254. n++;
  255. }
  256. p->npolicies = n;
  257. fd = INLINE_SYSCALL(open, 3, GRAPHENE_FILE, O_RDONLY, 0);
  258. if (IS_ERR(fd)) {
  259. ret = -ERRNO(fd);
  260. goto out;
  261. }
  262. ret = INLINE_SYSCALL(ioctl, 3, fd, GRAPHENE_SET_TASK, p);
  263. ret = IS_ERR(ret) ? -ERRNO(ret) : 0;
  264. out:
  265. if (fd != -1)
  266. INLINE_SYSCALL(close, 1, fd);
  267. if (preload_paths) {
  268. for (int i = 0 ; i < npreload ; i++)
  269. free((void *) preload_paths[i]);
  270. free(preload_paths);
  271. }
  272. if (fs_paths) {
  273. for (int i = 0 ; i < nfs ; i++)
  274. free((void *) fs_paths[i]);
  275. free(fs_paths);
  276. }
  277. if (net_rules)
  278. free(net_rules);
  279. return ret;
  280. }