shim_sandbox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_sandbox.c
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <shim_fs.h>
  21. #include <shim_checkpoint.h>
  22. #include <shim_ipc.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <errno.h>
  26. struct shim_sandbox {
  27. unsigned int sbid;
  28. unsigned int parent_sbid;
  29. IDTYPE parent_vmid;
  30. };
  31. static struct shim_sandbox sandbox_info __attribute_migratable;
  32. static inline void append_uri (char * uri, int prefix_len, char * append,
  33. int append_len)
  34. {
  35. if (prefix_len && uri[prefix_len - 1] == ':') {
  36. if (append[0] == '/')
  37. memcpy(uri + prefix_len, append + 1, append_len);
  38. else
  39. memcpy(uri + prefix_len, append, append_len + 1);
  40. } else {
  41. if (append[0] != '/')
  42. uri[prefix_len++] = '/';
  43. memcpy(uri + prefix_len, append, append_len + 1);
  44. }
  45. }
  46. static int isolate_fs (struct config_store * cfg, const char * path)
  47. {
  48. struct shim_dentry * dent = NULL;
  49. int ret = 0;
  50. if ((ret = path_lookupat(NULL, path, LOOKUP_OPEN, &dent)) < 0)
  51. return ret;
  52. if (!(dent->state & DENTRY_ISDIRECTORY)) {
  53. put_dentry(dent);
  54. return -ENOTDIR;
  55. }
  56. int dpath_len = 0;
  57. char * dpath = dentry_get_path(dent, true, &dpath_len);
  58. bool root_created = false;
  59. char t[CONFIG_MAX], u[CONFIG_MAX];
  60. int nkeys;
  61. char * keybuf = __alloca(get_config_entries_size(cfg, "fs.mount.other"));
  62. nkeys = get_config_entries(cfg, "fs.mount.other", keybuf);
  63. if (nkeys <= 0)
  64. goto root;
  65. char k[CONFIG_MAX], p[CONFIG_MAX];
  66. char * tmp = strcpy_static(k, "fs.mount.other.", CONFIG_MAX);
  67. const char * key = keybuf, * next = NULL;
  68. for (int n = 0 ; n < nkeys ; key = next, n++) {
  69. for (next = key ; *next ; next++);
  70. next++;
  71. int key_len = next - key - 1;
  72. memcpy(tmp, key, key_len);
  73. char * kp = tmp + key_len;
  74. int ulen, plen;
  75. bool is_chroot = false;
  76. /* Skip FS that are not chroot */
  77. strcpy_static(kp, ".type", k + CONFIG_MAX - kp);
  78. if ((ret = get_config(cfg, k, t, CONFIG_MAX)) <= 0)
  79. continue;
  80. if (strpartcmp_static(t, "chroot"))
  81. is_chroot = true;
  82. strcpy_static(kp, ".uri", k + CONFIG_MAX - kp);
  83. if ((ulen = get_config(cfg, k, u, CONFIG_MAX)) <= 0)
  84. continue;
  85. strcpy_static(kp, ".path", k + CONFIG_MAX - kp);
  86. if ((plen = get_config(cfg, k, p, CONFIG_MAX)) <= 0)
  87. continue;
  88. if (plen >= dpath_len) {
  89. if (!memcmp(p, dpath, dpath_len)) {
  90. if (!p[dpath_len]) {
  91. root_created = true;
  92. debug("kept file rule: %s => %s\n", p, u);
  93. continue;
  94. }
  95. if (p[dpath_len] != '/')
  96. goto remove;
  97. /* keep this FS */
  98. continue;
  99. } else {
  100. remove:
  101. if (!is_chroot) {
  102. debug("kept file rule: %s => %s\n", p, u);
  103. continue;
  104. }
  105. set_config(cfg, k, NULL);
  106. strcpy_static(kp, ".type", k + CONFIG_MAX - kp);
  107. set_config(cfg, k, NULL);
  108. strcpy_static(kp, ".uri", k + CONFIG_MAX - kp);
  109. set_config(cfg, k, NULL);
  110. debug("deleted file rule: %s => %s\n", p, u);
  111. }
  112. } else {
  113. if (memcmp(p, dpath, plen))
  114. goto remove;
  115. assert(dpath[plen]);
  116. if (dpath[plen] != '/')
  117. goto remove;
  118. if (!is_chroot) {
  119. root_created = true;
  120. debug("kept file rule: %s => %s\n", p, u);
  121. continue;
  122. }
  123. append_uri(u, ulen, dpath + plen, dpath_len - plen);
  124. set_config(cfg, k, dpath);
  125. strcpy_static(kp, "uri", k + CONFIG_MAX - kp);
  126. set_config(cfg, k, u);
  127. root_created = true;
  128. debug("added file rule: %s => %s\n", dpath, u);
  129. }
  130. }
  131. root:
  132. if ((ret = get_config(cfg, "fs.mount.root.uri", u, CONFIG_MAX)) > 0) {
  133. int prefix_len = ret;
  134. if ((ret = get_config(cfg, "fs.mount.root.type", t, CONFIG_MAX)) > 0 &&
  135. strcmp_static(t, "chroot")) {
  136. /* remove the root FS */
  137. set_config(cfg, "fs.mount.root.uri", NULL);
  138. set_config(cfg, "fs.mount.root.type", NULL);
  139. debug("deleted file rule: root\n");
  140. /* add another FS as part of the original root FS */
  141. if (!root_created) {
  142. append_uri(u, prefix_len, dpath, dpath_len);
  143. set_config(cfg, "fs.mount.other.root.path", dpath);
  144. set_config(cfg, "fs.mount.other.root.uri", u);
  145. set_config(cfg, "fs.mount.other.root.type", "chroot");
  146. debug("added file rule: %s => %s\n", dpath, u);
  147. }
  148. }
  149. }
  150. return 0;
  151. }
  152. static int isolate_net (struct config_store * cfg, struct net_sb * sb)
  153. {
  154. int nkeys;
  155. char k[CONFIG_MAX];
  156. char * keybuf = __alloca(get_config_entries_size(cfg, "net.rules"));
  157. nkeys = get_config_entries(cfg, "net.rules", keybuf);
  158. if (nkeys <= 0)
  159. goto add;
  160. const char * key = keybuf, * next = NULL;
  161. memcpy(k, "net.rules.", 10);
  162. for (int n = 0 ; n < nkeys ; key = next, n++) {
  163. for (next = key ; *next ; next++);
  164. next++;
  165. int key_len = next - key - 1;
  166. memcpy(k + 10, key, key_len);
  167. k[10 + key_len] = 0;
  168. set_config(cfg, k, NULL);
  169. }
  170. add:
  171. if (!sb)
  172. return 0;
  173. for (int i = 0 ; i < sb->nrules ; i++) {
  174. struct net_sb_rule * r = &sb->rules[i];
  175. char u[CONFIG_MAX];
  176. int ulen;
  177. int family = -1;
  178. undo:
  179. ulen = 0;
  180. for (int turn = 0 ; turn < 2 ; turn++) {
  181. struct sockaddr * addr = turn ? r->r_addr : r->l_addr;
  182. if (turn)
  183. u[ulen++] = ':';
  184. if (!addr) {
  185. if (family == -1 || family == AF_INET)
  186. ulen += snprintf(u + ulen, CONFIG_MAX - ulen,
  187. "0.0.0.0:0-65535");
  188. else
  189. ulen += snprintf(u + ulen, CONFIG_MAX - ulen,
  190. "[0:0:0:0:0:0:0:0]:0-65535]");
  191. } else {
  192. if (addr->sa_family == AF_INET) {
  193. if (family == AF_INET6)
  194. goto next;
  195. family = AF_INET;
  196. struct sockaddr_in * saddr = (void *) addr;
  197. unsigned char * a = (void *) &saddr->sin_addr.s_addr;
  198. ulen += snprintf(u + ulen, CONFIG_MAX - ulen,
  199. "%d.%d.%d.%d:%u",
  200. a[0], a[1], a[2], a[3],
  201. __ntohs(saddr->sin_port));
  202. continue;
  203. }
  204. if (addr->sa_family == AF_INET6) {
  205. if (family == AF_INET)
  206. goto next;
  207. if (turn && family == -1) {
  208. family = AF_INET6;
  209. goto undo;
  210. }
  211. family = AF_INET6;
  212. struct sockaddr_in6 * saddr = (void *) addr;
  213. unsigned short * a = (void *) &saddr->sin6_addr.s6_addr;
  214. ulen += snprintf(u + ulen, CONFIG_MAX - ulen,
  215. "[%d:%d:%d:%d:%d:%d:%d:%d]:%u",
  216. a[0], a[1], a[2], a[3],
  217. a[4], a[5], a[6], a[7],
  218. __ntohs(saddr->sin6_port));
  219. continue;
  220. }
  221. goto next;
  222. }
  223. }
  224. snprintf(k + 10, CONFIG_MAX - 10, "%d", i + 1);
  225. set_config(cfg, k, u);
  226. debug("added net rule: %s\n", u);
  227. next:
  228. continue;
  229. }
  230. return 0;
  231. }
  232. static void * __malloc (int size)
  233. {
  234. return malloc(size);
  235. }
  236. static void __free (void * mem)
  237. {
  238. free(mem);
  239. }
  240. struct cfg_arg {
  241. PAL_HANDLE handle;
  242. int offset;
  243. };
  244. static int __write (void * f, void * buf, int len)
  245. {
  246. struct cfg_arg * arg = f;
  247. int bytes = DkStreamWrite(arg->handle, arg->offset, len, buf, NULL);
  248. if (!bytes)
  249. return -PAL_ERRNO;
  250. arg->offset += bytes;
  251. return bytes;
  252. }
  253. long shim_do_sandbox_create (int flags, const char * fs_sb,
  254. struct net_sb * net_sb)
  255. {
  256. unsigned int sbid;
  257. char uri[24];
  258. PAL_HANDLE handle = NULL;
  259. int ret = create_handle("file:sandbox-", uri, 24, &handle, &sbid);
  260. if (ret < 0)
  261. return ret;
  262. debug("create manifest: %s\n", uri);
  263. struct config_store * newcfg = __alloca(sizeof(struct config_store));
  264. memset(newcfg, 0, sizeof(struct config_store));
  265. newcfg->malloc = __malloc;
  266. newcfg->free = __free;
  267. if ((ret = copy_config(root_config, newcfg)) < 0) {
  268. newcfg = NULL;
  269. goto err;
  270. }
  271. if (flags & SANDBOX_FS)
  272. if ((ret = isolate_fs(newcfg, fs_sb)) < 0)
  273. goto err;
  274. if (flags & SANDBOX_NET)
  275. if ((ret = isolate_net(newcfg, net_sb)) < 0)
  276. goto err;
  277. struct cfg_arg arg;
  278. arg.handle = handle;
  279. arg.offset = 0;
  280. if ((ret = write_config(&arg, __write, newcfg)) < 0)
  281. goto err;
  282. DkObjectClose(handle);
  283. PAL_BOL success = DkProcessSandboxCreate(uri, flags & SANDBOX_RPC ?
  284. PAL_SANDBOX_PIPE : 0);
  285. if (!success) {
  286. ret = -PAL_ERRNO;
  287. goto err;
  288. }
  289. if (sandbox_info.sbid) {
  290. if (!sandbox_info.parent_sbid ||
  291. sandbox_info.parent_vmid != cur_process.vmid) {
  292. sandbox_info.parent_sbid = sandbox_info.sbid;
  293. sandbox_info.parent_vmid = cur_process.vmid;
  294. }
  295. }
  296. if (flags & SANDBOX_RPC)
  297. del_all_ipc_ports(0);
  298. if ((ret = free_config(root_config)) < 0)
  299. goto err;
  300. handle = DkStreamOpen(uri, PAL_ACCESS_RDONLY, 0, 0, 0);
  301. if (!handle)
  302. return -PAL_ERRNO;
  303. root_config = newcfg;
  304. sandbox_info.sbid = sbid;
  305. return sbid;
  306. err:
  307. free_config(newcfg);
  308. DkStreamDelete(handle, 0);
  309. DkObjectClose(handle);
  310. return ret;
  311. }
  312. int shim_do_sandbox_attach (unsigned int sbid)
  313. {
  314. return -ENOSYS;
  315. }
  316. long shim_do_sandbox_current (void)
  317. {
  318. return sandbox_info.sbid;
  319. }