db_process.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 "spinlock.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. DEFINE_LIST(trusted_child);
  41. struct trusted_child {
  42. LIST_TYPE(trusted_child) list;
  43. sgx_measurement_t mr_enclave;
  44. char uri[];
  45. };
  46. DEFINE_LISTP(trusted_child);
  47. static LISTP_TYPE(trusted_child) trusted_children = LISTP_INIT;
  48. static spinlock_t trusted_children_lock = INIT_SPINLOCK_UNLOCKED;
  49. int register_trusted_child(const char * uri, const char * mr_enclave_str)
  50. {
  51. struct trusted_child * tc = NULL, * new;
  52. int uri_len = strlen(uri);
  53. spinlock_lock(&trusted_children_lock);
  54. LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
  55. if (!memcmp(tc->uri, uri, uri_len + 1)) {
  56. spinlock_unlock(&trusted_children_lock);
  57. return 0;
  58. }
  59. }
  60. spinlock_unlock(&trusted_children_lock);
  61. new = malloc(sizeof(struct trusted_child) + uri_len);
  62. if (!new)
  63. return -PAL_ERROR_NOMEM;
  64. INIT_LIST_HEAD(new, list);
  65. memcpy(new->uri, uri, uri_len + 1);
  66. char mr_enclave_text[sizeof(sgx_measurement_t) * 2 + 1] = "\0";
  67. size_t nbytes = 0;
  68. for (; nbytes < sizeof(sgx_measurement_t) ; nbytes++) {
  69. char byte1 = mr_enclave_str[nbytes * 2];
  70. char byte2 = mr_enclave_str[nbytes * 2 + 1];
  71. unsigned char val = 0;
  72. if (byte1 == 0 || byte2 == 0) {
  73. break;
  74. }
  75. if (!(byte1 >= '0' && byte1 <= '9') &&
  76. !(byte1 >= 'a' && byte1 <= 'f')) {
  77. break;
  78. }
  79. if (!(byte2 >= '0' && byte2 <= '9') &&
  80. !(byte2 >= 'a' && byte2 <= 'f')) {
  81. break;
  82. }
  83. if (byte1 >= '0' && byte1 <= '9')
  84. val = byte1 - '0';
  85. if (byte1 >= 'a' && byte1 <= 'f')
  86. val = byte1 - 'a' + 10;
  87. val *= 16;
  88. if (byte2 >= '0' && byte2 <= '9')
  89. val += byte2 - '0';
  90. if (byte2 >= 'a' && byte2 <= 'f')
  91. val += byte2 - 'a' + 10;
  92. new->mr_enclave.m[nbytes] = val;
  93. snprintf(mr_enclave_text + nbytes * 2, 3, "%02x", val);
  94. }
  95. if (nbytes < sizeof(sgx_measurement_t)) {
  96. free(new);
  97. return -PAL_ERROR_INVAL;
  98. }
  99. SGX_DBG(DBG_S, "trusted: %s %s\n", mr_enclave_text, new->uri);
  100. spinlock_lock(&trusted_children_lock);
  101. LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
  102. if (!memcmp(tc->uri, uri, uri_len + 1)) {
  103. spinlock_unlock(&trusted_children_lock);
  104. free(new);
  105. return 0;
  106. }
  107. }
  108. LISTP_ADD_TAIL(new, &trusted_children, list);
  109. spinlock_unlock(&trusted_children_lock);
  110. return 0;
  111. }
  112. /*
  113. * For SGX, the creation of a child process requires a clean enclave and a secure channel
  114. * between the parent and child processes (enclaves). The establishment of the secure
  115. * channel must be resilient to a host-level, root-privilege adversary. Such an adversary
  116. * can either create arbitrary enclaves, or intercept the handshake protocol between the
  117. * parent and child enclaves to launch a man-in-the-middle attack.
  118. *
  119. * Prerequisites of a secure channel:
  120. * (1) A session key needs to be shared only between the parent and child enclaves.
  121. *
  122. * See the implementation in _DkStreamKeyExchange().
  123. * When initializing an RPC stream, both ends of the stream needs to use
  124. * Diffie-Hellman to exchange a session key. The key will be used to both identify
  125. * the connection (to prevent man-in-the-middle attack) and for future encryption.
  126. *
  127. * (2) Both the parent and child enclaves need to be proven by the Intel CPU.
  128. *
  129. * See the implementation in _DkStreamReportRequest() and _DkStreamReportRespond().
  130. * The two ends of the RPC stream need to exchange local attestation reports
  131. * signed by the Intel CPUs to prove themselves to be running inside enclaves
  132. * on the same platform. The local attestation reports contain no secret information
  133. * and can be verified cryptographically, and can be sent on an unencrypted channel.
  134. *
  135. * The flow of local attestation is as follows:
  136. * - Parent: Send targetinfo(Parent) to Child
  137. * - Child: Generate report(Child -> Parent) and send to Parent
  138. * - Parent: Verify report(Child -> Parent)
  139. * - Parent: Extract targetinfo(Child) from report(Child -> Parent)
  140. * and then generate report(Parent -> Child)
  141. * - Child: Verify report(Parent -> Child)
  142. *
  143. * (3) Both the parent and child enclaves need to have a white-listed measurement.
  144. *
  145. * See the implementation in check_child_mr_enclave() and check_parent_mr_enclave().
  146. * For a child process, we check if the child's mr_enclave is listed as
  147. * "sgx.trusted_children.xxx = ..." in the manifest.
  148. * For a parent process, we currently don't check its mr_enclave in the child.
  149. * This is a limitation because listing the parent's mr_enclave in the child's
  150. * manifest will change the child's mr_enclave, which then needs to be updated
  151. * in the parent's manifest, and eventually falls into a loop of updating both
  152. * manifest files.
  153. *
  154. * (4) The two parties who create the session key need to be the ones proven by the CPU
  155. * (for preventing man-in-the-middle attacks).
  156. *
  157. * See the implementation in check_child_mr_enclave() and check_parent_mr_enclave().
  158. * The local reports from both sides will contain a MAC, generated by hashing
  159. * the unique enclave ID (a 64-bit integer) using AES-CMAC with the session key.
  160. * Because both the enclave ID and the session key are randomly created for each
  161. * enclave, no report can be reused even from an enclave with the same mr_enclave.
  162. */
  163. struct proc_data {
  164. sgx_mac_t eid_mac;
  165. };
  166. static int generate_sign_data(const PAL_SESSION_KEY* session_key, uint64_t enclave_id,
  167. sgx_sign_data_t* sign_data) {
  168. struct proc_data data;
  169. int ret = lib_AESCMAC((uint8_t*)session_key, sizeof(*session_key),
  170. (uint8_t*)&enclave_id, sizeof(enclave_id),
  171. (uint8_t*)&data.eid_mac, sizeof(data.eid_mac));
  172. if (ret < 0)
  173. return ret;
  174. SGX_DBG(DBG_P|DBG_S, "Enclave identifier: %016lx -> %s\n", enclave_id,
  175. ALLOCA_BYTES2HEXSTR(data.eid_mac));
  176. /* Copy proc_data into sgx_sign_data_t */
  177. assert(sizeof(data) <= sizeof(*sign_data));
  178. memset(sign_data, 0, sizeof(*sign_data));
  179. memcpy(sign_data, &data, sizeof(data));
  180. return 0;
  181. }
  182. static int check_child_mr_enclave(PAL_HANDLE child, sgx_measurement_t* mr_enclave,
  183. struct pal_enclave_state* remote_state) {
  184. /* the process must be a clean process */
  185. if (remote_state->enclave_flags & PAL_ENCLAVE_INITIALIZED)
  186. return 1;
  187. sgx_sign_data_t sign_data;
  188. int ret = generate_sign_data(&child->process.session_key, remote_state->enclave_id,
  189. &sign_data);
  190. if (ret < 0)
  191. return ret;
  192. /* must make sure the signer of the report is also the owner of the key,
  193. in order to prevent man-in-the-middle attack */
  194. if (memcmp(&remote_state->enclave_data, &sign_data, sizeof(sign_data)))
  195. return 1;
  196. /* Always accept the same mr_enclave as child process */
  197. if (!memcmp(mr_enclave, &pal_sec.mr_enclave, sizeof(sgx_measurement_t))) {
  198. SGX_DBG(DBG_S, "trusted child: <forked>\n");
  199. return 0;
  200. }
  201. struct trusted_child * tc;
  202. spinlock_lock(&trusted_children_lock);
  203. /* Try to find a matching mr_enclave from the manifest */
  204. LISTP_FOR_EACH_ENTRY(tc, &trusted_children, list) {
  205. if (!memcmp(mr_enclave, &tc->mr_enclave, sizeof(sgx_measurement_t))) {
  206. spinlock_unlock(&trusted_children_lock);
  207. SGX_DBG(DBG_S, "trusted child: %s\n", tc->uri);
  208. return 0;
  209. }
  210. }
  211. spinlock_unlock(&trusted_children_lock);
  212. return 1;
  213. }
  214. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri, const char ** args)
  215. {
  216. /* only access creating process with regular file */
  217. if (!strstartswith_static(uri, URI_PREFIX_FILE))
  218. return -PAL_ERROR_INVAL;
  219. unsigned int child_pid;
  220. int stream_fd;
  221. int cargo_fd;
  222. int nargs = 0, ret;
  223. if (args)
  224. for (const char ** a = args ; *a ; a++)
  225. nargs++;
  226. ret = ocall_create_process(uri, nargs, args, &stream_fd, &cargo_fd, &child_pid);
  227. if (ret < 0)
  228. return ret;
  229. PAL_HANDLE child = malloc(HANDLE_SIZE(process));
  230. SET_HANDLE_TYPE(child, process);
  231. HANDLE_HDR(child)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1);
  232. child->process.stream = stream_fd;
  233. child->process.cargo = cargo_fd;
  234. child->process.pid = child_pid;
  235. child->process.nonblocking = PAL_FALSE;
  236. child->process.ssl_ctx = NULL;
  237. ret = _DkStreamKeyExchange(child, &child->process.session_key);
  238. if (ret < 0)
  239. goto failed;
  240. sgx_sign_data_t sign_data;
  241. ret = generate_sign_data(&child->process.session_key, pal_enclave_state.enclave_id,
  242. &sign_data);
  243. if (ret < 0)
  244. goto failed;
  245. ret = _DkStreamReportRequest(child, &sign_data, &check_child_mr_enclave);
  246. if (ret < 0)
  247. goto failed;
  248. ret = _DkStreamSecureInit(child, /*is_server=*/true, &child->process.session_key,
  249. (LIB_SSL_CONTEXT**)&child->process.ssl_ctx);
  250. if (ret < 0)
  251. goto failed;
  252. *handle = child;
  253. return 0;
  254. failed:
  255. free(child);
  256. return ret;
  257. }
  258. static int check_parent_mr_enclave(PAL_HANDLE parent, sgx_measurement_t* mr_enclave,
  259. struct pal_enclave_state* remote_state) {
  260. __UNUSED(mr_enclave);
  261. sgx_sign_data_t sign_data;
  262. int ret = generate_sign_data(&parent->process.session_key, remote_state->enclave_id,
  263. &sign_data);
  264. if (ret < 0)
  265. return ret;
  266. if (memcmp(&remote_state->enclave_data, &sign_data, sizeof(sign_data)))
  267. return 1;
  268. /* XXX: For now, accept any enclave, but eventually should challenge the parent process */
  269. return 0;
  270. }
  271. int init_child_process (PAL_HANDLE * parent_handle)
  272. {
  273. PAL_HANDLE parent = malloc(HANDLE_SIZE(process));
  274. SET_HANDLE_TYPE(parent, process);
  275. HANDLE_HDR(parent)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1);
  276. parent->process.stream = pal_sec.stream_fd;
  277. parent->process.cargo = pal_sec.cargo_fd;
  278. parent->process.pid = pal_sec.ppid;
  279. parent->process.nonblocking = PAL_FALSE;
  280. parent->process.ssl_ctx = NULL;
  281. int ret = _DkStreamKeyExchange(parent, &parent->process.session_key);
  282. if (ret < 0)
  283. return ret;
  284. sgx_sign_data_t sign_data;
  285. ret = generate_sign_data(&parent->process.session_key, pal_enclave_state.enclave_id,
  286. &sign_data);
  287. if (ret < 0)
  288. return ret;
  289. ret = _DkStreamReportRespond(parent, &sign_data, &check_parent_mr_enclave);
  290. if (ret < 0)
  291. return ret;
  292. ret = _DkStreamSecureInit(parent, /*is_server=*/false, &parent->process.session_key,
  293. (LIB_SSL_CONTEXT**)&parent->process.ssl_ctx);
  294. if (ret < 0)
  295. return ret;
  296. *parent_handle = parent;
  297. return 0;
  298. }
  299. void print_alloced_pages (void);
  300. noreturn void _DkProcessExit (int exitcode)
  301. {
  302. #if PRINT_ENCLAVE_STAT
  303. print_alloced_pages();
  304. #endif
  305. if (exitcode)
  306. SGX_DBG(DBG_I, "DkProcessExit: Returning exit code %d\n", exitcode);
  307. ocall_exit(exitcode, /*is_exitgroup=*/true);
  308. while (true) {
  309. /* nothing */;
  310. }
  311. }
  312. static int64_t proc_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  313. void * buffer)
  314. {
  315. if (offset)
  316. return -PAL_ERROR_INVAL;
  317. if (count != (uint32_t)count)
  318. return -PAL_ERROR_INVAL;
  319. ssize_t bytes;
  320. if (handle->process.ssl_ctx) {
  321. bytes = _DkStreamSecureRead(handle->process.ssl_ctx, buffer, count);
  322. } else {
  323. bytes = ocall_read(handle->process.stream, buffer, count);
  324. bytes = IS_ERR(bytes) ? unix_to_pal_error(ERRNO(bytes)) : bytes;
  325. }
  326. return bytes;
  327. }
  328. static int64_t proc_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  329. const void * buffer)
  330. {
  331. if (offset)
  332. return -PAL_ERROR_INVAL;
  333. if (count != (uint32_t)count)
  334. return -PAL_ERROR_INVAL;
  335. ssize_t bytes;
  336. if (handle->process.ssl_ctx) {
  337. bytes = _DkStreamSecureWrite(handle->process.ssl_ctx, buffer, count);
  338. } else {
  339. bytes = ocall_write(handle->process.stream, buffer, count);
  340. bytes = IS_ERR(bytes) ? unix_to_pal_error(ERRNO(bytes)) : bytes;
  341. }
  342. return bytes;
  343. }
  344. static int proc_close (PAL_HANDLE handle)
  345. {
  346. if (handle->process.stream != PAL_IDX_POISON) {
  347. ocall_close(handle->process.stream);
  348. handle->process.stream = PAL_IDX_POISON;
  349. }
  350. if (handle->process.cargo != PAL_IDX_POISON) {
  351. ocall_close(handle->process.cargo);
  352. handle->process.cargo = PAL_IDX_POISON;
  353. }
  354. if (handle->process.ssl_ctx) {
  355. _DkStreamSecureFree((LIB_SSL_CONTEXT*)handle->process.ssl_ctx);
  356. handle->process.ssl_ctx = NULL;
  357. }
  358. return 0;
  359. }
  360. static int proc_delete (PAL_HANDLE handle, int access)
  361. {
  362. int shutdown;
  363. switch (access) {
  364. case 0:
  365. shutdown = SHUT_RDWR;
  366. break;
  367. case PAL_DELETE_RD:
  368. shutdown = SHUT_RD;
  369. break;
  370. case PAL_DELETE_WR:
  371. shutdown = SHUT_WR;
  372. break;
  373. default:
  374. return -PAL_ERROR_INVAL;
  375. }
  376. if (handle->process.stream != PAL_IDX_POISON)
  377. ocall_shutdown(handle->process.stream, shutdown);
  378. if (handle->process.cargo != PAL_IDX_POISON)
  379. ocall_shutdown(handle->process.cargo, shutdown);
  380. return 0;
  381. }
  382. static int proc_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  383. int ret;
  384. if (handle->process.stream == PAL_IDX_POISON)
  385. return -PAL_ERROR_BADHANDLE;
  386. attr->handle_type = HANDLE_HDR(handle)->type;
  387. attr->nonblocking = handle->process.nonblocking;
  388. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  389. attr->secure = handle->process.ssl_ctx ? PAL_TRUE : PAL_FALSE;
  390. /* get number of bytes available for reading */
  391. ret = ocall_fionread(handle->process.stream);
  392. if (IS_ERR(ret))
  393. return unix_to_pal_error(ERRNO(ret));
  394. attr->pending_size = ret;
  395. /* query if there is data available for reading */
  396. struct pollfd pfd = {.fd = handle->process.stream, .events = POLLIN | POLLOUT, .revents = 0};
  397. ret = ocall_poll(&pfd, 1, 0);
  398. if (IS_ERR(ret))
  399. return unix_to_pal_error(ERRNO(ret));
  400. attr->readable = ret == 1 && (pfd.revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  401. attr->writable = ret == 1 && (pfd.revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  402. return 0;
  403. }
  404. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  405. {
  406. if (handle->process.stream == PAL_IDX_POISON)
  407. return -PAL_ERROR_BADHANDLE;
  408. if (attr->nonblocking != handle->process.nonblocking) {
  409. int ret = ocall_fsetnonblock(handle->process.stream,
  410. handle->process.nonblocking);
  411. if (IS_ERR(ret))
  412. return unix_to_pal_error(ERRNO(ret));
  413. handle->process.nonblocking = attr->nonblocking;
  414. }
  415. if (!attr->secure && handle->process.ssl_ctx) {
  416. /* remove TLS protection from process.stream */
  417. _DkStreamSecureFree((LIB_SSL_CONTEXT*)handle->process.ssl_ctx);
  418. handle->process.ssl_ctx = NULL;
  419. } else if (attr->secure && !handle->process.ssl_ctx) {
  420. /* adding TLS protection for process.stream is not yet implemented */
  421. SGX_DBG(DBG_E, "Securing a non-secure process handle is not supported!\n");
  422. return -PAL_ERROR_NOTSUPPORT;
  423. }
  424. return 0;
  425. }
  426. struct handle_ops proc_ops = {
  427. .read = &proc_read,
  428. .write = &proc_write,
  429. .close = &proc_close,
  430. .delete = &proc_delete,
  431. .attrquerybyhdl = &proc_attrquerybyhdl,
  432. .attrsetbyhdl = &proc_attrsetbyhdl,
  433. };