Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2011 Novell Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 10 | #include <linux/module.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 11 | #include <linux/fs.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/splice.h> |
| 15 | #include <linux/xattr.h> |
| 16 | #include <linux/security.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/namei.h> |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 20 | #include <linux/fdtable.h> |
| 21 | #include <linux/ratelimit.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | #include "overlayfs.h" |
| 23 | |
| 24 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) |
| 25 | |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 26 | static bool __read_mostly ovl_check_copy_up; |
| 27 | module_param_named(check_copy_up, ovl_check_copy_up, bool, |
| 28 | S_IWUSR | S_IRUGO); |
| 29 | MODULE_PARM_DESC(ovl_check_copy_up, |
| 30 | "Warn on copy-up when causing process also has a R/O fd open"); |
| 31 | |
| 32 | static int ovl_check_fd(const void *data, struct file *f, unsigned int fd) |
| 33 | { |
| 34 | const struct dentry *dentry = data; |
| 35 | |
| 36 | if (f->f_inode == d_inode(dentry)) |
| 37 | pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n", |
| 38 | f, fd, current->pid, current->comm); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Check the fds open by this process and warn if something like the following |
| 44 | * scenario is about to occur: |
| 45 | * |
| 46 | * fd1 = open("foo", O_RDONLY); |
| 47 | * fd2 = open("foo", O_RDWR); |
| 48 | */ |
| 49 | static void ovl_do_check_copy_up(struct dentry *dentry) |
| 50 | { |
| 51 | if (ovl_check_copy_up) |
| 52 | iterate_fd(current->files, 0, ovl_check_fd, dentry); |
| 53 | } |
| 54 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 55 | int ovl_copy_xattr(struct dentry *old, struct dentry *new) |
| 56 | { |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 57 | ssize_t list_size, size, value_size = 0; |
| 58 | char *buf, *name, *value = NULL; |
| 59 | int uninitialized_var(error); |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame^] | 60 | size_t slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 61 | |
| 62 | if (!old->d_inode->i_op->getxattr || |
| 63 | !new->d_inode->i_op->getxattr) |
| 64 | return 0; |
| 65 | |
| 66 | list_size = vfs_listxattr(old, NULL, 0); |
| 67 | if (list_size <= 0) { |
| 68 | if (list_size == -EOPNOTSUPP) |
| 69 | return 0; |
| 70 | return list_size; |
| 71 | } |
| 72 | |
| 73 | buf = kzalloc(list_size, GFP_KERNEL); |
| 74 | if (!buf) |
| 75 | return -ENOMEM; |
| 76 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 77 | list_size = vfs_listxattr(old, buf, list_size); |
| 78 | if (list_size <= 0) { |
| 79 | error = list_size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 80 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame^] | 83 | for (name = buf; list_size; name += slen) { |
| 84 | slen = strnlen(name, list_size) + 1; |
| 85 | |
| 86 | /* underlying fs providing us with an broken xattr list? */ |
| 87 | if (WARN_ON(slen > list_size)) { |
| 88 | error = -EIO; |
| 89 | break; |
| 90 | } |
| 91 | list_size -= slen; |
| 92 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 93 | if (ovl_is_private_xattr(name)) |
| 94 | continue; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 95 | retry: |
| 96 | size = vfs_getxattr(old, name, value, value_size); |
| 97 | if (size == -ERANGE) |
| 98 | size = vfs_getxattr(old, name, NULL, 0); |
| 99 | |
Miklos Szeredi | 97daf8b | 2015-11-10 17:08:41 +0100 | [diff] [blame] | 100 | if (size < 0) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 101 | error = size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 102 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 103 | } |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 104 | |
| 105 | if (size > value_size) { |
| 106 | void *new; |
| 107 | |
| 108 | new = krealloc(value, size, GFP_KERNEL); |
| 109 | if (!new) { |
| 110 | error = -ENOMEM; |
| 111 | break; |
| 112 | } |
| 113 | value = new; |
| 114 | value_size = size; |
| 115 | goto retry; |
| 116 | } |
| 117 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 118 | error = vfs_setxattr(new, name, value, size, 0); |
| 119 | if (error) |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 120 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 121 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 122 | kfree(value); |
| 123 | out: |
| 124 | kfree(buf); |
| 125 | return error; |
| 126 | } |
| 127 | |
| 128 | static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) |
| 129 | { |
| 130 | struct file *old_file; |
| 131 | struct file *new_file; |
| 132 | loff_t old_pos = 0; |
| 133 | loff_t new_pos = 0; |
| 134 | int error = 0; |
| 135 | |
| 136 | if (len == 0) |
| 137 | return 0; |
| 138 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 139 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 140 | if (IS_ERR(old_file)) |
| 141 | return PTR_ERR(old_file); |
| 142 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 143 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 144 | if (IS_ERR(new_file)) { |
| 145 | error = PTR_ERR(new_file); |
| 146 | goto out_fput; |
| 147 | } |
| 148 | |
| 149 | /* FIXME: copy up sparse files efficiently */ |
| 150 | while (len) { |
| 151 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; |
| 152 | long bytes; |
| 153 | |
| 154 | if (len < this_len) |
| 155 | this_len = len; |
| 156 | |
| 157 | if (signal_pending_state(TASK_KILLABLE, current)) { |
| 158 | error = -EINTR; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | bytes = do_splice_direct(old_file, &old_pos, |
| 163 | new_file, &new_pos, |
| 164 | this_len, SPLICE_F_MOVE); |
| 165 | if (bytes <= 0) { |
| 166 | error = bytes; |
| 167 | break; |
| 168 | } |
| 169 | WARN_ON(old_pos != new_pos); |
| 170 | |
| 171 | len -= bytes; |
| 172 | } |
| 173 | |
| 174 | fput(new_file); |
| 175 | out_fput: |
| 176 | fput(old_file); |
| 177 | return error; |
| 178 | } |
| 179 | |
| 180 | static char *ovl_read_symlink(struct dentry *realdentry) |
| 181 | { |
| 182 | int res; |
| 183 | char *buf; |
| 184 | struct inode *inode = realdentry->d_inode; |
| 185 | mm_segment_t old_fs; |
| 186 | |
| 187 | res = -EINVAL; |
| 188 | if (!inode->i_op->readlink) |
| 189 | goto err; |
| 190 | |
| 191 | res = -ENOMEM; |
| 192 | buf = (char *) __get_free_page(GFP_KERNEL); |
| 193 | if (!buf) |
| 194 | goto err; |
| 195 | |
| 196 | old_fs = get_fs(); |
| 197 | set_fs(get_ds()); |
| 198 | /* The cast to a user pointer is valid due to the set_fs() */ |
| 199 | res = inode->i_op->readlink(realdentry, |
| 200 | (char __user *)buf, PAGE_SIZE - 1); |
| 201 | set_fs(old_fs); |
| 202 | if (res < 0) { |
| 203 | free_page((unsigned long) buf); |
| 204 | goto err; |
| 205 | } |
| 206 | buf[res] = '\0'; |
| 207 | |
| 208 | return buf; |
| 209 | |
| 210 | err: |
| 211 | return ERR_PTR(res); |
| 212 | } |
| 213 | |
| 214 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
| 215 | { |
| 216 | struct iattr attr = { |
| 217 | .ia_valid = |
| 218 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, |
| 219 | .ia_atime = stat->atime, |
| 220 | .ia_mtime = stat->mtime, |
| 221 | }; |
| 222 | |
| 223 | return notify_change(upperdentry, &attr, NULL); |
| 224 | } |
| 225 | |
| 226 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) |
| 227 | { |
| 228 | int err = 0; |
| 229 | |
| 230 | if (!S_ISLNK(stat->mode)) { |
| 231 | struct iattr attr = { |
| 232 | .ia_valid = ATTR_MODE, |
| 233 | .ia_mode = stat->mode, |
| 234 | }; |
| 235 | err = notify_change(upperdentry, &attr, NULL); |
| 236 | } |
| 237 | if (!err) { |
| 238 | struct iattr attr = { |
| 239 | .ia_valid = ATTR_UID | ATTR_GID, |
| 240 | .ia_uid = stat->uid, |
| 241 | .ia_gid = stat->gid, |
| 242 | }; |
| 243 | err = notify_change(upperdentry, &attr, NULL); |
| 244 | } |
| 245 | if (!err) |
| 246 | ovl_set_timestamps(upperdentry, stat); |
| 247 | |
| 248 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, |
| 252 | struct dentry *dentry, struct path *lowerpath, |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 253 | struct kstat *stat, const char *link) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 254 | { |
| 255 | struct inode *wdir = workdir->d_inode; |
| 256 | struct inode *udir = upperdir->d_inode; |
| 257 | struct dentry *newdentry = NULL; |
| 258 | struct dentry *upper = NULL; |
| 259 | umode_t mode = stat->mode; |
| 260 | int err; |
| 261 | |
| 262 | newdentry = ovl_lookup_temp(workdir, dentry); |
| 263 | err = PTR_ERR(newdentry); |
| 264 | if (IS_ERR(newdentry)) |
| 265 | goto out; |
| 266 | |
| 267 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 268 | dentry->d_name.len); |
| 269 | err = PTR_ERR(upper); |
| 270 | if (IS_ERR(upper)) |
| 271 | goto out1; |
| 272 | |
| 273 | /* Can't properly set mode on creation because of the umask */ |
| 274 | stat->mode &= S_IFMT; |
| 275 | err = ovl_create_real(wdir, newdentry, stat, link, NULL, true); |
| 276 | stat->mode = mode; |
| 277 | if (err) |
| 278 | goto out2; |
| 279 | |
| 280 | if (S_ISREG(stat->mode)) { |
| 281 | struct path upperpath; |
Sohom Bhattacharjee | f134f24 | 2016-03-15 20:57:59 +0530 | [diff] [blame] | 282 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 283 | ovl_path_upper(dentry, &upperpath); |
| 284 | BUG_ON(upperpath.dentry != NULL); |
| 285 | upperpath.dentry = newdentry; |
| 286 | |
| 287 | err = ovl_copy_up_data(lowerpath, &upperpath, stat->size); |
| 288 | if (err) |
| 289 | goto out_cleanup; |
| 290 | } |
| 291 | |
| 292 | err = ovl_copy_xattr(lowerpath->dentry, newdentry); |
| 293 | if (err) |
| 294 | goto out_cleanup; |
| 295 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 296 | inode_lock(newdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 297 | err = ovl_set_attr(newdentry, stat); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 298 | inode_unlock(newdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 299 | if (err) |
| 300 | goto out_cleanup; |
| 301 | |
| 302 | err = ovl_do_rename(wdir, newdentry, udir, upper, 0); |
| 303 | if (err) |
| 304 | goto out_cleanup; |
| 305 | |
| 306 | ovl_dentry_update(dentry, newdentry); |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 307 | ovl_inode_update(d_inode(dentry), d_inode(newdentry)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 308 | newdentry = NULL; |
| 309 | |
| 310 | /* |
| 311 | * Non-directores become opaque when copied up. |
| 312 | */ |
| 313 | if (!S_ISDIR(stat->mode)) |
| 314 | ovl_dentry_set_opaque(dentry, true); |
| 315 | out2: |
| 316 | dput(upper); |
| 317 | out1: |
| 318 | dput(newdentry); |
| 319 | out: |
| 320 | return err; |
| 321 | |
| 322 | out_cleanup: |
| 323 | ovl_cleanup(wdir, newdentry); |
David Howells | ab79efa | 2015-09-18 11:45:22 +0100 | [diff] [blame] | 324 | goto out2; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Copy up a single dentry |
| 329 | * |
| 330 | * Directory renames only allowed on "pure upper" (already created on |
| 331 | * upper filesystem, never copied up). Directories which are on lower or |
| 332 | * are merged may not be renamed. For these -EXDEV is returned and |
| 333 | * userspace has to deal with it. This means, when copying up a |
| 334 | * directory we can rely on it and ancestors being stable. |
| 335 | * |
| 336 | * Non-directory renames start with copy up of source if necessary. The |
| 337 | * actual rename will only proceed once the copy up was successful. Copy |
| 338 | * up uses upper parent i_mutex for exclusion. Since rename can change |
| 339 | * d_parent it is possible that the copy up will lock the old parent. At |
| 340 | * that point the file will have already been copied up anyway. |
| 341 | */ |
| 342 | int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 343 | struct path *lowerpath, struct kstat *stat) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 344 | { |
| 345 | struct dentry *workdir = ovl_workdir(dentry); |
| 346 | int err; |
| 347 | struct kstat pstat; |
| 348 | struct path parentpath; |
| 349 | struct dentry *upperdir; |
| 350 | struct dentry *upperdentry; |
| 351 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 352 | char *link = NULL; |
| 353 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 354 | if (WARN_ON(!workdir)) |
| 355 | return -EROFS; |
| 356 | |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 357 | ovl_do_check_copy_up(lowerpath->dentry); |
| 358 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 359 | ovl_path_upper(parent, &parentpath); |
| 360 | upperdir = parentpath.dentry; |
| 361 | |
| 362 | err = vfs_getattr(&parentpath, &pstat); |
| 363 | if (err) |
| 364 | return err; |
| 365 | |
| 366 | if (S_ISLNK(stat->mode)) { |
| 367 | link = ovl_read_symlink(lowerpath->dentry); |
| 368 | if (IS_ERR(link)) |
| 369 | return PTR_ERR(link); |
| 370 | } |
| 371 | |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 372 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 373 | |
| 374 | err = -EIO; |
| 375 | if (lock_rename(workdir, upperdir) != NULL) { |
| 376 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); |
| 377 | goto out_unlock; |
| 378 | } |
| 379 | upperdentry = ovl_dentry_upper(dentry); |
| 380 | if (upperdentry) { |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 381 | /* Raced with another copy-up? Nothing to do, then... */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 382 | err = 0; |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 383 | goto out_unlock; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 387 | stat, link); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 388 | if (!err) { |
| 389 | /* Restore timestamps on parent (best effort) */ |
| 390 | ovl_set_timestamps(upperdir, &pstat); |
| 391 | } |
| 392 | out_unlock: |
| 393 | unlock_rename(workdir, upperdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 394 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 395 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 396 | if (link) |
| 397 | free_page((unsigned long) link); |
| 398 | |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | int ovl_copy_up(struct dentry *dentry) |
| 403 | { |
| 404 | int err; |
| 405 | |
| 406 | err = 0; |
| 407 | while (!err) { |
| 408 | struct dentry *next; |
| 409 | struct dentry *parent; |
| 410 | struct path lowerpath; |
| 411 | struct kstat stat; |
| 412 | enum ovl_path_type type = ovl_path_type(dentry); |
| 413 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 414 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 415 | break; |
| 416 | |
| 417 | next = dget(dentry); |
| 418 | /* find the topmost dentry not yet copied up */ |
| 419 | for (;;) { |
| 420 | parent = dget_parent(next); |
| 421 | |
| 422 | type = ovl_path_type(parent); |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 423 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | dput(next); |
| 427 | next = parent; |
| 428 | } |
| 429 | |
| 430 | ovl_path_lower(next, &lowerpath); |
| 431 | err = vfs_getattr(&lowerpath, &stat); |
| 432 | if (!err) |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 433 | err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 434 | |
| 435 | dput(parent); |
| 436 | dput(next); |
| 437 | } |
| 438 | |
| 439 | return err; |
| 440 | } |