blob: 1059f2a9be0b20d6cfa24c014dfd482469398f81 [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>
Heinrich Schuchardtd4c7cf62014-06-04 16:05:41 -070016#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/dirent.h>
18#include <linux/security.h>
19#include <linux/syscalls.h>
20#include <linux/unistd.h>
21
22#include <asm/uaccess.h>
23
Al Viro5c0ba4e2013-05-15 13:52:59 -040024int iterate_dir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Al Viro496ad9a2013-01-23 17:07:38 -050026 struct inode *inode = file_inode(file);
Al Viro61922692016-04-20 23:08:32 -040027 bool shared = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 int res = -ENOTDIR;
Al Viro61922692016-04-20 23:08:32 -040029 if (file->f_op->iterate_shared)
30 shared = true;
31 else if (!file->f_op->iterate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 goto out;
33
34 res = security_file_permission(file, MAY_READ);
35 if (res)
36 goto out;
37
Al Viro00235412016-05-26 00:05:12 -040038 if (shared) {
Al Viro61922692016-04-20 23:08:32 -040039 inode_lock_shared(inode);
Al Viro00235412016-05-26 00:05:12 -040040 } else {
41 res = down_write_killable(&inode->i_rwsem);
42 if (res)
43 goto out;
44 }
Liam R. Howlettda784512007-12-06 17:39:54 -050045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 res = -ENOENT;
47 if (!IS_DEADDIR(inode)) {
Al Viro2233f312013-05-22 21:44:23 -040048 ctx->pos = file->f_pos;
Al Viro61922692016-04-20 23:08:32 -040049 if (shared)
50 res = file->f_op->iterate_shared(file, ctx);
51 else
52 res = file->f_op->iterate(file, ctx);
Al Viro2233f312013-05-22 21:44:23 -040053 file->f_pos = ctx->pos;
Heinrich Schuchardtd4c7cf62014-06-04 16:05:41 -070054 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 file_accessed(file);
56 }
Al Viro61922692016-04-20 23:08:32 -040057 if (shared)
58 inode_unlock_shared(inode);
59 else
60 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061out:
62 return res;
63}
Al Viro5c0ba4e2013-05-15 13:52:59 -040064EXPORT_SYMBOL(iterate_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
Linus Torvalds89f58402019-10-05 11:32:52 -070067 * POSIX says that a dirent name cannot contain NULL or a '/'.
68 *
69 * It's not 100% clear what we should really do in this case.
70 * The filesystem is clearly corrupted, but returning a hard
71 * error means that you now don't see any of the other names
72 * either, so that isn't a perfect alternative.
73 *
74 * And if you return an error, what error do you use? Several
75 * filesystems seem to have decided on EUCLEAN being the error
76 * code for EFSCORRUPTED, and that may be the error to use. Or
77 * just EIO, which is perhaps more obvious to users.
78 *
79 * In order to see the other file names in the directory, the
80 * caller might want to make this a "soft" error: skip the
81 * entry, and return the error at the end instead.
82 *
83 * Note that this should likely do a "memchr(name, 0, len)"
84 * check too, since that would be filesystem corruption as
85 * well. However, that case can't actually confuse user space,
86 * which has to do a strlen() on the name anyway to find the
87 * filename length, and the above "soft error" worry means
88 * that it's probably better left alone until we have that
89 * issue clarified.
90 */
91static int verify_dirent_name(const char *name, int len)
92{
Linus Torvalds1944687182019-10-18 18:41:16 -040093 if (!len)
Linus Torvalds89f58402019-10-05 11:32:52 -070094 return -EIO;
Linus Torvalds1944687182019-10-18 18:41:16 -040095 if (memchr(name, '/', len))
Linus Torvalds89f58402019-10-05 11:32:52 -070096 return -EIO;
97 return 0;
98}
99
100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * Traditional linux readdir() handling..
102 *
103 * "count=1" is a special case, meaning that the buffer is one
104 * dirent-structure in size and that the code can't handle more
105 * anyway. Thus the special "fillonedir()" function for that
106 * case (the low-level handlers don't need to care about this).
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109#ifdef __ARCH_WANT_OLD_READDIR
110
111struct old_linux_dirent {
112 unsigned long d_ino;
113 unsigned long d_offset;
114 unsigned short d_namlen;
115 char d_name[1];
116};
117
118struct readdir_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400119 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct old_linux_dirent __user * dirent;
121 int result;
122};
123
Miklos Szerediac7576f2014-10-30 17:37:34 +0100124static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
125 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100127 struct readdir_callback *buf =
128 container_of(ctx, struct readdir_callback, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct old_linux_dirent __user * dirent;
David Howellsafefdbb2006-10-03 01:13:46 -0700130 unsigned long d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (buf->result)
133 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700134 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400135 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
136 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700137 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 buf->result++;
140 dirent = buf->dirent;
141 if (!access_ok(VERIFY_WRITE, dirent,
142 (unsigned long)(dirent->d_name + namlen + 1) -
143 (unsigned long)dirent))
144 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700145 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 __put_user(offset, &dirent->d_offset) ||
147 __put_user(namlen, &dirent->d_namlen) ||
148 __copy_to_user(dirent->d_name, name, namlen) ||
149 __put_user(0, dirent->d_name + namlen))
150 goto efault;
151 return 0;
152efault:
153 buf->result = -EFAULT;
154 return -EFAULT;
155}
156
Heiko Carstensd4e82042009-01-14 14:14:34 +0100157SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
158 struct old_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 int error;
Al Viro63b6df12016-04-20 17:08:21 -0400161 struct fd f = fdget_pos(fd);
Al Viroac6614b2013-05-22 22:22:04 -0400162 struct readdir_callback buf = {
163 .ctx.actor = fillonedir,
164 .dirent = dirent
165 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Al Viro2903ff02012-08-28 12:52:22 -0400167 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400168 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Al Viro5c0ba4e2013-05-15 13:52:59 -0400170 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400171 if (buf.result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 error = buf.result;
173
Al Viro63b6df12016-04-20 17:08:21 -0400174 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return error;
176}
177
178#endif /* __ARCH_WANT_OLD_READDIR */
179
180/*
181 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
182 * interface.
183 */
184struct linux_dirent {
185 unsigned long d_ino;
186 unsigned long d_off;
187 unsigned short d_reclen;
188 char d_name[1];
189};
190
191struct getdents_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400192 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct linux_dirent __user * current_dir;
194 struct linux_dirent __user * previous;
195 int count;
196 int error;
197};
198
Miklos Szerediac7576f2014-10-30 17:37:34 +0100199static int filldir(struct dir_context *ctx, const char *name, int namlen,
200 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct linux_dirent __user * dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100203 struct getdents_callback *buf =
204 container_of(ctx, struct getdents_callback, ctx);
David Howellsafefdbb2006-10-03 01:13:46 -0700205 unsigned long d_ino;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700206 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
207 sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds89f58402019-10-05 11:32:52 -0700209 buf->error = verify_dirent_name(name, namlen);
210 if (unlikely(buf->error))
211 return buf->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 buf->error = -EINVAL; /* only used if we fail.. */
213 if (reclen > buf->count)
214 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700215 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400216 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
217 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700218 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 dirent = buf->previous;
221 if (dirent) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400222 if (signal_pending(current))
223 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (__put_user(offset, &dirent->d_off))
225 goto efault;
226 }
227 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700228 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto efault;
230 if (__put_user(reclen, &dirent->d_reclen))
231 goto efault;
232 if (copy_to_user(dirent->d_name, name, namlen))
233 goto efault;
234 if (__put_user(0, dirent->d_name + namlen))
235 goto efault;
236 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
237 goto efault;
238 buf->previous = dirent;
239 dirent = (void __user *)dirent + reclen;
240 buf->current_dir = dirent;
241 buf->count -= reclen;
242 return 0;
243efault:
244 buf->error = -EFAULT;
245 return -EFAULT;
246}
247
Heiko Carstens20f37032009-01-14 14:14:23 +0100248SYSCALL_DEFINE3(getdents, unsigned int, fd,
249 struct linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Al Viro2903ff02012-08-28 12:52:22 -0400251 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct linux_dirent __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400253 struct getdents_callback buf = {
254 .ctx.actor = filldir,
255 .count = count,
256 .current_dir = dirent
257 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int error;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400261 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Al Viro63b6df12016-04-20 17:08:21 -0400263 f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400264 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400265 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Al Viro5c0ba4e2013-05-15 13:52:59 -0400267 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400268 if (error >= 0)
269 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 lastdirent = buf.previous;
271 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400272 if (put_user(buf.ctx.pos, &lastdirent->d_off))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 error = -EFAULT;
274 else
275 error = count - buf.count;
276 }
Al Viro63b6df12016-04-20 17:08:21 -0400277 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return error;
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281struct getdents_callback64 {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400282 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct linux_dirent64 __user * current_dir;
284 struct linux_dirent64 __user * previous;
285 int count;
286 int error;
287};
288
Miklos Szerediac7576f2014-10-30 17:37:34 +0100289static int filldir64(struct dir_context *ctx, const char *name, int namlen,
290 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct linux_dirent64 __user *dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100293 struct getdents_callback64 *buf =
294 container_of(ctx, struct getdents_callback64, ctx);
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700295 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
296 sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds89f58402019-10-05 11:32:52 -0700298 buf->error = verify_dirent_name(name, namlen);
299 if (unlikely(buf->error))
300 return buf->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 buf->error = -EINVAL; /* only used if we fail.. */
302 if (reclen > buf->count)
303 return -EINVAL;
304 dirent = buf->previous;
305 if (dirent) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400306 if (signal_pending(current))
307 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (__put_user(offset, &dirent->d_off))
309 goto efault;
310 }
311 dirent = buf->current_dir;
312 if (__put_user(ino, &dirent->d_ino))
313 goto efault;
314 if (__put_user(0, &dirent->d_off))
315 goto efault;
316 if (__put_user(reclen, &dirent->d_reclen))
317 goto efault;
318 if (__put_user(d_type, &dirent->d_type))
319 goto efault;
320 if (copy_to_user(dirent->d_name, name, namlen))
321 goto efault;
322 if (__put_user(0, dirent->d_name + namlen))
323 goto efault;
324 buf->previous = dirent;
325 dirent = (void __user *)dirent + reclen;
326 buf->current_dir = dirent;
327 buf->count -= reclen;
328 return 0;
329efault:
330 buf->error = -EFAULT;
331 return -EFAULT;
332}
333
Heiko Carstens20f37032009-01-14 14:14:23 +0100334SYSCALL_DEFINE3(getdents64, unsigned int, fd,
335 struct linux_dirent64 __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Al Viro2903ff02012-08-28 12:52:22 -0400337 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct linux_dirent64 __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400339 struct getdents_callback64 buf = {
340 .ctx.actor = filldir64,
341 .count = count,
342 .current_dir = dirent
343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int error;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400347 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Al Viro63b6df12016-04-20 17:08:21 -0400349 f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400350 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400351 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Al Viro5c0ba4e2013-05-15 13:52:59 -0400353 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400354 if (error >= 0)
355 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 lastdirent = buf.previous;
357 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400358 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (__put_user(d_off, &lastdirent->d_off))
Al Viro53c9c5c2008-08-24 07:29:52 -0400360 error = -EFAULT;
361 else
362 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Al Viro63b6df12016-04-20 17:08:21 -0400364 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return error;
366}