blob: 58a61f55e0d09f868bb1bac6a0b4d6e5042822ad [file] [log] [blame]
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +05301#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>
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +05308#include <linux/fs_struct.h>
9#include <linux/fsnotify.h>
Jeff Mahoneyed5afea2011-04-14 15:22:16 -070010#include <linux/personality.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080011#include <linux/uaccess.h>
Al Viro2b891022017-04-08 18:16:56 -040012#include <linux/compat.h>
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053013#include "internal.h"
Al Viro15169fe2011-11-25 00:50:41 -050014#include "mount.h"
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053015
16static 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 /*
Adam Buchbinder48fc7f72012-09-19 21:48:00 -040026 * We need to make sure whether the file system
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053027 * support decoding of the file handle
28 */
Al Virod8c95842011-12-07 18:16:57 -050029 if (!path->dentry->d_sb->s_export_op ||
30 !path->dentry->d_sb->s_export_op->fh_to_dentry)
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053031 return -EOPNOTSUPP;
32
33 if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
34 return -EFAULT;
35
36 if (f_handle.handle_bytes > MAX_HANDLE_SZ)
37 return -EINVAL;
38
39 handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
40 GFP_KERNEL);
41 if (!handle)
42 return -ENOMEM;
43
Adam Buchbinder48fc7f72012-09-19 21:48:00 -040044 /* convert handle size to multiple of sizeof(u32) */
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053045 handle_dwords = f_handle.handle_bytes >> 2;
46
47 /* we ask for a non connected handle */
48 retval = exportfs_encode_fh(path->dentry,
49 (struct fid *)handle->f_handle,
50 &handle_dwords, 0);
51 handle->handle_type = retval;
52 /* convert handle size to bytes */
53 handle_bytes = handle_dwords * sizeof(u32);
54 handle->handle_bytes = handle_bytes;
55 if ((handle->handle_bytes > f_handle.handle_bytes) ||
Namjae Jeon216b6cb2012-08-29 10:10:10 -040056 (retval == FILEID_INVALID) || (retval == -ENOSPC)) {
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053057 /* As per old exportfs_encode_fh documentation
58 * we could return ENOSPC to indicate overflow
59 * But file system returned 255 always. So handle
60 * both the values
61 */
62 /*
63 * set the handle size to zero so we copy only
64 * non variable part of the file_handle
65 */
66 handle_bytes = 0;
67 retval = -EOVERFLOW;
68 } else
69 retval = 0;
70 /* copy the mount id */
Al Viro15169fe2011-11-25 00:50:41 -050071 if (copy_to_user(mnt_id, &real_mount(path->mnt)->mnt_id,
72 sizeof(*mnt_id)) ||
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053073 copy_to_user(ufh, handle,
74 sizeof(struct file_handle) + handle_bytes))
75 retval = -EFAULT;
76 kfree(handle);
77 return retval;
78}
79
80/**
81 * sys_name_to_handle_at: convert name to handle
82 * @dfd: directory relative to which name is interpreted if not absolute
83 * @name: name that should be converted to handle.
84 * @handle: resulting file handle
85 * @mnt_id: mount id of the file system containing the file
86 * @flag: flag value to indicate whether to follow symlink or not
87 *
88 * @handle->handle_size indicate the space available to store the
89 * variable part of the file handle in bytes. If there is not
90 * enough space, the field is updated to return the minimum
91 * value required.
92 */
93SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
94 struct file_handle __user *, handle, int __user *, mnt_id,
95 int, flag)
96{
97 struct path path;
98 int lookup_flags;
99 int err;
100
101 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
102 return -EINVAL;
103
104 lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
105 if (flag & AT_EMPTY_PATH)
106 lookup_flags |= LOOKUP_EMPTY;
107 err = user_path_at(dfd, name, lookup_flags, &path);
108 if (!err) {
109 err = do_sys_name_to_handle(&path, handle, mnt_id);
110 path_put(&path);
111 }
112 return err;
113}
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530114
115static struct vfsmount *get_vfsmount_from_fd(int fd)
116{
Al Viro2903ff02012-08-28 12:52:22 -0400117 struct vfsmount *mnt;
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530118
119 if (fd == AT_FDCWD) {
120 struct fs_struct *fs = current->fs;
121 spin_lock(&fs->lock);
Al Viro2903ff02012-08-28 12:52:22 -0400122 mnt = mntget(fs->pwd.mnt);
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530123 spin_unlock(&fs->lock);
124 } else {
Al Viro2903ff02012-08-28 12:52:22 -0400125 struct fd f = fdget(fd);
126 if (!f.file)
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530127 return ERR_PTR(-EBADF);
Al Viro2903ff02012-08-28 12:52:22 -0400128 mnt = mntget(f.file->f_path.mnt);
129 fdput(f);
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530130 }
Al Viro2903ff02012-08-28 12:52:22 -0400131 return mnt;
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530132}
133
134static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
135{
136 return 1;
137}
138
139static int do_handle_to_path(int mountdirfd, struct file_handle *handle,
140 struct path *path)
141{
142 int retval = 0;
143 int handle_dwords;
144
145 path->mnt = get_vfsmount_from_fd(mountdirfd);
146 if (IS_ERR(path->mnt)) {
147 retval = PTR_ERR(path->mnt);
148 goto out_err;
149 }
150 /* change the handle size to multiple of sizeof(u32) */
151 handle_dwords = handle->handle_bytes >> 2;
152 path->dentry = exportfs_decode_fh(path->mnt,
153 (struct fid *)handle->f_handle,
154 handle_dwords, handle->handle_type,
155 vfs_dentry_acceptable, NULL);
156 if (IS_ERR(path->dentry)) {
157 retval = PTR_ERR(path->dentry);
158 goto out_mnt;
159 }
160 return 0;
161out_mnt:
162 mntput(path->mnt);
163out_err:
164 return retval;
165}
166
167static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
168 struct path *path)
169{
170 int retval = 0;
171 struct file_handle f_handle;
172 struct file_handle *handle = NULL;
173
174 /*
175 * With handle we don't look at the execute bit on the
176 * the directory. Ideally we would like CAP_DAC_SEARCH.
177 * But we don't have that
178 */
179 if (!capable(CAP_DAC_READ_SEARCH)) {
180 retval = -EPERM;
181 goto out_err;
182 }
183 if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
184 retval = -EFAULT;
185 goto out_err;
186 }
187 if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
188 (f_handle.handle_bytes == 0)) {
189 retval = -EINVAL;
190 goto out_err;
191 }
192 handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
193 GFP_KERNEL);
194 if (!handle) {
195 retval = -ENOMEM;
196 goto out_err;
197 }
198 /* copy the full handle */
Sasha Levin161f873b2015-01-28 15:30:43 -0500199 *handle = f_handle;
200 if (copy_from_user(&handle->f_handle,
201 &ufh->f_handle,
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530202 f_handle.handle_bytes)) {
203 retval = -EFAULT;
204 goto out_handle;
205 }
206
207 retval = do_handle_to_path(mountdirfd, handle, path);
208
209out_handle:
210 kfree(handle);
211out_err:
212 return retval;
213}
214
215long do_handle_open(int mountdirfd,
216 struct file_handle __user *ufh, int open_flag)
217{
218 long retval = 0;
219 struct path path;
220 struct file *file;
221 int fd;
222
223 retval = handle_to_path(mountdirfd, ufh, &path);
224 if (retval)
225 return retval;
226
227 fd = get_unused_fd_flags(open_flag);
228 if (fd < 0) {
229 path_put(&path);
230 return fd;
231 }
Jann Horn378c6522016-03-22 14:25:36 -0700232 file = file_open_root(path.dentry, path.mnt, "", open_flag, 0);
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530233 if (IS_ERR(file)) {
234 put_unused_fd(fd);
235 retval = PTR_ERR(file);
236 } else {
237 retval = fd;
238 fsnotify_open(file);
239 fd_install(fd, file);
240 }
241 path_put(&path);
242 return retval;
243}
244
245/**
246 * sys_open_by_handle_at: Open the file handle
247 * @mountdirfd: directory file descriptor
248 * @handle: file handle to be opened
249 * @flag: open flags.
250 *
251 * @mountdirfd indicate the directory file descriptor
252 * of the mount point. file handle is decoded relative
253 * to the vfsmount pointed by the @mountdirfd. @flags
254 * value is same as the open(2) flags.
255 */
256SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
257 struct file_handle __user *, handle,
258 int, flags)
259{
260 long ret;
261
262 if (force_o_largefile())
263 flags |= O_LARGEFILE;
264
265 ret = do_handle_open(mountdirfd, handle, flags);
266 return ret;
267}
Al Viro2b891022017-04-08 18:16:56 -0400268
269#ifdef CONFIG_COMPAT
270/*
271 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
272 * doesn't set the O_LARGEFILE flag.
273 */
274COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
275 struct file_handle __user *, handle, int, flags)
276{
277 return do_handle_open(mountdirfd, handle, flags);
278}
279#endif