]>
Commit | Line | Data |
---|---|---|
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 ([email protected]) | |
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 | ||
28 | struct legacy_fs_context { | |
29 | char *legacy_data; /* Data page for legacy filesystems */ | |
30 | size_t data_size; | |
31 | }; | |
32 | ||
33 | static 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 | */ | |
48 | static 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 | { | |
54 | struct fs_context *fc; | |
55 | int ret = -ENOMEM; | |
56 | ||
57 | fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL); | |
58 | if (!fc) | |
59 | return ERR_PTR(-ENOMEM); | |
60 | ||
61 | fc->purpose = purpose; | |
62 | fc->sb_flags = sb_flags; | |
63 | fc->sb_flags_mask = sb_flags_mask; | |
64 | fc->fs_type = get_filesystem(fs_type); | |
65 | fc->cred = get_current_cred(); | |
66 | fc->net_ns = get_net(current->nsproxy->net_ns); | |
67 | ||
68 | switch (purpose) { | |
69 | case FS_CONTEXT_FOR_MOUNT: | |
70 | fc->user_ns = get_user_ns(fc->cred->user_ns); | |
71 | break; | |
72 | } | |
73 | ||
74 | ret = legacy_init_fs_context(fc); | |
75 | if (ret < 0) | |
76 | goto err_fc; | |
77 | fc->need_free = true; | |
78 | return fc; | |
79 | ||
80 | err_fc: | |
81 | put_fs_context(fc); | |
82 | return ERR_PTR(ret); | |
83 | } | |
84 | ||
85 | struct fs_context *fs_context_for_mount(struct file_system_type *fs_type, | |
86 | unsigned int sb_flags) | |
87 | { | |
88 | return alloc_fs_context(fs_type, NULL, sb_flags, 0, | |
89 | FS_CONTEXT_FOR_MOUNT); | |
90 | } | |
91 | EXPORT_SYMBOL(fs_context_for_mount); | |
92 | ||
93 | static void legacy_fs_context_free(struct fs_context *fc); | |
94 | /** | |
95 | * put_fs_context - Dispose of a superblock configuration context. | |
96 | * @fc: The context to dispose of. | |
97 | */ | |
98 | void put_fs_context(struct fs_context *fc) | |
99 | { | |
100 | struct super_block *sb; | |
101 | ||
102 | if (fc->root) { | |
103 | sb = fc->root->d_sb; | |
104 | dput(fc->root); | |
105 | fc->root = NULL; | |
106 | deactivate_super(sb); | |
107 | } | |
108 | ||
109 | if (fc->need_free) | |
110 | legacy_fs_context_free(fc); | |
111 | ||
112 | security_free_mnt_opts(&fc->security); | |
113 | if (fc->net_ns) | |
114 | put_net(fc->net_ns); | |
115 | put_user_ns(fc->user_ns); | |
116 | put_cred(fc->cred); | |
117 | kfree(fc->subtype); | |
118 | put_filesystem(fc->fs_type); | |
119 | kfree(fc->source); | |
120 | kfree(fc); | |
121 | } | |
122 | EXPORT_SYMBOL(put_fs_context); | |
123 | ||
124 | /* | |
125 | * Free the config for a filesystem that doesn't support fs_context. | |
126 | */ | |
127 | static void legacy_fs_context_free(struct fs_context *fc) | |
128 | { | |
129 | kfree(fc->fs_private); | |
130 | } | |
131 | ||
132 | /* | |
133 | * Add monolithic mount data. | |
134 | */ | |
135 | static int legacy_parse_monolithic(struct fs_context *fc, void *data) | |
136 | { | |
137 | struct legacy_fs_context *ctx = fc->fs_private; | |
138 | ctx->legacy_data = data; | |
139 | if (!ctx->legacy_data) | |
140 | return 0; | |
141 | if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA) | |
142 | return 0; | |
143 | return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security); | |
144 | } | |
145 | ||
146 | /* | |
147 | * Get a mountable root with the legacy mount command. | |
148 | */ | |
149 | int legacy_get_tree(struct fs_context *fc) | |
150 | { | |
151 | struct legacy_fs_context *ctx = fc->fs_private; | |
152 | struct super_block *sb; | |
153 | struct dentry *root; | |
154 | ||
155 | root = fc->fs_type->mount(fc->fs_type, fc->sb_flags, | |
156 | fc->source, ctx->legacy_data); | |
157 | if (IS_ERR(root)) | |
158 | return PTR_ERR(root); | |
159 | ||
160 | sb = root->d_sb; | |
161 | BUG_ON(!sb); | |
162 | ||
163 | fc->root = root; | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /* | |
168 | * Initialise a legacy context for a filesystem that doesn't support | |
169 | * fs_context. | |
170 | */ | |
171 | static int legacy_init_fs_context(struct fs_context *fc) | |
172 | { | |
173 | fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL); | |
174 | if (!fc->fs_private) | |
175 | return -ENOMEM; | |
176 | return 0; | |
177 | } | |
178 | ||
179 | int parse_monolithic_mount_data(struct fs_context *fc, void *data) | |
180 | { | |
181 | return legacy_parse_monolithic(fc, data); | |
182 | } |