db_files.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_files.c
  17. *
  18. * This file contains operands to handle streams with URIs that start with
  19. * "file:" or "dir:".
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_linux_error.h"
  27. #include "pal_debug.h"
  28. #include "pal_error.h"
  29. #include "api.h"
  30. #include <linux/types.h>
  31. typedef __kernel_pid_t pid_t;
  32. #undef __GLIBC__
  33. #include <linux/stat.h>
  34. #include <linux/fs.h>
  35. #include <asm/stat.h>
  36. #include <asm/fcntl.h>
  37. #include "enclave_pages.h"
  38. /* 'open' operation for file streams */
  39. static int file_open (PAL_HANDLE * handle, const char * type, const char * uri,
  40. int access, int share, int create, int options)
  41. {
  42. if (!strcmp_static(type, "file"))
  43. return -PAL_ERROR_INVAL;
  44. /* try to do the real open */
  45. int fd = ocall_open(uri, access|create|options, share);
  46. if (IS_ERR(fd))
  47. return unix_to_pal_error(ERRNO(fd));
  48. /* if try_create_path succeeded, prepare for the file handle */
  49. int len = strlen(uri);
  50. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len + 1);
  51. SET_HANDLE_TYPE(hdl, file);
  52. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(0)|WRITABLE(0);
  53. hdl->file.fd = fd;
  54. hdl->file.append = 0;
  55. hdl->file.pass = 0;
  56. char * path = (void *) hdl + HANDLE_SIZE(file);
  57. get_norm_path(uri, path, 0, len + 1);
  58. hdl->file.realpath = (PAL_STR) path;
  59. sgx_stub_t * stubs;
  60. uint64_t total;
  61. int ret = load_trusted_file(hdl, &stubs, &total, create);
  62. if (ret < 0) {
  63. SGX_DBG(DBG_E, "Accessing file:%s is denied. (%s) "
  64. "This file is not trusted or allowed.\n", hdl->file.realpath,
  65. PAL_STRERROR(-ret));
  66. free(hdl);
  67. return ret;
  68. }
  69. hdl->file.stubs = (PAL_PTR) stubs;
  70. hdl->file.total = total;
  71. *handle = hdl;
  72. return 0;
  73. }
  74. /* 'read' operation for file streams. */
  75. static int64_t file_read (PAL_HANDLE handle, uint64_t offset, uint64_t count,
  76. void * buffer)
  77. {
  78. sgx_stub_t * stubs = (sgx_stub_t *) handle->file.stubs;
  79. unsigned int total = handle->file.total;
  80. int ret;
  81. if (offset >= total)
  82. return 0;
  83. uint64_t end = (offset + count > total) ? total : offset + count;
  84. uint64_t map_start, map_end;
  85. if (stubs) {
  86. map_start = offset & ~(TRUSTED_STUB_SIZE - 1);
  87. map_end = (end + TRUSTED_STUB_SIZE - 1) & ~(TRUSTED_STUB_SIZE - 1);
  88. /* Don't go past the end of file with the stub map either */
  89. if (map_end > total)
  90. map_end = ALLOC_ALIGNUP(total);
  91. } else {
  92. map_start = ALLOC_ALIGNDOWN(offset);
  93. map_end = ALLOC_ALIGNUP(end);
  94. }
  95. void * umem;
  96. ret = ocall_map_untrusted(handle->file.fd, map_start,
  97. map_end - map_start, PROT_READ, &umem);
  98. if (IS_ERR(ret))
  99. return unix_to_pal_error(ERRNO(ret));
  100. if (stubs) {
  101. ret = copy_and_verify_trusted_file(handle->file.realpath, umem,
  102. map_start, map_end,
  103. buffer, offset, end - offset,
  104. stubs, total);
  105. if (ret < 0) {
  106. ocall_unmap_untrusted(umem, map_end - map_start);
  107. return ret;
  108. }
  109. } else {
  110. memcpy(buffer, umem + (offset - map_start), end - offset);
  111. }
  112. ocall_unmap_untrusted(umem, map_end - map_start);
  113. return end - offset;
  114. }
  115. /* 'write' operation for file streams. */
  116. static int64_t file_write(PAL_HANDLE handle, uint64_t offset, uint64_t count,
  117. const void * buffer)
  118. {
  119. uint64_t map_start = ALLOC_ALIGNDOWN(offset);
  120. uint64_t map_end = ALLOC_ALIGNUP(offset + count);
  121. void * umem;
  122. int ret;
  123. ret = ocall_map_untrusted(handle->file.fd, map_start,
  124. map_end - map_start, PROT_WRITE, &umem);
  125. if (IS_ERR(ret))
  126. return unix_to_pal_error(ERRNO(ret));
  127. if (offset + count > handle->file.total) {
  128. ocall_ftruncate(handle->file.fd, offset + count);
  129. handle->file.total = offset + count;
  130. }
  131. memcpy(umem + offset - map_start, buffer, count);
  132. ocall_unmap_untrusted(umem, map_end - map_start);
  133. return count;
  134. }
  135. /* 'close' operation for file streams. In this case, it will only
  136. close the file withou deleting it. */
  137. static int file_close (PAL_HANDLE handle)
  138. {
  139. int fd = handle->file.fd;
  140. ocall_close(fd);
  141. /* initial realpath is part of handle object and will be freed with it */
  142. if (handle->file.realpath &&
  143. handle->file.realpath != (void *) handle + HANDLE_SIZE(file))
  144. free((void *) handle->file.realpath);
  145. return 0;
  146. }
  147. /* 'delete' operation for file streams. It will actually delete
  148. the file if we can successfully close it. */
  149. static int file_delete (PAL_HANDLE handle, int access)
  150. {
  151. if (access)
  152. return -PAL_ERROR_INVAL;
  153. int ret = ocall_delete(handle->file.realpath);
  154. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  155. }
  156. /* 'map' operation for file stream. */
  157. static int file_map (PAL_HANDLE handle, void ** addr, int prot,
  158. uint64_t offset, uint64_t size)
  159. {
  160. sgx_stub_t * stubs = (sgx_stub_t *) handle->file.stubs;
  161. uint64_t total = handle->file.total;
  162. void * mem = *addr;
  163. void * umem;
  164. int ret;
  165. /*
  166. * If the file is listed in the manifest as an "allowed" file,
  167. * we allow mapping the file outside the enclave, if the library OS
  168. * does not request a specific address.
  169. */
  170. if (!mem && !stubs && !(prot & PAL_PROT_WRITECOPY)) {
  171. ret = ocall_map_untrusted(handle->file.fd, offset, size,
  172. HOST_PROT(prot), &mem);
  173. if (!IS_ERR(ret))
  174. *addr = mem;
  175. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  176. }
  177. if (!(prot & PAL_PROT_WRITECOPY) && (prot & PAL_PROT_WRITE)) {
  178. SGX_DBG(DBG_E, "file_map does not currently support writable pass-through mappings on SGX. You may add the PAL_PROT_WRITECOPY (MAP_PRIVATE) flag to your file mapping to keep the writes inside the enclave but they won't be reflected outside of the enclave.\n");
  179. return -PAL_ERROR_DENIED;
  180. }
  181. mem = get_reserved_pages(mem, size);
  182. if (!mem)
  183. return -PAL_ERROR_NOMEM;
  184. uint64_t end = (offset + size > total) ? total : offset + size;
  185. uint64_t map_start, map_end;
  186. if (stubs) {
  187. map_start = offset & ~(TRUSTED_STUB_SIZE - 1);
  188. map_end = (end + TRUSTED_STUB_SIZE - 1) & ~(TRUSTED_STUB_SIZE - 1);
  189. } else {
  190. map_start = ALLOC_ALIGNDOWN(offset);
  191. map_end = ALLOC_ALIGNUP(end);
  192. }
  193. ret = ocall_map_untrusted(handle->file.fd, map_start,
  194. map_end - map_start, PROT_READ, &umem);
  195. if (IS_ERR(ret)) {
  196. SGX_DBG(DBG_E, "file_map - ocall returned %d\n", ret);
  197. return unix_to_pal_error(ERRNO(ret));
  198. }
  199. if (stubs) {
  200. ret = copy_and_verify_trusted_file(handle->file.realpath, umem,
  201. map_start, map_end,
  202. mem, offset, end - offset,
  203. stubs, total);
  204. if (ret < 0) {
  205. SGX_DBG(DBG_E, "file_map - verify trusted returned %d\n", ret);
  206. ocall_unmap_untrusted(umem, map_end - map_start);
  207. return ret;
  208. }
  209. } else {
  210. memcpy(mem, umem + (offset - map_start), end - offset);
  211. }
  212. ocall_unmap_untrusted(umem, map_end - map_start);
  213. *addr = mem;
  214. return 0;
  215. }
  216. /* 'setlength' operation for file stream. */
  217. static int64_t file_setlength (PAL_HANDLE handle, uint64_t length)
  218. {
  219. int ret = ocall_ftruncate(handle->file.fd, length);
  220. if (IS_ERR(ret))
  221. return unix_to_pal_error(ERRNO(ret));
  222. handle->file.total = length;
  223. return (int64_t) length;
  224. }
  225. /* 'flush' operation for file stream. */
  226. static int file_flush (PAL_HANDLE handle)
  227. {
  228. ocall_fsync(handle->file.fd);
  229. return 0;
  230. }
  231. static inline int file_stat_type (struct stat * stat)
  232. {
  233. if (S_ISREG(stat->st_mode))
  234. return pal_type_file;
  235. if (S_ISDIR(stat->st_mode))
  236. return pal_type_dir;
  237. if (S_ISCHR(stat->st_mode))
  238. return pal_type_dev;
  239. if (S_ISFIFO(stat->st_mode))
  240. return pal_type_pipe;
  241. if (S_ISSOCK(stat->st_mode))
  242. return pal_type_dev;
  243. return 0;
  244. }
  245. /* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
  246. static inline void
  247. file_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  248. {
  249. attr->handle_type = file_stat_type(stat);
  250. attr->disconnected = PAL_FALSE;
  251. attr->nonblocking = PAL_FALSE;
  252. attr->readable = stataccess(stat, ACCESS_R);
  253. attr->writable = stataccess(stat, ACCESS_W);
  254. attr->runnable = stataccess(stat, ACCESS_X);
  255. attr->share_flags = stat->st_mode;
  256. attr->pending_size = stat->st_size;
  257. }
  258. /* 'attrquery' operation for file streams */
  259. static int file_attrquery (const char * type, const char * uri,
  260. PAL_STREAM_ATTR * attr)
  261. {
  262. if (!strcmp_static(type, "file") && !strcmp_static(type, "dir") )
  263. return -PAL_ERROR_INVAL;
  264. /* try to do the real open */
  265. int fd = ocall_open(uri, 0, 0);
  266. if (IS_ERR(fd))
  267. return unix_to_pal_error(ERRNO(fd));
  268. struct stat stat_buf;
  269. int ret = ocall_fstat(fd, &stat_buf);
  270. ocall_close(fd);
  271. /* if it failed, return the right error code */
  272. if (IS_ERR(ret))
  273. return unix_to_pal_error(ERRNO(ret));
  274. file_attrcopy(attr, &stat_buf);
  275. return 0;
  276. }
  277. /* 'attrquerybyhdl' operation for file streams */
  278. static int file_attrquerybyhdl (PAL_HANDLE handle,
  279. PAL_STREAM_ATTR * attr)
  280. {
  281. int fd = handle->file.fd;
  282. struct stat stat_buf;
  283. int ret = ocall_fstat(fd, &stat_buf);
  284. if (IS_ERR(ret))
  285. return unix_to_pal_error(ERRNO(ret));
  286. file_attrcopy(attr, &stat_buf);
  287. return 0;
  288. }
  289. static int file_attrsetbyhdl (PAL_HANDLE handle,
  290. PAL_STREAM_ATTR * attr)
  291. {
  292. int fd = handle->file.fd;
  293. int ret = ocall_fchmod(fd, attr->share_flags | 0600);
  294. if (IS_ERR(ret))
  295. return unix_to_pal_error(ERRNO(ret));
  296. return 0;
  297. }
  298. static int file_rename (PAL_HANDLE handle, const char * type,
  299. const char * uri)
  300. {
  301. if (!strcmp_static(type, "file"))
  302. return -PAL_ERROR_INVAL;
  303. char* tmp = strdup(uri);
  304. if (!tmp)
  305. return -PAL_ERROR_NOMEM;
  306. int ret = ocall_rename(handle->file.realpath, uri);
  307. if (IS_ERR(ret)) {
  308. free(tmp);
  309. return unix_to_pal_error(ERRNO(ret));
  310. }
  311. /* initial realpath is part of handle object and will be freed with it */
  312. if (handle->file.realpath &&
  313. handle->file.realpath != (void *) handle + HANDLE_SIZE(file)) {
  314. free((void *) handle->file.realpath);
  315. }
  316. handle->file.realpath = tmp;
  317. return 0;
  318. }
  319. static int file_getname (PAL_HANDLE handle, char * buffer, size_t count)
  320. {
  321. if (!handle->file.realpath)
  322. return 0;
  323. int len = strlen(handle->file.realpath);
  324. char * tmp = strcpy_static(buffer, "file:", count);
  325. if (!tmp || buffer + count < tmp + len + 1)
  326. return -PAL_ERROR_TOOLONG;
  327. memcpy(tmp, handle->file.realpath, len + 1);
  328. return tmp + len - buffer;
  329. }
  330. const char * file_getrealpath (PAL_HANDLE handle)
  331. {
  332. return handle->file.realpath;
  333. }
  334. struct handle_ops file_ops = {
  335. .getname = &file_getname,
  336. .getrealpath = &file_getrealpath,
  337. .open = &file_open,
  338. .read = &file_read,
  339. .write = &file_write,
  340. .close = &file_close,
  341. .delete = &file_delete,
  342. .map = &file_map,
  343. .setlength = &file_setlength,
  344. .flush = &file_flush,
  345. .attrquery = &file_attrquery,
  346. .attrquerybyhdl = &file_attrquerybyhdl,
  347. .attrsetbyhdl = &file_attrsetbyhdl,
  348. .rename = &file_rename,
  349. };
  350. /* 'open' operation for directory stream. Directory stream does not have a
  351. specific type prefix, its URI looks the same file streams, plus it
  352. ended with slashes. dir_open will be called by file_open. */
  353. static int dir_open (PAL_HANDLE * handle, const char * type, const char * uri,
  354. int access, int share, int create, int options)
  355. {
  356. if (!strcmp_static(type, "dir"))
  357. return -PAL_ERROR_INVAL;
  358. if (!WITHIN_MASK(access, PAL_ACCESS_MASK))
  359. return -PAL_ERROR_INVAL;
  360. int ret;
  361. if (create & PAL_CREATE_TRY) {
  362. ret = ocall_mkdir(uri, share);
  363. if (IS_ERR(ret) && ERRNO(ret) == EEXIST &&
  364. create & PAL_CREATE_ALWAYS)
  365. return -PAL_ERROR_STREAMEXIST;
  366. }
  367. ret = ocall_open(uri, O_DIRECTORY|options, 0);
  368. if (IS_ERR(ret))
  369. return unix_to_pal_error(ERRNO(ret));
  370. int len = strlen(uri);
  371. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  372. SET_HANDLE_TYPE(hdl, dir);
  373. HANDLE_HDR(hdl)->flags |= RFD(0);
  374. hdl->dir.fd = ret;
  375. char * path = (void *) hdl + HANDLE_SIZE(dir);
  376. memcpy(path, uri, len + 1);
  377. hdl->dir.realpath = (PAL_STR) path;
  378. hdl->dir.buf = (PAL_PTR) NULL;
  379. hdl->dir.ptr = (PAL_PTR) NULL;
  380. hdl->dir.end = (PAL_PTR) NULL;
  381. hdl->dir.endofstream = PAL_FALSE;
  382. *handle = hdl;
  383. return 0;
  384. }
  385. #define DIRBUF_SIZE 1024
  386. /* 'read' operation for directory stream. Directory stream will not
  387. need a 'write' operat4on. */
  388. static int64_t dir_read (PAL_HANDLE handle, uint64_t offset, size_t count,
  389. void * buf)
  390. {
  391. if (offset)
  392. return -PAL_ERROR_INVAL;
  393. void * dent_buf = (void *) handle->dir.buf ? : __alloca(DIRBUF_SIZE);
  394. void * ptr = (void *) handle->dir.ptr;
  395. void * end = (void *) handle->dir.end;
  396. int bytes = 0;
  397. if (ptr && ptr < end)
  398. goto output;
  399. do {
  400. if (handle->dir.endofstream)
  401. break;
  402. int size = ocall_getdents(handle->dir.fd, dent_buf, DIRBUF_SIZE);
  403. if (IS_ERR(size))
  404. return unix_to_pal_error(ERRNO(size));
  405. if (size == 0) {
  406. handle->dir.endofstream = PAL_TRUE;
  407. break;
  408. }
  409. ptr = dent_buf;
  410. end = dent_buf + size;
  411. output:
  412. while (ptr < end) {
  413. struct linux_dirent64 * d = (struct linux_dirent64 *) ptr;
  414. if (d->d_name[0] == '.' &&
  415. (!d->d_name[1] || d->d_name[1] == '.'))
  416. goto next;
  417. bool isdir = (d->d_type == DT_DIR);
  418. size_t len = strlen(d->d_name);
  419. if (len + (isdir ? 2 : 1) > count)
  420. break;
  421. memcpy(buf, d->d_name, len);
  422. if (isdir)
  423. ((char *) buf)[len++] = '/';
  424. ((char *) buf)[len++] = '\0';
  425. bytes += len;
  426. buf += len;
  427. count -= len;
  428. next:
  429. ptr += d->d_reclen;
  430. }
  431. } while (ptr == end);
  432. if (ptr < end) {
  433. if (!handle->dir.buf)
  434. handle->dir.buf = (PAL_PTR) malloc(DIRBUF_SIZE);
  435. if ((void *) handle->dir.buf != ptr) {
  436. memmove((void *) handle->dir.buf, ptr, end - ptr);
  437. end = (void *) handle->dir.buf + (end - ptr);
  438. ptr = (void *) handle->dir.buf;
  439. }
  440. if (!bytes)
  441. return -PAL_ERROR_OVERFLOW;
  442. }
  443. return bytes ? : -PAL_ERROR_ENDOFSTREAM;
  444. }
  445. /* 'close' operation of directory streams */
  446. static int dir_close (PAL_HANDLE handle)
  447. {
  448. int fd = handle->dir.fd;
  449. ocall_close(fd);
  450. if (handle->dir.buf) {
  451. free((void *) handle->dir.buf);
  452. handle->dir.buf = handle->dir.ptr = handle->dir.end = (PAL_PTR) NULL;
  453. }
  454. /* initial realpath is part of handle object and will be freed with it */
  455. if (handle->dir.realpath &&
  456. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir))
  457. free((void *) handle->dir.realpath);
  458. return 0;
  459. }
  460. /* 'delete' operation of directoy streams */
  461. static int dir_delete (PAL_HANDLE handle, int access)
  462. {
  463. if (access)
  464. return -PAL_ERROR_INVAL;
  465. int ret = dir_close(handle);
  466. if (ret < 0)
  467. return ret;
  468. ret = ocall_delete(handle->dir.realpath);
  469. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  470. }
  471. static int dir_rename (PAL_HANDLE handle, const char * type,
  472. const char * uri)
  473. {
  474. if (!strcmp_static(type, "dir"))
  475. return -PAL_ERROR_INVAL;
  476. char* tmp = strdup(uri);
  477. if (!tmp)
  478. return -PAL_ERROR_NOMEM;
  479. int ret = ocall_rename(handle->dir.realpath, uri);
  480. if (IS_ERR(ret)) {
  481. free(tmp);
  482. return unix_to_pal_error(ERRNO(ret));
  483. }
  484. /* initial realpath is part of handle object and will be freed with it */
  485. if (handle->dir.realpath &&
  486. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir)) {
  487. free((void *) handle->dir.realpath);
  488. }
  489. handle->dir.realpath = tmp;
  490. return 0;
  491. }
  492. static int dir_getname (PAL_HANDLE handle, char * buffer, size_t count)
  493. {
  494. if (!handle->dir.realpath)
  495. return 0;
  496. size_t len = strlen(handle->dir.realpath);
  497. char * tmp = strcpy_static(buffer, "dir:", count);
  498. if (!tmp || buffer + count < tmp + len + 1)
  499. return -PAL_ERROR_TOOLONG;
  500. memcpy(tmp, handle->dir.realpath, len + 1);
  501. return tmp + len - buffer;
  502. if (len + 6 >= count)
  503. return -PAL_ERROR_TOOLONG;
  504. }
  505. static const char * dir_getrealpath (PAL_HANDLE handle)
  506. {
  507. return handle->dir.realpath;
  508. }
  509. struct handle_ops dir_ops = {
  510. .getname = &dir_getname,
  511. .getrealpath = &dir_getrealpath,
  512. .open = &dir_open,
  513. .read = &dir_read,
  514. .close = &dir_close,
  515. .delete = &dir_delete,
  516. .attrquery = &file_attrquery,
  517. .attrquerybyhdl = &file_attrquerybyhdl,
  518. .attrsetbyhdl = &file_attrsetbyhdl,
  519. .rename = &dir_rename,
  520. };