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 },
45 { "posixacl", SB_POSIXACL },
47 { "sync", SB_SYNCHRONOUS },
51 static const struct constant_table common_clear_sb_flag[] = {
52 { "async", SB_SYNCHRONOUS },
53 { "nolazytime", SB_LAZYTIME },
54 { "nomand", SB_MANDLOCK },
56 { "silent", SB_SILENT },
60 static const char *const forbidden_sb_flag[] = {
84 * Check for a common mount option that manipulates s_flags.
86 static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
91 for (i = 0; i < ARRAY_SIZE(forbidden_sb_flag); i++)
92 if (strcmp(key, forbidden_sb_flag[i]) == 0)
95 token = lookup_constant(common_set_sb_flag, key, 0);
97 fc->sb_flags |= token;
98 fc->sb_flags_mask |= token;
102 token = lookup_constant(common_clear_sb_flag, key, 0);
104 fc->sb_flags &= ~token;
105 fc->sb_flags_mask |= token;
113 * vfs_parse_fs_param - Add a single parameter to a superblock config
114 * @fc: The filesystem context to modify
115 * @param: The parameter
117 * A single mount option in string form is applied to the filesystem context
118 * being set up. Certain standard options (for example "ro") are translated
119 * into flag bits without going to the filesystem. The active security module
120 * is allowed to observe and poach options. Any other options are passed over
121 * to the filesystem to parse.
123 * This may be called multiple times for a context.
125 * Returns 0 on success and a negative error code on failure. In the event of
126 * failure, supplementary error information may have been set.
128 int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
133 return invalf(fc, "Unnamed parameter\n");
135 ret = vfs_parse_sb_flag(fc, param->key);
136 if (ret != -ENOPARAM)
139 ret = security_fs_context_parse_param(fc, param);
140 if (ret != -ENOPARAM)
141 /* Param belongs to the LSM or is disallowed by the LSM; so
142 * don't pass to the FS.
146 if (fc->ops->parse_param) {
147 ret = fc->ops->parse_param(fc, param);
148 if (ret != -ENOPARAM)
152 /* If the filesystem doesn't take any arguments, give it the
153 * default handling of source.
155 if (strcmp(param->key, "source") == 0) {
156 if (param->type != fs_value_is_string)
157 return invalf(fc, "VFS: Non-string source");
159 return invalf(fc, "VFS: Multiple sources");
160 fc->source = param->string;
161 param->string = NULL;
165 return invalf(fc, "%s: Unknown parameter '%s'",
166 fc->fs_type->name, param->key);
168 EXPORT_SYMBOL(vfs_parse_fs_param);
171 * vfs_parse_fs_string - Convenience function to just parse a string.
173 int vfs_parse_fs_string(struct fs_context *fc, const char *key,
174 const char *value, size_t v_size)
178 struct fs_parameter param = {
180 .type = fs_value_is_flag,
185 param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
188 param.type = fs_value_is_string;
191 ret = vfs_parse_fs_param(fc, ¶m);
195 EXPORT_SYMBOL(vfs_parse_fs_string);
198 * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
199 * @ctx: The superblock configuration to fill in.
200 * @data: The data to parse
202 * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
203 * called from the ->monolithic_mount_data() fs_context operation.
205 * Returns 0 on success or the error returned by the ->parse_option() fs_context
206 * operation on failure.
208 int generic_parse_monolithic(struct fs_context *fc, void *data)
210 char *options = data, *key;
216 ret = security_sb_eat_lsm_opts(options, &fc->security);
220 while ((key = strsep(&options, ",")) != NULL) {
223 char *value = strchr(key, '=');
229 v_len = strlen(value);
231 ret = vfs_parse_fs_string(fc, key, value, v_len);
239 EXPORT_SYMBOL(generic_parse_monolithic);
242 * alloc_fs_context - Create a filesystem context.
243 * @fs_type: The filesystem type.
244 * @reference: The dentry from which this one derives (or NULL)
245 * @sb_flags: Filesystem/superblock flags (SB_*)
246 * @sb_flags_mask: Applicable members of @sb_flags
247 * @purpose: The purpose that this configuration shall be used for.
249 * Open a filesystem and create a mount context. The mount context is
250 * initialised with the supplied flags and, if a submount/automount from
251 * another superblock (referred to by @reference) is supplied, may have
252 * parameters such as namespaces copied across from that superblock.
254 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
255 struct dentry *reference,
256 unsigned int sb_flags,
257 unsigned int sb_flags_mask,
258 enum fs_context_purpose purpose)
260 int (*init_fs_context)(struct fs_context *);
261 struct fs_context *fc;
264 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
266 return ERR_PTR(-ENOMEM);
268 fc->purpose = purpose;
269 fc->sb_flags = sb_flags;
270 fc->sb_flags_mask = sb_flags_mask;
271 fc->fs_type = get_filesystem(fs_type);
272 fc->cred = get_current_cred();
273 fc->net_ns = get_net(current->nsproxy->net_ns);
275 mutex_init(&fc->uapi_mutex);
278 case FS_CONTEXT_FOR_MOUNT:
279 fc->user_ns = get_user_ns(fc->cred->user_ns);
281 case FS_CONTEXT_FOR_SUBMOUNT:
282 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
284 case FS_CONTEXT_FOR_RECONFIGURE:
285 atomic_inc(&reference->d_sb->s_active);
286 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
287 fc->root = dget(reference);
291 /* TODO: Make all filesystems support this unconditionally */
292 init_fs_context = fc->fs_type->init_fs_context;
293 if (!init_fs_context)
294 init_fs_context = legacy_init_fs_context;
296 ret = init_fs_context(fc);
299 fc->need_free = true;
307 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
308 unsigned int sb_flags)
310 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
311 FS_CONTEXT_FOR_MOUNT);
313 EXPORT_SYMBOL(fs_context_for_mount);
315 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
316 unsigned int sb_flags,
317 unsigned int sb_flags_mask)
319 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
320 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
322 EXPORT_SYMBOL(fs_context_for_reconfigure);
324 struct fs_context *fs_context_for_submount(struct file_system_type *type,
325 struct dentry *reference)
327 return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
329 EXPORT_SYMBOL(fs_context_for_submount);
331 void fc_drop_locked(struct fs_context *fc)
333 struct super_block *sb = fc->root->d_sb;
336 deactivate_locked_super(sb);
339 static void legacy_fs_context_free(struct fs_context *fc);
342 * vfs_dup_fc_config: Duplicate a filesystem context.
343 * @src_fc: The context to copy.
345 struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
347 struct fs_context *fc;
350 if (!src_fc->ops->dup)
351 return ERR_PTR(-EOPNOTSUPP);
353 fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
355 return ERR_PTR(-ENOMEM);
357 mutex_init(&fc->uapi_mutex);
359 fc->fs_private = NULL;
360 fc->s_fs_info = NULL;
363 get_filesystem(fc->fs_type);
365 get_user_ns(fc->user_ns);
368 refcount_inc(&fc->log->usage);
370 /* Can't call put until we've called ->dup */
371 ret = fc->ops->dup(fc, src_fc);
375 ret = security_fs_context_dup(fc, src_fc);
384 EXPORT_SYMBOL(vfs_dup_fs_context);
387 * logfc - Log a message to a filesystem context
388 * @fc: The filesystem context to log to.
389 * @fmt: The format of the buffer.
391 void logfc(struct fs_context *fc, const char *fmt, ...)
393 static const char store_failure[] = "OOM: Can't store error string";
394 struct fc_log *log = fc ? fc->log : NULL;
401 if (!strchr(fmt, '%')) {
403 goto unformatted_string;
405 if (strcmp(fmt, "%s") == 0) {
406 p = va_arg(va, const char *);
407 goto unformatted_string;
410 q = kvasprintf(GFP_KERNEL, fmt, va);
418 if ((unsigned long)p >= (unsigned long)__start_rodata &&
419 (unsigned long)p < (unsigned long)__end_rodata)
421 if (log && within_module_core((unsigned long)p, log->owner))
423 q = kstrdup(p, GFP_KERNEL);
435 printk(KERN_WARNING "%s\n", q + 2);
438 printk(KERN_ERR "%s\n", q + 2);
441 printk(KERN_NOTICE "%s\n", q + 2);
447 unsigned int logsize = ARRAY_SIZE(log->buffer);
450 index = log->head & (logsize - 1);
451 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
452 sizeof(log->tail) != sizeof(u8));
453 if ((u8)(log->head - log->tail) == logsize) {
454 /* The buffer is full, discard the oldest message */
455 if (log->need_free & (1 << index))
456 kfree(log->buffer[index]);
460 log->buffer[index] = q;
461 log->need_free &= ~(1 << index);
462 log->need_free |= freeable << index;
467 EXPORT_SYMBOL(logfc);
470 * Free a logging structure.
472 static void put_fc_log(struct fs_context *fc)
474 struct fc_log *log = fc->log;
478 if (refcount_dec_and_test(&log->usage)) {
480 for (i = 0; i <= 7; i++)
481 if (log->need_free & (1 << i))
482 kfree(log->buffer[i]);
489 * put_fs_context - Dispose of a superblock configuration context.
490 * @fc: The context to dispose of.
492 void put_fs_context(struct fs_context *fc)
494 struct super_block *sb;
500 deactivate_super(sb);
503 if (fc->need_free && fc->ops && fc->ops->free)
506 security_free_mnt_opts(&fc->security);
508 put_user_ns(fc->user_ns);
511 put_filesystem(fc->fs_type);
515 EXPORT_SYMBOL(put_fs_context);
518 * Free the config for a filesystem that doesn't support fs_context.
520 static void legacy_fs_context_free(struct fs_context *fc)
522 struct legacy_fs_context *ctx = fc->fs_private;
525 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
526 kfree(ctx->legacy_data);
532 * Duplicate a legacy config.
534 static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
536 struct legacy_fs_context *ctx;
537 struct legacy_fs_context *src_ctx = src_fc->fs_private;
539 ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
543 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
544 ctx->legacy_data = kmemdup(src_ctx->legacy_data,
545 src_ctx->data_size, GFP_KERNEL);
546 if (!ctx->legacy_data) {
552 fc->fs_private = ctx;
557 * Add a parameter to a legacy config. We build up a comma-separated list of
560 static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
562 struct legacy_fs_context *ctx = fc->fs_private;
563 unsigned int size = ctx->data_size;
566 if (strcmp(param->key, "source") == 0) {
567 if (param->type != fs_value_is_string)
568 return invalf(fc, "VFS: Legacy: Non-string source");
570 return invalf(fc, "VFS: Legacy: Multiple sources");
571 fc->source = param->string;
572 param->string = NULL;
576 if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
577 return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
579 switch (param->type) {
580 case fs_value_is_string:
581 len = 1 + param->size;
583 case fs_value_is_flag:
584 len += strlen(param->key);
587 return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
591 if (len > PAGE_SIZE - 2 - size)
592 return invalf(fc, "VFS: Legacy: Cumulative options too large");
593 if (strchr(param->key, ',') ||
594 (param->type == fs_value_is_string &&
595 memchr(param->string, ',', param->size)))
596 return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
598 if (!ctx->legacy_data) {
599 ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
600 if (!ctx->legacy_data)
604 ctx->legacy_data[size++] = ',';
605 len = strlen(param->key);
606 memcpy(ctx->legacy_data + size, param->key, len);
608 if (param->type == fs_value_is_string) {
609 ctx->legacy_data[size++] = '=';
610 memcpy(ctx->legacy_data + size, param->string, param->size);
613 ctx->legacy_data[size] = '\0';
614 ctx->data_size = size;
615 ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
620 * Add monolithic mount data.
622 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
624 struct legacy_fs_context *ctx = fc->fs_private;
626 if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
627 pr_warn("VFS: Can't mix monolithic and individual options\n");
631 ctx->legacy_data = data;
632 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
633 if (!ctx->legacy_data)
636 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
638 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
642 * Get a mountable root with the legacy mount command.
644 static int legacy_get_tree(struct fs_context *fc)
646 struct legacy_fs_context *ctx = fc->fs_private;
647 struct super_block *sb;
650 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
651 fc->source, ctx->legacy_data);
653 return PTR_ERR(root);
665 static int legacy_reconfigure(struct fs_context *fc)
667 struct legacy_fs_context *ctx = fc->fs_private;
668 struct super_block *sb = fc->root->d_sb;
670 if (!sb->s_op->remount_fs)
673 return sb->s_op->remount_fs(sb, &fc->sb_flags,
674 ctx ? ctx->legacy_data : NULL);
677 const struct fs_context_operations legacy_fs_context_ops = {
678 .free = legacy_fs_context_free,
679 .dup = legacy_fs_context_dup,
680 .parse_param = legacy_parse_param,
681 .parse_monolithic = legacy_parse_monolithic,
682 .get_tree = legacy_get_tree,
683 .reconfigure = legacy_reconfigure,
687 * Initialise a legacy context for a filesystem that doesn't support
690 static int legacy_init_fs_context(struct fs_context *fc)
692 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
695 fc->ops = &legacy_fs_context_ops;
699 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
701 int (*monolithic_mount_data)(struct fs_context *, void *);
703 monolithic_mount_data = fc->ops->parse_monolithic;
704 if (!monolithic_mount_data)
705 monolithic_mount_data = generic_parse_monolithic;
707 return monolithic_mount_data(fc, data);
711 * Clean up a context after performing an action on it and put it into a state
712 * from where it can be used to reconfigure a superblock.
714 * Note that here we do only the parts that can't fail; the rest is in
715 * finish_clean_context() below and in between those fs_context is marked
716 * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
717 * successful mount or remount we need to report success to userland.
718 * Trying to do full reinit (for the sake of possible subsequent remount)
719 * and failing to allocate memory would've put us into a nasty situation.
720 * So here we only discard the old state and reinitialization is left
721 * until we actually try to reconfigure.
723 void vfs_clean_context(struct fs_context *fc)
725 if (fc->need_free && fc->ops && fc->ops->free)
727 fc->need_free = false;
728 fc->fs_private = NULL;
729 fc->s_fs_info = NULL;
731 security_free_mnt_opts(&fc->security);
735 fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
736 fc->phase = FS_CONTEXT_AWAITING_RECONF;
739 int finish_clean_context(struct fs_context *fc)
743 if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
746 if (fc->fs_type->init_fs_context)
747 error = fc->fs_type->init_fs_context(fc);
749 error = legacy_init_fs_context(fc);
750 if (unlikely(error)) {
751 fc->phase = FS_CONTEXT_FAILED;
754 fc->need_free = true;
755 fc->phase = FS_CONTEXT_RECONF_PARAMS;