db_files.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_files.c
  15. *
  16. * This file contains operands to handle streams with URIs that start with
  17. * "file:" or "dir:".
  18. */
  19. #include <linux/types.h>
  20. #include "api.h"
  21. #include "assert.h"
  22. #include "pal.h"
  23. #include "pal_debug.h"
  24. #include "pal_defs.h"
  25. #include "pal_error.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_linux_defs.h"
  29. #include "pal_linux_error.h"
  30. typedef __kernel_pid_t pid_t;
  31. #undef __GLIBC__
  32. #include <asm/fcntl.h>
  33. #include <asm/stat.h>
  34. #include <linux/fs.h>
  35. #include <linux/stat.h>
  36. #include "enclave_pages.h"
  37. /* 'open' operation for file streams */
  38. static int file_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  39. int create, int options) {
  40. if (strcmp_static(type, URI_TYPE_FILE))
  41. return -PAL_ERROR_INVAL;
  42. /* try to do the real open */
  43. int fd = ocall_open(uri, access | create | options, share);
  44. if (IS_ERR(fd))
  45. return unix_to_pal_error(ERRNO(fd));
  46. /* if try_create_path succeeded, prepare for the file handle */
  47. size_t len = strlen(uri) + 1;
  48. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len);
  49. SET_HANDLE_TYPE(hdl, file);
  50. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0);
  51. hdl->file.fd = fd;
  52. char* path = (void*)hdl + HANDLE_SIZE(file);
  53. int ret;
  54. if ((ret = get_norm_path(uri, path, &len)) < 0) {
  55. SGX_DBG(DBG_E, "Could not normalize path (%s): %s\n", uri, pal_strerror(ret));
  56. free(hdl);
  57. return ret;
  58. }
  59. hdl->file.realpath = (PAL_STR)path;
  60. sgx_stub_t* stubs;
  61. uint64_t total;
  62. ret = load_trusted_file(hdl, &stubs, &total, create);
  63. if (ret < 0) {
  64. SGX_DBG(DBG_E,
  65. "Accessing file:%s is denied. (%s) "
  66. "This file is not trusted or allowed.\n",
  67. hdl->file.realpath, pal_strerror(ret));
  68. free(hdl);
  69. return ret;
  70. }
  71. hdl->file.stubs = (PAL_PTR)stubs;
  72. hdl->file.total = total;
  73. if (hdl->file.stubs && hdl->file.total) {
  74. /* case of trusted file: mmap the whole file in untrusted memory for future reads/writes */
  75. ret = ocall_mmap_untrusted(hdl->file.fd, 0, hdl->file.total, PROT_READ, &hdl->file.umem);
  76. if (IS_ERR(ret)) {
  77. /* note that we don't free stubs because they are re-used in same trusted file */
  78. free(hdl);
  79. return unix_to_pal_error(ERRNO(ret));
  80. }
  81. }
  82. *handle = hdl;
  83. return 0;
  84. }
  85. /* 'read' operation for file streams. */
  86. static int64_t file_read(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buffer) {
  87. int64_t ret;
  88. sgx_stub_t* stubs = (sgx_stub_t*)handle->file.stubs;
  89. if (!stubs) {
  90. ret = ocall_pread(handle->file.fd, buffer, count, offset);
  91. if (IS_ERR(ret))
  92. return unix_to_pal_error(ERRNO(ret));
  93. return ret;
  94. }
  95. /* case of trusted file: already mmaped in umem, copy from there and verify hash */
  96. uint64_t total = handle->file.total;
  97. if (offset >= total)
  98. return 0;
  99. uint64_t end = (offset + count > total) ? total : offset + count;
  100. uint64_t map_start = ALIGN_DOWN(offset, TRUSTED_STUB_SIZE);
  101. uint64_t map_end = ALIGN_UP(end, TRUSTED_STUB_SIZE);
  102. if (map_end > total)
  103. map_end = ALLOC_ALIGN_UP(total);
  104. ret = copy_and_verify_trusted_file(handle->file.realpath, handle->file.umem + map_start,
  105. map_start, map_end, buffer, offset, end - offset, stubs, total);
  106. if (ret < 0)
  107. return ret;
  108. return end - offset;
  109. }
  110. /* 'write' operation for file streams. */
  111. static int64_t file_write(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer) {
  112. int64_t ret;
  113. sgx_stub_t* stubs = (sgx_stub_t*)handle->file.stubs;
  114. if (!stubs) {
  115. ret = ocall_pwrite(handle->file.fd, buffer, count, offset);
  116. if (IS_ERR(ret))
  117. return unix_to_pal_error(ERRNO(ret));
  118. return ret;
  119. }
  120. /* case of trusted file: disallow writing completely */
  121. SGX_DBG(DBG_E, "Writing to a trusted file (%s) is disallowed!\n", handle->file.realpath);
  122. return -PAL_ERROR_DENIED;
  123. }
  124. /* 'close' operation for file streams. In this case, it will only
  125. close the file without deleting it. */
  126. static int file_close(PAL_HANDLE handle) {
  127. int fd = handle->file.fd;
  128. if (handle->file.stubs && handle->file.total) {
  129. /* case of trusted file: the whole file was mmapped in untrusted memory */
  130. ocall_munmap_untrusted(handle->file.umem, handle->file.total);
  131. }
  132. ocall_close(fd);
  133. /* initial realpath is part of handle object and will be freed with it */
  134. if (handle->file.realpath && handle->file.realpath != (void*)handle + HANDLE_SIZE(file))
  135. free((void*)handle->file.realpath);
  136. return 0;
  137. }
  138. /* 'delete' operation for file streams. It will actually delete
  139. the file if we can successfully close it. */
  140. static int file_delete(PAL_HANDLE handle, int access) {
  141. if (access)
  142. return -PAL_ERROR_INVAL;
  143. int ret = ocall_delete(handle->file.realpath);
  144. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  145. }
  146. /* 'map' operation for file stream. */
  147. static int file_map(PAL_HANDLE handle, void** addr, int prot, uint64_t offset, uint64_t size) {
  148. sgx_stub_t* stubs = (sgx_stub_t*)handle->file.stubs;
  149. uint64_t total = handle->file.total;
  150. void* mem = *addr;
  151. void* umem;
  152. int ret;
  153. /*
  154. * If the file is listed in the manifest as an "allowed" file,
  155. * we allow mapping the file outside the enclave, if the library OS
  156. * does not request a specific address.
  157. */
  158. if (!mem && !stubs && !(prot & PAL_PROT_WRITECOPY)) {
  159. ret = ocall_mmap_untrusted(handle->file.fd, offset, size, HOST_PROT(prot), &mem);
  160. if (!IS_ERR(ret))
  161. *addr = mem;
  162. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  163. }
  164. if (!(prot & PAL_PROT_WRITECOPY) && (prot & PAL_PROT_WRITE)) {
  165. SGX_DBG(DBG_E,
  166. "file_map does not currently support writable pass-through mappings on SGX. You "
  167. "may add the PAL_PROT_WRITECOPY (MAP_PRIVATE) flag to your file mapping to keep "
  168. "the writes inside the enclave but they won't be reflected outside of the "
  169. "enclave.\n");
  170. return -PAL_ERROR_DENIED;
  171. }
  172. mem = get_reserved_pages(mem, size);
  173. if (!mem)
  174. return -PAL_ERROR_NOMEM;
  175. uint64_t end = (offset + size > total) ? total : offset + size;
  176. uint64_t map_start, map_end;
  177. if (stubs) {
  178. map_start = ALIGN_DOWN(offset, TRUSTED_STUB_SIZE);
  179. map_end = ALIGN_UP(end, TRUSTED_STUB_SIZE);
  180. } else {
  181. map_start = ALLOC_ALIGN_DOWN(offset);
  182. map_end = ALLOC_ALIGN_UP(end);
  183. }
  184. ret = ocall_mmap_untrusted(handle->file.fd, map_start, map_end - map_start, PROT_READ, &umem);
  185. if (IS_ERR(ret)) {
  186. SGX_DBG(DBG_E, "file_map - ocall returned %d\n", ret);
  187. return unix_to_pal_error(ERRNO(ret));
  188. }
  189. if (stubs) {
  190. ret = copy_and_verify_trusted_file(handle->file.realpath, umem, map_start, map_end, mem,
  191. offset, end - offset, stubs, total);
  192. if (ret < 0) {
  193. SGX_DBG(DBG_E, "file_map - verify trusted returned %d\n", ret);
  194. ocall_munmap_untrusted(umem, map_end - map_start);
  195. return ret;
  196. }
  197. } else {
  198. memcpy(mem, umem + (offset - map_start), end - offset);
  199. }
  200. ocall_munmap_untrusted(umem, map_end - map_start);
  201. *addr = mem;
  202. return 0;
  203. }
  204. /* 'setlength' operation for file stream. */
  205. static int64_t file_setlength(PAL_HANDLE handle, uint64_t length) {
  206. int ret = ocall_ftruncate(handle->file.fd, length);
  207. if (IS_ERR(ret))
  208. return unix_to_pal_error(ERRNO(ret));
  209. handle->file.total = length;
  210. return (int64_t)length;
  211. }
  212. /* 'flush' operation for file stream. */
  213. static int file_flush(PAL_HANDLE handle) {
  214. ocall_fsync(handle->file.fd);
  215. return 0;
  216. }
  217. static inline int file_stat_type(struct stat* stat) {
  218. if (S_ISREG(stat->st_mode))
  219. return pal_type_file;
  220. if (S_ISDIR(stat->st_mode))
  221. return pal_type_dir;
  222. if (S_ISCHR(stat->st_mode))
  223. return pal_type_dev;
  224. if (S_ISFIFO(stat->st_mode))
  225. return pal_type_pipe;
  226. if (S_ISSOCK(stat->st_mode))
  227. return pal_type_dev;
  228. return 0;
  229. }
  230. /* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
  231. static inline void file_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat) {
  232. attr->handle_type = file_stat_type(stat);
  233. attr->disconnected = PAL_FALSE;
  234. attr->nonblocking = PAL_FALSE;
  235. attr->readable = stataccess(stat, ACCESS_R);
  236. attr->writable = stataccess(stat, ACCESS_W);
  237. attr->runnable = stataccess(stat, ACCESS_X);
  238. attr->share_flags = stat->st_mode;
  239. attr->pending_size = stat->st_size;
  240. }
  241. /* 'attrquery' operation for file streams */
  242. static int file_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
  243. if (strcmp_static(type, URI_TYPE_FILE) && strcmp_static(type, URI_TYPE_DIR))
  244. return -PAL_ERROR_INVAL;
  245. /* try to do the real open */
  246. int fd = ocall_open(uri, 0, 0);
  247. if (IS_ERR(fd))
  248. return unix_to_pal_error(ERRNO(fd));
  249. struct stat stat_buf;
  250. int ret = ocall_fstat(fd, &stat_buf);
  251. ocall_close(fd);
  252. /* if it failed, return the right error code */
  253. if (IS_ERR(ret))
  254. return unix_to_pal_error(ERRNO(ret));
  255. file_attrcopy(attr, &stat_buf);
  256. return 0;
  257. }
  258. /* 'attrquerybyhdl' operation for file streams */
  259. static int file_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  260. int fd = handle->file.fd;
  261. struct stat stat_buf;
  262. int ret = ocall_fstat(fd, &stat_buf);
  263. if (IS_ERR(ret))
  264. return unix_to_pal_error(ERRNO(ret));
  265. file_attrcopy(attr, &stat_buf);
  266. return 0;
  267. }
  268. static int file_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  269. int fd = handle->file.fd;
  270. int ret = ocall_fchmod(fd, attr->share_flags | 0600);
  271. if (IS_ERR(ret))
  272. return unix_to_pal_error(ERRNO(ret));
  273. return 0;
  274. }
  275. static int file_rename(PAL_HANDLE handle, const char* type, const char* uri) {
  276. if (strcmp_static(type, URI_TYPE_FILE))
  277. return -PAL_ERROR_INVAL;
  278. char* tmp = strdup(uri);
  279. if (!tmp)
  280. return -PAL_ERROR_NOMEM;
  281. int ret = ocall_rename(handle->file.realpath, uri);
  282. if (IS_ERR(ret)) {
  283. free(tmp);
  284. return unix_to_pal_error(ERRNO(ret));
  285. }
  286. /* initial realpath is part of handle object and will be freed with it */
  287. if (handle->file.realpath && handle->file.realpath != (void*)handle + HANDLE_SIZE(file)) {
  288. free((void*)handle->file.realpath);
  289. }
  290. handle->file.realpath = tmp;
  291. return 0;
  292. }
  293. static int file_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  294. if (!handle->file.realpath)
  295. return 0;
  296. int len = strlen(handle->file.realpath);
  297. char* tmp = strcpy_static(buffer, URI_PREFIX_FILE, count);
  298. if (!tmp || buffer + count < tmp + len + 1)
  299. return -PAL_ERROR_TOOLONG;
  300. memcpy(tmp, handle->file.realpath, len + 1);
  301. return tmp + len - buffer;
  302. }
  303. const char* file_getrealpath(PAL_HANDLE handle) {
  304. return handle->file.realpath;
  305. }
  306. struct handle_ops file_ops = {
  307. .getname = &file_getname,
  308. .getrealpath = &file_getrealpath,
  309. .open = &file_open,
  310. .read = &file_read,
  311. .write = &file_write,
  312. .close = &file_close,
  313. .delete = &file_delete,
  314. .map = &file_map,
  315. .setlength = &file_setlength,
  316. .flush = &file_flush,
  317. .attrquery = &file_attrquery,
  318. .attrquerybyhdl = &file_attrquerybyhdl,
  319. .attrsetbyhdl = &file_attrsetbyhdl,
  320. .rename = &file_rename,
  321. };
  322. /* 'open' operation for directory stream. Directory stream does not have a
  323. specific type prefix, its URI looks the same file streams, plus it
  324. ended with slashes. dir_open will be called by file_open. */
  325. static int dir_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  326. int create, int options) {
  327. if (strcmp_static(type, URI_TYPE_DIR))
  328. return -PAL_ERROR_INVAL;
  329. if (!WITHIN_MASK(access, PAL_ACCESS_MASK))
  330. return -PAL_ERROR_INVAL;
  331. int ret;
  332. if (create & PAL_CREATE_TRY) {
  333. ret = ocall_mkdir(uri, share);
  334. if (IS_ERR(ret) && ERRNO(ret) == EEXIST && create & PAL_CREATE_ALWAYS)
  335. return -PAL_ERROR_STREAMEXIST;
  336. }
  337. ret = ocall_open(uri, O_DIRECTORY | options, 0);
  338. if (IS_ERR(ret))
  339. return unix_to_pal_error(ERRNO(ret));
  340. int len = strlen(uri);
  341. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  342. SET_HANDLE_TYPE(hdl, dir);
  343. HANDLE_HDR(hdl)->flags |= RFD(0);
  344. hdl->dir.fd = ret;
  345. char* path = (void*)hdl + HANDLE_SIZE(dir);
  346. memcpy(path, uri, len + 1);
  347. hdl->dir.realpath = (PAL_STR)path;
  348. hdl->dir.buf = (PAL_PTR)NULL;
  349. hdl->dir.ptr = (PAL_PTR)NULL;
  350. hdl->dir.end = (PAL_PTR)NULL;
  351. hdl->dir.endofstream = PAL_FALSE;
  352. *handle = hdl;
  353. return 0;
  354. }
  355. #define DIRBUF_SIZE 1024
  356. static inline bool is_dot_or_dotdot(const char* name) {
  357. return (name[0] == '.' && !name[1]) || (name[0] == '.' && name[1] == '.' && !name[2]);
  358. }
  359. /* 'read' operation for directory stream. Directory stream will not
  360. need a 'write' operation. */
  361. static int64_t dir_read(PAL_HANDLE handle, uint64_t offset, size_t count, void* _buf) {
  362. size_t bytes_written = 0;
  363. char* buf = (char*)_buf;
  364. if (offset) {
  365. return -PAL_ERROR_INVAL;
  366. }
  367. if (handle->dir.endofstream == PAL_TRUE) {
  368. return -PAL_ERROR_ENDOFSTREAM;
  369. }
  370. while (1) {
  371. while ((char*)handle->dir.ptr < (char*)handle->dir.end) {
  372. struct linux_dirent64* dirent = (struct linux_dirent64*)handle->dir.ptr;
  373. if (is_dot_or_dotdot(dirent->d_name)) {
  374. goto skip;
  375. }
  376. bool is_dir = dirent->d_type == DT_DIR;
  377. size_t len = strlen(dirent->d_name);
  378. if (len + 1 + (is_dir ? 1 : 0) > count) {
  379. goto out;
  380. }
  381. memcpy(buf, dirent->d_name, len);
  382. if (is_dir) {
  383. buf[len++] = '/';
  384. }
  385. buf[len++] = '\0';
  386. buf += len;
  387. bytes_written += len;
  388. count -= len;
  389. skip:
  390. handle->dir.ptr = (char*)handle->dir.ptr + dirent->d_reclen;
  391. }
  392. if (!count) {
  393. /* No space left, returning */
  394. goto out;
  395. }
  396. if (!handle->dir.buf) {
  397. handle->dir.buf = (PAL_PTR)malloc(DIRBUF_SIZE);
  398. if (!handle->dir.buf) {
  399. return -PAL_ERROR_NOMEM;
  400. }
  401. }
  402. int size = ocall_getdents(handle->dir.fd, handle->dir.buf, DIRBUF_SIZE);
  403. if (IS_ERR(size)) {
  404. /*
  405. * If something was written just return that and pretend no error
  406. * was seen - it will be caught next time.
  407. */
  408. if (bytes_written) {
  409. return bytes_written;
  410. }
  411. return unix_to_pal_error(ERRNO(size));
  412. }
  413. if (!size) {
  414. handle->dir.endofstream = PAL_TRUE;
  415. goto out;
  416. }
  417. handle->dir.ptr = handle->dir.buf;
  418. handle->dir.end = (char*)handle->dir.buf + size;
  419. }
  420. out:
  421. return (int64_t)bytes_written ?: -PAL_ERROR_ENDOFSTREAM;
  422. }
  423. /* 'close' operation of directory streams */
  424. static int dir_close(PAL_HANDLE handle) {
  425. int fd = handle->dir.fd;
  426. ocall_close(fd);
  427. if (handle->dir.buf) {
  428. free((void*)handle->dir.buf);
  429. handle->dir.buf = handle->dir.ptr = handle->dir.end = (PAL_PTR)NULL;
  430. }
  431. /* initial realpath is part of handle object and will be freed with it */
  432. if (handle->dir.realpath && handle->dir.realpath != (void*)handle + HANDLE_SIZE(dir))
  433. free((void*)handle->dir.realpath);
  434. return 0;
  435. }
  436. /* 'delete' operation of directoy streams */
  437. static int dir_delete(PAL_HANDLE handle, int access) {
  438. if (access)
  439. return -PAL_ERROR_INVAL;
  440. int ret = dir_close(handle);
  441. if (ret < 0)
  442. return ret;
  443. ret = ocall_delete(handle->dir.realpath);
  444. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  445. }
  446. static int dir_rename(PAL_HANDLE handle, const char* type, const char* uri) {
  447. if (strcmp_static(type, URI_TYPE_DIR))
  448. return -PAL_ERROR_INVAL;
  449. char* tmp = strdup(uri);
  450. if (!tmp)
  451. return -PAL_ERROR_NOMEM;
  452. int ret = ocall_rename(handle->dir.realpath, uri);
  453. if (IS_ERR(ret)) {
  454. free(tmp);
  455. return unix_to_pal_error(ERRNO(ret));
  456. }
  457. /* initial realpath is part of handle object and will be freed with it */
  458. if (handle->dir.realpath && handle->dir.realpath != (void*)handle + HANDLE_SIZE(dir)) {
  459. free((void*)handle->dir.realpath);
  460. }
  461. handle->dir.realpath = tmp;
  462. return 0;
  463. }
  464. static int dir_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  465. if (!handle->dir.realpath)
  466. return 0;
  467. size_t len = strlen(handle->dir.realpath);
  468. char* tmp = strcpy_static(buffer, URI_PREFIX_DIR, count);
  469. if (!tmp || buffer + count < tmp + len + 1)
  470. return -PAL_ERROR_TOOLONG;
  471. memcpy(tmp, handle->dir.realpath, len + 1);
  472. return tmp + len - buffer;
  473. if (len + 6 >= count)
  474. return -PAL_ERROR_TOOLONG;
  475. }
  476. static const char* dir_getrealpath(PAL_HANDLE handle) {
  477. return handle->dir.realpath;
  478. }
  479. struct handle_ops dir_ops = {
  480. .getname = &dir_getname,
  481. .getrealpath = &dir_getrealpath,
  482. .open = &dir_open,
  483. .read = &dir_read,
  484. .close = &dir_close,
  485. .delete = &dir_delete,
  486. .attrquery = &file_attrquery,
  487. .attrquerybyhdl = &file_attrquerybyhdl,
  488. .attrsetbyhdl = &file_attrsetbyhdl,
  489. .rename = &dir_rename,
  490. };