Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 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> |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 11 | #include <linux/mount.h> |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 12 | #include <linux/xattr.h> |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 13 | #include <linux/uio.h> |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 14 | #include "overlayfs.h" |
| 15 | |
| 16 | static struct file *ovl_open_realfile(const struct file *file) |
| 17 | { |
| 18 | struct inode *inode = file_inode(file); |
| 19 | struct inode *upperinode = ovl_inode_upper(inode); |
| 20 | struct inode *realinode = upperinode ?: ovl_inode_lower(inode); |
| 21 | struct file *realfile; |
| 22 | const struct cred *old_cred; |
| 23 | |
| 24 | old_cred = ovl_override_creds(inode->i_sb); |
| 25 | realfile = open_with_fake_path(&file->f_path, file->f_flags | O_NOATIME, |
| 26 | realinode, current_cred()); |
| 27 | revert_creds(old_cred); |
| 28 | |
| 29 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", |
| 30 | file, file, upperinode ? 'u' : 'l', file->f_flags, |
| 31 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
| 32 | |
| 33 | return realfile; |
| 34 | } |
| 35 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 36 | #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) |
| 37 | |
| 38 | static int ovl_change_flags(struct file *file, unsigned int flags) |
| 39 | { |
| 40 | struct inode *inode = file_inode(file); |
| 41 | int err; |
| 42 | |
| 43 | /* No atime modificaton on underlying */ |
| 44 | flags |= O_NOATIME; |
| 45 | |
| 46 | /* If some flag changed that cannot be changed then something's amiss */ |
| 47 | if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK)) |
| 48 | return -EIO; |
| 49 | |
| 50 | flags &= OVL_SETFL_MASK; |
| 51 | |
| 52 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) |
| 53 | return -EPERM; |
| 54 | |
| 55 | if (flags & O_DIRECT) { |
| 56 | if (!file->f_mapping->a_ops || |
| 57 | !file->f_mapping->a_ops->direct_IO) |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | if (file->f_op->check_flags) { |
| 62 | err = file->f_op->check_flags(flags); |
| 63 | if (err) |
| 64 | return err; |
| 65 | } |
| 66 | |
| 67 | spin_lock(&file->f_lock); |
| 68 | file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; |
| 69 | spin_unlock(&file->f_lock); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
| 75 | { |
| 76 | struct inode *inode = file_inode(file); |
| 77 | |
| 78 | real->flags = 0; |
| 79 | real->file = file->private_data; |
| 80 | |
| 81 | /* Has it been copied up since we'd opened it? */ |
| 82 | if (unlikely(file_inode(real->file) != ovl_inode_real(inode))) { |
| 83 | real->flags = FDPUT_FPUT; |
| 84 | real->file = ovl_open_realfile(file); |
| 85 | |
| 86 | return PTR_ERR_OR_ZERO(real->file); |
| 87 | } |
| 88 | |
| 89 | /* Did the flags change since open? */ |
| 90 | if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME)) |
| 91 | return ovl_change_flags(real->file, file->f_flags); |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 96 | static int ovl_open(struct inode *inode, struct file *file) |
| 97 | { |
| 98 | struct dentry *dentry = file_dentry(file); |
| 99 | struct file *realfile; |
| 100 | int err; |
| 101 | |
| 102 | err = ovl_open_maybe_copy_up(dentry, file->f_flags); |
| 103 | if (err) |
| 104 | return err; |
| 105 | |
| 106 | /* No longer need these flags, so don't pass them on to underlying fs */ |
| 107 | file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 108 | |
| 109 | realfile = ovl_open_realfile(file); |
| 110 | if (IS_ERR(realfile)) |
| 111 | return PTR_ERR(realfile); |
| 112 | |
Miklos Szeredi | f7c7239 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 113 | /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ |
| 114 | file->f_mapping = realfile->f_mapping; |
| 115 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 116 | file->private_data = realfile; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int ovl_release(struct inode *inode, struct file *file) |
| 122 | { |
| 123 | fput(file->private_data); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) |
| 129 | { |
| 130 | struct inode *realinode = ovl_inode_real(file_inode(file)); |
| 131 | |
| 132 | return generic_file_llseek_size(file, offset, whence, |
| 133 | realinode->i_sb->s_maxbytes, |
| 134 | i_size_read(realinode)); |
| 135 | } |
| 136 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 137 | static void ovl_file_accessed(struct file *file) |
| 138 | { |
| 139 | struct inode *inode, *upperinode; |
| 140 | |
| 141 | if (file->f_flags & O_NOATIME) |
| 142 | return; |
| 143 | |
| 144 | inode = file_inode(file); |
| 145 | upperinode = ovl_inode_upper(inode); |
| 146 | |
| 147 | if (!upperinode) |
| 148 | return; |
| 149 | |
| 150 | if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) || |
| 151 | !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) { |
| 152 | inode->i_mtime = upperinode->i_mtime; |
| 153 | inode->i_ctime = upperinode->i_ctime; |
| 154 | } |
| 155 | |
| 156 | touch_atime(&file->f_path); |
| 157 | } |
| 158 | |
| 159 | static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb) |
| 160 | { |
| 161 | int ifl = iocb->ki_flags; |
| 162 | rwf_t flags = 0; |
| 163 | |
| 164 | if (ifl & IOCB_NOWAIT) |
| 165 | flags |= RWF_NOWAIT; |
| 166 | if (ifl & IOCB_HIPRI) |
| 167 | flags |= RWF_HIPRI; |
| 168 | if (ifl & IOCB_DSYNC) |
| 169 | flags |= RWF_DSYNC; |
| 170 | if (ifl & IOCB_SYNC) |
| 171 | flags |= RWF_SYNC; |
| 172 | |
| 173 | return flags; |
| 174 | } |
| 175 | |
| 176 | static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 177 | { |
| 178 | struct file *file = iocb->ki_filp; |
| 179 | struct fd real; |
| 180 | const struct cred *old_cred; |
| 181 | ssize_t ret; |
| 182 | |
| 183 | if (!iov_iter_count(iter)) |
| 184 | return 0; |
| 185 | |
| 186 | ret = ovl_real_fdget(file, &real); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | |
| 190 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 191 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, |
| 192 | ovl_iocb_to_rwf(iocb)); |
| 193 | revert_creds(old_cred); |
| 194 | |
| 195 | ovl_file_accessed(file); |
| 196 | |
| 197 | fdput(real); |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 202 | static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 203 | { |
| 204 | struct file *file = iocb->ki_filp; |
| 205 | struct inode *inode = file_inode(file); |
| 206 | struct fd real; |
| 207 | const struct cred *old_cred; |
| 208 | ssize_t ret; |
| 209 | |
| 210 | if (!iov_iter_count(iter)) |
| 211 | return 0; |
| 212 | |
| 213 | inode_lock(inode); |
| 214 | /* Update mode */ |
| 215 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 216 | ret = file_remove_privs(file); |
| 217 | if (ret) |
| 218 | goto out_unlock; |
| 219 | |
| 220 | ret = ovl_real_fdget(file, &real); |
| 221 | if (ret) |
| 222 | goto out_unlock; |
| 223 | |
| 224 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 225 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, |
| 226 | ovl_iocb_to_rwf(iocb)); |
| 227 | revert_creds(old_cred); |
| 228 | |
| 229 | /* Update size */ |
| 230 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 231 | |
| 232 | fdput(real); |
| 233 | |
| 234 | out_unlock: |
| 235 | inode_unlock(inode); |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 240 | static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 241 | { |
| 242 | struct fd real; |
| 243 | const struct cred *old_cred; |
| 244 | int ret; |
| 245 | |
| 246 | ret = ovl_real_fdget(file, &real); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
| 250 | /* Don't sync lower file for fear of receiving EROFS error */ |
| 251 | if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) { |
| 252 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 253 | ret = vfs_fsync_range(real.file, start, end, datasync); |
| 254 | revert_creds(old_cred); |
| 255 | } |
| 256 | |
| 257 | fdput(real); |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 262 | static int ovl_mmap(struct file *file, struct vm_area_struct *vma) |
| 263 | { |
| 264 | struct file *realfile = file->private_data; |
| 265 | const struct cred *old_cred; |
| 266 | int ret; |
| 267 | |
| 268 | if (!realfile->f_op->mmap) |
| 269 | return -ENODEV; |
| 270 | |
| 271 | if (WARN_ON(file != vma->vm_file)) |
| 272 | return -EIO; |
| 273 | |
| 274 | vma->vm_file = get_file(realfile); |
| 275 | |
| 276 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 277 | ret = call_mmap(vma->vm_file, vma); |
| 278 | revert_creds(old_cred); |
| 279 | |
| 280 | if (ret) { |
| 281 | /* Drop reference count from new vm_file value */ |
| 282 | fput(realfile); |
| 283 | } else { |
| 284 | /* Drop reference count from previous vm_file value */ |
| 285 | fput(file); |
| 286 | } |
| 287 | |
| 288 | ovl_file_accessed(file); |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 293 | static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 294 | { |
| 295 | struct inode *inode = file_inode(file); |
| 296 | struct fd real; |
| 297 | const struct cred *old_cred; |
| 298 | int ret; |
| 299 | |
| 300 | ret = ovl_real_fdget(file, &real); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
| 304 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 305 | ret = vfs_fallocate(real.file, mode, offset, len); |
| 306 | revert_creds(old_cred); |
| 307 | |
| 308 | /* Update size */ |
| 309 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 310 | |
| 311 | fdput(real); |
| 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 316 | static long ovl_real_ioctl(struct file *file, unsigned int cmd, |
| 317 | unsigned long arg) |
| 318 | { |
| 319 | struct fd real; |
| 320 | const struct cred *old_cred; |
| 321 | long ret; |
| 322 | |
| 323 | ret = ovl_real_fdget(file, &real); |
| 324 | if (ret) |
| 325 | return ret; |
| 326 | |
| 327 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 328 | ret = vfs_ioctl(real.file, cmd, arg); |
| 329 | revert_creds(old_cred); |
| 330 | |
| 331 | fdput(real); |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 337 | { |
| 338 | long ret; |
| 339 | struct inode *inode = file_inode(file); |
| 340 | |
| 341 | switch (cmd) { |
| 342 | case FS_IOC_GETFLAGS: |
| 343 | ret = ovl_real_ioctl(file, cmd, arg); |
| 344 | break; |
| 345 | |
| 346 | case FS_IOC_SETFLAGS: |
| 347 | if (!inode_owner_or_capable(inode)) |
| 348 | return -EACCES; |
| 349 | |
| 350 | ret = mnt_want_write_file(file); |
| 351 | if (ret) |
| 352 | return ret; |
| 353 | |
| 354 | ret = ovl_copy_up(file_dentry(file)); |
| 355 | if (!ret) { |
| 356 | ret = ovl_real_ioctl(file, cmd, arg); |
| 357 | |
| 358 | inode_lock(inode); |
| 359 | ovl_copyflags(ovl_inode_real(inode), inode); |
| 360 | inode_unlock(inode); |
| 361 | } |
| 362 | |
| 363 | mnt_drop_write_file(file); |
| 364 | break; |
| 365 | |
| 366 | default: |
| 367 | ret = -ENOTTY; |
| 368 | } |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static long ovl_compat_ioctl(struct file *file, unsigned int cmd, |
| 374 | unsigned long arg) |
| 375 | { |
| 376 | switch (cmd) { |
| 377 | case FS_IOC32_GETFLAGS: |
| 378 | cmd = FS_IOC_GETFLAGS; |
| 379 | break; |
| 380 | |
| 381 | case FS_IOC32_SETFLAGS: |
| 382 | cmd = FS_IOC_SETFLAGS; |
| 383 | break; |
| 384 | |
| 385 | default: |
| 386 | return -ENOIOCTLCMD; |
| 387 | } |
| 388 | |
| 389 | return ovl_ioctl(file, cmd, arg); |
| 390 | } |
| 391 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame^] | 392 | enum ovl_copyop { |
| 393 | OVL_COPY, |
| 394 | OVL_CLONE, |
| 395 | OVL_DEDUPE, |
| 396 | }; |
| 397 | |
| 398 | static ssize_t ovl_copyfile(struct file *file_in, loff_t pos_in, |
| 399 | struct file *file_out, loff_t pos_out, |
| 400 | u64 len, unsigned int flags, enum ovl_copyop op) |
| 401 | { |
| 402 | struct inode *inode_out = file_inode(file_out); |
| 403 | struct fd real_in, real_out; |
| 404 | const struct cred *old_cred; |
| 405 | ssize_t ret; |
| 406 | |
| 407 | ret = ovl_real_fdget(file_out, &real_out); |
| 408 | if (ret) |
| 409 | return ret; |
| 410 | |
| 411 | ret = ovl_real_fdget(file_in, &real_in); |
| 412 | if (ret) { |
| 413 | fdput(real_out); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | old_cred = ovl_override_creds(file_inode(file_out)->i_sb); |
| 418 | switch (op) { |
| 419 | case OVL_COPY: |
| 420 | ret = vfs_copy_file_range(real_in.file, pos_in, |
| 421 | real_out.file, pos_out, len, flags); |
| 422 | break; |
| 423 | |
| 424 | case OVL_CLONE: |
| 425 | ret = vfs_clone_file_range(real_in.file, pos_in, |
| 426 | real_out.file, pos_out, len); |
| 427 | break; |
| 428 | |
| 429 | case OVL_DEDUPE: |
| 430 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, |
| 431 | real_out.file, pos_out, len); |
| 432 | break; |
| 433 | } |
| 434 | revert_creds(old_cred); |
| 435 | |
| 436 | /* Update size */ |
| 437 | ovl_copyattr(ovl_inode_real(inode_out), inode_out); |
| 438 | |
| 439 | fdput(real_in); |
| 440 | fdput(real_out); |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in, |
| 446 | struct file *file_out, loff_t pos_out, |
| 447 | size_t len, unsigned int flags) |
| 448 | { |
| 449 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags, |
| 450 | OVL_COPY); |
| 451 | } |
| 452 | |
| 453 | static int ovl_clone_file_range(struct file *file_in, loff_t pos_in, |
| 454 | struct file *file_out, loff_t pos_out, u64 len) |
| 455 | { |
| 456 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0, |
| 457 | OVL_CLONE); |
| 458 | } |
| 459 | |
| 460 | static int ovl_dedupe_file_range(struct file *file_in, loff_t pos_in, |
| 461 | struct file *file_out, loff_t pos_out, u64 len) |
| 462 | { |
| 463 | /* |
| 464 | * Don't copy up because of a dedupe request, this wouldn't make sense |
| 465 | * most of the time (data would be duplicated instead of deduplicated). |
| 466 | */ |
| 467 | if (!ovl_inode_upper(file_inode(file_in)) || |
| 468 | !ovl_inode_upper(file_inode(file_out))) |
| 469 | return -EPERM; |
| 470 | |
| 471 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0, |
| 472 | OVL_DEDUPE); |
| 473 | } |
| 474 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 475 | const struct file_operations ovl_file_operations = { |
| 476 | .open = ovl_open, |
| 477 | .release = ovl_release, |
| 478 | .llseek = ovl_llseek, |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 479 | .read_iter = ovl_read_iter, |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 480 | .write_iter = ovl_write_iter, |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 481 | .fsync = ovl_fsync, |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 482 | .mmap = ovl_mmap, |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 483 | .fallocate = ovl_fallocate, |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 484 | .unlocked_ioctl = ovl_ioctl, |
| 485 | .compat_ioctl = ovl_compat_ioctl, |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame^] | 486 | |
| 487 | .copy_file_range = ovl_copy_file_range, |
| 488 | .clone_file_range = ovl_clone_file_range, |
| 489 | .dedupe_file_range = ovl_dedupe_file_range, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 490 | }; |