ovl: fix wrong flags check in FS_IOC_FS[SG]ETXATTR ioctls
[linux-2.6-microblaze.git] / fs / overlayfs / file.c
1 /*
2  * Copyright (C) 2017 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  */
8
9 #include <linux/cred.h>
10 #include <linux/file.h>
11 #include <linux/mount.h>
12 #include <linux/xattr.h>
13 #include <linux/uio.h>
14 #include <linux/uaccess.h>
15 #include "overlayfs.h"
16
17 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
18 {
19         if (realinode != ovl_inode_upper(inode))
20                 return 'l';
21         if (ovl_has_upperdata(inode))
22                 return 'u';
23         else
24                 return 'm';
25 }
26
27 static struct file *ovl_open_realfile(const struct file *file,
28                                       struct inode *realinode)
29 {
30         struct inode *inode = file_inode(file);
31         struct file *realfile;
32         const struct cred *old_cred;
33         int flags = file->f_flags | O_NOATIME | FMODE_NONOTIFY;
34
35         old_cred = ovl_override_creds(inode->i_sb);
36         realfile = open_with_fake_path(&file->f_path, flags, realinode,
37                                        current_cred());
38         revert_creds(old_cred);
39
40         pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
41                  file, file, ovl_whatisit(inode, realinode), file->f_flags,
42                  realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
43
44         return realfile;
45 }
46
47 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
48
49 static int ovl_change_flags(struct file *file, unsigned int flags)
50 {
51         struct inode *inode = file_inode(file);
52         int err;
53
54         /* No atime modificaton on underlying */
55         flags |= O_NOATIME | FMODE_NONOTIFY;
56
57         /* If some flag changed that cannot be changed then something's amiss */
58         if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
59                 return -EIO;
60
61         flags &= OVL_SETFL_MASK;
62
63         if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
64                 return -EPERM;
65
66         if (flags & O_DIRECT) {
67                 if (!file->f_mapping->a_ops ||
68                     !file->f_mapping->a_ops->direct_IO)
69                         return -EINVAL;
70         }
71
72         if (file->f_op->check_flags) {
73                 err = file->f_op->check_flags(flags);
74                 if (err)
75                         return err;
76         }
77
78         spin_lock(&file->f_lock);
79         file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
80         spin_unlock(&file->f_lock);
81
82         return 0;
83 }
84
85 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
86                                bool allow_meta)
87 {
88         struct inode *inode = file_inode(file);
89         struct inode *realinode;
90
91         real->flags = 0;
92         real->file = file->private_data;
93
94         if (allow_meta)
95                 realinode = ovl_inode_real(inode);
96         else
97                 realinode = ovl_inode_realdata(inode);
98
99         /* Has it been copied up since we'd opened it? */
100         if (unlikely(file_inode(real->file) != realinode)) {
101                 real->flags = FDPUT_FPUT;
102                 real->file = ovl_open_realfile(file, realinode);
103
104                 return PTR_ERR_OR_ZERO(real->file);
105         }
106
107         /* Did the flags change since open? */
108         if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
109                 return ovl_change_flags(real->file, file->f_flags);
110
111         return 0;
112 }
113
114 static int ovl_real_fdget(const struct file *file, struct fd *real)
115 {
116         return ovl_real_fdget_meta(file, real, false);
117 }
118
119 static int ovl_open(struct inode *inode, struct file *file)
120 {
121         struct file *realfile;
122         int err;
123
124         err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
125         if (err)
126                 return err;
127
128         /* No longer need these flags, so don't pass them on to underlying fs */
129         file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
130
131         realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
132         if (IS_ERR(realfile))
133                 return PTR_ERR(realfile);
134
135         file->private_data = realfile;
136
137         return 0;
138 }
139
140 static int ovl_release(struct inode *inode, struct file *file)
141 {
142         fput(file->private_data);
143
144         return 0;
145 }
146
147 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
148 {
149         struct inode *inode = file_inode(file);
150         struct fd real;
151         const struct cred *old_cred;
152         ssize_t ret;
153
154         /*
155          * The two special cases below do not need to involve real fs,
156          * so we can optimizing concurrent callers.
157          */
158         if (offset == 0) {
159                 if (whence == SEEK_CUR)
160                         return file->f_pos;
161
162                 if (whence == SEEK_SET)
163                         return vfs_setpos(file, 0, 0);
164         }
165
166         ret = ovl_real_fdget(file, &real);
167         if (ret)
168                 return ret;
169
170         /*
171          * Overlay file f_pos is the master copy that is preserved
172          * through copy up and modified on read/write, but only real
173          * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
174          * limitations that are more strict than ->s_maxbytes for specific
175          * files, so we use the real file to perform seeks.
176          */
177         inode_lock(inode);
178         real.file->f_pos = file->f_pos;
179
180         old_cred = ovl_override_creds(inode->i_sb);
181         ret = vfs_llseek(real.file, offset, whence);
182         revert_creds(old_cred);
183
184         file->f_pos = real.file->f_pos;
185         inode_unlock(inode);
186
187         fdput(real);
188
189         return ret;
190 }
191
192 static void ovl_file_accessed(struct file *file)
193 {
194         struct inode *inode, *upperinode;
195
196         if (file->f_flags & O_NOATIME)
197                 return;
198
199         inode = file_inode(file);
200         upperinode = ovl_inode_upper(inode);
201
202         if (!upperinode)
203                 return;
204
205         if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
206              !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
207                 inode->i_mtime = upperinode->i_mtime;
208                 inode->i_ctime = upperinode->i_ctime;
209         }
210
211         touch_atime(&file->f_path);
212 }
213
214 static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
215 {
216         int ifl = iocb->ki_flags;
217         rwf_t flags = 0;
218
219         if (ifl & IOCB_NOWAIT)
220                 flags |= RWF_NOWAIT;
221         if (ifl & IOCB_HIPRI)
222                 flags |= RWF_HIPRI;
223         if (ifl & IOCB_DSYNC)
224                 flags |= RWF_DSYNC;
225         if (ifl & IOCB_SYNC)
226                 flags |= RWF_SYNC;
227
228         return flags;
229 }
230
231 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
232 {
233         struct file *file = iocb->ki_filp;
234         struct fd real;
235         const struct cred *old_cred;
236         ssize_t ret;
237
238         if (!iov_iter_count(iter))
239                 return 0;
240
241         ret = ovl_real_fdget(file, &real);
242         if (ret)
243                 return ret;
244
245         old_cred = ovl_override_creds(file_inode(file)->i_sb);
246         ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
247                             ovl_iocb_to_rwf(iocb));
248         revert_creds(old_cred);
249
250         ovl_file_accessed(file);
251
252         fdput(real);
253
254         return ret;
255 }
256
257 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
258 {
259         struct file *file = iocb->ki_filp;
260         struct inode *inode = file_inode(file);
261         struct fd real;
262         const struct cred *old_cred;
263         ssize_t ret;
264
265         if (!iov_iter_count(iter))
266                 return 0;
267
268         inode_lock(inode);
269         /* Update mode */
270         ovl_copyattr(ovl_inode_real(inode), inode);
271         ret = file_remove_privs(file);
272         if (ret)
273                 goto out_unlock;
274
275         ret = ovl_real_fdget(file, &real);
276         if (ret)
277                 goto out_unlock;
278
279         old_cred = ovl_override_creds(file_inode(file)->i_sb);
280         file_start_write(real.file);
281         ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
282                              ovl_iocb_to_rwf(iocb));
283         file_end_write(real.file);
284         revert_creds(old_cred);
285
286         /* Update size */
287         ovl_copyattr(ovl_inode_real(inode), inode);
288
289         fdput(real);
290
291 out_unlock:
292         inode_unlock(inode);
293
294         return ret;
295 }
296
297 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
298 {
299         struct fd real;
300         const struct cred *old_cred;
301         int ret;
302
303         ret = ovl_real_fdget_meta(file, &real, !datasync);
304         if (ret)
305                 return ret;
306
307         /* Don't sync lower file for fear of receiving EROFS error */
308         if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
309                 old_cred = ovl_override_creds(file_inode(file)->i_sb);
310                 ret = vfs_fsync_range(real.file, start, end, datasync);
311                 revert_creds(old_cred);
312         }
313
314         fdput(real);
315
316         return ret;
317 }
318
319 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
320 {
321         struct file *realfile = file->private_data;
322         const struct cred *old_cred;
323         int ret;
324
325         if (!realfile->f_op->mmap)
326                 return -ENODEV;
327
328         if (WARN_ON(file != vma->vm_file))
329                 return -EIO;
330
331         vma->vm_file = get_file(realfile);
332
333         old_cred = ovl_override_creds(file_inode(file)->i_sb);
334         ret = call_mmap(vma->vm_file, vma);
335         revert_creds(old_cred);
336
337         if (ret) {
338                 /* Drop reference count from new vm_file value */
339                 fput(realfile);
340         } else {
341                 /* Drop reference count from previous vm_file value */
342                 fput(file);
343         }
344
345         ovl_file_accessed(file);
346
347         return ret;
348 }
349
350 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
351 {
352         struct inode *inode = file_inode(file);
353         struct fd real;
354         const struct cred *old_cred;
355         int ret;
356
357         ret = ovl_real_fdget(file, &real);
358         if (ret)
359                 return ret;
360
361         old_cred = ovl_override_creds(file_inode(file)->i_sb);
362         ret = vfs_fallocate(real.file, mode, offset, len);
363         revert_creds(old_cred);
364
365         /* Update size */
366         ovl_copyattr(ovl_inode_real(inode), inode);
367
368         fdput(real);
369
370         return ret;
371 }
372
373 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
374 {
375         struct fd real;
376         const struct cred *old_cred;
377         int ret;
378
379         ret = ovl_real_fdget(file, &real);
380         if (ret)
381                 return ret;
382
383         old_cred = ovl_override_creds(file_inode(file)->i_sb);
384         ret = vfs_fadvise(real.file, offset, len, advice);
385         revert_creds(old_cred);
386
387         fdput(real);
388
389         return ret;
390 }
391
392 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
393                            unsigned long arg)
394 {
395         struct fd real;
396         const struct cred *old_cred;
397         long ret;
398
399         ret = ovl_real_fdget(file, &real);
400         if (ret)
401                 return ret;
402
403         old_cred = ovl_override_creds(file_inode(file)->i_sb);
404         ret = vfs_ioctl(real.file, cmd, arg);
405         revert_creds(old_cred);
406
407         fdput(real);
408
409         return ret;
410 }
411
412 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
413                                 unsigned long arg, unsigned int iflags)
414 {
415         long ret;
416         struct inode *inode = file_inode(file);
417         unsigned int old_iflags;
418
419         if (!inode_owner_or_capable(inode))
420                 return -EACCES;
421
422         ret = mnt_want_write_file(file);
423         if (ret)
424                 return ret;
425
426         inode_lock(inode);
427
428         /* Check the capability before cred override */
429         ret = -EPERM;
430         old_iflags = READ_ONCE(inode->i_flags);
431         if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
432             !capable(CAP_LINUX_IMMUTABLE))
433                 goto unlock;
434
435         ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
436         if (ret)
437                 goto unlock;
438
439         ret = ovl_real_ioctl(file, cmd, arg);
440
441         ovl_copyflags(ovl_inode_real(inode), inode);
442 unlock:
443         inode_unlock(inode);
444
445         mnt_drop_write_file(file);
446
447         return ret;
448
449 }
450
451 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
452 {
453         unsigned int iflags = 0;
454
455         if (flags & FS_SYNC_FL)
456                 iflags |= S_SYNC;
457         if (flags & FS_APPEND_FL)
458                 iflags |= S_APPEND;
459         if (flags & FS_IMMUTABLE_FL)
460                 iflags |= S_IMMUTABLE;
461         if (flags & FS_NOATIME_FL)
462                 iflags |= S_NOATIME;
463
464         return iflags;
465 }
466
467 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
468                                   unsigned long arg)
469 {
470         unsigned int flags;
471
472         if (get_user(flags, (int __user *) arg))
473                 return -EFAULT;
474
475         return ovl_ioctl_set_flags(file, cmd, arg,
476                                    ovl_fsflags_to_iflags(flags));
477 }
478
479 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
480 {
481         unsigned int iflags = 0;
482
483         if (xflags & FS_XFLAG_SYNC)
484                 iflags |= S_SYNC;
485         if (xflags & FS_XFLAG_APPEND)
486                 iflags |= S_APPEND;
487         if (xflags & FS_XFLAG_IMMUTABLE)
488                 iflags |= S_IMMUTABLE;
489         if (xflags & FS_XFLAG_NOATIME)
490                 iflags |= S_NOATIME;
491
492         return iflags;
493 }
494
495 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
496                                    unsigned long arg)
497 {
498         struct fsxattr fa;
499
500         memset(&fa, 0, sizeof(fa));
501         if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
502                 return -EFAULT;
503
504         return ovl_ioctl_set_flags(file, cmd, arg,
505                                    ovl_fsxflags_to_iflags(fa.fsx_xflags));
506 }
507
508 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
509 {
510         long ret;
511
512         switch (cmd) {
513         case FS_IOC_GETFLAGS:
514         case FS_IOC_FSGETXATTR:
515                 ret = ovl_real_ioctl(file, cmd, arg);
516                 break;
517
518         case FS_IOC_SETFLAGS:
519                 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
520                 break;
521
522         case FS_IOC_FSSETXATTR:
523                 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
524                 break;
525
526         default:
527                 ret = -ENOTTY;
528         }
529
530         return ret;
531 }
532
533 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
534                              unsigned long arg)
535 {
536         switch (cmd) {
537         case FS_IOC32_GETFLAGS:
538                 cmd = FS_IOC_GETFLAGS;
539                 break;
540
541         case FS_IOC32_SETFLAGS:
542                 cmd = FS_IOC_SETFLAGS;
543                 break;
544
545         default:
546                 return -ENOIOCTLCMD;
547         }
548
549         return ovl_ioctl(file, cmd, arg);
550 }
551
552 enum ovl_copyop {
553         OVL_COPY,
554         OVL_CLONE,
555         OVL_DEDUPE,
556 };
557
558 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
559                             struct file *file_out, loff_t pos_out,
560                             loff_t len, unsigned int flags, enum ovl_copyop op)
561 {
562         struct inode *inode_out = file_inode(file_out);
563         struct fd real_in, real_out;
564         const struct cred *old_cred;
565         loff_t ret;
566
567         ret = ovl_real_fdget(file_out, &real_out);
568         if (ret)
569                 return ret;
570
571         ret = ovl_real_fdget(file_in, &real_in);
572         if (ret) {
573                 fdput(real_out);
574                 return ret;
575         }
576
577         old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
578         switch (op) {
579         case OVL_COPY:
580                 ret = vfs_copy_file_range(real_in.file, pos_in,
581                                           real_out.file, pos_out, len, flags);
582                 break;
583
584         case OVL_CLONE:
585                 ret = vfs_clone_file_range(real_in.file, pos_in,
586                                            real_out.file, pos_out, len, flags);
587                 break;
588
589         case OVL_DEDUPE:
590                 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
591                                                 real_out.file, pos_out, len,
592                                                 flags);
593                 break;
594         }
595         revert_creds(old_cred);
596
597         /* Update size */
598         ovl_copyattr(ovl_inode_real(inode_out), inode_out);
599
600         fdput(real_in);
601         fdput(real_out);
602
603         return ret;
604 }
605
606 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
607                                    struct file *file_out, loff_t pos_out,
608                                    size_t len, unsigned int flags)
609 {
610         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
611                             OVL_COPY);
612 }
613
614 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
615                                    struct file *file_out, loff_t pos_out,
616                                    loff_t len, unsigned int remap_flags)
617 {
618         enum ovl_copyop op;
619
620         if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
621                 return -EINVAL;
622
623         if (remap_flags & REMAP_FILE_DEDUP)
624                 op = OVL_DEDUPE;
625         else
626                 op = OVL_CLONE;
627
628         /*
629          * Don't copy up because of a dedupe request, this wouldn't make sense
630          * most of the time (data would be duplicated instead of deduplicated).
631          */
632         if (op == OVL_DEDUPE &&
633             (!ovl_inode_upper(file_inode(file_in)) ||
634              !ovl_inode_upper(file_inode(file_out))))
635                 return -EPERM;
636
637         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
638                             remap_flags, op);
639 }
640
641 const struct file_operations ovl_file_operations = {
642         .open           = ovl_open,
643         .release        = ovl_release,
644         .llseek         = ovl_llseek,
645         .read_iter      = ovl_read_iter,
646         .write_iter     = ovl_write_iter,
647         .fsync          = ovl_fsync,
648         .mmap           = ovl_mmap,
649         .fallocate      = ovl_fallocate,
650         .fadvise        = ovl_fadvise,
651         .unlocked_ioctl = ovl_ioctl,
652         .compat_ioctl   = ovl_compat_ioctl,
653
654         .copy_file_range        = ovl_copy_file_range,
655         .remap_file_range       = ovl_remap_file_range,
656 };