copy_up.c 9.8 KB
Newer Older
M
Miklos Szeredi 已提交
1 2 3 4 5 6 7 8 9
/*
 *
 * Copyright (C) 2011 Novell Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

10
#include <linux/module.h>
M
Miklos Szeredi 已提交
11 12 13 14 15 16 17 18 19
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/splice.h>
#include <linux/xattr.h>
#include <linux/security.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/namei.h>
20 21
#include <linux/fdtable.h>
#include <linux/ratelimit.h>
M
Miklos Szeredi 已提交
22
#include "overlayfs.h"
23
#include "ovl_entry.h"
M
Miklos Szeredi 已提交
24 25 26

#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)

27 28 29 30 31 32 33 34 35 36
static bool __read_mostly ovl_check_copy_up;
module_param_named(check_copy_up, ovl_check_copy_up, bool,
		   S_IWUSR | S_IRUGO);
MODULE_PARM_DESC(ovl_check_copy_up,
		 "Warn on copy-up when causing process also has a R/O fd open");

static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
{
	const struct dentry *dentry = data;

A
Al Viro 已提交
37
	if (file_inode(f) == d_inode(dentry))
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
		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",
				    f, fd, current->pid, current->comm);
	return 0;
}

/*
 * Check the fds open by this process and warn if something like the following
 * scenario is about to occur:
 *
 *	fd1 = open("foo", O_RDONLY);
 *	fd2 = open("foo", O_RDWR);
 */
static void ovl_do_check_copy_up(struct dentry *dentry)
{
	if (ovl_check_copy_up)
		iterate_fd(current->files, 0, ovl_check_fd, dentry);
}

M
Miklos Szeredi 已提交
56 57
int ovl_copy_xattr(struct dentry *old, struct dentry *new)
{
58 59 60
	ssize_t list_size, size, value_size = 0;
	char *buf, *name, *value = NULL;
	int uninitialized_var(error);
61
	size_t slen;
M
Miklos Szeredi 已提交
62

63 64
	if (!(old->d_inode->i_opflags & IOP_XATTR) ||
	    !(new->d_inode->i_opflags & IOP_XATTR))
M
Miklos Szeredi 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		return 0;

	list_size = vfs_listxattr(old, NULL, 0);
	if (list_size <= 0) {
		if (list_size == -EOPNOTSUPP)
			return 0;
		return list_size;
	}

	buf = kzalloc(list_size, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	list_size = vfs_listxattr(old, buf, list_size);
	if (list_size <= 0) {
		error = list_size;
81
		goto out;
M
Miklos Szeredi 已提交
82 83
	}

84 85 86 87 88 89 90 91 92 93
	for (name = buf; list_size; name += slen) {
		slen = strnlen(name, list_size) + 1;

		/* underlying fs providing us with an broken xattr list? */
		if (WARN_ON(slen > list_size)) {
			error = -EIO;
			break;
		}
		list_size -= slen;

M
Miklos Szeredi 已提交
94 95
		if (ovl_is_private_xattr(name))
			continue;
96 97 98 99 100
retry:
		size = vfs_getxattr(old, name, value, value_size);
		if (size == -ERANGE)
			size = vfs_getxattr(old, name, NULL, 0);

M
Miklos Szeredi 已提交
101
		if (size < 0) {
M
Miklos Szeredi 已提交
102
			error = size;
103
			break;
M
Miklos Szeredi 已提交
104
		}
105 106 107 108 109 110 111 112 113 114 115 116 117 118

		if (size > value_size) {
			void *new;

			new = krealloc(value, size, GFP_KERNEL);
			if (!new) {
				error = -ENOMEM;
				break;
			}
			value = new;
			value_size = size;
			goto retry;
		}

119 120 121 122 123 124 125
		error = security_inode_copy_up_xattr(name);
		if (error < 0 && error != -EOPNOTSUPP)
			break;
		if (error == 1) {
			error = 0;
			continue; /* Discard */
		}
M
Miklos Szeredi 已提交
126 127
		error = vfs_setxattr(new, name, value, size, 0);
		if (error)
128
			break;
M
Miklos Szeredi 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	}
	kfree(value);
out:
	kfree(buf);
	return error;
}

static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
{
	struct file *old_file;
	struct file *new_file;
	loff_t old_pos = 0;
	loff_t new_pos = 0;
	int error = 0;

	if (len == 0)
		return 0;

147
	old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
M
Miklos Szeredi 已提交
148 149 150
	if (IS_ERR(old_file))
		return PTR_ERR(old_file);

151
	new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
M
Miklos Szeredi 已提交
152 153 154 155 156
	if (IS_ERR(new_file)) {
		error = PTR_ERR(new_file);
		goto out_fput;
	}

157 158 159 160 161 162 163
	/* Try to use clone_file_range to clone up within the same fs */
	error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
	if (!error)
		goto out;
	/* Couldn't clone, so now we try to copy the data */
	error = 0;

M
Miklos Szeredi 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	/* FIXME: copy up sparse files efficiently */
	while (len) {
		size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
		long bytes;

		if (len < this_len)
			this_len = len;

		if (signal_pending_state(TASK_KILLABLE, current)) {
			error = -EINTR;
			break;
		}

		bytes = do_splice_direct(old_file, &old_pos,
					 new_file, &new_pos,
					 this_len, SPLICE_F_MOVE);
		if (bytes <= 0) {
			error = bytes;
			break;
		}
		WARN_ON(old_pos != new_pos);

		len -= bytes;
	}
188
out:
M
Miklos Szeredi 已提交
189 190
	if (!error)
		error = vfs_fsync(new_file, 0);
M
Miklos Szeredi 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	fput(new_file);
out_fput:
	fput(old_file);
	return error;
}

static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
{
	struct iattr attr = {
		.ia_valid =
		     ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
		.ia_atime = stat->atime,
		.ia_mtime = stat->mtime,
	};

	return notify_change(upperdentry, &attr, NULL);
}

int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
{
	int err = 0;

	if (!S_ISLNK(stat->mode)) {
		struct iattr attr = {
			.ia_valid = ATTR_MODE,
			.ia_mode = stat->mode,
		};
		err = notify_change(upperdentry, &attr, NULL);
	}
	if (!err) {
		struct iattr attr = {
			.ia_valid = ATTR_UID | ATTR_GID,
			.ia_uid = stat->uid,
			.ia_gid = stat->gid,
		};
		err = notify_change(upperdentry, &attr, NULL);
	}
	if (!err)
		ovl_set_timestamps(upperdentry, stat);

	return err;
}

static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
			      struct dentry *dentry, struct path *lowerpath,
236
			      struct kstat *stat, const char *link,
237
			      struct kstat *pstat, bool tmpfile)
M
Miklos Szeredi 已提交
238 239 240 241 242
{
	struct inode *wdir = workdir->d_inode;
	struct inode *udir = upperdir->d_inode;
	struct dentry *newdentry = NULL;
	struct dentry *upper = NULL;
243
	struct dentry *temp = NULL;
M
Miklos Szeredi 已提交
244
	int err;
245 246
	const struct cred *old_creds = NULL;
	struct cred *new_creds = NULL;
A
Al Viro 已提交
247 248 249 250 251 252
	struct cattr cattr = {
		/* Can't properly set mode on creation because of the umask */
		.mode = stat->mode & S_IFMT,
		.rdev = stat->rdev,
		.link = link
	};
M
Miklos Szeredi 已提交
253 254 255 256 257

	upper = lookup_one_len(dentry->d_name.name, upperdir,
			       dentry->d_name.len);
	err = PTR_ERR(upper);
	if (IS_ERR(upper))
258
		goto out;
M
Miklos Szeredi 已提交
259

260 261
	err = security_inode_copy_up(dentry, &new_creds);
	if (err < 0)
262
		goto out1;
263 264 265 266

	if (new_creds)
		old_creds = override_creds(new_creds);

267 268 269 270
	if (tmpfile)
		temp = ovl_do_tmpfile(upperdir, stat->mode);
	else
		temp = ovl_lookup_temp(workdir, dentry);
271 272 273 274
	err = PTR_ERR(temp);
	if (IS_ERR(temp))
		goto out1;

275 276 277
	err = 0;
	if (!tmpfile)
		err = ovl_create_real(wdir, temp, &cattr, NULL, true);
278 279 280 281 282 283

	if (new_creds) {
		revert_creds(old_creds);
		put_cred(new_creds);
	}

M
Miklos Szeredi 已提交
284 285 286 287 288
	if (err)
		goto out2;

	if (S_ISREG(stat->mode)) {
		struct path upperpath;
289

M
Miklos Szeredi 已提交
290 291
		ovl_path_upper(dentry, &upperpath);
		BUG_ON(upperpath.dentry != NULL);
292
		upperpath.dentry = temp;
M
Miklos Szeredi 已提交
293 294 295 296 297 298

		err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
		if (err)
			goto out_cleanup;
	}

299
	err = ovl_copy_xattr(lowerpath->dentry, temp);
M
Miklos Szeredi 已提交
300 301 302
	if (err)
		goto out_cleanup;

303 304 305
	inode_lock(temp->d_inode);
	err = ovl_set_attr(temp, stat);
	inode_unlock(temp->d_inode);
M
Miklos Szeredi 已提交
306 307 308
	if (err)
		goto out_cleanup;

309 310 311 312
	if (tmpfile)
		err = ovl_do_link(temp, udir, upper, true);
	else
		err = ovl_do_rename(wdir, temp, udir, upper, 0);
M
Miklos Szeredi 已提交
313 314 315
	if (err)
		goto out_cleanup;

316
	newdentry = dget(tmpfile ? upper : temp);
M
Miklos Szeredi 已提交
317
	ovl_dentry_update(dentry, newdentry);
318
	ovl_inode_update(d_inode(dentry), d_inode(newdentry));
319 320 321

	/* Restore timestamps on parent (best effort) */
	ovl_set_timestamps(upperdir, pstat);
M
Miklos Szeredi 已提交
322
out2:
323
	dput(temp);
M
Miklos Szeredi 已提交
324
out1:
325
	dput(upper);
M
Miklos Szeredi 已提交
326 327 328 329
out:
	return err;

out_cleanup:
330 331
	if (!tmpfile)
		ovl_cleanup(wdir, temp);
D
David Howells 已提交
332
	goto out2;
M
Miklos Szeredi 已提交
333 334 335 336 337
}

/*
 * Copy up a single dentry
 *
M
Miklos Szeredi 已提交
338 339 340 341 342
 * All renames start with copy up of source if necessary.  The actual
 * rename will only proceed once the copy up was successful.  Copy up uses
 * upper parent i_mutex for exclusion.  Since rename can change d_parent it
 * is possible that the copy up will lock the old parent.  At that point
 * the file will have already been copied up anyway.
M
Miklos Szeredi 已提交
343
 */
344 345
static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
			   struct path *lowerpath, struct kstat *stat)
M
Miklos Szeredi 已提交
346
{
M
Miklos Szeredi 已提交
347
	DEFINE_DELAYED_CALL(done);
M
Miklos Szeredi 已提交
348 349 350 351
	struct dentry *workdir = ovl_workdir(dentry);
	int err;
	struct kstat pstat;
	struct path parentpath;
M
Miklos Szeredi 已提交
352
	struct dentry *lowerdentry = lowerpath->dentry;
M
Miklos Szeredi 已提交
353
	struct dentry *upperdir;
M
Miklos Szeredi 已提交
354
	const char *link = NULL;
355 356 357
	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
	/* Should we copyup with O_TMPFILE or with workdir? */
	bool tmpfile = S_ISREG(stat->mode) && ofs->tmpfile;
M
Miklos Szeredi 已提交
358

359 360 361
	if (WARN_ON(!workdir))
		return -EROFS;

M
Miklos Szeredi 已提交
362
	ovl_do_check_copy_up(lowerdentry);
363

M
Miklos Szeredi 已提交
364 365 366 367 368 369 370 371
	ovl_path_upper(parent, &parentpath);
	upperdir = parentpath.dentry;

	err = vfs_getattr(&parentpath, &pstat);
	if (err)
		return err;

	if (S_ISLNK(stat->mode)) {
M
Miklos Szeredi 已提交
372
		link = vfs_get_link(lowerdentry, &done);
M
Miklos Szeredi 已提交
373 374 375 376 377 378 379 380 381
		if (IS_ERR(link))
			return PTR_ERR(link);
	}

	err = -EIO;
	if (lock_rename(workdir, upperdir) != NULL) {
		pr_err("overlayfs: failed to lock workdir+upperdir\n");
		goto out_unlock;
	}
M
Miklos Szeredi 已提交
382
	if (ovl_dentry_upper(dentry)) {
383
		/* Raced with another copy-up?  Nothing to do, then... */
M
Miklos Szeredi 已提交
384
		err = 0;
385
		goto out_unlock;
M
Miklos Szeredi 已提交
386 387 388
	}

	err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
389
				 stat, link, &pstat, tmpfile);
M
Miklos Szeredi 已提交
390 391
out_unlock:
	unlock_rename(workdir, upperdir);
M
Miklos Szeredi 已提交
392
	do_delayed_call(&done);
M
Miklos Szeredi 已提交
393 394 395 396

	return err;
}

397
int ovl_copy_up_flags(struct dentry *dentry, int flags)
M
Miklos Szeredi 已提交
398
{
399 400
	int err = 0;
	const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
M
Miklos Szeredi 已提交
401 402 403 404 405 406 407 408

	while (!err) {
		struct dentry *next;
		struct dentry *parent;
		struct path lowerpath;
		struct kstat stat;
		enum ovl_path_type type = ovl_path_type(dentry);

M
Miklos Szeredi 已提交
409
		if (OVL_TYPE_UPPER(type))
M
Miklos Szeredi 已提交
410 411 412 413 414 415 416 417
			break;

		next = dget(dentry);
		/* find the topmost dentry not yet copied up */
		for (;;) {
			parent = dget_parent(next);

			type = ovl_path_type(parent);
M
Miklos Szeredi 已提交
418
			if (OVL_TYPE_UPPER(type))
M
Miklos Szeredi 已提交
419 420 421 422 423 424 425 426
				break;

			dput(next);
			next = parent;
		}

		ovl_path_lower(next, &lowerpath);
		err = vfs_getattr(&lowerpath, &stat);
427 428 429
		/* maybe truncate regular file. this has no effect on dirs */
		if (flags & O_TRUNC)
			stat.size = 0;
M
Miklos Szeredi 已提交
430
		if (!err)
431
			err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
M
Miklos Szeredi 已提交
432 433 434 435

		dput(parent);
		dput(next);
	}
436
	revert_creds(old_cred);
M
Miklos Szeredi 已提交
437 438 439

	return err;
}
440 441 442 443 444

int ovl_copy_up(struct dentry *dentry)
{
	return ovl_copy_up_flags(dentry, 0);
}