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 },
50 static const struct constant_table common_clear_sb_flag[] = {
51 { "async", SB_SYNCHRONOUS },
52 { "nolazytime", SB_LAZYTIME },
53 { "nomand", SB_MANDLOCK },
55 { "silent", SB_SILENT },
58 static const char *const forbidden_sb_flag[] = {
82 * Check for a common mount option that manipulates s_flags.
84 static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
89 for (i = 0; i < ARRAY_SIZE(forbidden_sb_flag); i++)
90 if (strcmp(key, forbidden_sb_flag[i]) == 0)
93 token = lookup_constant(common_set_sb_flag, key, 0);
95 fc->sb_flags |= token;
96 fc->sb_flags_mask |= token;
100 token = lookup_constant(common_clear_sb_flag, key, 0);
102 fc->sb_flags &= ~token;
103 fc->sb_flags_mask |= token;
111 * vfs_parse_fs_param - Add a single parameter to a superblock config
112 * @fc: The filesystem context to modify
113 * @param: The parameter
115 * A single mount option in string form is applied to the filesystem context
116 * being set up. Certain standard options (for example "ro") are translated
117 * into flag bits without going to the filesystem. The active security module
118 * is allowed to observe and poach options. Any other options are passed over
119 * to the filesystem to parse.
121 * This may be called multiple times for a context.
123 * Returns 0 on success and a negative error code on failure. In the event of
124 * failure, supplementary error information may have been set.
126 int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
131 return invalf(fc, "Unnamed parameter\n");
133 ret = vfs_parse_sb_flag(fc, param->key);
134 if (ret != -ENOPARAM)
137 ret = security_fs_context_parse_param(fc, param);
138 if (ret != -ENOPARAM)
139 /* Param belongs to the LSM or is disallowed by the LSM; so
140 * don't pass to the FS.
144 if (fc->ops->parse_param) {
145 ret = fc->ops->parse_param(fc, param);
146 if (ret != -ENOPARAM)
150 /* If the filesystem doesn't take any arguments, give it the
151 * default handling of source.
153 if (strcmp(param->key, "source") == 0) {
154 if (param->type != fs_value_is_string)
155 return invalf(fc, "VFS: Non-string source");
157 return invalf(fc, "VFS: Multiple sources");
158 fc->source = param->string;
159 param->string = NULL;
163 return invalf(fc, "%s: Unknown parameter '%s'",
164 fc->fs_type->name, param->key);
166 EXPORT_SYMBOL(vfs_parse_fs_param);
169 * vfs_parse_fs_string - Convenience function to just parse a string.
171 int vfs_parse_fs_string(struct fs_context *fc, const char *key,
172 const char *value, size_t v_size)
176 struct fs_parameter param = {
178 .type = fs_value_is_string,
183 param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
188 ret = vfs_parse_fs_param(fc, ¶m);
192 EXPORT_SYMBOL(vfs_parse_fs_string);
195 * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
196 * @ctx: The superblock configuration to fill in.
197 * @data: The data to parse
199 * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
200 * called from the ->monolithic_mount_data() fs_context operation.
202 * Returns 0 on success or the error returned by the ->parse_option() fs_context
203 * operation on failure.
205 int generic_parse_monolithic(struct fs_context *fc, void *data)
207 char *options = data, *key;
213 ret = security_sb_eat_lsm_opts(options, &fc->security);
217 while ((key = strsep(&options, ",")) != NULL) {
220 char *value = strchr(key, '=');
226 v_len = strlen(value);
228 ret = vfs_parse_fs_string(fc, key, value, v_len);
236 EXPORT_SYMBOL(generic_parse_monolithic);
239 * alloc_fs_context - Create a filesystem context.
240 * @fs_type: The filesystem type.
241 * @reference: The dentry from which this one derives (or NULL)
242 * @sb_flags: Filesystem/superblock flags (SB_*)
243 * @sb_flags_mask: Applicable members of @sb_flags
244 * @purpose: The purpose that this configuration shall be used for.
246 * Open a filesystem and create a mount context. The mount context is
247 * initialised with the supplied flags and, if a submount/automount from
248 * another superblock (referred to by @reference) is supplied, may have
249 * parameters such as namespaces copied across from that superblock.
251 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
252 struct dentry *reference,
253 unsigned int sb_flags,
254 unsigned int sb_flags_mask,
255 enum fs_context_purpose purpose)
257 int (*init_fs_context)(struct fs_context *);
258 struct fs_context *fc;
261 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
263 return ERR_PTR(-ENOMEM);
265 fc->purpose = purpose;
266 fc->sb_flags = sb_flags;
267 fc->sb_flags_mask = sb_flags_mask;
268 fc->fs_type = get_filesystem(fs_type);
269 fc->cred = get_current_cred();
270 fc->net_ns = get_net(current->nsproxy->net_ns);
272 mutex_init(&fc->uapi_mutex);
275 case FS_CONTEXT_FOR_MOUNT:
276 fc->user_ns = get_user_ns(fc->cred->user_ns);
278 case FS_CONTEXT_FOR_SUBMOUNT:
279 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
281 case FS_CONTEXT_FOR_RECONFIGURE:
282 atomic_inc(&reference->d_sb->s_active);
283 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
284 fc->root = dget(reference);
288 /* TODO: Make all filesystems support this unconditionally */
289 init_fs_context = fc->fs_type->init_fs_context;
290 if (!init_fs_context)
291 init_fs_context = legacy_init_fs_context;
293 ret = init_fs_context(fc);
296 fc->need_free = true;
304 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
305 unsigned int sb_flags)
307 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
308 FS_CONTEXT_FOR_MOUNT);
310 EXPORT_SYMBOL(fs_context_for_mount);
312 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
313 unsigned int sb_flags,
314 unsigned int sb_flags_mask)
316 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
317 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
319 EXPORT_SYMBOL(fs_context_for_reconfigure);
321 struct fs_context *fs_context_for_submount(struct file_system_type *type,
322 struct dentry *reference)
324 return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
326 EXPORT_SYMBOL(fs_context_for_submount);
328 void fc_drop_locked(struct fs_context *fc)
330 struct super_block *sb = fc->root->d_sb;
333 deactivate_locked_super(sb);
336 static void legacy_fs_context_free(struct fs_context *fc);
339 * vfs_dup_fc_config: Duplicate a filesystem context.
340 * @src_fc: The context to copy.
342 struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
344 struct fs_context *fc;
347 if (!src_fc->ops->dup)
348 return ERR_PTR(-EOPNOTSUPP);
350 fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
352 return ERR_PTR(-ENOMEM);
354 mutex_init(&fc->uapi_mutex);
356 fc->fs_private = NULL;
357 fc->s_fs_info = NULL;
360 get_filesystem(fc->fs_type);
362 get_user_ns(fc->user_ns);
365 refcount_inc(&fc->log->usage);
367 /* Can't call put until we've called ->dup */
368 ret = fc->ops->dup(fc, src_fc);
372 ret = security_fs_context_dup(fc, src_fc);
381 EXPORT_SYMBOL(vfs_dup_fs_context);
384 * logfc - Log a message to a filesystem context
385 * @fc: The filesystem context to log to.
386 * @fmt: The format of the buffer.
388 void logfc(struct fs_context *fc, const char *fmt, ...)
390 static const char store_failure[] = "OOM: Can't store error string";
391 struct fc_log *log = fc ? fc->log : NULL;
398 if (!strchr(fmt, '%')) {
400 goto unformatted_string;
402 if (strcmp(fmt, "%s") == 0) {
403 p = va_arg(va, const char *);
404 goto unformatted_string;
407 q = kvasprintf(GFP_KERNEL, fmt, va);
415 if ((unsigned long)p >= (unsigned long)__start_rodata &&
416 (unsigned long)p < (unsigned long)__end_rodata)
418 if (log && within_module_core((unsigned long)p, log->owner))
420 q = kstrdup(p, GFP_KERNEL);
432 printk(KERN_WARNING "%s\n", q + 2);
435 printk(KERN_ERR "%s\n", q + 2);
438 printk(KERN_NOTICE "%s\n", q + 2);
444 unsigned int logsize = ARRAY_SIZE(log->buffer);
447 index = log->head & (logsize - 1);
448 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
449 sizeof(log->tail) != sizeof(u8));
450 if ((u8)(log->head - log->tail) == logsize) {
451 /* The buffer is full, discard the oldest message */
452 if (log->need_free & (1 << index))
453 kfree(log->buffer[index]);
457 log->buffer[index] = q;
458 log->need_free &= ~(1 << index);
459 log->need_free |= freeable << index;
464 EXPORT_SYMBOL(logfc);
467 * Free a logging structure.
469 static void put_fc_log(struct fs_context *fc)
471 struct fc_log *log = fc->log;
475 if (refcount_dec_and_test(&log->usage)) {
477 for (i = 0; i <= 7; i++)
478 if (log->need_free & (1 << i))
479 kfree(log->buffer[i]);
486 * put_fs_context - Dispose of a superblock configuration context.
487 * @fc: The context to dispose of.
489 void put_fs_context(struct fs_context *fc)
491 struct super_block *sb;
497 deactivate_super(sb);
500 if (fc->need_free && fc->ops && fc->ops->free)
503 security_free_mnt_opts(&fc->security);
505 put_user_ns(fc->user_ns);
508 put_filesystem(fc->fs_type);
512 EXPORT_SYMBOL(put_fs_context);
515 * Free the config for a filesystem that doesn't support fs_context.
517 static void legacy_fs_context_free(struct fs_context *fc)
519 struct legacy_fs_context *ctx = fc->fs_private;
522 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
523 kfree(ctx->legacy_data);
529 * Duplicate a legacy config.
531 static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
533 struct legacy_fs_context *ctx;
534 struct legacy_fs_context *src_ctx = src_fc->fs_private;
536 ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
540 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
541 ctx->legacy_data = kmemdup(src_ctx->legacy_data,
542 src_ctx->data_size, GFP_KERNEL);
543 if (!ctx->legacy_data) {
549 fc->fs_private = ctx;
554 * Add a parameter to a legacy config. We build up a comma-separated list of
557 static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
559 struct legacy_fs_context *ctx = fc->fs_private;
560 unsigned int size = ctx->data_size;
563 if (strcmp(param->key, "source") == 0) {
564 if (param->type != fs_value_is_string)
565 return invalf(fc, "VFS: Legacy: Non-string source");
567 return invalf(fc, "VFS: Legacy: Multiple sources");
568 fc->source = param->string;
569 param->string = NULL;
573 if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
574 return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
576 switch (param->type) {
577 case fs_value_is_string:
578 len = 1 + param->size;
580 case fs_value_is_flag:
581 len += strlen(param->key);
584 return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
588 if (len > PAGE_SIZE - 2 - size)
589 return invalf(fc, "VFS: Legacy: Cumulative options too large");
590 if (strchr(param->key, ',') ||
591 (param->type == fs_value_is_string &&
592 memchr(param->string, ',', param->size)))
593 return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
595 if (!ctx->legacy_data) {
596 ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
597 if (!ctx->legacy_data)
601 ctx->legacy_data[size++] = ',';
602 len = strlen(param->key);
603 memcpy(ctx->legacy_data + size, param->key, len);
605 if (param->type == fs_value_is_string) {
606 ctx->legacy_data[size++] = '=';
607 memcpy(ctx->legacy_data + size, param->string, param->size);
610 ctx->legacy_data[size] = '\0';
611 ctx->data_size = size;
612 ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
617 * Add monolithic mount data.
619 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
621 struct legacy_fs_context *ctx = fc->fs_private;
623 if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
624 pr_warn("VFS: Can't mix monolithic and individual options\n");
628 ctx->legacy_data = data;
629 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
630 if (!ctx->legacy_data)
633 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
635 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
639 * Get a mountable root with the legacy mount command.
641 static int legacy_get_tree(struct fs_context *fc)
643 struct legacy_fs_context *ctx = fc->fs_private;
644 struct super_block *sb;
647 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
648 fc->source, ctx->legacy_data);
650 return PTR_ERR(root);
662 static int legacy_reconfigure(struct fs_context *fc)
664 struct legacy_fs_context *ctx = fc->fs_private;
665 struct super_block *sb = fc->root->d_sb;
667 if (!sb->s_op->remount_fs)
670 return sb->s_op->remount_fs(sb, &fc->sb_flags,
671 ctx ? ctx->legacy_data : NULL);
674 const struct fs_context_operations legacy_fs_context_ops = {
675 .free = legacy_fs_context_free,
676 .dup = legacy_fs_context_dup,
677 .parse_param = legacy_parse_param,
678 .parse_monolithic = legacy_parse_monolithic,
679 .get_tree = legacy_get_tree,
680 .reconfigure = legacy_reconfigure,
684 * Initialise a legacy context for a filesystem that doesn't support
687 static int legacy_init_fs_context(struct fs_context *fc)
689 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
692 fc->ops = &legacy_fs_context_ops;
696 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
698 int (*monolithic_mount_data)(struct fs_context *, void *);
700 monolithic_mount_data = fc->ops->parse_monolithic;
701 if (!monolithic_mount_data)
702 monolithic_mount_data = generic_parse_monolithic;
704 return monolithic_mount_data(fc, data);
708 * Clean up a context after performing an action on it and put it into a state
709 * from where it can be used to reconfigure a superblock.
711 * Note that here we do only the parts that can't fail; the rest is in
712 * finish_clean_context() below and in between those fs_context is marked
713 * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
714 * successful mount or remount we need to report success to userland.
715 * Trying to do full reinit (for the sake of possible subsequent remount)
716 * and failing to allocate memory would've put us into a nasty situation.
717 * So here we only discard the old state and reinitialization is left
718 * until we actually try to reconfigure.
720 void vfs_clean_context(struct fs_context *fc)
722 if (fc->need_free && fc->ops && fc->ops->free)
724 fc->need_free = false;
725 fc->fs_private = NULL;
726 fc->s_fs_info = NULL;
728 security_free_mnt_opts(&fc->security);
732 fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
733 fc->phase = FS_CONTEXT_AWAITING_RECONF;
736 int finish_clean_context(struct fs_context *fc)
740 if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
743 if (fc->fs_type->init_fs_context)
744 error = fc->fs_type->init_fs_context(fc);
746 error = legacy_init_fs_context(fc);
747 if (unlikely(error)) {
748 fc->phase = FS_CONTEXT_FAILED;
751 fc->need_free = true;
752 fc->phase = FS_CONTEXT_RECONF_PARAMS;