fhandle.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 t make sure wether 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 == 255) || (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 path path;
  106. if (fd == AT_FDCWD) {
  107. struct fs_struct *fs = current->fs;
  108. spin_lock(&fs->lock);
  109. path = fs->pwd;
  110. mntget(path.mnt);
  111. spin_unlock(&fs->lock);
  112. } else {
  113. int fput_needed;
  114. struct file *file = fget_light(fd, &fput_needed);
  115. if (!file)
  116. return ERR_PTR(-EBADF);
  117. path = file->f_path;
  118. mntget(path.mnt);
  119. fput_light(file, fput_needed);
  120. }
  121. return path.mnt;
  122. }
  123. static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
  124. {
  125. return 1;
  126. }
  127. static int do_handle_to_path(int mountdirfd, struct file_handle *handle,
  128. struct path *path)
  129. {
  130. int retval = 0;
  131. int handle_dwords;
  132. path->mnt = get_vfsmount_from_fd(mountdirfd);
  133. if (IS_ERR(path->mnt)) {
  134. retval = PTR_ERR(path->mnt);
  135. goto out_err;
  136. }
  137. /* change the handle size to multiple of sizeof(u32) */
  138. handle_dwords = handle->handle_bytes >> 2;
  139. path->dentry = exportfs_decode_fh(path->mnt,
  140. (struct fid *)handle->f_handle,
  141. handle_dwords, handle->handle_type,
  142. vfs_dentry_acceptable, NULL);
  143. if (IS_ERR(path->dentry)) {
  144. retval = PTR_ERR(path->dentry);
  145. goto out_mnt;
  146. }
  147. return 0;
  148. out_mnt:
  149. mntput(path->mnt);
  150. out_err:
  151. return retval;
  152. }
  153. static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
  154. struct path *path)
  155. {
  156. int retval = 0;
  157. struct file_handle f_handle;
  158. struct file_handle *handle = NULL;
  159. /*
  160. * With handle we don't look at the execute bit on the
  161. * the directory. Ideally we would like CAP_DAC_SEARCH.
  162. * But we don't have that
  163. */
  164. if (!capable(CAP_DAC_READ_SEARCH)) {
  165. retval = -EPERM;
  166. goto out_err;
  167. }
  168. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
  169. retval = -EFAULT;
  170. goto out_err;
  171. }
  172. if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
  173. (f_handle.handle_bytes == 0)) {
  174. retval = -EINVAL;
  175. goto out_err;
  176. }
  177. handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
  178. GFP_KERNEL);
  179. if (!handle) {
  180. retval = -ENOMEM;
  181. goto out_err;
  182. }
  183. /* copy the full handle */
  184. *handle = f_handle;
  185. if (copy_from_user(&handle->f_handle,
  186. &ufh->f_handle,
  187. f_handle.handle_bytes)) {
  188. retval = -EFAULT;
  189. goto out_handle;
  190. }
  191. retval = do_handle_to_path(mountdirfd, handle, path);
  192. out_handle:
  193. kfree(handle);
  194. out_err:
  195. return retval;
  196. }
  197. long do_handle_open(int mountdirfd,
  198. struct file_handle __user *ufh, int open_flag)
  199. {
  200. long retval = 0;
  201. struct path path;
  202. struct file *file;
  203. int fd;
  204. retval = handle_to_path(mountdirfd, ufh, &path);
  205. if (retval)
  206. return retval;
  207. fd = get_unused_fd_flags(open_flag);
  208. if (fd < 0) {
  209. path_put(&path);
  210. return fd;
  211. }
  212. file = file_open_root(path.dentry, path.mnt, "", open_flag);
  213. if (IS_ERR(file)) {
  214. put_unused_fd(fd);
  215. retval = PTR_ERR(file);
  216. } else {
  217. retval = fd;
  218. fsnotify_open(file);
  219. fd_install(fd, file);
  220. }
  221. path_put(&path);
  222. return retval;
  223. }
  224. /**
  225. * sys_open_by_handle_at: Open the file handle
  226. * @mountdirfd: directory file descriptor
  227. * @handle: file handle to be opened
  228. * @flag: open flags.
  229. *
  230. * @mountdirfd indicate the directory file descriptor
  231. * of the mount point. file handle is decoded relative
  232. * to the vfsmount pointed by the @mountdirfd. @flags
  233. * value is same as the open(2) flags.
  234. */
  235. SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  236. struct file_handle __user *, handle,
  237. int, flags)
  238. {
  239. long ret;
  240. if (force_o_largefile())
  241. flags |= O_LARGEFILE;
  242. ret = do_handle_open(mountdirfd, handle, flags);
  243. return ret;
  244. }