1 /* Provide a way to create a superblock configuration context within the kernel
2 * that allows a superblock to be set up prior to mounting.
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/fs_context.h>
16 #include <linux/fs_parser.h>
18 #include <linux/mount.h>
19 #include <linux/nsproxy.h>
20 #include <linux/slab.h>
21 #include <linux/magic.h>
22 #include <linux/security.h>
23 #include <linux/mnt_namespace.h>
24 #include <linux/pid_namespace.h>
25 #include <linux/user_namespace.h>
26 #include <net/net_namespace.h>
27 #include <asm/sections.h>
31 enum legacy_fs_param {
32 LEGACY_FS_UNSET_PARAMS,
33 LEGACY_FS_MONOLITHIC_PARAMS,
34 LEGACY_FS_INDIVIDUAL_PARAMS,
37 struct legacy_fs_context {
38 char *legacy_data; /* Data page for legacy filesystems */
40 enum legacy_fs_param param_type;
43 static int legacy_init_fs_context(struct fs_context *fc);
45 static const struct constant_table common_set_sb_flag[] = {
46 { "dirsync", SB_DIRSYNC },
47 { "lazytime", SB_LAZYTIME },
48 { "mand", SB_MANDLOCK },
49 { "posixacl", SB_POSIXACL },
51 { "sync", SB_SYNCHRONOUS },
54 static const struct constant_table common_clear_sb_flag[] = {
55 { "async", SB_SYNCHRONOUS },
56 { "nolazytime", SB_LAZYTIME },
57 { "nomand", SB_MANDLOCK },
59 { "silent", SB_SILENT },
62 static const char *const forbidden_sb_flag[] = {
86 * Check for a common mount option that manipulates s_flags.
88 static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
93 for (i = 0; i < ARRAY_SIZE(forbidden_sb_flag); i++)
94 if (strcmp(key, forbidden_sb_flag[i]) == 0)
97 token = lookup_constant(common_set_sb_flag, key, 0);
99 fc->sb_flags |= token;
100 fc->sb_flags_mask |= token;
104 token = lookup_constant(common_clear_sb_flag, key, 0);
106 fc->sb_flags &= ~token;
107 fc->sb_flags_mask |= token;
115 * vfs_parse_fs_param - Add a single parameter to a superblock config
116 * @fc: The filesystem context to modify
117 * @param: The parameter
119 * A single mount option in string form is applied to the filesystem context
120 * being set up. Certain standard options (for example "ro") are translated
121 * into flag bits without going to the filesystem. The active security module
122 * is allowed to observe and poach options. Any other options are passed over
123 * to the filesystem to parse.
125 * This may be called multiple times for a context.
127 * Returns 0 on success and a negative error code on failure. In the event of
128 * failure, supplementary error information may have been set.
130 int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
135 return invalf(fc, "Unnamed parameter\n");
137 ret = vfs_parse_sb_flag(fc, param->key);
138 if (ret != -ENOPARAM)
141 ret = security_fs_context_parse_param(fc, param);
142 if (ret != -ENOPARAM)
143 /* Param belongs to the LSM or is disallowed by the LSM; so
144 * don't pass to the FS.
148 if (fc->ops->parse_param) {
149 ret = fc->ops->parse_param(fc, param);
150 if (ret != -ENOPARAM)
154 /* If the filesystem doesn't take any arguments, give it the
155 * default handling of source.
157 if (strcmp(param->key, "source") == 0) {
158 if (param->type != fs_value_is_string)
159 return invalf(fc, "VFS: Non-string source");
161 return invalf(fc, "VFS: Multiple sources");
162 fc->source = param->string;
163 param->string = NULL;
167 return invalf(fc, "%s: Unknown parameter '%s'",
168 fc->fs_type->name, param->key);
170 EXPORT_SYMBOL(vfs_parse_fs_param);
173 * vfs_parse_fs_string - Convenience function to just parse a string.
175 int vfs_parse_fs_string(struct fs_context *fc, const char *key,
176 const char *value, size_t v_size)
180 struct fs_parameter param = {
182 .type = fs_value_is_string,
187 param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
192 ret = vfs_parse_fs_param(fc, ¶m);
196 EXPORT_SYMBOL(vfs_parse_fs_string);
199 * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
200 * @ctx: The superblock configuration to fill in.
201 * @data: The data to parse
203 * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
204 * called from the ->monolithic_mount_data() fs_context operation.
206 * Returns 0 on success or the error returned by the ->parse_option() fs_context
207 * operation on failure.
209 int generic_parse_monolithic(struct fs_context *fc, void *data)
211 char *options = data, *key;
217 ret = security_sb_eat_lsm_opts(options, &fc->security);
221 while ((key = strsep(&options, ",")) != NULL) {
224 char *value = strchr(key, '=');
230 v_len = strlen(value);
232 ret = vfs_parse_fs_string(fc, key, value, v_len);
240 EXPORT_SYMBOL(generic_parse_monolithic);
243 * alloc_fs_context - Create a filesystem context.
244 * @fs_type: The filesystem type.
245 * @reference: The dentry from which this one derives (or NULL)
246 * @sb_flags: Filesystem/superblock flags (SB_*)
247 * @sb_flags_mask: Applicable members of @sb_flags
248 * @purpose: The purpose that this configuration shall be used for.
250 * Open a filesystem and create a mount context. The mount context is
251 * initialised with the supplied flags and, if a submount/automount from
252 * another superblock (referred to by @reference) is supplied, may have
253 * parameters such as namespaces copied across from that superblock.
255 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
256 struct dentry *reference,
257 unsigned int sb_flags,
258 unsigned int sb_flags_mask,
259 enum fs_context_purpose purpose)
261 int (*init_fs_context)(struct fs_context *);
262 struct fs_context *fc;
265 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
267 return ERR_PTR(-ENOMEM);
269 fc->purpose = purpose;
270 fc->sb_flags = sb_flags;
271 fc->sb_flags_mask = sb_flags_mask;
272 fc->fs_type = get_filesystem(fs_type);
273 fc->cred = get_current_cred();
274 fc->net_ns = get_net(current->nsproxy->net_ns);
276 mutex_init(&fc->uapi_mutex);
279 case FS_CONTEXT_FOR_MOUNT:
280 fc->user_ns = get_user_ns(fc->cred->user_ns);
282 case FS_CONTEXT_FOR_SUBMOUNT:
283 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
285 case FS_CONTEXT_FOR_RECONFIGURE:
286 /* We don't pin any namespaces as the superblock's
287 * subscriptions cannot be changed at this point.
289 atomic_inc(&reference->d_sb->s_active);
290 fc->root = dget(reference);
294 /* TODO: Make all filesystems support this unconditionally */
295 init_fs_context = fc->fs_type->init_fs_context;
296 if (!init_fs_context)
297 init_fs_context = legacy_init_fs_context;
299 ret = init_fs_context(fc);
302 fc->need_free = true;
310 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
311 unsigned int sb_flags)
313 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
314 FS_CONTEXT_FOR_MOUNT);
316 EXPORT_SYMBOL(fs_context_for_mount);
318 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
319 unsigned int sb_flags,
320 unsigned int sb_flags_mask)
322 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
323 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
325 EXPORT_SYMBOL(fs_context_for_reconfigure);
327 struct fs_context *fs_context_for_submount(struct file_system_type *type,
328 struct dentry *reference)
330 return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
332 EXPORT_SYMBOL(fs_context_for_submount);
334 void fc_drop_locked(struct fs_context *fc)
336 struct super_block *sb = fc->root->d_sb;
339 deactivate_locked_super(sb);
342 static void legacy_fs_context_free(struct fs_context *fc);
345 * vfs_dup_fc_config: Duplicate a filesystem context.
346 * @src_fc: The context to copy.
348 struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
350 struct fs_context *fc;
353 if (!src_fc->ops->dup)
354 return ERR_PTR(-EOPNOTSUPP);
356 fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
358 return ERR_PTR(-ENOMEM);
360 mutex_init(&fc->uapi_mutex);
362 fc->fs_private = NULL;
363 fc->s_fs_info = NULL;
366 get_filesystem(fc->fs_type);
368 get_user_ns(fc->user_ns);
371 refcount_inc(&fc->log->usage);
373 /* Can't call put until we've called ->dup */
374 ret = fc->ops->dup(fc, src_fc);
378 ret = security_fs_context_dup(fc, src_fc);
387 EXPORT_SYMBOL(vfs_dup_fs_context);
390 * logfc - Log a message to a filesystem context
391 * @fc: The filesystem context to log to.
392 * @fmt: The format of the buffer.
394 void logfc(struct fs_context *fc, const char *fmt, ...)
396 static const char store_failure[] = "OOM: Can't store error string";
397 struct fc_log *log = fc ? fc->log : NULL;
404 if (!strchr(fmt, '%')) {
406 goto unformatted_string;
408 if (strcmp(fmt, "%s") == 0) {
409 p = va_arg(va, const char *);
410 goto unformatted_string;
413 q = kvasprintf(GFP_KERNEL, fmt, va);
421 if ((unsigned long)p >= (unsigned long)__start_rodata &&
422 (unsigned long)p < (unsigned long)__end_rodata)
424 if (log && within_module_core((unsigned long)p, log->owner))
426 q = kstrdup(p, GFP_KERNEL);
438 printk(KERN_WARNING "%s\n", q + 2);
441 printk(KERN_ERR "%s\n", q + 2);
444 printk(KERN_NOTICE "%s\n", q + 2);
450 unsigned int logsize = ARRAY_SIZE(log->buffer);
453 index = log->head & (logsize - 1);
454 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
455 sizeof(log->tail) != sizeof(u8));
456 if ((u8)(log->head - log->tail) == logsize) {
457 /* The buffer is full, discard the oldest message */
458 if (log->need_free & (1 << index))
459 kfree(log->buffer[index]);
463 log->buffer[index] = q;
464 log->need_free &= ~(1 << index);
465 log->need_free |= freeable << index;
470 EXPORT_SYMBOL(logfc);
473 * Free a logging structure.
475 static void put_fc_log(struct fs_context *fc)
477 struct fc_log *log = fc->log;
481 if (refcount_dec_and_test(&log->usage)) {
483 for (i = 0; i <= 7; i++)
484 if (log->need_free & (1 << i))
485 kfree(log->buffer[i]);
492 * put_fs_context - Dispose of a superblock configuration context.
493 * @fc: The context to dispose of.
495 void put_fs_context(struct fs_context *fc)
497 struct super_block *sb;
503 deactivate_super(sb);
506 if (fc->need_free && fc->ops && fc->ops->free)
509 security_free_mnt_opts(&fc->security);
511 put_user_ns(fc->user_ns);
515 put_filesystem(fc->fs_type);
519 EXPORT_SYMBOL(put_fs_context);
522 * Free the config for a filesystem that doesn't support fs_context.
524 static void legacy_fs_context_free(struct fs_context *fc)
526 struct legacy_fs_context *ctx = fc->fs_private;
529 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
530 kfree(ctx->legacy_data);
536 * Duplicate a legacy config.
538 static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
540 struct legacy_fs_context *ctx;
541 struct legacy_fs_context *src_ctx = src_fc->fs_private;
543 ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
547 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
548 ctx->legacy_data = kmemdup(src_ctx->legacy_data,
549 src_ctx->data_size, GFP_KERNEL);
550 if (!ctx->legacy_data) {
556 fc->fs_private = ctx;
561 * Add a parameter to a legacy config. We build up a comma-separated list of
564 static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
566 struct legacy_fs_context *ctx = fc->fs_private;
567 unsigned int size = ctx->data_size;
570 if (strcmp(param->key, "source") == 0) {
571 if (param->type != fs_value_is_string)
572 return invalf(fc, "VFS: Legacy: Non-string source");
574 return invalf(fc, "VFS: Legacy: Multiple sources");
575 fc->source = param->string;
576 param->string = NULL;
580 if ((fc->fs_type->fs_flags & FS_HAS_SUBTYPE) &&
581 strcmp(param->key, "subtype") == 0) {
582 if (param->type != fs_value_is_string)
583 return invalf(fc, "VFS: Legacy: Non-string subtype");
585 return invalf(fc, "VFS: Legacy: Multiple subtype");
586 fc->subtype = param->string;
587 param->string = NULL;
591 if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
592 return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
594 switch (param->type) {
595 case fs_value_is_string:
596 len = 1 + param->size;
598 case fs_value_is_flag:
599 len += strlen(param->key);
602 return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
606 if (len > PAGE_SIZE - 2 - size)
607 return invalf(fc, "VFS: Legacy: Cumulative options too large");
608 if (strchr(param->key, ',') ||
609 (param->type == fs_value_is_string &&
610 memchr(param->string, ',', param->size)))
611 return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
613 if (!ctx->legacy_data) {
614 ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
615 if (!ctx->legacy_data)
619 ctx->legacy_data[size++] = ',';
620 len = strlen(param->key);
621 memcpy(ctx->legacy_data + size, param->key, len);
623 if (param->type == fs_value_is_string) {
624 ctx->legacy_data[size++] = '=';
625 memcpy(ctx->legacy_data + size, param->string, param->size);
628 ctx->legacy_data[size] = '\0';
629 ctx->data_size = size;
630 ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
635 * Add monolithic mount data.
637 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
639 struct legacy_fs_context *ctx = fc->fs_private;
641 if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
642 pr_warn("VFS: Can't mix monolithic and individual options\n");
646 ctx->legacy_data = data;
647 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
648 if (!ctx->legacy_data)
651 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
653 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
657 * Get a mountable root with the legacy mount command.
659 static int legacy_get_tree(struct fs_context *fc)
661 struct legacy_fs_context *ctx = fc->fs_private;
662 struct super_block *sb;
665 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
666 fc->source, ctx->legacy_data);
668 return PTR_ERR(root);
680 static int legacy_reconfigure(struct fs_context *fc)
682 struct legacy_fs_context *ctx = fc->fs_private;
683 struct super_block *sb = fc->root->d_sb;
685 if (!sb->s_op->remount_fs)
688 return sb->s_op->remount_fs(sb, &fc->sb_flags,
689 ctx ? ctx->legacy_data : NULL);
692 const struct fs_context_operations legacy_fs_context_ops = {
693 .free = legacy_fs_context_free,
694 .dup = legacy_fs_context_dup,
695 .parse_param = legacy_parse_param,
696 .parse_monolithic = legacy_parse_monolithic,
697 .get_tree = legacy_get_tree,
698 .reconfigure = legacy_reconfigure,
702 * Initialise a legacy context for a filesystem that doesn't support
705 static int legacy_init_fs_context(struct fs_context *fc)
707 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
710 fc->ops = &legacy_fs_context_ops;
714 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
716 int (*monolithic_mount_data)(struct fs_context *, void *);
718 monolithic_mount_data = fc->ops->parse_monolithic;
719 if (!monolithic_mount_data)
720 monolithic_mount_data = generic_parse_monolithic;
722 return monolithic_mount_data(fc, data);
726 * Clean up a context after performing an action on it and put it into a state
727 * from where it can be used to reconfigure a superblock.
729 * Note that here we do only the parts that can't fail; the rest is in
730 * finish_clean_context() below and in between those fs_context is marked
731 * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
732 * successful mount or remount we need to report success to userland.
733 * Trying to do full reinit (for the sake of possible subsequent remount)
734 * and failing to allocate memory would've put us into a nasty situation.
735 * So here we only discard the old state and reinitialization is left
736 * until we actually try to reconfigure.
738 void vfs_clean_context(struct fs_context *fc)
740 if (fc->need_free && fc->ops && fc->ops->free)
742 fc->need_free = false;
743 fc->fs_private = NULL;
744 fc->s_fs_info = NULL;
746 security_free_mnt_opts(&fc->security);
752 fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
753 fc->phase = FS_CONTEXT_AWAITING_RECONF;
756 int finish_clean_context(struct fs_context *fc)
760 if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
763 if (fc->fs_type->init_fs_context)
764 error = fc->fs_type->init_fs_context(fc);
766 error = legacy_init_fs_context(fc);
767 if (unlikely(error)) {
768 fc->phase = FS_CONTEXT_FAILED;
771 fc->need_free = true;
772 fc->phase = FS_CONTEXT_RECONF_PARAMS;