blob: 536054080bc8da2bc5ffe8201f2522c669a8ea92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/readdir.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 */
6
Kevin Winchester85c9fe82010-08-09 17:20:22 -07007#include <linux/stddef.h>
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07008#include <linux/kernel.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/time.h>
11#include <linux/mm.h>
12#include <linux/errno.h>
13#include <linux/stat.h>
14#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fs.h>
16#include <linux/dirent.h>
17#include <linux/security.h>
18#include <linux/syscalls.h>
19#include <linux/unistd.h>
20
21#include <asm/uaccess.h>
22
23int vfs_readdir(struct file *file, filldir_t filler, void *buf)
24{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -080025 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int res = -ENOTDIR;
27 if (!file->f_op || !file->f_op->readdir)
28 goto out;
29
30 res = security_file_permission(file, MAY_READ);
31 if (res)
32 goto out;
33
Liam R. Howlettda784512007-12-06 17:39:54 -050034 res = mutex_lock_killable(&inode->i_mutex);
35 if (res)
36 goto out;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 res = -ENOENT;
39 if (!IS_DEADDIR(inode)) {
40 res = file->f_op->readdir(file, buf, filler);
41 file_accessed(file);
42 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080043 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044out:
45 return res;
46}
47
48EXPORT_SYMBOL(vfs_readdir);
49
Tom Marshall384972f2017-04-28 22:46:37 +000050static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Traditional linux readdir() handling..
60 *
61 * "count=1" is a special case, meaning that the buffer is one
62 * dirent-structure in size and that the code can't handle more
63 * anyway. Thus the special "fillonedir()" function for that
64 * case (the low-level handlers don't need to care about this).
65 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#ifdef __ARCH_WANT_OLD_READDIR
68
69struct 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
76struct readdir_callback {
77 struct old_linux_dirent __user * dirent;
78 int result;
Tom Marshall384972f2017-04-28 22:46:37 +000079 bool romnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
82static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -070083 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct readdir_callback * buf = (struct readdir_callback *) __buf;
86 struct old_linux_dirent __user * dirent;
David Howellsafefdbb2006-10-03 01:13:46 -070087 unsigned long d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (buf->result)
90 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -070091 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -040092 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
93 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -070094 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -040095 }
Tom Marshall384972f2017-04-28 22:46:37 +000096 if (hide_name(name, namlen) && buf->romnt)
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 buf->result++;
99 dirent = buf->dirent;
100 if (!access_ok(VERIFY_WRITE, dirent,
101 (unsigned long)(dirent->d_name + namlen + 1) -
102 (unsigned long)dirent))
103 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700104 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 __put_user(offset, &dirent->d_offset) ||
106 __put_user(namlen, &dirent->d_namlen) ||
107 __copy_to_user(dirent->d_name, name, namlen) ||
108 __put_user(0, dirent->d_name + namlen))
109 goto efault;
110 return 0;
111efault:
112 buf->result = -EFAULT;
113 return -EFAULT;
114}
115
Heiko Carstensd4e82042009-01-14 14:14:34 +0100116SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
117 struct old_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 int error;
120 struct file * file;
121 struct readdir_callback buf;
122
123 error = -EBADF;
124 file = fget(fd);
125 if (!file)
126 goto out;
127
128 buf.result = 0;
129 buf.dirent = dirent;
Tom Marshall384972f2017-04-28 22:46:37 +0000130 buf.romnt = (file->f_path.dentry->d_sb->s_flags & MS_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 error = vfs_readdir(file, fillonedir, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -0400133 if (buf.result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 error = buf.result;
135
136 fput(file);
137out:
138 return error;
139}
140
141#endif /* __ARCH_WANT_OLD_READDIR */
142
143/*
144 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
145 * interface.
146 */
147struct linux_dirent {
148 unsigned long d_ino;
149 unsigned long d_off;
150 unsigned short d_reclen;
151 char d_name[1];
152};
153
154struct getdents_callback {
155 struct linux_dirent __user * current_dir;
156 struct linux_dirent __user * previous;
157 int count;
158 int error;
Tom Marshall384972f2017-04-28 22:46:37 +0000159 bool romnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -0700163 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct linux_dirent __user * dirent;
166 struct getdents_callback * buf = (struct getdents_callback *) __buf;
David Howellsafefdbb2006-10-03 01:13:46 -0700167 unsigned long d_ino;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700168 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
169 sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 buf->error = -EINVAL; /* only used if we fail.. */
172 if (reclen > buf->count)
173 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700174 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400175 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
176 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700177 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400178 }
Tom Marshall384972f2017-04-28 22:46:37 +0000179 if (hide_name(name, namlen) && buf->romnt)
180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 dirent = buf->previous;
182 if (dirent) {
183 if (__put_user(offset, &dirent->d_off))
184 goto efault;
185 }
186 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700187 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto efault;
189 if (__put_user(reclen, &dirent->d_reclen))
190 goto efault;
191 if (copy_to_user(dirent->d_name, name, namlen))
192 goto efault;
193 if (__put_user(0, dirent->d_name + namlen))
194 goto efault;
195 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
196 goto efault;
197 buf->previous = dirent;
198 dirent = (void __user *)dirent + reclen;
199 buf->current_dir = dirent;
200 buf->count -= reclen;
201 return 0;
202efault:
203 buf->error = -EFAULT;
204 return -EFAULT;
205}
206
Heiko Carstens20f37032009-01-14 14:14:23 +0100207SYSCALL_DEFINE3(getdents, unsigned int, fd,
208 struct linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct file * file;
211 struct linux_dirent __user * lastdirent;
212 struct getdents_callback buf;
213 int error;
214
215 error = -EFAULT;
216 if (!access_ok(VERIFY_WRITE, dirent, count))
217 goto out;
218
219 error = -EBADF;
220 file = fget(fd);
221 if (!file)
222 goto out;
223
224 buf.current_dir = dirent;
225 buf.previous = NULL;
226 buf.count = count;
227 buf.error = 0;
Tom Marshall384972f2017-04-28 22:46:37 +0000228 buf.romnt = (file->f_path.dentry->d_sb->s_flags & MS_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 error = vfs_readdir(file, filldir, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -0400231 if (error >= 0)
232 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 lastdirent = buf.previous;
234 if (lastdirent) {
235 if (put_user(file->f_pos, &lastdirent->d_off))
236 error = -EFAULT;
237 else
238 error = count - buf.count;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 fput(file);
241out:
242 return error;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245struct getdents_callback64 {
246 struct linux_dirent64 __user * current_dir;
247 struct linux_dirent64 __user * previous;
248 int count;
249 int error;
Tom Marshall384972f2017-04-28 22:46:37 +0000250 bool romnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
David Howellsafefdbb2006-10-03 01:13:46 -0700254 u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct linux_dirent64 __user *dirent;
257 struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700258 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
259 sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 buf->error = -EINVAL; /* only used if we fail.. */
262 if (reclen > buf->count)
263 return -EINVAL;
Tom Marshall384972f2017-04-28 22:46:37 +0000264 if (hide_name(name, namlen) && buf->romnt)
265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dirent = buf->previous;
267 if (dirent) {
268 if (__put_user(offset, &dirent->d_off))
269 goto efault;
270 }
271 dirent = buf->current_dir;
272 if (__put_user(ino, &dirent->d_ino))
273 goto efault;
274 if (__put_user(0, &dirent->d_off))
275 goto efault;
276 if (__put_user(reclen, &dirent->d_reclen))
277 goto efault;
278 if (__put_user(d_type, &dirent->d_type))
279 goto efault;
280 if (copy_to_user(dirent->d_name, name, namlen))
281 goto efault;
282 if (__put_user(0, dirent->d_name + namlen))
283 goto efault;
284 buf->previous = dirent;
285 dirent = (void __user *)dirent + reclen;
286 buf->current_dir = dirent;
287 buf->count -= reclen;
288 return 0;
289efault:
290 buf->error = -EFAULT;
291 return -EFAULT;
292}
293
Heiko Carstens20f37032009-01-14 14:14:23 +0100294SYSCALL_DEFINE3(getdents64, unsigned int, fd,
295 struct linux_dirent64 __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct file * file;
298 struct linux_dirent64 __user * lastdirent;
299 struct getdents_callback64 buf;
300 int error;
301
302 error = -EFAULT;
303 if (!access_ok(VERIFY_WRITE, dirent, count))
304 goto out;
305
306 error = -EBADF;
307 file = fget(fd);
308 if (!file)
309 goto out;
310
311 buf.current_dir = dirent;
312 buf.previous = NULL;
313 buf.count = count;
314 buf.error = 0;
Tom Marshall384972f2017-04-28 22:46:37 +0000315 buf.romnt = (file->f_path.dentry->d_sb->s_flags & MS_RDONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 error = vfs_readdir(file, filldir64, &buf);
Al Viro53c9c5c2008-08-24 07:29:52 -0400318 if (error >= 0)
319 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 lastdirent = buf.previous;
321 if (lastdirent) {
322 typeof(lastdirent->d_off) d_off = file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (__put_user(d_off, &lastdirent->d_off))
Al Viro53c9c5c2008-08-24 07:29:52 -0400324 error = -EFAULT;
325 else
326 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 fput(file);
329out:
330 return error;
331}