blob: 01e332725e94dae0ac658b80f5363b61550e3833 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
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 Howellsfb5bb2c32015-07-07 15:04:44 +010010#include <linux/module.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#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 Howellsfb5bb2c32015-07-07 15:04:44 +010020#include <linux/fdtable.h>
21#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022#include "overlayfs.h"
23
24#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
David Howellsfb5bb2c32015-07-07 15:04:44 +010026static bool __read_mostly ovl_check_copy_up;
27module_param_named(check_copy_up, ovl_check_copy_up, bool,
28 S_IWUSR | S_IRUGO);
29MODULE_PARM_DESC(ovl_check_copy_up,
30 "Warn on copy-up when causing process also has a R/O fd open");
31
32static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
33{
34 const struct dentry *dentry = data;
35
Al Viro45063092016-12-04 18:24:56 -050036 if (file_inode(f) == d_inode(dentry))
David Howellsfb5bb2c32015-07-07 15:04:44 +010037 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 */
49static 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 Szeredie9be9d52014-10-24 00:14:38 +020055int ovl_copy_xattr(struct dentry *old, struct dentry *new)
56{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050057 ssize_t list_size, size, value_size = 0;
58 char *buf, *name, *value = NULL;
59 int uninitialized_var(error);
Miklos Szeredi8b326c62016-09-16 14:12:11 +020060 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020061
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020062 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
63 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064 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 Szeredie9be9d52014-10-24 00:14:38 +020077 list_size = vfs_listxattr(old, buf, list_size);
78 if (list_size <= 0) {
79 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050080 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020081 }
82
Miklos Szeredi8b326c62016-09-16 14:12:11 +020083 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 Szeredi09562542016-08-08 15:08:49 +020093 if (ovl_is_private_xattr(name))
94 continue;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050095retry:
96 size = vfs_getxattr(old, name, value, value_size);
97 if (size == -ERANGE)
98 size = vfs_getxattr(old, name, NULL, 0);
99
Miklos Szeredi97daf8b2015-11-10 17:08:41 +0100100 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200101 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500102 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200103 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500104
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
Vivek Goyal121ab822016-07-13 10:44:49 -0400118 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 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200125 error = vfs_setxattr(new, name, value, size, 0);
126 if (error)
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500127 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200129 kfree(value);
130out:
131 kfree(buf);
132 return error;
133}
134
135static 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
David Howells04803342015-09-18 11:45:12 +0100146 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147 if (IS_ERR(old_file))
148 return PTR_ERR(old_file);
149
David Howells04803342015-09-18 11:45:12 +0100150 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151 if (IS_ERR(new_file)) {
152 error = PTR_ERR(new_file);
153 goto out_fput;
154 }
155
Amir Goldstein2ea98462016-09-23 11:38:12 +0300156 /* Try to use clone_file_range to clone up within the same fs */
157 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
158 if (!error)
159 goto out;
160 /* Couldn't clone, so now we try to copy the data */
161 error = 0;
162
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163 /* FIXME: copy up sparse files efficiently */
164 while (len) {
165 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
166 long bytes;
167
168 if (len < this_len)
169 this_len = len;
170
171 if (signal_pending_state(TASK_KILLABLE, current)) {
172 error = -EINTR;
173 break;
174 }
175
176 bytes = do_splice_direct(old_file, &old_pos,
177 new_file, &new_pos,
178 this_len, SPLICE_F_MOVE);
179 if (bytes <= 0) {
180 error = bytes;
181 break;
182 }
183 WARN_ON(old_pos != new_pos);
184
185 len -= bytes;
186 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300187out:
Miklos Szeredi641089c2016-10-31 14:42:14 +0100188 if (!error)
189 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200190 fput(new_file);
191out_fput:
192 fput(old_file);
193 return error;
194}
195
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
197{
198 struct iattr attr = {
199 .ia_valid =
200 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
201 .ia_atime = stat->atime,
202 .ia_mtime = stat->mtime,
203 };
204
205 return notify_change(upperdentry, &attr, NULL);
206}
207
208int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
209{
210 int err = 0;
211
212 if (!S_ISLNK(stat->mode)) {
213 struct iattr attr = {
214 .ia_valid = ATTR_MODE,
215 .ia_mode = stat->mode,
216 };
217 err = notify_change(upperdentry, &attr, NULL);
218 }
219 if (!err) {
220 struct iattr attr = {
221 .ia_valid = ATTR_UID | ATTR_GID,
222 .ia_uid = stat->uid,
223 .ia_gid = stat->gid,
224 };
225 err = notify_change(upperdentry, &attr, NULL);
226 }
227 if (!err)
228 ovl_set_timestamps(upperdentry, stat);
229
230 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200231}
232
233static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
234 struct dentry *dentry, struct path *lowerpath,
Amir Goldstein42f269b2017-01-17 06:34:54 +0200235 struct kstat *stat, const char *link,
236 struct kstat *pstat)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237{
238 struct inode *wdir = workdir->d_inode;
239 struct inode *udir = upperdir->d_inode;
240 struct dentry *newdentry = NULL;
241 struct dentry *upper = NULL;
Amir Goldstein42f269b2017-01-17 06:34:54 +0200242 struct dentry *temp = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200243 int err;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400244 const struct cred *old_creds = NULL;
245 struct cred *new_creds = NULL;
Al Viro32a3d842016-12-04 17:33:17 +0000246 struct cattr cattr = {
247 /* Can't properly set mode on creation because of the umask */
248 .mode = stat->mode & S_IFMT,
249 .rdev = stat->rdev,
250 .link = link
251 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200252
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200253 upper = lookup_one_len(dentry->d_name.name, upperdir,
254 dentry->d_name.len);
255 err = PTR_ERR(upper);
256 if (IS_ERR(upper))
Amir Goldstein42f269b2017-01-17 06:34:54 +0200257 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200258
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400259 err = security_inode_copy_up(dentry, &new_creds);
260 if (err < 0)
Amir Goldstein42f269b2017-01-17 06:34:54 +0200261 goto out1;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400262
263 if (new_creds)
264 old_creds = override_creds(new_creds);
265
Amir Goldstein42f269b2017-01-17 06:34:54 +0200266 temp = ovl_lookup_temp(workdir, dentry);
267 err = PTR_ERR(temp);
268 if (IS_ERR(temp))
269 goto out1;
270
271 err = ovl_create_real(wdir, temp, &cattr, NULL, true);
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400272
273 if (new_creds) {
274 revert_creds(old_creds);
275 put_cred(new_creds);
276 }
277
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278 if (err)
279 goto out2;
280
281 if (S_ISREG(stat->mode)) {
282 struct path upperpath;
Sohom Bhattacharjeef134f242016-03-15 20:57:59 +0530283
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200284 ovl_path_upper(dentry, &upperpath);
285 BUG_ON(upperpath.dentry != NULL);
Amir Goldstein42f269b2017-01-17 06:34:54 +0200286 upperpath.dentry = temp;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287
288 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
289 if (err)
290 goto out_cleanup;
291 }
292
Amir Goldstein42f269b2017-01-17 06:34:54 +0200293 err = ovl_copy_xattr(lowerpath->dentry, temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294 if (err)
295 goto out_cleanup;
296
Amir Goldstein42f269b2017-01-17 06:34:54 +0200297 inode_lock(temp->d_inode);
298 err = ovl_set_attr(temp, stat);
299 inode_unlock(temp->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300 if (err)
301 goto out_cleanup;
302
Amir Goldstein42f269b2017-01-17 06:34:54 +0200303 err = ovl_do_rename(wdir, temp, udir, upper, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200304 if (err)
305 goto out_cleanup;
306
Amir Goldstein42f269b2017-01-17 06:34:54 +0200307 newdentry = dget(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200309 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
Amir Goldstein42f269b2017-01-17 06:34:54 +0200310
311 /* Restore timestamps on parent (best effort) */
312 ovl_set_timestamps(upperdir, pstat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313out2:
Amir Goldstein42f269b2017-01-17 06:34:54 +0200314 dput(temp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200315out1:
Amir Goldstein42f269b2017-01-17 06:34:54 +0200316 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317out:
318 return err;
319
320out_cleanup:
Amir Goldstein42f269b2017-01-17 06:34:54 +0200321 ovl_cleanup(wdir, temp);
David Howellsab79efa2015-09-18 11:45:22 +0100322 goto out2;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323}
324
325/*
326 * Copy up a single dentry
327 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100328 * All renames start with copy up of source if necessary. The actual
329 * rename will only proceed once the copy up was successful. Copy up uses
330 * upper parent i_mutex for exclusion. Since rename can change d_parent it
331 * is possible that the copy up will lock the old parent. At that point
332 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333 */
Amir Goldstein9aba6522016-11-12 21:36:03 +0200334static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
335 struct path *lowerpath, struct kstat *stat)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200336{
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200337 DEFINE_DELAYED_CALL(done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 struct dentry *workdir = ovl_workdir(dentry);
339 int err;
340 struct kstat pstat;
341 struct path parentpath;
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200342 struct dentry *lowerdentry = lowerpath->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 struct dentry *upperdir;
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200344 const char *link = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200345
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200346 if (WARN_ON(!workdir))
347 return -EROFS;
348
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200349 ovl_do_check_copy_up(lowerdentry);
David Howellsfb5bb2c32015-07-07 15:04:44 +0100350
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 ovl_path_upper(parent, &parentpath);
352 upperdir = parentpath.dentry;
353
354 err = vfs_getattr(&parentpath, &pstat);
355 if (err)
356 return err;
357
358 if (S_ISLNK(stat->mode)) {
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200359 link = vfs_get_link(lowerdentry, &done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360 if (IS_ERR(link))
361 return PTR_ERR(link);
362 }
363
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200364 err = -EIO;
365 if (lock_rename(workdir, upperdir) != NULL) {
366 pr_err("overlayfs: failed to lock workdir+upperdir\n");
367 goto out_unlock;
368 }
Miklos Szeredia6c60652016-12-16 11:02:56 +0100369 if (ovl_dentry_upper(dentry)) {
Al Viro0f7ff2d2015-12-06 12:31:07 -0500370 /* Raced with another copy-up? Nothing to do, then... */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371 err = 0;
Al Viro0f7ff2d2015-12-06 12:31:07 -0500372 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200373 }
374
375 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
Amir Goldstein42f269b2017-01-17 06:34:54 +0200376 stat, link, &pstat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377out_unlock:
378 unlock_rename(workdir, upperdir);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200379 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380
381 return err;
382}
383
Amir Goldstein9aba6522016-11-12 21:36:03 +0200384int ovl_copy_up_flags(struct dentry *dentry, int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400386 int err = 0;
387 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389 while (!err) {
390 struct dentry *next;
391 struct dentry *parent;
392 struct path lowerpath;
393 struct kstat stat;
394 enum ovl_path_type type = ovl_path_type(dentry);
395
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100396 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397 break;
398
399 next = dget(dentry);
400 /* find the topmost dentry not yet copied up */
401 for (;;) {
402 parent = dget_parent(next);
403
404 type = ovl_path_type(parent);
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100405 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 break;
407
408 dput(next);
409 next = parent;
410 }
411
412 ovl_path_lower(next, &lowerpath);
413 err = vfs_getattr(&lowerpath, &stat);
Amir Goldstein9aba6522016-11-12 21:36:03 +0200414 /* maybe truncate regular file. this has no effect on dirs */
415 if (flags & O_TRUNC)
416 stat.size = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417 if (!err)
Al Viro0f7ff2d2015-12-06 12:31:07 -0500418 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200419
420 dput(parent);
421 dput(next);
422 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400423 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424
425 return err;
426}
Amir Goldstein9aba6522016-11-12 21:36:03 +0200427
428int ovl_copy_up(struct dentry *dentry)
429{
430 return ovl_copy_up_flags(dentry, 0);
431}