1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Provide a way to create a superblock configuration context within the kernel
3 * that allows a superblock to be set up prior to mounting.
5 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/fs_context.h>
12 #include <linux/fs_parser.h>
14 #include <linux/mount.h>
15 #include <linux/nsproxy.h>
16 #include <linux/slab.h>
17 #include <linux/magic.h>
18 #include <linux/security.h>
19 #include <linux/mnt_namespace.h>
20 #include <linux/pid_namespace.h>
21 #include <linux/user_namespace.h>
22 #include <net/net_namespace.h>
23 #include <asm/sections.h>
27 enum legacy_fs_param {
28 LEGACY_FS_UNSET_PARAMS,
29 LEGACY_FS_MONOLITHIC_PARAMS,
30 LEGACY_FS_INDIVIDUAL_PARAMS,
33 struct legacy_fs_context {
34 char *legacy_data; /* Data page for legacy filesystems */
36 enum legacy_fs_param param_type;
39 static int legacy_init_fs_context(struct fs_context *fc);
41 static const struct constant_table common_set_sb_flag[] = {
42 { "dirsync", SB_DIRSYNC },
43 { "lazytime", SB_LAZYTIME },
44 { "mand", SB_MANDLOCK },
46 { "sync", SB_SYNCHRONOUS },
50 static const struct constant_table common_clear_sb_flag[] = {
51 { "async", SB_SYNCHRONOUS },
52 { "nolazytime", SB_LAZYTIME },
53 { "nomand", SB_MANDLOCK },
59 * Check for a common mount option that manipulates s_flags.
61 static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
65 token = lookup_constant(common_set_sb_flag, key, 0);
67 fc->sb_flags |= token;
68 fc->sb_flags_mask |= token;
72 token = lookup_constant(common_clear_sb_flag, key, 0);
74 fc->sb_flags &= ~token;
75 fc->sb_flags_mask |= token;
83 * vfs_parse_fs_param_source - Handle setting "source" via parameter
84 * @fc: The filesystem context to modify
85 * @param: The parameter
87 * This is a simple helper for filesystems to verify that the "source" they
90 * Returns 0 on success, -ENOPARAM if this is not "source" parameter, and
91 * -EINVAL otherwise. In the event of failure, supplementary error information
94 int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param)
96 if (strcmp(param->key, "source") != 0)
99 if (param->type != fs_value_is_string)
100 return invalf(fc, "Non-string source");
103 return invalf(fc, "Multiple sources");
105 fc->source = param->string;
106 param->string = NULL;
109 EXPORT_SYMBOL(vfs_parse_fs_param_source);
112 * vfs_parse_fs_param - Add a single parameter to a superblock config
113 * @fc: The filesystem context to modify
114 * @param: The parameter
116 * A single mount option in string form is applied to the filesystem context
117 * being set up. Certain standard options (for example "ro") are translated
118 * into flag bits without going to the filesystem. The active security module
119 * is allowed to observe and poach options. Any other options are passed over
120 * to the filesystem to parse.
122 * This may be called multiple times for a context.
124 * Returns 0 on success and a negative error code on failure. In the event of
125 * failure, supplementary error information may have been set.
127 int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
132 return invalf(fc, "Unnamed parameter\n");
134 ret = vfs_parse_sb_flag(fc, param->key);
135 if (ret != -ENOPARAM)
138 ret = security_fs_context_parse_param(fc, param);
139 if (ret != -ENOPARAM)
140 /* Param belongs to the LSM or is disallowed by the LSM; so
141 * don't pass to the FS.
145 if (fc->ops->parse_param) {
146 ret = fc->ops->parse_param(fc, param);
147 if (ret != -ENOPARAM)
151 /* If the filesystem doesn't take any arguments, give it the
152 * default handling of source.
154 ret = vfs_parse_fs_param_source(fc, param);
155 if (ret != -ENOPARAM)
158 return invalf(fc, "%s: Unknown parameter '%s'",
159 fc->fs_type->name, param->key);
161 EXPORT_SYMBOL(vfs_parse_fs_param);
164 * vfs_parse_fs_string - Convenience function to just parse a string.
165 * @fc: Filesystem context.
166 * @key: Parameter name.
167 * @value: Default value.
168 * @v_size: Maximum number of bytes in the value.
170 int vfs_parse_fs_string(struct fs_context *fc, const char *key,
171 const char *value, size_t v_size)
175 struct fs_parameter param = {
177 .type = fs_value_is_flag,
182 param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
185 param.type = fs_value_is_string;
188 ret = vfs_parse_fs_param(fc, ¶m);
192 EXPORT_SYMBOL(vfs_parse_fs_string);
195 * vfs_parse_monolithic_sep - Parse key[=val][,key[=val]]* mount data
196 * @fc: The superblock configuration to fill in.
197 * @data: The data to parse
198 * @sep: callback for separating next option
200 * Parse a blob of data that's in key[=val][,key[=val]]* form with a custom
201 * option separator callback.
203 * Returns 0 on success or the error returned by the ->parse_option() fs_context
204 * operation on failure.
206 int vfs_parse_monolithic_sep(struct fs_context *fc, void *data,
207 char *(*sep)(char **))
209 char *options = data, *key;
215 ret = security_sb_eat_lsm_opts(options, &fc->security);
219 while ((key = sep(&options)) != NULL) {
222 char *value = strchr(key, '=');
228 v_len = strlen(value);
230 ret = vfs_parse_fs_string(fc, key, value, v_len);
238 EXPORT_SYMBOL(vfs_parse_monolithic_sep);
240 static char *vfs_parse_comma_sep(char **s)
242 return strsep(s, ",");
246 * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
247 * @fc: The superblock configuration to fill in.
248 * @data: The data to parse
250 * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
251 * called from the ->monolithic_mount_data() fs_context operation.
253 * Returns 0 on success or the error returned by the ->parse_option() fs_context
254 * operation on failure.
256 int generic_parse_monolithic(struct fs_context *fc, void *data)
258 return vfs_parse_monolithic_sep(fc, data, vfs_parse_comma_sep);
260 EXPORT_SYMBOL(generic_parse_monolithic);
263 * alloc_fs_context - Create a filesystem context.
264 * @fs_type: The filesystem type.
265 * @reference: The dentry from which this one derives (or NULL)
266 * @sb_flags: Filesystem/superblock flags (SB_*)
267 * @sb_flags_mask: Applicable members of @sb_flags
268 * @purpose: The purpose that this configuration shall be used for.
270 * Open a filesystem and create a mount context. The mount context is
271 * initialised with the supplied flags and, if a submount/automount from
272 * another superblock (referred to by @reference) is supplied, may have
273 * parameters such as namespaces copied across from that superblock.
275 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
276 struct dentry *reference,
277 unsigned int sb_flags,
278 unsigned int sb_flags_mask,
279 enum fs_context_purpose purpose)
281 int (*init_fs_context)(struct fs_context *);
282 struct fs_context *fc;
285 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT);
287 return ERR_PTR(-ENOMEM);
289 fc->purpose = purpose;
290 fc->sb_flags = sb_flags;
291 fc->sb_flags_mask = sb_flags_mask;
292 fc->fs_type = get_filesystem(fs_type);
293 fc->cred = get_current_cred();
294 fc->net_ns = get_net(current->nsproxy->net_ns);
295 fc->log.prefix = fs_type->name;
297 mutex_init(&fc->uapi_mutex);
300 case FS_CONTEXT_FOR_MOUNT:
301 fc->user_ns = get_user_ns(fc->cred->user_ns);
303 case FS_CONTEXT_FOR_SUBMOUNT:
304 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
306 case FS_CONTEXT_FOR_RECONFIGURE:
307 atomic_inc(&reference->d_sb->s_active);
308 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
309 fc->root = dget(reference);
313 /* TODO: Make all filesystems support this unconditionally */
314 init_fs_context = fc->fs_type->init_fs_context;
315 if (!init_fs_context)
316 init_fs_context = legacy_init_fs_context;
318 ret = init_fs_context(fc);
321 fc->need_free = true;
329 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
330 unsigned int sb_flags)
332 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
333 FS_CONTEXT_FOR_MOUNT);
335 EXPORT_SYMBOL(fs_context_for_mount);
337 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
338 unsigned int sb_flags,
339 unsigned int sb_flags_mask)
341 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
342 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
344 EXPORT_SYMBOL(fs_context_for_reconfigure);
347 * fs_context_for_submount: allocate a new fs_context for a submount
348 * @type: file_system_type of the new context
349 * @reference: reference dentry from which to copy relevant info
351 * Allocate a new fs_context suitable for a submount. This also ensures that
352 * the fc->security object is inherited from @reference (if needed).
354 struct fs_context *fs_context_for_submount(struct file_system_type *type,
355 struct dentry *reference)
357 struct fs_context *fc;
360 fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
364 ret = security_fs_context_submount(fc, reference->d_sb);
372 EXPORT_SYMBOL(fs_context_for_submount);
374 void fc_drop_locked(struct fs_context *fc)
376 struct super_block *sb = fc->root->d_sb;
379 deactivate_locked_super(sb);
382 static void legacy_fs_context_free(struct fs_context *fc);
385 * vfs_dup_fs_context - Duplicate a filesystem context.
386 * @src_fc: The context to copy.
388 struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
390 struct fs_context *fc;
393 if (!src_fc->ops->dup)
394 return ERR_PTR(-EOPNOTSUPP);
396 fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
398 return ERR_PTR(-ENOMEM);
400 mutex_init(&fc->uapi_mutex);
402 fc->fs_private = NULL;
403 fc->s_fs_info = NULL;
406 get_filesystem(fc->fs_type);
408 get_user_ns(fc->user_ns);
411 refcount_inc(&fc->log.log->usage);
413 /* Can't call put until we've called ->dup */
414 ret = fc->ops->dup(fc, src_fc);
418 ret = security_fs_context_dup(fc, src_fc);
427 EXPORT_SYMBOL(vfs_dup_fs_context);
430 * logfc - Log a message to a filesystem context
431 * @log: The filesystem context to log to, or NULL to use printk.
432 * @prefix: A string to prefix the output with, or NULL.
433 * @level: 'w' for a warning, 'e' for an error. Anything else is a notice.
434 * @fmt: The format of the buffer.
436 void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
439 struct va_format vaf = {.fmt = fmt, .va = &va};
445 printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
446 prefix ? ": " : "", &vaf);
449 printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
450 prefix ? ": " : "", &vaf);
453 printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
454 prefix ? ": " : "", &vaf);
458 unsigned int logsize = ARRAY_SIZE(log->buffer);
460 char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
461 prefix ? prefix : "",
462 prefix ? ": " : "", &vaf);
464 index = log->head & (logsize - 1);
465 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
466 sizeof(log->tail) != sizeof(u8));
467 if ((u8)(log->head - log->tail) == logsize) {
468 /* The buffer is full, discard the oldest message */
469 if (log->need_free & (1 << index))
470 kfree(log->buffer[index]);
474 log->buffer[index] = q ? q : "OOM: Can't store error string";
476 log->need_free |= 1 << index;
478 log->need_free &= ~(1 << index);
483 EXPORT_SYMBOL(logfc);
486 * Free a logging structure.
488 static void put_fc_log(struct fs_context *fc)
490 struct fc_log *log = fc->log.log;
494 if (refcount_dec_and_test(&log->usage)) {
496 for (i = 0; i <= 7; i++)
497 if (log->need_free & (1 << i))
498 kfree(log->buffer[i]);
505 * put_fs_context - Dispose of a superblock configuration context.
506 * @fc: The context to dispose of.
508 void put_fs_context(struct fs_context *fc)
510 struct super_block *sb;
516 deactivate_super(sb);
519 if (fc->need_free && fc->ops && fc->ops->free)
522 security_free_mnt_opts(&fc->security);
524 put_user_ns(fc->user_ns);
527 put_filesystem(fc->fs_type);
531 EXPORT_SYMBOL(put_fs_context);
534 * Free the config for a filesystem that doesn't support fs_context.
536 static void legacy_fs_context_free(struct fs_context *fc)
538 struct legacy_fs_context *ctx = fc->fs_private;
541 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
542 kfree(ctx->legacy_data);
548 * Duplicate a legacy config.
550 static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
552 struct legacy_fs_context *ctx;
553 struct legacy_fs_context *src_ctx = src_fc->fs_private;
555 ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
559 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
560 ctx->legacy_data = kmemdup(src_ctx->legacy_data,
561 src_ctx->data_size, GFP_KERNEL);
562 if (!ctx->legacy_data) {
568 fc->fs_private = ctx;
573 * Add a parameter to a legacy config. We build up a comma-separated list of
576 static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
578 struct legacy_fs_context *ctx = fc->fs_private;
579 unsigned int size = ctx->data_size;
583 ret = vfs_parse_fs_param_source(fc, param);
584 if (ret != -ENOPARAM)
587 if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
588 return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
590 switch (param->type) {
591 case fs_value_is_string:
592 len = 1 + param->size;
594 case fs_value_is_flag:
595 len += strlen(param->key);
598 return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
602 if (size + len + 2 > PAGE_SIZE)
603 return invalf(fc, "VFS: Legacy: Cumulative options too large");
604 if (strchr(param->key, ',') ||
605 (param->type == fs_value_is_string &&
606 memchr(param->string, ',', param->size)))
607 return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
609 if (!ctx->legacy_data) {
610 ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
611 if (!ctx->legacy_data)
616 ctx->legacy_data[size++] = ',';
617 len = strlen(param->key);
618 memcpy(ctx->legacy_data + size, param->key, len);
620 if (param->type == fs_value_is_string) {
621 ctx->legacy_data[size++] = '=';
622 memcpy(ctx->legacy_data + size, param->string, param->size);
625 ctx->legacy_data[size] = '\0';
626 ctx->data_size = size;
627 ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
632 * Add monolithic mount data.
634 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
636 struct legacy_fs_context *ctx = fc->fs_private;
638 if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
639 pr_warn("VFS: Can't mix monolithic and individual options\n");
643 ctx->legacy_data = data;
644 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
645 if (!ctx->legacy_data)
648 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
650 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
654 * Get a mountable root with the legacy mount command.
656 static int legacy_get_tree(struct fs_context *fc)
658 struct legacy_fs_context *ctx = fc->fs_private;
659 struct super_block *sb;
662 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
663 fc->source, ctx->legacy_data);
665 return PTR_ERR(root);
677 static int legacy_reconfigure(struct fs_context *fc)
679 struct legacy_fs_context *ctx = fc->fs_private;
680 struct super_block *sb = fc->root->d_sb;
682 if (!sb->s_op->remount_fs)
685 return sb->s_op->remount_fs(sb, &fc->sb_flags,
686 ctx ? ctx->legacy_data : NULL);
689 const struct fs_context_operations legacy_fs_context_ops = {
690 .free = legacy_fs_context_free,
691 .dup = legacy_fs_context_dup,
692 .parse_param = legacy_parse_param,
693 .parse_monolithic = legacy_parse_monolithic,
694 .get_tree = legacy_get_tree,
695 .reconfigure = legacy_reconfigure,
699 * Initialise a legacy context for a filesystem that doesn't support
702 static int legacy_init_fs_context(struct fs_context *fc)
704 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT);
707 fc->ops = &legacy_fs_context_ops;
711 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
713 int (*monolithic_mount_data)(struct fs_context *, void *);
715 monolithic_mount_data = fc->ops->parse_monolithic;
716 if (!monolithic_mount_data)
717 monolithic_mount_data = generic_parse_monolithic;
719 return monolithic_mount_data(fc, data);
723 * Clean up a context after performing an action on it and put it into a state
724 * from where it can be used to reconfigure a superblock.
726 * Note that here we do only the parts that can't fail; the rest is in
727 * finish_clean_context() below and in between those fs_context is marked
728 * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
729 * successful mount or remount we need to report success to userland.
730 * Trying to do full reinit (for the sake of possible subsequent remount)
731 * and failing to allocate memory would've put us into a nasty situation.
732 * So here we only discard the old state and reinitialization is left
733 * until we actually try to reconfigure.
735 void vfs_clean_context(struct fs_context *fc)
737 if (fc->need_free && fc->ops && fc->ops->free)
739 fc->need_free = false;
740 fc->fs_private = NULL;
741 fc->s_fs_info = NULL;
743 security_free_mnt_opts(&fc->security);
746 fc->exclusive = false;
748 fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
749 fc->phase = FS_CONTEXT_AWAITING_RECONF;
752 int finish_clean_context(struct fs_context *fc)
756 if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
759 if (fc->fs_type->init_fs_context)
760 error = fc->fs_type->init_fs_context(fc);
762 error = legacy_init_fs_context(fc);
763 if (unlikely(error)) {
764 fc->phase = FS_CONTEXT_FAILED;
767 fc->need_free = true;
768 fc->phase = FS_CONTEXT_RECONF_PARAMS;