db_files.c 15 KB

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