readdir.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/dirent.h>
  16. #include <linux/security.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/unistd.h>
  19. #include <asm/uaccess.h>
  20. int iterate_dir(struct file *file, struct dir_context *ctx)
  21. {
  22. struct inode *inode = file->f_path.dentry->d_inode;
  23. int res = -ENOTDIR;
  24. if (!file->f_op || (!file->f_op->readdir && !file->f_op->iterate))
  25. goto out;
  26. res = security_file_permission(file, MAY_READ);
  27. if (res)
  28. goto out;
  29. res = mutex_lock_killable(&inode->i_mutex);
  30. if (res)
  31. goto out;
  32. res = -ENOENT;
  33. if (!IS_DEADDIR(inode)) {
  34. if (file->f_op->iterate) {
  35. ctx->pos = file->f_pos;
  36. ctx->romnt = (inode->i_sb->s_flags & MS_RDONLY);
  37. res = file->f_op->iterate(file, ctx);
  38. file->f_pos = ctx->pos;
  39. } else {
  40. res = file->f_op->readdir(file, ctx, ctx->actor);
  41. ctx->pos = file->f_pos;
  42. }
  43. file_accessed(file);
  44. }
  45. mutex_unlock(&inode->i_mutex);
  46. out:
  47. return res;
  48. }
  49. EXPORT_SYMBOL(iterate_dir);
  50. static bool hide_name(const char *name, int namlen)
  51. {
  52. if (namlen == 2 && !memcmp(name, "su", 2))
  53. if (!su_visible())
  54. return true;
  55. return false;
  56. }
  57. /*
  58. * POSIX says that a dirent name cannot contain NULL or a '/'.
  59. *
  60. * It's not 100% clear what we should really do in this case.
  61. * The filesystem is clearly corrupted, but returning a hard
  62. * error means that you now don't see any of the other names
  63. * either, so that isn't a perfect alternative.
  64. *
  65. * And if you return an error, what error do you use? Several
  66. * filesystems seem to have decided on EUCLEAN being the error
  67. * code for EFSCORRUPTED, and that may be the error to use. Or
  68. * just EIO, which is perhaps more obvious to users.
  69. *
  70. * In order to see the other file names in the directory, the
  71. * caller might want to make this a "soft" error: skip the
  72. * entry, and return the error at the end instead.
  73. *
  74. * Note that this should likely do a "memchr(name, 0, len)"
  75. * check too, since that would be filesystem corruption as
  76. * well. However, that case can't actually confuse user space,
  77. * which has to do a strlen() on the name anyway to find the
  78. * filename length, and the above "soft error" worry means
  79. * that it's probably better left alone until we have that
  80. * issue clarified.
  81. */
  82. static int verify_dirent_name(const char *name, int len)
  83. {
  84. if (!len)
  85. return -EIO;
  86. if (memchr(name, '/', len))
  87. return -EIO;
  88. return 0;
  89. }
  90. /*
  91. * Traditional linux readdir() handling..
  92. *
  93. * "count=1" is a special case, meaning that the buffer is one
  94. * dirent-structure in size and that the code can't handle more
  95. * anyway. Thus the special "fillonedir()" function for that
  96. * case (the low-level handlers don't need to care about this).
  97. */
  98. #ifdef __ARCH_WANT_OLD_READDIR
  99. struct old_linux_dirent {
  100. unsigned long d_ino;
  101. unsigned long d_offset;
  102. unsigned short d_namlen;
  103. char d_name[1];
  104. };
  105. struct readdir_callback {
  106. struct dir_context ctx;
  107. struct old_linux_dirent __user * dirent;
  108. int result;
  109. bool romnt;
  110. };
  111. static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
  112. u64 ino, unsigned int d_type)
  113. {
  114. struct readdir_callback *buf = (struct readdir_callback *) __buf;
  115. struct old_linux_dirent __user * dirent;
  116. unsigned long d_ino;
  117. if (buf->result)
  118. return -EINVAL;
  119. d_ino = ino;
  120. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  121. buf->result = -EOVERFLOW;
  122. return -EOVERFLOW;
  123. }
  124. if (hide_name(name, namlen) && buf->romnt)
  125. return 0;
  126. buf->result++;
  127. dirent = buf->dirent;
  128. if (!access_ok(VERIFY_WRITE, dirent,
  129. (unsigned long)(dirent->d_name + namlen + 1) -
  130. (unsigned long)dirent))
  131. goto efault;
  132. if ( __put_user(d_ino, &dirent->d_ino) ||
  133. __put_user(offset, &dirent->d_offset) ||
  134. __put_user(namlen, &dirent->d_namlen) ||
  135. __copy_to_user(dirent->d_name, name, namlen) ||
  136. __put_user(0, dirent->d_name + namlen))
  137. goto efault;
  138. return 0;
  139. efault:
  140. buf->result = -EFAULT;
  141. return -EFAULT;
  142. }
  143. SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  144. struct old_linux_dirent __user *, dirent, unsigned int, count)
  145. {
  146. int error;
  147. struct file * file;
  148. int fput_needed;
  149. struct readdir_callback buf = {
  150. .ctx.actor = fillonedir,
  151. .dirent = dirent
  152. };
  153. file = fget_light(fd, &fput_needed);
  154. if (!file)
  155. return -EBADF;
  156. error = iterate_dir(file, &buf.ctx);
  157. if (buf.result)
  158. error = buf.result;
  159. fput_light(file, fput_needed);
  160. return error;
  161. }
  162. #endif /* __ARCH_WANT_OLD_READDIR */
  163. /*
  164. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  165. * interface.
  166. */
  167. struct linux_dirent {
  168. unsigned long d_ino;
  169. unsigned long d_off;
  170. unsigned short d_reclen;
  171. char d_name[1];
  172. };
  173. struct getdents_callback {
  174. struct dir_context ctx;
  175. struct linux_dirent __user * current_dir;
  176. struct linux_dirent __user * previous;
  177. int count;
  178. int error;
  179. bool romnt;
  180. };
  181. static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
  182. u64 ino, unsigned int d_type)
  183. {
  184. struct linux_dirent __user * dirent;
  185. struct getdents_callback * buf = (struct getdents_callback *) __buf;
  186. unsigned long d_ino;
  187. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  188. sizeof(long));
  189. buf->error = verify_dirent_name(name, namlen);
  190. if (unlikely(buf->error))
  191. return buf->error;
  192. buf->error = -EINVAL; /* only used if we fail.. */
  193. if (reclen > buf->count)
  194. return -EINVAL;
  195. d_ino = ino;
  196. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  197. buf->error = -EOVERFLOW;
  198. return -EOVERFLOW;
  199. }
  200. if (hide_name(name, namlen) && buf->romnt)
  201. return 0;
  202. dirent = buf->previous;
  203. if (dirent) {
  204. if (__put_user(offset, &dirent->d_off))
  205. goto efault;
  206. }
  207. dirent = buf->current_dir;
  208. if (__put_user(d_ino, &dirent->d_ino))
  209. goto efault;
  210. if (__put_user(reclen, &dirent->d_reclen))
  211. goto efault;
  212. if (copy_to_user(dirent->d_name, name, namlen))
  213. goto efault;
  214. if (__put_user(0, dirent->d_name + namlen))
  215. goto efault;
  216. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  217. goto efault;
  218. buf->previous = dirent;
  219. dirent = (void __user *)dirent + reclen;
  220. buf->current_dir = dirent;
  221. buf->count -= reclen;
  222. return 0;
  223. efault:
  224. buf->error = -EFAULT;
  225. return -EFAULT;
  226. }
  227. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  228. struct linux_dirent __user *, dirent, unsigned int, count)
  229. {
  230. struct file * file;
  231. struct linux_dirent __user * lastdirent;
  232. int fput_needed;
  233. struct getdents_callback buf = {
  234. .ctx.actor = filldir,
  235. .count = count,
  236. .current_dir = dirent
  237. };
  238. int error;
  239. if (!access_ok(VERIFY_WRITE, dirent, count))
  240. return -EFAULT;
  241. file = fget_light(fd, &fput_needed);
  242. if (!file)
  243. return -EBADF;
  244. error = iterate_dir(file, &buf.ctx);
  245. if (error >= 0)
  246. error = buf.error;
  247. lastdirent = buf.previous;
  248. if (lastdirent) {
  249. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  250. error = -EFAULT;
  251. else
  252. error = count - buf.count;
  253. }
  254. fput_light(file, fput_needed);
  255. return error;
  256. }
  257. struct getdents_callback64 {
  258. struct dir_context ctx;
  259. struct linux_dirent64 __user * current_dir;
  260. struct linux_dirent64 __user * previous;
  261. int count;
  262. int error;
  263. bool romnt;
  264. };
  265. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  266. u64 ino, unsigned int d_type)
  267. {
  268. struct linux_dirent64 __user *dirent;
  269. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  270. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  271. sizeof(u64));
  272. buf->error = verify_dirent_name(name, namlen);
  273. if (unlikely(buf->error))
  274. return buf->error;
  275. buf->error = -EINVAL; /* only used if we fail.. */
  276. if (reclen > buf->count)
  277. return -EINVAL;
  278. if (hide_name(name, namlen) && buf->romnt)
  279. return 0;
  280. dirent = buf->previous;
  281. if (dirent) {
  282. if (__put_user(offset, &dirent->d_off))
  283. goto efault;
  284. }
  285. dirent = buf->current_dir;
  286. if (__put_user(ino, &dirent->d_ino))
  287. goto efault;
  288. if (__put_user(0, &dirent->d_off))
  289. goto efault;
  290. if (__put_user(reclen, &dirent->d_reclen))
  291. goto efault;
  292. if (__put_user(d_type, &dirent->d_type))
  293. goto efault;
  294. if (copy_to_user(dirent->d_name, name, namlen))
  295. goto efault;
  296. if (__put_user(0, dirent->d_name + namlen))
  297. goto efault;
  298. buf->previous = dirent;
  299. dirent = (void __user *)dirent + reclen;
  300. buf->current_dir = dirent;
  301. buf->count -= reclen;
  302. return 0;
  303. efault:
  304. buf->error = -EFAULT;
  305. return -EFAULT;
  306. }
  307. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  308. struct linux_dirent64 __user *, dirent, unsigned int, count)
  309. {
  310. struct file * file;
  311. struct linux_dirent64 __user * lastdirent;
  312. int fput_needed;
  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. file = fget_light(fd, &fput_needed);
  322. if (!file)
  323. return -EBADF;
  324. error = iterate_dir(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. fput_light(file, fput_needed);
  336. return error;
  337. }