db_process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_process.c
  15. *
  16. * This source file contains functions to create a child process and terminate
  17. * the running process. Child does not inherit any objects or memory from its
  18. * parent pricess. A Parent process may not modify the execution of its
  19. * children. It can wait for a child to exit using its handle. Also, parent and
  20. * child may communicate through I/O streams provided by the parent to the child
  21. * at creation.
  22. */
  23. #include "pal_defs.h"
  24. #include "pal_linux_defs.h"
  25. #include "pal.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_linux_error.h"
  29. #include "pal_debug.h"
  30. #include "pal_error.h"
  31. #include "pal_security.h"
  32. #include "pal_crypto.h"
  33. #include "api.h"
  34. #include <linux/sched.h>
  35. #include <linux/types.h>
  36. #include <linux/fs.h>
  37. typedef __kernel_pid_t pid_t;
  38. #include <asm/fcntl.h>
  39. DEFINE_LIST(trusted_child);
  40. struct trusted_child {
  41. LIST_TYPE(trusted_child) list;
  42. sgx_arch_hash_t mrenclave;
  43. char uri[];
  44. };
  45. DEFINE_LISTP(trusted_child);
  46. static LISTP_TYPE(trusted_child) trusted_children = LISTP_INIT;
  47. static struct spinlock trusted_children_lock = LOCK_INIT;
  48. int register_trusted_child(const char * uri, const char * mrenclave_str)
  49. {
  50. struct trusted_child * tc = NULL, * new;
  51. int uri_len = strlen(uri);
  52. _DkSpinLock(&trusted_children_lock);
  53. LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
  54. if (!memcmp(tc->uri, uri, uri_len + 1)) {
  55. _DkSpinUnlock(&trusted_children_lock);
  56. return 0;
  57. }
  58. }
  59. _DkSpinUnlock(&trusted_children_lock);
  60. new = malloc(sizeof(struct trusted_child) + uri_len);
  61. if (!new)
  62. return -PAL_ERROR_NOMEM;
  63. INIT_LIST_HEAD(new, list);
  64. memcpy(new->uri, uri, uri_len + 1);
  65. char mrenclave_text[sizeof(sgx_arch_hash_t) * 2 + 1] = "\0";
  66. size_t nbytes = 0;
  67. for (; nbytes < sizeof(sgx_arch_hash_t) ; nbytes++) {
  68. char byte1 = mrenclave_str[nbytes * 2];
  69. char byte2 = mrenclave_str[nbytes * 2 + 1];
  70. unsigned char val = 0;
  71. if (byte1 == 0 || byte2 == 0) {
  72. break;
  73. }
  74. if (!(byte1 >= '0' && byte1 <= '9') &&
  75. !(byte1 >= 'a' && byte1 <= 'f')) {
  76. break;
  77. }
  78. if (!(byte2 >= '0' && byte2 <= '9') &&
  79. !(byte2 >= 'a' && byte2 <= 'f')) {
  80. break;
  81. }
  82. if (byte1 >= '0' && byte1 <= '9')
  83. val = byte1 - '0';
  84. if (byte1 >= 'a' && byte1 <= 'f')
  85. val = byte1 - 'a' + 10;
  86. val *= 16;
  87. if (byte2 >= '0' && byte2 <= '9')
  88. val += byte2 - '0';
  89. if (byte2 >= 'a' && byte2 <= 'f')
  90. val += byte2 - 'a' + 10;
  91. new->mrenclave[nbytes] = val;
  92. snprintf(mrenclave_text + nbytes * 2, 3, "%02x", val);
  93. }
  94. if (nbytes < sizeof(sgx_arch_hash_t)) {
  95. free(new);
  96. return -PAL_ERROR_INVAL;
  97. }
  98. SGX_DBG(DBG_S, "trusted: %s %s\n", mrenclave_text, new->uri);
  99. _DkSpinLock(&trusted_children_lock);
  100. LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
  101. if (!memcmp(tc->uri, uri, uri_len + 1)) {
  102. _DkSpinUnlock(&trusted_children_lock);
  103. free(new);
  104. return 0;
  105. }
  106. }
  107. LISTP_ADD_TAIL(new, &trusted_children, list);
  108. _DkSpinUnlock(&trusted_children_lock);
  109. return 0;
  110. }
  111. struct proc_attestation_data {
  112. sgx_arch_mac_t keyhash_mac;
  113. uint8_t reserved[PAL_ATTESTATION_DATA_SIZE - sizeof(sgx_arch_mac_t)];
  114. } __attribute__((packed));
  115. struct check_child_param {
  116. PAL_MAC_KEY mac_key;
  117. const char * uri;
  118. };
  119. static int check_child_mrenclave (sgx_arch_hash_t * mrenclave,
  120. void * signed_data, void * check_param)
  121. {
  122. struct pal_enclave_state * remote_state = signed_data;
  123. struct proc_attestation_data * data = (void *) &remote_state->data;
  124. /* the process must be a clean process */
  125. if (remote_state->enclave_flags & PAL_ENCLAVE_INITIALIZED)
  126. return 1;
  127. struct check_child_param * param = check_param;
  128. /* must make sure the signer of the report is also the owner of the key,
  129. in order to prevent man-in-the-middle attack */
  130. struct proc_attestation_data check_data;
  131. memset(&check_data, 0, sizeof(struct proc_attestation_data));
  132. lib_AESCMAC((void *) &param->mac_key, AES_CMAC_KEY_LEN,
  133. remote_state->enclave_identifier,
  134. sizeof(remote_state->enclave_identifier),
  135. check_data.keyhash_mac, sizeof(check_data.keyhash_mac));
  136. if (memcmp(data, &check_data, sizeof(struct proc_attestation_data)))
  137. return 1;
  138. /* always accept our own as child */
  139. if (!memcmp(mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t))) {
  140. SGX_DBG(DBG_S, "trusted child: <forked>\n");
  141. return 0;
  142. }
  143. struct trusted_child * tc;
  144. _DkSpinLock(&trusted_children_lock);
  145. LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
  146. if (!memcmp(mrenclave, tc->mrenclave, sizeof(sgx_arch_hash_t))) {
  147. _DkSpinUnlock(&trusted_children_lock);
  148. SGX_DBG(DBG_S, "trusted child: %s\n", tc->uri);
  149. return 0;
  150. }
  151. }
  152. _DkSpinUnlock(&trusted_children_lock);
  153. return 1;
  154. }
  155. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri, 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 (IS_ERR(ret))
  170. return unix_to_pal_error(ERRNO(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)|WRITABLE(1)|WRITABLE(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. lib_AESCMAC((void *) &param.mac_key, AES_CMAC_KEY_LEN,
  189. pal_enclave_state.enclave_identifier,
  190. sizeof(pal_enclave_state.enclave_identifier),
  191. data.keyhash_mac, sizeof(data.keyhash_mac));
  192. SGX_DBG(DBG_P|DBG_S, "Attestation data: %s\n",
  193. ALLOCA_BYTES2HEXSTR(data.keyhash_mac));
  194. ret = _DkStreamAttestationRequest(proc, &data,
  195. &check_child_mrenclave, &param);
  196. if (ret < 0)
  197. return ret;
  198. *handle = proc;
  199. return 0;
  200. }
  201. struct check_parent_param {
  202. PAL_MAC_KEY mac_key;
  203. };
  204. static int check_parent_mrenclave (sgx_arch_hash_t * mrenclave,
  205. void * signed_data, void * check_param)
  206. {
  207. __UNUSED(mrenclave);
  208. struct pal_enclave_state * remote_state = signed_data;
  209. struct proc_attestation_param * data = (void *) &remote_state->data;
  210. struct check_parent_param * param = check_param;
  211. /* must make sure the signer of the report is also the owner of the key,
  212. in order to prevent man-in-the-middle attack */
  213. struct proc_attestation_data check_data;
  214. memset(&check_data, 0, sizeof(struct proc_attestation_data));
  215. lib_AESCMAC((void *) &param->mac_key, AES_CMAC_KEY_LEN,
  216. remote_state->enclave_identifier,
  217. sizeof(remote_state->enclave_identifier),
  218. check_data.keyhash_mac, sizeof(check_data.keyhash_mac));
  219. if (memcmp(data, &check_data, sizeof(struct proc_attestation_data)))
  220. return 1;
  221. /* for now, we will accept any enclave as a parent, but eventually
  222. we should check parent, maybe using crypto challenge */
  223. return 0;
  224. }
  225. int init_child_process (PAL_HANDLE * parent_handle)
  226. {
  227. PAL_HANDLE parent = malloc(HANDLE_SIZE(process));
  228. SET_HANDLE_TYPE(parent, process);
  229. HANDLE_HDR(parent)->flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITABLE(1)|WRITABLE(2);
  230. parent->process.stream_in = pal_sec.proc_fds[0];
  231. parent->process.stream_out = pal_sec.proc_fds[1];
  232. parent->process.cargo = pal_sec.proc_fds[2];
  233. parent->process.pid = pal_sec.ppid;
  234. parent->process.nonblocking = PAL_FALSE;
  235. PAL_SESSION_KEY session_key;
  236. int ret = _DkStreamKeyExchange(parent, &session_key);
  237. if (ret < 0)
  238. return ret;
  239. struct check_parent_param param;
  240. session_key_to_mac_key(&session_key, &param.mac_key);
  241. struct proc_attestation_data data;
  242. memset(&data, 0, sizeof(struct proc_attestation_data));
  243. lib_AESCMAC((void *) &param.mac_key, AES_CMAC_KEY_LEN,
  244. pal_enclave_state.enclave_identifier,
  245. sizeof(pal_enclave_state.enclave_identifier),
  246. data.keyhash_mac, sizeof(data.keyhash_mac));
  247. SGX_DBG(DBG_P|DBG_S, "Attestation data: %s\n",
  248. ALLOCA_BYTES2HEXSTR(data.keyhash_mac));
  249. ret = _DkStreamAttestationRespond(parent, &data,
  250. &check_parent_mrenclave,
  251. &param);
  252. if (ret < 0)
  253. return ret;
  254. *parent_handle = parent;
  255. return 0;
  256. }
  257. void print_alloced_pages (void);
  258. noreturn void _DkProcessExit (int exitcode)
  259. {
  260. #if PRINT_ENCLAVE_STAT
  261. print_alloced_pages();
  262. #endif
  263. if (exitcode)
  264. SGX_DBG(DBG_I, "DkProcessExit: Returning exit code %d\n", exitcode);
  265. ocall_exit(exitcode, /*is_exitgroup=*/true);
  266. while (true) {
  267. /* nothing */;
  268. }
  269. }
  270. static int64_t proc_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  271. void * buffer)
  272. {
  273. if (offset)
  274. return -PAL_ERROR_INVAL;
  275. if (count >= (1ULL << (sizeof(unsigned int) * 8)))
  276. return -PAL_ERROR_INVAL;
  277. int bytes = ocall_read(handle->process.stream_in, buffer, count);
  278. return IS_ERR(bytes) ? unix_to_pal_error(ERRNO(bytes)) : bytes;
  279. }
  280. static int64_t proc_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  281. const void * buffer)
  282. {
  283. if (offset)
  284. return -PAL_ERROR_INVAL;
  285. if (count >= (1ULL << (sizeof(unsigned int) * 8)))
  286. return -PAL_ERROR_INVAL;
  287. int bytes = ocall_write(handle->process.stream_out, buffer, count);
  288. if (IS_ERR(bytes)) {
  289. bytes = unix_to_pal_error(ERRNO(bytes));
  290. if (bytes == -PAL_ERROR_TRYAGAIN)
  291. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  292. return bytes;
  293. }
  294. if ((uint64_t)bytes == count)
  295. HANDLE_HDR(handle)->flags |= WRITABLE(1);
  296. else
  297. HANDLE_HDR(handle)->flags &= ~WRITABLE(1);
  298. return bytes;
  299. }
  300. static int proc_close (PAL_HANDLE handle)
  301. {
  302. if (handle->process.stream_in != PAL_IDX_POISON) {
  303. ocall_close(handle->process.stream_in);
  304. handle->process.stream_in = PAL_IDX_POISON;
  305. }
  306. if (handle->process.stream_out != PAL_IDX_POISON) {
  307. ocall_close(handle->process.stream_out);
  308. handle->process.stream_out = PAL_IDX_POISON;
  309. }
  310. if (handle->process.cargo != PAL_IDX_POISON) {
  311. ocall_close(handle->process.cargo);
  312. handle->process.cargo = PAL_IDX_POISON;
  313. }
  314. return 0;
  315. }
  316. static int proc_delete (PAL_HANDLE handle, int access)
  317. {
  318. int shutdown;
  319. switch (access) {
  320. case 0:
  321. shutdown = SHUT_RDWR;
  322. break;
  323. case PAL_DELETE_RD:
  324. shutdown = SHUT_RD;
  325. break;
  326. case PAL_DELETE_WR:
  327. shutdown = SHUT_WR;
  328. break;
  329. default:
  330. return -PAL_ERROR_INVAL;
  331. }
  332. if (access != PAL_DELETE_WR &&
  333. handle->process.stream_in != PAL_IDX_POISON) {
  334. ocall_close(handle->process.stream_in);
  335. handle->process.stream_in = PAL_IDX_POISON;
  336. }
  337. if (access != PAL_DELETE_RD &&
  338. handle->process.stream_out != PAL_IDX_POISON) {
  339. ocall_close(handle->process.stream_out);
  340. handle->process.stream_out = PAL_IDX_POISON;
  341. }
  342. if (handle->process.cargo != PAL_IDX_POISON)
  343. ocall_sock_shutdown(handle->process.cargo, shutdown);
  344. return 0;
  345. }
  346. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  347. {
  348. if (handle->process.stream_in == PAL_IDX_POISON)
  349. return -PAL_ERROR_BADHANDLE;
  350. int ret = ocall_fionread(handle->process.stream_in);
  351. if (IS_ERR(ret))
  352. return unix_to_pal_error(ERRNO(ret));
  353. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  354. attr->pending_size = ret;
  355. attr->disconnected = HANDLE_HDR(handle)->flags & (ERROR(0)|ERROR(1));
  356. attr->readable = (attr->pending_size > 0);
  357. attr->writable = HANDLE_HDR(handle)->flags & WRITABLE(1);
  358. attr->nonblocking = handle->process.nonblocking;
  359. return 0;
  360. }
  361. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  362. {
  363. if (handle->process.stream_in == PAL_IDX_POISON)
  364. return -PAL_ERROR_BADHANDLE;
  365. if (attr->nonblocking != handle->process.nonblocking) {
  366. int ret = ocall_fsetnonblock(handle->process.stream_in,
  367. handle->process.nonblocking);
  368. if (IS_ERR(ret))
  369. return unix_to_pal_error(ERRNO(ret));
  370. handle->process.nonblocking = attr->nonblocking;
  371. }
  372. return 0;
  373. }
  374. struct handle_ops proc_ops = {
  375. .read = &proc_read,
  376. .write = &proc_write,
  377. .close = &proc_close,
  378. .delete = &proc_delete,
  379. .attrquerybyhdl = &proc_attrquerybyhdl,
  380. .attrsetbyhdl = &proc_attrsetbyhdl,
  381. };