blob: a6f9ca621e0b5a0b1f6485c28eac2ddd4b593733 [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,
Al Viro0f7ff2d2015-12-06 12:31:07 -0500235 struct kstat *stat, const char *link)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236{
237 struct inode *wdir = workdir->d_inode;
238 struct inode *udir = upperdir->d_inode;
239 struct dentry *newdentry = NULL;
240 struct dentry *upper = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200241 int err;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400242 const struct cred *old_creds = NULL;
243 struct cred *new_creds = NULL;
Al Viro32a3d842016-12-04 17:33:17 +0000244 struct cattr cattr = {
245 /* Can't properly set mode on creation because of the umask */
246 .mode = stat->mode & S_IFMT,
247 .rdev = stat->rdev,
248 .link = link
249 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200250
251 newdentry = ovl_lookup_temp(workdir, dentry);
252 err = PTR_ERR(newdentry);
253 if (IS_ERR(newdentry))
254 goto out;
255
256 upper = lookup_one_len(dentry->d_name.name, upperdir,
257 dentry->d_name.len);
258 err = PTR_ERR(upper);
259 if (IS_ERR(upper))
260 goto out1;
261
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400262 err = security_inode_copy_up(dentry, &new_creds);
263 if (err < 0)
264 goto out2;
265
266 if (new_creds)
267 old_creds = override_creds(new_creds);
268
Al Viro32a3d842016-12-04 17:33:17 +0000269 err = ovl_create_real(wdir, newdentry, &cattr, NULL, true);
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400270
271 if (new_creds) {
272 revert_creds(old_creds);
273 put_cred(new_creds);
274 }
275
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276 if (err)
277 goto out2;
278
279 if (S_ISREG(stat->mode)) {
280 struct path upperpath;
Sohom Bhattacharjeef134f242016-03-15 20:57:59 +0530281
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 ovl_path_upper(dentry, &upperpath);
283 BUG_ON(upperpath.dentry != NULL);
284 upperpath.dentry = newdentry;
285
286 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
287 if (err)
288 goto out_cleanup;
289 }
290
291 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
292 if (err)
293 goto out_cleanup;
294
Al Viro59551022016-01-22 15:40:57 -0500295 inode_lock(newdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296 err = ovl_set_attr(newdentry, stat);
Al Viro59551022016-01-22 15:40:57 -0500297 inode_unlock(newdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298 if (err)
299 goto out_cleanup;
300
301 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
302 if (err)
303 goto out_cleanup;
304
305 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200306 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307 newdentry = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308out2:
309 dput(upper);
310out1:
311 dput(newdentry);
312out:
313 return err;
314
315out_cleanup:
316 ovl_cleanup(wdir, newdentry);
David Howellsab79efa2015-09-18 11:45:22 +0100317 goto out2;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318}
319
320/*
321 * Copy up a single dentry
322 *
Miklos Szeredia6c60652016-12-16 11:02:56 +0100323 * All renames start with copy up of source if necessary. The actual
324 * rename will only proceed once the copy up was successful. Copy up uses
325 * upper parent i_mutex for exclusion. Since rename can change d_parent it
326 * is possible that the copy up will lock the old parent. At that point
327 * the file will have already been copied up anyway.
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328 */
Amir Goldstein9aba6522016-11-12 21:36:03 +0200329static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
330 struct path *lowerpath, struct kstat *stat)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331{
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200332 DEFINE_DELAYED_CALL(done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333 struct dentry *workdir = ovl_workdir(dentry);
334 int err;
335 struct kstat pstat;
336 struct path parentpath;
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200337 struct dentry *lowerdentry = lowerpath->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 struct dentry *upperdir;
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200339 const char *link = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200340
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200341 if (WARN_ON(!workdir))
342 return -EROFS;
343
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200344 ovl_do_check_copy_up(lowerdentry);
David Howellsfb5bb2c32015-07-07 15:04:44 +0100345
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346 ovl_path_upper(parent, &parentpath);
347 upperdir = parentpath.dentry;
348
David Howellsa528d352017-01-31 16:46:22 +0000349 err = vfs_getattr(&parentpath, &pstat,
350 STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 if (err)
352 return err;
353
354 if (S_ISLNK(stat->mode)) {
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200355 link = vfs_get_link(lowerdentry, &done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200356 if (IS_ERR(link))
357 return PTR_ERR(link);
358 }
359
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360 err = -EIO;
361 if (lock_rename(workdir, upperdir) != NULL) {
362 pr_err("overlayfs: failed to lock workdir+upperdir\n");
363 goto out_unlock;
364 }
Miklos Szeredia6c60652016-12-16 11:02:56 +0100365 if (ovl_dentry_upper(dentry)) {
Al Viro0f7ff2d2015-12-06 12:31:07 -0500366 /* Raced with another copy-up? Nothing to do, then... */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367 err = 0;
Al Viro0f7ff2d2015-12-06 12:31:07 -0500368 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369 }
370
371 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
Al Viro0f7ff2d2015-12-06 12:31:07 -0500372 stat, link);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200373 if (!err) {
374 /* Restore timestamps on parent (best effort) */
375 ovl_set_timestamps(upperdir, &pstat);
376 }
377out_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);
David Howellsa528d352017-01-31 16:46:22 +0000413 err = vfs_getattr(&lowerpath, &stat,
414 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Amir Goldstein9aba6522016-11-12 21:36:03 +0200415 /* maybe truncate regular file. this has no effect on dirs */
416 if (flags & O_TRUNC)
417 stat.size = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200418 if (!err)
Al Viro0f7ff2d2015-12-06 12:31:07 -0500419 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420
421 dput(parent);
422 dput(next);
423 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400424 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425
426 return err;
427}
Amir Goldstein9aba6522016-11-12 21:36:03 +0200428
429int ovl_copy_up(struct dentry *dentry)
430{
431 return ovl_copy_up_flags(dentry, 0);
432}