fsopen.c 12 KB

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