Linux Audio

Check our new training course

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