• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <uapi/linux/magic.h>
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/xattr.h>
11 #include <linux/mount.h>
12 #include <linux/parser.h>
13 #include <linux/module.h>
14 #include <linux/statfs.h>
15 #include <linux/seq_file.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/exportfs.h>
18 #include "overlayfs.h"
19 
20 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21 MODULE_DESCRIPTION("Overlay filesystem");
22 MODULE_LICENSE("GPL");
23 
24 
25 struct ovl_dir_cache;
26 
27 #define OVL_MAX_STACK 500
28 
29 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
30 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
31 MODULE_PARM_DESC(redirect_dir,
32 		 "Default to on or off for the redirect_dir feature");
33 
34 static bool ovl_redirect_always_follow =
35 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
36 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
37 		   bool, 0644);
38 MODULE_PARM_DESC(redirect_always_follow,
39 		 "Follow redirects even if redirect_dir feature is turned off");
40 
41 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
42 module_param_named(index, ovl_index_def, bool, 0644);
43 MODULE_PARM_DESC(index,
44 		 "Default to on or off for the inodes index feature");
45 
46 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
47 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
48 MODULE_PARM_DESC(nfs_export,
49 		 "Default to on or off for the NFS export feature");
50 
51 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
52 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
53 MODULE_PARM_DESC(xino_auto,
54 		 "Auto enable xino feature");
55 
56 static bool __read_mostly ovl_override_creds_def = true;
57 module_param_named(override_creds, ovl_override_creds_def, bool, 0644);
58 MODULE_PARM_DESC(ovl_override_creds_def,
59 		 "Use mounter's credentials for accesses");
60 
ovl_entry_stack_free(struct ovl_entry * oe)61 static void ovl_entry_stack_free(struct ovl_entry *oe)
62 {
63 	unsigned int i;
64 
65 	for (i = 0; i < oe->numlower; i++)
66 		dput(oe->lowerstack[i].dentry);
67 }
68 
69 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
70 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
71 MODULE_PARM_DESC(metacopy,
72 		 "Default to on or off for the metadata only copy up feature");
73 
ovl_dentry_release(struct dentry * dentry)74 static void ovl_dentry_release(struct dentry *dentry)
75 {
76 	struct ovl_entry *oe = dentry->d_fsdata;
77 
78 	if (oe) {
79 		ovl_entry_stack_free(oe);
80 		kfree_rcu(oe, rcu);
81 	}
82 }
83 
ovl_d_real(struct dentry * dentry,const struct inode * inode)84 static struct dentry *ovl_d_real(struct dentry *dentry,
85 				 const struct inode *inode)
86 {
87 	struct dentry *real = NULL, *lower;
88 
89 	/* It's an overlay file */
90 	if (inode && d_inode(dentry) == inode)
91 		return dentry;
92 
93 	if (!d_is_reg(dentry)) {
94 		if (!inode || inode == d_inode(dentry))
95 			return dentry;
96 		goto bug;
97 	}
98 
99 	real = ovl_dentry_upper(dentry);
100 	if (real && (inode == d_inode(real)))
101 		return real;
102 
103 	if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
104 		return real;
105 
106 	lower = ovl_dentry_lowerdata(dentry);
107 	if (!lower)
108 		goto bug;
109 	real = lower;
110 
111 	/* Handle recursion */
112 	real = d_real(real, inode);
113 
114 	if (!inode || inode == d_inode(real))
115 		return real;
116 bug:
117 	WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
118 	     __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
119 	     inode ? inode->i_ino : 0, real,
120 	     real && d_inode(real) ? d_inode(real)->i_ino : 0);
121 	return dentry;
122 }
123 
ovl_dentry_revalidate(struct dentry * dentry,unsigned int flags)124 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
125 {
126 	struct ovl_entry *oe = dentry->d_fsdata;
127 	unsigned int i;
128 	int ret = 1;
129 
130 	for (i = 0; i < oe->numlower; i++) {
131 		struct dentry *d = oe->lowerstack[i].dentry;
132 
133 		if (d->d_flags & DCACHE_OP_REVALIDATE) {
134 			ret = d->d_op->d_revalidate(d, flags);
135 			if (ret < 0)
136 				return ret;
137 			if (!ret) {
138 				if (!(flags & LOOKUP_RCU))
139 					d_invalidate(d);
140 				return -ESTALE;
141 			}
142 		}
143 	}
144 	return 1;
145 }
146 
ovl_dentry_weak_revalidate(struct dentry * dentry,unsigned int flags)147 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
148 {
149 	struct ovl_entry *oe = dentry->d_fsdata;
150 	unsigned int i;
151 	int ret = 1;
152 
153 	for (i = 0; i < oe->numlower; i++) {
154 		struct dentry *d = oe->lowerstack[i].dentry;
155 
156 		if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
157 			ret = d->d_op->d_weak_revalidate(d, flags);
158 			if (ret <= 0)
159 				break;
160 		}
161 	}
162 	return ret;
163 }
164 
165 static const struct dentry_operations ovl_dentry_operations = {
166 	.d_release = ovl_dentry_release,
167 	.d_real = ovl_d_real,
168 };
169 
170 static const struct dentry_operations ovl_reval_dentry_operations = {
171 	.d_release = ovl_dentry_release,
172 	.d_real = ovl_d_real,
173 	.d_revalidate = ovl_dentry_revalidate,
174 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
175 };
176 
177 static struct kmem_cache *ovl_inode_cachep;
178 
ovl_alloc_inode(struct super_block * sb)179 static struct inode *ovl_alloc_inode(struct super_block *sb)
180 {
181 	struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
182 
183 	if (!oi)
184 		return NULL;
185 
186 	oi->cache = NULL;
187 	oi->redirect = NULL;
188 	oi->version = 0;
189 	oi->flags = 0;
190 	oi->__upperdentry = NULL;
191 	oi->lower = NULL;
192 	oi->lowerdata = NULL;
193 	mutex_init(&oi->lock);
194 
195 	return &oi->vfs_inode;
196 }
197 
ovl_free_inode(struct inode * inode)198 static void ovl_free_inode(struct inode *inode)
199 {
200 	struct ovl_inode *oi = OVL_I(inode);
201 
202 	kfree(oi->redirect);
203 	mutex_destroy(&oi->lock);
204 	kmem_cache_free(ovl_inode_cachep, oi);
205 }
206 
ovl_destroy_inode(struct inode * inode)207 static void ovl_destroy_inode(struct inode *inode)
208 {
209 	struct ovl_inode *oi = OVL_I(inode);
210 
211 	dput(oi->__upperdentry);
212 	iput(oi->lower);
213 	if (S_ISDIR(inode->i_mode))
214 		ovl_dir_cache_free(inode);
215 	else
216 		iput(oi->lowerdata);
217 }
218 
ovl_free_fs(struct ovl_fs * ofs)219 static void ovl_free_fs(struct ovl_fs *ofs)
220 {
221 	unsigned i;
222 
223 	iput(ofs->workbasedir_trap);
224 	iput(ofs->indexdir_trap);
225 	iput(ofs->workdir_trap);
226 	iput(ofs->upperdir_trap);
227 	dput(ofs->indexdir);
228 	dput(ofs->workdir);
229 	if (ofs->workdir_locked)
230 		ovl_inuse_unlock(ofs->workbasedir);
231 	dput(ofs->workbasedir);
232 	if (ofs->upperdir_locked)
233 		ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
234 	mntput(ofs->upper_mnt);
235 	for (i = 0; i < ofs->numlower; i++) {
236 		iput(ofs->lower_layers[i].trap);
237 		mntput(ofs->lower_layers[i].mnt);
238 	}
239 	for (i = 0; i < ofs->numlowerfs; i++)
240 		free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
241 	kfree(ofs->lower_layers);
242 	kfree(ofs->lower_fs);
243 
244 	kfree(ofs->config.lowerdir);
245 	kfree(ofs->config.upperdir);
246 	kfree(ofs->config.workdir);
247 	kfree(ofs->config.redirect_mode);
248 	if (ofs->creator_cred)
249 		put_cred(ofs->creator_cred);
250 	kfree(ofs);
251 }
252 
ovl_put_super(struct super_block * sb)253 static void ovl_put_super(struct super_block *sb)
254 {
255 	struct ovl_fs *ofs = sb->s_fs_info;
256 
257 	ovl_free_fs(ofs);
258 }
259 
260 /* Sync real dirty inodes in upper filesystem (if it exists) */
ovl_sync_fs(struct super_block * sb,int wait)261 static int ovl_sync_fs(struct super_block *sb, int wait)
262 {
263 	struct ovl_fs *ofs = sb->s_fs_info;
264 	struct super_block *upper_sb;
265 	int ret;
266 
267 	if (!ofs->upper_mnt)
268 		return 0;
269 
270 	/*
271 	 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
272 	 * All the super blocks will be iterated, including upper_sb.
273 	 *
274 	 * If this is a syncfs(2) call, then we do need to call
275 	 * sync_filesystem() on upper_sb, but enough if we do it when being
276 	 * called with wait == 1.
277 	 */
278 	if (!wait)
279 		return 0;
280 
281 	upper_sb = ofs->upper_mnt->mnt_sb;
282 
283 	down_read(&upper_sb->s_umount);
284 	ret = sync_filesystem(upper_sb);
285 	up_read(&upper_sb->s_umount);
286 
287 	return ret;
288 }
289 
290 /**
291  * ovl_statfs
292  * @sb: The overlayfs super block
293  * @buf: The struct kstatfs to fill in with stats
294  *
295  * Get the filesystem statistics.  As writes always target the upper layer
296  * filesystem pass the statfs to the upper filesystem (if it exists)
297  */
ovl_statfs(struct dentry * dentry,struct kstatfs * buf)298 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
299 {
300 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
301 	struct dentry *root_dentry = dentry->d_sb->s_root;
302 	struct path path;
303 	int err;
304 
305 	ovl_path_real(root_dentry, &path);
306 
307 	err = vfs_statfs(&path, buf);
308 	if (!err) {
309 		buf->f_namelen = ofs->namelen;
310 		buf->f_type = OVERLAYFS_SUPER_MAGIC;
311 	}
312 
313 	return err;
314 }
315 
316 /* Will this overlay be forced to mount/remount ro? */
ovl_force_readonly(struct ovl_fs * ofs)317 static bool ovl_force_readonly(struct ovl_fs *ofs)
318 {
319 	return (!ofs->upper_mnt || !ofs->workdir);
320 }
321 
ovl_redirect_mode_def(void)322 static const char *ovl_redirect_mode_def(void)
323 {
324 	return ovl_redirect_dir_def ? "on" : "off";
325 }
326 
327 enum {
328 	OVL_XINO_OFF,
329 	OVL_XINO_AUTO,
330 	OVL_XINO_ON,
331 };
332 
333 static const char * const ovl_xino_str[] = {
334 	"off",
335 	"auto",
336 	"on",
337 };
338 
ovl_xino_def(void)339 static inline int ovl_xino_def(void)
340 {
341 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
342 }
343 
344 /**
345  * ovl_show_options
346  *
347  * Prints the mount options for a given superblock.
348  * Returns zero; does not fail.
349  */
ovl_show_options(struct seq_file * m,struct dentry * dentry)350 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
351 {
352 	struct super_block *sb = dentry->d_sb;
353 	struct ovl_fs *ofs = sb->s_fs_info;
354 
355 	seq_show_option(m, "lowerdir", ofs->config.lowerdir);
356 	if (ofs->config.upperdir) {
357 		seq_show_option(m, "upperdir", ofs->config.upperdir);
358 		seq_show_option(m, "workdir", ofs->config.workdir);
359 	}
360 	if (ofs->config.default_permissions)
361 		seq_puts(m, ",default_permissions");
362 	if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
363 		seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
364 	if (ofs->config.index != ovl_index_def)
365 		seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
366 	if (ofs->config.nfs_export != ovl_nfs_export_def)
367 		seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
368 						"on" : "off");
369 	if (ofs->config.xino != ovl_xino_def())
370 		seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
371 	if (ofs->config.metacopy != ovl_metacopy_def)
372 		seq_printf(m, ",metacopy=%s",
373 			   ofs->config.metacopy ? "on" : "off");
374 	if (ofs->config.override_creds != ovl_override_creds_def)
375 		seq_show_option(m, "override_creds",
376 				ofs->config.override_creds ? "on" : "off");
377 	return 0;
378 }
379 
ovl_remount(struct super_block * sb,int * flags,char * data)380 static int ovl_remount(struct super_block *sb, int *flags, char *data)
381 {
382 	struct ovl_fs *ofs = sb->s_fs_info;
383 
384 	if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
385 		return -EROFS;
386 
387 	return 0;
388 }
389 
390 static const struct super_operations ovl_super_operations = {
391 	.alloc_inode	= ovl_alloc_inode,
392 	.free_inode	= ovl_free_inode,
393 	.destroy_inode	= ovl_destroy_inode,
394 	.drop_inode	= generic_delete_inode,
395 	.put_super	= ovl_put_super,
396 	.sync_fs	= ovl_sync_fs,
397 	.statfs		= ovl_statfs,
398 	.show_options	= ovl_show_options,
399 	.remount_fs	= ovl_remount,
400 };
401 
402 enum {
403 	OPT_LOWERDIR,
404 	OPT_UPPERDIR,
405 	OPT_WORKDIR,
406 	OPT_DEFAULT_PERMISSIONS,
407 	OPT_REDIRECT_DIR,
408 	OPT_INDEX_ON,
409 	OPT_INDEX_OFF,
410 	OPT_NFS_EXPORT_ON,
411 	OPT_NFS_EXPORT_OFF,
412 	OPT_XINO_ON,
413 	OPT_XINO_OFF,
414 	OPT_XINO_AUTO,
415 	OPT_METACOPY_ON,
416 	OPT_METACOPY_OFF,
417 	OPT_OVERRIDE_CREDS_ON,
418 	OPT_OVERRIDE_CREDS_OFF,
419 	OPT_ERR,
420 };
421 
422 static const match_table_t ovl_tokens = {
423 	{OPT_LOWERDIR,			"lowerdir=%s"},
424 	{OPT_UPPERDIR,			"upperdir=%s"},
425 	{OPT_WORKDIR,			"workdir=%s"},
426 	{OPT_DEFAULT_PERMISSIONS,	"default_permissions"},
427 	{OPT_REDIRECT_DIR,		"redirect_dir=%s"},
428 	{OPT_INDEX_ON,			"index=on"},
429 	{OPT_INDEX_OFF,			"index=off"},
430 	{OPT_NFS_EXPORT_ON,		"nfs_export=on"},
431 	{OPT_NFS_EXPORT_OFF,		"nfs_export=off"},
432 	{OPT_XINO_ON,			"xino=on"},
433 	{OPT_XINO_OFF,			"xino=off"},
434 	{OPT_XINO_AUTO,			"xino=auto"},
435 	{OPT_METACOPY_ON,		"metacopy=on"},
436 	{OPT_METACOPY_OFF,		"metacopy=off"},
437 	{OPT_OVERRIDE_CREDS_ON,		"override_creds=on"},
438 	{OPT_OVERRIDE_CREDS_OFF,	"override_creds=off"},
439 	{OPT_ERR,			NULL}
440 };
441 
ovl_next_opt(char ** s)442 static char *ovl_next_opt(char **s)
443 {
444 	char *sbegin = *s;
445 	char *p;
446 
447 	if (sbegin == NULL)
448 		return NULL;
449 
450 	for (p = sbegin; *p; p++) {
451 		if (*p == '\\') {
452 			p++;
453 			if (!*p)
454 				break;
455 		} else if (*p == ',') {
456 			*p = '\0';
457 			*s = p + 1;
458 			return sbegin;
459 		}
460 	}
461 	*s = NULL;
462 	return sbegin;
463 }
464 
ovl_parse_redirect_mode(struct ovl_config * config,const char * mode)465 static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
466 {
467 	if (strcmp(mode, "on") == 0) {
468 		config->redirect_dir = true;
469 		/*
470 		 * Does not make sense to have redirect creation without
471 		 * redirect following.
472 		 */
473 		config->redirect_follow = true;
474 	} else if (strcmp(mode, "follow") == 0) {
475 		config->redirect_follow = true;
476 	} else if (strcmp(mode, "off") == 0) {
477 		if (ovl_redirect_always_follow)
478 			config->redirect_follow = true;
479 	} else if (strcmp(mode, "nofollow") != 0) {
480 		pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
481 		       mode);
482 		return -EINVAL;
483 	}
484 
485 	return 0;
486 }
487 
ovl_parse_opt(char * opt,struct ovl_config * config)488 static int ovl_parse_opt(char *opt, struct ovl_config *config)
489 {
490 	char *p;
491 	int err;
492 	bool metacopy_opt = false, redirect_opt = false;
493 
494 	config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
495 	if (!config->redirect_mode)
496 		return -ENOMEM;
497 	config->override_creds = ovl_override_creds_def;
498 
499 	while ((p = ovl_next_opt(&opt)) != NULL) {
500 		int token;
501 		substring_t args[MAX_OPT_ARGS];
502 
503 		if (!*p)
504 			continue;
505 
506 		token = match_token(p, ovl_tokens, args);
507 		switch (token) {
508 		case OPT_UPPERDIR:
509 			kfree(config->upperdir);
510 			config->upperdir = match_strdup(&args[0]);
511 			if (!config->upperdir)
512 				return -ENOMEM;
513 			break;
514 
515 		case OPT_LOWERDIR:
516 			kfree(config->lowerdir);
517 			config->lowerdir = match_strdup(&args[0]);
518 			if (!config->lowerdir)
519 				return -ENOMEM;
520 			break;
521 
522 		case OPT_WORKDIR:
523 			kfree(config->workdir);
524 			config->workdir = match_strdup(&args[0]);
525 			if (!config->workdir)
526 				return -ENOMEM;
527 			break;
528 
529 		case OPT_DEFAULT_PERMISSIONS:
530 			config->default_permissions = true;
531 			break;
532 
533 		case OPT_REDIRECT_DIR:
534 			kfree(config->redirect_mode);
535 			config->redirect_mode = match_strdup(&args[0]);
536 			if (!config->redirect_mode)
537 				return -ENOMEM;
538 			redirect_opt = true;
539 			break;
540 
541 		case OPT_INDEX_ON:
542 			config->index = true;
543 			break;
544 
545 		case OPT_INDEX_OFF:
546 			config->index = false;
547 			break;
548 
549 		case OPT_NFS_EXPORT_ON:
550 			config->nfs_export = true;
551 			break;
552 
553 		case OPT_NFS_EXPORT_OFF:
554 			config->nfs_export = false;
555 			break;
556 
557 		case OPT_XINO_ON:
558 			config->xino = OVL_XINO_ON;
559 			break;
560 
561 		case OPT_XINO_OFF:
562 			config->xino = OVL_XINO_OFF;
563 			break;
564 
565 		case OPT_XINO_AUTO:
566 			config->xino = OVL_XINO_AUTO;
567 			break;
568 
569 		case OPT_METACOPY_ON:
570 			config->metacopy = true;
571 			metacopy_opt = true;
572 			break;
573 
574 		case OPT_METACOPY_OFF:
575 			config->metacopy = false;
576 			break;
577 
578 		case OPT_OVERRIDE_CREDS_ON:
579 			config->override_creds = true;
580 			break;
581 
582 		case OPT_OVERRIDE_CREDS_OFF:
583 			config->override_creds = false;
584 			break;
585 
586 		default:
587 			pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
588 			return -EINVAL;
589 		}
590 	}
591 
592 	/* Workdir is useless in non-upper mount */
593 	if (!config->upperdir && config->workdir) {
594 		pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
595 			config->workdir);
596 		kfree(config->workdir);
597 		config->workdir = NULL;
598 	}
599 
600 	err = ovl_parse_redirect_mode(config, config->redirect_mode);
601 	if (err)
602 		return err;
603 
604 	/*
605 	 * This is to make the logic below simpler.  It doesn't make any other
606 	 * difference, since config->redirect_dir is only used for upper.
607 	 */
608 	if (!config->upperdir && config->redirect_follow)
609 		config->redirect_dir = true;
610 
611 	/* Resolve metacopy -> redirect_dir dependency */
612 	if (config->metacopy && !config->redirect_dir) {
613 		if (metacopy_opt && redirect_opt) {
614 			pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
615 			       config->redirect_mode);
616 			return -EINVAL;
617 		}
618 		if (redirect_opt) {
619 			/*
620 			 * There was an explicit redirect_dir=... that resulted
621 			 * in this conflict.
622 			 */
623 			pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
624 				config->redirect_mode);
625 			config->metacopy = false;
626 		} else {
627 			/* Automatically enable redirect otherwise. */
628 			config->redirect_follow = config->redirect_dir = true;
629 		}
630 	}
631 
632 	return 0;
633 }
634 
635 #define OVL_WORKDIR_NAME "work"
636 #define OVL_INDEXDIR_NAME "index"
637 
ovl_workdir_create(struct ovl_fs * ofs,const char * name,bool persist)638 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
639 					 const char *name, bool persist)
640 {
641 	struct inode *dir =  ofs->workbasedir->d_inode;
642 	struct vfsmount *mnt = ofs->upper_mnt;
643 	struct dentry *work;
644 	int err;
645 	bool retried = false;
646 	bool locked = false;
647 
648 	inode_lock_nested(dir, I_MUTEX_PARENT);
649 	locked = true;
650 
651 retry:
652 	work = lookup_one_len(name, ofs->workbasedir, strlen(name));
653 
654 	if (!IS_ERR(work)) {
655 		struct iattr attr = {
656 			.ia_valid = ATTR_MODE,
657 			.ia_mode = S_IFDIR | 0,
658 		};
659 
660 		if (work->d_inode) {
661 			err = -EEXIST;
662 			if (retried)
663 				goto out_dput;
664 
665 			if (persist)
666 				goto out_unlock;
667 
668 			retried = true;
669 			ovl_workdir_cleanup(dir, mnt, work, 0);
670 			dput(work);
671 			goto retry;
672 		}
673 
674 		err = ovl_mkdir_real(dir, &work, attr.ia_mode);
675 		if (err)
676 			goto out_dput;
677 
678 		/* Weird filesystem returning with hashed negative (kernfs)? */
679 		err = -EINVAL;
680 		if (d_really_is_negative(work))
681 			goto out_dput;
682 
683 		/*
684 		 * Try to remove POSIX ACL xattrs from workdir.  We are good if:
685 		 *
686 		 * a) success (there was a POSIX ACL xattr and was removed)
687 		 * b) -ENODATA (there was no POSIX ACL xattr)
688 		 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
689 		 *
690 		 * There are various other error values that could effectively
691 		 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
692 		 * if the xattr name is too long), but the set of filesystems
693 		 * allowed as upper are limited to "normal" ones, where checking
694 		 * for the above two errors is sufficient.
695 		 */
696 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
697 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
698 			goto out_dput;
699 
700 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
701 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
702 			goto out_dput;
703 
704 		/* Clear any inherited mode bits */
705 		inode_lock(work->d_inode);
706 		err = notify_change(work, &attr, NULL);
707 		inode_unlock(work->d_inode);
708 		if (err)
709 			goto out_dput;
710 	} else {
711 		err = PTR_ERR(work);
712 		goto out_err;
713 	}
714 out_unlock:
715 	if (locked)
716 		inode_unlock(dir);
717 
718 	return work;
719 
720 out_dput:
721 	dput(work);
722 out_err:
723 	pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
724 		ofs->config.workdir, name, -err);
725 	work = NULL;
726 	goto out_unlock;
727 }
728 
ovl_unescape(char * s)729 static void ovl_unescape(char *s)
730 {
731 	char *d = s;
732 
733 	for (;; s++, d++) {
734 		if (*s == '\\')
735 			s++;
736 		*d = *s;
737 		if (!*s)
738 			break;
739 	}
740 }
741 
ovl_mount_dir_noesc(const char * name,struct path * path)742 static int ovl_mount_dir_noesc(const char *name, struct path *path)
743 {
744 	int err = -EINVAL;
745 
746 	if (!*name) {
747 		pr_err("overlayfs: empty lowerdir\n");
748 		goto out;
749 	}
750 	err = kern_path(name, LOOKUP_FOLLOW, path);
751 	if (err) {
752 		pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
753 		goto out;
754 	}
755 	err = -EINVAL;
756 	if (ovl_dentry_weird(path->dentry)) {
757 		pr_err("overlayfs: filesystem on '%s' not supported\n", name);
758 		goto out_put;
759 	}
760 	if (!d_is_dir(path->dentry)) {
761 		pr_err("overlayfs: '%s' not a directory\n", name);
762 		goto out_put;
763 	}
764 	return 0;
765 
766 out_put:
767 	path_put_init(path);
768 out:
769 	return err;
770 }
771 
ovl_mount_dir(const char * name,struct path * path)772 static int ovl_mount_dir(const char *name, struct path *path)
773 {
774 	int err = -ENOMEM;
775 	char *tmp = kstrdup(name, GFP_KERNEL);
776 
777 	if (tmp) {
778 		ovl_unescape(tmp);
779 		err = ovl_mount_dir_noesc(tmp, path);
780 
781 		if (!err)
782 			if (ovl_dentry_remote(path->dentry)) {
783 				pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
784 				       tmp);
785 				path_put_init(path);
786 				err = -EINVAL;
787 			}
788 		kfree(tmp);
789 	}
790 	return err;
791 }
792 
ovl_check_namelen(struct path * path,struct ovl_fs * ofs,const char * name)793 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
794 			     const char *name)
795 {
796 	struct kstatfs statfs;
797 	int err = vfs_statfs(path, &statfs);
798 
799 	if (err)
800 		pr_err("overlayfs: statfs failed on '%s'\n", name);
801 	else
802 		ofs->namelen = max(ofs->namelen, statfs.f_namelen);
803 
804 	return err;
805 }
806 
ovl_lower_dir(const char * name,struct path * path,struct ovl_fs * ofs,int * stack_depth,bool * remote)807 static int ovl_lower_dir(const char *name, struct path *path,
808 			 struct ovl_fs *ofs, int *stack_depth, bool *remote)
809 {
810 	int fh_type;
811 	int err;
812 
813 	err = ovl_mount_dir_noesc(name, path);
814 	if (err)
815 		goto out;
816 
817 	err = ovl_check_namelen(path, ofs, name);
818 	if (err)
819 		goto out_put;
820 
821 	*stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
822 
823 	if (ovl_dentry_remote(path->dentry))
824 		*remote = true;
825 
826 	/*
827 	 * The inodes index feature and NFS export need to encode and decode
828 	 * file handles, so they require that all layers support them.
829 	 */
830 	fh_type = ovl_can_decode_fh(path->dentry->d_sb);
831 	if ((ofs->config.nfs_export ||
832 	     (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
833 		ofs->config.index = false;
834 		ofs->config.nfs_export = false;
835 		pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
836 			name);
837 	}
838 
839 	/* Check if lower fs has 32bit inode numbers */
840 	if (fh_type != FILEID_INO32_GEN)
841 		ofs->xino_bits = 0;
842 
843 	return 0;
844 
845 out_put:
846 	path_put_init(path);
847 out:
848 	return err;
849 }
850 
851 /* Workdir should not be subdir of upperdir and vice versa */
ovl_workdir_ok(struct dentry * workdir,struct dentry * upperdir)852 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
853 {
854 	bool ok = false;
855 
856 	if (workdir != upperdir) {
857 		ok = (lock_rename(workdir, upperdir) == NULL);
858 		unlock_rename(workdir, upperdir);
859 	}
860 	return ok;
861 }
862 
ovl_split_lowerdirs(char * str)863 static unsigned int ovl_split_lowerdirs(char *str)
864 {
865 	unsigned int ctr = 1;
866 	char *s, *d;
867 
868 	for (s = d = str;; s++, d++) {
869 		if (*s == '\\') {
870 			s++;
871 		} else if (*s == ':') {
872 			*d = '\0';
873 			ctr++;
874 			continue;
875 		}
876 		*d = *s;
877 		if (!*s)
878 			break;
879 	}
880 	return ctr;
881 }
882 
883 static int __maybe_unused
ovl_posix_acl_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size,int flags)884 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
885 			struct dentry *dentry, struct inode *inode,
886 			const char *name, void *buffer, size_t size, int flags)
887 {
888 	return ovl_xattr_get(dentry, inode, handler->name, buffer, size, flags);
889 }
890 
891 static int __maybe_unused
ovl_posix_acl_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)892 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
893 			struct dentry *dentry, struct inode *inode,
894 			const char *name, const void *value,
895 			size_t size, int flags)
896 {
897 	struct dentry *workdir = ovl_workdir(dentry);
898 	struct inode *realinode = ovl_inode_real(inode);
899 	struct posix_acl *acl = NULL;
900 	int err;
901 
902 	/* Check that everything is OK before copy-up */
903 	if (value) {
904 		acl = posix_acl_from_xattr(&init_user_ns, value, size);
905 		if (IS_ERR(acl))
906 			return PTR_ERR(acl);
907 	}
908 	err = -EOPNOTSUPP;
909 	if (!IS_POSIXACL(d_inode(workdir)))
910 		goto out_acl_release;
911 	if (!realinode->i_op->set_acl)
912 		goto out_acl_release;
913 	if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
914 		err = acl ? -EACCES : 0;
915 		goto out_acl_release;
916 	}
917 	err = -EPERM;
918 	if (!inode_owner_or_capable(inode))
919 		goto out_acl_release;
920 
921 	posix_acl_release(acl);
922 
923 	/*
924 	 * Check if sgid bit needs to be cleared (actual setacl operation will
925 	 * be done with mounter's capabilities and so that won't do it for us).
926 	 */
927 	if (unlikely(inode->i_mode & S_ISGID) &&
928 	    handler->flags == ACL_TYPE_ACCESS &&
929 	    !in_group_p(inode->i_gid) &&
930 	    !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
931 		struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
932 
933 		err = ovl_setattr(dentry, &iattr);
934 		if (err)
935 			return err;
936 	}
937 
938 	err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
939 	if (!err)
940 		ovl_copyattr(ovl_inode_real(inode), inode);
941 
942 	return err;
943 
944 out_acl_release:
945 	posix_acl_release(acl);
946 	return err;
947 }
948 
ovl_own_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size,int flags)949 static int ovl_own_xattr_get(const struct xattr_handler *handler,
950 			     struct dentry *dentry, struct inode *inode,
951 			     const char *name, void *buffer, size_t size,
952 			     int flags)
953 {
954 	return -EOPNOTSUPP;
955 }
956 
ovl_own_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)957 static int ovl_own_xattr_set(const struct xattr_handler *handler,
958 			     struct dentry *dentry, struct inode *inode,
959 			     const char *name, const void *value,
960 			     size_t size, int flags)
961 {
962 	return -EOPNOTSUPP;
963 }
964 
ovl_other_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size,int flags)965 static int ovl_other_xattr_get(const struct xattr_handler *handler,
966 			       struct dentry *dentry, struct inode *inode,
967 			       const char *name, void *buffer, size_t size,
968 			       int flags)
969 {
970 	return ovl_xattr_get(dentry, inode, name, buffer, size, flags);
971 }
972 
ovl_other_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)973 static int ovl_other_xattr_set(const struct xattr_handler *handler,
974 			       struct dentry *dentry, struct inode *inode,
975 			       const char *name, const void *value,
976 			       size_t size, int flags)
977 {
978 	return ovl_xattr_set(dentry, inode, name, value, size, flags);
979 }
980 
981 static const struct xattr_handler __maybe_unused
982 ovl_posix_acl_access_xattr_handler = {
983 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
984 	.flags = ACL_TYPE_ACCESS,
985 	.get = ovl_posix_acl_xattr_get,
986 	.set = ovl_posix_acl_xattr_set,
987 };
988 
989 static const struct xattr_handler __maybe_unused
990 ovl_posix_acl_default_xattr_handler = {
991 	.name = XATTR_NAME_POSIX_ACL_DEFAULT,
992 	.flags = ACL_TYPE_DEFAULT,
993 	.get = ovl_posix_acl_xattr_get,
994 	.set = ovl_posix_acl_xattr_set,
995 };
996 
997 static const struct xattr_handler ovl_own_xattr_handler = {
998 	.prefix	= OVL_XATTR_PREFIX,
999 	.get = ovl_own_xattr_get,
1000 	.set = ovl_own_xattr_set,
1001 };
1002 
1003 static const struct xattr_handler ovl_other_xattr_handler = {
1004 	.prefix	= "", /* catch all */
1005 	.get = ovl_other_xattr_get,
1006 	.set = ovl_other_xattr_set,
1007 };
1008 
1009 static const struct xattr_handler *ovl_xattr_handlers[] = {
1010 #ifdef CONFIG_FS_POSIX_ACL
1011 	&ovl_posix_acl_access_xattr_handler,
1012 	&ovl_posix_acl_default_xattr_handler,
1013 #endif
1014 	&ovl_own_xattr_handler,
1015 	&ovl_other_xattr_handler,
1016 	NULL
1017 };
1018 
ovl_setup_trap(struct super_block * sb,struct dentry * dir,struct inode ** ptrap,const char * name)1019 static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
1020 			  struct inode **ptrap, const char *name)
1021 {
1022 	struct inode *trap;
1023 	int err;
1024 
1025 	trap = ovl_get_trap_inode(sb, dir);
1026 	err = PTR_ERR_OR_ZERO(trap);
1027 	if (err) {
1028 		if (err == -ELOOP)
1029 			pr_err("overlayfs: conflicting %s path\n", name);
1030 		return err;
1031 	}
1032 
1033 	*ptrap = trap;
1034 	return 0;
1035 }
1036 
1037 /*
1038  * Determine how we treat concurrent use of upperdir/workdir based on the
1039  * index feature. This is papering over mount leaks of container runtimes,
1040  * for example, an old overlay mount is leaked and now its upperdir is
1041  * attempted to be used as a lower layer in a new overlay mount.
1042  */
ovl_report_in_use(struct ovl_fs * ofs,const char * name)1043 static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1044 {
1045 	if (ofs->config.index) {
1046 		pr_err("overlayfs: %s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
1047 		       name);
1048 		return -EBUSY;
1049 	} else {
1050 		pr_warn("overlayfs: %s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
1051 			name);
1052 		return 0;
1053 	}
1054 }
1055 
ovl_get_upper(struct super_block * sb,struct ovl_fs * ofs,struct path * upperpath)1056 static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1057 			 struct path *upperpath)
1058 {
1059 	struct vfsmount *upper_mnt;
1060 	int err;
1061 
1062 	err = ovl_mount_dir(ofs->config.upperdir, upperpath);
1063 	if (err)
1064 		goto out;
1065 
1066 	/* Upper fs should not be r/o */
1067 	if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1068 		pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1069 		err = -EINVAL;
1070 		goto out;
1071 	}
1072 
1073 	err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1074 	if (err)
1075 		goto out;
1076 
1077 	err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
1078 			     "upperdir");
1079 	if (err)
1080 		goto out;
1081 
1082 	upper_mnt = clone_private_mount(upperpath);
1083 	err = PTR_ERR(upper_mnt);
1084 	if (IS_ERR(upper_mnt)) {
1085 		pr_err("overlayfs: failed to clone upperpath\n");
1086 		goto out;
1087 	}
1088 
1089 	/* Don't inherit atime flags */
1090 	upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1091 	ofs->upper_mnt = upper_mnt;
1092 
1093 	if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1094 		ofs->upperdir_locked = true;
1095 	} else {
1096 		err = ovl_report_in_use(ofs, "upperdir");
1097 		if (err)
1098 			goto out;
1099 	}
1100 
1101 	err = 0;
1102 out:
1103 	return err;
1104 }
1105 
ovl_make_workdir(struct super_block * sb,struct ovl_fs * ofs,struct path * workpath)1106 static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1107 			    struct path *workpath)
1108 {
1109 	struct vfsmount *mnt = ofs->upper_mnt;
1110 	struct dentry *temp;
1111 	int fh_type;
1112 	int err;
1113 
1114 	err = mnt_want_write(mnt);
1115 	if (err)
1116 		return err;
1117 
1118 	ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1119 	if (!ofs->workdir)
1120 		goto out;
1121 
1122 	err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1123 	if (err)
1124 		goto out;
1125 
1126 	/*
1127 	 * Upper should support d_type, else whiteouts are visible.  Given
1128 	 * workdir and upper are on same fs, we can do iterate_dir() on
1129 	 * workdir. This check requires successful creation of workdir in
1130 	 * previous step.
1131 	 */
1132 	err = ovl_check_d_type_supported(workpath);
1133 	if (err < 0)
1134 		goto out;
1135 
1136 	/*
1137 	 * We allowed this configuration and don't want to break users over
1138 	 * kernel upgrade. So warn instead of erroring out.
1139 	 */
1140 	if (!err)
1141 		pr_warn("overlayfs: upper fs needs to support d_type.\n");
1142 
1143 	/* Check if upper/work fs supports O_TMPFILE */
1144 	temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1145 	ofs->tmpfile = !IS_ERR(temp);
1146 	if (ofs->tmpfile)
1147 		dput(temp);
1148 	else
1149 		pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1150 
1151 	/*
1152 	 * Check if upper/work fs supports trusted.overlay.* xattr
1153 	 */
1154 	err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
1155 	if (err) {
1156 		ofs->noxattr = true;
1157 		ofs->config.index = false;
1158 		ofs->config.metacopy = false;
1159 		pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1160 		err = 0;
1161 	} else {
1162 		vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
1163 	}
1164 
1165 	/* Check if upper/work fs supports file handles */
1166 	fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1167 	if (ofs->config.index && !fh_type) {
1168 		ofs->config.index = false;
1169 		pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1170 	}
1171 
1172 	/* Check if upper fs has 32bit inode numbers */
1173 	if (fh_type != FILEID_INO32_GEN)
1174 		ofs->xino_bits = 0;
1175 
1176 	/* NFS export of r/w mount depends on index */
1177 	if (ofs->config.nfs_export && !ofs->config.index) {
1178 		pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1179 		ofs->config.nfs_export = false;
1180 	}
1181 out:
1182 	mnt_drop_write(mnt);
1183 	return err;
1184 }
1185 
ovl_get_workdir(struct super_block * sb,struct ovl_fs * ofs,struct path * upperpath)1186 static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1187 			   struct path *upperpath)
1188 {
1189 	int err;
1190 	struct path workpath = { };
1191 
1192 	err = ovl_mount_dir(ofs->config.workdir, &workpath);
1193 	if (err)
1194 		goto out;
1195 
1196 	err = -EINVAL;
1197 	if (upperpath->mnt != workpath.mnt) {
1198 		pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1199 		goto out;
1200 	}
1201 	if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1202 		pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1203 		goto out;
1204 	}
1205 
1206 	ofs->workbasedir = dget(workpath.dentry);
1207 
1208 	if (ovl_inuse_trylock(ofs->workbasedir)) {
1209 		ofs->workdir_locked = true;
1210 	} else {
1211 		err = ovl_report_in_use(ofs, "workdir");
1212 		if (err)
1213 			goto out;
1214 	}
1215 
1216 	err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1217 			     "workdir");
1218 	if (err)
1219 		goto out;
1220 
1221 	err = ovl_make_workdir(sb, ofs, &workpath);
1222 
1223 out:
1224 	path_put(&workpath);
1225 
1226 	return err;
1227 }
1228 
ovl_get_indexdir(struct super_block * sb,struct ovl_fs * ofs,struct ovl_entry * oe,struct path * upperpath)1229 static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1230 			    struct ovl_entry *oe, struct path *upperpath)
1231 {
1232 	struct vfsmount *mnt = ofs->upper_mnt;
1233 	int err;
1234 
1235 	err = mnt_want_write(mnt);
1236 	if (err)
1237 		return err;
1238 
1239 	/* Verify lower root is upper root origin */
1240 	err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
1241 				true);
1242 	if (err) {
1243 		pr_err("overlayfs: failed to verify upper root origin\n");
1244 		goto out;
1245 	}
1246 
1247 	ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1248 	if (ofs->indexdir) {
1249 		err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1250 				     "indexdir");
1251 		if (err)
1252 			goto out;
1253 
1254 		/*
1255 		 * Verify upper root is exclusively associated with index dir.
1256 		 * Older kernels stored upper fh in "trusted.overlay.origin"
1257 		 * xattr. If that xattr exists, verify that it is a match to
1258 		 * upper dir file handle. In any case, verify or set xattr
1259 		 * "trusted.overlay.upper" to indicate that index may have
1260 		 * directory entries.
1261 		 */
1262 		if (ovl_check_origin_xattr(ofs->indexdir)) {
1263 			err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1264 						upperpath->dentry, true, false);
1265 			if (err)
1266 				pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1267 		}
1268 		err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
1269 		if (err)
1270 			pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
1271 
1272 		/* Cleanup bad/stale/orphan index entries */
1273 		if (!err)
1274 			err = ovl_indexdir_cleanup(ofs);
1275 	}
1276 	if (err || !ofs->indexdir)
1277 		pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1278 
1279 out:
1280 	mnt_drop_write(mnt);
1281 	return err;
1282 }
1283 
ovl_lower_uuid_ok(struct ovl_fs * ofs,const uuid_t * uuid)1284 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1285 {
1286 	unsigned int i;
1287 
1288 	if (!ofs->config.nfs_export && !ofs->upper_mnt)
1289 		return true;
1290 
1291 	/*
1292 	 * We allow using single lower with null uuid for index and nfs_export
1293 	 * for example to support those features with single lower squashfs.
1294 	 * To avoid regressions in setups of overlay with re-formatted lower
1295 	 * squashfs, do not allow decoding origin with lower null uuid unless
1296 	 * user opted-in to one of the new features that require following the
1297 	 * lower inode of non-dir upper.
1298 	 */
1299 	if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
1300 	    uuid_is_null(uuid))
1301 		return false;
1302 
1303 	for (i = 0; i < ofs->numlowerfs; i++) {
1304 		/*
1305 		 * We use uuid to associate an overlay lower file handle with a
1306 		 * lower layer, so we can accept lower fs with null uuid as long
1307 		 * as all lower layers with null uuid are on the same fs.
1308 		 * if we detect multiple lower fs with the same uuid, we
1309 		 * disable lower file handle decoding on all of them.
1310 		 */
1311 		if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid)) {
1312 			ofs->lower_fs[i].bad_uuid = true;
1313 			return false;
1314 		}
1315 	}
1316 	return true;
1317 }
1318 
1319 /* Get a unique fsid for the layer */
ovl_get_fsid(struct ovl_fs * ofs,const struct path * path)1320 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1321 {
1322 	struct super_block *sb = path->mnt->mnt_sb;
1323 	unsigned int i;
1324 	dev_t dev;
1325 	int err;
1326 	bool bad_uuid = false;
1327 
1328 	/* fsid 0 is reserved for upper fs even with non upper overlay */
1329 	if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1330 		return 0;
1331 
1332 	for (i = 0; i < ofs->numlowerfs; i++) {
1333 		if (ofs->lower_fs[i].sb == sb)
1334 			return i + 1;
1335 	}
1336 
1337 	if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1338 		bad_uuid = true;
1339 		if (ofs->config.index || ofs->config.nfs_export) {
1340 			ofs->config.index = false;
1341 			ofs->config.nfs_export = false;
1342 			pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1343 				uuid_is_null(&sb->s_uuid) ? "null" :
1344 							    "conflicting",
1345 				path->dentry);
1346 		}
1347 	}
1348 
1349 	err = get_anon_bdev(&dev);
1350 	if (err) {
1351 		pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1352 		return err;
1353 	}
1354 
1355 	ofs->lower_fs[ofs->numlowerfs].sb = sb;
1356 	ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1357 	ofs->lower_fs[ofs->numlowerfs].bad_uuid = bad_uuid;
1358 	ofs->numlowerfs++;
1359 
1360 	return ofs->numlowerfs;
1361 }
1362 
ovl_get_lower_layers(struct super_block * sb,struct ovl_fs * ofs,struct path * stack,unsigned int numlower)1363 static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs,
1364 				struct path *stack, unsigned int numlower)
1365 {
1366 	int err;
1367 	unsigned int i;
1368 
1369 	err = -ENOMEM;
1370 	ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1371 				    GFP_KERNEL);
1372 	if (ofs->lower_layers == NULL)
1373 		goto out;
1374 
1375 	ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1376 				GFP_KERNEL);
1377 	if (ofs->lower_fs == NULL)
1378 		goto out;
1379 
1380 	for (i = 0; i < numlower; i++) {
1381 		struct vfsmount *mnt;
1382 		struct inode *trap;
1383 		int fsid;
1384 
1385 		err = fsid = ovl_get_fsid(ofs, &stack[i]);
1386 		if (err < 0)
1387 			goto out;
1388 
1389 		/*
1390 		 * Check if lower root conflicts with this overlay layers before
1391 		 * checking if it is in-use as upperdir/workdir of "another"
1392 		 * mount, because we do not bother to check in ovl_is_inuse() if
1393 		 * the upperdir/workdir is in fact in-use by our
1394 		 * upperdir/workdir.
1395 		 */
1396 		err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1397 		if (err)
1398 			goto out;
1399 
1400 		if (ovl_is_inuse(stack[i].dentry)) {
1401 			err = ovl_report_in_use(ofs, "lowerdir");
1402 			if (err) {
1403 				iput(trap);
1404 				goto out;
1405 			}
1406 		}
1407 
1408 		mnt = clone_private_mount(&stack[i]);
1409 		err = PTR_ERR(mnt);
1410 		if (IS_ERR(mnt)) {
1411 			pr_err("overlayfs: failed to clone lowerpath\n");
1412 			iput(trap);
1413 			goto out;
1414 		}
1415 
1416 		/*
1417 		 * Make lower layers R/O.  That way fchmod/fchown on lower file
1418 		 * will fail instead of modifying lower fs.
1419 		 */
1420 		mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1421 
1422 		ofs->lower_layers[ofs->numlower].trap = trap;
1423 		ofs->lower_layers[ofs->numlower].mnt = mnt;
1424 		ofs->lower_layers[ofs->numlower].idx = i + 1;
1425 		ofs->lower_layers[ofs->numlower].fsid = fsid;
1426 		if (fsid) {
1427 			ofs->lower_layers[ofs->numlower].fs =
1428 				&ofs->lower_fs[fsid - 1];
1429 		}
1430 		ofs->numlower++;
1431 	}
1432 
1433 	/*
1434 	 * When all layers on same fs, overlay can use real inode numbers.
1435 	 * With mount option "xino=on", mounter declares that there are enough
1436 	 * free high bits in underlying fs to hold the unique fsid.
1437 	 * If overlayfs does encounter underlying inodes using the high xino
1438 	 * bits reserved for fsid, it emits a warning and uses the original
1439 	 * inode number.
1440 	 */
1441 	if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
1442 		ofs->xino_bits = 0;
1443 		ofs->config.xino = OVL_XINO_OFF;
1444 	} else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1445 		/*
1446 		 * This is a roundup of number of bits needed for numlowerfs+1
1447 		 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1448 		 * upper fs even with non upper overlay.
1449 		 */
1450 		BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1451 		ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1452 	}
1453 
1454 	if (ofs->xino_bits) {
1455 		pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1456 			ofs->xino_bits);
1457 	}
1458 
1459 	err = 0;
1460 out:
1461 	return err;
1462 }
1463 
ovl_get_lowerstack(struct super_block * sb,struct ovl_fs * ofs)1464 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1465 					    struct ovl_fs *ofs)
1466 {
1467 	int err;
1468 	char *lowertmp, *lower;
1469 	struct path *stack = NULL;
1470 	unsigned int stacklen, numlower = 0, i;
1471 	bool remote = false;
1472 	struct ovl_entry *oe;
1473 
1474 	err = -ENOMEM;
1475 	lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1476 	if (!lowertmp)
1477 		goto out_err;
1478 
1479 	err = -EINVAL;
1480 	stacklen = ovl_split_lowerdirs(lowertmp);
1481 	if (stacklen > OVL_MAX_STACK) {
1482 		pr_err("overlayfs: too many lower directories, limit is %d\n",
1483 		       OVL_MAX_STACK);
1484 		goto out_err;
1485 	} else if (!ofs->config.upperdir && stacklen == 1) {
1486 		pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1487 		goto out_err;
1488 	} else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1489 		   ofs->config.redirect_follow) {
1490 		pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1491 		ofs->config.nfs_export = false;
1492 	}
1493 
1494 	err = -ENOMEM;
1495 	stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1496 	if (!stack)
1497 		goto out_err;
1498 
1499 	err = -EINVAL;
1500 	lower = lowertmp;
1501 	for (numlower = 0; numlower < stacklen; numlower++) {
1502 		err = ovl_lower_dir(lower, &stack[numlower], ofs,
1503 				    &sb->s_stack_depth, &remote);
1504 		if (err)
1505 			goto out_err;
1506 
1507 		lower = strchr(lower, '\0') + 1;
1508 	}
1509 
1510 	err = -EINVAL;
1511 	sb->s_stack_depth++;
1512 	if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1513 		pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1514 		goto out_err;
1515 	}
1516 
1517 	err = ovl_get_lower_layers(sb, ofs, stack, numlower);
1518 	if (err)
1519 		goto out_err;
1520 
1521 	err = -ENOMEM;
1522 	oe = ovl_alloc_entry(numlower);
1523 	if (!oe)
1524 		goto out_err;
1525 
1526 	for (i = 0; i < numlower; i++) {
1527 		oe->lowerstack[i].dentry = dget(stack[i].dentry);
1528 		oe->lowerstack[i].layer = &ofs->lower_layers[i];
1529 	}
1530 
1531 	if (remote)
1532 		sb->s_d_op = &ovl_reval_dentry_operations;
1533 	else
1534 		sb->s_d_op = &ovl_dentry_operations;
1535 
1536 out:
1537 	for (i = 0; i < numlower; i++)
1538 		path_put(&stack[i]);
1539 	kfree(stack);
1540 	kfree(lowertmp);
1541 
1542 	return oe;
1543 
1544 out_err:
1545 	oe = ERR_PTR(err);
1546 	goto out;
1547 }
1548 
1549 /*
1550  * Check if this layer root is a descendant of:
1551  * - another layer of this overlayfs instance
1552  * - upper/work dir of any overlayfs instance
1553  */
ovl_check_layer(struct super_block * sb,struct ovl_fs * ofs,struct dentry * dentry,const char * name,bool is_lower)1554 static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1555 			   struct dentry *dentry, const char *name,
1556 			   bool is_lower)
1557 {
1558 	struct dentry *next = dentry, *parent;
1559 	int err = 0;
1560 
1561 	if (!dentry)
1562 		return 0;
1563 
1564 	parent = dget_parent(next);
1565 
1566 	/* Walk back ancestors to root (inclusive) looking for traps */
1567 	while (!err && parent != next) {
1568 		if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
1569 			err = -ELOOP;
1570 			pr_err("overlayfs: overlapping %s path\n", name);
1571 		} else if (ovl_is_inuse(parent)) {
1572 			err = ovl_report_in_use(ofs, name);
1573 		}
1574 		next = parent;
1575 		parent = dget_parent(next);
1576 		dput(next);
1577 	}
1578 
1579 	dput(parent);
1580 
1581 	return err;
1582 }
1583 
1584 /*
1585  * Check if any of the layers or work dirs overlap.
1586  */
ovl_check_overlapping_layers(struct super_block * sb,struct ovl_fs * ofs)1587 static int ovl_check_overlapping_layers(struct super_block *sb,
1588 					struct ovl_fs *ofs)
1589 {
1590 	int i, err;
1591 
1592 	if (ofs->upper_mnt) {
1593 		err = ovl_check_layer(sb, ofs, ofs->upper_mnt->mnt_root,
1594 				      "upperdir", false);
1595 		if (err)
1596 			return err;
1597 
1598 		/*
1599 		 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1600 		 * this instance and covers overlapping work and index dirs,
1601 		 * unless work or index dir have been moved since created inside
1602 		 * workbasedir.  In that case, we already have their traps in
1603 		 * inode cache and we will catch that case on lookup.
1604 		 */
1605 		err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1606 				      false);
1607 		if (err)
1608 			return err;
1609 	}
1610 
1611 	for (i = 0; i < ofs->numlower; i++) {
1612 		err = ovl_check_layer(sb, ofs,
1613 				      ofs->lower_layers[i].mnt->mnt_root,
1614 				      "lowerdir", true);
1615 		if (err)
1616 			return err;
1617 	}
1618 
1619 	return 0;
1620 }
1621 
ovl_fill_super(struct super_block * sb,void * data,int silent)1622 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1623 {
1624 	struct path upperpath = { };
1625 	struct dentry *root_dentry;
1626 	struct ovl_entry *oe;
1627 	struct ovl_fs *ofs;
1628 	struct cred *cred;
1629 	int err;
1630 
1631 	err = -ENOMEM;
1632 	ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1633 	if (!ofs)
1634 		goto out;
1635 
1636 	ofs->creator_cred = cred = prepare_creds();
1637 	if (!cred)
1638 		goto out_err;
1639 
1640 	ofs->config.index = ovl_index_def;
1641 	ofs->config.nfs_export = ovl_nfs_export_def;
1642 	ofs->config.xino = ovl_xino_def();
1643 	ofs->config.metacopy = ovl_metacopy_def;
1644 	err = ovl_parse_opt((char *) data, &ofs->config);
1645 	if (err)
1646 		goto out_err;
1647 
1648 	err = -EINVAL;
1649 	if (!ofs->config.lowerdir) {
1650 		if (!silent)
1651 			pr_err("overlayfs: missing 'lowerdir'\n");
1652 		goto out_err;
1653 	}
1654 
1655 	sb->s_stack_depth = 0;
1656 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1657 	/* Assume underlaying fs uses 32bit inodes unless proven otherwise */
1658 	if (ofs->config.xino != OVL_XINO_OFF)
1659 		ofs->xino_bits = BITS_PER_LONG - 32;
1660 
1661 	/* alloc/destroy_inode needed for setting up traps in inode cache */
1662 	sb->s_op = &ovl_super_operations;
1663 
1664 	if (ofs->config.upperdir) {
1665 		if (!ofs->config.workdir) {
1666 			pr_err("overlayfs: missing 'workdir'\n");
1667 			goto out_err;
1668 		}
1669 
1670 		err = ovl_get_upper(sb, ofs, &upperpath);
1671 		if (err)
1672 			goto out_err;
1673 
1674 		err = ovl_get_workdir(sb, ofs, &upperpath);
1675 		if (err)
1676 			goto out_err;
1677 
1678 		if (!ofs->workdir)
1679 			sb->s_flags |= SB_RDONLY;
1680 
1681 		sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1682 		sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
1683 
1684 	}
1685 	oe = ovl_get_lowerstack(sb, ofs);
1686 	err = PTR_ERR(oe);
1687 	if (IS_ERR(oe))
1688 		goto out_err;
1689 
1690 	/* If the upper fs is nonexistent, we mark overlayfs r/o too */
1691 	if (!ofs->upper_mnt)
1692 		sb->s_flags |= SB_RDONLY;
1693 
1694 	if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1695 		err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
1696 		if (err)
1697 			goto out_free_oe;
1698 
1699 		/* Force r/o mount with no index dir */
1700 		if (!ofs->indexdir) {
1701 			dput(ofs->workdir);
1702 			ofs->workdir = NULL;
1703 			sb->s_flags |= SB_RDONLY;
1704 		}
1705 
1706 	}
1707 
1708 	err = ovl_check_overlapping_layers(sb, ofs);
1709 	if (err)
1710 		goto out_free_oe;
1711 
1712 	/* Show index=off in /proc/mounts for forced r/o mount */
1713 	if (!ofs->indexdir) {
1714 		ofs->config.index = false;
1715 		if (ofs->upper_mnt && ofs->config.nfs_export) {
1716 			pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1717 			ofs->config.nfs_export = false;
1718 		}
1719 	}
1720 
1721 	if (ofs->config.metacopy && ofs->config.nfs_export) {
1722 		pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1723 		ofs->config.nfs_export = false;
1724 	}
1725 
1726 	if (ofs->config.nfs_export)
1727 		sb->s_export_op = &ovl_export_operations;
1728 
1729 	/* Never override disk quota limits or use reserved space */
1730 	cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1731 
1732 	sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1733 	sb->s_xattr = ovl_xattr_handlers;
1734 	sb->s_fs_info = ofs;
1735 	sb->s_flags |= SB_POSIXACL;
1736 	sb->s_iflags |= SB_I_SKIP_SYNC;
1737 
1738 	err = -ENOMEM;
1739 	root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1740 	if (!root_dentry)
1741 		goto out_free_oe;
1742 
1743 	root_dentry->d_fsdata = oe;
1744 
1745 	mntput(upperpath.mnt);
1746 	if (upperpath.dentry) {
1747 		ovl_dentry_set_upper_alias(root_dentry);
1748 		if (ovl_is_impuredir(upperpath.dentry))
1749 			ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1750 	}
1751 
1752 	/* Root is always merge -> can have whiteouts */
1753 	ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
1754 	ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
1755 	ovl_set_upperdata(d_inode(root_dentry));
1756 	ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1757 		       ovl_dentry_lower(root_dentry), NULL);
1758 
1759 	sb->s_root = root_dentry;
1760 	return 0;
1761 
1762 out_free_oe:
1763 	ovl_entry_stack_free(oe);
1764 	kfree(oe);
1765 out_err:
1766 	path_put(&upperpath);
1767 	ovl_free_fs(ofs);
1768 out:
1769 	return err;
1770 }
1771 
ovl_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * raw_data)1772 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1773 				const char *dev_name, void *raw_data)
1774 {
1775 	return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1776 }
1777 
1778 static struct file_system_type ovl_fs_type = {
1779 	.owner		= THIS_MODULE,
1780 	.name		= "overlay",
1781 	.mount		= ovl_mount,
1782 	.kill_sb	= kill_anon_super,
1783 };
1784 MODULE_ALIAS_FS("overlay");
1785 
ovl_inode_init_once(void * foo)1786 static void ovl_inode_init_once(void *foo)
1787 {
1788 	struct ovl_inode *oi = foo;
1789 
1790 	inode_init_once(&oi->vfs_inode);
1791 }
1792 
ovl_init(void)1793 static int __init ovl_init(void)
1794 {
1795 	int err;
1796 
1797 	ovl_inode_cachep = kmem_cache_create("ovl_inode",
1798 					     sizeof(struct ovl_inode), 0,
1799 					     (SLAB_RECLAIM_ACCOUNT|
1800 					      SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1801 					     ovl_inode_init_once);
1802 	if (ovl_inode_cachep == NULL)
1803 		return -ENOMEM;
1804 
1805 	err = register_filesystem(&ovl_fs_type);
1806 	if (err)
1807 		kmem_cache_destroy(ovl_inode_cachep);
1808 
1809 	return err;
1810 }
1811 
ovl_exit(void)1812 static void __exit ovl_exit(void)
1813 {
1814 	unregister_filesystem(&ovl_fs_type);
1815 
1816 	/*
1817 	 * Make sure all delayed rcu free inodes are flushed before we
1818 	 * destroy cache.
1819 	 */
1820 	rcu_barrier();
1821 	kmem_cache_destroy(ovl_inode_cachep);
1822 
1823 }
1824 
1825 module_init(ovl_init);
1826 module_exit(ovl_exit);
1827