vfs: Put security flags into the fs_context struct
[linux-block.git] / fs / fs_context.c
CommitLineData
9bc61ab1
DH
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.
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
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.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/fs_context.h>
15#include <linux/fs.h>
16#include <linux/mount.h>
17#include <linux/nsproxy.h>
18#include <linux/slab.h>
19#include <linux/magic.h>
20#include <linux/security.h>
21#include <linux/mnt_namespace.h>
22#include <linux/pid_namespace.h>
23#include <linux/user_namespace.h>
24#include <net/net_namespace.h>
25#include "mount.h"
26#include "internal.h"
27
28struct legacy_fs_context {
29 char *legacy_data; /* Data page for legacy filesystems */
30 size_t data_size;
31};
32
33static int legacy_init_fs_context(struct fs_context *fc);
34
35/**
36 * alloc_fs_context - Create a filesystem context.
37 * @fs_type: The filesystem type.
38 * @reference: The dentry from which this one derives (or NULL)
39 * @sb_flags: Filesystem/superblock flags (SB_*)
40 * @sb_flags_mask: Applicable members of @sb_flags
41 * @purpose: The purpose that this configuration shall be used for.
42 *
43 * Open a filesystem and create a mount context. The mount context is
44 * initialised with the supplied flags and, if a submount/automount from
45 * another superblock (referred to by @reference) is supplied, may have
46 * parameters such as namespaces copied across from that superblock.
47 */
48static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
49 struct dentry *reference,
50 unsigned int sb_flags,
51 unsigned int sb_flags_mask,
52 enum fs_context_purpose purpose)
53{
f3a09c92 54 int (*init_fs_context)(struct fs_context *);
9bc61ab1
DH
55 struct fs_context *fc;
56 int ret = -ENOMEM;
57
58 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
59 if (!fc)
60 return ERR_PTR(-ENOMEM);
61
62 fc->purpose = purpose;
63 fc->sb_flags = sb_flags;
64 fc->sb_flags_mask = sb_flags_mask;
65 fc->fs_type = get_filesystem(fs_type);
66 fc->cred = get_current_cred();
67 fc->net_ns = get_net(current->nsproxy->net_ns);
68
69 switch (purpose) {
70 case FS_CONTEXT_FOR_MOUNT:
71 fc->user_ns = get_user_ns(fc->cred->user_ns);
72 break;
e1a91586
AV
73 case FS_CONTEXT_FOR_SUBMOUNT:
74 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
75 break;
8d0347f6
DH
76 case FS_CONTEXT_FOR_RECONFIGURE:
77 /* We don't pin any namespaces as the superblock's
78 * subscriptions cannot be changed at this point.
79 */
80 atomic_inc(&reference->d_sb->s_active);
81 fc->root = dget(reference);
82 break;
9bc61ab1
DH
83 }
84
f3a09c92
AV
85 /* TODO: Make all filesystems support this unconditionally */
86 init_fs_context = fc->fs_type->init_fs_context;
87 if (!init_fs_context)
88 init_fs_context = legacy_init_fs_context;
89
90 ret = init_fs_context(fc);
9bc61ab1
DH
91 if (ret < 0)
92 goto err_fc;
93 fc->need_free = true;
94 return fc;
95
96err_fc:
97 put_fs_context(fc);
98 return ERR_PTR(ret);
99}
100
101struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
102 unsigned int sb_flags)
103{
104 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
105 FS_CONTEXT_FOR_MOUNT);
106}
107EXPORT_SYMBOL(fs_context_for_mount);
108
8d0347f6
DH
109struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
110 unsigned int sb_flags,
111 unsigned int sb_flags_mask)
112{
113 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
114 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
115}
116EXPORT_SYMBOL(fs_context_for_reconfigure);
117
e1a91586
AV
118struct fs_context *fs_context_for_submount(struct file_system_type *type,
119 struct dentry *reference)
120{
121 return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
122}
123EXPORT_SYMBOL(fs_context_for_submount);
124
c9ce29ed
AV
125void fc_drop_locked(struct fs_context *fc)
126{
127 struct super_block *sb = fc->root->d_sb;
128 dput(fc->root);
129 fc->root = NULL;
130 deactivate_locked_super(sb);
131}
132
9bc61ab1 133static void legacy_fs_context_free(struct fs_context *fc);
8d0347f6 134
9bc61ab1
DH
135/**
136 * put_fs_context - Dispose of a superblock configuration context.
137 * @fc: The context to dispose of.
138 */
139void put_fs_context(struct fs_context *fc)
140{
141 struct super_block *sb;
142
143 if (fc->root) {
144 sb = fc->root->d_sb;
145 dput(fc->root);
146 fc->root = NULL;
147 deactivate_super(sb);
148 }
149
f3a09c92
AV
150 if (fc->need_free && fc->ops && fc->ops->free)
151 fc->ops->free(fc);
9bc61ab1
DH
152
153 security_free_mnt_opts(&fc->security);
8d0347f6 154 put_net(fc->net_ns);
9bc61ab1
DH
155 put_user_ns(fc->user_ns);
156 put_cred(fc->cred);
157 kfree(fc->subtype);
158 put_filesystem(fc->fs_type);
159 kfree(fc->source);
160 kfree(fc);
161}
162EXPORT_SYMBOL(put_fs_context);
163
164/*
165 * Free the config for a filesystem that doesn't support fs_context.
166 */
167static void legacy_fs_context_free(struct fs_context *fc)
168{
169 kfree(fc->fs_private);
170}
171
172/*
173 * Add monolithic mount data.
174 */
175static int legacy_parse_monolithic(struct fs_context *fc, void *data)
176{
177 struct legacy_fs_context *ctx = fc->fs_private;
178 ctx->legacy_data = data;
179 if (!ctx->legacy_data)
180 return 0;
181 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
182 return 0;
183 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
184}
185
186/*
187 * Get a mountable root with the legacy mount command.
188 */
f3a09c92 189static int legacy_get_tree(struct fs_context *fc)
9bc61ab1
DH
190{
191 struct legacy_fs_context *ctx = fc->fs_private;
192 struct super_block *sb;
193 struct dentry *root;
194
195 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
196 fc->source, ctx->legacy_data);
197 if (IS_ERR(root))
198 return PTR_ERR(root);
199
200 sb = root->d_sb;
201 BUG_ON(!sb);
202
203 fc->root = root;
204 return 0;
205}
206
8d0347f6
DH
207/*
208 * Handle remount.
209 */
f3a09c92 210static int legacy_reconfigure(struct fs_context *fc)
8d0347f6
DH
211{
212 struct legacy_fs_context *ctx = fc->fs_private;
213 struct super_block *sb = fc->root->d_sb;
214
215 if (!sb->s_op->remount_fs)
216 return 0;
217
218 return sb->s_op->remount_fs(sb, &fc->sb_flags,
219 ctx ? ctx->legacy_data : NULL);
220}
221
f3a09c92
AV
222const struct fs_context_operations legacy_fs_context_ops = {
223 .free = legacy_fs_context_free,
224 .parse_monolithic = legacy_parse_monolithic,
225 .get_tree = legacy_get_tree,
226 .reconfigure = legacy_reconfigure,
227};
228
9bc61ab1
DH
229/*
230 * Initialise a legacy context for a filesystem that doesn't support
231 * fs_context.
232 */
233static int legacy_init_fs_context(struct fs_context *fc)
234{
235 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
236 if (!fc->fs_private)
237 return -ENOMEM;
f3a09c92 238 fc->ops = &legacy_fs_context_ops;
9bc61ab1
DH
239 return 0;
240}
241
242int parse_monolithic_mount_data(struct fs_context *fc, void *data)
243{
f3a09c92
AV
244 int (*monolithic_mount_data)(struct fs_context *, void *);
245 monolithic_mount_data = fc->ops->parse_monolithic;
246 return monolithic_mount_data(fc, data);
9bc61ab1 247}