readdir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <asm/uaccess.h>
  21. int iterate_dir(struct file *file, struct dir_context *ctx)
  22. {
  23. struct inode *inode = file_inode(file);
  24. int res = -ENOTDIR;
  25. if (!file->f_op->iterate)
  26. goto out;
  27. res = security_file_permission(file, MAY_READ);
  28. if (res)
  29. goto out;
  30. res = mutex_lock_killable(&inode->i_mutex);
  31. if (res)
  32. goto out;
  33. res = -ENOENT;
  34. if (!IS_DEADDIR(inode)) {
  35. ctx->pos = file->f_pos;
  36. res = file->f_op->iterate(file, ctx);
  37. file->f_pos = ctx->pos;
  38. fsnotify_access(file);
  39. file_accessed(file);
  40. }
  41. mutex_unlock(&inode->i_mutex);
  42. out:
  43. return res;
  44. }
  45. EXPORT_SYMBOL(iterate_dir);
  46. /*
  47. * Traditional linux readdir() handling..
  48. *
  49. * "count=1" is a special case, meaning that the buffer is one
  50. * dirent-structure in size and that the code can't handle more
  51. * anyway. Thus the special "fillonedir()" function for that
  52. * case (the low-level handlers don't need to care about this).
  53. */
  54. #ifdef __ARCH_WANT_OLD_READDIR
  55. struct old_linux_dirent {
  56. unsigned long d_ino;
  57. unsigned long d_offset;
  58. unsigned short d_namlen;
  59. char d_name[1];
  60. };
  61. struct readdir_callback {
  62. struct dir_context ctx;
  63. struct old_linux_dirent __user * dirent;
  64. int result;
  65. };
  66. static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
  67. loff_t offset, u64 ino, unsigned int d_type)
  68. {
  69. struct readdir_callback *buf =
  70. container_of(ctx, struct readdir_callback, ctx);
  71. struct old_linux_dirent __user * dirent;
  72. unsigned long d_ino;
  73. if (buf->result)
  74. return -EINVAL;
  75. d_ino = ino;
  76. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  77. buf->result = -EOVERFLOW;
  78. return -EOVERFLOW;
  79. }
  80. buf->result++;
  81. dirent = buf->dirent;
  82. if (!access_ok(VERIFY_WRITE, dirent,
  83. (unsigned long)(dirent->d_name + namlen + 1) -
  84. (unsigned long)dirent))
  85. goto efault;
  86. if ( __put_user(d_ino, &dirent->d_ino) ||
  87. __put_user(offset, &dirent->d_offset) ||
  88. __put_user(namlen, &dirent->d_namlen) ||
  89. __copy_to_user(dirent->d_name, name, namlen) ||
  90. __put_user(0, dirent->d_name + namlen))
  91. goto efault;
  92. return 0;
  93. efault:
  94. buf->result = -EFAULT;
  95. return -EFAULT;
  96. }
  97. SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  98. struct old_linux_dirent __user *, dirent, unsigned int, count)
  99. {
  100. int error;
  101. struct fd f = fdget(fd);
  102. struct readdir_callback buf = {
  103. .ctx.actor = fillonedir,
  104. .dirent = dirent
  105. };
  106. if (!f.file)
  107. return -EBADF;
  108. error = iterate_dir(f.file, &buf.ctx);
  109. if (buf.result)
  110. error = buf.result;
  111. fdput(f);
  112. return error;
  113. }
  114. #endif /* __ARCH_WANT_OLD_READDIR */
  115. /*
  116. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  117. * interface.
  118. */
  119. struct linux_dirent {
  120. unsigned long d_ino;
  121. unsigned long d_off;
  122. unsigned short d_reclen;
  123. char d_name[1];
  124. };
  125. struct getdents_callback {
  126. struct dir_context ctx;
  127. struct linux_dirent __user * current_dir;
  128. struct linux_dirent __user * previous;
  129. int count;
  130. int error;
  131. };
  132. static int filldir(struct dir_context *ctx, const char *name, int namlen,
  133. loff_t offset, u64 ino, unsigned int d_type)
  134. {
  135. struct linux_dirent __user * dirent;
  136. struct getdents_callback *buf =
  137. container_of(ctx, struct getdents_callback, ctx);
  138. unsigned long d_ino;
  139. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  140. sizeof(long));
  141. buf->error = -EINVAL; /* only used if we fail.. */
  142. if (reclen > buf->count)
  143. return -EINVAL;
  144. d_ino = ino;
  145. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  146. buf->error = -EOVERFLOW;
  147. return -EOVERFLOW;
  148. }
  149. dirent = buf->previous;
  150. if (dirent) {
  151. if (__put_user(offset, &dirent->d_off))
  152. goto efault;
  153. }
  154. dirent = buf->current_dir;
  155. if (__put_user(d_ino, &dirent->d_ino))
  156. goto efault;
  157. if (__put_user(reclen, &dirent->d_reclen))
  158. goto efault;
  159. if (copy_to_user(dirent->d_name, name, namlen))
  160. goto efault;
  161. if (__put_user(0, dirent->d_name + namlen))
  162. goto efault;
  163. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  164. goto efault;
  165. buf->previous = dirent;
  166. dirent = (void __user *)dirent + reclen;
  167. buf->current_dir = dirent;
  168. buf->count -= reclen;
  169. return 0;
  170. efault:
  171. buf->error = -EFAULT;
  172. return -EFAULT;
  173. }
  174. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  175. struct linux_dirent __user *, dirent, unsigned int, count)
  176. {
  177. struct fd f;
  178. struct linux_dirent __user * lastdirent;
  179. struct getdents_callback buf = {
  180. .ctx.actor = filldir,
  181. .count = count,
  182. .current_dir = dirent
  183. };
  184. int error;
  185. if (!access_ok(VERIFY_WRITE, dirent, count))
  186. return -EFAULT;
  187. f = fdget(fd);
  188. if (!f.file)
  189. return -EBADF;
  190. error = iterate_dir(f.file, &buf.ctx);
  191. if (error >= 0)
  192. error = buf.error;
  193. lastdirent = buf.previous;
  194. if (lastdirent) {
  195. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  196. error = -EFAULT;
  197. else
  198. error = count - buf.count;
  199. }
  200. fdput(f);
  201. return error;
  202. }
  203. struct getdents_callback64 {
  204. struct dir_context ctx;
  205. struct linux_dirent64 __user * current_dir;
  206. struct linux_dirent64 __user * previous;
  207. int count;
  208. int error;
  209. };
  210. static int filldir64(struct dir_context *ctx, const char *name, int namlen,
  211. loff_t offset, u64 ino, unsigned int d_type)
  212. {
  213. struct linux_dirent64 __user *dirent;
  214. struct getdents_callback64 *buf =
  215. container_of(ctx, struct getdents_callback64, ctx);
  216. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  217. sizeof(u64));
  218. buf->error = -EINVAL; /* only used if we fail.. */
  219. if (reclen > buf->count)
  220. return -EINVAL;
  221. dirent = buf->previous;
  222. if (dirent) {
  223. if (__put_user(offset, &dirent->d_off))
  224. goto efault;
  225. }
  226. dirent = buf->current_dir;
  227. if (__put_user(ino, &dirent->d_ino))
  228. goto efault;
  229. if (__put_user(0, &dirent->d_off))
  230. goto efault;
  231. if (__put_user(reclen, &dirent->d_reclen))
  232. goto efault;
  233. if (__put_user(d_type, &dirent->d_type))
  234. goto efault;
  235. if (copy_to_user(dirent->d_name, name, namlen))
  236. goto efault;
  237. if (__put_user(0, dirent->d_name + namlen))
  238. goto efault;
  239. buf->previous = dirent;
  240. dirent = (void __user *)dirent + reclen;
  241. buf->current_dir = dirent;
  242. buf->count -= reclen;
  243. return 0;
  244. efault:
  245. buf->error = -EFAULT;
  246. return -EFAULT;
  247. }
  248. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  249. struct linux_dirent64 __user *, dirent, unsigned int, count)
  250. {
  251. struct fd f;
  252. struct linux_dirent64 __user * lastdirent;
  253. struct getdents_callback64 buf = {
  254. .ctx.actor = filldir64,
  255. .count = count,
  256. .current_dir = dirent
  257. };
  258. int error;
  259. if (!access_ok(VERIFY_WRITE, dirent, count))
  260. return -EFAULT;
  261. f = fdget(fd);
  262. if (!f.file)
  263. return -EBADF;
  264. error = iterate_dir(f.file, &buf.ctx);
  265. if (error >= 0)
  266. error = buf.error;
  267. lastdirent = buf.previous;
  268. if (lastdirent) {
  269. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  270. if (__put_user(d_off, &lastdirent->d_off))
  271. error = -EFAULT;
  272. else
  273. error = count - buf.count;
  274. }
  275. fdput(f);
  276. return error;
  277. }