GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / readdir.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/readdir.c
4  *
5  *  Copyright (C) 1995  Linus Torvalds
6  */
7
8 #include <linux/stddef.h>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/time.h>
12 #include <linux/mm.h>
13 #include <linux/errno.h>
14 #include <linux/stat.h>
15 #include <linux/file.h>
16 #include <linux/fs.h>
17 #include <linux/fsnotify.h>
18 #include <linux/dirent.h>
19 #include <linux/security.h>
20 #include <linux/syscalls.h>
21 #include <linux/unistd.h>
22 #include <linux/compat.h>
23
24 #include <linux/uaccess.h>
25
26 int iterate_dir(struct file *file, struct dir_context *ctx)
27 {
28         struct inode *inode = file_inode(file);
29         bool shared = false;
30         int res = -ENOTDIR;
31         if (file->f_op->iterate_shared)
32                 shared = true;
33         else if (!file->f_op->iterate)
34                 goto out;
35
36         res = security_file_permission(file, MAY_READ);
37         if (res)
38                 goto out;
39
40         if (shared) {
41                 inode_lock_shared(inode);
42         } else {
43                 res = down_write_killable(&inode->i_rwsem);
44                 if (res)
45                         goto out;
46         }
47
48         res = -ENOENT;
49         if (!IS_DEADDIR(inode)) {
50                 ctx->pos = file->f_pos;
51                 if (shared)
52                         res = file->f_op->iterate_shared(file, ctx);
53                 else
54                         res = file->f_op->iterate(file, ctx);
55                 file->f_pos = ctx->pos;
56                 fsnotify_access(file);
57                 file_accessed(file);
58         }
59         if (shared)
60                 inode_unlock_shared(inode);
61         else
62                 inode_unlock(inode);
63 out:
64         return res;
65 }
66 EXPORT_SYMBOL(iterate_dir);
67
68 /*
69  * POSIX says that a dirent name cannot contain NULL or a '/'.
70  *
71  * It's not 100% clear what we should really do in this case.
72  * The filesystem is clearly corrupted, but returning a hard
73  * error means that you now don't see any of the other names
74  * either, so that isn't a perfect alternative.
75  *
76  * And if you return an error, what error do you use? Several
77  * filesystems seem to have decided on EUCLEAN being the error
78  * code for EFSCORRUPTED, and that may be the error to use. Or
79  * just EIO, which is perhaps more obvious to users.
80  *
81  * In order to see the other file names in the directory, the
82  * caller might want to make this a "soft" error: skip the
83  * entry, and return the error at the end instead.
84  *
85  * Note that this should likely do a "memchr(name, 0, len)"
86  * check too, since that would be filesystem corruption as
87  * well. However, that case can't actually confuse user space,
88  * which has to do a strlen() on the name anyway to find the
89  * filename length, and the above "soft error" worry means
90  * that it's probably better left alone until we have that
91  * issue clarified.
92  */
93 static int verify_dirent_name(const char *name, int len)
94 {
95         if (!len)
96                 return -EIO;
97         if (memchr(name, '/', len))
98                 return -EIO;
99         return 0;
100 }
101
102 /*
103  * Traditional linux readdir() handling..
104  *
105  * "count=1" is a special case, meaning that the buffer is one
106  * dirent-structure in size and that the code can't handle more
107  * anyway. Thus the special "fillonedir()" function for that
108  * case (the low-level handlers don't need to care about this).
109  */
110
111 #ifdef __ARCH_WANT_OLD_READDIR
112
113 struct old_linux_dirent {
114         unsigned long   d_ino;
115         unsigned long   d_offset;
116         unsigned short  d_namlen;
117         char            d_name[1];
118 };
119
120 struct readdir_callback {
121         struct dir_context ctx;
122         struct old_linux_dirent __user * dirent;
123         int result;
124 };
125
126 static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
127                       loff_t offset, u64 ino, unsigned int d_type)
128 {
129         struct readdir_callback *buf =
130                 container_of(ctx, struct readdir_callback, ctx);
131         struct old_linux_dirent __user * dirent;
132         unsigned long d_ino;
133
134         if (buf->result)
135                 return -EINVAL;
136         buf->result = verify_dirent_name(name, namlen);
137         if (buf->result < 0)
138                 return buf->result;
139         d_ino = ino;
140         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
141                 buf->result = -EOVERFLOW;
142                 return -EOVERFLOW;
143         }
144         buf->result++;
145         dirent = buf->dirent;
146         if (!access_ok(VERIFY_WRITE, dirent,
147                         (unsigned long)(dirent->d_name + namlen + 1) -
148                                 (unsigned long)dirent))
149                 goto efault;
150         if (    __put_user(d_ino, &dirent->d_ino) ||
151                 __put_user(offset, &dirent->d_offset) ||
152                 __put_user(namlen, &dirent->d_namlen) ||
153                 __copy_to_user(dirent->d_name, name, namlen) ||
154                 __put_user(0, dirent->d_name + namlen))
155                 goto efault;
156         return 0;
157 efault:
158         buf->result = -EFAULT;
159         return -EFAULT;
160 }
161
162 SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
163                 struct old_linux_dirent __user *, dirent, unsigned int, count)
164 {
165         int error;
166         struct fd f = fdget_pos(fd);
167         struct readdir_callback buf = {
168                 .ctx.actor = fillonedir,
169                 .dirent = dirent
170         };
171
172         if (!f.file)
173                 return -EBADF;
174
175         error = iterate_dir(f.file, &buf.ctx);
176         if (buf.result)
177                 error = buf.result;
178
179         fdput_pos(f);
180         return error;
181 }
182
183 #endif /* __ARCH_WANT_OLD_READDIR */
184
185 /*
186  * New, all-improved, singing, dancing, iBCS2-compliant getdents()
187  * interface. 
188  */
189 struct linux_dirent {
190         unsigned long   d_ino;
191         unsigned long   d_off;
192         unsigned short  d_reclen;
193         char            d_name[1];
194 };
195
196 struct getdents_callback {
197         struct dir_context ctx;
198         struct linux_dirent __user * current_dir;
199         struct linux_dirent __user * previous;
200         int count;
201         int error;
202 };
203
204 static int filldir(struct dir_context *ctx, const char *name, int namlen,
205                    loff_t offset, u64 ino, unsigned int d_type)
206 {
207         struct linux_dirent __user * dirent;
208         struct getdents_callback *buf =
209                 container_of(ctx, struct getdents_callback, ctx);
210         unsigned long d_ino;
211         int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
212                 sizeof(long));
213
214         buf->error = verify_dirent_name(name, namlen);
215         if (unlikely(buf->error))
216                 return buf->error;
217         buf->error = -EINVAL;   /* only used if we fail.. */
218         if (reclen > buf->count)
219                 return -EINVAL;
220         d_ino = ino;
221         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
222                 buf->error = -EOVERFLOW;
223                 return -EOVERFLOW;
224         }
225         dirent = buf->previous;
226         if (dirent) {
227                 if (signal_pending(current))
228                         return -EINTR;
229                 if (__put_user(offset, &dirent->d_off))
230                         goto efault;
231         }
232         dirent = buf->current_dir;
233         if (__put_user(d_ino, &dirent->d_ino))
234                 goto efault;
235         if (__put_user(reclen, &dirent->d_reclen))
236                 goto efault;
237         if (copy_to_user(dirent->d_name, name, namlen))
238                 goto efault;
239         if (__put_user(0, dirent->d_name + namlen))
240                 goto efault;
241         if (__put_user(d_type, (char __user *) dirent + reclen - 1))
242                 goto efault;
243         buf->previous = dirent;
244         dirent = (void __user *)dirent + reclen;
245         buf->current_dir = dirent;
246         buf->count -= reclen;
247         return 0;
248 efault:
249         buf->error = -EFAULT;
250         return -EFAULT;
251 }
252
253 SYSCALL_DEFINE3(getdents, unsigned int, fd,
254                 struct linux_dirent __user *, dirent, unsigned int, count)
255 {
256         struct fd f;
257         struct linux_dirent __user * lastdirent;
258         struct getdents_callback buf = {
259                 .ctx.actor = filldir,
260                 .count = count,
261                 .current_dir = dirent
262         };
263         int error;
264
265         if (!access_ok(VERIFY_WRITE, dirent, count))
266                 return -EFAULT;
267
268         f = fdget_pos(fd);
269         if (!f.file)
270                 return -EBADF;
271
272         error = iterate_dir(f.file, &buf.ctx);
273         if (error >= 0)
274                 error = buf.error;
275         lastdirent = buf.previous;
276         if (lastdirent) {
277                 if (put_user(buf.ctx.pos, &lastdirent->d_off))
278                         error = -EFAULT;
279                 else
280                         error = count - buf.count;
281         }
282         fdput_pos(f);
283         return error;
284 }
285
286 struct getdents_callback64 {
287         struct dir_context ctx;
288         struct linux_dirent64 __user * current_dir;
289         struct linux_dirent64 __user * previous;
290         int count;
291         int error;
292 };
293
294 static int filldir64(struct dir_context *ctx, const char *name, int namlen,
295                      loff_t offset, u64 ino, unsigned int d_type)
296 {
297         struct linux_dirent64 __user *dirent;
298         struct getdents_callback64 *buf =
299                 container_of(ctx, struct getdents_callback64, ctx);
300         int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
301                 sizeof(u64));
302
303         buf->error = verify_dirent_name(name, namlen);
304         if (unlikely(buf->error))
305                 return buf->error;
306         buf->error = -EINVAL;   /* only used if we fail.. */
307         if (reclen > buf->count)
308                 return -EINVAL;
309         dirent = buf->previous;
310         if (dirent) {
311                 if (signal_pending(current))
312                         return -EINTR;
313                 if (__put_user(offset, &dirent->d_off))
314                         goto efault;
315         }
316         dirent = buf->current_dir;
317         if (__put_user(ino, &dirent->d_ino))
318                 goto efault;
319         if (__put_user(0, &dirent->d_off))
320                 goto efault;
321         if (__put_user(reclen, &dirent->d_reclen))
322                 goto efault;
323         if (__put_user(d_type, &dirent->d_type))
324                 goto efault;
325         if (copy_to_user(dirent->d_name, name, namlen))
326                 goto efault;
327         if (__put_user(0, dirent->d_name + namlen))
328                 goto efault;
329         buf->previous = dirent;
330         dirent = (void __user *)dirent + reclen;
331         buf->current_dir = dirent;
332         buf->count -= reclen;
333         return 0;
334 efault:
335         buf->error = -EFAULT;
336         return -EFAULT;
337 }
338
339 SYSCALL_DEFINE3(getdents64, unsigned int, fd,
340                 struct linux_dirent64 __user *, dirent, unsigned int, count)
341 {
342         struct fd f;
343         struct linux_dirent64 __user * lastdirent;
344         struct getdents_callback64 buf = {
345                 .ctx.actor = filldir64,
346                 .count = count,
347                 .current_dir = dirent
348         };
349         int error;
350
351         if (!access_ok(VERIFY_WRITE, dirent, count))
352                 return -EFAULT;
353
354         f = fdget_pos(fd);
355         if (!f.file)
356                 return -EBADF;
357
358         error = iterate_dir(f.file, &buf.ctx);
359         if (error >= 0)
360                 error = buf.error;
361         lastdirent = buf.previous;
362         if (lastdirent) {
363                 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
364                 if (__put_user(d_off, &lastdirent->d_off))
365                         error = -EFAULT;
366                 else
367                         error = count - buf.count;
368         }
369         fdput_pos(f);
370         return error;
371 }
372
373 #ifdef CONFIG_COMPAT
374 struct compat_old_linux_dirent {
375         compat_ulong_t  d_ino;
376         compat_ulong_t  d_offset;
377         unsigned short  d_namlen;
378         char            d_name[1];
379 };
380
381 struct compat_readdir_callback {
382         struct dir_context ctx;
383         struct compat_old_linux_dirent __user *dirent;
384         int result;
385 };
386
387 static int compat_fillonedir(struct dir_context *ctx, const char *name,
388                              int namlen, loff_t offset, u64 ino,
389                              unsigned int d_type)
390 {
391         struct compat_readdir_callback *buf =
392                 container_of(ctx, struct compat_readdir_callback, ctx);
393         struct compat_old_linux_dirent __user *dirent;
394         compat_ulong_t d_ino;
395
396         if (buf->result)
397                 return -EINVAL;
398         buf->result = verify_dirent_name(name, namlen);
399         if (buf->result < 0)
400                 return buf->result;
401         d_ino = ino;
402         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
403                 buf->result = -EOVERFLOW;
404                 return -EOVERFLOW;
405         }
406         buf->result++;
407         dirent = buf->dirent;
408         if (!access_ok(VERIFY_WRITE, dirent,
409                         (unsigned long)(dirent->d_name + namlen + 1) -
410                                 (unsigned long)dirent))
411                 goto efault;
412         if (    __put_user(d_ino, &dirent->d_ino) ||
413                 __put_user(offset, &dirent->d_offset) ||
414                 __put_user(namlen, &dirent->d_namlen) ||
415                 __copy_to_user(dirent->d_name, name, namlen) ||
416                 __put_user(0, dirent->d_name + namlen))
417                 goto efault;
418         return 0;
419 efault:
420         buf->result = -EFAULT;
421         return -EFAULT;
422 }
423
424 COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
425                 struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
426 {
427         int error;
428         struct fd f = fdget_pos(fd);
429         struct compat_readdir_callback buf = {
430                 .ctx.actor = compat_fillonedir,
431                 .dirent = dirent
432         };
433
434         if (!f.file)
435                 return -EBADF;
436
437         error = iterate_dir(f.file, &buf.ctx);
438         if (buf.result)
439                 error = buf.result;
440
441         fdput_pos(f);
442         return error;
443 }
444
445 struct compat_linux_dirent {
446         compat_ulong_t  d_ino;
447         compat_ulong_t  d_off;
448         unsigned short  d_reclen;
449         char            d_name[1];
450 };
451
452 struct compat_getdents_callback {
453         struct dir_context ctx;
454         struct compat_linux_dirent __user *current_dir;
455         struct compat_linux_dirent __user *previous;
456         int count;
457         int error;
458 };
459
460 static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
461                 loff_t offset, u64 ino, unsigned int d_type)
462 {
463         struct compat_linux_dirent __user * dirent;
464         struct compat_getdents_callback *buf =
465                 container_of(ctx, struct compat_getdents_callback, ctx);
466         compat_ulong_t d_ino;
467         int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
468                 namlen + 2, sizeof(compat_long_t));
469
470         buf->error = -EINVAL;   /* only used if we fail.. */
471         if (reclen > buf->count)
472                 return -EINVAL;
473         d_ino = ino;
474         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
475                 buf->error = -EOVERFLOW;
476                 return -EOVERFLOW;
477         }
478         dirent = buf->previous;
479         if (dirent) {
480                 if (signal_pending(current))
481                         return -EINTR;
482                 if (__put_user(offset, &dirent->d_off))
483                         goto efault;
484         }
485         dirent = buf->current_dir;
486         if (__put_user(d_ino, &dirent->d_ino))
487                 goto efault;
488         if (__put_user(reclen, &dirent->d_reclen))
489                 goto efault;
490         if (copy_to_user(dirent->d_name, name, namlen))
491                 goto efault;
492         if (__put_user(0, dirent->d_name + namlen))
493                 goto efault;
494         if (__put_user(d_type, (char  __user *) dirent + reclen - 1))
495                 goto efault;
496         buf->previous = dirent;
497         dirent = (void __user *)dirent + reclen;
498         buf->current_dir = dirent;
499         buf->count -= reclen;
500         return 0;
501 efault:
502         buf->error = -EFAULT;
503         return -EFAULT;
504 }
505
506 COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
507                 struct compat_linux_dirent __user *, dirent, unsigned int, count)
508 {
509         struct fd f;
510         struct compat_linux_dirent __user * lastdirent;
511         struct compat_getdents_callback buf = {
512                 .ctx.actor = compat_filldir,
513                 .current_dir = dirent,
514                 .count = count
515         };
516         int error;
517
518         if (!access_ok(VERIFY_WRITE, dirent, count))
519                 return -EFAULT;
520
521         f = fdget_pos(fd);
522         if (!f.file)
523                 return -EBADF;
524
525         error = iterate_dir(f.file, &buf.ctx);
526         if (error >= 0)
527                 error = buf.error;
528         lastdirent = buf.previous;
529         if (lastdirent) {
530                 if (put_user(buf.ctx.pos, &lastdirent->d_off))
531                         error = -EFAULT;
532                 else
533                         error = count - buf.count;
534         }
535         fdput_pos(f);
536         return error;
537 }
538 #endif