Commit | Line | Data |
---|---|---|
e9be9d5e MS |
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 | ||
fb5bb2c3 | 10 | #include <linux/module.h> |
e9be9d5e MS |
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> | |
5b825c3a | 19 | #include <linux/cred.h> |
e9be9d5e | 20 | #include <linux/namei.h> |
fb5bb2c3 DH |
21 | #include <linux/fdtable.h> |
22 | #include <linux/ratelimit.h> | |
e9be9d5e MS |
23 | #include "overlayfs.h" |
24 | ||
25 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) | |
26 | ||
fb5bb2c3 DH |
27 | static bool __read_mostly ovl_check_copy_up; |
28 | module_param_named(check_copy_up, ovl_check_copy_up, bool, | |
29 | S_IWUSR | S_IRUGO); | |
30 | MODULE_PARM_DESC(ovl_check_copy_up, | |
31 | "Warn on copy-up when causing process also has a R/O fd open"); | |
32 | ||
33 | static int ovl_check_fd(const void *data, struct file *f, unsigned int fd) | |
34 | { | |
35 | const struct dentry *dentry = data; | |
36 | ||
45063097 | 37 | if (file_inode(f) == d_inode(dentry)) |
fb5bb2c3 DH |
38 | 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", |
39 | f, fd, current->pid, current->comm); | |
40 | return 0; | |
41 | } | |
42 | ||
43 | /* | |
44 | * Check the fds open by this process and warn if something like the following | |
45 | * scenario is about to occur: | |
46 | * | |
47 | * fd1 = open("foo", O_RDONLY); | |
48 | * fd2 = open("foo", O_RDWR); | |
49 | */ | |
50 | static void ovl_do_check_copy_up(struct dentry *dentry) | |
51 | { | |
52 | if (ovl_check_copy_up) | |
53 | iterate_fd(current->files, 0, ovl_check_fd, dentry); | |
54 | } | |
55 | ||
e9be9d5e MS |
56 | int ovl_copy_xattr(struct dentry *old, struct dentry *new) |
57 | { | |
e4ad29fa VC |
58 | ssize_t list_size, size, value_size = 0; |
59 | char *buf, *name, *value = NULL; | |
60 | int uninitialized_var(error); | |
8b326c61 | 61 | size_t slen; |
e9be9d5e | 62 | |
5d6c3191 AG |
63 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
64 | !(new->d_inode->i_opflags & IOP_XATTR)) | |
e9be9d5e MS |
65 | return 0; |
66 | ||
67 | list_size = vfs_listxattr(old, NULL, 0); | |
68 | if (list_size <= 0) { | |
69 | if (list_size == -EOPNOTSUPP) | |
70 | return 0; | |
71 | return list_size; | |
72 | } | |
73 | ||
74 | buf = kzalloc(list_size, GFP_KERNEL); | |
75 | if (!buf) | |
76 | return -ENOMEM; | |
77 | ||
e9be9d5e MS |
78 | list_size = vfs_listxattr(old, buf, list_size); |
79 | if (list_size <= 0) { | |
80 | error = list_size; | |
e4ad29fa | 81 | goto out; |
e9be9d5e MS |
82 | } |
83 | ||
8b326c61 MS |
84 | for (name = buf; list_size; name += slen) { |
85 | slen = strnlen(name, list_size) + 1; | |
86 | ||
87 | /* underlying fs providing us with an broken xattr list? */ | |
88 | if (WARN_ON(slen > list_size)) { | |
89 | error = -EIO; | |
90 | break; | |
91 | } | |
92 | list_size -= slen; | |
93 | ||
0956254a MS |
94 | if (ovl_is_private_xattr(name)) |
95 | continue; | |
e4ad29fa VC |
96 | retry: |
97 | size = vfs_getxattr(old, name, value, value_size); | |
98 | if (size == -ERANGE) | |
99 | size = vfs_getxattr(old, name, NULL, 0); | |
100 | ||
97daf8b9 | 101 | if (size < 0) { |
e9be9d5e | 102 | error = size; |
e4ad29fa | 103 | break; |
e9be9d5e | 104 | } |
e4ad29fa VC |
105 | |
106 | if (size > value_size) { | |
107 | void *new; | |
108 | ||
109 | new = krealloc(value, size, GFP_KERNEL); | |
110 | if (!new) { | |
111 | error = -ENOMEM; | |
112 | break; | |
113 | } | |
114 | value = new; | |
115 | value_size = size; | |
116 | goto retry; | |
117 | } | |
118 | ||
121ab822 VG |
119 | error = security_inode_copy_up_xattr(name); |
120 | if (error < 0 && error != -EOPNOTSUPP) | |
121 | break; | |
122 | if (error == 1) { | |
123 | error = 0; | |
124 | continue; /* Discard */ | |
125 | } | |
e9be9d5e MS |
126 | error = vfs_setxattr(new, name, value, size, 0); |
127 | if (error) | |
e4ad29fa | 128 | break; |
e9be9d5e | 129 | } |
e9be9d5e MS |
130 | kfree(value); |
131 | out: | |
132 | kfree(buf); | |
133 | return error; | |
134 | } | |
135 | ||
136 | static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) | |
137 | { | |
138 | struct file *old_file; | |
139 | struct file *new_file; | |
140 | loff_t old_pos = 0; | |
141 | loff_t new_pos = 0; | |
142 | int error = 0; | |
143 | ||
144 | if (len == 0) | |
145 | return 0; | |
146 | ||
0480334f | 147 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
e9be9d5e MS |
148 | if (IS_ERR(old_file)) |
149 | return PTR_ERR(old_file); | |
150 | ||
0480334f | 151 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
e9be9d5e MS |
152 | if (IS_ERR(new_file)) { |
153 | error = PTR_ERR(new_file); | |
154 | goto out_fput; | |
155 | } | |
156 | ||
2ea98466 AG |
157 | /* Try to use clone_file_range to clone up within the same fs */ |
158 | error = vfs_clone_file_range(old_file, 0, new_file, 0, len); | |
159 | if (!error) | |
160 | goto out; | |
161 | /* Couldn't clone, so now we try to copy the data */ | |
162 | error = 0; | |
163 | ||
e9be9d5e MS |
164 | /* FIXME: copy up sparse files efficiently */ |
165 | while (len) { | |
166 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; | |
167 | long bytes; | |
168 | ||
169 | if (len < this_len) | |
170 | this_len = len; | |
171 | ||
172 | if (signal_pending_state(TASK_KILLABLE, current)) { | |
173 | error = -EINTR; | |
174 | break; | |
175 | } | |
176 | ||
177 | bytes = do_splice_direct(old_file, &old_pos, | |
178 | new_file, &new_pos, | |
179 | this_len, SPLICE_F_MOVE); | |
180 | if (bytes <= 0) { | |
181 | error = bytes; | |
182 | break; | |
183 | } | |
184 | WARN_ON(old_pos != new_pos); | |
185 | ||
186 | len -= bytes; | |
187 | } | |
2ea98466 | 188 | out: |
641089c1 MS |
189 | if (!error) |
190 | error = vfs_fsync(new_file, 0); | |
e9be9d5e MS |
191 | fput(new_file); |
192 | out_fput: | |
193 | fput(old_file); | |
194 | return error; | |
195 | } | |
196 | ||
e9be9d5e MS |
197 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
198 | { | |
199 | struct iattr attr = { | |
200 | .ia_valid = | |
201 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, | |
202 | .ia_atime = stat->atime, | |
203 | .ia_mtime = stat->mtime, | |
204 | }; | |
205 | ||
206 | return notify_change(upperdentry, &attr, NULL); | |
207 | } | |
208 | ||
209 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) | |
210 | { | |
211 | int err = 0; | |
212 | ||
213 | if (!S_ISLNK(stat->mode)) { | |
214 | struct iattr attr = { | |
215 | .ia_valid = ATTR_MODE, | |
216 | .ia_mode = stat->mode, | |
217 | }; | |
218 | err = notify_change(upperdentry, &attr, NULL); | |
219 | } | |
220 | if (!err) { | |
221 | struct iattr attr = { | |
222 | .ia_valid = ATTR_UID | ATTR_GID, | |
223 | .ia_uid = stat->uid, | |
224 | .ia_gid = stat->gid, | |
225 | }; | |
226 | err = notify_change(upperdentry, &attr, NULL); | |
227 | } | |
228 | if (!err) | |
229 | ovl_set_timestamps(upperdentry, stat); | |
230 | ||
231 | return err; | |
e9be9d5e MS |
232 | } |
233 | ||
234 | static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, | |
235 | struct dentry *dentry, struct path *lowerpath, | |
0f7ff2da | 236 | struct kstat *stat, const char *link) |
e9be9d5e MS |
237 | { |
238 | struct inode *wdir = workdir->d_inode; | |
239 | struct inode *udir = upperdir->d_inode; | |
240 | struct dentry *newdentry = NULL; | |
241 | struct dentry *upper = NULL; | |
e9be9d5e | 242 | int err; |
d8ad8b49 VG |
243 | const struct cred *old_creds = NULL; |
244 | struct cred *new_creds = NULL; | |
32a3d848 AV |
245 | struct cattr cattr = { |
246 | /* Can't properly set mode on creation because of the umask */ | |
247 | .mode = stat->mode & S_IFMT, | |
248 | .rdev = stat->rdev, | |
249 | .link = link | |
250 | }; | |
e9be9d5e MS |
251 | |
252 | newdentry = ovl_lookup_temp(workdir, dentry); | |
253 | err = PTR_ERR(newdentry); | |
254 | if (IS_ERR(newdentry)) | |
255 | goto out; | |
256 | ||
257 | upper = lookup_one_len(dentry->d_name.name, upperdir, | |
258 | dentry->d_name.len); | |
259 | err = PTR_ERR(upper); | |
260 | if (IS_ERR(upper)) | |
261 | goto out1; | |
262 | ||
d8ad8b49 VG |
263 | err = security_inode_copy_up(dentry, &new_creds); |
264 | if (err < 0) | |
265 | goto out2; | |
266 | ||
267 | if (new_creds) | |
268 | old_creds = override_creds(new_creds); | |
269 | ||
32a3d848 | 270 | err = ovl_create_real(wdir, newdentry, &cattr, NULL, true); |
d8ad8b49 VG |
271 | |
272 | if (new_creds) { | |
273 | revert_creds(old_creds); | |
274 | put_cred(new_creds); | |
275 | } | |
276 | ||
e9be9d5e MS |
277 | if (err) |
278 | goto out2; | |
279 | ||
280 | if (S_ISREG(stat->mode)) { | |
281 | struct path upperpath; | |
f134f244 | 282 | |
e9be9d5e MS |
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 | ||
5955102c | 296 | inode_lock(newdentry->d_inode); |
e9be9d5e | 297 | err = ovl_set_attr(newdentry, stat); |
5955102c | 298 | inode_unlock(newdentry->d_inode); |
e9be9d5e MS |
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); | |
39b681f8 | 307 | ovl_inode_update(d_inode(dentry), d_inode(newdentry)); |
e9be9d5e | 308 | newdentry = NULL; |
e9be9d5e MS |
309 | out2: |
310 | dput(upper); | |
311 | out1: | |
312 | dput(newdentry); | |
313 | out: | |
314 | return err; | |
315 | ||
316 | out_cleanup: | |
317 | ovl_cleanup(wdir, newdentry); | |
ab79efab | 318 | goto out2; |
e9be9d5e MS |
319 | } |
320 | ||
321 | /* | |
322 | * Copy up a single dentry | |
323 | * | |
a6c60655 MS |
324 | * All renames start with copy up of source if necessary. The actual |
325 | * rename will only proceed once the copy up was successful. Copy up uses | |
326 | * upper parent i_mutex for exclusion. Since rename can change d_parent it | |
327 | * is possible that the copy up will lock the old parent. At that point | |
328 | * the file will have already been copied up anyway. | |
e9be9d5e | 329 | */ |
9aba6521 AG |
330 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
331 | struct path *lowerpath, struct kstat *stat) | |
e9be9d5e | 332 | { |
7764235b | 333 | DEFINE_DELAYED_CALL(done); |
e9be9d5e MS |
334 | struct dentry *workdir = ovl_workdir(dentry); |
335 | int err; | |
336 | struct kstat pstat; | |
337 | struct path parentpath; | |
7764235b | 338 | struct dentry *lowerdentry = lowerpath->dentry; |
e9be9d5e | 339 | struct dentry *upperdir; |
7764235b | 340 | const char *link = NULL; |
e9be9d5e | 341 | |
cc6f67bc MS |
342 | if (WARN_ON(!workdir)) |
343 | return -EROFS; | |
344 | ||
7764235b | 345 | ovl_do_check_copy_up(lowerdentry); |
fb5bb2c3 | 346 | |
e9be9d5e MS |
347 | ovl_path_upper(parent, &parentpath); |
348 | upperdir = parentpath.dentry; | |
349 | ||
350 | err = vfs_getattr(&parentpath, &pstat); | |
351 | if (err) | |
352 | return err; | |
353 | ||
354 | if (S_ISLNK(stat->mode)) { | |
7764235b | 355 | link = vfs_get_link(lowerdentry, &done); |
e9be9d5e MS |
356 | if (IS_ERR(link)) |
357 | return PTR_ERR(link); | |
358 | } | |
359 | ||
e9be9d5e MS |
360 | err = -EIO; |
361 | if (lock_rename(workdir, upperdir) != NULL) { | |
362 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); | |
363 | goto out_unlock; | |
364 | } | |
a6c60655 | 365 | if (ovl_dentry_upper(dentry)) { |
0f7ff2da | 366 | /* Raced with another copy-up? Nothing to do, then... */ |
e9be9d5e | 367 | err = 0; |
0f7ff2da | 368 | goto out_unlock; |
e9be9d5e MS |
369 | } |
370 | ||
371 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, | |
0f7ff2da | 372 | stat, link); |
e9be9d5e MS |
373 | if (!err) { |
374 | /* Restore timestamps on parent (best effort) */ | |
375 | ovl_set_timestamps(upperdir, &pstat); | |
376 | } | |
377 | out_unlock: | |
378 | unlock_rename(workdir, upperdir); | |
7764235b | 379 | do_delayed_call(&done); |
e9be9d5e MS |
380 | |
381 | return err; | |
382 | } | |
383 | ||
9aba6521 | 384 | int ovl_copy_up_flags(struct dentry *dentry, int flags) |
e9be9d5e | 385 | { |
8eac98b8 VG |
386 | int err = 0; |
387 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); | |
e9be9d5e | 388 | |
e9be9d5e MS |
389 | 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 | ||
1afaba1e | 396 | if (OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
397 | 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); | |
1afaba1e | 405 | if (OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
406 | break; |
407 | ||
408 | dput(next); | |
409 | next = parent; | |
410 | } | |
411 | ||
412 | ovl_path_lower(next, &lowerpath); | |
413 | err = vfs_getattr(&lowerpath, &stat); | |
9aba6521 AG |
414 | /* maybe truncate regular file. this has no effect on dirs */ |
415 | if (flags & O_TRUNC) | |
416 | stat.size = 0; | |
e9be9d5e | 417 | if (!err) |
0f7ff2da | 418 | err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
e9be9d5e MS |
419 | |
420 | dput(parent); | |
421 | dput(next); | |
422 | } | |
8eac98b8 | 423 | revert_creds(old_cred); |
e9be9d5e MS |
424 | |
425 | return err; | |
426 | } | |
9aba6521 AG |
427 | |
428 | int ovl_copy_up(struct dentry *dentry) | |
429 | { | |
430 | return ovl_copy_up_flags(dentry, 0); | |
431 | } |