readdir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. d_ino = ino;
  123. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  124. buf->result = -EOVERFLOW;
  125. return -EOVERFLOW;
  126. }
  127. buf->result++;
  128. dirent = buf->dirent;
  129. if (!access_ok(VERIFY_WRITE, dirent,
  130. (unsigned long)(dirent->d_name + namlen + 1) -
  131. (unsigned long)dirent))
  132. goto efault;
  133. if ( __put_user(d_ino, &dirent->d_ino) ||
  134. __put_user(offset, &dirent->d_offset) ||
  135. __put_user(namlen, &dirent->d_namlen) ||
  136. __copy_to_user(dirent->d_name, name, namlen) ||
  137. __put_user(0, dirent->d_name + namlen))
  138. goto efault;
  139. return 0;
  140. efault:
  141. buf->result = -EFAULT;
  142. return -EFAULT;
  143. }
  144. SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  145. struct old_linux_dirent __user *, dirent, unsigned int, count)
  146. {
  147. int error;
  148. struct fd f = fdget_pos(fd);
  149. struct readdir_callback buf = {
  150. .ctx.actor = fillonedir,
  151. .dirent = dirent
  152. };
  153. if (!f.file)
  154. return -EBADF;
  155. error = iterate_dir(f.file, &buf.ctx);
  156. if (buf.result)
  157. error = buf.result;
  158. fdput_pos(f);
  159. return error;
  160. }
  161. #endif /* __ARCH_WANT_OLD_READDIR */
  162. /*
  163. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  164. * interface.
  165. */
  166. struct linux_dirent {
  167. unsigned long d_ino;
  168. unsigned long d_off;
  169. unsigned short d_reclen;
  170. char d_name[1];
  171. };
  172. struct getdents_callback {
  173. struct dir_context ctx;
  174. struct linux_dirent __user * current_dir;
  175. struct linux_dirent __user * previous;
  176. int count;
  177. int error;
  178. };
  179. static int filldir(struct dir_context *ctx, const char *name, int namlen,
  180. loff_t offset, u64 ino, unsigned int d_type)
  181. {
  182. struct linux_dirent __user * dirent;
  183. struct getdents_callback *buf =
  184. container_of(ctx, struct getdents_callback, ctx);
  185. unsigned long d_ino;
  186. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  187. sizeof(long));
  188. buf->error = verify_dirent_name(name, namlen);
  189. if (unlikely(buf->error))
  190. return buf->error;
  191. buf->error = -EINVAL; /* only used if we fail.. */
  192. if (reclen > buf->count)
  193. return -EINVAL;
  194. d_ino = ino;
  195. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  196. buf->error = -EOVERFLOW;
  197. return -EOVERFLOW;
  198. }
  199. dirent = buf->previous;
  200. if (dirent) {
  201. if (signal_pending(current))
  202. return -EINTR;
  203. if (__put_user(offset, &dirent->d_off))
  204. goto efault;
  205. }
  206. dirent = buf->current_dir;
  207. if (__put_user(d_ino, &dirent->d_ino))
  208. goto efault;
  209. if (__put_user(reclen, &dirent->d_reclen))
  210. goto efault;
  211. if (copy_to_user(dirent->d_name, name, namlen))
  212. goto efault;
  213. if (__put_user(0, dirent->d_name + namlen))
  214. goto efault;
  215. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  216. goto efault;
  217. buf->previous = dirent;
  218. dirent = (void __user *)dirent + reclen;
  219. buf->current_dir = dirent;
  220. buf->count -= reclen;
  221. return 0;
  222. efault:
  223. buf->error = -EFAULT;
  224. return -EFAULT;
  225. }
  226. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  227. struct linux_dirent __user *, dirent, unsigned int, count)
  228. {
  229. struct fd f;
  230. struct linux_dirent __user * lastdirent;
  231. struct getdents_callback buf = {
  232. .ctx.actor = filldir,
  233. .count = count,
  234. .current_dir = dirent
  235. };
  236. int error;
  237. if (!access_ok(VERIFY_WRITE, dirent, count))
  238. return -EFAULT;
  239. f = fdget_pos(fd);
  240. if (!f.file)
  241. return -EBADF;
  242. error = iterate_dir(f.file, &buf.ctx);
  243. if (error >= 0)
  244. error = buf.error;
  245. lastdirent = buf.previous;
  246. if (lastdirent) {
  247. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  248. error = -EFAULT;
  249. else
  250. error = count - buf.count;
  251. }
  252. fdput_pos(f);
  253. return error;
  254. }
  255. struct getdents_callback64 {
  256. struct dir_context ctx;
  257. struct linux_dirent64 __user * current_dir;
  258. struct linux_dirent64 __user * previous;
  259. int count;
  260. int error;
  261. };
  262. static int filldir64(struct dir_context *ctx, const char *name, int namlen,
  263. loff_t offset, u64 ino, unsigned int d_type)
  264. {
  265. struct linux_dirent64 __user *dirent;
  266. struct getdents_callback64 *buf =
  267. container_of(ctx, struct getdents_callback64, ctx);
  268. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  269. sizeof(u64));
  270. buf->error = verify_dirent_name(name, namlen);
  271. if (unlikely(buf->error))
  272. return buf->error;
  273. buf->error = -EINVAL; /* only used if we fail.. */
  274. if (reclen > buf->count)
  275. return -EINVAL;
  276. dirent = buf->previous;
  277. if (dirent) {
  278. if (signal_pending(current))
  279. return -EINTR;
  280. if (__put_user(offset, &dirent->d_off))
  281. goto efault;
  282. }
  283. dirent = buf->current_dir;
  284. if (__put_user(ino, &dirent->d_ino))
  285. goto efault;
  286. if (__put_user(0, &dirent->d_off))
  287. goto efault;
  288. if (__put_user(reclen, &dirent->d_reclen))
  289. goto efault;
  290. if (__put_user(d_type, &dirent->d_type))
  291. goto efault;
  292. if (copy_to_user(dirent->d_name, name, namlen))
  293. goto efault;
  294. if (__put_user(0, dirent->d_name + namlen))
  295. goto efault;
  296. buf->previous = dirent;
  297. dirent = (void __user *)dirent + reclen;
  298. buf->current_dir = dirent;
  299. buf->count -= reclen;
  300. return 0;
  301. efault:
  302. buf->error = -EFAULT;
  303. return -EFAULT;
  304. }
  305. int ksys_getdents64(unsigned int fd, struct linux_dirent64 __user *dirent,
  306. unsigned int count)
  307. {
  308. struct fd f;
  309. struct linux_dirent64 __user * lastdirent;
  310. struct getdents_callback64 buf = {
  311. .ctx.actor = filldir64,
  312. .count = count,
  313. .current_dir = dirent
  314. };
  315. int error;
  316. if (!access_ok(VERIFY_WRITE, dirent, count))
  317. return -EFAULT;
  318. f = fdget_pos(fd);
  319. if (!f.file)
  320. return -EBADF;
  321. error = iterate_dir(f.file, &buf.ctx);
  322. if (error >= 0)
  323. error = buf.error;
  324. lastdirent = buf.previous;
  325. if (lastdirent) {
  326. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  327. if (__put_user(d_off, &lastdirent->d_off))
  328. error = -EFAULT;
  329. else
  330. error = count - buf.count;
  331. }
  332. fdput_pos(f);
  333. return error;
  334. }
  335. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  336. struct linux_dirent64 __user *, dirent, unsigned int, count)
  337. {
  338. return ksys_getdents64(fd, dirent, count);
  339. }
  340. #ifdef CONFIG_COMPAT
  341. struct compat_old_linux_dirent {
  342. compat_ulong_t d_ino;
  343. compat_ulong_t d_offset;
  344. unsigned short d_namlen;
  345. char d_name[1];
  346. };
  347. struct compat_readdir_callback {
  348. struct dir_context ctx;
  349. struct compat_old_linux_dirent __user *dirent;
  350. int result;
  351. };
  352. static int compat_fillonedir(struct dir_context *ctx, const char *name,
  353. int namlen, loff_t offset, u64 ino,
  354. unsigned int d_type)
  355. {
  356. struct compat_readdir_callback *buf =
  357. container_of(ctx, struct compat_readdir_callback, ctx);
  358. struct compat_old_linux_dirent __user *dirent;
  359. compat_ulong_t d_ino;
  360. if (buf->result)
  361. return -EINVAL;
  362. d_ino = ino;
  363. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  364. buf->result = -EOVERFLOW;
  365. return -EOVERFLOW;
  366. }
  367. buf->result++;
  368. dirent = buf->dirent;
  369. if (!access_ok(VERIFY_WRITE, dirent,
  370. (unsigned long)(dirent->d_name + namlen + 1) -
  371. (unsigned long)dirent))
  372. goto efault;
  373. if ( __put_user(d_ino, &dirent->d_ino) ||
  374. __put_user(offset, &dirent->d_offset) ||
  375. __put_user(namlen, &dirent->d_namlen) ||
  376. __copy_to_user(dirent->d_name, name, namlen) ||
  377. __put_user(0, dirent->d_name + namlen))
  378. goto efault;
  379. return 0;
  380. efault:
  381. buf->result = -EFAULT;
  382. return -EFAULT;
  383. }
  384. COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  385. struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
  386. {
  387. int error;
  388. struct fd f = fdget_pos(fd);
  389. struct compat_readdir_callback buf = {
  390. .ctx.actor = compat_fillonedir,
  391. .dirent = dirent
  392. };
  393. if (!f.file)
  394. return -EBADF;
  395. error = iterate_dir(f.file, &buf.ctx);
  396. if (buf.result)
  397. error = buf.result;
  398. fdput_pos(f);
  399. return error;
  400. }
  401. struct compat_linux_dirent {
  402. compat_ulong_t d_ino;
  403. compat_ulong_t d_off;
  404. unsigned short d_reclen;
  405. char d_name[1];
  406. };
  407. struct compat_getdents_callback {
  408. struct dir_context ctx;
  409. struct compat_linux_dirent __user *current_dir;
  410. struct compat_linux_dirent __user *previous;
  411. int count;
  412. int error;
  413. };
  414. static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
  415. loff_t offset, u64 ino, unsigned int d_type)
  416. {
  417. struct compat_linux_dirent __user * dirent;
  418. struct compat_getdents_callback *buf =
  419. container_of(ctx, struct compat_getdents_callback, ctx);
  420. compat_ulong_t d_ino;
  421. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  422. namlen + 2, sizeof(compat_long_t));
  423. buf->error = -EINVAL; /* only used if we fail.. */
  424. if (reclen > buf->count)
  425. return -EINVAL;
  426. d_ino = ino;
  427. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  428. buf->error = -EOVERFLOW;
  429. return -EOVERFLOW;
  430. }
  431. dirent = buf->previous;
  432. if (dirent) {
  433. if (signal_pending(current))
  434. return -EINTR;
  435. if (__put_user(offset, &dirent->d_off))
  436. goto efault;
  437. }
  438. dirent = buf->current_dir;
  439. if (__put_user(d_ino, &dirent->d_ino))
  440. goto efault;
  441. if (__put_user(reclen, &dirent->d_reclen))
  442. goto efault;
  443. if (copy_to_user(dirent->d_name, name, namlen))
  444. goto efault;
  445. if (__put_user(0, dirent->d_name + namlen))
  446. goto efault;
  447. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  448. goto efault;
  449. buf->previous = dirent;
  450. dirent = (void __user *)dirent + reclen;
  451. buf->current_dir = dirent;
  452. buf->count -= reclen;
  453. return 0;
  454. efault:
  455. buf->error = -EFAULT;
  456. return -EFAULT;
  457. }
  458. COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
  459. struct compat_linux_dirent __user *, dirent, unsigned int, count)
  460. {
  461. struct fd f;
  462. struct compat_linux_dirent __user * lastdirent;
  463. struct compat_getdents_callback buf = {
  464. .ctx.actor = compat_filldir,
  465. .current_dir = dirent,
  466. .count = count
  467. };
  468. int error;
  469. if (!access_ok(VERIFY_WRITE, dirent, count))
  470. return -EFAULT;
  471. f = fdget_pos(fd);
  472. if (!f.file)
  473. return -EBADF;
  474. error = iterate_dir(f.file, &buf.ctx);
  475. if (error >= 0)
  476. error = buf.error;
  477. lastdirent = buf.previous;
  478. if (lastdirent) {
  479. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  480. error = -EFAULT;
  481. else
  482. error = count - buf.count;
  483. }
  484. fdput_pos(f);
  485. return error;
  486. }
  487. #endif