Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 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. |
| 4 | * |
| 5 | * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved. |
| 6 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 10 | #include <linux/module.h> |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 11 | #include <linux/fs_context.h> |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 12 | #include <linux/fs_parser.h> |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 13 | #include <linux/fs.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> |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 23 | #include <asm/sections.h> |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 24 | #include "mount.h" |
| 25 | #include "internal.h" |
| 26 | |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 27 | enum legacy_fs_param { |
| 28 | LEGACY_FS_UNSET_PARAMS, |
| 29 | LEGACY_FS_MONOLITHIC_PARAMS, |
| 30 | LEGACY_FS_INDIVIDUAL_PARAMS, |
| 31 | }; |
| 32 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 33 | struct legacy_fs_context { |
| 34 | char *legacy_data; /* Data page for legacy filesystems */ |
| 35 | size_t data_size; |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 36 | enum legacy_fs_param param_type; |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static int legacy_init_fs_context(struct fs_context *fc); |
| 40 | |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 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 }, |
| 46 | { "ro", SB_RDONLY }, |
| 47 | { "sync", SB_SYNCHRONOUS }, |
| 48 | }; |
| 49 | |
| 50 | static const struct constant_table common_clear_sb_flag[] = { |
| 51 | { "async", SB_SYNCHRONOUS }, |
| 52 | { "nolazytime", SB_LAZYTIME }, |
| 53 | { "nomand", SB_MANDLOCK }, |
| 54 | { "rw", SB_RDONLY }, |
| 55 | { "silent", SB_SILENT }, |
| 56 | }; |
| 57 | |
| 58 | static const char *const forbidden_sb_flag[] = { |
| 59 | "bind", |
| 60 | "dev", |
| 61 | "exec", |
| 62 | "move", |
| 63 | "noatime", |
| 64 | "nodev", |
| 65 | "nodiratime", |
| 66 | "noexec", |
| 67 | "norelatime", |
| 68 | "nostrictatime", |
| 69 | "nosuid", |
| 70 | "private", |
| 71 | "rec", |
| 72 | "relatime", |
| 73 | "remount", |
| 74 | "shared", |
| 75 | "slave", |
| 76 | "strictatime", |
| 77 | "suid", |
| 78 | "unbindable", |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * Check for a common mount option that manipulates s_flags. |
| 83 | */ |
| 84 | static int vfs_parse_sb_flag(struct fs_context *fc, const char *key) |
| 85 | { |
| 86 | unsigned int token; |
| 87 | unsigned int i; |
| 88 | |
| 89 | for (i = 0; i < ARRAY_SIZE(forbidden_sb_flag); i++) |
| 90 | if (strcmp(key, forbidden_sb_flag[i]) == 0) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | token = lookup_constant(common_set_sb_flag, key, 0); |
| 94 | if (token) { |
| 95 | fc->sb_flags |= token; |
| 96 | fc->sb_flags_mask |= token; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | token = lookup_constant(common_clear_sb_flag, key, 0); |
| 101 | if (token) { |
| 102 | fc->sb_flags &= ~token; |
| 103 | fc->sb_flags_mask |= token; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | return -ENOPARAM; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * vfs_parse_fs_param - Add a single parameter to a superblock config |
| 112 | * @fc: The filesystem context to modify |
| 113 | * @param: The parameter |
| 114 | * |
| 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. |
| 120 | * |
| 121 | * This may be called multiple times for a context. |
| 122 | * |
| 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. |
| 125 | */ |
| 126 | int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param) |
| 127 | { |
| 128 | int ret; |
| 129 | |
| 130 | if (!param->key) |
| 131 | return invalf(fc, "Unnamed parameter\n"); |
| 132 | |
| 133 | ret = vfs_parse_sb_flag(fc, param->key); |
| 134 | if (ret != -ENOPARAM) |
| 135 | return ret; |
| 136 | |
| 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. |
| 141 | */ |
| 142 | return ret; |
| 143 | |
| 144 | if (fc->ops->parse_param) { |
| 145 | ret = fc->ops->parse_param(fc, param); |
| 146 | if (ret != -ENOPARAM) |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* If the filesystem doesn't take any arguments, give it the |
| 151 | * default handling of source. |
| 152 | */ |
| 153 | if (strcmp(param->key, "source") == 0) { |
| 154 | if (param->type != fs_value_is_string) |
| 155 | return invalf(fc, "VFS: Non-string source"); |
| 156 | if (fc->source) |
| 157 | return invalf(fc, "VFS: Multiple sources"); |
| 158 | fc->source = param->string; |
| 159 | param->string = NULL; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | return invalf(fc, "%s: Unknown parameter '%s'", |
| 164 | fc->fs_type->name, param->key); |
| 165 | } |
| 166 | EXPORT_SYMBOL(vfs_parse_fs_param); |
| 167 | |
| 168 | /** |
| 169 | * vfs_parse_fs_string - Convenience function to just parse a string. |
| 170 | */ |
| 171 | int vfs_parse_fs_string(struct fs_context *fc, const char *key, |
| 172 | const char *value, size_t v_size) |
| 173 | { |
| 174 | int ret; |
| 175 | |
| 176 | struct fs_parameter param = { |
| 177 | .key = key, |
| 178 | .type = fs_value_is_string, |
| 179 | .size = v_size, |
| 180 | }; |
| 181 | |
| 182 | if (v_size > 0) { |
| 183 | param.string = kmemdup_nul(value, v_size, GFP_KERNEL); |
| 184 | if (!param.string) |
| 185 | return -ENOMEM; |
| 186 | } |
| 187 | |
| 188 | ret = vfs_parse_fs_param(fc, ¶m); |
| 189 | kfree(param.string); |
| 190 | return ret; |
| 191 | } |
| 192 | EXPORT_SYMBOL(vfs_parse_fs_string); |
| 193 | |
| 194 | /** |
| 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 |
| 198 | * |
| 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. |
| 201 | * |
| 202 | * Returns 0 on success or the error returned by the ->parse_option() fs_context |
| 203 | * operation on failure. |
| 204 | */ |
| 205 | int generic_parse_monolithic(struct fs_context *fc, void *data) |
| 206 | { |
| 207 | char *options = data, *key; |
| 208 | int ret = 0; |
| 209 | |
| 210 | if (!options) |
| 211 | return 0; |
| 212 | |
| 213 | ret = security_sb_eat_lsm_opts(options, &fc->security); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | while ((key = strsep(&options, ",")) != NULL) { |
| 218 | if (*key) { |
| 219 | size_t v_len = 0; |
| 220 | char *value = strchr(key, '='); |
| 221 | |
| 222 | if (value) { |
| 223 | if (value == key) |
| 224 | continue; |
| 225 | *value++ = 0; |
| 226 | v_len = strlen(value); |
| 227 | } |
| 228 | ret = vfs_parse_fs_string(fc, key, value, v_len); |
| 229 | if (ret < 0) |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | EXPORT_SYMBOL(generic_parse_monolithic); |
| 237 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 238 | /** |
| 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. |
| 245 | * |
| 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. |
| 250 | */ |
| 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) |
| 256 | { |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 257 | int (*init_fs_context)(struct fs_context *); |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 258 | struct fs_context *fc; |
| 259 | int ret = -ENOMEM; |
| 260 | |
| 261 | fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL); |
| 262 | if (!fc) |
| 263 | return ERR_PTR(-ENOMEM); |
| 264 | |
| 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); |
| 271 | |
David Howells | 24dcb3d | 2018-11-01 23:33:31 +0000 | [diff] [blame] | 272 | mutex_init(&fc->uapi_mutex); |
| 273 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 274 | switch (purpose) { |
| 275 | case FS_CONTEXT_FOR_MOUNT: |
| 276 | fc->user_ns = get_user_ns(fc->cred->user_ns); |
| 277 | break; |
Al Viro | e1a9158 | 2018-12-23 16:25:31 -0500 | [diff] [blame] | 278 | case FS_CONTEXT_FOR_SUBMOUNT: |
| 279 | fc->user_ns = get_user_ns(reference->d_sb->s_user_ns); |
| 280 | break; |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 281 | case FS_CONTEXT_FOR_RECONFIGURE: |
| 282 | /* We don't pin any namespaces as the superblock's |
| 283 | * subscriptions cannot be changed at this point. |
| 284 | */ |
| 285 | atomic_inc(&reference->d_sb->s_active); |
| 286 | fc->root = dget(reference); |
| 287 | break; |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 288 | } |
| 289 | |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 290 | /* TODO: Make all filesystems support this unconditionally */ |
| 291 | init_fs_context = fc->fs_type->init_fs_context; |
| 292 | if (!init_fs_context) |
| 293 | init_fs_context = legacy_init_fs_context; |
| 294 | |
| 295 | ret = init_fs_context(fc); |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 296 | if (ret < 0) |
| 297 | goto err_fc; |
| 298 | fc->need_free = true; |
| 299 | return fc; |
| 300 | |
| 301 | err_fc: |
| 302 | put_fs_context(fc); |
| 303 | return ERR_PTR(ret); |
| 304 | } |
| 305 | |
| 306 | struct fs_context *fs_context_for_mount(struct file_system_type *fs_type, |
| 307 | unsigned int sb_flags) |
| 308 | { |
| 309 | return alloc_fs_context(fs_type, NULL, sb_flags, 0, |
| 310 | FS_CONTEXT_FOR_MOUNT); |
| 311 | } |
| 312 | EXPORT_SYMBOL(fs_context_for_mount); |
| 313 | |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 314 | struct fs_context *fs_context_for_reconfigure(struct dentry *dentry, |
| 315 | unsigned int sb_flags, |
| 316 | unsigned int sb_flags_mask) |
| 317 | { |
| 318 | return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags, |
| 319 | sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE); |
| 320 | } |
| 321 | EXPORT_SYMBOL(fs_context_for_reconfigure); |
| 322 | |
Al Viro | e1a9158 | 2018-12-23 16:25:31 -0500 | [diff] [blame] | 323 | struct fs_context *fs_context_for_submount(struct file_system_type *type, |
| 324 | struct dentry *reference) |
| 325 | { |
| 326 | return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT); |
| 327 | } |
| 328 | EXPORT_SYMBOL(fs_context_for_submount); |
| 329 | |
Al Viro | c9ce29e | 2018-12-20 15:04:50 -0500 | [diff] [blame] | 330 | void fc_drop_locked(struct fs_context *fc) |
| 331 | { |
| 332 | struct super_block *sb = fc->root->d_sb; |
| 333 | dput(fc->root); |
| 334 | fc->root = NULL; |
| 335 | deactivate_locked_super(sb); |
| 336 | } |
| 337 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 338 | static void legacy_fs_context_free(struct fs_context *fc); |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 339 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 340 | /** |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 341 | * vfs_dup_fc_config: Duplicate a filesystem context. |
| 342 | * @src_fc: The context to copy. |
| 343 | */ |
| 344 | struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc) |
| 345 | { |
| 346 | struct fs_context *fc; |
| 347 | int ret; |
| 348 | |
| 349 | if (!src_fc->ops->dup) |
| 350 | return ERR_PTR(-EOPNOTSUPP); |
| 351 | |
| 352 | fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL); |
| 353 | if (!fc) |
| 354 | return ERR_PTR(-ENOMEM); |
| 355 | |
David Howells | 24dcb3d | 2018-11-01 23:33:31 +0000 | [diff] [blame] | 356 | mutex_init(&fc->uapi_mutex); |
| 357 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 358 | fc->fs_private = NULL; |
| 359 | fc->s_fs_info = NULL; |
| 360 | fc->source = NULL; |
| 361 | fc->security = NULL; |
| 362 | get_filesystem(fc->fs_type); |
| 363 | get_net(fc->net_ns); |
| 364 | get_user_ns(fc->user_ns); |
| 365 | get_cred(fc->cred); |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 366 | if (fc->log) |
| 367 | refcount_inc(&fc->log->usage); |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 368 | |
| 369 | /* Can't call put until we've called ->dup */ |
| 370 | ret = fc->ops->dup(fc, src_fc); |
| 371 | if (ret < 0) |
| 372 | goto err_fc; |
| 373 | |
| 374 | ret = security_fs_context_dup(fc, src_fc); |
| 375 | if (ret < 0) |
| 376 | goto err_fc; |
| 377 | return fc; |
| 378 | |
| 379 | err_fc: |
| 380 | put_fs_context(fc); |
| 381 | return ERR_PTR(ret); |
| 382 | } |
| 383 | EXPORT_SYMBOL(vfs_dup_fs_context); |
| 384 | |
David Howells | e7582e16 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 385 | /** |
| 386 | * logfc - Log a message to a filesystem context |
| 387 | * @fc: The filesystem context to log to. |
| 388 | * @fmt: The format of the buffer. |
| 389 | */ |
| 390 | void logfc(struct fs_context *fc, const char *fmt, ...) |
| 391 | { |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 392 | static const char store_failure[] = "OOM: Can't store error string"; |
| 393 | struct fc_log *log = fc ? fc->log : NULL; |
| 394 | const char *p; |
David Howells | e7582e16 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 395 | va_list va; |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 396 | char *q; |
| 397 | u8 freeable; |
David Howells | e7582e16 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 398 | |
| 399 | va_start(va, fmt); |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 400 | if (!strchr(fmt, '%')) { |
| 401 | p = fmt; |
| 402 | goto unformatted_string; |
| 403 | } |
| 404 | if (strcmp(fmt, "%s") == 0) { |
| 405 | p = va_arg(va, const char *); |
| 406 | goto unformatted_string; |
David Howells | e7582e16 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 407 | } |
| 408 | |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 409 | q = kvasprintf(GFP_KERNEL, fmt, va); |
| 410 | copied_string: |
| 411 | if (!q) |
| 412 | goto store_failure; |
| 413 | freeable = 1; |
| 414 | goto store_string; |
| 415 | |
| 416 | unformatted_string: |
| 417 | if ((unsigned long)p >= (unsigned long)__start_rodata && |
| 418 | (unsigned long)p < (unsigned long)__end_rodata) |
| 419 | goto const_string; |
| 420 | if (log && within_module_core((unsigned long)p, log->owner)) |
| 421 | goto const_string; |
| 422 | q = kstrdup(p, GFP_KERNEL); |
| 423 | goto copied_string; |
| 424 | |
| 425 | store_failure: |
| 426 | p = store_failure; |
| 427 | const_string: |
| 428 | q = (char *)p; |
| 429 | freeable = 0; |
| 430 | store_string: |
| 431 | if (!log) { |
| 432 | switch (fmt[0]) { |
| 433 | case 'w': |
| 434 | printk(KERN_WARNING "%s\n", q + 2); |
| 435 | break; |
| 436 | case 'e': |
| 437 | printk(KERN_ERR "%s\n", q + 2); |
| 438 | break; |
| 439 | default: |
| 440 | printk(KERN_NOTICE "%s\n", q + 2); |
| 441 | break; |
| 442 | } |
| 443 | if (freeable) |
| 444 | kfree(q); |
| 445 | } else { |
| 446 | unsigned int logsize = ARRAY_SIZE(log->buffer); |
| 447 | u8 index; |
| 448 | |
| 449 | index = log->head & (logsize - 1); |
| 450 | BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) || |
| 451 | sizeof(log->tail) != sizeof(u8)); |
| 452 | if ((u8)(log->head - log->tail) == logsize) { |
| 453 | /* The buffer is full, discard the oldest message */ |
| 454 | if (log->need_free & (1 << index)) |
| 455 | kfree(log->buffer[index]); |
| 456 | log->tail++; |
| 457 | } |
| 458 | |
| 459 | log->buffer[index] = q; |
| 460 | log->need_free &= ~(1 << index); |
| 461 | log->need_free |= freeable << index; |
| 462 | log->head++; |
| 463 | } |
David Howells | e7582e16 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 464 | va_end(va); |
| 465 | } |
| 466 | EXPORT_SYMBOL(logfc); |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Free a logging structure. |
| 470 | */ |
| 471 | static void put_fc_log(struct fs_context *fc) |
| 472 | { |
| 473 | struct fc_log *log = fc->log; |
| 474 | int i; |
| 475 | |
| 476 | if (log) { |
| 477 | if (refcount_dec_and_test(&log->usage)) { |
| 478 | fc->log = NULL; |
| 479 | for (i = 0; i <= 7; i++) |
| 480 | if (log->need_free & (1 << i)) |
| 481 | kfree(log->buffer[i]); |
| 482 | kfree(log); |
| 483 | } |
| 484 | } |
| 485 | } |
David Howells | e7582e16 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 486 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 487 | /** |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 488 | * put_fs_context - Dispose of a superblock configuration context. |
| 489 | * @fc: The context to dispose of. |
| 490 | */ |
| 491 | void put_fs_context(struct fs_context *fc) |
| 492 | { |
| 493 | struct super_block *sb; |
| 494 | |
| 495 | if (fc->root) { |
| 496 | sb = fc->root->d_sb; |
| 497 | dput(fc->root); |
| 498 | fc->root = NULL; |
| 499 | deactivate_super(sb); |
| 500 | } |
| 501 | |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 502 | if (fc->need_free && fc->ops && fc->ops->free) |
| 503 | fc->ops->free(fc); |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 504 | |
| 505 | security_free_mnt_opts(&fc->security); |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 506 | put_net(fc->net_ns); |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 507 | put_user_ns(fc->user_ns); |
| 508 | put_cred(fc->cred); |
| 509 | kfree(fc->subtype); |
David Howells | 007ec26 | 2018-11-01 23:34:29 +0000 | [diff] [blame] | 510 | put_fc_log(fc); |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 511 | put_filesystem(fc->fs_type); |
| 512 | kfree(fc->source); |
| 513 | kfree(fc); |
| 514 | } |
| 515 | EXPORT_SYMBOL(put_fs_context); |
| 516 | |
| 517 | /* |
| 518 | * Free the config for a filesystem that doesn't support fs_context. |
| 519 | */ |
| 520 | static void legacy_fs_context_free(struct fs_context *fc) |
| 521 | { |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 522 | struct legacy_fs_context *ctx = fc->fs_private; |
| 523 | |
| 524 | if (ctx) { |
| 525 | if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) |
| 526 | kfree(ctx->legacy_data); |
| 527 | kfree(ctx); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 532 | * Duplicate a legacy config. |
| 533 | */ |
| 534 | static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc) |
| 535 | { |
| 536 | struct legacy_fs_context *ctx; |
| 537 | struct legacy_fs_context *src_ctx = src_fc->fs_private; |
| 538 | |
| 539 | ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL); |
| 540 | if (!ctx) |
| 541 | return -ENOMEM; |
| 542 | |
| 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) { |
| 547 | kfree(ctx); |
| 548 | return -ENOMEM; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | fc->fs_private = ctx; |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 557 | * Add a parameter to a legacy config. We build up a comma-separated list of |
| 558 | * options. |
| 559 | */ |
| 560 | static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param) |
| 561 | { |
| 562 | struct legacy_fs_context *ctx = fc->fs_private; |
| 563 | unsigned int size = ctx->data_size; |
| 564 | size_t len = 0; |
| 565 | |
| 566 | if (strcmp(param->key, "source") == 0) { |
| 567 | if (param->type != fs_value_is_string) |
| 568 | return invalf(fc, "VFS: Legacy: Non-string source"); |
| 569 | if (fc->source) |
| 570 | return invalf(fc, "VFS: Legacy: Multiple sources"); |
| 571 | fc->source = param->string; |
| 572 | param->string = NULL; |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | if ((fc->fs_type->fs_flags & FS_HAS_SUBTYPE) && |
| 577 | strcmp(param->key, "subtype") == 0) { |
| 578 | if (param->type != fs_value_is_string) |
| 579 | return invalf(fc, "VFS: Legacy: Non-string subtype"); |
| 580 | if (fc->subtype) |
| 581 | return invalf(fc, "VFS: Legacy: Multiple subtype"); |
| 582 | fc->subtype = param->string; |
| 583 | param->string = NULL; |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS) |
| 588 | return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options"); |
| 589 | |
| 590 | switch (param->type) { |
| 591 | case fs_value_is_string: |
| 592 | len = 1 + param->size; |
| 593 | /* Fall through */ |
| 594 | case fs_value_is_flag: |
| 595 | len += strlen(param->key); |
| 596 | break; |
| 597 | default: |
| 598 | return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported", |
| 599 | param->key); |
| 600 | } |
| 601 | |
| 602 | if (len > PAGE_SIZE - 2 - size) |
| 603 | return invalf(fc, "VFS: Legacy: Cumulative options too large"); |
| 604 | if (strchr(param->key, ',') || |
| 605 | (param->type == fs_value_is_string && |
| 606 | memchr(param->string, ',', param->size))) |
| 607 | return invalf(fc, "VFS: Legacy: Option '%s' contained comma", |
| 608 | param->key); |
| 609 | if (!ctx->legacy_data) { |
| 610 | ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 611 | if (!ctx->legacy_data) |
| 612 | return -ENOMEM; |
| 613 | } |
| 614 | |
| 615 | ctx->legacy_data[size++] = ','; |
| 616 | len = strlen(param->key); |
| 617 | memcpy(ctx->legacy_data + size, param->key, len); |
| 618 | size += len; |
| 619 | if (param->type == fs_value_is_string) { |
| 620 | ctx->legacy_data[size++] = '='; |
| 621 | memcpy(ctx->legacy_data + size, param->string, param->size); |
| 622 | size += param->size; |
| 623 | } |
| 624 | ctx->legacy_data[size] = '\0'; |
| 625 | ctx->data_size = size; |
| 626 | ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS; |
| 627 | return 0; |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Add monolithic mount data. |
| 632 | */ |
| 633 | static int legacy_parse_monolithic(struct fs_context *fc, void *data) |
| 634 | { |
| 635 | struct legacy_fs_context *ctx = fc->fs_private; |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 636 | |
| 637 | if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) { |
| 638 | pr_warn("VFS: Can't mix monolithic and individual options\n"); |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 642 | ctx->legacy_data = data; |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 643 | ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS; |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 644 | if (!ctx->legacy_data) |
| 645 | return 0; |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 646 | |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 647 | if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA) |
| 648 | return 0; |
| 649 | return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security); |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Get a mountable root with the legacy mount command. |
| 654 | */ |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 655 | static int legacy_get_tree(struct fs_context *fc) |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 656 | { |
| 657 | struct legacy_fs_context *ctx = fc->fs_private; |
| 658 | struct super_block *sb; |
| 659 | struct dentry *root; |
| 660 | |
| 661 | root = fc->fs_type->mount(fc->fs_type, fc->sb_flags, |
| 662 | fc->source, ctx->legacy_data); |
| 663 | if (IS_ERR(root)) |
| 664 | return PTR_ERR(root); |
| 665 | |
| 666 | sb = root->d_sb; |
| 667 | BUG_ON(!sb); |
| 668 | |
| 669 | fc->root = root; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 674 | * Handle remount. |
| 675 | */ |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 676 | static int legacy_reconfigure(struct fs_context *fc) |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 677 | { |
| 678 | struct legacy_fs_context *ctx = fc->fs_private; |
| 679 | struct super_block *sb = fc->root->d_sb; |
| 680 | |
| 681 | if (!sb->s_op->remount_fs) |
| 682 | return 0; |
| 683 | |
| 684 | return sb->s_op->remount_fs(sb, &fc->sb_flags, |
| 685 | ctx ? ctx->legacy_data : NULL); |
| 686 | } |
| 687 | |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 688 | const struct fs_context_operations legacy_fs_context_ops = { |
| 689 | .free = legacy_fs_context_free, |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 690 | .dup = legacy_fs_context_dup, |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 691 | .parse_param = legacy_parse_param, |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 692 | .parse_monolithic = legacy_parse_monolithic, |
| 693 | .get_tree = legacy_get_tree, |
| 694 | .reconfigure = legacy_reconfigure, |
| 695 | }; |
| 696 | |
David Howells | 8d0347f | 2018-11-04 09:28:36 -0500 | [diff] [blame] | 697 | /* |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 698 | * Initialise a legacy context for a filesystem that doesn't support |
| 699 | * fs_context. |
| 700 | */ |
| 701 | static int legacy_init_fs_context(struct fs_context *fc) |
| 702 | { |
| 703 | fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL); |
| 704 | if (!fc->fs_private) |
| 705 | return -ENOMEM; |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 706 | fc->ops = &legacy_fs_context_ops; |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | int parse_monolithic_mount_data(struct fs_context *fc, void *data) |
| 711 | { |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 712 | int (*monolithic_mount_data)(struct fs_context *, void *); |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 713 | |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 714 | monolithic_mount_data = fc->ops->parse_monolithic; |
David Howells | 3e1aeb0 | 2018-11-01 23:07:25 +0000 | [diff] [blame] | 715 | if (!monolithic_mount_data) |
| 716 | monolithic_mount_data = generic_parse_monolithic; |
| 717 | |
Al Viro | f3a09c9 | 2018-12-23 18:55:56 -0500 | [diff] [blame] | 718 | return monolithic_mount_data(fc, data); |
David Howells | 9bc61ab | 2018-11-04 03:19:03 -0500 | [diff] [blame] | 719 | } |
David Howells | ecdab150 | 2018-11-01 23:36:09 +0000 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * Clean up a context after performing an action on it and put it into a state |
| 723 | * from where it can be used to reconfigure a superblock. |
| 724 | * |
| 725 | * Note that here we do only the parts that can't fail; the rest is in |
| 726 | * finish_clean_context() below and in between those fs_context is marked |
| 727 | * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after |
| 728 | * successful mount or remount we need to report success to userland. |
| 729 | * Trying to do full reinit (for the sake of possible subsequent remount) |
| 730 | * and failing to allocate memory would've put us into a nasty situation. |
| 731 | * So here we only discard the old state and reinitialization is left |
| 732 | * until we actually try to reconfigure. |
| 733 | */ |
| 734 | void vfs_clean_context(struct fs_context *fc) |
| 735 | { |
| 736 | if (fc->need_free && fc->ops && fc->ops->free) |
| 737 | fc->ops->free(fc); |
| 738 | fc->need_free = false; |
| 739 | fc->fs_private = NULL; |
| 740 | fc->s_fs_info = NULL; |
| 741 | fc->sb_flags = 0; |
| 742 | security_free_mnt_opts(&fc->security); |
| 743 | kfree(fc->subtype); |
| 744 | fc->subtype = NULL; |
| 745 | kfree(fc->source); |
| 746 | fc->source = NULL; |
| 747 | |
| 748 | fc->purpose = FS_CONTEXT_FOR_RECONFIGURE; |
| 749 | fc->phase = FS_CONTEXT_AWAITING_RECONF; |
| 750 | } |
| 751 | |
| 752 | int finish_clean_context(struct fs_context *fc) |
| 753 | { |
| 754 | int error; |
| 755 | |
| 756 | if (fc->phase != FS_CONTEXT_AWAITING_RECONF) |
| 757 | return 0; |
| 758 | |
| 759 | if (fc->fs_type->init_fs_context) |
| 760 | error = fc->fs_type->init_fs_context(fc); |
| 761 | else |
| 762 | error = legacy_init_fs_context(fc); |
| 763 | if (unlikely(error)) { |
| 764 | fc->phase = FS_CONTEXT_FAILED; |
| 765 | return error; |
| 766 | } |
| 767 | fc->need_free = true; |
| 768 | fc->phase = FS_CONTEXT_RECONF_PARAMS; |
| 769 | return 0; |
| 770 | } |