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); | |
e9be9d5e MS |
60 | |
61 | if (!old->d_inode->i_op->getxattr || | |
62 | !new->d_inode->i_op->getxattr) | |
63 | return 0; | |
64 | ||
65 | list_size = vfs_listxattr(old, NULL, 0); | |
66 | if (list_size <= 0) { | |
67 | if (list_size == -EOPNOTSUPP) | |
68 | return 0; | |
69 | return list_size; | |
70 | } | |
71 | ||
72 | buf = kzalloc(list_size, GFP_KERNEL); | |
73 | if (!buf) | |
74 | return -ENOMEM; | |
75 | ||
e9be9d5e MS |
76 | list_size = vfs_listxattr(old, buf, list_size); |
77 | if (list_size <= 0) { | |
78 | error = list_size; | |
e4ad29fa | 79 | goto out; |
e9be9d5e MS |
80 | } |
81 | ||
82 | for (name = buf; name < (buf + list_size); name += strlen(name) + 1) { | |
0956254a MS |
83 | if (ovl_is_private_xattr(name)) |
84 | continue; | |
e4ad29fa VC |
85 | retry: |
86 | size = vfs_getxattr(old, name, value, value_size); | |
87 | if (size == -ERANGE) | |
88 | size = vfs_getxattr(old, name, NULL, 0); | |
89 | ||
97daf8b9 | 90 | if (size < 0) { |
e9be9d5e | 91 | error = size; |
e4ad29fa | 92 | break; |
e9be9d5e | 93 | } |
e4ad29fa VC |
94 | |
95 | if (size > value_size) { | |
96 | void *new; | |
97 | ||
98 | new = krealloc(value, size, GFP_KERNEL); | |
99 | if (!new) { | |
100 | error = -ENOMEM; | |
101 | break; | |
102 | } | |
103 | value = new; | |
104 | value_size = size; | |
105 | goto retry; | |
106 | } | |
107 | ||
e9be9d5e MS |
108 | error = vfs_setxattr(new, name, value, size, 0); |
109 | if (error) | |
e4ad29fa | 110 | break; |
e9be9d5e | 111 | } |
e9be9d5e MS |
112 | kfree(value); |
113 | out: | |
114 | kfree(buf); | |
115 | return error; | |
116 | } | |
117 | ||
118 | static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) | |
119 | { | |
120 | struct file *old_file; | |
121 | struct file *new_file; | |
122 | loff_t old_pos = 0; | |
123 | loff_t new_pos = 0; | |
124 | int error = 0; | |
125 | ||
126 | if (len == 0) | |
127 | return 0; | |
128 | ||
0480334f | 129 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
e9be9d5e MS |
130 | if (IS_ERR(old_file)) |
131 | return PTR_ERR(old_file); | |
132 | ||
0480334f | 133 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
e9be9d5e MS |
134 | if (IS_ERR(new_file)) { |
135 | error = PTR_ERR(new_file); | |
136 | goto out_fput; | |
137 | } | |
138 | ||
139 | /* FIXME: copy up sparse files efficiently */ | |
140 | while (len) { | |
141 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; | |
142 | long bytes; | |
143 | ||
144 | if (len < this_len) | |
145 | this_len = len; | |
146 | ||
147 | if (signal_pending_state(TASK_KILLABLE, current)) { | |
148 | error = -EINTR; | |
149 | break; | |
150 | } | |
151 | ||
152 | bytes = do_splice_direct(old_file, &old_pos, | |
153 | new_file, &new_pos, | |
154 | this_len, SPLICE_F_MOVE); | |
155 | if (bytes <= 0) { | |
156 | error = bytes; | |
157 | break; | |
158 | } | |
159 | WARN_ON(old_pos != new_pos); | |
160 | ||
161 | len -= bytes; | |
162 | } | |
163 | ||
164 | fput(new_file); | |
165 | out_fput: | |
166 | fput(old_file); | |
167 | return error; | |
168 | } | |
169 | ||
170 | static char *ovl_read_symlink(struct dentry *realdentry) | |
171 | { | |
172 | int res; | |
173 | char *buf; | |
174 | struct inode *inode = realdentry->d_inode; | |
175 | mm_segment_t old_fs; | |
176 | ||
177 | res = -EINVAL; | |
178 | if (!inode->i_op->readlink) | |
179 | goto err; | |
180 | ||
181 | res = -ENOMEM; | |
182 | buf = (char *) __get_free_page(GFP_KERNEL); | |
183 | if (!buf) | |
184 | goto err; | |
185 | ||
186 | old_fs = get_fs(); | |
187 | set_fs(get_ds()); | |
188 | /* The cast to a user pointer is valid due to the set_fs() */ | |
189 | res = inode->i_op->readlink(realdentry, | |
190 | (char __user *)buf, PAGE_SIZE - 1); | |
191 | set_fs(old_fs); | |
192 | if (res < 0) { | |
193 | free_page((unsigned long) buf); | |
194 | goto err; | |
195 | } | |
196 | buf[res] = '\0'; | |
197 | ||
198 | return buf; | |
199 | ||
200 | err: | |
201 | return ERR_PTR(res); | |
202 | } | |
203 | ||
204 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) | |
205 | { | |
206 | struct iattr attr = { | |
207 | .ia_valid = | |
208 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, | |
209 | .ia_atime = stat->atime, | |
210 | .ia_mtime = stat->mtime, | |
211 | }; | |
212 | ||
213 | return notify_change(upperdentry, &attr, NULL); | |
214 | } | |
215 | ||
216 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) | |
217 | { | |
218 | int err = 0; | |
219 | ||
220 | if (!S_ISLNK(stat->mode)) { | |
221 | struct iattr attr = { | |
222 | .ia_valid = ATTR_MODE, | |
223 | .ia_mode = stat->mode, | |
224 | }; | |
225 | err = notify_change(upperdentry, &attr, NULL); | |
226 | } | |
227 | if (!err) { | |
228 | struct iattr attr = { | |
229 | .ia_valid = ATTR_UID | ATTR_GID, | |
230 | .ia_uid = stat->uid, | |
231 | .ia_gid = stat->gid, | |
232 | }; | |
233 | err = notify_change(upperdentry, &attr, NULL); | |
234 | } | |
235 | if (!err) | |
236 | ovl_set_timestamps(upperdentry, stat); | |
237 | ||
238 | return err; | |
e9be9d5e MS |
239 | } |
240 | ||
241 | static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, | |
242 | struct dentry *dentry, struct path *lowerpath, | |
0f7ff2da | 243 | struct kstat *stat, const char *link) |
e9be9d5e MS |
244 | { |
245 | struct inode *wdir = workdir->d_inode; | |
246 | struct inode *udir = upperdir->d_inode; | |
247 | struct dentry *newdentry = NULL; | |
248 | struct dentry *upper = NULL; | |
249 | umode_t mode = stat->mode; | |
250 | int err; | |
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 | ||
263 | /* Can't properly set mode on creation because of the umask */ | |
264 | stat->mode &= S_IFMT; | |
265 | err = ovl_create_real(wdir, newdentry, stat, link, NULL, true); | |
266 | stat->mode = mode; | |
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 MS |
334 | { |
335 | struct dentry *workdir = ovl_workdir(dentry); | |
336 | int err; | |
337 | struct kstat pstat; | |
338 | struct path parentpath; | |
339 | struct dentry *upperdir; | |
340 | struct dentry *upperdentry; | |
341 | const struct cred *old_cred; | |
e9be9d5e MS |
342 | char *link = NULL; |
343 | ||
cc6f67bc MS |
344 | if (WARN_ON(!workdir)) |
345 | return -EROFS; | |
346 | ||
fb5bb2c3 DH |
347 | ovl_do_check_copy_up(lowerpath->dentry); |
348 | ||
e9be9d5e MS |
349 | ovl_path_upper(parent, &parentpath); |
350 | upperdir = parentpath.dentry; | |
351 | ||
352 | err = vfs_getattr(&parentpath, &pstat); | |
353 | if (err) | |
354 | return err; | |
355 | ||
356 | if (S_ISLNK(stat->mode)) { | |
357 | link = ovl_read_symlink(lowerpath->dentry); | |
358 | if (IS_ERR(link)) | |
359 | return PTR_ERR(link); | |
360 | } | |
361 | ||
3fe6e52f | 362 | old_cred = ovl_override_creds(dentry->d_sb); |
e9be9d5e MS |
363 | |
364 | err = -EIO; | |
365 | if (lock_rename(workdir, upperdir) != NULL) { | |
366 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); | |
367 | goto out_unlock; | |
368 | } | |
369 | upperdentry = ovl_dentry_upper(dentry); | |
370 | if (upperdentry) { | |
0f7ff2da | 371 | /* Raced with another copy-up? Nothing to do, then... */ |
e9be9d5e | 372 | err = 0; |
0f7ff2da | 373 | goto out_unlock; |
e9be9d5e MS |
374 | } |
375 | ||
376 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, | |
0f7ff2da | 377 | stat, link); |
e9be9d5e MS |
378 | if (!err) { |
379 | /* Restore timestamps on parent (best effort) */ | |
380 | ovl_set_timestamps(upperdir, &pstat); | |
381 | } | |
382 | out_unlock: | |
383 | unlock_rename(workdir, upperdir); | |
e9be9d5e | 384 | revert_creds(old_cred); |
e9be9d5e | 385 | |
e9be9d5e MS |
386 | if (link) |
387 | free_page((unsigned long) link); | |
388 | ||
389 | return err; | |
390 | } | |
391 | ||
392 | int ovl_copy_up(struct dentry *dentry) | |
393 | { | |
394 | int err; | |
395 | ||
396 | err = 0; | |
397 | while (!err) { | |
398 | struct dentry *next; | |
399 | struct dentry *parent; | |
400 | struct path lowerpath; | |
401 | struct kstat stat; | |
402 | enum ovl_path_type type = ovl_path_type(dentry); | |
403 | ||
1afaba1e | 404 | if (OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
405 | break; |
406 | ||
407 | next = dget(dentry); | |
408 | /* find the topmost dentry not yet copied up */ | |
409 | for (;;) { | |
410 | parent = dget_parent(next); | |
411 | ||
412 | type = ovl_path_type(parent); | |
1afaba1e | 413 | if (OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
414 | break; |
415 | ||
416 | dput(next); | |
417 | next = parent; | |
418 | } | |
419 | ||
420 | ovl_path_lower(next, &lowerpath); | |
421 | err = vfs_getattr(&lowerpath, &stat); | |
422 | if (!err) | |
0f7ff2da | 423 | err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
e9be9d5e MS |
424 | |
425 | dput(parent); | |
426 | dput(next); | |
427 | } | |
428 | ||
429 | return err; | |
430 | } |