fhandle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * It's racy as we're not taking rename_lock but we're able to ignore
  163. * permissions and we just need an approximation whether we were able
  164. * to follow a path to the file.
  165. *
  166. * It's also potentially expensive on some filesystems especially if
  167. * there is a deep path.
  168. */
  169. d = dget(dentry);
  170. while (d != root && !IS_ROOT(d)) {
  171. struct dentry *parent = dget_parent(d);
  172. /*
  173. * We know that we have the ability to override DAC permissions
  174. * as we've verified this earlier via CAP_DAC_READ_SEARCH. But
  175. * we also need to make sure that there aren't any unmapped
  176. * inodes in the path that would prevent us from reaching the
  177. * file.
  178. */
  179. if (!privileged_wrt_inode_uidgid(user_ns, idmap,
  180. d_inode(parent))) {
  181. dput(d);
  182. dput(parent);
  183. return retval;
  184. }
  185. dput(d);
  186. d = parent;
  187. }
  188. if (!(ctx->flags & HANDLE_CHECK_SUBTREE) || d == root)
  189. retval = 1;
  190. WARN_ON_ONCE(d != root && d != root->d_sb->s_root);
  191. dput(d);
  192. return retval;
  193. }
  194. static int do_handle_to_path(struct file_handle *handle, struct path *path,
  195. struct handle_to_path_ctx *ctx)
  196. {
  197. int handle_dwords;
  198. struct vfsmount *mnt = ctx->root.mnt;
  199. /* change the handle size to multiple of sizeof(u32) */
  200. handle_dwords = handle->handle_bytes >> 2;
  201. path->dentry = exportfs_decode_fh_raw(mnt,
  202. (struct fid *)handle->f_handle,
  203. handle_dwords, handle->handle_type,
  204. ctx->fh_flags,
  205. vfs_dentry_acceptable, ctx);
  206. if (IS_ERR_OR_NULL(path->dentry)) {
  207. if (path->dentry == ERR_PTR(-ENOMEM))
  208. return -ENOMEM;
  209. return -ESTALE;
  210. }
  211. path->mnt = mntget(mnt);
  212. return 0;
  213. }
  214. /*
  215. * Allow relaxed permissions of file handles if the caller has the
  216. * ability to mount the filesystem or create a bind-mount of the
  217. * provided @mountdirfd.
  218. *
  219. * In both cases the caller may be able to get an unobstructed way to
  220. * the encoded file handle. If the caller is only able to create a
  221. * bind-mount we need to verify that there are no locked mounts on top
  222. * of it that could prevent us from getting to the encoded file.
  223. *
  224. * In principle, locked mounts can prevent the caller from mounting the
  225. * filesystem but that only applies to procfs and sysfs neither of which
  226. * support decoding file handles.
  227. */
  228. static inline bool may_decode_fh(struct handle_to_path_ctx *ctx,
  229. unsigned int o_flags)
  230. {
  231. struct path *root = &ctx->root;
  232. /*
  233. * Restrict to O_DIRECTORY to provide a deterministic API that avoids a
  234. * confusing api in the face of disconnected non-dir dentries.
  235. *
  236. * There's only one dentry for each directory inode (VFS rule)...
  237. */
  238. if (!(o_flags & O_DIRECTORY))
  239. return false;
  240. if (ns_capable(root->mnt->mnt_sb->s_user_ns, CAP_SYS_ADMIN))
  241. ctx->flags = HANDLE_CHECK_PERMS;
  242. else if (is_mounted(root->mnt) &&
  243. ns_capable(real_mount(root->mnt)->mnt_ns->user_ns,
  244. CAP_SYS_ADMIN) &&
  245. !has_locked_children(real_mount(root->mnt), root->dentry))
  246. ctx->flags = HANDLE_CHECK_PERMS | HANDLE_CHECK_SUBTREE;
  247. else
  248. return false;
  249. /* Are we able to override DAC permissions? */
  250. if (!ns_capable(current_user_ns(), CAP_DAC_READ_SEARCH))
  251. return false;
  252. ctx->fh_flags = EXPORT_FH_DIR_ONLY;
  253. return true;
  254. }
  255. static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
  256. struct path *path, unsigned int o_flags)
  257. {
  258. int retval = 0;
  259. struct file_handle f_handle;
  260. struct file_handle *handle = NULL;
  261. struct handle_to_path_ctx ctx = {};
  262. retval = get_path_from_fd(mountdirfd, &ctx.root);
  263. if (retval)
  264. goto out_err;
  265. if (!capable(CAP_DAC_READ_SEARCH) && !may_decode_fh(&ctx, o_flags)) {
  266. retval = -EPERM;
  267. goto out_path;
  268. }
  269. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
  270. retval = -EFAULT;
  271. goto out_path;
  272. }
  273. if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
  274. (f_handle.handle_bytes == 0)) {
  275. retval = -EINVAL;
  276. goto out_path;
  277. }
  278. handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
  279. GFP_KERNEL);
  280. if (!handle) {
  281. retval = -ENOMEM;
  282. goto out_path;
  283. }
  284. /* copy the full handle */
  285. *handle = f_handle;
  286. if (copy_from_user(&handle->f_handle,
  287. &ufh->f_handle,
  288. f_handle.handle_bytes)) {
  289. retval = -EFAULT;
  290. goto out_handle;
  291. }
  292. retval = do_handle_to_path(handle, path, &ctx);
  293. out_handle:
  294. kfree(handle);
  295. out_path:
  296. path_put(&ctx.root);
  297. out_err:
  298. return retval;
  299. }
  300. static long do_handle_open(int mountdirfd, struct file_handle __user *ufh,
  301. int open_flag)
  302. {
  303. long retval = 0;
  304. struct path path;
  305. struct file *file;
  306. int fd;
  307. retval = handle_to_path(mountdirfd, ufh, &path, open_flag);
  308. if (retval)
  309. return retval;
  310. fd = get_unused_fd_flags(open_flag);
  311. if (fd < 0) {
  312. path_put(&path);
  313. return fd;
  314. }
  315. file = file_open_root(&path, "", open_flag, 0);
  316. if (IS_ERR(file)) {
  317. put_unused_fd(fd);
  318. retval = PTR_ERR(file);
  319. } else {
  320. retval = fd;
  321. fd_install(fd, file);
  322. }
  323. path_put(&path);
  324. return retval;
  325. }
  326. /**
  327. * sys_open_by_handle_at: Open the file handle
  328. * @mountdirfd: directory file descriptor
  329. * @handle: file handle to be opened
  330. * @flags: open flags.
  331. *
  332. * @mountdirfd indicate the directory file descriptor
  333. * of the mount point. file handle is decoded relative
  334. * to the vfsmount pointed by the @mountdirfd. @flags
  335. * value is same as the open(2) flags.
  336. */
  337. SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  338. struct file_handle __user *, handle,
  339. int, flags)
  340. {
  341. long ret;
  342. if (force_o_largefile())
  343. flags |= O_LARGEFILE;
  344. ret = do_handle_open(mountdirfd, handle, flags);
  345. return ret;
  346. }
  347. #ifdef CONFIG_COMPAT
  348. /*
  349. * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
  350. * doesn't set the O_LARGEFILE flag.
  351. */
  352. COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  353. struct file_handle __user *, handle, int, flags)
  354. {
  355. return do_handle_open(mountdirfd, handle, flags);
  356. }
  357. #endif