David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Red Hat, Inc. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/cred.h> |
| 7 | #include <linux/file.h> |
| 8 | #include <linux/mount.h> |
| 9 | #include <linux/xattr.h> |
| 10 | #include <linux/uio.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 12 | #include <linux/splice.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 13 | #include <linux/security.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 14 | #include <linux/mm.h> |
| 15 | #include <linux/fs.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 16 | #include "overlayfs.h" |
| 17 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 18 | struct ovl_aio_req { |
| 19 | struct kiocb iocb; |
| 20 | refcount_t ref; |
| 21 | struct kiocb *orig_iocb; |
| 22 | struct fd fd; |
| 23 | }; |
| 24 | |
| 25 | static struct kmem_cache *ovl_aio_request_cachep; |
| 26 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | static char ovl_whatisit(struct inode *inode, struct inode *realinode) |
| 28 | { |
| 29 | if (realinode != ovl_inode_upper(inode)) |
| 30 | return 'l'; |
| 31 | if (ovl_has_upperdata(inode)) |
| 32 | return 'u'; |
| 33 | else |
| 34 | return 'm'; |
| 35 | } |
| 36 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 37 | /* No atime modificaton nor notify on underlying */ |
| 38 | #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) |
| 39 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 40 | static struct file *ovl_open_realfile(const struct file *file, |
| 41 | struct inode *realinode) |
| 42 | { |
| 43 | struct inode *inode = file_inode(file); |
| 44 | struct file *realfile; |
| 45 | const struct cred *old_cred; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 46 | int flags = file->f_flags | OVL_OPEN_FLAGS; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 47 | int acc_mode = ACC_MODE(flags); |
| 48 | int err; |
| 49 | |
| 50 | if (flags & O_APPEND) |
| 51 | acc_mode |= MAY_APPEND; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | |
| 53 | old_cred = ovl_override_creds(inode->i_sb); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 54 | err = inode_permission(realinode, MAY_OPEN | acc_mode); |
| 55 | if (err) { |
| 56 | realfile = ERR_PTR(err); |
| 57 | } else if (!inode_owner_or_capable(realinode)) { |
| 58 | realfile = ERR_PTR(-EPERM); |
| 59 | } else { |
| 60 | realfile = open_with_fake_path(&file->f_path, flags, realinode, |
| 61 | current_cred()); |
| 62 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 63 | revert_creds(old_cred); |
| 64 | |
| 65 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", |
| 66 | file, file, ovl_whatisit(inode, realinode), file->f_flags, |
| 67 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
| 68 | |
| 69 | return realfile; |
| 70 | } |
| 71 | |
| 72 | #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) |
| 73 | |
| 74 | static int ovl_change_flags(struct file *file, unsigned int flags) |
| 75 | { |
| 76 | struct inode *inode = file_inode(file); |
| 77 | int err; |
| 78 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 79 | flags |= OVL_OPEN_FLAGS; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 80 | |
| 81 | /* If some flag changed that cannot be changed then something's amiss */ |
| 82 | if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK)) |
| 83 | return -EIO; |
| 84 | |
| 85 | flags &= OVL_SETFL_MASK; |
| 86 | |
| 87 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) |
| 88 | return -EPERM; |
| 89 | |
| 90 | if (flags & O_DIRECT) { |
| 91 | if (!file->f_mapping->a_ops || |
| 92 | !file->f_mapping->a_ops->direct_IO) |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | if (file->f_op->check_flags) { |
| 97 | err = file->f_op->check_flags(flags); |
| 98 | if (err) |
| 99 | return err; |
| 100 | } |
| 101 | |
| 102 | spin_lock(&file->f_lock); |
| 103 | file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; |
| 104 | spin_unlock(&file->f_lock); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int ovl_real_fdget_meta(const struct file *file, struct fd *real, |
| 110 | bool allow_meta) |
| 111 | { |
| 112 | struct inode *inode = file_inode(file); |
| 113 | struct inode *realinode; |
| 114 | |
| 115 | real->flags = 0; |
| 116 | real->file = file->private_data; |
| 117 | |
| 118 | if (allow_meta) |
| 119 | realinode = ovl_inode_real(inode); |
| 120 | else |
| 121 | realinode = ovl_inode_realdata(inode); |
| 122 | |
| 123 | /* Has it been copied up since we'd opened it? */ |
| 124 | if (unlikely(file_inode(real->file) != realinode)) { |
| 125 | real->flags = FDPUT_FPUT; |
| 126 | real->file = ovl_open_realfile(file, realinode); |
| 127 | |
| 128 | return PTR_ERR_OR_ZERO(real->file); |
| 129 | } |
| 130 | |
| 131 | /* Did the flags change since open? */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 132 | if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 133 | return ovl_change_flags(real->file, file->f_flags); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
| 139 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 140 | if (d_is_dir(file_dentry(file))) { |
| 141 | real->flags = 0; |
| 142 | real->file = ovl_dir_real_file(file, false); |
| 143 | |
| 144 | return PTR_ERR_OR_ZERO(real->file); |
| 145 | } |
| 146 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 147 | return ovl_real_fdget_meta(file, real, false); |
| 148 | } |
| 149 | |
| 150 | static int ovl_open(struct inode *inode, struct file *file) |
| 151 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 152 | struct file *realfile; |
| 153 | int err; |
| 154 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 155 | err = ovl_maybe_copy_up(file_dentry(file), file->f_flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 156 | if (err) |
| 157 | return err; |
| 158 | |
| 159 | /* No longer need these flags, so don't pass them on to underlying fs */ |
| 160 | file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 161 | |
| 162 | realfile = ovl_open_realfile(file, ovl_inode_realdata(inode)); |
| 163 | if (IS_ERR(realfile)) |
| 164 | return PTR_ERR(realfile); |
| 165 | |
| 166 | file->private_data = realfile; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int ovl_release(struct inode *inode, struct file *file) |
| 172 | { |
| 173 | fput(file->private_data); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) |
| 179 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 180 | struct inode *inode = file_inode(file); |
| 181 | struct fd real; |
| 182 | const struct cred *old_cred; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 183 | loff_t ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 184 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 185 | /* |
| 186 | * The two special cases below do not need to involve real fs, |
| 187 | * so we can optimizing concurrent callers. |
| 188 | */ |
| 189 | if (offset == 0) { |
| 190 | if (whence == SEEK_CUR) |
| 191 | return file->f_pos; |
| 192 | |
| 193 | if (whence == SEEK_SET) |
| 194 | return vfs_setpos(file, 0, 0); |
| 195 | } |
| 196 | |
| 197 | ret = ovl_real_fdget(file, &real); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | /* |
| 202 | * Overlay file f_pos is the master copy that is preserved |
| 203 | * through copy up and modified on read/write, but only real |
| 204 | * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose |
| 205 | * limitations that are more strict than ->s_maxbytes for specific |
| 206 | * files, so we use the real file to perform seeks. |
| 207 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 208 | ovl_inode_lock(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 209 | real.file->f_pos = file->f_pos; |
| 210 | |
| 211 | old_cred = ovl_override_creds(inode->i_sb); |
| 212 | ret = vfs_llseek(real.file, offset, whence); |
| 213 | revert_creds(old_cred); |
| 214 | |
| 215 | file->f_pos = real.file->f_pos; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 216 | ovl_inode_unlock(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 217 | |
| 218 | fdput(real); |
| 219 | |
| 220 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void ovl_file_accessed(struct file *file) |
| 224 | { |
| 225 | struct inode *inode, *upperinode; |
| 226 | |
| 227 | if (file->f_flags & O_NOATIME) |
| 228 | return; |
| 229 | |
| 230 | inode = file_inode(file); |
| 231 | upperinode = ovl_inode_upper(inode); |
| 232 | |
| 233 | if (!upperinode) |
| 234 | return; |
| 235 | |
| 236 | if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) || |
| 237 | !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) { |
| 238 | inode->i_mtime = upperinode->i_mtime; |
| 239 | inode->i_ctime = upperinode->i_ctime; |
| 240 | } |
| 241 | |
| 242 | touch_atime(&file->f_path); |
| 243 | } |
| 244 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 245 | static rwf_t ovl_iocb_to_rwf(int ifl) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 246 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 247 | rwf_t flags = 0; |
| 248 | |
| 249 | if (ifl & IOCB_NOWAIT) |
| 250 | flags |= RWF_NOWAIT; |
| 251 | if (ifl & IOCB_HIPRI) |
| 252 | flags |= RWF_HIPRI; |
| 253 | if (ifl & IOCB_DSYNC) |
| 254 | flags |= RWF_DSYNC; |
| 255 | if (ifl & IOCB_SYNC) |
| 256 | flags |= RWF_SYNC; |
| 257 | |
| 258 | return flags; |
| 259 | } |
| 260 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 261 | static inline void ovl_aio_put(struct ovl_aio_req *aio_req) |
| 262 | { |
| 263 | if (refcount_dec_and_test(&aio_req->ref)) { |
| 264 | fdput(aio_req->fd); |
| 265 | kmem_cache_free(ovl_aio_request_cachep, aio_req); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req) |
| 270 | { |
| 271 | struct kiocb *iocb = &aio_req->iocb; |
| 272 | struct kiocb *orig_iocb = aio_req->orig_iocb; |
| 273 | |
| 274 | if (iocb->ki_flags & IOCB_WRITE) { |
| 275 | struct inode *inode = file_inode(orig_iocb->ki_filp); |
| 276 | |
| 277 | /* Actually acquired in ovl_write_iter() */ |
| 278 | __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb, |
| 279 | SB_FREEZE_WRITE); |
| 280 | file_end_write(iocb->ki_filp); |
| 281 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 282 | } |
| 283 | |
| 284 | orig_iocb->ki_pos = iocb->ki_pos; |
| 285 | ovl_aio_put(aio_req); |
| 286 | } |
| 287 | |
| 288 | static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2) |
| 289 | { |
| 290 | struct ovl_aio_req *aio_req = container_of(iocb, |
| 291 | struct ovl_aio_req, iocb); |
| 292 | struct kiocb *orig_iocb = aio_req->orig_iocb; |
| 293 | |
| 294 | ovl_aio_cleanup_handler(aio_req); |
| 295 | orig_iocb->ki_complete(orig_iocb, res, res2); |
| 296 | } |
| 297 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 298 | static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 299 | { |
| 300 | struct file *file = iocb->ki_filp; |
| 301 | struct fd real; |
| 302 | const struct cred *old_cred; |
| 303 | ssize_t ret; |
| 304 | |
| 305 | if (!iov_iter_count(iter)) |
| 306 | return 0; |
| 307 | |
| 308 | ret = ovl_real_fdget(file, &real); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 312 | ret = -EINVAL; |
| 313 | if (iocb->ki_flags & IOCB_DIRECT && |
| 314 | (!real.file->f_mapping->a_ops || |
| 315 | !real.file->f_mapping->a_ops->direct_IO)) |
| 316 | goto out_fdput; |
| 317 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 318 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 319 | if (is_sync_kiocb(iocb)) { |
| 320 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, |
| 321 | ovl_iocb_to_rwf(iocb->ki_flags)); |
| 322 | } else { |
| 323 | struct ovl_aio_req *aio_req; |
| 324 | |
| 325 | ret = -ENOMEM; |
| 326 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); |
| 327 | if (!aio_req) |
| 328 | goto out; |
| 329 | |
| 330 | aio_req->fd = real; |
| 331 | real.flags = 0; |
| 332 | aio_req->orig_iocb = iocb; |
| 333 | kiocb_clone(&aio_req->iocb, iocb, real.file); |
| 334 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
| 335 | refcount_set(&aio_req->ref, 2); |
| 336 | ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter); |
| 337 | ovl_aio_put(aio_req); |
| 338 | if (ret != -EIOCBQUEUED) |
| 339 | ovl_aio_cleanup_handler(aio_req); |
| 340 | } |
| 341 | out: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 342 | revert_creds(old_cred); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 343 | ovl_file_accessed(file); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 344 | out_fdput: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 345 | fdput(real); |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 351 | { |
| 352 | struct file *file = iocb->ki_filp; |
| 353 | struct inode *inode = file_inode(file); |
| 354 | struct fd real; |
| 355 | const struct cred *old_cred; |
| 356 | ssize_t ret; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 357 | int ifl = iocb->ki_flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 358 | |
| 359 | if (!iov_iter_count(iter)) |
| 360 | return 0; |
| 361 | |
| 362 | inode_lock(inode); |
| 363 | /* Update mode */ |
| 364 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 365 | ret = file_remove_privs(file); |
| 366 | if (ret) |
| 367 | goto out_unlock; |
| 368 | |
| 369 | ret = ovl_real_fdget(file, &real); |
| 370 | if (ret) |
| 371 | goto out_unlock; |
| 372 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 373 | ret = -EINVAL; |
| 374 | if (iocb->ki_flags & IOCB_DIRECT && |
| 375 | (!real.file->f_mapping->a_ops || |
| 376 | !real.file->f_mapping->a_ops->direct_IO)) |
| 377 | goto out_fdput; |
| 378 | |
| 379 | if (!ovl_should_sync(OVL_FS(inode->i_sb))) |
| 380 | ifl &= ~(IOCB_DSYNC | IOCB_SYNC); |
| 381 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 382 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 383 | if (is_sync_kiocb(iocb)) { |
| 384 | file_start_write(real.file); |
| 385 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, |
| 386 | ovl_iocb_to_rwf(ifl)); |
| 387 | file_end_write(real.file); |
| 388 | /* Update size */ |
| 389 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 390 | } else { |
| 391 | struct ovl_aio_req *aio_req; |
| 392 | |
| 393 | ret = -ENOMEM; |
| 394 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); |
| 395 | if (!aio_req) |
| 396 | goto out; |
| 397 | |
| 398 | file_start_write(real.file); |
| 399 | /* Pacify lockdep, same trick as done in aio_write() */ |
| 400 | __sb_writers_release(file_inode(real.file)->i_sb, |
| 401 | SB_FREEZE_WRITE); |
| 402 | aio_req->fd = real; |
| 403 | real.flags = 0; |
| 404 | aio_req->orig_iocb = iocb; |
| 405 | kiocb_clone(&aio_req->iocb, iocb, real.file); |
| 406 | aio_req->iocb.ki_flags = ifl; |
| 407 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
| 408 | refcount_set(&aio_req->ref, 2); |
| 409 | ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter); |
| 410 | ovl_aio_put(aio_req); |
| 411 | if (ret != -EIOCBQUEUED) |
| 412 | ovl_aio_cleanup_handler(aio_req); |
| 413 | } |
| 414 | out: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 415 | revert_creds(old_cred); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 416 | out_fdput: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 417 | fdput(real); |
| 418 | |
| 419 | out_unlock: |
| 420 | inode_unlock(inode); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 425 | /* |
| 426 | * Calling iter_file_splice_write() directly from overlay's f_op may deadlock |
| 427 | * due to lock order inversion between pipe->mutex in iter_file_splice_write() |
| 428 | * and file_start_write(real.file) in ovl_write_iter(). |
| 429 | * |
| 430 | * So do everything ovl_write_iter() does and call iter_file_splice_write() on |
| 431 | * the real file. |
| 432 | */ |
| 433 | static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, |
| 434 | loff_t *ppos, size_t len, unsigned int flags) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 435 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 436 | struct fd real; |
| 437 | const struct cred *old_cred; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 438 | struct inode *inode = file_inode(out); |
| 439 | struct inode *realinode = ovl_inode_real(inode); |
| 440 | ssize_t ret; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 441 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 442 | inode_lock(inode); |
| 443 | /* Update mode */ |
| 444 | ovl_copyattr(realinode, inode); |
| 445 | ret = file_remove_privs(out); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 446 | if (ret) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 447 | goto out_unlock; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 448 | |
| 449 | ret = ovl_real_fdget(out, &real); |
| 450 | if (ret) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 451 | goto out_unlock; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 452 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 453 | old_cred = ovl_override_creds(inode->i_sb); |
| 454 | file_start_write(real.file); |
| 455 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 456 | ret = iter_file_splice_write(pipe, real.file, ppos, len, flags); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 457 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 458 | file_end_write(real.file); |
| 459 | /* Update size */ |
| 460 | ovl_copyattr(realinode, inode); |
| 461 | revert_creds(old_cred); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 462 | fdput(real); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 463 | |
| 464 | out_unlock: |
| 465 | inode_unlock(inode); |
| 466 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 467 | return ret; |
| 468 | } |
| 469 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 470 | static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 471 | { |
| 472 | struct fd real; |
| 473 | const struct cred *old_cred; |
| 474 | int ret; |
| 475 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 476 | ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb)); |
| 477 | if (ret <= 0) |
| 478 | return ret; |
| 479 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 480 | ret = ovl_real_fdget_meta(file, &real, !datasync); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | |
| 484 | /* Don't sync lower file for fear of receiving EROFS error */ |
| 485 | if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) { |
| 486 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 487 | ret = vfs_fsync_range(real.file, start, end, datasync); |
| 488 | revert_creds(old_cred); |
| 489 | } |
| 490 | |
| 491 | fdput(real); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | static int ovl_mmap(struct file *file, struct vm_area_struct *vma) |
| 497 | { |
| 498 | struct file *realfile = file->private_data; |
| 499 | const struct cred *old_cred; |
| 500 | int ret; |
| 501 | |
| 502 | if (!realfile->f_op->mmap) |
| 503 | return -ENODEV; |
| 504 | |
| 505 | if (WARN_ON(file != vma->vm_file)) |
| 506 | return -EIO; |
| 507 | |
| 508 | vma->vm_file = get_file(realfile); |
| 509 | |
| 510 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 511 | ret = call_mmap(vma->vm_file, vma); |
| 512 | revert_creds(old_cred); |
| 513 | |
| 514 | if (ret) { |
| 515 | /* Drop reference count from new vm_file value */ |
| 516 | fput(realfile); |
| 517 | } else { |
| 518 | /* Drop reference count from previous vm_file value */ |
| 519 | fput(file); |
| 520 | } |
| 521 | |
| 522 | ovl_file_accessed(file); |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 528 | { |
| 529 | struct inode *inode = file_inode(file); |
| 530 | struct fd real; |
| 531 | const struct cred *old_cred; |
| 532 | int ret; |
| 533 | |
| 534 | ret = ovl_real_fdget(file, &real); |
| 535 | if (ret) |
| 536 | return ret; |
| 537 | |
| 538 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 539 | ret = vfs_fallocate(real.file, mode, offset, len); |
| 540 | revert_creds(old_cred); |
| 541 | |
| 542 | /* Update size */ |
| 543 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 544 | |
| 545 | fdput(real); |
| 546 | |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) |
| 551 | { |
| 552 | struct fd real; |
| 553 | const struct cred *old_cred; |
| 554 | int ret; |
| 555 | |
| 556 | ret = ovl_real_fdget(file, &real); |
| 557 | if (ret) |
| 558 | return ret; |
| 559 | |
| 560 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 561 | ret = vfs_fadvise(real.file, offset, len, advice); |
| 562 | revert_creds(old_cred); |
| 563 | |
| 564 | fdput(real); |
| 565 | |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | static long ovl_real_ioctl(struct file *file, unsigned int cmd, |
| 570 | unsigned long arg) |
| 571 | { |
| 572 | struct fd real; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 573 | long ret; |
| 574 | |
| 575 | ret = ovl_real_fdget(file, &real); |
| 576 | if (ret) |
| 577 | return ret; |
| 578 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 579 | ret = security_file_ioctl(real.file, cmd, arg); |
| 580 | if (!ret) { |
| 581 | /* |
| 582 | * Don't override creds, since we currently can't safely check |
| 583 | * permissions before doing so. |
| 584 | */ |
| 585 | ret = vfs_ioctl(real.file, cmd, arg); |
| 586 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 587 | |
| 588 | fdput(real); |
| 589 | |
| 590 | return ret; |
| 591 | } |
| 592 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 593 | static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 594 | unsigned long arg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 595 | { |
| 596 | long ret; |
| 597 | struct inode *inode = file_inode(file); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 598 | |
| 599 | if (!inode_owner_or_capable(inode)) |
| 600 | return -EACCES; |
| 601 | |
| 602 | ret = mnt_want_write_file(file); |
| 603 | if (ret) |
| 604 | return ret; |
| 605 | |
| 606 | inode_lock(inode); |
| 607 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 608 | /* |
| 609 | * Prevent copy up if immutable and has no CAP_LINUX_IMMUTABLE |
| 610 | * capability. |
| 611 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 612 | ret = -EPERM; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 613 | if (!ovl_has_upperdata(inode) && IS_IMMUTABLE(inode) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 614 | !capable(CAP_LINUX_IMMUTABLE)) |
| 615 | goto unlock; |
| 616 | |
| 617 | ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY); |
| 618 | if (ret) |
| 619 | goto unlock; |
| 620 | |
| 621 | ret = ovl_real_ioctl(file, cmd, arg); |
| 622 | |
| 623 | ovl_copyflags(ovl_inode_real(inode), inode); |
| 624 | unlock: |
| 625 | inode_unlock(inode); |
| 626 | |
| 627 | mnt_drop_write_file(file); |
| 628 | |
| 629 | return ret; |
| 630 | |
| 631 | } |
| 632 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 633 | long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 634 | { |
| 635 | long ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 636 | |
| 637 | switch (cmd) { |
| 638 | case FS_IOC_GETFLAGS: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 639 | case FS_IOC_FSGETXATTR: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 640 | ret = ovl_real_ioctl(file, cmd, arg); |
| 641 | break; |
| 642 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 643 | case FS_IOC_FSSETXATTR: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 644 | case FS_IOC_SETFLAGS: |
| 645 | ret = ovl_ioctl_set_flags(file, cmd, arg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 646 | break; |
| 647 | |
| 648 | default: |
| 649 | ret = -ENOTTY; |
| 650 | } |
| 651 | |
| 652 | return ret; |
| 653 | } |
| 654 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 655 | #ifdef CONFIG_COMPAT |
| 656 | long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 657 | { |
| 658 | switch (cmd) { |
| 659 | case FS_IOC32_GETFLAGS: |
| 660 | cmd = FS_IOC_GETFLAGS; |
| 661 | break; |
| 662 | |
| 663 | case FS_IOC32_SETFLAGS: |
| 664 | cmd = FS_IOC_SETFLAGS; |
| 665 | break; |
| 666 | |
| 667 | default: |
| 668 | return -ENOIOCTLCMD; |
| 669 | } |
| 670 | |
| 671 | return ovl_ioctl(file, cmd, arg); |
| 672 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 673 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 674 | |
| 675 | enum ovl_copyop { |
| 676 | OVL_COPY, |
| 677 | OVL_CLONE, |
| 678 | OVL_DEDUPE, |
| 679 | }; |
| 680 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 681 | static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 682 | struct file *file_out, loff_t pos_out, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 683 | loff_t len, unsigned int flags, enum ovl_copyop op) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 684 | { |
| 685 | struct inode *inode_out = file_inode(file_out); |
| 686 | struct fd real_in, real_out; |
| 687 | const struct cred *old_cred; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 688 | loff_t ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 689 | |
| 690 | ret = ovl_real_fdget(file_out, &real_out); |
| 691 | if (ret) |
| 692 | return ret; |
| 693 | |
| 694 | ret = ovl_real_fdget(file_in, &real_in); |
| 695 | if (ret) { |
| 696 | fdput(real_out); |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | old_cred = ovl_override_creds(file_inode(file_out)->i_sb); |
| 701 | switch (op) { |
| 702 | case OVL_COPY: |
| 703 | ret = vfs_copy_file_range(real_in.file, pos_in, |
| 704 | real_out.file, pos_out, len, flags); |
| 705 | break; |
| 706 | |
| 707 | case OVL_CLONE: |
| 708 | ret = vfs_clone_file_range(real_in.file, pos_in, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 709 | real_out.file, pos_out, len, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 710 | break; |
| 711 | |
| 712 | case OVL_DEDUPE: |
| 713 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 714 | real_out.file, pos_out, len, |
| 715 | flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 716 | break; |
| 717 | } |
| 718 | revert_creds(old_cred); |
| 719 | |
| 720 | /* Update size */ |
| 721 | ovl_copyattr(ovl_inode_real(inode_out), inode_out); |
| 722 | |
| 723 | fdput(real_in); |
| 724 | fdput(real_out); |
| 725 | |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in, |
| 730 | struct file *file_out, loff_t pos_out, |
| 731 | size_t len, unsigned int flags) |
| 732 | { |
| 733 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags, |
| 734 | OVL_COPY); |
| 735 | } |
| 736 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 737 | static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in, |
| 738 | struct file *file_out, loff_t pos_out, |
| 739 | loff_t len, unsigned int remap_flags) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 740 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 741 | enum ovl_copyop op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 742 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 743 | if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) |
| 744 | return -EINVAL; |
| 745 | |
| 746 | if (remap_flags & REMAP_FILE_DEDUP) |
| 747 | op = OVL_DEDUPE; |
| 748 | else |
| 749 | op = OVL_CLONE; |
| 750 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 751 | /* |
| 752 | * Don't copy up because of a dedupe request, this wouldn't make sense |
| 753 | * most of the time (data would be duplicated instead of deduplicated). |
| 754 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 755 | if (op == OVL_DEDUPE && |
| 756 | (!ovl_inode_upper(file_inode(file_in)) || |
| 757 | !ovl_inode_upper(file_inode(file_out)))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 758 | return -EPERM; |
| 759 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 760 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, |
| 761 | remap_flags, op); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | const struct file_operations ovl_file_operations = { |
| 765 | .open = ovl_open, |
| 766 | .release = ovl_release, |
| 767 | .llseek = ovl_llseek, |
| 768 | .read_iter = ovl_read_iter, |
| 769 | .write_iter = ovl_write_iter, |
| 770 | .fsync = ovl_fsync, |
| 771 | .mmap = ovl_mmap, |
| 772 | .fallocate = ovl_fallocate, |
| 773 | .fadvise = ovl_fadvise, |
| 774 | .unlocked_ioctl = ovl_ioctl, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 775 | #ifdef CONFIG_COMPAT |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 776 | .compat_ioctl = ovl_compat_ioctl, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 777 | #endif |
| 778 | .splice_read = generic_file_splice_read, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 779 | .splice_write = ovl_splice_write, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 780 | |
| 781 | .copy_file_range = ovl_copy_file_range, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 782 | .remap_file_range = ovl_remap_file_range, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 783 | }; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 784 | |
| 785 | int __init ovl_aio_request_cache_init(void) |
| 786 | { |
| 787 | ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req", |
| 788 | sizeof(struct ovl_aio_req), |
| 789 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 790 | if (!ovl_aio_request_cachep) |
| 791 | return -ENOMEM; |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | void ovl_aio_request_cache_destroy(void) |
| 797 | { |
| 798 | kmem_cache_destroy(ovl_aio_request_cachep); |
| 799 | } |