db_process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. * db_process.c
  17. *
  18. * This source file contains functions to create a child process and terminate
  19. * the running process. Child does not inherit any objects or memory from its
  20. * parent pricess. A Parent process may not modify the execution of its
  21. * children. It can wait for a child to exit using its handle. Also, parent and
  22. * child may communicate through I/O streams provided by the parent to the child
  23. * at creation.
  24. */
  25. #include "pal_defs.h"
  26. #include "pal_linux_defs.h"
  27. #include "pal.h"
  28. #include "pal_internal.h"
  29. #include "pal_linux.h"
  30. #include "pal_debug.h"
  31. #include "pal_error.h"
  32. #include "pal_security.h"
  33. #include "pal_crypto.h"
  34. #include "api.h"
  35. #include <linux/sched.h>
  36. #include <linux/types.h>
  37. #include <linux/fs.h>
  38. typedef __kernel_pid_t pid_t;
  39. #include <asm/fcntl.h>
  40. struct trusted_child {
  41. struct list_head list;
  42. sgx_arch_hash_t mrenclave;
  43. char uri[];
  44. };
  45. static LIST_HEAD(trusted_children);
  46. static struct spinlock trusted_children_lock = LOCK_INIT;
  47. int register_trusted_child(const char * uri, const char * mrenclave_str)
  48. {
  49. struct trusted_child * tc = NULL, * new;
  50. int uri_len = strlen(uri);
  51. _DkSpinLock(&trusted_children_lock);
  52. list_for_each_entry(tc, &trusted_children, list) {
  53. if (!memcmp(tc->uri, uri, uri_len + 1)) {
  54. _DkSpinUnlock(&trusted_children_lock);
  55. return 0;
  56. }
  57. }
  58. _DkSpinUnlock(&trusted_children_lock);
  59. new = malloc(sizeof(struct trusted_child) + uri_len);
  60. if (!new)
  61. return -PAL_ERROR_NOMEM;
  62. INIT_LIST_HEAD(&new->list);
  63. memcpy(new->uri, uri, uri_len + 1);
  64. char mrenclave_text[sizeof(sgx_arch_hash_t) * 2 + 1] = "\0";
  65. int nbytes = 0;
  66. for (; nbytes < sizeof(sgx_arch_hash_t) ; nbytes++) {
  67. char byte1 = mrenclave_str[nbytes * 2];
  68. char byte2 = mrenclave_str[nbytes * 2 + 1];
  69. unsigned char val = 0;
  70. if (byte1 == 0 || byte2 == 0) {
  71. break;
  72. }
  73. if (!(byte1 >= '0' && byte1 <= '9') &&
  74. !(byte1 >= 'a' && byte1 <= 'f')) {
  75. break;
  76. }
  77. if (!(byte2 >= '0' && byte2 <= '9') &&
  78. !(byte2 >= 'a' && byte2 <= 'f')) {
  79. break;
  80. }
  81. if (byte1 >= '0' && byte1 <= '9')
  82. val = byte1 - '0';
  83. if (byte1 >= 'a' && byte1 <= 'f')
  84. val = byte1 - 'a' + 10;
  85. val *= 16;
  86. if (byte2 >= '0' && byte2 <= '9')
  87. val += byte2 - '0';
  88. if (byte2 >= 'a' && byte2 <= 'f')
  89. val += byte2 - 'a' + 10;
  90. new->mrenclave[nbytes] = val;
  91. snprintf(mrenclave_text + nbytes * 2, 3, "%02x", val);
  92. }
  93. if (nbytes < sizeof(sgx_arch_hash_t)) {
  94. free(new);
  95. return -PAL_ERROR_INVAL;
  96. }
  97. SGX_DBG(DBG_S, "trusted: %s %s\n", mrenclave_text, new->uri);
  98. _DkSpinLock(&trusted_children_lock);
  99. list_for_each_entry(tc, &trusted_children, list) {
  100. if (!memcmp(tc->uri, uri, uri_len + 1)) {
  101. _DkSpinUnlock(&trusted_children_lock);
  102. free(new);
  103. return 0;
  104. }
  105. }
  106. list_add_tail(&new->list, &trusted_children);
  107. _DkSpinUnlock(&trusted_children_lock);
  108. return 0;
  109. }
  110. struct proc_attestation_data {
  111. sgx_arch_mac_t keyhash_mac;
  112. uint8_t reserved[PAL_ATTESTATION_DATA_SIZE - sizeof(sgx_arch_mac_t)];
  113. } __attribute__((packed));
  114. struct check_child_param {
  115. PAL_MAC_KEY mac_key;
  116. const char * uri;
  117. };
  118. static int check_child_mrenclave (sgx_arch_hash_t * mrenclave,
  119. void * signed_data, void * check_param)
  120. {
  121. struct pal_enclave_state * remote_state = signed_data;
  122. struct proc_attestation_data * data = (void *) &remote_state->data;
  123. /* the process must be a clean process */
  124. if (remote_state->enclave_flags & PAL_ENCLAVE_INITIALIZED)
  125. return 1;
  126. struct check_child_param * param = check_param;
  127. /* must make sure the signer of the report is also the owner of the key,
  128. in order to prevent man-in-the-middle attack */
  129. struct proc_attestation_data check_data;
  130. memset(&check_data, 0, sizeof(struct proc_attestation_data));
  131. AES_CMAC((void *) &param->mac_key,
  132. remote_state->enclave_keyhash,
  133. sizeof(remote_state->enclave_keyhash),
  134. check_data.keyhash_mac);
  135. if (memcmp(data, &check_data, sizeof(struct proc_attestation_data)))
  136. return 1;
  137. /* always accept our own as child */
  138. if (!memcmp(mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t))) {
  139. SGX_DBG(DBG_S, "trusted child: <forked>\n");
  140. return 0;
  141. }
  142. struct trusted_child * tc;
  143. _DkSpinLock(&trusted_children_lock);
  144. list_for_each_entry(tc, &trusted_children, list) {
  145. if (!memcmp(mrenclave, tc->mrenclave, sizeof(sgx_arch_hash_t))) {
  146. _DkSpinUnlock(&trusted_children_lock);
  147. SGX_DBG(DBG_S, "trusted child: %s\n", tc->uri);
  148. return 0;
  149. }
  150. }
  151. _DkSpinUnlock(&trusted_children_lock);
  152. return 1;
  153. }
  154. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri,
  155. int flags, const char ** args)
  156. {
  157. /* only access creating process with regular file */
  158. if (!strpartcmp_static(uri, "file:"))
  159. return -PAL_ERROR_INVAL;
  160. unsigned int child_pid;
  161. int proc_fds[3];
  162. int nargs = 0, ret;
  163. if (args)
  164. for (const char ** a = args ; *a ; a++)
  165. nargs++;
  166. ret = ocall_create_process(uri, nargs, args,
  167. proc_fds,
  168. &child_pid);
  169. if (ret < 0)
  170. return ret;
  171. PAL_HANDLE proc = malloc(HANDLE_SIZE(process));
  172. SET_HANDLE_TYPE(proc, process);
  173. HANDLE_HDR(proc)->flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(2);
  174. proc->process.stream_in = proc_fds[0];
  175. proc->process.stream_out = proc_fds[1];
  176. proc->process.cargo = proc_fds[2];
  177. proc->process.pid = child_pid;
  178. proc->process.nonblocking = PAL_FALSE;
  179. PAL_SESSION_KEY session_key;
  180. ret = _DkStreamKeyExchange(proc, &session_key);
  181. if (ret < 0)
  182. return ret;
  183. struct check_child_param param;
  184. session_key_to_mac_key(&session_key, &param.mac_key);
  185. param.uri = uri;
  186. struct proc_attestation_data data;
  187. memset(&data, 0, sizeof(struct proc_attestation_data));
  188. AES_CMAC((void *) &param.mac_key,
  189. pal_enclave_state.enclave_keyhash,
  190. sizeof(pal_enclave_state.enclave_keyhash),
  191. data.keyhash_mac);
  192. SGX_DBG(DBG_P|DBG_S, "Attestation data: %s\n", hex2str(data.keyhash_mac));
  193. ret = _DkStreamAttestationRequest(proc, &data,
  194. &check_child_mrenclave, &param);
  195. if (ret < 0)
  196. return ret;
  197. *handle = proc;
  198. return 0;
  199. }
  200. struct check_parent_param {
  201. PAL_MAC_KEY mac_key;
  202. };
  203. static int check_parent_mrenclave (sgx_arch_hash_t * mrenclave,
  204. void * signed_data, void * check_param)
  205. {
  206. struct pal_enclave_state * remote_state = signed_data;
  207. struct proc_attestation_param * data = (void *) &remote_state->data;
  208. struct check_parent_param * param = check_param;
  209. /* must make sure the signer of the report is also the owner of the key,
  210. in order to prevent man-in-the-middle attack */
  211. struct proc_attestation_data check_data;
  212. memset(&check_data, 0, sizeof(struct proc_attestation_data));
  213. AES_CMAC((void *) &param->mac_key,
  214. remote_state->enclave_keyhash,
  215. sizeof(remote_state->enclave_keyhash),
  216. check_data.keyhash_mac);
  217. if (memcmp(data, &check_data, sizeof(struct proc_attestation_data)))
  218. return 1;
  219. /* for now, we will accept any enclave as a parent, but eventually
  220. we should check parent, maybe using crypto challenge */
  221. return 0;
  222. }
  223. int init_child_process (PAL_HANDLE * parent_handle)
  224. {
  225. PAL_HANDLE parent = malloc(HANDLE_SIZE(process));
  226. SET_HANDLE_TYPE(parent, process);
  227. HANDLE_HDR(parent)->flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(2);
  228. parent->process.stream_in = pal_sec.proc_fds[0];
  229. parent->process.stream_out = pal_sec.proc_fds[1];
  230. parent->process.cargo = pal_sec.proc_fds[2];
  231. parent->process.pid = pal_sec.ppid;
  232. parent->process.nonblocking = PAL_FALSE;
  233. PAL_SESSION_KEY session_key;
  234. int ret = _DkStreamKeyExchange(parent, &session_key);
  235. if (ret < 0)
  236. return ret;
  237. struct check_parent_param param;
  238. session_key_to_mac_key(&session_key, &param.mac_key);
  239. struct proc_attestation_data data;
  240. memset(&data, 0, sizeof(struct proc_attestation_data));
  241. AES_CMAC((void *) &param.mac_key,
  242. pal_enclave_state.enclave_keyhash,
  243. sizeof(pal_enclave_state.enclave_keyhash),
  244. data.keyhash_mac);
  245. SGX_DBG(DBG_P|DBG_S, "Attestation data: %s\n", hex2str(data.keyhash_mac));
  246. ret = _DkStreamAttestationRespond(parent, &data,
  247. &check_parent_mrenclave,
  248. &param);
  249. if (ret < 0)
  250. return ret;
  251. *parent_handle = parent;
  252. return 0;
  253. }
  254. void print_alloced_pages (void);
  255. void _DkProcessExit (int exitcode)
  256. {
  257. if (__pal_control.parent_process)
  258. _DkObjectClose(__pal_control.parent_process);
  259. if (__pal_control.manifest_handle)
  260. _DkObjectClose(__pal_control.manifest_handle);
  261. #if PRINT_ENCLAVE_STAT
  262. print_alloced_pages();
  263. #endif
  264. ocall_exit();
  265. }
  266. int _DkProcessSandboxCreate (const char * manifest, int flags)
  267. {
  268. return -PAL_ERROR_NOTIMPLEMENTED;
  269. }
  270. static int proc_read (PAL_HANDLE handle, int offset, int count,
  271. void * buffer)
  272. {
  273. return ocall_read(handle->process.stream_in, buffer, count);
  274. }
  275. static int proc_write (PAL_HANDLE handle, int offset, int count,
  276. const void * buffer)
  277. {
  278. int bytes = ocall_write(handle->process.stream_out, buffer, count);
  279. if (bytes == -PAL_ERROR_TRYAGAIN)
  280. HANDLE_HDR(handle)->flags &= ~WRITEABLE(1);
  281. if (bytes < 0)
  282. return bytes;
  283. if (bytes == count)
  284. HANDLE_HDR(handle)->flags |= WRITEABLE(1);
  285. else
  286. HANDLE_HDR(handle)->flags &= ~WRITEABLE(1);
  287. return bytes;
  288. }
  289. static int proc_close (PAL_HANDLE handle)
  290. {
  291. if (handle->process.stream_in != PAL_IDX_POISON) {
  292. ocall_close(handle->process.stream_in);
  293. handle->process.stream_in = PAL_IDX_POISON;
  294. }
  295. if (handle->process.stream_out != PAL_IDX_POISON) {
  296. ocall_close(handle->process.stream_out);
  297. handle->process.stream_out = PAL_IDX_POISON;
  298. }
  299. if (handle->process.cargo != PAL_IDX_POISON) {
  300. ocall_close(handle->process.cargo);
  301. handle->process.cargo = PAL_IDX_POISON;
  302. }
  303. return 0;
  304. }
  305. static int proc_delete (PAL_HANDLE handle, int access)
  306. {
  307. int shutdown;
  308. switch (access) {
  309. case 0:
  310. shutdown = SHUT_RDWR;
  311. break;
  312. case PAL_DELETE_RD:
  313. shutdown = SHUT_RD;
  314. break;
  315. case PAL_DELETE_WR:
  316. shutdown = SHUT_WR;
  317. break;
  318. default:
  319. return -PAL_ERROR_INVAL;
  320. }
  321. if (access != PAL_DELETE_WR &&
  322. handle->process.stream_in != PAL_IDX_POISON) {
  323. ocall_close(handle->process.stream_in);
  324. handle->process.stream_in = PAL_IDX_POISON;
  325. }
  326. if (access != PAL_DELETE_RD &&
  327. handle->process.stream_out != PAL_IDX_POISON) {
  328. ocall_close(handle->process.stream_out);
  329. handle->process.stream_out = PAL_IDX_POISON;
  330. }
  331. if (handle->process.cargo != PAL_IDX_POISON)
  332. ocall_sock_shutdown(handle->process.cargo, shutdown);
  333. return 0;
  334. }
  335. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  336. {
  337. if (handle->process.stream_in == PAL_IDX_POISON)
  338. return -PAL_ERROR_BADHANDLE;
  339. int ret = ocall_fionread(handle->process.stream_in);
  340. if (ret < 0)
  341. return -ret;
  342. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  343. attr->pending_size = ret;
  344. attr->disconnected = HANDLE_HDR(handle)->flags & (ERROR(0)|ERROR(1));
  345. attr->readable = (attr->pending_size > 0);
  346. attr->writeable = HANDLE_HDR(handle)->flags & WRITEABLE(1);
  347. attr->nonblocking = handle->process.nonblocking;
  348. return 0;
  349. }
  350. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  351. {
  352. if (handle->process.stream_in == PAL_IDX_POISON)
  353. return -PAL_ERROR_BADHANDLE;
  354. if (attr->nonblocking != handle->process.nonblocking) {
  355. int ret = ocall_fsetnonblock(handle->process.stream_in,
  356. handle->process.nonblocking);
  357. if (ret < 0)
  358. return ret;
  359. handle->process.nonblocking = attr->nonblocking;
  360. }
  361. return 0;
  362. }
  363. struct handle_ops proc_ops = {
  364. .read = &proc_read,
  365. .write = &proc_write,
  366. .close = &proc_close,
  367. .delete = &proc_delete,
  368. .attrquerybyhdl = &proc_attrquerybyhdl,
  369. .attrsetbyhdl = &proc_attrsetbyhdl,
  370. };