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 17 static long do_sys_name_to_handle(const struct path *path, 18 struct file_handle __user *ufh, 19 void __user *mnt_id, bool unique_mntid, 20 int fh_flags) 21 { 22 long retval; 23 struct file_handle f_handle; 24 int handle_dwords, handle_bytes; 25 struct file_handle *handle = NULL; 26 27 /* 28 * We need to make sure whether the file system support decoding of 29 * the file handle if decodeable file handle was requested. 30 */ 31 if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags)) 32 return -EOPNOTSUPP; 33 34 /* 35 * A request to encode a connectable handle for a disconnected dentry 36 * is unexpected since AT_EMPTY_PATH is not allowed. 37 */ 38 if (fh_flags & EXPORT_FH_CONNECTABLE && 39 WARN_ON(path->dentry->d_flags & DCACHE_DISCONNECTED)) 40 return -EINVAL; 41 42 if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) 43 return -EFAULT; 44 45 if (f_handle.handle_bytes > MAX_HANDLE_SZ) 46 return -EINVAL; 47 48 handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes), 49 GFP_KERNEL); 50 if (!handle) 51 return -ENOMEM; 52 53 /* convert handle size to multiple of sizeof(u32) */ 54 handle_dwords = f_handle.handle_bytes >> 2; 55 56 /* Encode a possibly decodeable/connectable file handle */ 57 retval = exportfs_encode_fh(path->dentry, 58 (struct fid *)handle->f_handle, 59 &handle_dwords, fh_flags); 60 handle->handle_type = retval; 61 /* convert handle size to bytes */ 62 handle_bytes = handle_dwords * sizeof(u32); 63 handle->handle_bytes = handle_bytes; 64 if ((handle->handle_bytes > f_handle.handle_bytes) || 65 (retval == FILEID_INVALID) || (retval < 0)) { 66 /* As per old exportfs_encode_fh documentation 67 * we could return ENOSPC to indicate overflow 68 * But file system returned 255 always. So handle 69 * both the values 70 */ 71 if (retval == FILEID_INVALID || retval == -ENOSPC) 72 retval = -EOVERFLOW; 73 /* 74 * set the handle size to zero so we copy only 75 * non variable part of the file_handle 76 */ 77 handle_bytes = 0; 78 } else { 79 /* 80 * When asked to encode a connectable file handle, encode this 81 * property in the file handle itself, so that we later know 82 * how to decode it. 83 * For sanity, also encode in the file handle if the encoded 84 * object is a directory and verify this during decode, because 85 * decoding directory file handles is quite different than 86 * decoding connectable non-directory file handles. 87 */ 88 if (fh_flags & EXPORT_FH_CONNECTABLE) { 89 handle->handle_type |= FILEID_IS_CONNECTABLE; 90 if (d_is_dir(path->dentry)) 91 handle->handle_type |= FILEID_IS_DIR; 92 } 93 retval = 0; 94 } 95 /* copy the mount id */ 96 if (unique_mntid) { 97 if (put_user(real_mount(path->mnt)->mnt_id_unique, 98 (u64 __user *) mnt_id)) 99 retval = -EFAULT; 100 } else { 101 if (put_user(real_mount(path->mnt)->mnt_id, 102 (int __user *) mnt_id)) 103 retval = -EFAULT; 104 } 105 /* copy the handle */ 106 if (retval != -EFAULT && 107 copy_to_user(ufh, handle, 108 struct_size(handle, f_handle, handle_bytes))) 109 retval = -EFAULT; 110 kfree(handle); 111 return retval; 112 } 113 114 /** 115 * sys_name_to_handle_at: convert name to handle 116 * @dfd: directory relative to which name is interpreted if not absolute 117 * @name: name that should be converted to handle. 118 * @handle: resulting file handle 119 * @mnt_id: mount id of the file system containing the file 120 * (u64 if AT_HANDLE_MNT_ID_UNIQUE, otherwise int) 121 * @flag: flag value to indicate whether to follow symlink or not 122 * and whether a decodable file handle is required. 123 * 124 * @handle->handle_size indicate the space available to store the 125 * variable part of the file handle in bytes. If there is not 126 * enough space, the field is updated to return the minimum 127 * value required. 128 */ 129 SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name, 130 struct file_handle __user *, handle, void __user *, mnt_id, 131 int, flag) 132 { 133 struct path path; 134 int lookup_flags; 135 int fh_flags = 0; 136 int err; 137 138 if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID | 139 AT_HANDLE_MNT_ID_UNIQUE | AT_HANDLE_CONNECTABLE)) 140 return -EINVAL; 141 142 /* 143 * AT_HANDLE_FID means there is no intention to decode file handle 144 * AT_HANDLE_CONNECTABLE means there is an intention to decode a 145 * connected fd (with known path), so these flags are conflicting. 146 * AT_EMPTY_PATH could be used along with a dfd that refers to a 147 * disconnected non-directory, which cannot be used to encode a 148 * connectable file handle, because its parent is unknown. 149 */ 150 if (flag & AT_HANDLE_CONNECTABLE && 151 flag & (AT_HANDLE_FID | AT_EMPTY_PATH)) 152 return -EINVAL; 153 else if (flag & AT_HANDLE_FID) 154 fh_flags |= EXPORT_FH_FID; 155 else if (flag & AT_HANDLE_CONNECTABLE) 156 fh_flags |= EXPORT_FH_CONNECTABLE; 157 158 lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0; 159 if (flag & AT_EMPTY_PATH) 160 lookup_flags |= LOOKUP_EMPTY; 161 err = user_path_at(dfd, name, lookup_flags, &path); 162 if (!err) { 163 err = do_sys_name_to_handle(&path, handle, mnt_id, 164 flag & AT_HANDLE_MNT_ID_UNIQUE, 165 fh_flags); 166 path_put(&path); 167 } 168 return err; 169 } 170 171 static int get_path_anchor(int fd, struct path *root) 172 { 173 if (fd >= 0) { 174 CLASS(fd, f)(fd); 175 if (fd_empty(f)) 176 return -EBADF; 177 *root = fd_file(f)->f_path; 178 path_get(root); 179 return 0; 180 } 181 182 if (fd == AT_FDCWD) { 183 get_fs_pwd(current->fs, root); 184 return 0; 185 } 186 187 if (fd == FD_PIDFS_ROOT) { 188 pidfs_get_root(root); 189 return 0; 190 } 191 192 return -EBADF; 193 } 194 195 static int vfs_dentry_acceptable(void *context, struct dentry *dentry) 196 { 197 struct handle_to_path_ctx *ctx = context; 198 struct user_namespace *user_ns = current_user_ns(); 199 struct dentry *d, *root = ctx->root.dentry; 200 struct mnt_idmap *idmap = mnt_idmap(ctx->root.mnt); 201 int retval = 0; 202 203 if (!root) 204 return 1; 205 206 /* Old permission model with global CAP_DAC_READ_SEARCH. */ 207 if (!ctx->flags) 208 return 1; 209 210 /* 211 * Verify that the decoded dentry itself has a valid id mapping. 212 * In case the decoded dentry is the mountfd root itself, this 213 * verifies that the mountfd inode itself has a valid id mapping. 214 */ 215 if (!privileged_wrt_inode_uidgid(user_ns, idmap, d_inode(dentry))) 216 return 0; 217 218 /* 219 * It's racy as we're not taking rename_lock but we're able to ignore 220 * permissions and we just need an approximation whether we were able 221 * to follow a path to the file. 222 * 223 * It's also potentially expensive on some filesystems especially if 224 * there is a deep path. 225 */ 226 d = dget(dentry); 227 while (d != root && !IS_ROOT(d)) { 228 struct dentry *parent = dget_parent(d); 229 230 /* 231 * We know that we have the ability to override DAC permissions 232 * as we've verified this earlier via CAP_DAC_READ_SEARCH. But 233 * we also need to make sure that there aren't any unmapped 234 * inodes in the path that would prevent us from reaching the 235 * file. 236 */ 237 if (!privileged_wrt_inode_uidgid(user_ns, idmap, 238 d_inode(parent))) { 239 dput(d); 240 dput(parent); 241 return retval; 242 } 243 244 dput(d); 245 d = parent; 246 } 247 248 if (!(ctx->flags & HANDLE_CHECK_SUBTREE) || d == root) 249 retval = 1; 250 /* 251 * exportfs_decode_fh_raw() does not call acceptable() callback with 252 * a disconnected directory dentry, so we should have reached either 253 * mount fd directory or sb root. 254 */ 255 if (ctx->fh_flags & EXPORT_FH_DIR_ONLY) 256 WARN_ON_ONCE(d != root && d != root->d_sb->s_root); 257 dput(d); 258 return retval; 259 } 260 261 static int do_handle_to_path(struct file_handle *handle, struct path *path, 262 struct handle_to_path_ctx *ctx) 263 { 264 int handle_dwords; 265 struct vfsmount *mnt = ctx->root.mnt; 266 struct dentry *dentry; 267 268 /* change the handle size to multiple of sizeof(u32) */ 269 handle_dwords = handle->handle_bytes >> 2; 270 dentry = exportfs_decode_fh_raw(mnt, (struct fid *)handle->f_handle, 271 handle_dwords, handle->handle_type, 272 ctx->fh_flags, vfs_dentry_acceptable, 273 ctx); 274 if (IS_ERR_OR_NULL(dentry)) { 275 if (dentry == ERR_PTR(-ENOMEM)) 276 return -ENOMEM; 277 return -ESTALE; 278 } 279 path->dentry = dentry; 280 path->mnt = mntget(mnt); 281 return 0; 282 } 283 284 static inline int may_decode_fh(struct handle_to_path_ctx *ctx, 285 unsigned int o_flags) 286 { 287 struct path *root = &ctx->root; 288 289 if (capable(CAP_DAC_READ_SEARCH)) 290 return 0; 291 292 /* 293 * Allow relaxed permissions of file handles if the caller has 294 * the ability to mount the filesystem or create a bind-mount of 295 * the provided @mountdirfd. 296 * 297 * In both cases the caller may be able to get an unobstructed 298 * way to the encoded file handle. If the caller is only able to 299 * create a bind-mount we need to verify that there are no 300 * locked mounts on top of it that could prevent us from getting 301 * to the encoded file. 302 * 303 * In principle, locked mounts can prevent the caller from 304 * mounting the filesystem but that only applies to procfs and 305 * sysfs neither of which support decoding file handles. 306 * 307 * Restrict to O_DIRECTORY to provide a deterministic API that 308 * avoids a confusing api in the face of disconnected non-dir 309 * dentries. 310 * 311 * There's only one dentry for each directory inode (VFS rule)... 312 */ 313 if (!(o_flags & O_DIRECTORY)) 314 return -EPERM; 315 316 if (ns_capable(root->mnt->mnt_sb->s_user_ns, CAP_SYS_ADMIN)) 317 ctx->flags = HANDLE_CHECK_PERMS; 318 else if (is_mounted(root->mnt) && 319 ns_capable(real_mount(root->mnt)->mnt_ns->user_ns, 320 CAP_SYS_ADMIN) && 321 !has_locked_children(real_mount(root->mnt), root->dentry)) 322 ctx->flags = HANDLE_CHECK_PERMS | HANDLE_CHECK_SUBTREE; 323 else 324 return -EPERM; 325 326 /* Are we able to override DAC permissions? */ 327 if (!ns_capable(current_user_ns(), CAP_DAC_READ_SEARCH)) 328 return -EPERM; 329 330 ctx->fh_flags = EXPORT_FH_DIR_ONLY; 331 return 0; 332 } 333 334 static int handle_to_path(int mountdirfd, struct file_handle __user *ufh, 335 struct path *path, unsigned int o_flags) 336 { 337 int retval = 0; 338 struct file_handle f_handle; 339 struct file_handle *handle __free(kfree) = NULL; 340 struct handle_to_path_ctx ctx = {}; 341 const struct export_operations *eops; 342 343 if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) 344 return -EFAULT; 345 346 if ((f_handle.handle_bytes > MAX_HANDLE_SZ) || 347 (f_handle.handle_bytes == 0)) 348 return -EINVAL; 349 350 if (f_handle.handle_type < 0 || 351 FILEID_USER_FLAGS(f_handle.handle_type) & ~FILEID_VALID_USER_FLAGS) 352 return -EINVAL; 353 354 retval = get_path_anchor(mountdirfd, &ctx.root); 355 if (retval) 356 return retval; 357 358 eops = ctx.root.mnt->mnt_sb->s_export_op; 359 if (eops && eops->permission) 360 retval = eops->permission(&ctx, o_flags); 361 else 362 retval = may_decode_fh(&ctx, o_flags); 363 if (retval) 364 goto out_path; 365 366 handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes), 367 GFP_KERNEL); 368 if (!handle) { 369 retval = -ENOMEM; 370 goto out_path; 371 } 372 /* copy the full handle */ 373 *handle = f_handle; 374 if (copy_from_user(&handle->f_handle, 375 &ufh->f_handle, 376 f_handle.handle_bytes)) { 377 retval = -EFAULT; 378 goto out_path; 379 } 380 381 /* 382 * If handle was encoded with AT_HANDLE_CONNECTABLE, verify that we 383 * are decoding an fd with connected path, which is accessible from 384 * the mount fd path. 385 */ 386 if (f_handle.handle_type & FILEID_IS_CONNECTABLE) { 387 ctx.fh_flags |= EXPORT_FH_CONNECTABLE; 388 ctx.flags |= HANDLE_CHECK_SUBTREE; 389 } 390 if (f_handle.handle_type & FILEID_IS_DIR) 391 ctx.fh_flags |= EXPORT_FH_DIR_ONLY; 392 /* Filesystem code should not be exposed to user flags */ 393 handle->handle_type &= ~FILEID_USER_FLAGS_MASK; 394 retval = do_handle_to_path(handle, path, &ctx); 395 396 out_path: 397 path_put(&ctx.root); 398 return retval; 399 } 400 401 static long do_handle_open(int mountdirfd, struct file_handle __user *ufh, 402 int open_flag) 403 { 404 long retval = 0; 405 struct path path __free(path_put) = {}; 406 struct file *file; 407 const struct export_operations *eops; 408 409 retval = handle_to_path(mountdirfd, ufh, &path, open_flag); 410 if (retval) 411 return retval; 412 413 CLASS(get_unused_fd, fd)(open_flag); 414 if (fd < 0) 415 return fd; 416 417 eops = path.mnt->mnt_sb->s_export_op; 418 if (eops->open) 419 file = eops->open(&path, open_flag); 420 else 421 file = file_open_root(&path, "", open_flag, 0); 422 if (IS_ERR(file)) 423 return PTR_ERR(file); 424 425 fd_install(fd, file); 426 return take_fd(fd); 427 } 428 429 /** 430 * sys_open_by_handle_at: Open the file handle 431 * @mountdirfd: directory file descriptor 432 * @handle: file handle to be opened 433 * @flags: open flags. 434 * 435 * @mountdirfd indicate the directory file descriptor 436 * of the mount point. file handle is decoded relative 437 * to the vfsmount pointed by the @mountdirfd. @flags 438 * value is same as the open(2) flags. 439 */ 440 SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd, 441 struct file_handle __user *, handle, 442 int, flags) 443 { 444 long ret; 445 446 if (force_o_largefile()) 447 flags |= O_LARGEFILE; 448 449 ret = do_handle_open(mountdirfd, handle, flags); 450 return ret; 451 } 452 453 #ifdef CONFIG_COMPAT 454 /* 455 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it 456 * doesn't set the O_LARGEFILE flag. 457 */ 458 COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd, 459 struct file_handle __user *, handle, int, flags) 460 { 461 return do_handle_open(mountdirfd, handle, flags); 462 } 463 #endif 464