]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/overlayfs/inode.c
ovl: hash overlay non-dir inodes by copy up origin
[mirror_ubuntu-bionic-kernel.git] / fs / overlayfs / inode.c
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
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/cred.h>
13 #include <linux/xattr.h>
14 #include <linux/posix_acl.h>
15 #include "overlayfs.h"
16
17 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
18 {
19 int err;
20 struct dentry *upperdentry;
21 const struct cred *old_cred;
22
23 /*
24 * Check for permissions before trying to copy-up. This is redundant
25 * since it will be rechecked later by ->setattr() on upper dentry. But
26 * without this, copy-up can be triggered by just about anybody.
27 *
28 * We don't initialize inode->size, which just means that
29 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
30 * check for a swapfile (which this won't be anyway).
31 */
32 err = setattr_prepare(dentry, attr);
33 if (err)
34 return err;
35
36 err = ovl_want_write(dentry);
37 if (err)
38 goto out;
39
40 err = ovl_copy_up(dentry);
41 if (!err) {
42 upperdentry = ovl_dentry_upper(dentry);
43
44 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
45 attr->ia_valid &= ~ATTR_MODE;
46
47 inode_lock(upperdentry->d_inode);
48 old_cred = ovl_override_creds(dentry->d_sb);
49 err = notify_change(upperdentry, attr, NULL);
50 revert_creds(old_cred);
51 if (!err)
52 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
53 inode_unlock(upperdentry->d_inode);
54 }
55 ovl_drop_write(dentry);
56 out:
57 return err;
58 }
59
60 int ovl_getattr(const struct path *path, struct kstat *stat,
61 u32 request_mask, unsigned int flags)
62 {
63 struct dentry *dentry = path->dentry;
64 enum ovl_path_type type;
65 struct path realpath;
66 const struct cred *old_cred;
67 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
68 int err;
69
70 type = ovl_path_real(dentry, &realpath);
71 old_cred = ovl_override_creds(dentry->d_sb);
72 err = vfs_getattr(&realpath, stat, request_mask, flags);
73 if (err)
74 goto out;
75
76 /*
77 * When all layers are on the same fs, all real inode number are
78 * unique, so we use the overlay st_dev, which is friendly to du -x.
79 *
80 * We also use st_ino of the copy up origin, if we know it.
81 * This guaranties constant st_dev/st_ino across copy up.
82 *
83 * If filesystem supports NFS export ops, this also guaranties
84 * persistent st_ino across mount cycle.
85 */
86 if (ovl_same_sb(dentry->d_sb)) {
87 if (OVL_TYPE_ORIGIN(type)) {
88 struct kstat lowerstat;
89 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
90
91 ovl_path_lower(dentry, &realpath);
92 err = vfs_getattr(&realpath, &lowerstat,
93 lowermask, flags);
94 if (err)
95 goto out;
96
97 WARN_ON_ONCE(stat->dev != lowerstat.dev);
98 /*
99 * Lower hardlinks may be broken on copy up to different
100 * upper files, so we cannot use the lower origin st_ino
101 * for those different files, even for the same fs case.
102 * With inodes index enabled, it is safe to use st_ino
103 * of an indexed hardlinked origin. The index validates
104 * that the upper hardlink is not broken.
105 */
106 if (is_dir || lowerstat.nlink == 1 ||
107 ovl_test_flag(OVL_INDEX, d_inode(dentry)))
108 stat->ino = lowerstat.ino;
109 }
110 stat->dev = dentry->d_sb->s_dev;
111 } else if (is_dir) {
112 /*
113 * If not all layers are on the same fs the pair {real st_ino;
114 * overlay st_dev} is not unique, so use the non persistent
115 * overlay st_ino.
116 *
117 * Always use the overlay st_dev for directories, so 'find
118 * -xdev' will scan the entire overlay mount and won't cross the
119 * overlay mount boundaries.
120 */
121 stat->dev = dentry->d_sb->s_dev;
122 stat->ino = dentry->d_inode->i_ino;
123 }
124
125 /*
126 * It's probably not worth it to count subdirs to get the
127 * correct link count. nlink=1 seems to pacify 'find' and
128 * other utilities.
129 */
130 if (is_dir && OVL_TYPE_MERGE(type))
131 stat->nlink = 1;
132
133 out:
134 revert_creds(old_cred);
135
136 return err;
137 }
138
139 int ovl_permission(struct inode *inode, int mask)
140 {
141 struct inode *upperinode = ovl_inode_upper(inode);
142 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
143 const struct cred *old_cred;
144 int err;
145
146 /* Careful in RCU walk mode */
147 if (!realinode) {
148 WARN_ON(!(mask & MAY_NOT_BLOCK));
149 return -ECHILD;
150 }
151
152 /*
153 * Check overlay inode with the creds of task and underlying inode
154 * with creds of mounter
155 */
156 err = generic_permission(inode, mask);
157 if (err)
158 return err;
159
160 old_cred = ovl_override_creds(inode->i_sb);
161 if (!upperinode &&
162 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
163 mask &= ~(MAY_WRITE | MAY_APPEND);
164 /* Make sure mounter can read file for copy up later */
165 mask |= MAY_READ;
166 }
167 err = inode_permission(realinode, mask);
168 revert_creds(old_cred);
169
170 return err;
171 }
172
173 static const char *ovl_get_link(struct dentry *dentry,
174 struct inode *inode,
175 struct delayed_call *done)
176 {
177 const struct cred *old_cred;
178 const char *p;
179
180 if (!dentry)
181 return ERR_PTR(-ECHILD);
182
183 old_cred = ovl_override_creds(dentry->d_sb);
184 p = vfs_get_link(ovl_dentry_real(dentry), done);
185 revert_creds(old_cred);
186 return p;
187 }
188
189 bool ovl_is_private_xattr(const char *name)
190 {
191 return strncmp(name, OVL_XATTR_PREFIX,
192 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
193 }
194
195 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
196 size_t size, int flags)
197 {
198 int err;
199 struct path realpath;
200 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
201 const struct cred *old_cred;
202
203 err = ovl_want_write(dentry);
204 if (err)
205 goto out;
206
207 if (!value && !OVL_TYPE_UPPER(type)) {
208 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
209 if (err < 0)
210 goto out_drop_write;
211 }
212
213 err = ovl_copy_up(dentry);
214 if (err)
215 goto out_drop_write;
216
217 if (!OVL_TYPE_UPPER(type))
218 ovl_path_upper(dentry, &realpath);
219
220 old_cred = ovl_override_creds(dentry->d_sb);
221 if (value)
222 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
223 else {
224 WARN_ON(flags != XATTR_REPLACE);
225 err = vfs_removexattr(realpath.dentry, name);
226 }
227 revert_creds(old_cred);
228
229 out_drop_write:
230 ovl_drop_write(dentry);
231 out:
232 return err;
233 }
234
235 int ovl_xattr_get(struct dentry *dentry, const char *name,
236 void *value, size_t size)
237 {
238 struct dentry *realdentry = ovl_dentry_real(dentry);
239 ssize_t res;
240 const struct cred *old_cred;
241
242 old_cred = ovl_override_creds(dentry->d_sb);
243 res = vfs_getxattr(realdentry, name, value, size);
244 revert_creds(old_cred);
245 return res;
246 }
247
248 static bool ovl_can_list(const char *s)
249 {
250 /* List all non-trusted xatts */
251 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
252 return true;
253
254 /* Never list trusted.overlay, list other trusted for superuser only */
255 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
256 }
257
258 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
259 {
260 struct dentry *realdentry = ovl_dentry_real(dentry);
261 ssize_t res;
262 size_t len;
263 char *s;
264 const struct cred *old_cred;
265
266 old_cred = ovl_override_creds(dentry->d_sb);
267 res = vfs_listxattr(realdentry, list, size);
268 revert_creds(old_cred);
269 if (res <= 0 || size == 0)
270 return res;
271
272 /* filter out private xattrs */
273 for (s = list, len = res; len;) {
274 size_t slen = strnlen(s, len) + 1;
275
276 /* underlying fs providing us with an broken xattr list? */
277 if (WARN_ON(slen > len))
278 return -EIO;
279
280 len -= slen;
281 if (!ovl_can_list(s)) {
282 res -= slen;
283 memmove(s, s + slen, len);
284 } else {
285 s += slen;
286 }
287 }
288
289 return res;
290 }
291
292 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
293 {
294 struct inode *realinode = ovl_inode_real(inode);
295 const struct cred *old_cred;
296 struct posix_acl *acl;
297
298 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
299 return NULL;
300
301 old_cred = ovl_override_creds(inode->i_sb);
302 acl = get_acl(realinode, type);
303 revert_creds(old_cred);
304
305 return acl;
306 }
307
308 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
309 struct dentry *realdentry)
310 {
311 if (OVL_TYPE_UPPER(type))
312 return false;
313
314 if (special_file(realdentry->d_inode->i_mode))
315 return false;
316
317 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
318 return false;
319
320 return true;
321 }
322
323 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
324 {
325 int err = 0;
326 struct path realpath;
327 enum ovl_path_type type;
328
329 type = ovl_path_real(dentry, &realpath);
330 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
331 err = ovl_want_write(dentry);
332 if (!err) {
333 err = ovl_copy_up_flags(dentry, file_flags);
334 ovl_drop_write(dentry);
335 }
336 }
337
338 return err;
339 }
340
341 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
342 {
343 struct dentry *alias;
344 struct path upperpath;
345
346 if (!(flags & S_ATIME))
347 return 0;
348
349 alias = d_find_any_alias(inode);
350 if (!alias)
351 return 0;
352
353 ovl_path_upper(alias, &upperpath);
354 if (upperpath.dentry) {
355 touch_atime(&upperpath);
356 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
357 }
358
359 dput(alias);
360
361 return 0;
362 }
363
364 static const struct inode_operations ovl_file_inode_operations = {
365 .setattr = ovl_setattr,
366 .permission = ovl_permission,
367 .getattr = ovl_getattr,
368 .listxattr = ovl_listxattr,
369 .get_acl = ovl_get_acl,
370 .update_time = ovl_update_time,
371 };
372
373 static const struct inode_operations ovl_symlink_inode_operations = {
374 .setattr = ovl_setattr,
375 .get_link = ovl_get_link,
376 .getattr = ovl_getattr,
377 .listxattr = ovl_listxattr,
378 .update_time = ovl_update_time,
379 };
380
381 /*
382 * It is possible to stack overlayfs instance on top of another
383 * overlayfs instance as lower layer. We need to annonate the
384 * stackable i_mutex locks according to stack level of the super
385 * block instance. An overlayfs instance can never be in stack
386 * depth 0 (there is always a real fs below it). An overlayfs
387 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
388 *
389 * For example, here is a snip from /proc/lockdep_chains after
390 * dir_iterate of nested overlayfs:
391 *
392 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
393 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
394 * [...] &type->i_mutex_dir_key (stack_depth=0)
395 */
396 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
397
398 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
399 {
400 #ifdef CONFIG_LOCKDEP
401 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
402 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
403
404 int depth = inode->i_sb->s_stack_depth - 1;
405
406 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
407 depth = 0;
408
409 if (S_ISDIR(inode->i_mode))
410 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
411 else
412 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
413 #endif
414 }
415
416 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
417 {
418 inode->i_ino = get_next_ino();
419 inode->i_mode = mode;
420 inode->i_flags |= S_NOCMTIME;
421 #ifdef CONFIG_FS_POSIX_ACL
422 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
423 #endif
424
425 ovl_lockdep_annotate_inode_mutex_key(inode);
426
427 switch (mode & S_IFMT) {
428 case S_IFREG:
429 inode->i_op = &ovl_file_inode_operations;
430 break;
431
432 case S_IFDIR:
433 inode->i_op = &ovl_dir_inode_operations;
434 inode->i_fop = &ovl_dir_operations;
435 break;
436
437 case S_IFLNK:
438 inode->i_op = &ovl_symlink_inode_operations;
439 break;
440
441 default:
442 inode->i_op = &ovl_file_inode_operations;
443 init_special_inode(inode, mode, rdev);
444 break;
445 }
446 }
447
448 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
449 {
450 struct inode *inode;
451
452 inode = new_inode(sb);
453 if (inode)
454 ovl_fill_inode(inode, mode, rdev);
455
456 return inode;
457 }
458
459 static int ovl_inode_test(struct inode *inode, void *data)
460 {
461 return inode->i_private == data;
462 }
463
464 static int ovl_inode_set(struct inode *inode, void *data)
465 {
466 inode->i_private = data;
467 return 0;
468 }
469
470 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
471 struct dentry *upperdentry)
472 {
473 struct inode *lowerinode = lowerdentry ? d_inode(lowerdentry) : NULL;
474
475 /* Lower (origin) inode must match, even if NULL */
476 if (ovl_inode_lower(inode) != lowerinode)
477 return false;
478
479 /*
480 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
481 * This happens when finding a lower alias for a copied up hard link.
482 */
483 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
484 return false;
485
486 return true;
487 }
488
489 struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry)
490 {
491 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
492 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
493 struct inode *inode;
494
495 if (!realinode)
496 realinode = d_inode(lowerdentry);
497
498 if (!S_ISDIR(realinode->i_mode) &&
499 (upperdentry || (lowerdentry && ovl_indexdir(dentry->d_sb)))) {
500 struct inode *key = d_inode(lowerdentry ?: upperdentry);
501
502 inode = iget5_locked(dentry->d_sb, (unsigned long) key,
503 ovl_inode_test, ovl_inode_set, key);
504 if (!inode)
505 goto out_nomem;
506 if (!(inode->i_state & I_NEW)) {
507 /*
508 * Verify that the underlying files stored in the inode
509 * match those in the dentry.
510 */
511 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
512 iput(inode);
513 inode = ERR_PTR(-ESTALE);
514 goto out;
515 }
516
517 dput(upperdentry);
518 goto out;
519 }
520
521 set_nlink(inode, realinode->i_nlink);
522 } else {
523 inode = new_inode(dentry->d_sb);
524 if (!inode)
525 goto out_nomem;
526 }
527 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
528 ovl_inode_init(inode, upperdentry, lowerdentry);
529
530 if (upperdentry && ovl_is_impuredir(upperdentry))
531 ovl_set_flag(OVL_IMPURE, inode);
532
533 if (inode->i_state & I_NEW)
534 unlock_new_inode(inode);
535 out:
536 return inode;
537
538 out_nomem:
539 inode = ERR_PTR(-ENOMEM);
540 goto out;
541 }