db_files.c 17 KB

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