db_files.c 16 KB

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