shim_open.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_open.c
  15. *
  16. * Implementation of system call "read", "write", "open", "creat", "openat",
  17. * "close", "lseek", "pread64", "pwrite64", "getdents", "getdents64",
  18. * "fsync", "truncate" and "ftruncate".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_table.h>
  23. #include <shim_thread.h>
  24. #include <shim_handle.h>
  25. #include <shim_fs.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <errno.h>
  30. #include <dirent.h>
  31. #include <linux/stat.h>
  32. #include <linux/fcntl.h>
  33. int do_handle_read (struct shim_handle * hdl, void * buf, int count)
  34. {
  35. if (!(hdl->acc_mode & MAY_READ))
  36. return -EACCES;
  37. struct shim_mount * fs = hdl->fs;
  38. assert(fs && fs->fs_ops);
  39. if (!fs->fs_ops->read)
  40. return -EBADF;
  41. if (hdl->type == TYPE_DIR)
  42. return -EISDIR;
  43. return fs->fs_ops->read(hdl, buf, count);
  44. }
  45. size_t shim_do_read (int fd, void * buf, size_t count)
  46. {
  47. if (!buf || test_user_memory(buf, count, true))
  48. return -EFAULT;
  49. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  50. if (!hdl)
  51. return -EBADF;
  52. /* sockets may read from LibOS buffer due to MSG_PEEK, so need to call socket-specific recv */
  53. if (hdl->type == TYPE_SOCK) {
  54. put_handle(hdl);
  55. return shim_do_recvfrom(fd, buf, count, 0, NULL, NULL);
  56. }
  57. int ret = do_handle_read(hdl, buf, count);
  58. put_handle(hdl);
  59. return ret;
  60. }
  61. int do_handle_write (struct shim_handle * hdl, const void * buf, int count)
  62. {
  63. if (!(hdl->acc_mode & MAY_WRITE))
  64. return -EACCES;
  65. struct shim_mount * fs = hdl->fs;
  66. assert(fs && fs->fs_ops);
  67. if (!fs->fs_ops->write)
  68. return -EBADF;
  69. if (hdl->type == TYPE_DIR)
  70. return -EISDIR;
  71. return fs->fs_ops->write(hdl, buf, count);
  72. }
  73. size_t shim_do_write (int fd, const void * buf, size_t count)
  74. {
  75. if (!buf || test_user_memory((void *) buf, count, false))
  76. return -EFAULT;
  77. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  78. if (!hdl)
  79. return -EBADF;
  80. int ret = do_handle_write(hdl, buf, count);
  81. put_handle(hdl);
  82. return ret;
  83. }
  84. int shim_do_open (const char * file, int flags, mode_t mode)
  85. {
  86. if (!file || test_user_string(file))
  87. return -EFAULT;
  88. if (file[0] == '\0')
  89. return -EINVAL;
  90. struct shim_handle * hdl = get_new_handle();
  91. if (!hdl)
  92. return -ENOMEM;
  93. int ret = 0;
  94. ret = open_namei(hdl, NULL, file, flags, mode, NULL);
  95. if (ret < 0)
  96. goto out;
  97. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  98. out:
  99. put_handle(hdl);
  100. return ret;
  101. }
  102. int shim_do_creat (const char * path, mode_t mode)
  103. {
  104. return shim_do_open(path, O_CREAT|O_TRUNC|O_WRONLY, mode);
  105. }
  106. int shim_do_openat (int dfd, const char * filename, int flags, int mode)
  107. {
  108. if (!filename || test_user_string(filename))
  109. return -EFAULT;
  110. struct shim_dentry * dir = NULL;
  111. int ret = 0;
  112. if ((ret = get_dirfd_dentry(dfd, &dir)) < 0)
  113. return ret;
  114. struct shim_handle * hdl = get_new_handle();
  115. if (!hdl) {
  116. ret = -ENOMEM;
  117. goto out;
  118. }
  119. ret = open_namei(hdl, dir, filename, flags, mode, NULL);
  120. if (ret < 0)
  121. goto out_hdl;
  122. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  123. out_hdl:
  124. put_handle(hdl);
  125. out:
  126. put_dentry(dir);
  127. return ret;
  128. }
  129. int shim_do_close (int fd)
  130. {
  131. struct shim_handle * handle = detach_fd_handle(fd, NULL, NULL);
  132. if (!handle)
  133. return -EBADF;
  134. put_handle(handle);
  135. return 0;
  136. }
  137. /* lseek is simply doing arithmetic on the offset, no PAL call here */
  138. off_t shim_do_lseek (int fd, off_t offset, int origin)
  139. {
  140. if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END)
  141. return -EINVAL;
  142. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  143. if (!hdl)
  144. return -EBADF;
  145. int ret = 0;
  146. struct shim_mount * fs = hdl->fs;
  147. assert(fs && fs->fs_ops);
  148. if (!fs->fs_ops->seek) {
  149. ret = -ESPIPE;
  150. goto out;
  151. }
  152. if (hdl->type == TYPE_DIR) {
  153. /* TODO: handle lseek'ing of directories */
  154. ret = -ENOSYS;
  155. goto out;
  156. }
  157. ret = fs->fs_ops->seek(hdl, offset, origin);
  158. out:
  159. put_handle(hdl);
  160. return ret;
  161. }
  162. ssize_t shim_do_pread64 (int fd, char * buf, size_t count, loff_t pos)
  163. {
  164. if (!buf || test_user_memory(buf, count, true))
  165. return -EFAULT;
  166. if (pos < 0)
  167. return -EINVAL;
  168. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  169. if (!hdl)
  170. return -EBADF;
  171. struct shim_mount * fs = hdl->fs;
  172. ssize_t ret = -EACCES;
  173. if (!fs || !fs->fs_ops)
  174. goto out;
  175. if (!fs->fs_ops->seek) {
  176. ret = -ESPIPE;
  177. goto out;
  178. }
  179. if (!fs->fs_ops->read)
  180. goto out;
  181. if (hdl->type == TYPE_DIR)
  182. goto out;
  183. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  184. if (offset < 0) {
  185. ret = offset;
  186. goto out;
  187. }
  188. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  189. if (ret < 0)
  190. goto out;
  191. int bytes = fs->fs_ops->read(hdl, buf, count);
  192. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  193. if (ret < 0)
  194. goto out;
  195. ret = bytes;
  196. out:
  197. put_handle(hdl);
  198. return ret;
  199. }
  200. ssize_t shim_do_pwrite64 (int fd, char * buf, size_t count, loff_t pos)
  201. {
  202. if (!buf || test_user_memory(buf, count, false))
  203. return -EFAULT;
  204. if (pos < 0)
  205. return -EINVAL;
  206. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  207. if (!hdl)
  208. return -EBADF;
  209. struct shim_mount * fs = hdl->fs;
  210. ssize_t ret = -EACCES;
  211. if (!fs || !fs->fs_ops)
  212. goto out;
  213. if (!fs->fs_ops->seek) {
  214. ret = -ESPIPE;
  215. goto out;
  216. }
  217. if (!fs->fs_ops->write)
  218. goto out;
  219. if (hdl->type == TYPE_DIR)
  220. goto out;
  221. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  222. if (offset < 0) {
  223. ret = offset;
  224. goto out;
  225. }
  226. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  227. if (ret < 0)
  228. goto out;
  229. int bytes = fs->fs_ops->write(hdl, buf, count);
  230. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  231. if (ret < 0)
  232. goto out;
  233. ret = bytes;
  234. out:
  235. put_handle(hdl);
  236. return ret;
  237. }
  238. static inline int get_dirent_type (mode_t type)
  239. {
  240. switch (type) {
  241. case S_IFLNK:
  242. return LINUX_DT_LNK;
  243. case S_IFREG:
  244. return LINUX_DT_REG;
  245. case S_IFDIR:
  246. return LINUX_DT_DIR;
  247. case S_IFCHR:
  248. return LINUX_DT_CHR;
  249. case S_IFBLK:
  250. return LINUX_DT_BLK;
  251. case S_IFIFO:
  252. return LINUX_DT_FIFO;
  253. case S_IFSOCK:
  254. return LINUX_DT_SOCK;
  255. default:
  256. return LINUX_DT_UNKNOWN;
  257. }
  258. }
  259. size_t shim_do_getdents (int fd, struct linux_dirent * buf, size_t count)
  260. {
  261. if (!buf || test_user_memory(buf, count, true))
  262. return -EFAULT;
  263. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  264. if (!hdl)
  265. return -EBADF;
  266. int ret = -EACCES;
  267. if (hdl->type != TYPE_DIR) {
  268. ret = -ENOTDIR;
  269. goto out_no_unlock;
  270. }
  271. /* DEP 3/3/17: Properly handle an unlinked directory */
  272. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  273. ret = -ENOENT;
  274. goto out_no_unlock;
  275. }
  276. /* we are grabbing the lock because the handle content is actually
  277. updated */
  278. lock(&hdl->lock);
  279. struct shim_dir_handle * dirhdl = &hdl->dir_info;
  280. struct shim_dentry * dent = hdl->dentry;
  281. struct linux_dirent * b = buf;
  282. int bytes = 0;
  283. /* If we haven't listed the directory, do this first */
  284. if (!(dent->state & DENTRY_LISTED)) {
  285. ret = list_directory_dentry(dent);
  286. if (ret < 0)
  287. goto out;
  288. }
  289. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent) + \
  290. sizeof(struct linux_dirent_tail) + (len) + 1)
  291. #define ASSIGN_DIRENT(dent, name, type) \
  292. do { \
  293. int len = strlen(name); \
  294. if (bytes + DIRENT_SIZE(len) > count) \
  295. goto done; \
  296. \
  297. struct linux_dirent_tail * bt \
  298. = (void *) b + sizeof(struct linux_dirent) + len + 1; \
  299. \
  300. b->d_ino = (dent)->ino; \
  301. b->d_off = ++dirhdl->offset; \
  302. b->d_reclen = DIRENT_SIZE(len); \
  303. \
  304. memcpy(b->d_name, name, len + 1); \
  305. \
  306. bt->pad = 0; \
  307. bt->d_type = (type); \
  308. \
  309. b = (void *) bt + sizeof(struct linux_dirent_tail); \
  310. bytes += DIRENT_SIZE(len); \
  311. } while(0)
  312. if (dirhdl->dot) {
  313. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  314. put_dentry(dirhdl->dot);
  315. dirhdl->dot = NULL;
  316. }
  317. if (dirhdl->dotdot) {
  318. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  319. put_dentry(dirhdl->dotdot);
  320. dirhdl->dotdot = NULL;
  321. }
  322. if (dirhdl->ptr == (void *) -1) {
  323. ret = list_directory_handle(dent, hdl);
  324. if (ret < 0)
  325. goto out;
  326. }
  327. while (dirhdl->ptr && *dirhdl->ptr) {
  328. dent = *dirhdl->ptr;
  329. /* DEP 3/3/17: We need to filter negative dentries */
  330. if (!(dent->state & DENTRY_NEGATIVE))
  331. ASSIGN_DIRENT(dent, dentry_get_name(dent), get_dirent_type(dent->type));
  332. put_dentry(dent);
  333. *(dirhdl->ptr++) = NULL;
  334. }
  335. #undef DIRENT_SIZE
  336. #undef ASSIGN_DIRENT
  337. done:
  338. ret = bytes;
  339. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  340. * hold anything */
  341. if (bytes == 0 && (dirhdl->dot || dirhdl->dotdot ||
  342. (dirhdl->ptr && *dirhdl->ptr)))
  343. ret = -EINVAL;
  344. out:
  345. unlock(&hdl->lock);
  346. out_no_unlock:
  347. put_handle(hdl);
  348. return ret;
  349. }
  350. size_t shim_do_getdents64 (int fd, struct linux_dirent64 * buf, size_t count)
  351. {
  352. if (!buf || test_user_memory(buf, count, true))
  353. return -EFAULT;
  354. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  355. if (!hdl)
  356. return -EBADF;
  357. int ret = -EACCES;
  358. if (hdl->type != TYPE_DIR) {
  359. ret = -ENOTDIR;
  360. goto out;
  361. }
  362. /* DEP 3/3/17: Properly handle an unlinked directory */
  363. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  364. ret = -ENOENT;
  365. goto out;
  366. }
  367. lock(&hdl->lock);
  368. struct shim_dir_handle * dirhdl = &hdl->dir_info;
  369. struct shim_dentry * dent = hdl->dentry;
  370. struct linux_dirent64 * b = buf;
  371. int bytes = 0;
  372. /* If we haven't listed the directory, do this first */
  373. if (!(dent->state & DENTRY_LISTED)) {
  374. ret = list_directory_dentry(dent);
  375. if (ret) goto out;
  376. }
  377. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent64) + (len) + 1)
  378. #define ASSIGN_DIRENT(dent, name, type) \
  379. do { \
  380. int len = strlen(name); \
  381. if (bytes + DIRENT_SIZE(len) > count) \
  382. goto done; \
  383. \
  384. b->d_ino = (dent)->ino; \
  385. b->d_off = ++dirhdl->offset; \
  386. b->d_reclen = DIRENT_SIZE(len); \
  387. b->d_type = (type); \
  388. \
  389. memcpy(b->d_name, name, len + 1); \
  390. \
  391. b = (void *) b + DIRENT_SIZE(len); \
  392. bytes += DIRENT_SIZE(len); \
  393. } while(0)
  394. if (dirhdl->dot) {
  395. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  396. put_dentry(dirhdl->dot);
  397. dirhdl->dot = NULL;
  398. }
  399. if (dirhdl->dotdot) {
  400. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  401. put_dentry(dirhdl->dotdot);
  402. dirhdl->dotdot = NULL;
  403. }
  404. if (dirhdl->ptr == (void *) -1) {
  405. ret = list_directory_handle(dent, hdl);
  406. if (ret) goto out;
  407. }
  408. while (dirhdl->ptr && *dirhdl->ptr) {
  409. dent = *dirhdl->ptr;
  410. /* DEP 3/3/17: We need to filter negative dentries */
  411. if (!(dent->state & DENTRY_NEGATIVE))
  412. ASSIGN_DIRENT(dent, dentry_get_name(dent), get_dirent_type(dent->type));
  413. put_dentry(dent);
  414. *(dirhdl->ptr++) = NULL;
  415. }
  416. #undef DIRENT_SIZE
  417. #undef ASSIGN_DIRENT
  418. done:
  419. ret = bytes;
  420. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  421. * hold anything */
  422. if (bytes == 0 && (dirhdl->dot || dirhdl->dotdot ||
  423. (dirhdl->ptr && *dirhdl->ptr)))
  424. ret = -EINVAL;
  425. unlock(&hdl->lock);
  426. out:
  427. put_handle(hdl);
  428. return ret;
  429. }
  430. int shim_do_fsync (int fd)
  431. {
  432. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  433. if (!hdl)
  434. return -EBADF;
  435. int ret = -EACCES;
  436. struct shim_mount * fs = hdl->fs;
  437. if (!fs || !fs->fs_ops)
  438. goto out;
  439. if (hdl->type == TYPE_DIR)
  440. goto out;
  441. if (!fs->fs_ops->flush) {
  442. ret = -EROFS;
  443. goto out;
  444. }
  445. ret = fs->fs_ops->flush(hdl);
  446. out:
  447. put_handle(hdl);
  448. return ret;
  449. }
  450. // DEP 10/20/16: Assuming fsync >> fdatasync for now
  451. // and no app depends on only syncing data for correctness.
  452. int shim_do_fdatasync (int fd)
  453. {
  454. return shim_do_fsync(fd);
  455. }
  456. int shim_do_truncate (const char * path, loff_t length)
  457. {
  458. struct shim_dentry * dent = NULL;
  459. int ret = 0;
  460. if (!path || test_user_string(path))
  461. return -EFAULT;
  462. if ((ret = path_lookupat(NULL, path, 0, &dent, NULL)) < 0)
  463. return ret;
  464. struct shim_mount * fs = dent->fs;
  465. if (!fs || !fs->d_ops || !fs->d_ops->open) {
  466. ret = -EBADF;
  467. goto out;
  468. }
  469. if (!fs->fs_ops->truncate) {
  470. ret = -EROFS;
  471. goto out;
  472. }
  473. struct shim_handle * hdl = get_new_handle();
  474. if (!hdl) {
  475. ret = -ENOMEM;
  476. goto out;
  477. }
  478. hdl->fs = fs;
  479. if ((ret = fs->d_ops->open(hdl, dent, O_WRONLY)) < 0)
  480. goto out_handle;
  481. ret = fs->fs_ops->truncate(hdl, length);
  482. flush_handle(hdl);
  483. out_handle:
  484. put_handle(hdl);
  485. out:
  486. return ret;
  487. }
  488. int shim_do_ftruncate (int fd, loff_t length)
  489. {
  490. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  491. if (!hdl)
  492. return -EBADF;
  493. struct shim_mount * fs = hdl->fs;
  494. int ret = -EINVAL;
  495. if (!fs || !fs->fs_ops)
  496. goto out;
  497. if (hdl->type == TYPE_DIR ||
  498. !fs->fs_ops->truncate)
  499. goto out;
  500. ret = fs->fs_ops->truncate(hdl, length);
  501. out:
  502. put_handle(hdl);
  503. return ret;
  504. }