shim_open.c 16 KB

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