db_files.c 16 KB

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