fhandle.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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(struct path *path,
  17. struct file_handle __user *ufh,
  18. int __user *mnt_id)
  19. {
  20. long retval;
  21. struct file_handle f_handle;
  22. int handle_dwords, handle_bytes;
  23. struct file_handle *handle = NULL;
  24. /*
  25. * We need to make sure whether the file system
  26. * support decoding of the file handle
  27. */
  28. if (!path->dentry->d_sb->s_export_op ||
  29. !path->dentry->d_sb->s_export_op->fh_to_dentry)
  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 = kmalloc(sizeof(struct file_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 connected handle */
  42. retval = exportfs_encode_fh(path->dentry,
  43. (struct fid *)handle->f_handle,
  44. &handle_dwords, 0);
  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 == -ENOSPC)) {
  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. /*
  57. * set the handle size to zero so we copy only
  58. * non variable part of the file_handle
  59. */
  60. handle_bytes = 0;
  61. retval = -EOVERFLOW;
  62. } else
  63. retval = 0;
  64. /* copy the mount id */
  65. if (put_user(real_mount(path->mnt)->mnt_id, mnt_id) ||
  66. copy_to_user(ufh, handle,
  67. sizeof(struct file_handle) + handle_bytes))
  68. retval = -EFAULT;
  69. kfree(handle);
  70. return retval;
  71. }
  72. /**
  73. * sys_name_to_handle_at: convert name to handle
  74. * @dfd: directory relative to which name is interpreted if not absolute
  75. * @name: name that should be converted to handle.
  76. * @handle: resulting file handle
  77. * @mnt_id: mount id of the file system containing the file
  78. * @flag: flag value to indicate whether to follow symlink or not
  79. *
  80. * @handle->handle_size indicate the space available to store the
  81. * variable part of the file handle in bytes. If there is not
  82. * enough space, the field is updated to return the minimum
  83. * value required.
  84. */
  85. SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
  86. struct file_handle __user *, handle, int __user *, mnt_id,
  87. int, flag)
  88. {
  89. struct path path;
  90. int lookup_flags;
  91. int err;
  92. if ((flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  93. return -EINVAL;
  94. lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
  95. if (flag & AT_EMPTY_PATH)
  96. lookup_flags |= LOOKUP_EMPTY;
  97. err = user_path_at(dfd, name, lookup_flags, &path);
  98. if (!err) {
  99. err = do_sys_name_to_handle(&path, handle, mnt_id);
  100. path_put(&path);
  101. }
  102. return err;
  103. }
  104. static struct vfsmount *get_vfsmount_from_fd(int fd)
  105. {
  106. struct vfsmount *mnt;
  107. if (fd == AT_FDCWD) {
  108. struct fs_struct *fs = current->fs;
  109. spin_lock(&fs->lock);
  110. mnt = mntget(fs->pwd.mnt);
  111. spin_unlock(&fs->lock);
  112. } else {
  113. struct fd f = fdget(fd);
  114. if (!f.file)
  115. return ERR_PTR(-EBADF);
  116. mnt = mntget(f.file->f_path.mnt);
  117. fdput(f);
  118. }
  119. return mnt;
  120. }
  121. static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
  122. {
  123. return 1;
  124. }
  125. static int do_handle_to_path(int mountdirfd, struct file_handle *handle,
  126. struct path *path)
  127. {
  128. int retval = 0;
  129. int handle_dwords;
  130. path->mnt = get_vfsmount_from_fd(mountdirfd);
  131. if (IS_ERR(path->mnt)) {
  132. retval = PTR_ERR(path->mnt);
  133. goto out_err;
  134. }
  135. /* change the handle size to multiple of sizeof(u32) */
  136. handle_dwords = handle->handle_bytes >> 2;
  137. path->dentry = exportfs_decode_fh(path->mnt,
  138. (struct fid *)handle->f_handle,
  139. handle_dwords, handle->handle_type,
  140. vfs_dentry_acceptable, NULL);
  141. if (IS_ERR(path->dentry)) {
  142. retval = PTR_ERR(path->dentry);
  143. goto out_mnt;
  144. }
  145. return 0;
  146. out_mnt:
  147. mntput(path->mnt);
  148. out_err:
  149. return retval;
  150. }
  151. static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
  152. struct path *path)
  153. {
  154. int retval = 0;
  155. struct file_handle f_handle;
  156. struct file_handle *handle = NULL;
  157. /*
  158. * With handle we don't look at the execute bit on the
  159. * the directory. Ideally we would like CAP_DAC_SEARCH.
  160. * But we don't have that
  161. */
  162. if (!capable(CAP_DAC_READ_SEARCH)) {
  163. retval = -EPERM;
  164. goto out_err;
  165. }
  166. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
  167. retval = -EFAULT;
  168. goto out_err;
  169. }
  170. if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
  171. (f_handle.handle_bytes == 0)) {
  172. retval = -EINVAL;
  173. goto out_err;
  174. }
  175. handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
  176. GFP_KERNEL);
  177. if (!handle) {
  178. retval = -ENOMEM;
  179. goto out_err;
  180. }
  181. /* copy the full handle */
  182. *handle = f_handle;
  183. if (copy_from_user(&handle->f_handle,
  184. &ufh->f_handle,
  185. f_handle.handle_bytes)) {
  186. retval = -EFAULT;
  187. goto out_handle;
  188. }
  189. retval = do_handle_to_path(mountdirfd, handle, path);
  190. out_handle:
  191. kfree(handle);
  192. out_err:
  193. return retval;
  194. }
  195. static long do_handle_open(int mountdirfd, struct file_handle __user *ufh,
  196. int open_flag)
  197. {
  198. long retval = 0;
  199. struct path path;
  200. struct file *file;
  201. int fd;
  202. retval = handle_to_path(mountdirfd, ufh, &path);
  203. if (retval)
  204. return retval;
  205. fd = get_unused_fd_flags(open_flag);
  206. if (fd < 0) {
  207. path_put(&path);
  208. return fd;
  209. }
  210. file = file_open_root(path.dentry, path.mnt, "", open_flag, 0);
  211. if (IS_ERR(file)) {
  212. put_unused_fd(fd);
  213. retval = PTR_ERR(file);
  214. } else {
  215. retval = fd;
  216. fsnotify_open(file);
  217. fd_install(fd, file);
  218. }
  219. path_put(&path);
  220. return retval;
  221. }
  222. /**
  223. * sys_open_by_handle_at: Open the file handle
  224. * @mountdirfd: directory file descriptor
  225. * @handle: file handle to be opened
  226. * @flags: open flags.
  227. *
  228. * @mountdirfd indicate the directory file descriptor
  229. * of the mount point. file handle is decoded relative
  230. * to the vfsmount pointed by the @mountdirfd. @flags
  231. * value is same as the open(2) flags.
  232. */
  233. SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  234. struct file_handle __user *, handle,
  235. int, flags)
  236. {
  237. long ret;
  238. if (force_o_largefile())
  239. flags |= O_LARGEFILE;
  240. ret = do_handle_open(mountdirfd, handle, flags);
  241. return ret;
  242. }
  243. #ifdef CONFIG_COMPAT
  244. /*
  245. * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
  246. * doesn't set the O_LARGEFILE flag.
  247. */
  248. COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  249. struct file_handle __user *, handle, int, flags)
  250. {
  251. return do_handle_open(mountdirfd, handle, flags);
  252. }
  253. #endif