fhandle.c 6.5 KB

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