readdir.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * linux/fs/readdir.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. #include <linux/stddef.h>
  7. #include <linux/kernel.h>
  8. #include <linux/export.h>
  9. #include <linux/time.h>
  10. #include <linux/mm.h>
  11. #include <linux/errno.h>
  12. #include <linux/stat.h>
  13. #include <linux/file.h>
  14. #include <linux/fs.h>
  15. #include <linux/fsnotify.h>
  16. #include <linux/dirent.h>
  17. #include <linux/security.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/unistd.h>
  20. #include <linux/compat.h>
  21. #include <linux/uaccess.h>
  22. int iterate_dir(struct file *file, struct dir_context *ctx)
  23. {
  24. struct inode *inode = file_inode(file);
  25. bool shared = false;
  26. int res = -ENOTDIR;
  27. if (file->f_op->iterate_shared)
  28. shared = true;
  29. else if (!file->f_op->iterate)
  30. goto out;
  31. res = security_file_permission(file, MAY_READ);
  32. if (res)
  33. goto out;
  34. if (shared) {
  35. inode_lock_shared(inode);
  36. } else {
  37. res = down_write_killable(&inode->i_rwsem);
  38. if (res)
  39. goto out;
  40. }
  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. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  267. struct linux_dirent64 __user *, dirent, 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. #ifdef CONFIG_COMPAT
  297. struct compat_old_linux_dirent {
  298. compat_ulong_t d_ino;
  299. compat_ulong_t d_offset;
  300. unsigned short d_namlen;
  301. char d_name[1];
  302. };
  303. struct compat_readdir_callback {
  304. struct dir_context ctx;
  305. struct compat_old_linux_dirent __user *dirent;
  306. int result;
  307. };
  308. static int compat_fillonedir(struct dir_context *ctx, const char *name,
  309. int namlen, loff_t offset, u64 ino,
  310. unsigned int d_type)
  311. {
  312. struct compat_readdir_callback *buf =
  313. container_of(ctx, struct compat_readdir_callback, ctx);
  314. struct compat_old_linux_dirent __user *dirent;
  315. compat_ulong_t d_ino;
  316. if (buf->result)
  317. return -EINVAL;
  318. d_ino = ino;
  319. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  320. buf->result = -EOVERFLOW;
  321. return -EOVERFLOW;
  322. }
  323. buf->result++;
  324. dirent = buf->dirent;
  325. if (!access_ok(VERIFY_WRITE, dirent,
  326. (unsigned long)(dirent->d_name + namlen + 1) -
  327. (unsigned long)dirent))
  328. goto efault;
  329. if ( __put_user(d_ino, &dirent->d_ino) ||
  330. __put_user(offset, &dirent->d_offset) ||
  331. __put_user(namlen, &dirent->d_namlen) ||
  332. __copy_to_user(dirent->d_name, name, namlen) ||
  333. __put_user(0, dirent->d_name + namlen))
  334. goto efault;
  335. return 0;
  336. efault:
  337. buf->result = -EFAULT;
  338. return -EFAULT;
  339. }
  340. COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  341. struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
  342. {
  343. int error;
  344. struct fd f = fdget_pos(fd);
  345. struct compat_readdir_callback buf = {
  346. .ctx.actor = compat_fillonedir,
  347. .dirent = dirent
  348. };
  349. if (!f.file)
  350. return -EBADF;
  351. error = iterate_dir(f.file, &buf.ctx);
  352. if (buf.result)
  353. error = buf.result;
  354. fdput_pos(f);
  355. return error;
  356. }
  357. struct compat_linux_dirent {
  358. compat_ulong_t d_ino;
  359. compat_ulong_t d_off;
  360. unsigned short d_reclen;
  361. char d_name[1];
  362. };
  363. struct compat_getdents_callback {
  364. struct dir_context ctx;
  365. struct compat_linux_dirent __user *current_dir;
  366. struct compat_linux_dirent __user *previous;
  367. int count;
  368. int error;
  369. };
  370. static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
  371. loff_t offset, u64 ino, unsigned int d_type)
  372. {
  373. struct compat_linux_dirent __user * dirent;
  374. struct compat_getdents_callback *buf =
  375. container_of(ctx, struct compat_getdents_callback, ctx);
  376. compat_ulong_t d_ino;
  377. int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
  378. namlen + 2, sizeof(compat_long_t));
  379. buf->error = -EINVAL; /* only used if we fail.. */
  380. if (reclen > buf->count)
  381. return -EINVAL;
  382. d_ino = ino;
  383. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  384. buf->error = -EOVERFLOW;
  385. return -EOVERFLOW;
  386. }
  387. dirent = buf->previous;
  388. if (dirent) {
  389. if (signal_pending(current))
  390. return -EINTR;
  391. if (__put_user(offset, &dirent->d_off))
  392. goto efault;
  393. }
  394. dirent = buf->current_dir;
  395. if (__put_user(d_ino, &dirent->d_ino))
  396. goto efault;
  397. if (__put_user(reclen, &dirent->d_reclen))
  398. goto efault;
  399. if (copy_to_user(dirent->d_name, name, namlen))
  400. goto efault;
  401. if (__put_user(0, dirent->d_name + namlen))
  402. goto efault;
  403. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  404. goto efault;
  405. buf->previous = dirent;
  406. dirent = (void __user *)dirent + reclen;
  407. buf->current_dir = dirent;
  408. buf->count -= reclen;
  409. return 0;
  410. efault:
  411. buf->error = -EFAULT;
  412. return -EFAULT;
  413. }
  414. COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
  415. struct compat_linux_dirent __user *, dirent, unsigned int, count)
  416. {
  417. struct fd f;
  418. struct compat_linux_dirent __user * lastdirent;
  419. struct compat_getdents_callback buf = {
  420. .ctx.actor = compat_filldir,
  421. .current_dir = dirent,
  422. .count = count
  423. };
  424. int error;
  425. if (!access_ok(VERIFY_WRITE, dirent, count))
  426. return -EFAULT;
  427. f = fdget_pos(fd);
  428. if (!f.file)
  429. return -EBADF;
  430. error = iterate_dir(f.file, &buf.ctx);
  431. if (error >= 0)
  432. error = buf.error;
  433. lastdirent = buf.previous;
  434. if (lastdirent) {
  435. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  436. error = -EFAULT;
  437. else
  438. error = count - buf.count;
  439. }
  440. fdput_pos(f);
  441. return error;
  442. }
  443. #endif