1
2
3
4
5
6
7
8
9
10#include <linux/fs.h>
11#include <linux/slab.h>
12#include <linux/xattr.h>
13#include <linux/cred.h>
14#include <linux/posix_acl.h>
15#include <linux/ratelimit.h>
16#include "overlayfs.h"
17#include "ovl_entry.h"
18
19int ovl_setattr(struct dentry *dentry, struct iattr *attr)
20{
21 int err;
22 struct dentry *upperdentry;
23 const struct cred *old_cred;
24
25
26
27
28
29
30
31
32
33
34 err = inode_change_ok(dentry->d_inode, attr);
35 if (err)
36 return err;
37
38 err = ovl_want_write(dentry);
39 if (err)
40 goto out;
41
42 err = ovl_copy_up(dentry);
43 if (!err) {
44 upperdentry = ovl_dentry_upper(dentry);
45
46 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
47 attr->ia_valid &= ~ATTR_MODE;
48
49 mutex_lock(&upperdentry->d_inode->i_mutex);
50 old_cred = ovl_override_creds(dentry->d_sb);
51 err = notify_change(upperdentry, attr, NULL);
52 revert_creds(old_cred);
53 if (!err)
54 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
55 mutex_unlock(&upperdentry->d_inode->i_mutex);
56 }
57 ovl_drop_write(dentry);
58out:
59 return err;
60}
61
62int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
63 struct kstat *stat)
64{
65 enum ovl_path_type type;
66 struct path realpath;
67 const struct cred *old_cred;
68 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
69 int err;
70
71 type = ovl_path_real(dentry, &realpath);
72 old_cred = ovl_override_creds(dentry->d_sb);
73 err = vfs_getattr(&realpath, stat);
74 if (err)
75 goto out;
76
77
78
79
80
81
82
83
84
85
86
87 if (ovl_same_sb(dentry->d_sb)) {
88 if (OVL_TYPE_ORIGIN(type)) {
89 struct kstat lowerstat;
90
91 ovl_path_lower(dentry, &realpath);
92 err = vfs_getattr(&realpath, &lowerstat);
93 if (err)
94 goto out;
95
96 WARN_ON_ONCE(stat->dev != lowerstat.dev);
97
98
99
100
101
102
103
104
105 if (is_dir || lowerstat.nlink == 1 ||
106 ovl_test_flag(OVL_INDEX, d_inode(dentry)))
107 stat->ino = lowerstat.ino;
108 }
109 stat->dev = dentry->d_sb->s_dev;
110 } else if (is_dir) {
111
112
113
114
115
116
117
118
119
120 stat->dev = dentry->d_sb->s_dev;
121 stat->ino = dentry->d_inode->i_ino;
122 }
123
124
125
126
127
128
129 if (is_dir && OVL_TYPE_MERGE(type))
130 stat->nlink = 1;
131
132
133
134
135
136
137
138 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
139 stat->nlink = dentry->d_inode->i_nlink;
140
141out:
142 revert_creds(old_cred);
143
144 return err;
145}
146
147int ovl_permission(struct inode *inode, int mask)
148{
149 struct inode *upperinode = ovl_inode_upper(inode);
150 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
151 const struct cred *old_cred;
152 int err;
153
154
155 if (!realinode) {
156 WARN_ON(!(mask & MAY_NOT_BLOCK));
157 return -ECHILD;
158 }
159
160
161
162
163
164 err = generic_permission(inode, mask);
165 if (err)
166 return err;
167
168 old_cred = ovl_override_creds(inode->i_sb);
169 if (!upperinode &&
170 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
171 mask &= ~(MAY_WRITE | MAY_APPEND);
172
173 mask |= MAY_READ;
174 }
175 err = inode_permission(realinode, mask);
176 revert_creds(old_cred);
177
178 return err;
179}
180
181
182struct ovl_link_data {
183 struct dentry *realdentry;
184 void *cookie;
185};
186
187static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
188{
189 void *ret;
190 struct dentry *realdentry;
191 struct inode *realinode;
192 struct ovl_link_data *data = NULL;
193 const struct cred *old_cred;
194
195 realdentry = ovl_dentry_real(dentry);
196 realinode = realdentry->d_inode;
197
198 if (WARN_ON(!realinode->i_op->follow_link))
199 return ERR_PTR(-EPERM);
200
201 if (realinode->i_op->put_link) {
202 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
203 if (!data)
204 return ERR_PTR(-ENOMEM);
205 data->realdentry = realdentry;
206 }
207
208 old_cred = ovl_override_creds(dentry->d_sb);
209 ret = realinode->i_op->follow_link(realdentry, nd);
210 revert_creds(old_cred);
211 if (IS_ERR(ret)) {
212 kfree(data);
213 return ret;
214 }
215
216 if (data)
217 data->cookie = ret;
218
219 return data;
220}
221
222static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
223{
224 struct inode *realinode;
225 struct ovl_link_data *data = c;
226
227 if (!data)
228 return;
229
230 realinode = data->realdentry->d_inode;
231 realinode->i_op->put_link(data->realdentry, nd, data->cookie);
232 kfree(data);
233}
234
235bool ovl_is_private_xattr(const char *name)
236{
237 return strncmp(name, OVL_XATTR_PREFIX,
238 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
239}
240
241int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
242 const void *value, size_t size, int flags)
243{
244 int err;
245 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
246 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
247 const struct cred *old_cred;
248
249 err = ovl_want_write(dentry);
250 if (err)
251 goto out;
252
253 if (!value && !upperdentry) {
254 err = vfs_getxattr(realdentry, name, NULL, 0);
255 if (err < 0)
256 goto out_drop_write;
257 }
258
259 if (!upperdentry) {
260 err = ovl_copy_up(dentry);
261 if (err)
262 goto out_drop_write;
263
264 realdentry = ovl_dentry_upper(dentry);
265 }
266
267 old_cred = ovl_override_creds(dentry->d_sb);
268 if (value)
269 err = vfs_setxattr(realdentry, name, value, size, flags);
270 else {
271 WARN_ON(flags != XATTR_REPLACE);
272 err = vfs_removexattr(realdentry, name);
273 }
274 revert_creds(old_cred);
275
276out_drop_write:
277 ovl_drop_write(dentry);
278out:
279 return err;
280}
281
282int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
283 void *value, size_t size)
284{
285 ssize_t res;
286 const struct cred *old_cred;
287 struct dentry *realdentry =
288 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
289
290 old_cred = ovl_override_creds(dentry->d_sb);
291 res = vfs_getxattr(realdentry, name, value, size);
292 revert_creds(old_cred);
293 return res;
294}
295
296static bool ovl_can_list(const char *s)
297{
298
299 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
300 return true;
301
302
303 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
304}
305
306ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
307{
308 struct dentry *realdentry = ovl_dentry_real(dentry);
309 ssize_t res;
310 size_t len;
311 char *s;
312 const struct cred *old_cred;
313
314 old_cred = ovl_override_creds(dentry->d_sb);
315 res = vfs_listxattr(realdentry, list, size);
316 revert_creds(old_cred);
317 if (res <= 0 || size == 0)
318 return res;
319
320
321 for (s = list, len = res; len;) {
322 size_t slen = strnlen(s, len) + 1;
323
324
325 if (WARN_ON(slen > len))
326 return -EIO;
327
328 len -= slen;
329 if (!ovl_can_list(s)) {
330 res -= slen;
331 memmove(s, s + slen, len);
332 } else {
333 s += slen;
334 }
335 }
336
337 return res;
338}
339
340struct posix_acl *ovl_get_acl(struct inode *inode, int type)
341{
342 struct inode *realinode = ovl_inode_real(inode);
343 const struct cred *old_cred;
344 struct posix_acl *acl;
345
346 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
347 return NULL;
348
349 old_cred = ovl_override_creds(inode->i_sb);
350 acl = get_acl(realinode, type);
351 revert_creds(old_cred);
352
353 return acl;
354}
355
356static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
357{
358 if (ovl_dentry_upper(dentry) &&
359 ovl_dentry_has_upper_alias(dentry))
360 return false;
361
362 if (special_file(d_inode(dentry)->i_mode))
363 return false;
364
365 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
366 return false;
367
368 return true;
369}
370
371int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
372{
373 int err = 0;
374
375 if (ovl_open_need_copy_up(dentry, file_flags)) {
376 err = ovl_want_write(dentry);
377 if (!err) {
378 err = ovl_copy_up_flags(dentry, file_flags);
379 ovl_drop_write(dentry);
380 }
381 }
382
383 return err;
384}
385
386int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
387{
388 struct dentry *alias;
389 struct path upperpath;
390
391 if (!(flags & S_ATIME))
392 return 0;
393
394 alias = d_find_any_alias(inode);
395 if (!alias)
396 return 0;
397
398 ovl_path_upper(alias, &upperpath);
399 if (upperpath.dentry) {
400 touch_atime(&upperpath);
401 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
402 }
403
404 dput(alias);
405
406 return 0;
407}
408
409static const struct inode_operations_wrapper ovl_file_inode_operations = {
410 .ops = {
411 .setattr = ovl_setattr,
412 .permission = ovl_permission,
413 .getattr = ovl_getattr,
414 .setxattr = generic_setxattr,
415 .getxattr = generic_getxattr,
416 .listxattr = ovl_listxattr,
417 .removexattr = generic_removexattr,
418 .get_acl = ovl_get_acl,
419 .update_time = ovl_update_time,
420 },
421};
422
423static const struct inode_operations ovl_symlink_inode_operations = {
424 .setattr = ovl_setattr,
425 .follow_link = ovl_follow_link,
426 .put_link = ovl_put_link,
427 .readlink = generic_readlink,
428 .getattr = ovl_getattr,
429 .setxattr = generic_setxattr,
430 .getxattr = generic_getxattr,
431 .listxattr = ovl_listxattr,
432 .removexattr = generic_removexattr,
433 .update_time = ovl_update_time,
434};
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451#define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
452
453static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
454{
455#ifdef CONFIG_LOCKDEP
456 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
457 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
458 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
459
460 int depth = *get_s_stack_depth(inode->i_sb) - 1;
461
462 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
463 depth = 0;
464
465 if (S_ISDIR(inode->i_mode))
466 lockdep_set_class(&inode->i_mutex, &ovl_i_mutex_dir_key[depth]);
467 else
468 lockdep_set_class(&inode->i_mutex, &ovl_i_mutex_key[depth]);
469
470 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
471#endif
472}
473
474static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
475{
476 inode->i_ino = get_next_ino();
477 inode->i_mode = mode;
478 inode->i_flags |= S_NOCMTIME;
479
480 ovl_lockdep_annotate_inode_mutex_key(inode);
481
482 switch (mode & S_IFMT) {
483 case S_IFREG:
484 inode->i_op = &ovl_file_inode_operations.ops;
485 inode->i_flags |= S_IOPS_WRAPPER;
486 break;
487
488 case S_IFDIR:
489 inode->i_op = &ovl_dir_inode_operations.ops;
490 inode->i_fop = &ovl_dir_operations;
491 inode->i_flags |= S_IOPS_WRAPPER;
492 break;
493
494 case S_IFLNK:
495 inode->i_op = &ovl_symlink_inode_operations;
496 break;
497
498 default:
499 inode->i_op = &ovl_file_inode_operations.ops;
500 inode->i_flags |= S_IOPS_WRAPPER;
501 init_special_inode(inode, mode, rdev);
502 break;
503 }
504}
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530#define OVL_NLINK_ADD_UPPER (1 << 0)
531
532
533
534
535
536
537
538
539static int ovl_set_nlink_common(struct dentry *dentry,
540 struct dentry *realdentry, const char *format)
541{
542 struct inode *inode = d_inode(dentry);
543 struct inode *realinode = d_inode(realdentry);
544 char buf[13];
545 int len;
546
547 len = snprintf(buf, sizeof(buf), format,
548 (int) (inode->i_nlink - realinode->i_nlink));
549
550 return ovl_do_setxattr(ovl_dentry_upper(dentry),
551 OVL_XATTR_NLINK, buf, len, 0);
552}
553
554int ovl_set_nlink_upper(struct dentry *dentry)
555{
556 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
557}
558
559int ovl_set_nlink_lower(struct dentry *dentry)
560{
561 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
562}
563
564unsigned int ovl_get_nlink(struct dentry *lowerdentry,
565 struct dentry *upperdentry,
566 unsigned int fallback)
567{
568 int nlink_diff;
569 int nlink;
570 char buf[13];
571 int err;
572
573 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
574 return fallback;
575
576 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
577 if (err < 0)
578 goto fail;
579
580 buf[err] = '\0';
581 if ((buf[0] != 'L' && buf[0] != 'U') ||
582 (buf[1] != '+' && buf[1] != '-'))
583 goto fail;
584
585 err = kstrtoint(buf + 1, 10, &nlink_diff);
586 if (err < 0)
587 goto fail;
588
589 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
590 nlink += nlink_diff;
591
592 if (nlink <= 0)
593 goto fail;
594
595 return nlink;
596
597fail:
598 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
599 upperdentry, err);
600 return fallback;
601}
602
603struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
604{
605 struct inode *inode;
606
607 inode = new_inode(sb);
608 if (inode)
609 ovl_fill_inode(inode, mode, rdev);
610
611 return inode;
612}
613
614static int ovl_inode_test(struct inode *inode, void *data)
615{
616 return inode->i_private == data;
617}
618
619static int ovl_inode_set(struct inode *inode, void *data)
620{
621 inode->i_private = data;
622 return 0;
623}
624
625static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
626 struct dentry *upperdentry)
627{
628
629
630
631
632
633
634 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
635 return false;
636
637
638
639
640
641 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
642 return false;
643
644 return true;
645}
646
647struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry,
648 struct dentry *index)
649{
650 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
651 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
652 struct inode *inode;
653
654 bool indexed = (index || (ovl_indexdir(dentry->d_sb) && !upperdentry));
655
656 if (WARN_ON(upperdentry && indexed && !lowerdentry))
657 return ERR_PTR(-EIO);
658
659 if (!realinode)
660 realinode = d_inode(lowerdentry);
661
662
663
664
665
666
667
668 if (!S_ISDIR(realinode->i_mode) && (upperdentry || indexed)) {
669 struct inode *key = d_inode(indexed ? lowerdentry :
670 upperdentry);
671 unsigned int nlink;
672
673 inode = iget5_locked(dentry->d_sb, (unsigned long) key,
674 ovl_inode_test, ovl_inode_set, key);
675 if (!inode)
676 goto out_nomem;
677 if (!(inode->i_state & I_NEW)) {
678
679
680
681
682 if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
683 iput(inode);
684 inode = ERR_PTR(-ESTALE);
685 goto out;
686 }
687
688 dput(upperdentry);
689 goto out;
690 }
691
692 nlink = ovl_get_nlink(lowerdentry, upperdentry,
693 realinode->i_nlink);
694 set_nlink(inode, nlink);
695 } else {
696 inode = new_inode(dentry->d_sb);
697 if (!inode)
698 goto out_nomem;
699 }
700 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
701 ovl_inode_init(inode, upperdentry, lowerdentry);
702
703 if (upperdentry && ovl_is_impuredir(upperdentry))
704 ovl_set_flag(OVL_IMPURE, inode);
705
706 if (inode->i_state & I_NEW)
707 unlock_new_inode(inode);
708out:
709 return inode;
710
711out_nomem:
712 inode = ERR_PTR(-ENOMEM);
713 goto out;
714}
715