readdir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. * Traditional linux readdir() handling..
  62. *
  63. * "count=1" is a special case, meaning that the buffer is one
  64. * dirent-structure in size and that the code can't handle more
  65. * anyway. Thus the special "fillonedir()" function for that
  66. * case (the low-level handlers don't need to care about this).
  67. */
  68. #ifdef __ARCH_WANT_OLD_READDIR
  69. struct old_linux_dirent {
  70. unsigned long d_ino;
  71. unsigned long d_offset;
  72. unsigned short d_namlen;
  73. char d_name[1];
  74. };
  75. struct readdir_callback {
  76. struct dir_context ctx;
  77. struct old_linux_dirent __user * dirent;
  78. int result;
  79. };
  80. static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
  81. loff_t offset, u64 ino, unsigned int d_type)
  82. {
  83. struct readdir_callback *buf =
  84. container_of(ctx, struct readdir_callback, ctx);
  85. struct old_linux_dirent __user * dirent;
  86. unsigned long d_ino;
  87. if (buf->result)
  88. return -EINVAL;
  89. d_ino = ino;
  90. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  91. buf->result = -EOVERFLOW;
  92. return -EOVERFLOW;
  93. }
  94. buf->result++;
  95. dirent = buf->dirent;
  96. if (!access_ok(VERIFY_WRITE, dirent,
  97. (unsigned long)(dirent->d_name + namlen + 1) -
  98. (unsigned long)dirent))
  99. goto efault;
  100. if ( __put_user(d_ino, &dirent->d_ino) ||
  101. __put_user(offset, &dirent->d_offset) ||
  102. __put_user(namlen, &dirent->d_namlen) ||
  103. __copy_to_user(dirent->d_name, name, namlen) ||
  104. __put_user(0, dirent->d_name + namlen))
  105. goto efault;
  106. return 0;
  107. efault:
  108. buf->result = -EFAULT;
  109. return -EFAULT;
  110. }
  111. SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  112. struct old_linux_dirent __user *, dirent, unsigned int, count)
  113. {
  114. int error;
  115. struct fd f = fdget_pos(fd);
  116. struct readdir_callback buf = {
  117. .ctx.actor = fillonedir,
  118. .dirent = dirent
  119. };
  120. if (!f.file)
  121. return -EBADF;
  122. error = iterate_dir(f.file, &buf.ctx);
  123. if (buf.result)
  124. error = buf.result;
  125. fdput_pos(f);
  126. return error;
  127. }
  128. #endif /* __ARCH_WANT_OLD_READDIR */
  129. /*
  130. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  131. * interface.
  132. */
  133. struct linux_dirent {
  134. unsigned long d_ino;
  135. unsigned long d_off;
  136. unsigned short d_reclen;
  137. char d_name[1];
  138. };
  139. struct getdents_callback {
  140. struct dir_context ctx;
  141. struct linux_dirent __user * current_dir;
  142. struct linux_dirent __user * previous;
  143. int count;
  144. int error;
  145. };
  146. static int filldir(struct dir_context *ctx, const char *name, int namlen,
  147. loff_t offset, u64 ino, unsigned int d_type)
  148. {
  149. struct linux_dirent __user * dirent;
  150. struct getdents_callback *buf =
  151. container_of(ctx, struct getdents_callback, ctx);
  152. unsigned long d_ino;
  153. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  154. sizeof(long));
  155. buf->error = -EINVAL; /* only used if we fail.. */
  156. if (reclen > buf->count)
  157. return -EINVAL;
  158. d_ino = ino;
  159. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  160. buf->error = -EOVERFLOW;
  161. return -EOVERFLOW;
  162. }
  163. dirent = buf->previous;
  164. if (dirent) {
  165. if (signal_pending(current))
  166. return -EINTR;
  167. if (__put_user(offset, &dirent->d_off))
  168. goto efault;
  169. }
  170. dirent = buf->current_dir;
  171. if (__put_user(d_ino, &dirent->d_ino))
  172. goto efault;
  173. if (__put_user(reclen, &dirent->d_reclen))
  174. goto efault;
  175. if (copy_to_user(dirent->d_name, name, namlen))
  176. goto efault;
  177. if (__put_user(0, dirent->d_name + namlen))
  178. goto efault;
  179. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  180. goto efault;
  181. buf->previous = dirent;
  182. dirent = (void __user *)dirent + reclen;
  183. buf->current_dir = dirent;
  184. buf->count -= reclen;
  185. return 0;
  186. efault:
  187. buf->error = -EFAULT;
  188. return -EFAULT;
  189. }
  190. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  191. struct linux_dirent __user *, dirent, unsigned int, count)
  192. {
  193. struct fd f;
  194. struct linux_dirent __user * lastdirent;
  195. struct getdents_callback buf = {
  196. .ctx.actor = filldir,
  197. .count = count,
  198. .current_dir = dirent
  199. };
  200. int error;
  201. if (!access_ok(VERIFY_WRITE, dirent, count))
  202. return -EFAULT;
  203. f = fdget_pos(fd);
  204. if (!f.file)
  205. return -EBADF;
  206. error = iterate_dir(f.file, &buf.ctx);
  207. if (error >= 0)
  208. error = buf.error;
  209. lastdirent = buf.previous;
  210. if (lastdirent) {
  211. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  212. error = -EFAULT;
  213. else
  214. error = count - buf.count;
  215. }
  216. fdput_pos(f);
  217. return error;
  218. }
  219. struct getdents_callback64 {
  220. struct dir_context ctx;
  221. struct linux_dirent64 __user * current_dir;
  222. struct linux_dirent64 __user * previous;
  223. int count;
  224. int error;
  225. };
  226. static int filldir64(struct dir_context *ctx, const char *name, int namlen,
  227. loff_t offset, u64 ino, unsigned int d_type)
  228. {
  229. struct linux_dirent64 __user *dirent;
  230. struct getdents_callback64 *buf =
  231. container_of(ctx, struct getdents_callback64, ctx);
  232. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  233. sizeof(u64));
  234. buf->error = -EINVAL; /* only used if we fail.. */
  235. if (reclen > buf->count)
  236. return -EINVAL;
  237. dirent = buf->previous;
  238. if (dirent) {
  239. if (signal_pending(current))
  240. return -EINTR;
  241. if (__put_user(offset, &dirent->d_off))
  242. goto efault;
  243. }
  244. dirent = buf->current_dir;
  245. if (__put_user(ino, &dirent->d_ino))
  246. goto efault;
  247. if (__put_user(0, &dirent->d_off))
  248. goto efault;
  249. if (__put_user(reclen, &dirent->d_reclen))
  250. goto efault;
  251. if (__put_user(d_type, &dirent->d_type))
  252. goto efault;
  253. if (copy_to_user(dirent->d_name, name, namlen))
  254. goto efault;
  255. if (__put_user(0, dirent->d_name + namlen))
  256. goto efault;
  257. buf->previous = dirent;
  258. dirent = (void __user *)dirent + reclen;
  259. buf->current_dir = dirent;
  260. buf->count -= reclen;
  261. return 0;
  262. efault:
  263. buf->error = -EFAULT;
  264. return -EFAULT;
  265. }
  266. int ksys_getdents64(unsigned int fd, struct linux_dirent64 __user *dirent,
  267. unsigned int count)
  268. {
  269. struct fd f;
  270. struct linux_dirent64 __user * lastdirent;
  271. struct getdents_callback64 buf = {
  272. .ctx.actor = filldir64,
  273. .count = count,
  274. .current_dir = dirent
  275. };
  276. int error;
  277. if (!access_ok(VERIFY_WRITE, dirent, count))
  278. return -EFAULT;
  279. f = fdget_pos(fd);
  280. if (!f.file)
  281. return -EBADF;
  282. error = iterate_dir(f.file, &buf.ctx);
  283. if (error >= 0)
  284. error = buf.error;
  285. lastdirent = buf.previous;
  286. if (lastdirent) {
  287. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  288. if (__put_user(d_off, &lastdirent->d_off))
  289. error = -EFAULT;
  290. else
  291. error = count - buf.count;
  292. }
  293. fdput_pos(f);
  294. return error;
  295. }
  296. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  297. struct linux_dirent64 __user *, dirent, unsigned int, count)
  298. {
  299. return ksys_getdents64(fd, dirent, count);
  300. }
  301. #ifdef CONFIG_COMPAT
  302. struct compat_old_linux_dirent {
  303. compat_ulong_t d_ino;
  304. compat_ulong_t d_offset;
  305. unsigned short d_namlen;
  306. char d_name[1];
  307. };
  308. struct compat_readdir_callback {
  309. struct dir_context ctx;
  310. struct compat_old_linux_dirent __user *dirent;
  311. int result;
  312. };
  313. static int compat_fillonedir(struct dir_context *ctx, const char *name,
  314. int namlen, loff_t offset, u64 ino,
  315. unsigned int d_type)
  316. {
  317. struct compat_readdir_callback *buf =
  318. container_of(ctx, struct compat_readdir_callback, ctx);
  319. struct compat_old_linux_dirent __user *dirent;
  320. compat_ulong_t d_ino;
  321. if (buf->result)
  322. return -EINVAL;
  323. d_ino = ino;
  324. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  325. buf->result = -EOVERFLOW;
  326. return -EOVERFLOW;
  327. }
  328. buf->result++;
  329. dirent = buf->dirent;
  330. if (!access_ok(VERIFY_WRITE, dirent,
  331. (unsigned long)(dirent->d_name + namlen + 1) -
  332. (unsigned long)dirent))
  333. goto efault;
  334. if ( __put_user(d_ino, &dirent->d_ino) ||
  335. __put_user(offset, &dirent->d_offset) ||
  336. __put_user(namlen, &dirent->d_namlen) ||
  337. __copy_to_user(dirent->d_name, name, namlen) ||
  338. __put_user(0, dirent->d_name + namlen))
  339. goto efault;
  340. return 0;
  341. efault:
  342. buf->result = -EFAULT;
  343. return -EFAULT;
  344. }
  345. COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  346. struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
  347. {
  348. int error;
  349. struct fd f = fdget_pos(fd);
  350. struct compat_readdir_callback buf = {
  351. .ctx.actor = compat_fillonedir,
  352. .dirent = dirent
  353. };
  354. if (!f.file)
  355. return -EBADF;
  356. error = iterate_dir(f.file, &buf.ctx);
  357. if (buf.result)
  358. error = buf.result;
  359. fdput_pos(f);
  360. return error;
  361. }
  362. struct compat_linux_dirent {
  363. compat_ulong_t d_ino;
  364. compat_ulong_t d_off;
  365. unsigned short d_reclen;
  366. char d_name[1];
  367. };
  368. struct compat_getdents_callback {
  369. struct dir_context ctx;
  370. struct compat_linux_dirent __user *current_dir;
  371. struct compat_linux_dirent __user *previous;
  372. int count;
  373. int error;
  374. };
  375. static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
  376. loff_t offset, u64 ino, unsigned int d_type)
  377. {
  378. struct compat_linux_dirent __user * dirent;
  379. struct compat_getdents_callback *buf =
  380. container_of(ctx, struct compat_getdents_callback, ctx);
  381. compat_ulong_t d_ino;
  382. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  383. namlen + 2, sizeof(compat_long_t));
  384. buf->error = -EINVAL; /* only used if we fail.. */
  385. if (reclen > buf->count)
  386. return -EINVAL;
  387. d_ino = ino;
  388. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  389. buf->error = -EOVERFLOW;
  390. return -EOVERFLOW;
  391. }
  392. dirent = buf->previous;
  393. if (dirent) {
  394. if (signal_pending(current))
  395. return -EINTR;
  396. if (__put_user(offset, &dirent->d_off))
  397. goto efault;
  398. }
  399. dirent = buf->current_dir;
  400. if (__put_user(d_ino, &dirent->d_ino))
  401. goto efault;
  402. if (__put_user(reclen, &dirent->d_reclen))
  403. goto efault;
  404. if (copy_to_user(dirent->d_name, name, namlen))
  405. goto efault;
  406. if (__put_user(0, dirent->d_name + namlen))
  407. goto efault;
  408. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  409. goto efault;
  410. buf->previous = dirent;
  411. dirent = (void __user *)dirent + reclen;
  412. buf->current_dir = dirent;
  413. buf->count -= reclen;
  414. return 0;
  415. efault:
  416. buf->error = -EFAULT;
  417. return -EFAULT;
  418. }
  419. COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
  420. struct compat_linux_dirent __user *, dirent, unsigned int, count)
  421. {
  422. struct fd f;
  423. struct compat_linux_dirent __user * lastdirent;
  424. struct compat_getdents_callback buf = {
  425. .ctx.actor = compat_filldir,
  426. .current_dir = dirent,
  427. .count = count
  428. };
  429. int error;
  430. if (!access_ok(VERIFY_WRITE, dirent, count))
  431. return -EFAULT;
  432. f = fdget_pos(fd);
  433. if (!f.file)
  434. return -EBADF;
  435. error = iterate_dir(f.file, &buf.ctx);
  436. if (error >= 0)
  437. error = buf.error;
  438. lastdirent = buf.previous;
  439. if (lastdirent) {
  440. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  441. error = -EFAULT;
  442. else
  443. error = count - buf.count;
  444. }
  445. fdput_pos(f);
  446. return error;
  447. }
  448. #endif