db_process.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. ret = _DkStreamKeyExchange(child, &child->process.session_key);
  237. if (ret < 0)
  238. goto failed;
  239. sgx_sign_data_t sign_data;
  240. ret = generate_sign_data(&child->process.session_key, pal_enclave_state.enclave_id,
  241. &sign_data);
  242. if (ret < 0)
  243. goto failed;
  244. ret = _DkStreamReportRequest(child, &sign_data, &check_child_mr_enclave);
  245. if (ret < 0)
  246. goto failed;
  247. *handle = child;
  248. return 0;
  249. failed:
  250. free(child);
  251. return ret;
  252. }
  253. static int check_parent_mr_enclave(PAL_HANDLE parent, sgx_measurement_t* mr_enclave,
  254. struct pal_enclave_state* remote_state) {
  255. __UNUSED(mr_enclave);
  256. sgx_sign_data_t sign_data;
  257. int ret = generate_sign_data(&parent->process.session_key, remote_state->enclave_id,
  258. &sign_data);
  259. if (ret < 0)
  260. return ret;
  261. if (memcmp(&remote_state->enclave_data, &sign_data, sizeof(sign_data)))
  262. return 1;
  263. /* XXX: For now, accept any enclave, but eventually should challenge the parent process */
  264. return 0;
  265. }
  266. int init_child_process (PAL_HANDLE * parent_handle)
  267. {
  268. PAL_HANDLE parent = malloc(HANDLE_SIZE(process));
  269. SET_HANDLE_TYPE(parent, process);
  270. HANDLE_HDR(parent)->flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1);
  271. parent->process.stream = pal_sec.stream_fd;
  272. parent->process.cargo = pal_sec.cargo_fd;
  273. parent->process.pid = pal_sec.ppid;
  274. parent->process.nonblocking = PAL_FALSE;
  275. int ret = _DkStreamKeyExchange(parent, &parent->process.session_key);
  276. if (ret < 0)
  277. return ret;
  278. sgx_sign_data_t sign_data;
  279. ret = generate_sign_data(&parent->process.session_key, pal_enclave_state.enclave_id,
  280. &sign_data);
  281. if (ret < 0)
  282. return ret;
  283. ret = _DkStreamReportRespond(parent, &sign_data, &check_parent_mr_enclave);
  284. if (ret < 0)
  285. return ret;
  286. *parent_handle = parent;
  287. return 0;
  288. }
  289. void print_alloced_pages (void);
  290. noreturn void _DkProcessExit (int exitcode)
  291. {
  292. #if PRINT_ENCLAVE_STAT
  293. print_alloced_pages();
  294. #endif
  295. if (exitcode)
  296. SGX_DBG(DBG_I, "DkProcessExit: Returning exit code %d\n", exitcode);
  297. ocall_exit(exitcode, /*is_exitgroup=*/true);
  298. while (true) {
  299. /* nothing */;
  300. }
  301. }
  302. static int64_t proc_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  303. void * buffer)
  304. {
  305. if (offset)
  306. return -PAL_ERROR_INVAL;
  307. if (count >= (1ULL << (sizeof(unsigned int) * 8)))
  308. return -PAL_ERROR_INVAL;
  309. int bytes = ocall_read(handle->process.stream, buffer, count);
  310. return IS_ERR(bytes) ? unix_to_pal_error(ERRNO(bytes)) : bytes;
  311. }
  312. static int64_t proc_write (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  313. const void * buffer)
  314. {
  315. if (offset)
  316. return -PAL_ERROR_INVAL;
  317. if (count >= (1ULL << (sizeof(unsigned int) * 8)))
  318. return -PAL_ERROR_INVAL;
  319. int bytes = ocall_write(handle->process.stream, buffer, count);
  320. if (IS_ERR(bytes))
  321. return unix_to_pal_error(ERRNO(bytes));
  322. return bytes;
  323. }
  324. static int proc_close (PAL_HANDLE handle)
  325. {
  326. if (handle->process.stream != PAL_IDX_POISON) {
  327. ocall_close(handle->process.stream);
  328. handle->process.stream = PAL_IDX_POISON;
  329. }
  330. if (handle->process.cargo != PAL_IDX_POISON) {
  331. ocall_close(handle->process.cargo);
  332. handle->process.cargo = PAL_IDX_POISON;
  333. }
  334. return 0;
  335. }
  336. static int proc_delete (PAL_HANDLE handle, int access)
  337. {
  338. int shutdown;
  339. switch (access) {
  340. case 0:
  341. shutdown = SHUT_RDWR;
  342. break;
  343. case PAL_DELETE_RD:
  344. shutdown = SHUT_RD;
  345. break;
  346. case PAL_DELETE_WR:
  347. shutdown = SHUT_WR;
  348. break;
  349. default:
  350. return -PAL_ERROR_INVAL;
  351. }
  352. if (handle->process.stream != PAL_IDX_POISON)
  353. ocall_shutdown(handle->process.stream, shutdown);
  354. if (handle->process.cargo != PAL_IDX_POISON)
  355. ocall_shutdown(handle->process.cargo, shutdown);
  356. return 0;
  357. }
  358. static int proc_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  359. int ret;
  360. if (handle->process.stream == PAL_IDX_POISON)
  361. return -PAL_ERROR_BADHANDLE;
  362. attr->handle_type = HANDLE_HDR(handle)->type;
  363. attr->nonblocking = handle->process.nonblocking;
  364. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  365. /* get number of bytes available for reading */
  366. ret = ocall_fionread(handle->process.stream);
  367. if (IS_ERR(ret))
  368. return unix_to_pal_error(ERRNO(ret));
  369. attr->pending_size = ret;
  370. /* query if there is data available for reading */
  371. struct pollfd pfd = {.fd = handle->process.stream, .events = POLLIN | POLLOUT, .revents = 0};
  372. ret = ocall_poll(&pfd, 1, 0);
  373. if (IS_ERR(ret))
  374. return unix_to_pal_error(ERRNO(ret));
  375. attr->readable = ret == 1 && (pfd.revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  376. attr->writable = ret == 1 && (pfd.revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  377. return 0;
  378. }
  379. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  380. {
  381. if (handle->process.stream == PAL_IDX_POISON)
  382. return -PAL_ERROR_BADHANDLE;
  383. if (attr->nonblocking != handle->process.nonblocking) {
  384. int ret = ocall_fsetnonblock(handle->process.stream,
  385. handle->process.nonblocking);
  386. if (IS_ERR(ret))
  387. return unix_to_pal_error(ERRNO(ret));
  388. handle->process.nonblocking = attr->nonblocking;
  389. }
  390. return 0;
  391. }
  392. struct handle_ops proc_ops = {
  393. .read = &proc_read,
  394. .write = &proc_write,
  395. .close = &proc_close,
  396. .delete = &proc_delete,
  397. .attrquerybyhdl = &proc_attrquerybyhdl,
  398. .attrsetbyhdl = &proc_attrsetbyhdl,
  399. };