]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - fs/fhandle.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/syscalls.h>
3 #include <linux/slab.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>
17 static long do_sys_name_to_handle(struct path
*path
,
18 struct file_handle __user
*ufh
,
22 struct file_handle f_handle
;
23 int handle_dwords
, handle_bytes
;
24 struct file_handle
*handle
= NULL
;
27 * We need to make sure whether the file system
28 * support decoding of the file handle
30 if (!path
->dentry
->d_sb
->s_export_op
||
31 !path
->dentry
->d_sb
->s_export_op
->fh_to_dentry
)
34 if (copy_from_user(&f_handle
, ufh
, sizeof(struct file_handle
)))
37 if (f_handle
.handle_bytes
> MAX_HANDLE_SZ
)
40 handle
= kmalloc(sizeof(struct file_handle
) + f_handle
.handle_bytes
,
45 /* convert handle size to multiple of sizeof(u32) */
46 handle_dwords
= f_handle
.handle_bytes
>> 2;
48 /* we ask for a non connected handle */
49 retval
= exportfs_encode_fh(path
->dentry
,
50 (struct fid
*)handle
->f_handle
,
52 handle
->handle_type
= retval
;
53 /* convert handle size to bytes */
54 handle_bytes
= handle_dwords
* sizeof(u32
);
55 handle
->handle_bytes
= handle_bytes
;
56 if ((handle
->handle_bytes
> f_handle
.handle_bytes
) ||
57 (retval
== FILEID_INVALID
) || (retval
== -ENOSPC
)) {
58 /* As per old exportfs_encode_fh documentation
59 * we could return ENOSPC to indicate overflow
60 * But file system returned 255 always. So handle
64 * set the handle size to zero so we copy only
65 * non variable part of the file_handle
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
))
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
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
93 SYSCALL_DEFINE5(name_to_handle_at
, int, dfd
, const char __user
*, name
,
94 struct file_handle __user
*, handle
, int __user
*, mnt_id
,
101 if ((flag
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
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
);
109 err
= do_sys_name_to_handle(&path
, handle
, mnt_id
);
115 static struct vfsmount
*get_vfsmount_from_fd(int fd
)
117 struct vfsmount
*mnt
;
119 if (fd
== AT_FDCWD
) {
120 struct fs_struct
*fs
= current
->fs
;
121 spin_lock(&fs
->lock
);
122 mnt
= mntget(fs
->pwd
.mnt
);
123 spin_unlock(&fs
->lock
);
125 struct fd f
= fdget(fd
);
127 return ERR_PTR(-EBADF
);
128 mnt
= mntget(f
.file
->f_path
.mnt
);
134 static int vfs_dentry_acceptable(void *context
, struct dentry
*dentry
)
139 static int do_handle_to_path(int mountdirfd
, struct file_handle
*handle
,
145 path
->mnt
= get_vfsmount_from_fd(mountdirfd
);
146 if (IS_ERR(path
->mnt
)) {
147 retval
= PTR_ERR(path
->mnt
);
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
);
167 static int handle_to_path(int mountdirfd
, struct file_handle __user
*ufh
,
171 struct file_handle f_handle
;
172 struct file_handle
*handle
= NULL
;
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
179 if (!capable(CAP_DAC_READ_SEARCH
)) {
183 if (copy_from_user(&f_handle
, ufh
, sizeof(struct file_handle
))) {
187 if ((f_handle
.handle_bytes
> MAX_HANDLE_SZ
) ||
188 (f_handle
.handle_bytes
== 0)) {
192 handle
= kmalloc(sizeof(struct file_handle
) + f_handle
.handle_bytes
,
198 /* copy the full handle */
200 if (copy_from_user(&handle
->f_handle
,
202 f_handle
.handle_bytes
)) {
207 retval
= do_handle_to_path(mountdirfd
, handle
, path
);
215 static long do_handle_open(int mountdirfd
, struct file_handle __user
*ufh
,
223 retval
= handle_to_path(mountdirfd
, ufh
, &path
);
227 fd
= get_unused_fd_flags(open_flag
);
232 file
= file_open_root(path
.dentry
, path
.mnt
, "", open_flag
, 0);
235 retval
= PTR_ERR(file
);
239 fd_install(fd
, file
);
246 * sys_open_by_handle_at: Open the file handle
247 * @mountdirfd: directory file descriptor
248 * @handle: file handle to be opened
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.
256 SYSCALL_DEFINE3(open_by_handle_at
, int, mountdirfd
,
257 struct file_handle __user
*, handle
,
262 if (force_o_largefile())
263 flags
|= O_LARGEFILE
;
265 ret
= do_handle_open(mountdirfd
, handle
, flags
);
271 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
272 * doesn't set the O_LARGEFILE flag.
274 COMPAT_SYSCALL_DEFINE3(open_by_handle_at
, int, mountdirfd
,
275 struct file_handle __user
*, handle
, int, flags
)
277 return do_handle_open(mountdirfd
, handle
, flags
);