]>
git.ipfire.org Git - thirdparty/linux.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(const struct path
*path
,
18 struct file_handle __user
*ufh
,
19 int __user
*mnt_id
, int fh_flags
)
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 support decoding of
28 * the file handle if decodeable file handle was requested.
29 * Otherwise, even empty export_operations are sufficient to opt-in
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
))
37 if (copy_from_user(&f_handle
, ufh
, sizeof(struct file_handle
)))
40 if (f_handle
.handle_bytes
> MAX_HANDLE_SZ
)
43 handle
= kmalloc(sizeof(struct file_handle
) + f_handle
.handle_bytes
,
48 /* convert handle size to multiple of sizeof(u32) */
49 handle_dwords
= f_handle
.handle_bytes
>> 2;
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
66 if (retval
== FILEID_INVALID
|| retval
== -ENOSPC
)
69 * set the handle size to zero so we copy only
70 * non variable part of the file_handle
75 /* copy the mount id */
76 if (put_user(real_mount(path
->mnt
)->mnt_id
, mnt_id
) ||
77 copy_to_user(ufh
, handle
,
78 sizeof(struct file_handle
) + handle_bytes
))
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.
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
98 SYSCALL_DEFINE5(name_to_handle_at
, int, dfd
, const char __user
*, name
,
99 struct file_handle __user
*, handle
, int __user
*, mnt_id
,
107 if (flag
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
| AT_HANDLE_FID
))
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
);
116 err
= do_sys_name_to_handle(&path
, handle
, mnt_id
, fh_flags
);
122 static struct vfsmount
*get_vfsmount_from_fd(int fd
)
124 struct vfsmount
*mnt
;
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
);
132 struct fd f
= fdget(fd
);
134 return ERR_PTR(-EBADF
);
135 mnt
= mntget(f
.file
->f_path
.mnt
);
141 static int vfs_dentry_acceptable(void *context
, struct dentry
*dentry
)
146 static int do_handle_to_path(int mountdirfd
, struct file_handle
*handle
,
152 path
->mnt
= get_vfsmount_from_fd(mountdirfd
);
153 if (IS_ERR(path
->mnt
)) {
154 retval
= PTR_ERR(path
->mnt
);
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
);
174 static int handle_to_path(int mountdirfd
, struct file_handle __user
*ufh
,
178 struct file_handle f_handle
;
179 struct file_handle
*handle
= NULL
;
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
186 if (!capable(CAP_DAC_READ_SEARCH
)) {
190 if (copy_from_user(&f_handle
, ufh
, sizeof(struct file_handle
))) {
194 if ((f_handle
.handle_bytes
> MAX_HANDLE_SZ
) ||
195 (f_handle
.handle_bytes
== 0)) {
199 handle
= kmalloc(sizeof(struct file_handle
) + f_handle
.handle_bytes
,
205 /* copy the full handle */
207 if (copy_from_user(&handle
->f_handle
,
209 f_handle
.handle_bytes
)) {
214 retval
= do_handle_to_path(mountdirfd
, handle
, path
);
222 static long do_handle_open(int mountdirfd
, struct file_handle __user
*ufh
,
230 retval
= handle_to_path(mountdirfd
, ufh
, &path
);
234 fd
= get_unused_fd_flags(open_flag
);
239 file
= file_open_root(&path
, "", open_flag
, 0);
242 retval
= PTR_ERR(file
);
245 fd_install(fd
, file
);
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.
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.
262 SYSCALL_DEFINE3(open_by_handle_at
, int, mountdirfd
,
263 struct file_handle __user
*, handle
,
268 if (force_o_largefile())
269 flags
|= O_LARGEFILE
;
271 ret
= do_handle_open(mountdirfd
, handle
, flags
);
277 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
278 * doesn't set the O_LARGEFILE flag.
280 COMPAT_SYSCALL_DEFINE3(open_by_handle_at
, int, mountdirfd
,
281 struct file_handle __user
*, handle
, int, flags
)
283 return do_handle_open(mountdirfd
, handle
, flags
);