xref: /linux/fs/overlayfs/params.c (revision cbf4ccabcbfdda3da70c10b37e21e99cd2ee0946)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/fs.h>
4 #include <linux/module.h>
5 #include <linux/namei.h>
6 #include <linux/fs_context.h>
7 #include <linux/fs_parser.h>
8 #include <linux/posix_acl_xattr.h>
9 #include <linux/seq_file.h>
10 #include <linux/xattr.h>
11 #include "overlayfs.h"
12 #include "params.h"
13 
14 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
15 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
16 MODULE_PARM_DESC(redirect_dir,
17 		 "Default to on or off for the redirect_dir feature");
18 
19 static bool ovl_redirect_always_follow =
20 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
21 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
22 		   bool, 0644);
23 MODULE_PARM_DESC(redirect_always_follow,
24 		 "Follow redirects even if redirect_dir feature is turned off");
25 
26 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
27 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
28 MODULE_PARM_DESC(xino_auto,
29 		 "Auto enable xino feature");
30 
31 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
32 module_param_named(index, ovl_index_def, bool, 0644);
33 MODULE_PARM_DESC(index,
34 		 "Default to on or off for the inodes index feature");
35 
36 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
37 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
38 MODULE_PARM_DESC(nfs_export,
39 		 "Default to on or off for the NFS export feature");
40 
41 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
42 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
43 MODULE_PARM_DESC(metacopy,
44 		 "Default to on or off for the metadata only copy up feature");
45 
46 enum ovl_opt {
47 	Opt_lowerdir,
48 	Opt_lowerdir_add,
49 	Opt_datadir_add,
50 	Opt_upperdir,
51 	Opt_workdir,
52 	Opt_default_permissions,
53 	Opt_redirect_dir,
54 	Opt_index,
55 	Opt_uuid,
56 	Opt_nfs_export,
57 	Opt_userxattr,
58 	Opt_xino,
59 	Opt_metacopy,
60 	Opt_verity,
61 	Opt_fsync,
62 	Opt_volatile,
63 	Opt_override_creds,
64 };
65 
66 static const struct constant_table ovl_parameter_bool[] = {
67 	{ "on",		true  },
68 	{ "off",	false },
69 	{}
70 };
71 
72 static const struct constant_table ovl_parameter_uuid[] = {
73 	{ "off",	OVL_UUID_OFF  },
74 	{ "null",	OVL_UUID_NULL },
75 	{ "auto",	OVL_UUID_AUTO },
76 	{ "on",		OVL_UUID_ON   },
77 	{}
78 };
79 
80 static const char *ovl_uuid_mode(struct ovl_config *config)
81 {
82 	return ovl_parameter_uuid[config->uuid].name;
83 }
84 
85 static int ovl_uuid_def(void)
86 {
87 	return OVL_UUID_AUTO;
88 }
89 
90 static const struct constant_table ovl_parameter_xino[] = {
91 	{ "off",	OVL_XINO_OFF  },
92 	{ "auto",	OVL_XINO_AUTO },
93 	{ "on",		OVL_XINO_ON   },
94 	{}
95 };
96 
97 const char *ovl_xino_mode(struct ovl_config *config)
98 {
99 	return ovl_parameter_xino[config->xino].name;
100 }
101 
102 static int ovl_xino_def(void)
103 {
104 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
105 }
106 
107 const struct constant_table ovl_parameter_redirect_dir[] = {
108 	{ "off",	OVL_REDIRECT_OFF      },
109 	{ "follow",	OVL_REDIRECT_FOLLOW   },
110 	{ "followdata",	OVL_REDIRECT_FOLLOWDATA },
111 	{ "nofollow",	OVL_REDIRECT_NOFOLLOW },
112 	{ "on",		OVL_REDIRECT_ON       },
113 	{}
114 };
115 
116 static const char *ovl_redirect_mode_name(int redirect_mode)
117 {
118 	return ovl_parameter_redirect_dir[redirect_mode].name;
119 }
120 
121 const char *ovl_redirect_mode(struct ovl_config *config)
122 {
123 	return ovl_redirect_mode_name(config->redirect_mode);
124 }
125 
126 int ovl_redirect_follow_mode(bool follow_lower, size_t nr_data)
127 {
128 	if (ovl_redirect_always_follow && follow_lower)
129 		return OVL_REDIRECT_FOLLOW;
130 
131 	/* Data-only layers cannot be used without following redirects to them */
132 	return nr_data ? OVL_REDIRECT_FOLLOWDATA : OVL_REDIRECT_NOFOLLOW;
133 }
134 
135 /*
136  * The default for show_options() is resolved as if there are no data-only layers,
137  * so that redirect_dir=followdata, which is auto selected with data-only layers,
138  * is displayed in mount options.
139  */
140 static int ovl_redirect_mode_def(void)
141 {
142 	return ovl_redirect_dir_def ? OVL_REDIRECT_ON :
143 				      ovl_redirect_follow_mode(true, 0);
144 }
145 
146 static const struct constant_table ovl_parameter_verity[] = {
147 	{ "off",	OVL_VERITY_OFF     },
148 	{ "on",		OVL_VERITY_ON      },
149 	{ "require",	OVL_VERITY_REQUIRE },
150 	{}
151 };
152 
153 static const char *ovl_verity_mode(struct ovl_config *config)
154 {
155 	return ovl_parameter_verity[config->verity_mode].name;
156 }
157 
158 static int ovl_verity_mode_def(void)
159 {
160 	return OVL_VERITY_OFF;
161 }
162 
163 static const struct constant_table ovl_parameter_fsync[] = {
164 	{ "volatile",	OVL_FSYNC_VOLATILE },
165 	{ "auto",	OVL_FSYNC_AUTO     },
166 	{ "strict",	OVL_FSYNC_STRICT   },
167 	{}
168 };
169 
170 static const char *ovl_fsync_mode(struct ovl_config *config)
171 {
172 	return ovl_parameter_fsync[config->fsync_mode].name;
173 }
174 
175 static int ovl_fsync_mode_def(void)
176 {
177 	return OVL_FSYNC_AUTO;
178 }
179 
180 const struct fs_parameter_spec ovl_parameter_spec[] = {
181 	fsparam_string_empty("lowerdir",    Opt_lowerdir),
182 	fsparam_file_or_string("lowerdir+", Opt_lowerdir_add),
183 	fsparam_file_or_string("datadir+",  Opt_datadir_add),
184 	fsparam_file_or_string("upperdir",  Opt_upperdir),
185 	fsparam_file_or_string("workdir",   Opt_workdir),
186 	fsparam_flag("default_permissions", Opt_default_permissions),
187 	fsparam_enum("redirect_dir",        Opt_redirect_dir, ovl_parameter_redirect_dir),
188 	fsparam_enum("index",               Opt_index, ovl_parameter_bool),
189 	fsparam_enum("uuid",                Opt_uuid, ovl_parameter_uuid),
190 	fsparam_enum("nfs_export",          Opt_nfs_export, ovl_parameter_bool),
191 	fsparam_flag("userxattr",           Opt_userxattr),
192 	fsparam_enum("xino",                Opt_xino, ovl_parameter_xino),
193 	fsparam_enum("metacopy",            Opt_metacopy, ovl_parameter_bool),
194 	fsparam_enum("verity",              Opt_verity, ovl_parameter_verity),
195 	fsparam_enum("fsync",               Opt_fsync, ovl_parameter_fsync),
196 	fsparam_flag("volatile",            Opt_volatile),
197 	fsparam_flag_no("override_creds",   Opt_override_creds),
198 	{}
199 };
200 
201 static char *ovl_next_opt(char **s)
202 {
203 	char *sbegin = *s;
204 	char *p;
205 
206 	if (sbegin == NULL)
207 		return NULL;
208 
209 	for (p = sbegin; *p; p++) {
210 		if (*p == '\\') {
211 			p++;
212 			if (!*p)
213 				break;
214 		} else if (*p == ',') {
215 			*p = '\0';
216 			*s = p + 1;
217 			return sbegin;
218 		}
219 	}
220 	*s = NULL;
221 	return sbegin;
222 }
223 
224 static int ovl_parse_monolithic(struct fs_context *fc, void *data)
225 {
226 	return vfs_parse_monolithic_sep(fc, data, ovl_next_opt);
227 }
228 
229 static ssize_t ovl_parse_param_split_lowerdirs(char *str)
230 {
231 	ssize_t nr_layers = 1, nr_colons = 0;
232 	char *s, *d;
233 
234 	for (s = d = str;; s++, d++) {
235 		if (*s == '\\') {
236 			/* keep esc chars in split lowerdir */
237 			*d++ = *s++;
238 			/* A trailing backslash escapes nothing. */
239 			if (!*s)
240 				return -EINVAL;
241 		} else if (*s == ':') {
242 			bool next_colon = (*(s + 1) == ':');
243 
244 			nr_colons++;
245 			if (nr_colons == 2 && next_colon) {
246 				pr_err("only single ':' or double '::' sequences of unescaped colons in lowerdir mount option allowed.\n");
247 				return -EINVAL;
248 			}
249 			/* count layers, not colons */
250 			if (!next_colon)
251 				nr_layers++;
252 
253 			*d = '\0';
254 			continue;
255 		}
256 
257 		*d = *s;
258 		if (!*s) {
259 			/* trailing colons */
260 			if (nr_colons) {
261 				pr_err("unescaped trailing colons in lowerdir mount option.\n");
262 				return -EINVAL;
263 			}
264 			break;
265 		}
266 		nr_colons = 0;
267 	}
268 
269 	return nr_layers;
270 }
271 
272 static int ovl_mount_dir_noesc(const char *name, struct path *path)
273 {
274 	int err = -EINVAL;
275 
276 	if (!*name) {
277 		pr_err("empty lowerdir\n");
278 		goto out;
279 	}
280 	err = kern_path(name, LOOKUP_FOLLOW, path);
281 	if (err) {
282 		pr_err("failed to resolve '%s': %i\n", name, err);
283 		goto out;
284 	}
285 	return 0;
286 
287 out:
288 	return err;
289 }
290 
291 static void ovl_unescape(char *s)
292 {
293 	char *d = s;
294 
295 	for (;; s++, d++) {
296 		if (*s == '\\')
297 			s++;
298 		*d = *s;
299 		if (!*s)
300 			break;
301 	}
302 }
303 
304 static int ovl_mount_dir(const char *name, struct path *path)
305 {
306 	int err = -ENOMEM;
307 	char *tmp = kstrdup(name, GFP_KERNEL);
308 
309 	if (tmp) {
310 		ovl_unescape(tmp);
311 		err = ovl_mount_dir_noesc(tmp, path);
312 		kfree(tmp);
313 	}
314 	return err;
315 }
316 
317 static int ovl_mount_dir_check(struct fs_context *fc, const struct path *path,
318 			       enum ovl_opt layer, const char *name, bool upper)
319 {
320 	bool is_casefolded = ovl_dentry_casefolded(path->dentry);
321 	struct ovl_fs_context *ctx = fc->fs_private;
322 	struct ovl_fs *ofs = fc->s_fs_info;
323 
324 	if (!d_is_dir(path->dentry))
325 		return invalfc(fc, "%s is not a directory", name);
326 
327 	/*
328 	 * Allow filesystems that are case-folding capable but deny composing
329 	 * ovl stack from inconsistent case-folded directories.
330 	 */
331 	if (!ctx->casefold_set) {
332 		ofs->casefold = is_casefolded;
333 		ctx->casefold_set = true;
334 	}
335 
336 	if (ofs->casefold != is_casefolded) {
337 		return invalfc(fc, "case-%ssensitive directory on %s is inconsistent",
338 			       is_casefolded ? "in" : "", name);
339 	}
340 
341 	if (ovl_dentry_weird(path->dentry))
342 		return invalfc(fc, "filesystem on %s not supported", name);
343 
344 	/*
345 	 * Check whether upper path is read-only here to report failures
346 	 * early. Don't forget to recheck when the superblock is created
347 	 * as the mount attributes could change.
348 	 */
349 	if (upper) {
350 		if (path->dentry->d_flags & DCACHE_OP_REAL)
351 			return invalfc(fc, "filesystem on %s not supported as upperdir", name);
352 		if (__mnt_is_readonly(path->mnt))
353 			return invalfc(fc, "filesystem on %s is read-only", name);
354 	} else {
355 		if (ctx->lowerdir_all && layer != Opt_lowerdir)
356 			return invalfc(fc, "lowerdir+ and datadir+ cannot follow lowerdir");
357 		if (ctx->nr_data && layer == Opt_lowerdir_add)
358 			return invalfc(fc, "regular lower layers cannot follow data layers");
359 		if (ctx->nr == OVL_MAX_STACK)
360 			return invalfc(fc, "too many lower directories, limit is %d",
361 				       OVL_MAX_STACK);
362 	}
363 	return 0;
364 }
365 
366 static int ovl_ctx_realloc_lower(struct fs_context *fc)
367 {
368 	struct ovl_fs_context *ctx = fc->fs_private;
369 	struct ovl_fs_context_layer *l;
370 	size_t nr;
371 
372 	if (ctx->nr < ctx->capacity)
373 		return 0;
374 
375 	nr = min_t(size_t, max(4096 / sizeof(*l), ctx->capacity * 2),
376 		   OVL_MAX_STACK);
377 	l = krealloc_array(ctx->lower, nr, sizeof(*l), GFP_KERNEL_ACCOUNT);
378 	if (!l)
379 		return -ENOMEM;
380 
381 	ctx->lower = l;
382 	ctx->capacity = nr;
383 	return 0;
384 }
385 
386 static void ovl_add_layer(struct fs_context *fc, enum ovl_opt layer,
387 			 struct path *path, char **pname)
388 {
389 	struct ovl_fs *ofs = fc->s_fs_info;
390 	struct ovl_config *config = &ofs->config;
391 	struct ovl_fs_context *ctx = fc->fs_private;
392 	struct ovl_fs_context_layer *l;
393 
394 	switch (layer) {
395 	case Opt_workdir:
396 		swap(config->workdir, *pname);
397 		swap(ctx->work, *path);
398 		break;
399 	case Opt_upperdir:
400 		swap(config->upperdir, *pname);
401 		swap(ctx->upper, *path);
402 		break;
403 	case Opt_datadir_add:
404 		ctx->nr_data++;
405 		fallthrough;
406 	case Opt_lowerdir:
407 		fallthrough;
408 	case Opt_lowerdir_add:
409 		WARN_ON(ctx->nr >= ctx->capacity);
410 		l = &ctx->lower[ctx->nr++];
411 		memset(l, 0, sizeof(*l));
412 		swap(l->name, *pname);
413 		swap(l->path, *path);
414 		break;
415 	default:
416 		WARN_ON(1);
417 	}
418 }
419 
420 static inline bool is_upper_layer(enum ovl_opt layer)
421 {
422 	return layer == Opt_upperdir || layer == Opt_workdir;
423 }
424 
425 /* Handle non-file descriptor-based layer options that require path lookup. */
426 static inline int ovl_kern_path(const char *layer_name, struct path *layer_path,
427 				enum ovl_opt layer)
428 {
429 	int err;
430 
431 	switch (layer) {
432 	case Opt_upperdir:
433 		fallthrough;
434 	case Opt_workdir:
435 		fallthrough;
436 	case Opt_lowerdir:
437 		err = ovl_mount_dir(layer_name, layer_path);
438 		break;
439 	case Opt_lowerdir_add:
440 		fallthrough;
441 	case Opt_datadir_add:
442 		err = ovl_mount_dir_noesc(layer_name, layer_path);
443 		break;
444 	default:
445 		WARN_ON_ONCE(true);
446 		err = -EINVAL;
447 	}
448 
449 	return err;
450 }
451 
452 static int ovl_do_parse_layer(struct fs_context *fc, const char *layer_name,
453 			      struct path *layer_path, enum ovl_opt layer)
454 {
455 	char *name __free(kfree) = kstrdup(layer_name, GFP_KERNEL);
456 	bool upper;
457 	int err = 0;
458 
459 	if (!name)
460 		return -ENOMEM;
461 
462 	upper = is_upper_layer(layer);
463 	err = ovl_mount_dir_check(fc, layer_path, layer, name, upper);
464 	if (err)
465 		return err;
466 
467 	if (!upper) {
468 		err = ovl_ctx_realloc_lower(fc);
469 		if (err)
470 			return err;
471 	}
472 
473 	/* Store the user provided path string in ctx to show in mountinfo */
474 	ovl_add_layer(fc, layer, layer_path, &name);
475 	return err;
476 }
477 
478 static int ovl_parse_layer(struct fs_context *fc, struct fs_parameter *param,
479 			   enum ovl_opt layer)
480 {
481 	struct path layer_path __free(path_put) = {};
482 	int err = 0;
483 
484 	switch (param->type) {
485 	case fs_value_is_string:
486 		err = ovl_kern_path(param->string, &layer_path, layer);
487 		if (err)
488 			return err;
489 		err = ovl_do_parse_layer(fc, param->string, &layer_path, layer);
490 		break;
491 	case fs_value_is_file: {
492 		char *buf __free(kfree);
493 		char *layer_name;
494 
495 		buf = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT);
496 		if (!buf)
497 			return -ENOMEM;
498 
499 		layer_path = param->file->f_path;
500 		path_get(&layer_path);
501 
502 		layer_name = d_path(&layer_path, buf, PATH_MAX);
503 		if (IS_ERR(layer_name))
504 			return PTR_ERR(layer_name);
505 
506 		err = ovl_do_parse_layer(fc, layer_name, &layer_path, layer);
507 		break;
508 	}
509 	default:
510 		WARN_ON_ONCE(true);
511 		err = -EINVAL;
512 	}
513 
514 	return err;
515 }
516 
517 static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx)
518 {
519 	struct ovl_fs_context_layer *l = ctx->lower;
520 
521 	// Reset old user provided lowerdir string
522 	kfree(ctx->lowerdir_all);
523 	ctx->lowerdir_all = NULL;
524 
525 	for (size_t nr = 0; nr < ctx->nr; nr++, l++) {
526 		path_put(&l->path);
527 		kfree(l->name);
528 		l->name = NULL;
529 	}
530 	ctx->nr = 0;
531 	ctx->nr_data = 0;
532 }
533 
534 /*
535  * Parse lowerdir= mount option:
536  *
537  * e.g.: lowerdir=/lower1:/lower2:/lower3::/data1::/data2
538  *     Set "/lower1", "/lower2", and "/lower3" as lower layers and
539  *     "/data1" and "/data2" as data lower layers. Any existing lower
540  *     layers are replaced.
541  */
542 static int ovl_parse_param_lowerdir(const char *name, struct fs_context *fc)
543 {
544 	int err;
545 	struct ovl_fs_context *ctx = fc->fs_private;
546 	char *dup = NULL, *iter;
547 	ssize_t nr_lower, nr;
548 	bool data_layer = false;
549 
550 	/*
551 	 * Ensure we're backwards compatible with mount(2)
552 	 * by allowing relative paths.
553 	 */
554 
555 	/* drop all existing lower layers */
556 	ovl_reset_lowerdirs(ctx);
557 
558 	if (!*name)
559 		return 0;
560 
561 	if (*name == ':') {
562 		pr_err("cannot append lower layer\n");
563 		return -EINVAL;
564 	}
565 
566 	// Store user provided lowerdir string to show in mount options
567 	ctx->lowerdir_all = kstrdup(name, GFP_KERNEL);
568 	if (!ctx->lowerdir_all)
569 		return -ENOMEM;
570 
571 	dup = kstrdup(name, GFP_KERNEL);
572 	if (!dup)
573 		return -ENOMEM;
574 
575 	err = -EINVAL;
576 	nr_lower = ovl_parse_param_split_lowerdirs(dup);
577 	if (nr_lower < 0)
578 		goto out_err;
579 
580 	if (nr_lower > OVL_MAX_STACK) {
581 		pr_err("too many lower directories, limit is %d\n", OVL_MAX_STACK);
582 		goto out_err;
583 	}
584 
585 	iter = dup;
586 	for (nr = 0; nr < nr_lower; nr++) {
587 		struct path path __free(path_put) = {};
588 
589 		err = ovl_kern_path(iter, &path, Opt_lowerdir);
590 		if (err)
591 			goto out_err;
592 
593 		err = ovl_do_parse_layer(fc, iter, &path, Opt_lowerdir);
594 		if (err)
595 			goto out_err;
596 
597 		if (data_layer)
598 			ctx->nr_data++;
599 
600 		/* Calling strchr() again would overrun. */
601 		if (ctx->nr == nr_lower)
602 			break;
603 
604 		err = -EINVAL;
605 		iter = strchr(iter, '\0') + 1;
606 		if (*iter) {
607 			/*
608 			 * This is a regular layer so we require that
609 			 * there are no data layers.
610 			 */
611 			if (ctx->nr_data > 0) {
612 				pr_err("regular lower layers cannot follow data lower layers\n");
613 				goto out_err;
614 			}
615 
616 			data_layer = false;
617 			continue;
618 		}
619 
620 		/* This is a data lower layer. */
621 		data_layer = true;
622 		iter++;
623 	}
624 	kfree(dup);
625 	return 0;
626 
627 out_err:
628 	kfree(dup);
629 
630 	/* Intentionally don't realloc to a smaller size. */
631 	return err;
632 }
633 
634 static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param)
635 {
636 	int err = 0;
637 	struct fs_parse_result result;
638 	struct ovl_fs *ofs = fc->s_fs_info;
639 	struct ovl_config *config = &ofs->config;
640 	struct ovl_fs_context *ctx = fc->fs_private;
641 	int opt;
642 
643 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
644 		/*
645 		 * On remount overlayfs has always ignored all mount
646 		 * options no matter if malformed or not so for
647 		 * backwards compatibility we do the same here.
648 		 */
649 		if (fc->oldapi)
650 			return 0;
651 
652 		/*
653 		 * Give us the freedom to allow changing mount options
654 		 * with the new mount api in the future. So instead of
655 		 * silently ignoring everything we report a proper
656 		 * error. This is only visible for users of the new
657 		 * mount api.
658 		 */
659 		return invalfc(fc, "No changes allowed in reconfigure");
660 	}
661 
662 	opt = fs_parse(fc, ovl_parameter_spec, param, &result);
663 	if (opt < 0)
664 		return opt;
665 
666 	switch (opt) {
667 	case Opt_lowerdir:
668 		err = ovl_parse_param_lowerdir(param->string, fc);
669 		break;
670 	case Opt_lowerdir_add:
671 	case Opt_datadir_add:
672 	case Opt_upperdir:
673 	case Opt_workdir:
674 		err = ovl_parse_layer(fc, param, opt);
675 		break;
676 	case Opt_default_permissions:
677 		config->default_permissions = true;
678 		break;
679 	case Opt_redirect_dir:
680 		config->redirect_mode = result.uint_32;
681 		ctx->set.redirect = true;
682 		break;
683 	case Opt_index:
684 		config->index = result.uint_32;
685 		ctx->set.index = true;
686 		break;
687 	case Opt_uuid:
688 		config->uuid = result.uint_32;
689 		break;
690 	case Opt_nfs_export:
691 		config->nfs_export = result.uint_32;
692 		ctx->set.nfs_export = true;
693 		break;
694 	case Opt_xino:
695 		config->xino = result.uint_32;
696 		break;
697 	case Opt_metacopy:
698 		config->metacopy = result.uint_32;
699 		ctx->set.metacopy = true;
700 		break;
701 	case Opt_verity:
702 		config->verity_mode = result.uint_32;
703 		break;
704 	case Opt_fsync:
705 		config->fsync_mode = result.uint_32;
706 		break;
707 	case Opt_volatile:
708 		config->fsync_mode = OVL_FSYNC_VOLATILE;
709 		break;
710 	case Opt_userxattr:
711 		config->userxattr = true;
712 		break;
713 	case Opt_override_creds: {
714 		const struct cred *cred = NULL;
715 
716 		if (result.negated) {
717 			swap(cred, ofs->creator_cred);
718 			put_cred(cred);
719 			break;
720 		}
721 
722 		if (!current_in_userns(fc->user_ns)) {
723 			err = -EINVAL;
724 			break;
725 		}
726 
727 		cred = prepare_creds();
728 		if (cred)
729 			swap(cred, ofs->creator_cred);
730 		else
731 			err = -ENOMEM;
732 
733 		put_cred(cred);
734 		break;
735 	}
736 	default:
737 		pr_err("unrecognized mount option \"%s\" or missing value\n",
738 		       param->key);
739 		return -EINVAL;
740 	}
741 
742 	return err;
743 }
744 
745 static int ovl_get_tree(struct fs_context *fc)
746 {
747 	return get_tree_nodev(fc, ovl_fill_super);
748 }
749 
750 static inline void ovl_fs_context_free(struct ovl_fs_context *ctx)
751 {
752 	ovl_reset_lowerdirs(ctx);
753 	path_put(&ctx->upper);
754 	path_put(&ctx->work);
755 	kfree(ctx->lower);
756 	kfree(ctx);
757 }
758 
759 static void ovl_free(struct fs_context *fc)
760 {
761 	struct ovl_fs *ofs = fc->s_fs_info;
762 	struct ovl_fs_context *ctx = fc->fs_private;
763 
764 	/*
765 	 * ofs is stored in the fs_context when it is initialized.
766 	 * ofs is transferred to the superblock on a successful mount,
767 	 * but if an error occurs before the transfer we have to free
768 	 * it here.
769 	 */
770 	if (ofs)
771 		ovl_free_fs(ofs);
772 
773 	if (ctx)
774 		ovl_fs_context_free(ctx);
775 }
776 
777 static int ovl_reconfigure(struct fs_context *fc)
778 {
779 	struct super_block *sb = fc->root->d_sb;
780 	struct ovl_fs *ofs = OVL_FS(sb);
781 	struct super_block *upper_sb;
782 	int ret = 0;
783 
784 	if (!(fc->sb_flags & SB_RDONLY) && ovl_force_readonly(ofs))
785 		return -EROFS;
786 
787 	if (fc->sb_flags & SB_RDONLY && !sb_rdonly(sb)) {
788 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
789 		if (ovl_should_sync(ofs)) {
790 			down_read(&upper_sb->s_umount);
791 			ret = sync_filesystem(upper_sb);
792 			up_read(&upper_sb->s_umount);
793 		}
794 	}
795 
796 	return ret;
797 }
798 
799 static const struct fs_context_operations ovl_context_ops = {
800 	.parse_monolithic = ovl_parse_monolithic,
801 	.parse_param = ovl_parse_param,
802 	.get_tree    = ovl_get_tree,
803 	.reconfigure = ovl_reconfigure,
804 	.free        = ovl_free,
805 };
806 
807 /*
808  * This is called during fsopen() and will record the user namespace of
809  * the caller in fc->user_ns since we've raised FS_USERNS_MOUNT. We'll
810  * need it when we actually create the superblock to verify that the
811  * process creating the superblock is in the same user namespace as
812  * process that called fsopen().
813  */
814 int ovl_init_fs_context(struct fs_context *fc)
815 {
816 	struct ovl_fs_context *ctx;
817 	struct ovl_fs *ofs;
818 
819 	ctx = kzalloc_obj(*ctx, GFP_KERNEL_ACCOUNT);
820 	if (!ctx)
821 		return -ENOMEM;
822 
823 	/*
824 	 * By default we allocate for three lower layers. It's likely
825 	 * that it'll cover most users.
826 	 */
827 	ctx->lower = kmalloc_objs(*ctx->lower, 3, GFP_KERNEL_ACCOUNT);
828 	if (!ctx->lower)
829 		goto out_err;
830 	ctx->capacity = 3;
831 
832 	ofs = kzalloc_obj(struct ovl_fs);
833 	if (!ofs)
834 		goto out_err;
835 
836 	/*
837 	 * The module default determines only if new redirects are created in
838 	 * upper dir.  If they are not, the redirect follow mode will be
839 	 * determined after parsing mount options.
840 	 */
841 	ofs->config.redirect_mode	= ovl_redirect_dir_def ?
842 					  OVL_REDIRECT_ON : OVL_REDIRECT_OFF;
843 	ofs->config.index		= ovl_index_def;
844 	ofs->config.uuid		= ovl_uuid_def();
845 	ofs->config.nfs_export		= ovl_nfs_export_def;
846 	ofs->config.xino		= ovl_xino_def();
847 	ofs->config.metacopy		= ovl_metacopy_def;
848 	ofs->config.fsync_mode		= ovl_fsync_mode_def();
849 
850 	fc->s_fs_info		= ofs;
851 	fc->fs_private		= ctx;
852 	fc->ops			= &ovl_context_ops;
853 
854 	mutex_init(&ofs->whiteout_lock);
855 	return 0;
856 
857 out_err:
858 	ovl_fs_context_free(ctx);
859 	return -ENOMEM;
860 
861 }
862 
863 void ovl_free_fs(struct ovl_fs *ofs)
864 {
865 	struct vfsmount **mounts;
866 	unsigned i;
867 
868 	ovl_inuse_unlock_all(ofs);
869 	iput(ofs->workbasedir_trap);
870 	iput(ofs->workdir_trap);
871 	dput(ofs->whiteout);
872 	dput(ofs->workdir);
873 	dput(ofs->workbasedir);
874 
875 	/* Reuse ofs->config.lowerdirs as a vfsmount array before freeing it */
876 	mounts = (struct vfsmount **) ofs->config.lowerdirs;
877 	for (i = 0; i < ofs->numlayer; i++) {
878 		iput(ofs->layers[i].trap);
879 		kfree(ofs->config.lowerdirs[i]);
880 		mounts[i] = ofs->layers[i].mnt;
881 	}
882 	kern_unmount_array(mounts, ofs->numlayer);
883 	kfree(ofs->layers);
884 	for (i = 0; i < ofs->numfs; i++)
885 		free_anon_bdev(ofs->fs[i].pseudo_dev);
886 	kfree(ofs->fs);
887 
888 	kfree(ofs->config.lowerdirs);
889 	kfree(ofs->config.upperdir);
890 	kfree(ofs->config.workdir);
891 	if (ofs->creator_cred)
892 		put_cred(ofs->creator_cred);
893 	kfree(ofs);
894 }
895 
896 int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
897 			 struct ovl_config *config)
898 {
899 	struct ovl_opt_set set = ctx->set;
900 	/* The requested redirect_dir=, for reporting conflicts with it */
901 	int redirect_opt = config->redirect_mode;
902 	/* The redirect follow mode for redirect_dir=off and for the default */
903 	int follow_mode = ovl_redirect_follow_mode(!config->userxattr,
904 						   ctx->nr_data);
905 
906 	/* Workdir/index are useless in non-upper mount */
907 	if (!config->upperdir) {
908 		if (config->workdir) {
909 			pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
910 				config->workdir);
911 			kfree(config->workdir);
912 			config->workdir = NULL;
913 		}
914 		if (config->index && set.index) {
915 			pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
916 			set.index = false;
917 		}
918 		config->index = false;
919 	}
920 
921 	if (!config->upperdir && ovl_is_volatile(config)) {
922 		pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
923 		config->fsync_mode = ovl_fsync_mode_def();
924 	}
925 
926 	if (!config->upperdir && config->uuid == OVL_UUID_ON) {
927 		pr_info("option \"uuid=on\" requires an upper fs, falling back to uuid=null.\n");
928 		config->uuid = OVL_UUID_NULL;
929 	}
930 
931 	/*
932 	 * Resolve redirect_dir=off to the default redirect follow mode.
933 	 * With userxattr, redirects are not followed to lower by default.
934 	 */
935 	if (config->redirect_mode == OVL_REDIRECT_OFF)
936 		config->redirect_mode = follow_mode;
937 
938 	/*
939 	 * This is to make the logic below simpler.  It doesn't make any other
940 	 * difference, since redirect_dir=on is only used for upper.
941 	 */
942 	if (!config->upperdir && config->redirect_mode == OVL_REDIRECT_FOLLOW)
943 		config->redirect_mode = OVL_REDIRECT_ON;
944 
945 	/* metacopy -> redirect_dir dependency */
946 	if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON) {
947 		if (set.metacopy && set.redirect) {
948 			pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
949 			       ovl_redirect_mode_name(redirect_opt));
950 			return -EINVAL;
951 		}
952 		if (set.redirect) {
953 			/*
954 			 * There was an explicit redirect_dir=... that resulted
955 			 * in this conflict.
956 			 */
957 			pr_info("disabling metacopy due to redirect_dir=%s\n",
958 				ovl_redirect_mode_name(redirect_opt));
959 			config->metacopy = false;
960 		} else {
961 			/* Automatically enable redirect otherwise. */
962 			config->redirect_mode = OVL_REDIRECT_ON;
963 		}
964 	}
965 
966 	/* Resolve nfs_export -> !data-only layers dependency */
967 	if (config->nfs_export && ctx->nr_data) {
968 		if (set.nfs_export) {
969 			pr_err("NFS export is not supported with data-only layers\n");
970 			return -EINVAL;
971 		}
972 		pr_info("disabling nfs_export due to data-only layers\n");
973 		config->nfs_export = false;
974 	}
975 
976 	/* Resolve nfs_export -> index dependency */
977 	if (config->nfs_export && !config->index) {
978 		if (!config->upperdir &&
979 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
980 			pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
981 			config->nfs_export = false;
982 		} else if (set.nfs_export && set.index) {
983 			pr_err("conflicting options: nfs_export=on,index=off\n");
984 			return -EINVAL;
985 		} else if (set.index) {
986 			/*
987 			 * There was an explicit index=off that resulted
988 			 * in this conflict.
989 			 */
990 			pr_info("disabling nfs_export due to index=off\n");
991 			config->nfs_export = false;
992 		} else {
993 			/* Automatically enable index otherwise. */
994 			config->index = true;
995 		}
996 	}
997 
998 	/* Resolve nfs_export -> !metacopy && !verity dependency */
999 	if (config->nfs_export && config->metacopy) {
1000 		if (set.nfs_export && set.metacopy) {
1001 			pr_err("conflicting options: nfs_export=on,metacopy=on\n");
1002 			return -EINVAL;
1003 		}
1004 		if (set.metacopy) {
1005 			/*
1006 			 * There was an explicit metacopy=on that resulted
1007 			 * in this conflict.
1008 			 */
1009 			pr_info("disabling nfs_export due to metacopy=on\n");
1010 			config->nfs_export = false;
1011 		} else if (config->verity_mode) {
1012 			/*
1013 			 * There was an explicit verity=.. that resulted
1014 			 * in this conflict.
1015 			 */
1016 			pr_info("disabling nfs_export due to verity=%s\n",
1017 				ovl_verity_mode(config));
1018 			config->nfs_export = false;
1019 		} else {
1020 			/*
1021 			 * There was an explicit nfs_export=on that resulted
1022 			 * in this conflict.
1023 			 */
1024 			pr_info("disabling metacopy due to nfs_export=on\n");
1025 			config->metacopy = false;
1026 		}
1027 	}
1028 
1029 
1030 	/* Resolve userxattr -> !redirect && !metacopy dependency */
1031 	if (config->userxattr) {
1032 		if (set.redirect &&
1033 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW &&
1034 		    config->redirect_mode != OVL_REDIRECT_FOLLOWDATA) {
1035 			pr_err("conflicting options: userxattr,redirect_dir=%s\n",
1036 			       ovl_redirect_mode_name(redirect_opt));
1037 			return -EINVAL;
1038 		}
1039 		if (config->metacopy && set.metacopy) {
1040 			pr_err("conflicting options: userxattr,metacopy=on\n");
1041 			return -EINVAL;
1042 		}
1043 		/*
1044 		 * Silently disable default setting of redirect and metacopy.
1045 		 * This shall be the default in the future as well: these
1046 		 * options must be explicitly enabled if used together with
1047 		 * userxattr.
1048 		 */
1049 		if (!set.redirect)
1050 			config->redirect_mode = follow_mode;
1051 		config->metacopy = false;
1052 	}
1053 
1054 	/* Data-only layers are reached through redirects */
1055 	if (ctx->nr_data && config->redirect_mode == OVL_REDIRECT_NOFOLLOW) {
1056 		pr_err("redirect_dir=nofollow is not supported with data-only layers\n");
1057 		return -EINVAL;
1058 	}
1059 	if (!ctx->nr_data && config->redirect_mode == OVL_REDIRECT_FOLLOWDATA) {
1060 		pr_err("redirect_dir=followdata requires data-only layers\n");
1061 		return -EINVAL;
1062 	}
1063 
1064 	/*
1065 	 * Fail if we don't have trusted xattr capability and a feature was
1066 	 * explicitly requested that requires them.
1067 	 */
1068 	if (!config->userxattr && !capable(CAP_SYS_ADMIN)) {
1069 		if (set.redirect &&
1070 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
1071 			pr_err("redirect_dir requires permission to access trusted xattrs\n");
1072 			return -EPERM;
1073 		}
1074 		if (config->metacopy && set.metacopy) {
1075 			pr_err("metacopy requires permission to access trusted xattrs\n");
1076 			return -EPERM;
1077 		}
1078 		if (config->verity_mode) {
1079 			pr_err("verity requires permission to access trusted xattrs\n");
1080 			return -EPERM;
1081 		}
1082 		if (ctx->nr_data > 0) {
1083 			pr_err("lower data-only dirs require permission to access trusted xattrs\n");
1084 			return -EPERM;
1085 		}
1086 		/*
1087 		 * Other xattr-dependent features should be disabled without
1088 		 * great disturbance to the user in ovl_make_workdir().
1089 		 */
1090 	}
1091 
1092 	return 0;
1093 }
1094 
1095 /**
1096  * ovl_show_options
1097  * @m: the seq_file handle
1098  * @dentry: The dentry to query
1099  *
1100  * Prints the mount options for a given superblock.
1101  * Returns zero; does not fail.
1102  */
1103 int ovl_show_options(struct seq_file *m, struct dentry *dentry)
1104 {
1105 	struct super_block *sb = dentry->d_sb;
1106 	struct ovl_fs *ofs = OVL_FS(sb);
1107 	size_t nr, nr_merged_lower, nr_lower = 0;
1108 	char **lowerdirs = ofs->config.lowerdirs;
1109 
1110 	/*
1111 	 * lowerdirs[0] holds the colon separated list that user provided
1112 	 * with lowerdir mount option.
1113 	 * lowerdirs[1..numlayer] hold the lowerdir paths that were added
1114 	 * using the lowerdir+ and datadir+ mount options.
1115 	 * For now, we do not allow mixing the legacy lowerdir mount option
1116 	 * with the new lowerdir+ and datadir+ mount options.
1117 	 */
1118 	if (lowerdirs[0]) {
1119 		seq_show_option(m, "lowerdir", lowerdirs[0]);
1120 	} else {
1121 		nr_lower = ofs->numlayer;
1122 		nr_merged_lower = nr_lower - ofs->numdatalayer;
1123 	}
1124 	for (nr = 1; nr < nr_lower; nr++) {
1125 		if (nr < nr_merged_lower)
1126 			seq_show_option(m, "lowerdir+", lowerdirs[nr]);
1127 		else
1128 			seq_show_option(m, "datadir+", lowerdirs[nr]);
1129 	}
1130 	if (ofs->config.upperdir) {
1131 		seq_show_option(m, "upperdir", ofs->config.upperdir);
1132 		seq_show_option(m, "workdir", ofs->config.workdir);
1133 	}
1134 	if (ofs->config.default_permissions)
1135 		seq_puts(m, ",default_permissions");
1136 	if (ofs->config.redirect_mode != ovl_redirect_mode_def())
1137 		seq_printf(m, ",redirect_dir=%s",
1138 			   ovl_redirect_mode(&ofs->config));
1139 	if (ofs->config.index != ovl_index_def)
1140 		seq_printf(m, ",index=%s", str_on_off(ofs->config.index));
1141 	if (ofs->config.uuid != ovl_uuid_def())
1142 		seq_printf(m, ",uuid=%s", ovl_uuid_mode(&ofs->config));
1143 	if (ofs->config.nfs_export != ovl_nfs_export_def)
1144 		seq_printf(m, ",nfs_export=%s",
1145 			   str_on_off(ofs->config.nfs_export));
1146 	if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(ofs))
1147 		seq_printf(m, ",xino=%s", ovl_xino_mode(&ofs->config));
1148 	if (ofs->config.metacopy != ovl_metacopy_def)
1149 		seq_printf(m, ",metacopy=%s", str_on_off(ofs->config.metacopy));
1150 	if (ofs->config.fsync_mode != ovl_fsync_mode_def())
1151 		seq_printf(m, ",fsync=%s", ovl_fsync_mode(&ofs->config));
1152 	if (ofs->config.userxattr)
1153 		seq_puts(m, ",userxattr");
1154 	if (ofs->config.verity_mode != ovl_verity_mode_def())
1155 		seq_printf(m, ",verity=%s",
1156 			   ovl_verity_mode(&ofs->config));
1157 	return 0;
1158 }
1159