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