move mount_capable() further out
[linux-block.git] / fs / fsopen.c
CommitLineData
24dcb3d9
DH
1/* Filesystem access-by-fd.
2 *
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/fs_context.h>
ecdab150 13#include <linux/fs_parser.h>
24dcb3d9
DH
14#include <linux/slab.h>
15#include <linux/uaccess.h>
16#include <linux/syscalls.h>
17#include <linux/security.h>
18#include <linux/anon_inodes.h>
19#include <linux/namei.h>
20#include <linux/file.h>
21#include <uapi/linux/mount.h>
ecdab150 22#include "internal.h"
24dcb3d9
DH
23#include "mount.h"
24
007ec26c
DH
25/*
26 * Allow the user to read back any error, warning or informational messages.
27 */
28static ssize_t fscontext_read(struct file *file,
29 char __user *_buf, size_t len, loff_t *pos)
30{
31 struct fs_context *fc = file->private_data;
32 struct fc_log *log = fc->log;
33 unsigned int logsize = ARRAY_SIZE(log->buffer);
34 ssize_t ret;
35 char *p;
36 bool need_free;
37 int index, n;
38
39 ret = mutex_lock_interruptible(&fc->uapi_mutex);
40 if (ret < 0)
41 return ret;
42
43 if (log->head == log->tail) {
44 mutex_unlock(&fc->uapi_mutex);
45 return -ENODATA;
46 }
47
48 index = log->tail & (logsize - 1);
49 p = log->buffer[index];
50 need_free = log->need_free & (1 << index);
51 log->buffer[index] = NULL;
52 log->need_free &= ~(1 << index);
53 log->tail++;
54 mutex_unlock(&fc->uapi_mutex);
55
56 ret = -EMSGSIZE;
57 n = strlen(p);
58 if (n > len)
59 goto err_free;
60 ret = -EFAULT;
61 if (copy_to_user(_buf, p, n) != 0)
62 goto err_free;
63 ret = n;
64
65err_free:
66 if (need_free)
67 kfree(p);
68 return ret;
69}
70
24dcb3d9
DH
71static int fscontext_release(struct inode *inode, struct file *file)
72{
73 struct fs_context *fc = file->private_data;
74
75 if (fc) {
76 file->private_data = NULL;
77 put_fs_context(fc);
78 }
79 return 0;
80}
81
82const struct file_operations fscontext_fops = {
007ec26c 83 .read = fscontext_read,
24dcb3d9
DH
84 .release = fscontext_release,
85 .llseek = no_llseek,
86};
87
88/*
89 * Attach a filesystem context to a file and an fd.
90 */
91static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags)
92{
93 int fd;
94
1cdc415f 95 fd = anon_inode_getfd("[fscontext]", &fscontext_fops, fc,
24dcb3d9
DH
96 O_RDWR | o_flags);
97 if (fd < 0)
98 put_fs_context(fc);
99 return fd;
100}
101
007ec26c
DH
102static int fscontext_alloc_log(struct fs_context *fc)
103{
104 fc->log = kzalloc(sizeof(*fc->log), GFP_KERNEL);
105 if (!fc->log)
106 return -ENOMEM;
107 refcount_set(&fc->log->usage, 1);
108 fc->log->owner = fc->fs_type->owner;
109 return 0;
110}
111
24dcb3d9
DH
112/*
113 * Open a filesystem by name so that it can be configured for mounting.
114 *
115 * We are allowed to specify a container in which the filesystem will be
116 * opened, thereby indicating which namespaces will be used (notably, which
117 * network namespace will be used for network filesystems).
118 */
119SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
120{
121 struct file_system_type *fs_type;
122 struct fs_context *fc;
123 const char *fs_name;
007ec26c 124 int ret;
24dcb3d9
DH
125
126 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
127 return -EPERM;
128
129 if (flags & ~FSOPEN_CLOEXEC)
130 return -EINVAL;
131
132 fs_name = strndup_user(_fs_name, PAGE_SIZE);
133 if (IS_ERR(fs_name))
134 return PTR_ERR(fs_name);
135
136 fs_type = get_fs_type(fs_name);
137 kfree(fs_name);
138 if (!fs_type)
139 return -ENODEV;
140
141 fc = fs_context_for_mount(fs_type, 0);
142 put_filesystem(fs_type);
143 if (IS_ERR(fc))
144 return PTR_ERR(fc);
145
146 fc->phase = FS_CONTEXT_CREATE_PARAMS;
007ec26c
DH
147
148 ret = fscontext_alloc_log(fc);
149 if (ret < 0)
150 goto err_fc;
151
24dcb3d9 152 return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
007ec26c
DH
153
154err_fc:
155 put_fs_context(fc);
156 return ret;
24dcb3d9 157}
ecdab150 158
cf3cba4a
DH
159/*
160 * Pick a superblock into a context for reconfiguration.
161 */
162SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)
163{
164 struct fs_context *fc;
165 struct path target;
166 unsigned int lookup_flags;
167 int ret;
168
169 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
170 return -EPERM;
171
172 if ((flags & ~(FSPICK_CLOEXEC |
173 FSPICK_SYMLINK_NOFOLLOW |
174 FSPICK_NO_AUTOMOUNT |
175 FSPICK_EMPTY_PATH)) != 0)
176 return -EINVAL;
177
178 lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
179 if (flags & FSPICK_SYMLINK_NOFOLLOW)
180 lookup_flags &= ~LOOKUP_FOLLOW;
181 if (flags & FSPICK_NO_AUTOMOUNT)
182 lookup_flags &= ~LOOKUP_AUTOMOUNT;
183 if (flags & FSPICK_EMPTY_PATH)
184 lookup_flags |= LOOKUP_EMPTY;
185 ret = user_path_at(dfd, path, lookup_flags, &target);
186 if (ret < 0)
187 goto err;
188
189 ret = -EINVAL;
190 if (target.mnt->mnt_root != target.dentry)
191 goto err_path;
192
193 fc = fs_context_for_reconfigure(target.dentry, 0, 0);
194 if (IS_ERR(fc)) {
195 ret = PTR_ERR(fc);
196 goto err_path;
197 }
198
199 fc->phase = FS_CONTEXT_RECONF_PARAMS;
200
201 ret = fscontext_alloc_log(fc);
202 if (ret < 0)
203 goto err_fc;
204
205 path_put(&target);
206 return fscontext_create_fd(fc, flags & FSPICK_CLOEXEC ? O_CLOEXEC : 0);
207
208err_fc:
209 put_fs_context(fc);
210err_path:
211 path_put(&target);
212err:
213 return ret;
214}
215
ecdab150
DH
216/*
217 * Check the state and apply the configuration. Note that this function is
218 * allowed to 'steal' the value by setting param->xxx to NULL before returning.
219 */
220static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
221 struct fs_parameter *param)
222{
223 struct super_block *sb;
224 int ret;
225
226 ret = finish_clean_context(fc);
227 if (ret)
228 return ret;
229 switch (cmd) {
230 case FSCONFIG_CMD_CREATE:
231 if (fc->phase != FS_CONTEXT_CREATE_PARAMS)
232 return -EBUSY;
c3aabf07
AV
233 if (!mount_capable(fc))
234 return -EPERM;
ecdab150
DH
235 fc->phase = FS_CONTEXT_CREATING;
236 ret = vfs_get_tree(fc);
237 if (ret)
238 break;
239 sb = fc->root->d_sb;
240 ret = security_sb_kern_mount(sb);
241 if (unlikely(ret)) {
242 fc_drop_locked(fc);
243 break;
244 }
245 up_write(&sb->s_umount);
246 fc->phase = FS_CONTEXT_AWAITING_MOUNT;
247 return 0;
248 case FSCONFIG_CMD_RECONFIGURE:
249 if (fc->phase != FS_CONTEXT_RECONF_PARAMS)
250 return -EBUSY;
251 fc->phase = FS_CONTEXT_RECONFIGURING;
252 sb = fc->root->d_sb;
253 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
254 ret = -EPERM;
255 break;
256 }
257 down_write(&sb->s_umount);
258 ret = reconfigure_super(fc);
259 up_write(&sb->s_umount);
260 if (ret)
261 break;
262 vfs_clean_context(fc);
263 return 0;
264 default:
265 if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
266 fc->phase != FS_CONTEXT_RECONF_PARAMS)
267 return -EBUSY;
268
269 return vfs_parse_fs_param(fc, param);
270 }
271 fc->phase = FS_CONTEXT_FAILED;
272 return ret;
273}
274
275/**
276 * sys_fsconfig - Set parameters and trigger actions on a context
277 * @fd: The filesystem context to act upon
278 * @cmd: The action to take
279 * @_key: Where appropriate, the parameter key to set
280 * @_value: Where appropriate, the parameter value to set
281 * @aux: Additional information for the value
282 *
283 * This system call is used to set parameters on a context, including
284 * superblock settings, data source and security labelling.
285 *
286 * Actions include triggering the creation of a superblock and the
287 * reconfiguration of the superblock attached to the specified context.
288 *
289 * When setting a parameter, @cmd indicates the type of value being proposed
290 * and @_key indicates the parameter to be altered.
291 *
292 * @_value and @aux are used to specify the value, should a value be required:
293 *
294 * (*) fsconfig_set_flag: No value is specified. The parameter must be boolean
295 * in nature. The key may be prefixed with "no" to invert the
296 * setting. @_value must be NULL and @aux must be 0.
297 *
298 * (*) fsconfig_set_string: A string value is specified. The parameter can be
299 * expecting boolean, integer, string or take a path. A conversion to an
300 * appropriate type will be attempted (which may include looking up as a
301 * path). @_value points to a NUL-terminated string and @aux must be 0.
302 *
303 * (*) fsconfig_set_binary: A binary blob is specified. @_value points to the
304 * blob and @aux indicates its size. The parameter must be expecting a
305 * blob.
306 *
307 * (*) fsconfig_set_path: A non-empty path is specified. The parameter must be
308 * expecting a path object. @_value points to a NUL-terminated string that
309 * is the path and @aux is a file descriptor at which to start a relative
310 * lookup or AT_FDCWD.
311 *
312 * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
313 * implied.
314 *
315 * (*) fsconfig_set_fd: An open file descriptor is specified. @_value must be
316 * NULL and @aux indicates the file descriptor.
317 */
318SYSCALL_DEFINE5(fsconfig,
319 int, fd,
320 unsigned int, cmd,
321 const char __user *, _key,
322 const void __user *, _value,
323 int, aux)
324{
325 struct fs_context *fc;
326 struct fd f;
327 int ret;
328
329 struct fs_parameter param = {
330 .type = fs_value_is_undefined,
331 };
332
333 if (fd < 0)
334 return -EINVAL;
335
336 switch (cmd) {
337 case FSCONFIG_SET_FLAG:
338 if (!_key || _value || aux)
339 return -EINVAL;
340 break;
341 case FSCONFIG_SET_STRING:
342 if (!_key || !_value || aux)
343 return -EINVAL;
344 break;
345 case FSCONFIG_SET_BINARY:
346 if (!_key || !_value || aux <= 0 || aux > 1024 * 1024)
347 return -EINVAL;
348 break;
349 case FSCONFIG_SET_PATH:
350 case FSCONFIG_SET_PATH_EMPTY:
351 if (!_key || !_value || (aux != AT_FDCWD && aux < 0))
352 return -EINVAL;
353 break;
354 case FSCONFIG_SET_FD:
355 if (!_key || _value || aux < 0)
356 return -EINVAL;
357 break;
358 case FSCONFIG_CMD_CREATE:
359 case FSCONFIG_CMD_RECONFIGURE:
360 if (_key || _value || aux)
361 return -EINVAL;
362 break;
363 default:
364 return -EOPNOTSUPP;
365 }
366
367 f = fdget(fd);
368 if (!f.file)
369 return -EBADF;
370 ret = -EINVAL;
371 if (f.file->f_op != &fscontext_fops)
372 goto out_f;
373
374 fc = f.file->private_data;
375 if (fc->ops == &legacy_fs_context_ops) {
376 switch (cmd) {
377 case FSCONFIG_SET_BINARY:
378 case FSCONFIG_SET_PATH:
379 case FSCONFIG_SET_PATH_EMPTY:
380 case FSCONFIG_SET_FD:
381 ret = -EOPNOTSUPP;
382 goto out_f;
383 }
384 }
385
386 if (_key) {
387 param.key = strndup_user(_key, 256);
388 if (IS_ERR(param.key)) {
389 ret = PTR_ERR(param.key);
390 goto out_f;
391 }
392 }
393
394 switch (cmd) {
395 case FSCONFIG_SET_FLAG:
396 param.type = fs_value_is_flag;
397 break;
398 case FSCONFIG_SET_STRING:
399 param.type = fs_value_is_string;
400 param.string = strndup_user(_value, 256);
401 if (IS_ERR(param.string)) {
402 ret = PTR_ERR(param.string);
403 goto out_key;
404 }
405 param.size = strlen(param.string);
406 break;
407 case FSCONFIG_SET_BINARY:
408 param.type = fs_value_is_blob;
409 param.size = aux;
410 param.blob = memdup_user_nul(_value, aux);
411 if (IS_ERR(param.blob)) {
412 ret = PTR_ERR(param.blob);
413 goto out_key;
414 }
415 break;
416 case FSCONFIG_SET_PATH:
417 param.type = fs_value_is_filename;
418 param.name = getname_flags(_value, 0, NULL);
419 if (IS_ERR(param.name)) {
420 ret = PTR_ERR(param.name);
421 goto out_key;
422 }
423 param.dirfd = aux;
424 param.size = strlen(param.name->name);
425 break;
426 case FSCONFIG_SET_PATH_EMPTY:
427 param.type = fs_value_is_filename_empty;
428 param.name = getname_flags(_value, LOOKUP_EMPTY, NULL);
429 if (IS_ERR(param.name)) {
430 ret = PTR_ERR(param.name);
431 goto out_key;
432 }
433 param.dirfd = aux;
434 param.size = strlen(param.name->name);
435 break;
436 case FSCONFIG_SET_FD:
437 param.type = fs_value_is_file;
438 ret = -EBADF;
439 param.file = fget(aux);
440 if (!param.file)
441 goto out_key;
442 break;
443 default:
444 break;
445 }
446
447 ret = mutex_lock_interruptible(&fc->uapi_mutex);
448 if (ret == 0) {
449 ret = vfs_fsconfig_locked(fc, cmd, &param);
450 mutex_unlock(&fc->uapi_mutex);
451 }
452
453 /* Clean up the our record of any value that we obtained from
454 * userspace. Note that the value may have been stolen by the LSM or
455 * filesystem, in which case the value pointer will have been cleared.
456 */
457 switch (cmd) {
458 case FSCONFIG_SET_STRING:
459 case FSCONFIG_SET_BINARY:
460 kfree(param.string);
461 break;
462 case FSCONFIG_SET_PATH:
463 case FSCONFIG_SET_PATH_EMPTY:
464 if (param.name)
465 putname(param.name);
466 break;
467 case FSCONFIG_SET_FD:
468 if (param.file)
469 fput(param.file);
470 break;
471 default:
472 break;
473 }
474out_key:
475 kfree(param.key);
476out_f:
477 fdput(f);
478 return ret;
479}