shim_open.c 16 KB

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