readdir.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. struct readdir_callback buf = {
  149. .ctx.actor = fillonedir,
  150. .dirent = dirent
  151. };
  152. error = -EBADF;
  153. file = fget(fd);
  154. if (!file)
  155. goto out;
  156. error = iterate_dir(file, &buf.ctx);
  157. if (buf.result)
  158. error = buf.result;
  159. fput(file);
  160. out:
  161. return error;
  162. }
  163. #endif /* __ARCH_WANT_OLD_READDIR */
  164. /*
  165. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  166. * interface.
  167. */
  168. struct linux_dirent {
  169. unsigned long d_ino;
  170. unsigned long d_off;
  171. unsigned short d_reclen;
  172. char d_name[1];
  173. };
  174. struct getdents_callback {
  175. struct dir_context ctx;
  176. struct linux_dirent __user * current_dir;
  177. struct linux_dirent __user * previous;
  178. int count;
  179. int error;
  180. bool romnt;
  181. };
  182. static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
  183. u64 ino, unsigned int d_type)
  184. {
  185. struct linux_dirent __user * dirent;
  186. struct getdents_callback * buf = (struct getdents_callback *) __buf;
  187. unsigned long d_ino;
  188. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  189. sizeof(long));
  190. buf->error = verify_dirent_name(name, namlen);
  191. if (unlikely(buf->error))
  192. return buf->error;
  193. buf->error = -EINVAL; /* only used if we fail.. */
  194. if (reclen > buf->count)
  195. return -EINVAL;
  196. d_ino = ino;
  197. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  198. buf->error = -EOVERFLOW;
  199. return -EOVERFLOW;
  200. }
  201. if (hide_name(name, namlen) && buf->romnt)
  202. return 0;
  203. dirent = buf->previous;
  204. if (dirent) {
  205. if (__put_user(offset, &dirent->d_off))
  206. goto efault;
  207. }
  208. dirent = buf->current_dir;
  209. if (__put_user(d_ino, &dirent->d_ino))
  210. goto efault;
  211. if (__put_user(reclen, &dirent->d_reclen))
  212. goto efault;
  213. if (copy_to_user(dirent->d_name, name, namlen))
  214. goto efault;
  215. if (__put_user(0, dirent->d_name + namlen))
  216. goto efault;
  217. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  218. goto efault;
  219. buf->previous = dirent;
  220. dirent = (void __user *)dirent + reclen;
  221. buf->current_dir = dirent;
  222. buf->count -= reclen;
  223. return 0;
  224. efault:
  225. buf->error = -EFAULT;
  226. return -EFAULT;
  227. }
  228. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  229. struct linux_dirent __user *, dirent, unsigned int, count)
  230. {
  231. struct file * file;
  232. struct linux_dirent __user * lastdirent;
  233. struct getdents_callback buf = {
  234. .ctx.actor = filldir,
  235. .count = count,
  236. .current_dir = dirent
  237. };
  238. int error;
  239. error = -EFAULT;
  240. if (!access_ok(VERIFY_WRITE, dirent, count))
  241. goto out;
  242. error = -EBADF;
  243. file = fget(fd);
  244. if (!file)
  245. goto out;
  246. error = iterate_dir(file, &buf.ctx);
  247. if (error >= 0)
  248. error = buf.error;
  249. lastdirent = buf.previous;
  250. if (lastdirent) {
  251. if (put_user(buf.ctx.pos, &lastdirent->d_off))
  252. error = -EFAULT;
  253. else
  254. error = count - buf.count;
  255. }
  256. fput(file);
  257. out:
  258. return error;
  259. }
  260. struct getdents_callback64 {
  261. struct dir_context ctx;
  262. struct linux_dirent64 __user * current_dir;
  263. struct linux_dirent64 __user * previous;
  264. int count;
  265. int error;
  266. bool romnt;
  267. };
  268. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  269. u64 ino, unsigned int d_type)
  270. {
  271. struct linux_dirent64 __user *dirent;
  272. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  273. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  274. sizeof(u64));
  275. buf->error = verify_dirent_name(name, namlen);
  276. if (unlikely(buf->error))
  277. return buf->error;
  278. buf->error = -EINVAL; /* only used if we fail.. */
  279. if (reclen > buf->count)
  280. return -EINVAL;
  281. if (hide_name(name, namlen) && buf->romnt)
  282. return 0;
  283. dirent = buf->previous;
  284. if (dirent) {
  285. if (__put_user(offset, &dirent->d_off))
  286. goto efault;
  287. }
  288. dirent = buf->current_dir;
  289. if (__put_user(ino, &dirent->d_ino))
  290. goto efault;
  291. if (__put_user(0, &dirent->d_off))
  292. goto efault;
  293. if (__put_user(reclen, &dirent->d_reclen))
  294. goto efault;
  295. if (__put_user(d_type, &dirent->d_type))
  296. goto efault;
  297. if (copy_to_user(dirent->d_name, name, namlen))
  298. goto efault;
  299. if (__put_user(0, dirent->d_name + namlen))
  300. goto efault;
  301. buf->previous = dirent;
  302. dirent = (void __user *)dirent + reclen;
  303. buf->current_dir = dirent;
  304. buf->count -= reclen;
  305. return 0;
  306. efault:
  307. buf->error = -EFAULT;
  308. return -EFAULT;
  309. }
  310. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  311. struct linux_dirent64 __user *, dirent, unsigned int, count)
  312. {
  313. struct file * file;
  314. struct linux_dirent64 __user * lastdirent;
  315. struct getdents_callback64 buf = {
  316. .ctx.actor = filldir64,
  317. .count = count,
  318. .current_dir = dirent
  319. };
  320. int error;
  321. error = -EFAULT;
  322. if (!access_ok(VERIFY_WRITE, dirent, count))
  323. goto out;
  324. error = -EBADF;
  325. file = fget(fd);
  326. if (!file)
  327. goto out;
  328. error = iterate_dir(file, &buf.ctx);
  329. if (error >= 0)
  330. error = buf.error;
  331. lastdirent = buf.previous;
  332. if (lastdirent) {
  333. typeof(lastdirent->d_off) d_off = buf.ctx.pos;
  334. if (__put_user(d_off, &lastdirent->d_off))
  335. error = -EFAULT;
  336. else
  337. error = count - buf.count;
  338. }
  339. fput(file);
  340. out:
  341. return error;
  342. }