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