fhandle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/syscalls.h>
  3. #include <linux/slab.h>
  4. #include <linux/fs.h>
  5. #include <linux/file.h>
  6. #include <linux/mount.h>
  7. #include <linux/namei.h>
  8. #include <linux/exportfs.h>
  9. #include <linux/fs_struct.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/personality.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/compat.h>
  14. #include "internal.h"
  15. #include "mount.h"
  16. static long do_sys_name_to_handle(const struct path *path,
  17. struct file_handle __user *ufh,
  18. void __user *mnt_id, bool unique_mntid,
  19. int fh_flags)
  20. {
  21. long retval;
  22. struct file_handle f_handle;
  23. int handle_dwords, handle_bytes;
  24. struct file_handle *handle = NULL;
  25. /*
  26. * We need to make sure whether the file system support decoding of
  27. * the file handle if decodeable file handle was requested.
  28. */
  29. if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags))
  30. return -EOPNOTSUPP;
  31. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
  32. return -EFAULT;
  33. if (f_handle.handle_bytes > MAX_HANDLE_SZ)
  34. return -EINVAL;
  35. handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
  36. GFP_KERNEL);
  37. if (!handle)
  38. return -ENOMEM;
  39. /* convert handle size to multiple of sizeof(u32) */
  40. handle_dwords = f_handle.handle_bytes >> 2;
  41. /* we ask for a non connectable maybe decodeable file handle */
  42. retval = exportfs_encode_fh(path->dentry,
  43. (struct fid *)handle->f_handle,
  44. &handle_dwords, fh_flags);
  45. handle->handle_type = retval;
  46. /* convert handle size to bytes */
  47. handle_bytes = handle_dwords * sizeof(u32);
  48. handle->handle_bytes = handle_bytes;
  49. if ((handle->handle_bytes > f_handle.handle_bytes) ||
  50. (retval == FILEID_INVALID) || (retval < 0)) {
  51. /* As per old exportfs_encode_fh documentation
  52. * we could return ENOSPC to indicate overflow
  53. * But file system returned 255 always. So handle
  54. * both the values
  55. */
  56. if (retval == FILEID_INVALID || retval == -ENOSPC)
  57. retval = -EOVERFLOW;
  58. /*
  59. * set the handle size to zero so we copy only
  60. * non variable part of the file_handle
  61. */
  62. handle_bytes = 0;
  63. } else
  64. retval = 0;
  65. /* copy the mount id */
  66. if (unique_mntid) {
  67. if (put_user(real_mount(path->mnt)->mnt_id_unique,
  68. (u64 __user *) mnt_id))
  69. retval = -EFAULT;
  70. } else {
  71. if (put_user(real_mount(path->mnt)->mnt_id,
  72. (int __user *) mnt_id))
  73. retval = -EFAULT;
  74. }
  75. /* copy the handle */
  76. if (retval != -EFAULT &&
  77. copy_to_user(ufh, handle,
  78. struct_size(handle, f_handle, handle_bytes)))
  79. retval = -EFAULT;
  80. kfree(handle);
  81. return retval;
  82. }
  83. /**
  84. * sys_name_to_handle_at: convert name to handle
  85. * @dfd: directory relative to which name is interpreted if not absolute
  86. * @name: name that should be converted to handle.
  87. * @handle: resulting file handle
  88. * @mnt_id: mount id of the file system containing the file
  89. * (u64 if AT_HANDLE_MNT_ID_UNIQUE, otherwise int)
  90. * @flag: flag value to indicate whether to follow symlink or not
  91. * and whether a decodable file handle is required.
  92. *
  93. * @handle->handle_size indicate the space available to store the
  94. * variable part of the file handle in bytes. If there is not
  95. * enough space, the field is updated to return the minimum
  96. * value required.
  97. */
  98. SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
  99. struct file_handle __user *, handle, void __user *, mnt_id,
  100. int, flag)
  101. {
  102. struct path path;
  103. int lookup_flags;
  104. int fh_flags;
  105. int err;
  106. if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID |
  107. AT_HANDLE_MNT_ID_UNIQUE))
  108. return -EINVAL;
  109. lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
  110. fh_flags = (flag & AT_HANDLE_FID) ? EXPORT_FH_FID : 0;
  111. if (flag & AT_EMPTY_PATH)
  112. lookup_flags |= LOOKUP_EMPTY;
  113. err = user_path_at(dfd, name, lookup_flags, &path);
  114. if (!err) {
  115. err = do_sys_name_to_handle(&path, handle, mnt_id,
  116. flag & AT_HANDLE_MNT_ID_UNIQUE,
  117. fh_flags);
  118. path_put(&path);
  119. }
  120. return err;
  121. }
  122. static int get_path_from_fd(int fd, struct path *root)
  123. {
  124. if (fd == AT_FDCWD) {
  125. struct fs_struct *fs = current->fs;
  126. spin_lock(&fs->lock);
  127. *root = fs->pwd;
  128. path_get(root);
  129. spin_unlock(&fs->lock);
  130. } else {
  131. struct fd f = fdget(fd);
  132. if (!fd_file(f))
  133. return -EBADF;
  134. *root = fd_file(f)->f_path;
  135. path_get(root);
  136. fdput(f);
  137. }
  138. return 0;
  139. }
  140. enum handle_to_path_flags {
  141. HANDLE_CHECK_PERMS = (1 << 0),
  142. HANDLE_CHECK_SUBTREE = (1 << 1),
  143. };
  144. struct handle_to_path_ctx {
  145. struct path root;
  146. enum handle_to_path_flags flags;
  147. unsigned int fh_flags;
  148. };
  149. static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
  150. {
  151. struct handle_to_path_ctx *ctx = context;
  152. struct user_namespace *user_ns = current_user_ns();
  153. struct dentry *d, *root = ctx->root.dentry;
  154. struct mnt_idmap *idmap = mnt_idmap(ctx->root.mnt);
  155. int retval = 0;
  156. if (!root)
  157. return 1;
  158. /* Old permission model with global CAP_DAC_READ_SEARCH. */
  159. if (!ctx->flags)
  160. return 1;
  161. /*
  162. * Verify that the decoded dentry itself has a valid id mapping.
  163. * In case the decoded dentry is the mountfd root itself, this
  164. * verifies that the mountfd inode itself has a valid id mapping.
  165. */
  166. if (!privileged_wrt_inode_uidgid(user_ns, idmap, d_inode(dentry)))
  167. return 0;
  168. /*
  169. * It's racy as we're not taking rename_lock but we're able to ignore
  170. * permissions and we just need an approximation whether we were able
  171. * to follow a path to the file.
  172. *
  173. * It's also potentially expensive on some filesystems especially if
  174. * there is a deep path.
  175. */
  176. d = dget(dentry);
  177. while (d != root && !IS_ROOT(d)) {
  178. struct dentry *parent = dget_parent(d);
  179. /*
  180. * We know that we have the ability to override DAC permissions
  181. * as we've verified this earlier via CAP_DAC_READ_SEARCH. But
  182. * we also need to make sure that there aren't any unmapped
  183. * inodes in the path that would prevent us from reaching the
  184. * file.
  185. */
  186. if (!privileged_wrt_inode_uidgid(user_ns, idmap,
  187. d_inode(parent))) {
  188. dput(d);
  189. dput(parent);
  190. return retval;
  191. }
  192. dput(d);
  193. d = parent;
  194. }
  195. if (!(ctx->flags & HANDLE_CHECK_SUBTREE) || d == root)
  196. retval = 1;
  197. WARN_ON_ONCE(d != root && d != root->d_sb->s_root);
  198. dput(d);
  199. return retval;
  200. }
  201. static int do_handle_to_path(struct file_handle *handle, struct path *path,
  202. struct handle_to_path_ctx *ctx)
  203. {
  204. int handle_dwords;
  205. struct vfsmount *mnt = ctx->root.mnt;
  206. /* change the handle size to multiple of sizeof(u32) */
  207. handle_dwords = handle->handle_bytes >> 2;
  208. path->dentry = exportfs_decode_fh_raw(mnt,
  209. (struct fid *)handle->f_handle,
  210. handle_dwords, handle->handle_type,
  211. ctx->fh_flags,
  212. vfs_dentry_acceptable, ctx);
  213. if (IS_ERR_OR_NULL(path->dentry)) {
  214. if (path->dentry == ERR_PTR(-ENOMEM))
  215. return -ENOMEM;
  216. return -ESTALE;
  217. }
  218. path->mnt = mntget(mnt);
  219. return 0;
  220. }
  221. /*
  222. * Allow relaxed permissions of file handles if the caller has the
  223. * ability to mount the filesystem or create a bind-mount of the
  224. * provided @mountdirfd.
  225. *
  226. * In both cases the caller may be able to get an unobstructed way to
  227. * the encoded file handle. If the caller is only able to create a
  228. * bind-mount we need to verify that there are no locked mounts on top
  229. * of it that could prevent us from getting to the encoded file.
  230. *
  231. * In principle, locked mounts can prevent the caller from mounting the
  232. * filesystem but that only applies to procfs and sysfs neither of which
  233. * support decoding file handles.
  234. */
  235. static inline bool may_decode_fh(struct handle_to_path_ctx *ctx,
  236. unsigned int o_flags)
  237. {
  238. struct path *root = &ctx->root;
  239. /*
  240. * Restrict to O_DIRECTORY to provide a deterministic API that avoids a
  241. * confusing api in the face of disconnected non-dir dentries.
  242. *
  243. * There's only one dentry for each directory inode (VFS rule)...
  244. */
  245. if (!(o_flags & O_DIRECTORY))
  246. return false;
  247. if (ns_capable(root->mnt->mnt_sb->s_user_ns, CAP_SYS_ADMIN))
  248. ctx->flags = HANDLE_CHECK_PERMS;
  249. else if (is_mounted(root->mnt) &&
  250. ns_capable(real_mount(root->mnt)->mnt_ns->user_ns,
  251. CAP_SYS_ADMIN) &&
  252. !has_locked_children(real_mount(root->mnt), root->dentry))
  253. ctx->flags = HANDLE_CHECK_PERMS | HANDLE_CHECK_SUBTREE;
  254. else
  255. return false;
  256. /* Are we able to override DAC permissions? */
  257. if (!ns_capable(current_user_ns(), CAP_DAC_READ_SEARCH))
  258. return false;
  259. ctx->fh_flags = EXPORT_FH_DIR_ONLY;
  260. return true;
  261. }
  262. static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
  263. struct path *path, unsigned int o_flags)
  264. {
  265. int retval = 0;
  266. struct file_handle f_handle;
  267. struct file_handle *handle = NULL;
  268. struct handle_to_path_ctx ctx = {};
  269. retval = get_path_from_fd(mountdirfd, &ctx.root);
  270. if (retval)
  271. goto out_err;
  272. if (!capable(CAP_DAC_READ_SEARCH) && !may_decode_fh(&ctx, o_flags)) {
  273. retval = -EPERM;
  274. goto out_path;
  275. }
  276. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
  277. retval = -EFAULT;
  278. goto out_path;
  279. }
  280. if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
  281. (f_handle.handle_bytes == 0)) {
  282. retval = -EINVAL;
  283. goto out_path;
  284. }
  285. handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
  286. GFP_KERNEL);
  287. if (!handle) {
  288. retval = -ENOMEM;
  289. goto out_path;
  290. }
  291. /* copy the full handle */
  292. *handle = f_handle;
  293. if (copy_from_user(&handle->f_handle,
  294. &ufh->f_handle,
  295. f_handle.handle_bytes)) {
  296. retval = -EFAULT;
  297. goto out_handle;
  298. }
  299. retval = do_handle_to_path(handle, path, &ctx);
  300. out_handle:
  301. kfree(handle);
  302. out_path:
  303. path_put(&ctx.root);
  304. out_err:
  305. return retval;
  306. }
  307. static long do_handle_open(int mountdirfd, struct file_handle __user *ufh,
  308. int open_flag)
  309. {
  310. long retval = 0;
  311. struct path path;
  312. struct file *file;
  313. int fd;
  314. retval = handle_to_path(mountdirfd, ufh, &path, open_flag);
  315. if (retval)
  316. return retval;
  317. fd = get_unused_fd_flags(open_flag);
  318. if (fd < 0) {
  319. path_put(&path);
  320. return fd;
  321. }
  322. file = file_open_root(&path, "", open_flag, 0);
  323. if (IS_ERR(file)) {
  324. put_unused_fd(fd);
  325. retval = PTR_ERR(file);
  326. } else {
  327. retval = fd;
  328. fd_install(fd, file);
  329. }
  330. path_put(&path);
  331. return retval;
  332. }
  333. /**
  334. * sys_open_by_handle_at: Open the file handle
  335. * @mountdirfd: directory file descriptor
  336. * @handle: file handle to be opened
  337. * @flags: open flags.
  338. *
  339. * @mountdirfd indicate the directory file descriptor
  340. * of the mount point. file handle is decoded relative
  341. * to the vfsmount pointed by the @mountdirfd. @flags
  342. * value is same as the open(2) flags.
  343. */
  344. SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  345. struct file_handle __user *, handle,
  346. int, flags)
  347. {
  348. long ret;
  349. if (force_o_largefile())
  350. flags |= O_LARGEFILE;
  351. ret = do_handle_open(mountdirfd, handle, flags);
  352. return ret;
  353. }
  354. #ifdef CONFIG_COMPAT
  355. /*
  356. * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
  357. * doesn't set the O_LARGEFILE flag.
  358. */
  359. COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  360. struct file_handle __user *, handle, int, flags)
  361. {
  362. return do_handle_open(mountdirfd, handle, flags);
  363. }
  364. #endif