db_files.c 17 KB

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