readdir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/readdir.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. */
  7. #include <linux/stddef.h>
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/time.h>
  11. #include <linux/mm.h>
  12. #include <linux/errno.h>
  13. #include <linux/stat.h>
  14. #include <linux/file.h>
  15. #include <linux/fs.h>
  16. #include <linux/fsnotify.h>
  17. #include <linux/dirent.h>
  18. #include <linux/security.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/unistd.h>
  21. #include <linux/compat.h>
  22. #include <linux/uaccess.h>
  23. int iterate_dir(struct file *file, struct dir_context *ctx)
  24. {
  25. struct inode *inode = file_inode(file);
  26. bool shared = false;
  27. int res = -ENOTDIR;
  28. if (file->f_op->iterate_shared)
  29. shared = true;
  30. else if (!file->f_op->iterate)
  31. goto out;
  32. res = security_file_permission(file, MAY_READ);
  33. if (res)
  34. goto out;
  35. if (shared)
  36. res = down_read_killable(&inode->i_rwsem);
  37. else
  38. res = down_write_killable(&inode->i_rwsem);
  39. if (res)
  40. goto out;
  41. res = -ENOENT;
  42. if (!IS_DEADDIR(inode)) {
  43. ctx->pos = file->f_pos;
  44. if (shared)
  45. res = file->f_op->iterate_shared(file, ctx);
  46. else
  47. res = file->f_op->iterate(file, ctx);
  48. file->f_pos = ctx->pos;
  49. fsnotify_access(file);
  50. file_accessed(file);
  51. }
  52. if (shared)
  53. inode_unlock_shared(inode);
  54. else
  55. inode_unlock(inode);
  56. out:
  57. return res;
  58. }
  59. EXPORT_SYMBOL(iterate_dir);
  60. /*
  61. * POSIX says that a dirent name cannot contain NULL or a '/'.
  62. *
  63. * It's not 100% clear what we should really do in this case.
  64. * The filesystem is clearly corrupted, but returning a hard
  65. * error means that you now don't see any of the other names
  66. * either, so that isn't a perfect alternative.
  67. *
  68. * And if you return an error, what error do you use? Several
  69. * filesystems seem to have decided on EUCLEAN being the error
  70. * code for EFSCORRUPTED, and that may be the error to use. Or
  71. * just EIO, which is perhaps more obvious to users.
  72. *
  73. * In order to see the other file names in the directory, the
  74. * caller might want to make this a "soft" error: skip the
  75. * entry, and return the error at the end instead.
  76. *
  77. * Note that this should likely do a "memchr(name, 0, len)"
  78. * check too, since that would be filesystem corruption as
  79. * well. However, that case can't actually confuse user space,
  80. * which has to do a strlen() on the name anyway to find the
  81. * filename length, and the above "soft error" worry means
  82. * that it's probably better left alone until we have that
  83. * issue clarified.
  84. */
  85. static int verify_dirent_name(const char *name, int len)
  86. {
  87. if (!len)
  88. return -EIO;
  89. if (memchr(name, '/', len))
  90. return -EIO;
  91. return 0;
  92. }
  93. /*
  94. * Traditional linux readdir() handling..
  95. *
  96. * "count=1" is a special case, meaning that the buffer is one
  97. * dirent-structure in size and that the code can't handle more
  98. * anyway. Thus the special "fillonedir()" function for that
  99. * case (the low-level handlers don't need to care about this).
  100. */
  101. #ifdef __ARCH_WANT_OLD_READDIR
  102. struct old_linux_dirent {
  103. unsigned long d_ino;
  104. unsigned long d_offset;
  105. unsigned short d_namlen;
  106. char d_name[1];
  107. };
  108. struct readdir_callback {
  109. struct dir_context ctx;
  110. struct old_linux_dirent __user * dirent;
  111. int result;
  112. };
  113. static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
  114. loff_t offset, u64 ino, unsigned int d_type)
  115. {
  116. struct readdir_callback *buf =
  117. container_of(ctx, struct readdir_callback, ctx);
  118. struct old_linux_dirent __user * dirent;
  119. unsigned long d_ino;
  120. if (buf->result)
  121. return -EINVAL;
  122. buf->result = verify_dirent_name(name, namlen);
  123. if (buf->result < 0)
  124. return buf->result;
  125. d_ino = ino;
  126. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  127. buf->result = -EOVERFLOW;
  128. return -EOVERFLOW;
  129. }
  130. buf->result++;
  131. dirent = buf->dirent;
  132. if (!access_ok(VERIFY_WRITE, dirent,
  133. (unsigned long)(dirent->d_name + namlen + 1) -
  134. (unsigned long)dirent))
  135. goto efault;
  136. if ( __put_user(d_ino, &dirent->d_ino) ||
  137. __put_user(offset, &dirent->d_offset) ||
  138. __put_user(namlen, &dirent->d_namlen) ||
  139. __copy_to_user(dirent->d_name, name, namlen) ||
  140. __put_user(0, dirent->d_name + namlen))
  141. goto efault;
  142. return 0;
  143. efault:
  144. buf->result = -EFAULT;
  145. return -EFAULT;
  146. }
  147. SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  148. struct old_linux_dirent __user *, dirent, unsigned int, count)
  149. {
  150. int error;
  151. struct fd f = fdget_pos(fd);
  152. struct readdir_callback buf = {
  153. .ctx.actor = fillonedir,
  154. .dirent = dirent
  155. };
  156. if (!f.file)
  157. return -EBADF;
  158. error = iterate_dir(f.file, &buf.ctx);
  159. if (buf.result)
  160. error = buf.result;
  161. fdput_pos(f);
  162. return error;
  163. }
  164. #endif /* __ARCH_WANT_OLD_READDIR */
  165. /*
  166. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  167. * interface.
  168. */
  169. struct linux_dirent {
  170. unsigned long d_ino;
  171. unsigned long d_off;
  172. unsigned short d_reclen;
  173. char d_name[1];
  174. };
  175. struct getdents_callback {
  176. struct dir_context ctx;
  177. struct linux_dirent __user * current_dir;
  178. struct linux_dirent __user * previous;
  179. int count;
  180. int error;
  181. };
  182. static int filldir(struct dir_context *ctx, const char *name, int namlen,
  183. loff_t offset, u64 ino, unsigned int d_type)
  184. {
  185. struct linux_dirent __user * dirent;
  186. struct getdents_callback *buf =
  187. container_of(ctx, struct getdents_callback, ctx);
  188. unsigned long d_ino;
  189. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  190. sizeof(long));
  191. buf->error = verify_dirent_name(name, namlen);
  192. if (unlikely(buf->error))
  193. return buf->error;
  194. buf->error = -EINVAL; /* only used if we fail.. */
  195. if (reclen > buf->count)
  196. return -EINVAL;
  197. d_ino = ino;
  198. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  199. buf->error = -EOVERFLOW;
  200. return -EOVERFLOW;
  201. }
  202. dirent = buf->previous;
  203. if (dirent) {
  204. if (signal_pending(current))
  205. return -EINTR;
  206. if (__put_user(offset, &dirent->d_off))
  207. goto efault;
  208. }
  209. dirent = buf->current_dir;
  210. if (__put_user(d_ino, &dirent->d_ino))
  211. goto efault;
  212. if (__put_user(reclen, &dirent->d_reclen))
  213. goto efault;
  214. if (copy_to_user(dirent->d_name, name, namlen))
  215. goto efault;
  216. if (__put_user(0, dirent->d_name + namlen))
  217. goto efault;
  218. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  219. goto efault;
  220. buf->previous = dirent;
  221. dirent = (void __user *)dirent + reclen;
  222. buf->current_dir = dirent;
  223. buf->count -= reclen;
  224. return 0;
  225. efault:
  226. buf->error = -EFAULT;
  227. return -EFAULT;
  228. }
  229. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  230. struct linux_dirent __user *, dirent, unsigned int, count)
  231. {
  232. struct fd f;
  233. struct linux_dirent __user * lastdirent;
  234. struct getdents_callback buf = {
  235. .ctx.actor = filldir,
  236. .count = count,
  237. .current_dir = dirent
  238. };
  239. int error;
  240. if (!access_ok(VERIFY_WRITE, dirent, count))
  241. return -EFAULT;
  242. f = fdget_pos(fd);
  243. if (!f.file)
  244. return -EBADF;
  245. error = iterate_dir(f.file, &buf.ctx);
  246. if (error >= 0)
  247. error = buf.error;
  248. lastdirent = buf.previous;
  249. if (lastdirent) {
  250. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  251. error = -EFAULT;
  252. else
  253. error = count - buf.count;
  254. }
  255. fdput_pos(f);
  256. return error;
  257. }
  258. struct getdents_callback64 {
  259. struct dir_context ctx;
  260. struct linux_dirent64 __user * current_dir;
  261. struct linux_dirent64 __user * previous;
  262. int count;
  263. int error;
  264. };
  265. static int filldir64(struct dir_context *ctx, const char *name, int namlen,
  266. loff_t offset, u64 ino, unsigned int d_type)
  267. {
  268. struct linux_dirent64 __user *dirent;
  269. struct getdents_callback64 *buf =
  270. container_of(ctx, struct getdents_callback64, ctx);
  271. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  272. sizeof(u64));
  273. buf->error = verify_dirent_name(name, namlen);
  274. if (unlikely(buf->error))
  275. return buf->error;
  276. buf->error = -EINVAL; /* only used if we fail.. */
  277. if (reclen > buf->count)
  278. return -EINVAL;
  279. dirent = buf->previous;
  280. if (dirent) {
  281. if (signal_pending(current))
  282. return -EINTR;
  283. if (__put_user(offset, &dirent->d_off))
  284. goto efault;
  285. }
  286. dirent = buf->current_dir;
  287. if (__put_user(ino, &dirent->d_ino))
  288. goto efault;
  289. if (__put_user(0, &dirent->d_off))
  290. goto efault;
  291. if (__put_user(reclen, &dirent->d_reclen))
  292. goto efault;
  293. if (__put_user(d_type, &dirent->d_type))
  294. goto efault;
  295. if (copy_to_user(dirent->d_name, name, namlen))
  296. goto efault;
  297. if (__put_user(0, dirent->d_name + namlen))
  298. goto efault;
  299. buf->previous = dirent;
  300. dirent = (void __user *)dirent + reclen;
  301. buf->current_dir = dirent;
  302. buf->count -= reclen;
  303. return 0;
  304. efault:
  305. buf->error = -EFAULT;
  306. return -EFAULT;
  307. }
  308. int ksys_getdents64(unsigned int fd, struct linux_dirent64 __user *dirent,
  309. unsigned int count)
  310. {
  311. struct fd f;
  312. struct linux_dirent64 __user * lastdirent;
  313. struct getdents_callback64 buf = {
  314. .ctx.actor = filldir64,
  315. .count = count,
  316. .current_dir = dirent
  317. };
  318. int error;
  319. if (!access_ok(VERIFY_WRITE, dirent, count))
  320. return -EFAULT;
  321. f = fdget_pos(fd);
  322. if (!f.file)
  323. return -EBADF;
  324. error = iterate_dir(f.file, &buf.ctx);
  325. if (error >= 0)
  326. error = buf.error;
  327. lastdirent = buf.previous;
  328. if (lastdirent) {
  329. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  330. if (__put_user(d_off, &lastdirent->d_off))
  331. error = -EFAULT;
  332. else
  333. error = count - buf.count;
  334. }
  335. fdput_pos(f);
  336. return error;
  337. }
  338. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  339. struct linux_dirent64 __user *, dirent, unsigned int, count)
  340. {
  341. return ksys_getdents64(fd, dirent, count);
  342. }
  343. #ifdef CONFIG_COMPAT
  344. struct compat_old_linux_dirent {
  345. compat_ulong_t d_ino;
  346. compat_ulong_t d_offset;
  347. unsigned short d_namlen;
  348. char d_name[1];
  349. };
  350. struct compat_readdir_callback {
  351. struct dir_context ctx;
  352. struct compat_old_linux_dirent __user *dirent;
  353. int result;
  354. };
  355. static int compat_fillonedir(struct dir_context *ctx, const char *name,
  356. int namlen, loff_t offset, u64 ino,
  357. unsigned int d_type)
  358. {
  359. struct compat_readdir_callback *buf =
  360. container_of(ctx, struct compat_readdir_callback, ctx);
  361. struct compat_old_linux_dirent __user *dirent;
  362. compat_ulong_t d_ino;
  363. if (buf->result)
  364. return -EINVAL;
  365. buf->result = verify_dirent_name(name, namlen);
  366. if (buf->result < 0)
  367. return buf->result;
  368. d_ino = ino;
  369. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  370. buf->result = -EOVERFLOW;
  371. return -EOVERFLOW;
  372. }
  373. buf->result++;
  374. dirent = buf->dirent;
  375. if (!access_ok(VERIFY_WRITE, dirent,
  376. (unsigned long)(dirent->d_name + namlen + 1) -
  377. (unsigned long)dirent))
  378. goto efault;
  379. if ( __put_user(d_ino, &dirent->d_ino) ||
  380. __put_user(offset, &dirent->d_offset) ||
  381. __put_user(namlen, &dirent->d_namlen) ||
  382. __copy_to_user(dirent->d_name, name, namlen) ||
  383. __put_user(0, dirent->d_name + namlen))
  384. goto efault;
  385. return 0;
  386. efault:
  387. buf->result = -EFAULT;
  388. return -EFAULT;
  389. }
  390. COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  391. struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
  392. {
  393. int error;
  394. struct fd f = fdget_pos(fd);
  395. struct compat_readdir_callback buf = {
  396. .ctx.actor = compat_fillonedir,
  397. .dirent = dirent
  398. };
  399. if (!f.file)
  400. return -EBADF;
  401. error = iterate_dir(f.file, &buf.ctx);
  402. if (buf.result)
  403. error = buf.result;
  404. fdput_pos(f);
  405. return error;
  406. }
  407. struct compat_linux_dirent {
  408. compat_ulong_t d_ino;
  409. compat_ulong_t d_off;
  410. unsigned short d_reclen;
  411. char d_name[1];
  412. };
  413. struct compat_getdents_callback {
  414. struct dir_context ctx;
  415. struct compat_linux_dirent __user *current_dir;
  416. struct compat_linux_dirent __user *previous;
  417. int count;
  418. int error;
  419. };
  420. static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
  421. loff_t offset, u64 ino, unsigned int d_type)
  422. {
  423. struct compat_linux_dirent __user * dirent;
  424. struct compat_getdents_callback *buf =
  425. container_of(ctx, struct compat_getdents_callback, ctx);
  426. compat_ulong_t d_ino;
  427. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  428. namlen + 2, sizeof(compat_long_t));
  429. buf->error = -EINVAL; /* only used if we fail.. */
  430. if (reclen > buf->count)
  431. return -EINVAL;
  432. d_ino = ino;
  433. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  434. buf->error = -EOVERFLOW;
  435. return -EOVERFLOW;
  436. }
  437. dirent = buf->previous;
  438. if (dirent) {
  439. if (signal_pending(current))
  440. return -EINTR;
  441. if (__put_user(offset, &dirent->d_off))
  442. goto efault;
  443. }
  444. dirent = buf->current_dir;
  445. if (__put_user(d_ino, &dirent->d_ino))
  446. goto efault;
  447. if (__put_user(reclen, &dirent->d_reclen))
  448. goto efault;
  449. if (copy_to_user(dirent->d_name, name, namlen))
  450. goto efault;
  451. if (__put_user(0, dirent->d_name + namlen))
  452. goto efault;
  453. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  454. goto efault;
  455. buf->previous = dirent;
  456. dirent = (void __user *)dirent + reclen;
  457. buf->current_dir = dirent;
  458. buf->count -= reclen;
  459. return 0;
  460. efault:
  461. buf->error = -EFAULT;
  462. return -EFAULT;
  463. }
  464. COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
  465. struct compat_linux_dirent __user *, dirent, unsigned int, count)
  466. {
  467. struct fd f;
  468. struct compat_linux_dirent __user * lastdirent;
  469. struct compat_getdents_callback buf = {
  470. .ctx.actor = compat_filldir,
  471. .current_dir = dirent,
  472. .count = count
  473. };
  474. int error;
  475. if (!access_ok(VERIFY_WRITE, dirent, count))
  476. return -EFAULT;
  477. f = fdget_pos(fd);
  478. if (!f.file)
  479. return -EBADF;
  480. error = iterate_dir(f.file, &buf.ctx);
  481. if (error >= 0)
  482. error = buf.error;
  483. lastdirent = buf.previous;
  484. if (lastdirent) {
  485. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  486. error = -EFAULT;
  487. else
  488. error = count - buf.count;
  489. }
  490. fdput_pos(f);
  491. return error;
  492. }
  493. #endif