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. static long do_sys_name_to_handle(struct path *path,
  14. struct file_handle __user *ufh,
  15. int __user *mnt_id)
  16. {
  17. long retval;
  18. struct file_handle f_handle;
  19. int handle_dwords, handle_bytes;
  20. struct file_handle *handle = NULL;
  21. /*
  22. * We need t make sure wether the file system
  23. * support decoding of the file handle
  24. */
  25. if (!path->mnt->mnt_sb->s_export_op ||
  26. !path->mnt->mnt_sb->s_export_op->fh_to_dentry)
  27. return -EOPNOTSUPP;
  28. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
  29. return -EFAULT;
  30. if (f_handle.handle_bytes > MAX_HANDLE_SZ)
  31. return -EINVAL;
  32. handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
  33. GFP_KERNEL);
  34. if (!handle)
  35. return -ENOMEM;
  36. /* convert handle size to multiple of sizeof(u32) */
  37. handle_dwords = f_handle.handle_bytes >> 2;
  38. /* we ask for a non connected handle */
  39. retval = exportfs_encode_fh(path->dentry,
  40. (struct fid *)handle->f_handle,
  41. &handle_dwords, 0);
  42. handle->handle_type = retval;
  43. /* convert handle size to bytes */
  44. handle_bytes = handle_dwords * sizeof(u32);
  45. handle->handle_bytes = handle_bytes;
  46. if ((handle->handle_bytes > f_handle.handle_bytes) ||
  47. (retval == 255) || (retval == -ENOSPC)) {
  48. /* As per old exportfs_encode_fh documentation
  49. * we could return ENOSPC to indicate overflow
  50. * But file system returned 255 always. So handle
  51. * both the values
  52. */
  53. /*
  54. * set the handle size to zero so we copy only
  55. * non variable part of the file_handle
  56. */
  57. handle_bytes = 0;
  58. retval = -EOVERFLOW;
  59. } else
  60. retval = 0;
  61. /* copy the mount id */
  62. if (copy_to_user(mnt_id, &path->mnt->mnt_id, sizeof(*mnt_id)) ||
  63. copy_to_user(ufh, handle,
  64. sizeof(struct file_handle) + handle_bytes))
  65. retval = -EFAULT;
  66. kfree(handle);
  67. return retval;
  68. }
  69. /**
  70. * sys_name_to_handle_at: convert name to handle
  71. * @dfd: directory relative to which name is interpreted if not absolute
  72. * @name: name that should be converted to handle.
  73. * @handle: resulting file handle
  74. * @mnt_id: mount id of the file system containing the file
  75. * @flag: flag value to indicate whether to follow symlink or not
  76. *
  77. * @handle->handle_size indicate the space available to store the
  78. * variable part of the file handle in bytes. If there is not
  79. * enough space, the field is updated to return the minimum
  80. * value required.
  81. */
  82. SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
  83. struct file_handle __user *, handle, int __user *, mnt_id,
  84. int, flag)
  85. {
  86. struct path path;
  87. int lookup_flags;
  88. int err;
  89. if ((flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  90. return -EINVAL;
  91. lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
  92. if (flag & AT_EMPTY_PATH)
  93. lookup_flags |= LOOKUP_EMPTY;
  94. err = user_path_at(dfd, name, lookup_flags, &path);
  95. if (!err) {
  96. err = do_sys_name_to_handle(&path, handle, mnt_id);
  97. path_put(&path);
  98. }
  99. return err;
  100. }
  101. static struct vfsmount *get_vfsmount_from_fd(int fd)
  102. {
  103. struct path path;
  104. if (fd == AT_FDCWD) {
  105. struct fs_struct *fs = current->fs;
  106. spin_lock(&fs->lock);
  107. path = fs->pwd;
  108. mntget(path.mnt);
  109. spin_unlock(&fs->lock);
  110. } else {
  111. int fput_needed;
  112. struct file *file = fget_light(fd, &fput_needed);
  113. if (!file)
  114. return ERR_PTR(-EBADF);
  115. path = file->f_path;
  116. mntget(path.mnt);
  117. fput_light(file, fput_needed);
  118. }
  119. return path.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. if (copy_from_user(handle, ufh,
  183. sizeof(struct file_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);
  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. }