fsopen.c 11 KB

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