db_files.c 18 KB

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