blob: 57a12614addfd434f981a5b8c981a48ae1a71ceb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +05302#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>
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +05309#include <linux/fs_struct.h>
10#include <linux/fsnotify.h>
Jeff Mahoneyed5afea2011-04-14 15:22:16 -070011#include <linux/personality.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Al Viro2b891022017-04-08 18:16:56 -040013#include <linux/compat.h>
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053014#include "internal.h"
Al Viro15169fe2011-11-25 00:50:41 -050015#include "mount.h"
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053016
Al Viro6ccaaf52022-08-04 12:46:18 -040017static long do_sys_name_to_handle(const struct path *path,
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053018 struct file_handle __user *ufh,
Amir Goldstein96b2b072023-05-02 15:48:16 +030019 int __user *mnt_id, int fh_flags)
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053020{
21 long retval;
22 struct file_handle f_handle;
23 int handle_dwords, handle_bytes;
24 struct file_handle *handle = NULL;
25
26 /*
Amir Goldstein96b2b072023-05-02 15:48:16 +030027 * We need to make sure whether the file system support decoding of
28 * the file handle if decodeable file handle was requested.
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053029 */
Amir Goldstein66c62762023-10-23 21:07:58 +030030 if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags))
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
Nikita Zhandarovich3948aba2024-01-19 07:39:06 -080039 handle = kzalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053040 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
Amir Goldstein96b2b072023-05-02 15:48:16 +030047 /* we ask for a non connectable maybe decodeable file handle */
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053048 retval = exportfs_encode_fh(path->dentry,
49 (struct fid *)handle->f_handle,
Amir Goldstein96b2b072023-05-02 15:48:16 +030050 &handle_dwords, fh_flags);
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053051 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) ||
Amir Goldstein7cdafe62023-05-24 18:48:25 +030056 (retval == FILEID_INVALID) || (retval < 0)) {
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 */
Amir Goldstein7cdafe62023-05-24 18:48:25 +030062 if (retval == FILEID_INVALID || retval == -ENOSPC)
63 retval = -EOVERFLOW;
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053064 /*
65 * set the handle size to zero so we copy only
66 * non variable part of the file_handle
67 */
68 handle_bytes = 0;
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053069 } else
70 retval = 0;
71 /* copy the mount id */
David Windsor6391af62017-06-10 22:50:31 -040072 if (put_user(real_mount(path->mnt)->mnt_id, 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
Amir Goldstein96b2b072023-05-02 15:48:16 +030087 * and whether a decodable file handle is required.
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053088 *
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;
Amir Goldstein96b2b072023-05-02 15:48:16 +0300100 int fh_flags;
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +0530101 int err;
102
Amir Goldstein96b2b072023-05-02 15:48:16 +0300103 if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID))
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +0530104 return -EINVAL;
105
106 lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
Amir Goldstein96b2b072023-05-02 15:48:16 +0300107 fh_flags = (flag & AT_HANDLE_FID) ? EXPORT_FH_FID : 0;
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +0530108 if (flag & AT_EMPTY_PATH)
109 lookup_flags |= LOOKUP_EMPTY;
110 err = user_path_at(dfd, name, lookup_flags, &path);
111 if (!err) {
Amir Goldstein96b2b072023-05-02 15:48:16 +0300112 err = do_sys_name_to_handle(&path, handle, mnt_id, fh_flags);
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +0530113 path_put(&path);
114 }
115 return err;
116}
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530117
118static struct vfsmount *get_vfsmount_from_fd(int fd)
119{
Al Viro2903ff02012-08-28 12:52:22 -0400120 struct vfsmount *mnt;
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530121
122 if (fd == AT_FDCWD) {
123 struct fs_struct *fs = current->fs;
124 spin_lock(&fs->lock);
Al Viro2903ff02012-08-28 12:52:22 -0400125 mnt = mntget(fs->pwd.mnt);
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530126 spin_unlock(&fs->lock);
127 } else {
Al Viro2903ff02012-08-28 12:52:22 -0400128 struct fd f = fdget(fd);
129 if (!f.file)
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530130 return ERR_PTR(-EBADF);
Al Viro2903ff02012-08-28 12:52:22 -0400131 mnt = mntget(f.file->f_path.mnt);
132 fdput(f);
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530133 }
Al Viro2903ff02012-08-28 12:52:22 -0400134 return mnt;
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530135}
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
Randy Dunlap3d742d42021-02-24 12:00:48 -0800179 * directory. Ideally we would like CAP_DAC_SEARCH.
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530180 * 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 */
Sasha Levin161f873b2015-01-28 15:30:43 -0500202 *handle = f_handle;
203 if (copy_from_user(&handle->f_handle,
204 &ufh->f_handle,
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530205 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
Al Viro73ecf5c2017-10-14 13:18:33 -0400218static long do_handle_open(int mountdirfd, struct file_handle __user *ufh,
219 int open_flag)
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530220{
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 }
Al Viroffb37ca2021-04-01 19:00:57 -0400235 file = file_open_root(&path, "", open_flag, 0);
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530236 if (IS_ERR(file)) {
237 put_unused_fd(fd);
238 retval = PTR_ERR(file);
239 } else {
240 retval = fd;
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530241 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
Valdis Klētnieksa92c7ba2019-08-07 19:22:34 -0400251 * @flags: open flags.
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530252 *
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}
Al Viro2b891022017-04-08 18:16:56 -0400270
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