blob: d336db65a33eaa4124a741e89fbbbee6b849850f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/readdir.c
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 */
7
Kevin Winchester85c9fe82010-08-09 17:20:22 -07008#include <linux/stddef.h>
Milind Arun Choudhary022a1692007-05-08 00:29:02 -07009#include <linux/kernel.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/time.h>
12#include <linux/mm.h>
13#include <linux/errno.h>
14#include <linux/stat.h>
15#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
Heinrich Schuchardtd4c7cf62014-06-04 16:05:41 -070017#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/dirent.h>
19#include <linux/security.h>
20#include <linux/syscalls.h>
21#include <linux/unistd.h>
Al Viro0460b2a2017-04-08 18:10:08 -040022#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Al Viro5c0ba4e2013-05-15 13:52:59 -040026int iterate_dir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Al Viro496ad9a2013-01-23 17:07:38 -050028 struct inode *inode = file_inode(file);
Al Viro61922692016-04-20 23:08:32 -040029 bool shared = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int res = -ENOTDIR;
Al Viro61922692016-04-20 23:08:32 -040031 if (file->f_op->iterate_shared)
32 shared = true;
33 else if (!file->f_op->iterate)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 goto out;
35
36 res = security_file_permission(file, MAY_READ);
37 if (res)
38 goto out;
39
Al Viro00235412016-05-26 00:05:12 -040040 if (shared) {
Al Viro61922692016-04-20 23:08:32 -040041 inode_lock_shared(inode);
Al Viro00235412016-05-26 00:05:12 -040042 } else {
43 res = down_write_killable(&inode->i_rwsem);
44 if (res)
45 goto out;
46 }
Liam R. Howlettda784512007-12-06 17:39:54 -050047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 res = -ENOENT;
49 if (!IS_DEADDIR(inode)) {
Al Viro2233f312013-05-22 21:44:23 -040050 ctx->pos = file->f_pos;
Al Viro61922692016-04-20 23:08:32 -040051 if (shared)
52 res = file->f_op->iterate_shared(file, ctx);
53 else
54 res = file->f_op->iterate(file, ctx);
Al Viro2233f312013-05-22 21:44:23 -040055 file->f_pos = ctx->pos;
Heinrich Schuchardtd4c7cf62014-06-04 16:05:41 -070056 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 file_accessed(file);
58 }
Al Viro61922692016-04-20 23:08:32 -040059 if (shared)
60 inode_unlock_shared(inode);
61 else
62 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063out:
64 return res;
65}
Al Viro5c0ba4e2013-05-15 13:52:59 -040066EXPORT_SYMBOL(iterate_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * Traditional linux readdir() handling..
70 *
71 * "count=1" is a special case, meaning that the buffer is one
72 * dirent-structure in size and that the code can't handle more
73 * anyway. Thus the special "fillonedir()" function for that
74 * case (the low-level handlers don't need to care about this).
75 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#ifdef __ARCH_WANT_OLD_READDIR
78
79struct old_linux_dirent {
80 unsigned long d_ino;
81 unsigned long d_offset;
82 unsigned short d_namlen;
83 char d_name[1];
84};
85
86struct readdir_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -040087 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct old_linux_dirent __user * dirent;
89 int result;
90};
91
Miklos Szerediac7576f2014-10-30 17:37:34 +010092static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
93 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Miklos Szerediac7576f2014-10-30 17:37:34 +010095 struct readdir_callback *buf =
96 container_of(ctx, struct readdir_callback, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct old_linux_dirent __user * dirent;
David Howellsafefdbb2006-10-03 01:13:46 -070098 unsigned long d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 if (buf->result)
101 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700102 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400103 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
104 buf->result = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700105 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 buf->result++;
108 dirent = buf->dirent;
109 if (!access_ok(VERIFY_WRITE, dirent,
110 (unsigned long)(dirent->d_name + namlen + 1) -
111 (unsigned long)dirent))
112 goto efault;
David Howellsafefdbb2006-10-03 01:13:46 -0700113 if ( __put_user(d_ino, &dirent->d_ino) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 __put_user(offset, &dirent->d_offset) ||
115 __put_user(namlen, &dirent->d_namlen) ||
116 __copy_to_user(dirent->d_name, name, namlen) ||
117 __put_user(0, dirent->d_name + namlen))
118 goto efault;
119 return 0;
120efault:
121 buf->result = -EFAULT;
122 return -EFAULT;
123}
124
Heiko Carstensd4e82042009-01-14 14:14:34 +0100125SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
126 struct old_linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int error;
Al Viro63b6df12016-04-20 17:08:21 -0400129 struct fd f = fdget_pos(fd);
Al Viroac6614b2013-05-22 22:22:04 -0400130 struct readdir_callback buf = {
131 .ctx.actor = fillonedir,
132 .dirent = dirent
133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Al Viro2903ff02012-08-28 12:52:22 -0400135 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400136 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Al Viro5c0ba4e2013-05-15 13:52:59 -0400138 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400139 if (buf.result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 error = buf.result;
141
Al Viro63b6df12016-04-20 17:08:21 -0400142 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return error;
144}
145
146#endif /* __ARCH_WANT_OLD_READDIR */
147
148/*
149 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
150 * interface.
151 */
152struct linux_dirent {
153 unsigned long d_ino;
154 unsigned long d_off;
155 unsigned short d_reclen;
156 char d_name[1];
157};
158
159struct getdents_callback {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400160 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct linux_dirent __user * current_dir;
162 struct linux_dirent __user * previous;
163 int count;
164 int error;
165};
166
Miklos Szerediac7576f2014-10-30 17:37:34 +0100167static int filldir(struct dir_context *ctx, const char *name, int namlen,
168 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct linux_dirent __user * dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100171 struct getdents_callback *buf =
172 container_of(ctx, struct getdents_callback, ctx);
David Howellsafefdbb2006-10-03 01:13:46 -0700173 unsigned long d_ino;
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700174 int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
175 sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 buf->error = -EINVAL; /* only used if we fail.. */
178 if (reclen > buf->count)
179 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -0700180 d_ino = ino;
Al Viro8f3f6552008-08-12 00:28:24 -0400181 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
182 buf->error = -EOVERFLOW;
David Howellsafefdbb2006-10-03 01:13:46 -0700183 return -EOVERFLOW;
Al Viro8f3f6552008-08-12 00:28:24 -0400184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dirent = buf->previous;
186 if (dirent) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400187 if (signal_pending(current))
188 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (__put_user(offset, &dirent->d_off))
190 goto efault;
191 }
192 dirent = buf->current_dir;
David Howellsafefdbb2006-10-03 01:13:46 -0700193 if (__put_user(d_ino, &dirent->d_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto efault;
195 if (__put_user(reclen, &dirent->d_reclen))
196 goto efault;
197 if (copy_to_user(dirent->d_name, name, namlen))
198 goto efault;
199 if (__put_user(0, dirent->d_name + namlen))
200 goto efault;
201 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
202 goto efault;
203 buf->previous = dirent;
204 dirent = (void __user *)dirent + reclen;
205 buf->current_dir = dirent;
206 buf->count -= reclen;
207 return 0;
208efault:
209 buf->error = -EFAULT;
210 return -EFAULT;
211}
212
Heiko Carstens20f37032009-01-14 14:14:23 +0100213SYSCALL_DEFINE3(getdents, unsigned int, fd,
214 struct linux_dirent __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Al Viro2903ff02012-08-28 12:52:22 -0400216 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct linux_dirent __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400218 struct getdents_callback buf = {
219 .ctx.actor = filldir,
220 .count = count,
221 .current_dir = dirent
222 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int error;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400226 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Al Viro63b6df12016-04-20 17:08:21 -0400228 f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400229 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400230 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Al Viro5c0ba4e2013-05-15 13:52:59 -0400232 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400233 if (error >= 0)
234 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 lastdirent = buf.previous;
236 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400237 if (put_user(buf.ctx.pos, &lastdirent->d_off))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 error = -EFAULT;
239 else
240 error = count - buf.count;
241 }
Al Viro63b6df12016-04-20 17:08:21 -0400242 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return error;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246struct getdents_callback64 {
Al Viro5c0ba4e2013-05-15 13:52:59 -0400247 struct dir_context ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct linux_dirent64 __user * current_dir;
249 struct linux_dirent64 __user * previous;
250 int count;
251 int error;
252};
253
Miklos Szerediac7576f2014-10-30 17:37:34 +0100254static int filldir64(struct dir_context *ctx, const char *name, int namlen,
255 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct linux_dirent64 __user *dirent;
Miklos Szerediac7576f2014-10-30 17:37:34 +0100258 struct getdents_callback64 *buf =
259 container_of(ctx, struct getdents_callback64, ctx);
Kevin Winchester85c9fe82010-08-09 17:20:22 -0700260 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
261 sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 buf->error = -EINVAL; /* only used if we fail.. */
264 if (reclen > buf->count)
265 return -EINVAL;
266 dirent = buf->previous;
267 if (dirent) {
Theodore Ts'o1f60fbe2016-04-23 22:50:07 -0400268 if (signal_pending(current))
269 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (__put_user(offset, &dirent->d_off))
271 goto efault;
272 }
273 dirent = buf->current_dir;
274 if (__put_user(ino, &dirent->d_ino))
275 goto efault;
276 if (__put_user(0, &dirent->d_off))
277 goto efault;
278 if (__put_user(reclen, &dirent->d_reclen))
279 goto efault;
280 if (__put_user(d_type, &dirent->d_type))
281 goto efault;
282 if (copy_to_user(dirent->d_name, name, namlen))
283 goto efault;
284 if (__put_user(0, dirent->d_name + namlen))
285 goto efault;
286 buf->previous = dirent;
287 dirent = (void __user *)dirent + reclen;
288 buf->current_dir = dirent;
289 buf->count -= reclen;
290 return 0;
291efault:
292 buf->error = -EFAULT;
293 return -EFAULT;
294}
295
Heiko Carstens20f37032009-01-14 14:14:23 +0100296SYSCALL_DEFINE3(getdents64, unsigned int, fd,
297 struct linux_dirent64 __user *, dirent, unsigned int, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Al Viro2903ff02012-08-28 12:52:22 -0400299 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct linux_dirent64 __user * lastdirent;
Al Viroac6614b2013-05-22 22:22:04 -0400301 struct getdents_callback64 buf = {
302 .ctx.actor = filldir64,
303 .count = count,
304 .current_dir = dirent
305 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int error;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!access_ok(VERIFY_WRITE, dirent, count))
Al Viro863ced72012-04-21 18:40:32 -0400309 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Al Viro63b6df12016-04-20 17:08:21 -0400311 f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400312 if (!f.file)
Al Viro863ced72012-04-21 18:40:32 -0400313 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Al Viro5c0ba4e2013-05-15 13:52:59 -0400315 error = iterate_dir(f.file, &buf.ctx);
Al Viro53c9c5c2008-08-24 07:29:52 -0400316 if (error >= 0)
317 error = buf.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 lastdirent = buf.previous;
319 if (lastdirent) {
Al Virobb6f6192013-05-15 18:49:12 -0400320 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (__put_user(d_off, &lastdirent->d_off))
Al Viro53c9c5c2008-08-24 07:29:52 -0400322 error = -EFAULT;
323 else
324 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Al Viro63b6df12016-04-20 17:08:21 -0400326 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return error;
328}
Al Viro0460b2a2017-04-08 18:10:08 -0400329
330#ifdef CONFIG_COMPAT
331struct compat_old_linux_dirent {
332 compat_ulong_t d_ino;
333 compat_ulong_t d_offset;
334 unsigned short d_namlen;
335 char d_name[1];
336};
337
338struct compat_readdir_callback {
339 struct dir_context ctx;
340 struct compat_old_linux_dirent __user *dirent;
341 int result;
342};
343
344static int compat_fillonedir(struct dir_context *ctx, const char *name,
345 int namlen, loff_t offset, u64 ino,
346 unsigned int d_type)
347{
348 struct compat_readdir_callback *buf =
349 container_of(ctx, struct compat_readdir_callback, ctx);
350 struct compat_old_linux_dirent __user *dirent;
351 compat_ulong_t d_ino;
352
353 if (buf->result)
354 return -EINVAL;
355 d_ino = ino;
356 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
357 buf->result = -EOVERFLOW;
358 return -EOVERFLOW;
359 }
360 buf->result++;
361 dirent = buf->dirent;
362 if (!access_ok(VERIFY_WRITE, dirent,
363 (unsigned long)(dirent->d_name + namlen + 1) -
364 (unsigned long)dirent))
365 goto efault;
366 if ( __put_user(d_ino, &dirent->d_ino) ||
367 __put_user(offset, &dirent->d_offset) ||
368 __put_user(namlen, &dirent->d_namlen) ||
369 __copy_to_user(dirent->d_name, name, namlen) ||
370 __put_user(0, dirent->d_name + namlen))
371 goto efault;
372 return 0;
373efault:
374 buf->result = -EFAULT;
375 return -EFAULT;
376}
377
378COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
379 struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
380{
381 int error;
382 struct fd f = fdget_pos(fd);
383 struct compat_readdir_callback buf = {
384 .ctx.actor = compat_fillonedir,
385 .dirent = dirent
386 };
387
388 if (!f.file)
389 return -EBADF;
390
391 error = iterate_dir(f.file, &buf.ctx);
392 if (buf.result)
393 error = buf.result;
394
395 fdput_pos(f);
396 return error;
397}
398
399struct compat_linux_dirent {
400 compat_ulong_t d_ino;
401 compat_ulong_t d_off;
402 unsigned short d_reclen;
403 char d_name[1];
404};
405
406struct compat_getdents_callback {
407 struct dir_context ctx;
408 struct compat_linux_dirent __user *current_dir;
409 struct compat_linux_dirent __user *previous;
410 int count;
411 int error;
412};
413
414static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
415 loff_t offset, u64 ino, unsigned int d_type)
416{
417 struct compat_linux_dirent __user * dirent;
418 struct compat_getdents_callback *buf =
419 container_of(ctx, struct compat_getdents_callback, ctx);
420 compat_ulong_t d_ino;
421 int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
422 namlen + 2, sizeof(compat_long_t));
423
424 buf->error = -EINVAL; /* only used if we fail.. */
425 if (reclen > buf->count)
426 return -EINVAL;
427 d_ino = ino;
428 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
429 buf->error = -EOVERFLOW;
430 return -EOVERFLOW;
431 }
432 dirent = buf->previous;
433 if (dirent) {
434 if (signal_pending(current))
435 return -EINTR;
436 if (__put_user(offset, &dirent->d_off))
437 goto efault;
438 }
439 dirent = buf->current_dir;
440 if (__put_user(d_ino, &dirent->d_ino))
441 goto efault;
442 if (__put_user(reclen, &dirent->d_reclen))
443 goto efault;
444 if (copy_to_user(dirent->d_name, name, namlen))
445 goto efault;
446 if (__put_user(0, dirent->d_name + namlen))
447 goto efault;
448 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
449 goto efault;
450 buf->previous = dirent;
451 dirent = (void __user *)dirent + reclen;
452 buf->current_dir = dirent;
453 buf->count -= reclen;
454 return 0;
455efault:
456 buf->error = -EFAULT;
457 return -EFAULT;
458}
459
460COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
461 struct compat_linux_dirent __user *, dirent, unsigned int, count)
462{
463 struct fd f;
464 struct compat_linux_dirent __user * lastdirent;
465 struct compat_getdents_callback buf = {
466 .ctx.actor = compat_filldir,
467 .current_dir = dirent,
468 .count = count
469 };
470 int error;
471
472 if (!access_ok(VERIFY_WRITE, dirent, count))
473 return -EFAULT;
474
475 f = fdget_pos(fd);
476 if (!f.file)
477 return -EBADF;
478
479 error = iterate_dir(f.file, &buf.ctx);
480 if (error >= 0)
481 error = buf.error;
482 lastdirent = buf.previous;
483 if (lastdirent) {
484 if (put_user(buf.ctx.pos, &lastdirent->d_off))
485 error = -EFAULT;
486 else
487 error = count - buf.count;
488 }
489 fdput_pos(f);
490 return error;
491}
492#endif