fhandle.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <asm/uaccess.h>
  11. #include "internal.h"
  12. static long do_sys_name_to_handle(struct path *path,
  13. struct file_handle __user *ufh,
  14. int __user *mnt_id)
  15. {
  16. long retval;
  17. struct file_handle f_handle;
  18. int handle_dwords, handle_bytes;
  19. struct file_handle *handle = NULL;
  20. /*
  21. * We need t make sure wether the file system
  22. * support decoding of the file handle
  23. */
  24. if (!path->mnt->mnt_sb->s_export_op ||
  25. !path->mnt->mnt_sb->s_export_op->fh_to_dentry)
  26. return -EOPNOTSUPP;
  27. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
  28. return -EFAULT;
  29. if (f_handle.handle_bytes > MAX_HANDLE_SZ)
  30. return -EINVAL;
  31. handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
  32. GFP_KERNEL);
  33. if (!handle)
  34. return -ENOMEM;
  35. /* convert handle size to multiple of sizeof(u32) */
  36. handle_dwords = f_handle.handle_bytes >> 2;
  37. /* we ask for a non connected handle */
  38. retval = exportfs_encode_fh(path->dentry,
  39. (struct fid *)handle->f_handle,
  40. &handle_dwords, 0);
  41. handle->handle_type = retval;
  42. /* convert handle size to bytes */
  43. handle_bytes = handle_dwords * sizeof(u32);
  44. handle->handle_bytes = handle_bytes;
  45. if ((handle->handle_bytes > f_handle.handle_bytes) ||
  46. (retval == 255) || (retval == -ENOSPC)) {
  47. /* As per old exportfs_encode_fh documentation
  48. * we could return ENOSPC to indicate overflow
  49. * But file system returned 255 always. So handle
  50. * both the values
  51. */
  52. /*
  53. * set the handle size to zero so we copy only
  54. * non variable part of the file_handle
  55. */
  56. handle_bytes = 0;
  57. retval = -EOVERFLOW;
  58. } else
  59. retval = 0;
  60. /* copy the mount id */
  61. if (copy_to_user(mnt_id, &path->mnt->mnt_id, sizeof(*mnt_id)) ||
  62. copy_to_user(ufh, handle,
  63. sizeof(struct file_handle) + handle_bytes))
  64. retval = -EFAULT;
  65. kfree(handle);
  66. return retval;
  67. }
  68. /**
  69. * sys_name_to_handle_at: convert name to handle
  70. * @dfd: directory relative to which name is interpreted if not absolute
  71. * @name: name that should be converted to handle.
  72. * @handle: resulting file handle
  73. * @mnt_id: mount id of the file system containing the file
  74. * @flag: flag value to indicate whether to follow symlink or not
  75. *
  76. * @handle->handle_size indicate the space available to store the
  77. * variable part of the file handle in bytes. If there is not
  78. * enough space, the field is updated to return the minimum
  79. * value required.
  80. */
  81. SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
  82. struct file_handle __user *, handle, int __user *, mnt_id,
  83. int, flag)
  84. {
  85. struct path path;
  86. int lookup_flags;
  87. int err;
  88. if ((flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  89. return -EINVAL;
  90. lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
  91. if (flag & AT_EMPTY_PATH)
  92. lookup_flags |= LOOKUP_EMPTY;
  93. err = user_path_at(dfd, name, lookup_flags, &path);
  94. if (!err) {
  95. err = do_sys_name_to_handle(&path, handle, mnt_id);
  96. path_put(&path);
  97. }
  98. return err;
  99. }
  100. static struct vfsmount *get_vfsmount_from_fd(int fd)
  101. {
  102. struct path path;
  103. if (fd == AT_FDCWD) {
  104. struct fs_struct *fs = current->fs;
  105. spin_lock(&fs->lock);
  106. path = fs->pwd;
  107. mntget(path.mnt);
  108. spin_unlock(&fs->lock);
  109. } else {
  110. int fput_needed;
  111. struct file *file = fget_light(fd, &fput_needed);
  112. if (!file)
  113. return ERR_PTR(-EBADF);
  114. path = file->f_path;
  115. mntget(path.mnt);
  116. fput_light(file, fput_needed);
  117. }
  118. return path.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. if (copy_from_user(handle, ufh,
  182. sizeof(struct file_handle) +
  183. f_handle.handle_bytes)) {
  184. retval = -EFAULT;
  185. goto out_handle;
  186. }
  187. retval = do_handle_to_path(mountdirfd, handle, path);
  188. out_handle:
  189. kfree(handle);
  190. out_err:
  191. return retval;
  192. }
  193. long do_handle_open(int mountdirfd,
  194. struct file_handle __user *ufh, int open_flag)
  195. {
  196. long retval = 0;
  197. struct path path;
  198. struct file *file;
  199. int fd;
  200. retval = handle_to_path(mountdirfd, ufh, &path);
  201. if (retval)
  202. return retval;
  203. fd = get_unused_fd_flags(open_flag);
  204. if (fd < 0) {
  205. path_put(&path);
  206. return fd;
  207. }
  208. file = file_open_root(path.dentry, path.mnt, "", open_flag);
  209. if (IS_ERR(file)) {
  210. put_unused_fd(fd);
  211. retval = PTR_ERR(file);
  212. } else {
  213. retval = fd;
  214. fsnotify_open(file);
  215. fd_install(fd, file);
  216. }
  217. path_put(&path);
  218. return retval;
  219. }
  220. /**
  221. * sys_open_by_handle_at: Open the file handle
  222. * @mountdirfd: directory file descriptor
  223. * @handle: file handle to be opened
  224. * @flag: open flags.
  225. *
  226. * @mountdirfd indicate the directory file descriptor
  227. * of the mount point. file handle is decoded relative
  228. * to the vfsmount pointed by the @mountdirfd. @flags
  229. * value is same as the open(2) flags.
  230. */
  231. SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  232. struct file_handle __user *, handle,
  233. int, flags)
  234. {
  235. long ret;
  236. if (force_o_largefile())
  237. flags |= O_LARGEFILE;
  238. ret = do_handle_open(mountdirfd, handle, flags);
  239. return ret;
  240. }