162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#include <linux/syscalls.h> 362306a36Sopenharmony_ci#include <linux/slab.h> 462306a36Sopenharmony_ci#include <linux/fs.h> 562306a36Sopenharmony_ci#include <linux/file.h> 662306a36Sopenharmony_ci#include <linux/mount.h> 762306a36Sopenharmony_ci#include <linux/namei.h> 862306a36Sopenharmony_ci#include <linux/exportfs.h> 962306a36Sopenharmony_ci#include <linux/fs_struct.h> 1062306a36Sopenharmony_ci#include <linux/fsnotify.h> 1162306a36Sopenharmony_ci#include <linux/personality.h> 1262306a36Sopenharmony_ci#include <linux/uaccess.h> 1362306a36Sopenharmony_ci#include <linux/compat.h> 1462306a36Sopenharmony_ci#include "internal.h" 1562306a36Sopenharmony_ci#include "mount.h" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cistatic long do_sys_name_to_handle(const struct path *path, 1862306a36Sopenharmony_ci struct file_handle __user *ufh, 1962306a36Sopenharmony_ci int __user *mnt_id, int fh_flags) 2062306a36Sopenharmony_ci{ 2162306a36Sopenharmony_ci long retval; 2262306a36Sopenharmony_ci struct file_handle f_handle; 2362306a36Sopenharmony_ci int handle_dwords, handle_bytes; 2462306a36Sopenharmony_ci struct file_handle *handle = NULL; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci /* 2762306a36Sopenharmony_ci * We need to make sure whether the file system support decoding of 2862306a36Sopenharmony_ci * the file handle if decodeable file handle was requested. 2962306a36Sopenharmony_ci * Otherwise, even empty export_operations are sufficient to opt-in 3062306a36Sopenharmony_ci * to encoding FIDs. 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_ci if (!path->dentry->d_sb->s_export_op || 3362306a36Sopenharmony_ci (!(fh_flags & EXPORT_FH_FID) && 3462306a36Sopenharmony_ci !path->dentry->d_sb->s_export_op->fh_to_dentry)) 3562306a36Sopenharmony_ci return -EOPNOTSUPP; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) 3862306a36Sopenharmony_ci return -EFAULT; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci if (f_handle.handle_bytes > MAX_HANDLE_SZ) 4162306a36Sopenharmony_ci return -EINVAL; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci handle = kzalloc(sizeof(struct file_handle) + f_handle.handle_bytes, 4462306a36Sopenharmony_ci GFP_KERNEL); 4562306a36Sopenharmony_ci if (!handle) 4662306a36Sopenharmony_ci return -ENOMEM; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci /* convert handle size to multiple of sizeof(u32) */ 4962306a36Sopenharmony_ci handle_dwords = f_handle.handle_bytes >> 2; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci /* we ask for a non connectable maybe decodeable file handle */ 5262306a36Sopenharmony_ci retval = exportfs_encode_fh(path->dentry, 5362306a36Sopenharmony_ci (struct fid *)handle->f_handle, 5462306a36Sopenharmony_ci &handle_dwords, fh_flags); 5562306a36Sopenharmony_ci handle->handle_type = retval; 5662306a36Sopenharmony_ci /* convert handle size to bytes */ 5762306a36Sopenharmony_ci handle_bytes = handle_dwords * sizeof(u32); 5862306a36Sopenharmony_ci handle->handle_bytes = handle_bytes; 5962306a36Sopenharmony_ci if ((handle->handle_bytes > f_handle.handle_bytes) || 6062306a36Sopenharmony_ci (retval == FILEID_INVALID) || (retval < 0)) { 6162306a36Sopenharmony_ci /* As per old exportfs_encode_fh documentation 6262306a36Sopenharmony_ci * we could return ENOSPC to indicate overflow 6362306a36Sopenharmony_ci * But file system returned 255 always. So handle 6462306a36Sopenharmony_ci * both the values 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_ci if (retval == FILEID_INVALID || retval == -ENOSPC) 6762306a36Sopenharmony_ci retval = -EOVERFLOW; 6862306a36Sopenharmony_ci /* 6962306a36Sopenharmony_ci * set the handle size to zero so we copy only 7062306a36Sopenharmony_ci * non variable part of the file_handle 7162306a36Sopenharmony_ci */ 7262306a36Sopenharmony_ci handle_bytes = 0; 7362306a36Sopenharmony_ci } else 7462306a36Sopenharmony_ci retval = 0; 7562306a36Sopenharmony_ci /* copy the mount id */ 7662306a36Sopenharmony_ci if (put_user(real_mount(path->mnt)->mnt_id, mnt_id) || 7762306a36Sopenharmony_ci copy_to_user(ufh, handle, 7862306a36Sopenharmony_ci sizeof(struct file_handle) + handle_bytes)) 7962306a36Sopenharmony_ci retval = -EFAULT; 8062306a36Sopenharmony_ci kfree(handle); 8162306a36Sopenharmony_ci return retval; 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/** 8562306a36Sopenharmony_ci * sys_name_to_handle_at: convert name to handle 8662306a36Sopenharmony_ci * @dfd: directory relative to which name is interpreted if not absolute 8762306a36Sopenharmony_ci * @name: name that should be converted to handle. 8862306a36Sopenharmony_ci * @handle: resulting file handle 8962306a36Sopenharmony_ci * @mnt_id: mount id of the file system containing the file 9062306a36Sopenharmony_ci * @flag: flag value to indicate whether to follow symlink or not 9162306a36Sopenharmony_ci * and whether a decodable file handle is required. 9262306a36Sopenharmony_ci * 9362306a36Sopenharmony_ci * @handle->handle_size indicate the space available to store the 9462306a36Sopenharmony_ci * variable part of the file handle in bytes. If there is not 9562306a36Sopenharmony_ci * enough space, the field is updated to return the minimum 9662306a36Sopenharmony_ci * value required. 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_ciSYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name, 9962306a36Sopenharmony_ci struct file_handle __user *, handle, int __user *, mnt_id, 10062306a36Sopenharmony_ci int, flag) 10162306a36Sopenharmony_ci{ 10262306a36Sopenharmony_ci struct path path; 10362306a36Sopenharmony_ci int lookup_flags; 10462306a36Sopenharmony_ci int fh_flags; 10562306a36Sopenharmony_ci int err; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID)) 10862306a36Sopenharmony_ci return -EINVAL; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0; 11162306a36Sopenharmony_ci fh_flags = (flag & AT_HANDLE_FID) ? EXPORT_FH_FID : 0; 11262306a36Sopenharmony_ci if (flag & AT_EMPTY_PATH) 11362306a36Sopenharmony_ci lookup_flags |= LOOKUP_EMPTY; 11462306a36Sopenharmony_ci err = user_path_at(dfd, name, lookup_flags, &path); 11562306a36Sopenharmony_ci if (!err) { 11662306a36Sopenharmony_ci err = do_sys_name_to_handle(&path, handle, mnt_id, fh_flags); 11762306a36Sopenharmony_ci path_put(&path); 11862306a36Sopenharmony_ci } 11962306a36Sopenharmony_ci return err; 12062306a36Sopenharmony_ci} 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_cistatic struct vfsmount *get_vfsmount_from_fd(int fd) 12362306a36Sopenharmony_ci{ 12462306a36Sopenharmony_ci struct vfsmount *mnt; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci if (fd == AT_FDCWD) { 12762306a36Sopenharmony_ci struct fs_struct *fs = current->fs; 12862306a36Sopenharmony_ci spin_lock(&fs->lock); 12962306a36Sopenharmony_ci mnt = mntget(fs->pwd.mnt); 13062306a36Sopenharmony_ci spin_unlock(&fs->lock); 13162306a36Sopenharmony_ci } else { 13262306a36Sopenharmony_ci struct fd f = fdget(fd); 13362306a36Sopenharmony_ci if (!f.file) 13462306a36Sopenharmony_ci return ERR_PTR(-EBADF); 13562306a36Sopenharmony_ci mnt = mntget(f.file->f_path.mnt); 13662306a36Sopenharmony_ci fdput(f); 13762306a36Sopenharmony_ci } 13862306a36Sopenharmony_ci return mnt; 13962306a36Sopenharmony_ci} 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_cistatic int vfs_dentry_acceptable(void *context, struct dentry *dentry) 14262306a36Sopenharmony_ci{ 14362306a36Sopenharmony_ci return 1; 14462306a36Sopenharmony_ci} 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_cistatic int do_handle_to_path(int mountdirfd, struct file_handle *handle, 14762306a36Sopenharmony_ci struct path *path) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci int retval = 0; 15062306a36Sopenharmony_ci int handle_dwords; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci path->mnt = get_vfsmount_from_fd(mountdirfd); 15362306a36Sopenharmony_ci if (IS_ERR(path->mnt)) { 15462306a36Sopenharmony_ci retval = PTR_ERR(path->mnt); 15562306a36Sopenharmony_ci goto out_err; 15662306a36Sopenharmony_ci } 15762306a36Sopenharmony_ci /* change the handle size to multiple of sizeof(u32) */ 15862306a36Sopenharmony_ci handle_dwords = handle->handle_bytes >> 2; 15962306a36Sopenharmony_ci path->dentry = exportfs_decode_fh(path->mnt, 16062306a36Sopenharmony_ci (struct fid *)handle->f_handle, 16162306a36Sopenharmony_ci handle_dwords, handle->handle_type, 16262306a36Sopenharmony_ci vfs_dentry_acceptable, NULL); 16362306a36Sopenharmony_ci if (IS_ERR(path->dentry)) { 16462306a36Sopenharmony_ci retval = PTR_ERR(path->dentry); 16562306a36Sopenharmony_ci goto out_mnt; 16662306a36Sopenharmony_ci } 16762306a36Sopenharmony_ci return 0; 16862306a36Sopenharmony_ciout_mnt: 16962306a36Sopenharmony_ci mntput(path->mnt); 17062306a36Sopenharmony_ciout_err: 17162306a36Sopenharmony_ci return retval; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_cistatic int handle_to_path(int mountdirfd, struct file_handle __user *ufh, 17562306a36Sopenharmony_ci struct path *path) 17662306a36Sopenharmony_ci{ 17762306a36Sopenharmony_ci int retval = 0; 17862306a36Sopenharmony_ci struct file_handle f_handle; 17962306a36Sopenharmony_ci struct file_handle *handle = NULL; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci /* 18262306a36Sopenharmony_ci * With handle we don't look at the execute bit on the 18362306a36Sopenharmony_ci * directory. Ideally we would like CAP_DAC_SEARCH. 18462306a36Sopenharmony_ci * But we don't have that 18562306a36Sopenharmony_ci */ 18662306a36Sopenharmony_ci if (!capable(CAP_DAC_READ_SEARCH)) { 18762306a36Sopenharmony_ci retval = -EPERM; 18862306a36Sopenharmony_ci goto out_err; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) { 19162306a36Sopenharmony_ci retval = -EFAULT; 19262306a36Sopenharmony_ci goto out_err; 19362306a36Sopenharmony_ci } 19462306a36Sopenharmony_ci if ((f_handle.handle_bytes > MAX_HANDLE_SZ) || 19562306a36Sopenharmony_ci (f_handle.handle_bytes == 0)) { 19662306a36Sopenharmony_ci retval = -EINVAL; 19762306a36Sopenharmony_ci goto out_err; 19862306a36Sopenharmony_ci } 19962306a36Sopenharmony_ci handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes, 20062306a36Sopenharmony_ci GFP_KERNEL); 20162306a36Sopenharmony_ci if (!handle) { 20262306a36Sopenharmony_ci retval = -ENOMEM; 20362306a36Sopenharmony_ci goto out_err; 20462306a36Sopenharmony_ci } 20562306a36Sopenharmony_ci /* copy the full handle */ 20662306a36Sopenharmony_ci *handle = f_handle; 20762306a36Sopenharmony_ci if (copy_from_user(&handle->f_handle, 20862306a36Sopenharmony_ci &ufh->f_handle, 20962306a36Sopenharmony_ci f_handle.handle_bytes)) { 21062306a36Sopenharmony_ci retval = -EFAULT; 21162306a36Sopenharmony_ci goto out_handle; 21262306a36Sopenharmony_ci } 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci retval = do_handle_to_path(mountdirfd, handle, path); 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ciout_handle: 21762306a36Sopenharmony_ci kfree(handle); 21862306a36Sopenharmony_ciout_err: 21962306a36Sopenharmony_ci return retval; 22062306a36Sopenharmony_ci} 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_cistatic long do_handle_open(int mountdirfd, struct file_handle __user *ufh, 22362306a36Sopenharmony_ci int open_flag) 22462306a36Sopenharmony_ci{ 22562306a36Sopenharmony_ci long retval = 0; 22662306a36Sopenharmony_ci struct path path; 22762306a36Sopenharmony_ci struct file *file; 22862306a36Sopenharmony_ci int fd; 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci retval = handle_to_path(mountdirfd, ufh, &path); 23162306a36Sopenharmony_ci if (retval) 23262306a36Sopenharmony_ci return retval; 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci fd = get_unused_fd_flags(open_flag); 23562306a36Sopenharmony_ci if (fd < 0) { 23662306a36Sopenharmony_ci path_put(&path); 23762306a36Sopenharmony_ci return fd; 23862306a36Sopenharmony_ci } 23962306a36Sopenharmony_ci file = file_open_root(&path, "", open_flag, 0); 24062306a36Sopenharmony_ci if (IS_ERR(file)) { 24162306a36Sopenharmony_ci put_unused_fd(fd); 24262306a36Sopenharmony_ci retval = PTR_ERR(file); 24362306a36Sopenharmony_ci } else { 24462306a36Sopenharmony_ci retval = fd; 24562306a36Sopenharmony_ci fd_install(fd, file); 24662306a36Sopenharmony_ci } 24762306a36Sopenharmony_ci path_put(&path); 24862306a36Sopenharmony_ci return retval; 24962306a36Sopenharmony_ci} 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci/** 25262306a36Sopenharmony_ci * sys_open_by_handle_at: Open the file handle 25362306a36Sopenharmony_ci * @mountdirfd: directory file descriptor 25462306a36Sopenharmony_ci * @handle: file handle to be opened 25562306a36Sopenharmony_ci * @flags: open flags. 25662306a36Sopenharmony_ci * 25762306a36Sopenharmony_ci * @mountdirfd indicate the directory file descriptor 25862306a36Sopenharmony_ci * of the mount point. file handle is decoded relative 25962306a36Sopenharmony_ci * to the vfsmount pointed by the @mountdirfd. @flags 26062306a36Sopenharmony_ci * value is same as the open(2) flags. 26162306a36Sopenharmony_ci */ 26262306a36Sopenharmony_ciSYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd, 26362306a36Sopenharmony_ci struct file_handle __user *, handle, 26462306a36Sopenharmony_ci int, flags) 26562306a36Sopenharmony_ci{ 26662306a36Sopenharmony_ci long ret; 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci if (force_o_largefile()) 26962306a36Sopenharmony_ci flags |= O_LARGEFILE; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci ret = do_handle_open(mountdirfd, handle, flags); 27262306a36Sopenharmony_ci return ret; 27362306a36Sopenharmony_ci} 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci#ifdef CONFIG_COMPAT 27662306a36Sopenharmony_ci/* 27762306a36Sopenharmony_ci * Exactly like fs/open.c:sys_open_by_handle_at(), except that it 27862306a36Sopenharmony_ci * doesn't set the O_LARGEFILE flag. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd, 28162306a36Sopenharmony_ci struct file_handle __user *, handle, int, flags) 28262306a36Sopenharmony_ci{ 28362306a36Sopenharmony_ci return do_handle_open(mountdirfd, handle, flags); 28462306a36Sopenharmony_ci} 28562306a36Sopenharmony_ci#endif 286