readdir.c 7.4 KB

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