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 10 #include <linux/module.h> 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> 20 #include <linux/fdtable.h> 21 #include <linux/ratelimit.h> 22 #include "overlayfs.h" 23 24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) 25 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 55 int ovl_copy_xattr(struct dentry *old, struct dentry *new) 56 { 57 ssize_t list_size, size, value_size = 0; 58 char *buf, *name, *value = NULL; 59 int uninitialized_var(error); 60 size_t slen; 61 62 if (!(old->d_inode->i_opflags & IOP_XATTR) || 63 !(new->d_inode->i_opflags & IOP_XATTR)) 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 77 list_size = vfs_listxattr(old, buf, list_size); 78 if (list_size <= 0) { 79 error = list_size; 80 goto out; 81 } 82 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 93 if (ovl_is_private_xattr(name)) 94 continue; 95 retry: 96 size = vfs_getxattr(old, name, value, value_size); 97 if (size == -ERANGE) 98 size = vfs_getxattr(old, name, NULL, 0); 99 100 if (size < 0) { 101 error = size; 102 break; 103 } 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 118 error = security_inode_copy_up_xattr(name); 119 if (error < 0 && error != -EOPNOTSUPP) 120 break; 121 if (error == 1) { 122 error = 0; 123 continue; /* Discard */ 124 } 125 error = vfs_setxattr(new, name, value, size, 0); 126 if (error) 127 break; 128 } 129 kfree(value); 130 out: 131 kfree(buf); 132 return error; 133 } 134 135 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) 136 { 137 struct file *old_file; 138 struct file *new_file; 139 loff_t old_pos = 0; 140 loff_t new_pos = 0; 141 int error = 0; 142 143 if (len == 0) 144 return 0; 145 146 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); 147 if (IS_ERR(old_file)) 148 return PTR_ERR(old_file); 149 150 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); 151 if (IS_ERR(new_file)) { 152 error = PTR_ERR(new_file); 153 goto out_fput; 154 } 155 156 /* FIXME: copy up sparse files efficiently */ 157 while (len) { 158 size_t this_len = OVL_COPY_UP_CHUNK_SIZE; 159 long bytes; 160 161 if (len < this_len) 162 this_len = len; 163 164 if (signal_pending_state(TASK_KILLABLE, current)) { 165 error = -EINTR; 166 break; 167 } 168 169 bytes = do_splice_direct(old_file, &old_pos, 170 new_file, &new_pos, 171 this_len, SPLICE_F_MOVE); 172 if (bytes <= 0) { 173 error = bytes; 174 break; 175 } 176 WARN_ON(old_pos != new_pos); 177 178 len -= bytes; 179 } 180 181 fput(new_file); 182 out_fput: 183 fput(old_file); 184 return error; 185 } 186 187 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) 188 { 189 struct iattr attr = { 190 .ia_valid = 191 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, 192 .ia_atime = stat->atime, 193 .ia_mtime = stat->mtime, 194 }; 195 196 return notify_change(upperdentry, &attr, NULL); 197 } 198 199 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) 200 { 201 int err = 0; 202 203 if (!S_ISLNK(stat->mode)) { 204 struct iattr attr = { 205 .ia_valid = ATTR_MODE, 206 .ia_mode = stat->mode, 207 }; 208 err = notify_change(upperdentry, &attr, NULL); 209 } 210 if (!err) { 211 struct iattr attr = { 212 .ia_valid = ATTR_UID | ATTR_GID, 213 .ia_uid = stat->uid, 214 .ia_gid = stat->gid, 215 }; 216 err = notify_change(upperdentry, &attr, NULL); 217 } 218 if (!err) 219 ovl_set_timestamps(upperdentry, stat); 220 221 return err; 222 } 223 224 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, 225 struct dentry *dentry, struct path *lowerpath, 226 struct kstat *stat, const char *link) 227 { 228 struct inode *wdir = workdir->d_inode; 229 struct inode *udir = upperdir->d_inode; 230 struct dentry *newdentry = NULL; 231 struct dentry *upper = NULL; 232 umode_t mode = stat->mode; 233 int err; 234 const struct cred *old_creds = NULL; 235 struct cred *new_creds = NULL; 236 237 newdentry = ovl_lookup_temp(workdir, dentry); 238 err = PTR_ERR(newdentry); 239 if (IS_ERR(newdentry)) 240 goto out; 241 242 upper = lookup_one_len(dentry->d_name.name, upperdir, 243 dentry->d_name.len); 244 err = PTR_ERR(upper); 245 if (IS_ERR(upper)) 246 goto out1; 247 248 err = security_inode_copy_up(dentry, &new_creds); 249 if (err < 0) 250 goto out2; 251 252 if (new_creds) 253 old_creds = override_creds(new_creds); 254 255 /* Can't properly set mode on creation because of the umask */ 256 stat->mode &= S_IFMT; 257 err = ovl_create_real(wdir, newdentry, stat, link, NULL, true); 258 stat->mode = mode; 259 260 if (new_creds) { 261 revert_creds(old_creds); 262 put_cred(new_creds); 263 } 264 265 if (err) 266 goto out2; 267 268 if (S_ISREG(stat->mode)) { 269 struct path upperpath; 270 271 ovl_path_upper(dentry, &upperpath); 272 BUG_ON(upperpath.dentry != NULL); 273 upperpath.dentry = newdentry; 274 275 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size); 276 if (err) 277 goto out_cleanup; 278 } 279 280 err = ovl_copy_xattr(lowerpath->dentry, newdentry); 281 if (err) 282 goto out_cleanup; 283 284 inode_lock(newdentry->d_inode); 285 err = ovl_set_attr(newdentry, stat); 286 inode_unlock(newdentry->d_inode); 287 if (err) 288 goto out_cleanup; 289 290 err = ovl_do_rename(wdir, newdentry, udir, upper, 0); 291 if (err) 292 goto out_cleanup; 293 294 ovl_dentry_update(dentry, newdentry); 295 ovl_inode_update(d_inode(dentry), d_inode(newdentry)); 296 newdentry = NULL; 297 298 /* 299 * Non-directores become opaque when copied up. 300 */ 301 if (!S_ISDIR(stat->mode)) 302 ovl_dentry_set_opaque(dentry, true); 303 out2: 304 dput(upper); 305 out1: 306 dput(newdentry); 307 out: 308 return err; 309 310 out_cleanup: 311 ovl_cleanup(wdir, newdentry); 312 goto out2; 313 } 314 315 /* 316 * Copy up a single dentry 317 * 318 * Directory renames only allowed on "pure upper" (already created on 319 * upper filesystem, never copied up). Directories which are on lower or 320 * are merged may not be renamed. For these -EXDEV is returned and 321 * userspace has to deal with it. This means, when copying up a 322 * directory we can rely on it and ancestors being stable. 323 * 324 * Non-directory renames start with copy up of source if necessary. The 325 * actual rename will only proceed once the copy up was successful. Copy 326 * up uses upper parent i_mutex for exclusion. Since rename can change 327 * d_parent it is possible that the copy up will lock the old parent. At 328 * that point the file will have already been copied up anyway. 329 */ 330 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, 331 struct path *lowerpath, struct kstat *stat) 332 { 333 DEFINE_DELAYED_CALL(done); 334 struct dentry *workdir = ovl_workdir(dentry); 335 int err; 336 struct kstat pstat; 337 struct path parentpath; 338 struct dentry *lowerdentry = lowerpath->dentry; 339 struct dentry *upperdir; 340 struct dentry *upperdentry; 341 const char *link = NULL; 342 343 if (WARN_ON(!workdir)) 344 return -EROFS; 345 346 ovl_do_check_copy_up(lowerdentry); 347 348 ovl_path_upper(parent, &parentpath); 349 upperdir = parentpath.dentry; 350 351 err = vfs_getattr(&parentpath, &pstat); 352 if (err) 353 return err; 354 355 if (S_ISLNK(stat->mode)) { 356 link = vfs_get_link(lowerdentry, &done); 357 if (IS_ERR(link)) 358 return PTR_ERR(link); 359 } 360 361 err = -EIO; 362 if (lock_rename(workdir, upperdir) != NULL) { 363 pr_err("overlayfs: failed to lock workdir+upperdir\n"); 364 goto out_unlock; 365 } 366 upperdentry = ovl_dentry_upper(dentry); 367 if (upperdentry) { 368 /* Raced with another copy-up? Nothing to do, then... */ 369 err = 0; 370 goto out_unlock; 371 } 372 373 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, 374 stat, link); 375 if (!err) { 376 /* Restore timestamps on parent (best effort) */ 377 ovl_set_timestamps(upperdir, &pstat); 378 } 379 out_unlock: 380 unlock_rename(workdir, upperdir); 381 do_delayed_call(&done); 382 383 return err; 384 } 385 386 int ovl_copy_up(struct dentry *dentry) 387 { 388 int err = 0; 389 const struct cred *old_cred = ovl_override_creds(dentry->d_sb); 390 391 while (!err) { 392 struct dentry *next; 393 struct dentry *parent; 394 struct path lowerpath; 395 struct kstat stat; 396 enum ovl_path_type type = ovl_path_type(dentry); 397 398 if (OVL_TYPE_UPPER(type)) 399 break; 400 401 next = dget(dentry); 402 /* find the topmost dentry not yet copied up */ 403 for (;;) { 404 parent = dget_parent(next); 405 406 type = ovl_path_type(parent); 407 if (OVL_TYPE_UPPER(type)) 408 break; 409 410 dput(next); 411 next = parent; 412 } 413 414 ovl_path_lower(next, &lowerpath); 415 err = vfs_getattr(&lowerpath, &stat); 416 if (!err) 417 err = ovl_copy_up_one(parent, next, &lowerpath, &stat); 418 419 dput(parent); 420 dput(next); 421 } 422 revert_creds(old_cred); 423 424 return err; 425 } 426