1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/namei.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 /* 9 * Some corrections by tytso. 10 */ 11 12 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 13 * lookup logic. 14 */ 15 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 16 */ 17 18 #include <linux/init.h> 19 #include <linux/export.h> 20 #include <linux/slab.h> 21 #include <linux/wordpart.h> 22 #include <linux/fs.h> 23 #include <linux/filelock.h> 24 #include <linux/namei.h> 25 #include <linux/pagemap.h> 26 #include <linux/sched/mm.h> 27 #include <linux/fsnotify.h> 28 #include <linux/personality.h> 29 #include <linux/security.h> 30 #include <linux/syscalls.h> 31 #include <linux/mount.h> 32 #include <linux/audit.h> 33 #include <linux/capability.h> 34 #include <linux/file.h> 35 #include <linux/fcntl.h> 36 #include <linux/device_cgroup.h> 37 #include <linux/fs_struct.h> 38 #include <linux/posix_acl.h> 39 #include <linux/hash.h> 40 #include <linux/bitops.h> 41 #include <linux/init_task.h> 42 #include <linux/uaccess.h> 43 44 #include "internal.h" 45 #include "mount.h" 46 47 /* [Feb-1997 T. Schoebel-Theuer] 48 * Fundamental changes in the pathname lookup mechanisms (namei) 49 * were necessary because of omirr. The reason is that omirr needs 50 * to know the _real_ pathname, not the user-supplied one, in case 51 * of symlinks (and also when transname replacements occur). 52 * 53 * The new code replaces the old recursive symlink resolution with 54 * an iterative one (in case of non-nested symlink chains). It does 55 * this with calls to <fs>_follow_link(). 56 * As a side effect, dir_namei(), _namei() and follow_link() are now 57 * replaced with a single function lookup_dentry() that can handle all 58 * the special cases of the former code. 59 * 60 * With the new dcache, the pathname is stored at each inode, at least as 61 * long as the refcount of the inode is positive. As a side effect, the 62 * size of the dcache depends on the inode cache and thus is dynamic. 63 * 64 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 65 * resolution to correspond with current state of the code. 66 * 67 * Note that the symlink resolution is not *completely* iterative. 68 * There is still a significant amount of tail- and mid- recursion in 69 * the algorithm. Also, note that <fs>_readlink() is not used in 70 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 71 * may return different results than <fs>_follow_link(). Many virtual 72 * filesystems (including /proc) exhibit this behavior. 73 */ 74 75 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 76 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 77 * and the name already exists in form of a symlink, try to create the new 78 * name indicated by the symlink. The old code always complained that the 79 * name already exists, due to not following the symlink even if its target 80 * is nonexistent. The new semantics affects also mknod() and link() when 81 * the name is a symlink pointing to a non-existent name. 82 * 83 * I don't know which semantics is the right one, since I have no access 84 * to standards. But I found by trial that HP-UX 9.0 has the full "new" 85 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 86 * "old" one. Personally, I think the new semantics is much more logical. 87 * Note that "ln old new" where "new" is a symlink pointing to a non-existing 88 * file does succeed in both HP-UX and SunOs, but not in Solaris 89 * and in the old Linux semantics. 90 */ 91 92 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 93 * semantics. See the comments in "open_namei" and "do_link" below. 94 * 95 * [10-Sep-98 Alan Modra] Another symlink change. 96 */ 97 98 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 99 * inside the path - always follow. 100 * in the last component in creation/removal/renaming - never follow. 101 * if LOOKUP_FOLLOW passed - follow. 102 * if the pathname has trailing slashes - follow. 103 * otherwise - don't follow. 104 * (applied in that order). 105 * 106 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 107 * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 108 * During the 2.4 we need to fix the userland stuff depending on it - 109 * hopefully we will be able to get rid of that wart in 2.5. So far only 110 * XEmacs seems to be relying on it... 111 */ 112 /* 113 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 114 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 115 * any extra contention... 116 */ 117 118 /* In order to reduce some races, while at the same time doing additional 119 * checking and hopefully speeding things up, we copy filenames to the 120 * kernel data space before using them.. 121 * 122 * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 123 * PATH_MAX includes the nul terminator --RR. 124 */ 125 126 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) 127 128 struct filename * 129 getname_flags(const char __user *filename, int flags) 130 { 131 struct filename *result; 132 char *kname; 133 int len; 134 135 result = audit_reusename(filename); 136 if (result) 137 return result; 138 139 result = __getname(); 140 if (unlikely(!result)) 141 return ERR_PTR(-ENOMEM); 142 143 /* 144 * First, try to embed the struct filename inside the names_cache 145 * allocation 146 */ 147 kname = (char *)result->iname; 148 result->name = kname; 149 150 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); 151 /* 152 * Handle both empty path and copy failure in one go. 153 */ 154 if (unlikely(len <= 0)) { 155 if (unlikely(len < 0)) { 156 __putname(result); 157 return ERR_PTR(len); 158 } 159 160 /* The empty path is special. */ 161 if (!(flags & LOOKUP_EMPTY)) { 162 __putname(result); 163 return ERR_PTR(-ENOENT); 164 } 165 } 166 167 /* 168 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a 169 * separate struct filename so we can dedicate the entire 170 * names_cache allocation for the pathname, and re-do the copy from 171 * userland. 172 */ 173 if (unlikely(len == EMBEDDED_NAME_MAX)) { 174 const size_t size = offsetof(struct filename, iname[1]); 175 kname = (char *)result; 176 177 /* 178 * size is chosen that way we to guarantee that 179 * result->iname[0] is within the same object and that 180 * kname can't be equal to result->iname, no matter what. 181 */ 182 result = kzalloc(size, GFP_KERNEL); 183 if (unlikely(!result)) { 184 __putname(kname); 185 return ERR_PTR(-ENOMEM); 186 } 187 result->name = kname; 188 len = strncpy_from_user(kname, filename, PATH_MAX); 189 if (unlikely(len < 0)) { 190 __putname(kname); 191 kfree(result); 192 return ERR_PTR(len); 193 } 194 /* The empty path is special. */ 195 if (unlikely(!len) && !(flags & LOOKUP_EMPTY)) { 196 __putname(kname); 197 kfree(result); 198 return ERR_PTR(-ENOENT); 199 } 200 if (unlikely(len == PATH_MAX)) { 201 __putname(kname); 202 kfree(result); 203 return ERR_PTR(-ENAMETOOLONG); 204 } 205 } 206 207 atomic_set(&result->refcnt, 1); 208 result->uptr = filename; 209 result->aname = NULL; 210 audit_getname(result); 211 return result; 212 } 213 214 struct filename * 215 getname_uflags(const char __user *filename, int uflags) 216 { 217 int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0; 218 219 return getname_flags(filename, flags); 220 } 221 222 struct filename * 223 getname(const char __user * filename) 224 { 225 return getname_flags(filename, 0); 226 } 227 228 struct filename * 229 getname_kernel(const char * filename) 230 { 231 struct filename *result; 232 int len = strlen(filename) + 1; 233 234 result = __getname(); 235 if (unlikely(!result)) 236 return ERR_PTR(-ENOMEM); 237 238 if (len <= EMBEDDED_NAME_MAX) { 239 result->name = (char *)result->iname; 240 } else if (len <= PATH_MAX) { 241 const size_t size = offsetof(struct filename, iname[1]); 242 struct filename *tmp; 243 244 tmp = kmalloc(size, GFP_KERNEL); 245 if (unlikely(!tmp)) { 246 __putname(result); 247 return ERR_PTR(-ENOMEM); 248 } 249 tmp->name = (char *)result; 250 result = tmp; 251 } else { 252 __putname(result); 253 return ERR_PTR(-ENAMETOOLONG); 254 } 255 memcpy((char *)result->name, filename, len); 256 result->uptr = NULL; 257 result->aname = NULL; 258 atomic_set(&result->refcnt, 1); 259 audit_getname(result); 260 261 return result; 262 } 263 EXPORT_SYMBOL(getname_kernel); 264 265 void putname(struct filename *name) 266 { 267 if (IS_ERR(name)) 268 return; 269 270 if (WARN_ON_ONCE(!atomic_read(&name->refcnt))) 271 return; 272 273 if (!atomic_dec_and_test(&name->refcnt)) 274 return; 275 276 if (name->name != name->iname) { 277 __putname(name->name); 278 kfree(name); 279 } else 280 __putname(name); 281 } 282 EXPORT_SYMBOL(putname); 283 284 /** 285 * check_acl - perform ACL permission checking 286 * @idmap: idmap of the mount the inode was found from 287 * @inode: inode to check permissions on 288 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 289 * 290 * This function performs the ACL permission checking. Since this function 291 * retrieve POSIX acls it needs to know whether it is called from a blocking or 292 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 293 * 294 * If the inode has been found through an idmapped mount the idmap of 295 * the vfsmount must be passed through @idmap. This function will then take 296 * care to map the inode according to @idmap before checking permissions. 297 * On non-idmapped mounts or if permission checking is to be performed on the 298 * raw inode simply pass @nop_mnt_idmap. 299 */ 300 static int check_acl(struct mnt_idmap *idmap, 301 struct inode *inode, int mask) 302 { 303 #ifdef CONFIG_FS_POSIX_ACL 304 struct posix_acl *acl; 305 306 if (mask & MAY_NOT_BLOCK) { 307 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 308 if (!acl) 309 return -EAGAIN; 310 /* no ->get_inode_acl() calls in RCU mode... */ 311 if (is_uncached_acl(acl)) 312 return -ECHILD; 313 return posix_acl_permission(idmap, inode, acl, mask); 314 } 315 316 acl = get_inode_acl(inode, ACL_TYPE_ACCESS); 317 if (IS_ERR(acl)) 318 return PTR_ERR(acl); 319 if (acl) { 320 int error = posix_acl_permission(idmap, inode, acl, mask); 321 posix_acl_release(acl); 322 return error; 323 } 324 #endif 325 326 return -EAGAIN; 327 } 328 329 /** 330 * acl_permission_check - perform basic UNIX permission checking 331 * @idmap: idmap of the mount the inode was found from 332 * @inode: inode to check permissions on 333 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 334 * 335 * This function performs the basic UNIX permission checking. Since this 336 * function may retrieve POSIX acls it needs to know whether it is called from a 337 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 338 * 339 * If the inode has been found through an idmapped mount the idmap of 340 * the vfsmount must be passed through @idmap. This function will then take 341 * care to map the inode according to @idmap before checking permissions. 342 * On non-idmapped mounts or if permission checking is to be performed on the 343 * raw inode simply pass @nop_mnt_idmap. 344 */ 345 static int acl_permission_check(struct mnt_idmap *idmap, 346 struct inode *inode, int mask) 347 { 348 unsigned int mode = inode->i_mode; 349 vfsuid_t vfsuid; 350 351 /* Are we the owner? If so, ACL's don't matter */ 352 vfsuid = i_uid_into_vfsuid(idmap, inode); 353 if (likely(vfsuid_eq_kuid(vfsuid, current_fsuid()))) { 354 mask &= 7; 355 mode >>= 6; 356 return (mask & ~mode) ? -EACCES : 0; 357 } 358 359 /* Do we have ACL's? */ 360 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 361 int error = check_acl(idmap, inode, mask); 362 if (error != -EAGAIN) 363 return error; 364 } 365 366 /* Only RWX matters for group/other mode bits */ 367 mask &= 7; 368 369 /* 370 * Are the group permissions different from 371 * the other permissions in the bits we care 372 * about? Need to check group ownership if so. 373 */ 374 if (mask & (mode ^ (mode >> 3))) { 375 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 376 if (vfsgid_in_group_p(vfsgid)) 377 mode >>= 3; 378 } 379 380 /* Bits in 'mode' clear that we require? */ 381 return (mask & ~mode) ? -EACCES : 0; 382 } 383 384 /** 385 * generic_permission - check for access rights on a Posix-like filesystem 386 * @idmap: idmap of the mount the inode was found from 387 * @inode: inode to check access rights for 388 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, 389 * %MAY_NOT_BLOCK ...) 390 * 391 * Used to check for read/write/execute permissions on a file. 392 * We use "fsuid" for this, letting us set arbitrary permissions 393 * for filesystem access without changing the "normal" uids which 394 * are used for other things. 395 * 396 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 397 * request cannot be satisfied (eg. requires blocking or too much complexity). 398 * It would then be called again in ref-walk mode. 399 * 400 * If the inode has been found through an idmapped mount the idmap of 401 * the vfsmount must be passed through @idmap. This function will then take 402 * care to map the inode according to @idmap before checking permissions. 403 * On non-idmapped mounts or if permission checking is to be performed on the 404 * raw inode simply pass @nop_mnt_idmap. 405 */ 406 int generic_permission(struct mnt_idmap *idmap, struct inode *inode, 407 int mask) 408 { 409 int ret; 410 411 /* 412 * Do the basic permission checks. 413 */ 414 ret = acl_permission_check(idmap, inode, mask); 415 if (ret != -EACCES) 416 return ret; 417 418 if (S_ISDIR(inode->i_mode)) { 419 /* DACs are overridable for directories */ 420 if (!(mask & MAY_WRITE)) 421 if (capable_wrt_inode_uidgid(idmap, inode, 422 CAP_DAC_READ_SEARCH)) 423 return 0; 424 if (capable_wrt_inode_uidgid(idmap, inode, 425 CAP_DAC_OVERRIDE)) 426 return 0; 427 return -EACCES; 428 } 429 430 /* 431 * Searching includes executable on directories, else just read. 432 */ 433 mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 434 if (mask == MAY_READ) 435 if (capable_wrt_inode_uidgid(idmap, inode, 436 CAP_DAC_READ_SEARCH)) 437 return 0; 438 /* 439 * Read/write DACs are always overridable. 440 * Executable DACs are overridable when there is 441 * at least one exec bit set. 442 */ 443 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 444 if (capable_wrt_inode_uidgid(idmap, inode, 445 CAP_DAC_OVERRIDE)) 446 return 0; 447 448 return -EACCES; 449 } 450 EXPORT_SYMBOL(generic_permission); 451 452 /** 453 * do_inode_permission - UNIX permission checking 454 * @idmap: idmap of the mount the inode was found from 455 * @inode: inode to check permissions on 456 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 457 * 458 * We _really_ want to just do "generic_permission()" without 459 * even looking at the inode->i_op values. So we keep a cache 460 * flag in inode->i_opflags, that says "this has not special 461 * permission function, use the fast case". 462 */ 463 static inline int do_inode_permission(struct mnt_idmap *idmap, 464 struct inode *inode, int mask) 465 { 466 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 467 if (likely(inode->i_op->permission)) 468 return inode->i_op->permission(idmap, inode, mask); 469 470 /* This gets set once for the inode lifetime */ 471 spin_lock(&inode->i_lock); 472 inode->i_opflags |= IOP_FASTPERM; 473 spin_unlock(&inode->i_lock); 474 } 475 return generic_permission(idmap, inode, mask); 476 } 477 478 /** 479 * sb_permission - Check superblock-level permissions 480 * @sb: Superblock of inode to check permission on 481 * @inode: Inode to check permission on 482 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 483 * 484 * Separate out file-system wide checks from inode-specific permission checks. 485 */ 486 static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 487 { 488 if (unlikely(mask & MAY_WRITE)) { 489 umode_t mode = inode->i_mode; 490 491 /* Nobody gets write access to a read-only fs. */ 492 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 493 return -EROFS; 494 } 495 return 0; 496 } 497 498 /** 499 * inode_permission - Check for access rights to a given inode 500 * @idmap: idmap of the mount the inode was found from 501 * @inode: Inode to check permission on 502 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 503 * 504 * Check for read/write/execute permissions on an inode. We use fs[ug]id for 505 * this, letting us set arbitrary permissions for filesystem access without 506 * changing the "normal" UIDs which are used for other things. 507 * 508 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 509 */ 510 int inode_permission(struct mnt_idmap *idmap, 511 struct inode *inode, int mask) 512 { 513 int retval; 514 515 retval = sb_permission(inode->i_sb, inode, mask); 516 if (retval) 517 return retval; 518 519 if (unlikely(mask & MAY_WRITE)) { 520 /* 521 * Nobody gets write access to an immutable file. 522 */ 523 if (IS_IMMUTABLE(inode)) 524 return -EPERM; 525 526 /* 527 * Updating mtime will likely cause i_uid and i_gid to be 528 * written back improperly if their true value is unknown 529 * to the vfs. 530 */ 531 if (HAS_UNMAPPED_ID(idmap, inode)) 532 return -EACCES; 533 } 534 535 retval = do_inode_permission(idmap, inode, mask); 536 if (retval) 537 return retval; 538 539 retval = devcgroup_inode_permission(inode, mask); 540 if (retval) 541 return retval; 542 543 return security_inode_permission(inode, mask); 544 } 545 EXPORT_SYMBOL(inode_permission); 546 547 /** 548 * path_get - get a reference to a path 549 * @path: path to get the reference to 550 * 551 * Given a path increment the reference count to the dentry and the vfsmount. 552 */ 553 void path_get(const struct path *path) 554 { 555 mntget(path->mnt); 556 dget(path->dentry); 557 } 558 EXPORT_SYMBOL(path_get); 559 560 /** 561 * path_put - put a reference to a path 562 * @path: path to put the reference to 563 * 564 * Given a path decrement the reference count to the dentry and the vfsmount. 565 */ 566 void path_put(const struct path *path) 567 { 568 dput(path->dentry); 569 mntput(path->mnt); 570 } 571 EXPORT_SYMBOL(path_put); 572 573 #define EMBEDDED_LEVELS 2 574 struct nameidata { 575 struct path path; 576 struct qstr last; 577 struct path root; 578 struct inode *inode; /* path.dentry.d_inode */ 579 unsigned int flags, state; 580 unsigned seq, next_seq, m_seq, r_seq; 581 int last_type; 582 unsigned depth; 583 int total_link_count; 584 struct saved { 585 struct path link; 586 struct delayed_call done; 587 const char *name; 588 unsigned seq; 589 } *stack, internal[EMBEDDED_LEVELS]; 590 struct filename *name; 591 struct nameidata *saved; 592 unsigned root_seq; 593 int dfd; 594 vfsuid_t dir_vfsuid; 595 umode_t dir_mode; 596 } __randomize_layout; 597 598 #define ND_ROOT_PRESET 1 599 #define ND_ROOT_GRABBED 2 600 #define ND_JUMPED 4 601 602 static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) 603 { 604 struct nameidata *old = current->nameidata; 605 p->stack = p->internal; 606 p->depth = 0; 607 p->dfd = dfd; 608 p->name = name; 609 p->path.mnt = NULL; 610 p->path.dentry = NULL; 611 p->total_link_count = old ? old->total_link_count : 0; 612 p->saved = old; 613 current->nameidata = p; 614 } 615 616 static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name, 617 const struct path *root) 618 { 619 __set_nameidata(p, dfd, name); 620 p->state = 0; 621 if (unlikely(root)) { 622 p->state = ND_ROOT_PRESET; 623 p->root = *root; 624 } 625 } 626 627 static void restore_nameidata(void) 628 { 629 struct nameidata *now = current->nameidata, *old = now->saved; 630 631 current->nameidata = old; 632 if (old) 633 old->total_link_count = now->total_link_count; 634 if (now->stack != now->internal) 635 kfree(now->stack); 636 } 637 638 static bool nd_alloc_stack(struct nameidata *nd) 639 { 640 struct saved *p; 641 642 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), 643 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); 644 if (unlikely(!p)) 645 return false; 646 memcpy(p, nd->internal, sizeof(nd->internal)); 647 nd->stack = p; 648 return true; 649 } 650 651 /** 652 * path_connected - Verify that a dentry is below mnt.mnt_root 653 * @mnt: The mountpoint to check. 654 * @dentry: The dentry to check. 655 * 656 * Rename can sometimes move a file or directory outside of a bind 657 * mount, path_connected allows those cases to be detected. 658 */ 659 static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) 660 { 661 struct super_block *sb = mnt->mnt_sb; 662 663 /* Bind mounts can have disconnected paths */ 664 if (mnt->mnt_root == sb->s_root) 665 return true; 666 667 return is_subdir(dentry, mnt->mnt_root); 668 } 669 670 static void drop_links(struct nameidata *nd) 671 { 672 int i = nd->depth; 673 while (i--) { 674 struct saved *last = nd->stack + i; 675 do_delayed_call(&last->done); 676 clear_delayed_call(&last->done); 677 } 678 } 679 680 static void leave_rcu(struct nameidata *nd) 681 { 682 nd->flags &= ~LOOKUP_RCU; 683 nd->seq = nd->next_seq = 0; 684 rcu_read_unlock(); 685 } 686 687 static void terminate_walk(struct nameidata *nd) 688 { 689 drop_links(nd); 690 if (!(nd->flags & LOOKUP_RCU)) { 691 int i; 692 path_put(&nd->path); 693 for (i = 0; i < nd->depth; i++) 694 path_put(&nd->stack[i].link); 695 if (nd->state & ND_ROOT_GRABBED) { 696 path_put(&nd->root); 697 nd->state &= ~ND_ROOT_GRABBED; 698 } 699 } else { 700 leave_rcu(nd); 701 } 702 nd->depth = 0; 703 nd->path.mnt = NULL; 704 nd->path.dentry = NULL; 705 } 706 707 /* path_put is needed afterwards regardless of success or failure */ 708 static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) 709 { 710 int res = __legitimize_mnt(path->mnt, mseq); 711 if (unlikely(res)) { 712 if (res > 0) 713 path->mnt = NULL; 714 path->dentry = NULL; 715 return false; 716 } 717 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { 718 path->dentry = NULL; 719 return false; 720 } 721 return !read_seqcount_retry(&path->dentry->d_seq, seq); 722 } 723 724 static inline bool legitimize_path(struct nameidata *nd, 725 struct path *path, unsigned seq) 726 { 727 return __legitimize_path(path, seq, nd->m_seq); 728 } 729 730 static bool legitimize_links(struct nameidata *nd) 731 { 732 int i; 733 if (unlikely(nd->flags & LOOKUP_CACHED)) { 734 drop_links(nd); 735 nd->depth = 0; 736 return false; 737 } 738 for (i = 0; i < nd->depth; i++) { 739 struct saved *last = nd->stack + i; 740 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { 741 drop_links(nd); 742 nd->depth = i + 1; 743 return false; 744 } 745 } 746 return true; 747 } 748 749 static bool legitimize_root(struct nameidata *nd) 750 { 751 /* Nothing to do if nd->root is zero or is managed by the VFS user. */ 752 if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) 753 return true; 754 nd->state |= ND_ROOT_GRABBED; 755 return legitimize_path(nd, &nd->root, nd->root_seq); 756 } 757 758 /* 759 * Path walking has 2 modes, rcu-walk and ref-walk (see 760 * Documentation/filesystems/path-lookup.txt). In situations when we can't 761 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 762 * normal reference counts on dentries and vfsmounts to transition to ref-walk 763 * mode. Refcounts are grabbed at the last known good point before rcu-walk 764 * got stuck, so ref-walk may continue from there. If this is not successful 765 * (eg. a seqcount has changed), then failure is returned and it's up to caller 766 * to restart the path walk from the beginning in ref-walk mode. 767 */ 768 769 /** 770 * try_to_unlazy - try to switch to ref-walk mode. 771 * @nd: nameidata pathwalk data 772 * Returns: true on success, false on failure 773 * 774 * try_to_unlazy attempts to legitimize the current nd->path and nd->root 775 * for ref-walk mode. 776 * Must be called from rcu-walk context. 777 * Nothing should touch nameidata between try_to_unlazy() failure and 778 * terminate_walk(). 779 */ 780 static bool try_to_unlazy(struct nameidata *nd) 781 { 782 struct dentry *parent = nd->path.dentry; 783 784 BUG_ON(!(nd->flags & LOOKUP_RCU)); 785 786 if (unlikely(!legitimize_links(nd))) 787 goto out1; 788 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) 789 goto out; 790 if (unlikely(!legitimize_root(nd))) 791 goto out; 792 leave_rcu(nd); 793 BUG_ON(nd->inode != parent->d_inode); 794 return true; 795 796 out1: 797 nd->path.mnt = NULL; 798 nd->path.dentry = NULL; 799 out: 800 leave_rcu(nd); 801 return false; 802 } 803 804 /** 805 * try_to_unlazy_next - try to switch to ref-walk mode. 806 * @nd: nameidata pathwalk data 807 * @dentry: next dentry to step into 808 * Returns: true on success, false on failure 809 * 810 * Similar to try_to_unlazy(), but here we have the next dentry already 811 * picked by rcu-walk and want to legitimize that in addition to the current 812 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. 813 * Nothing should touch nameidata between try_to_unlazy_next() failure and 814 * terminate_walk(). 815 */ 816 static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry) 817 { 818 int res; 819 BUG_ON(!(nd->flags & LOOKUP_RCU)); 820 821 if (unlikely(!legitimize_links(nd))) 822 goto out2; 823 res = __legitimize_mnt(nd->path.mnt, nd->m_seq); 824 if (unlikely(res)) { 825 if (res > 0) 826 goto out2; 827 goto out1; 828 } 829 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) 830 goto out1; 831 832 /* 833 * We need to move both the parent and the dentry from the RCU domain 834 * to be properly refcounted. And the sequence number in the dentry 835 * validates *both* dentry counters, since we checked the sequence 836 * number of the parent after we got the child sequence number. So we 837 * know the parent must still be valid if the child sequence number is 838 */ 839 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) 840 goto out; 841 if (read_seqcount_retry(&dentry->d_seq, nd->next_seq)) 842 goto out_dput; 843 /* 844 * Sequence counts matched. Now make sure that the root is 845 * still valid and get it if required. 846 */ 847 if (unlikely(!legitimize_root(nd))) 848 goto out_dput; 849 leave_rcu(nd); 850 return true; 851 852 out2: 853 nd->path.mnt = NULL; 854 out1: 855 nd->path.dentry = NULL; 856 out: 857 leave_rcu(nd); 858 return false; 859 out_dput: 860 leave_rcu(nd); 861 dput(dentry); 862 return false; 863 } 864 865 static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 866 { 867 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) 868 return dentry->d_op->d_revalidate(dentry, flags); 869 else 870 return 1; 871 } 872 873 /** 874 * complete_walk - successful completion of path walk 875 * @nd: pointer nameidata 876 * 877 * If we had been in RCU mode, drop out of it and legitimize nd->path. 878 * Revalidate the final result, unless we'd already done that during 879 * the path walk or the filesystem doesn't ask for it. Return 0 on 880 * success, -error on failure. In case of failure caller does not 881 * need to drop nd->path. 882 */ 883 static int complete_walk(struct nameidata *nd) 884 { 885 struct dentry *dentry = nd->path.dentry; 886 int status; 887 888 if (nd->flags & LOOKUP_RCU) { 889 /* 890 * We don't want to zero nd->root for scoped-lookups or 891 * externally-managed nd->root. 892 */ 893 if (!(nd->state & ND_ROOT_PRESET)) 894 if (!(nd->flags & LOOKUP_IS_SCOPED)) 895 nd->root.mnt = NULL; 896 nd->flags &= ~LOOKUP_CACHED; 897 if (!try_to_unlazy(nd)) 898 return -ECHILD; 899 } 900 901 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 902 /* 903 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't 904 * ever step outside the root during lookup" and should already 905 * be guaranteed by the rest of namei, we want to avoid a namei 906 * BUG resulting in userspace being given a path that was not 907 * scoped within the root at some point during the lookup. 908 * 909 * So, do a final sanity-check to make sure that in the 910 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) 911 * we won't silently return an fd completely outside of the 912 * requested root to userspace. 913 * 914 * Userspace could move the path outside the root after this 915 * check, but as discussed elsewhere this is not a concern (the 916 * resolved file was inside the root at some point). 917 */ 918 if (!path_is_under(&nd->path, &nd->root)) 919 return -EXDEV; 920 } 921 922 if (likely(!(nd->state & ND_JUMPED))) 923 return 0; 924 925 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 926 return 0; 927 928 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 929 if (status > 0) 930 return 0; 931 932 if (!status) 933 status = -ESTALE; 934 935 return status; 936 } 937 938 static int set_root(struct nameidata *nd) 939 { 940 struct fs_struct *fs = current->fs; 941 942 /* 943 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we 944 * still have to ensure it doesn't happen because it will cause a breakout 945 * from the dirfd. 946 */ 947 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) 948 return -ENOTRECOVERABLE; 949 950 if (nd->flags & LOOKUP_RCU) { 951 unsigned seq; 952 953 do { 954 seq = read_seqcount_begin(&fs->seq); 955 nd->root = fs->root; 956 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 957 } while (read_seqcount_retry(&fs->seq, seq)); 958 } else { 959 get_fs_root(fs, &nd->root); 960 nd->state |= ND_ROOT_GRABBED; 961 } 962 return 0; 963 } 964 965 static int nd_jump_root(struct nameidata *nd) 966 { 967 if (unlikely(nd->flags & LOOKUP_BENEATH)) 968 return -EXDEV; 969 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 970 /* Absolute path arguments to path_init() are allowed. */ 971 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) 972 return -EXDEV; 973 } 974 if (!nd->root.mnt) { 975 int error = set_root(nd); 976 if (error) 977 return error; 978 } 979 if (nd->flags & LOOKUP_RCU) { 980 struct dentry *d; 981 nd->path = nd->root; 982 d = nd->path.dentry; 983 nd->inode = d->d_inode; 984 nd->seq = nd->root_seq; 985 if (read_seqcount_retry(&d->d_seq, nd->seq)) 986 return -ECHILD; 987 } else { 988 path_put(&nd->path); 989 nd->path = nd->root; 990 path_get(&nd->path); 991 nd->inode = nd->path.dentry->d_inode; 992 } 993 nd->state |= ND_JUMPED; 994 return 0; 995 } 996 997 /* 998 * Helper to directly jump to a known parsed path from ->get_link, 999 * caller must have taken a reference to path beforehand. 1000 */ 1001 int nd_jump_link(const struct path *path) 1002 { 1003 int error = -ELOOP; 1004 struct nameidata *nd = current->nameidata; 1005 1006 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) 1007 goto err; 1008 1009 error = -EXDEV; 1010 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 1011 if (nd->path.mnt != path->mnt) 1012 goto err; 1013 } 1014 /* Not currently safe for scoped-lookups. */ 1015 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) 1016 goto err; 1017 1018 path_put(&nd->path); 1019 nd->path = *path; 1020 nd->inode = nd->path.dentry->d_inode; 1021 nd->state |= ND_JUMPED; 1022 return 0; 1023 1024 err: 1025 path_put(path); 1026 return error; 1027 } 1028 1029 static inline void put_link(struct nameidata *nd) 1030 { 1031 struct saved *last = nd->stack + --nd->depth; 1032 do_delayed_call(&last->done); 1033 if (!(nd->flags & LOOKUP_RCU)) 1034 path_put(&last->link); 1035 } 1036 1037 static int sysctl_protected_symlinks __read_mostly; 1038 static int sysctl_protected_hardlinks __read_mostly; 1039 static int sysctl_protected_fifos __read_mostly; 1040 static int sysctl_protected_regular __read_mostly; 1041 1042 #ifdef CONFIG_SYSCTL 1043 static struct ctl_table namei_sysctls[] = { 1044 { 1045 .procname = "protected_symlinks", 1046 .data = &sysctl_protected_symlinks, 1047 .maxlen = sizeof(int), 1048 .mode = 0644, 1049 .proc_handler = proc_dointvec_minmax, 1050 .extra1 = SYSCTL_ZERO, 1051 .extra2 = SYSCTL_ONE, 1052 }, 1053 { 1054 .procname = "protected_hardlinks", 1055 .data = &sysctl_protected_hardlinks, 1056 .maxlen = sizeof(int), 1057 .mode = 0644, 1058 .proc_handler = proc_dointvec_minmax, 1059 .extra1 = SYSCTL_ZERO, 1060 .extra2 = SYSCTL_ONE, 1061 }, 1062 { 1063 .procname = "protected_fifos", 1064 .data = &sysctl_protected_fifos, 1065 .maxlen = sizeof(int), 1066 .mode = 0644, 1067 .proc_handler = proc_dointvec_minmax, 1068 .extra1 = SYSCTL_ZERO, 1069 .extra2 = SYSCTL_TWO, 1070 }, 1071 { 1072 .procname = "protected_regular", 1073 .data = &sysctl_protected_regular, 1074 .maxlen = sizeof(int), 1075 .mode = 0644, 1076 .proc_handler = proc_dointvec_minmax, 1077 .extra1 = SYSCTL_ZERO, 1078 .extra2 = SYSCTL_TWO, 1079 }, 1080 }; 1081 1082 static int __init init_fs_namei_sysctls(void) 1083 { 1084 register_sysctl_init("fs", namei_sysctls); 1085 return 0; 1086 } 1087 fs_initcall(init_fs_namei_sysctls); 1088 1089 #endif /* CONFIG_SYSCTL */ 1090 1091 /** 1092 * may_follow_link - Check symlink following for unsafe situations 1093 * @nd: nameidata pathwalk data 1094 * @inode: Used for idmapping. 1095 * 1096 * In the case of the sysctl_protected_symlinks sysctl being enabled, 1097 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 1098 * in a sticky world-writable directory. This is to protect privileged 1099 * processes from failing races against path names that may change out 1100 * from under them by way of other users creating malicious symlinks. 1101 * It will permit symlinks to be followed only when outside a sticky 1102 * world-writable directory, or when the uid of the symlink and follower 1103 * match, or when the directory owner matches the symlink's owner. 1104 * 1105 * Returns 0 if following the symlink is allowed, -ve on error. 1106 */ 1107 static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) 1108 { 1109 struct mnt_idmap *idmap; 1110 vfsuid_t vfsuid; 1111 1112 if (!sysctl_protected_symlinks) 1113 return 0; 1114 1115 idmap = mnt_idmap(nd->path.mnt); 1116 vfsuid = i_uid_into_vfsuid(idmap, inode); 1117 /* Allowed if owner and follower match. */ 1118 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) 1119 return 0; 1120 1121 /* Allowed if parent directory not sticky and world-writable. */ 1122 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 1123 return 0; 1124 1125 /* Allowed if parent directory and link owner match. */ 1126 if (vfsuid_valid(nd->dir_vfsuid) && vfsuid_eq(nd->dir_vfsuid, vfsuid)) 1127 return 0; 1128 1129 if (nd->flags & LOOKUP_RCU) 1130 return -ECHILD; 1131 1132 audit_inode(nd->name, nd->stack[0].link.dentry, 0); 1133 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); 1134 return -EACCES; 1135 } 1136 1137 /** 1138 * safe_hardlink_source - Check for safe hardlink conditions 1139 * @idmap: idmap of the mount the inode was found from 1140 * @inode: the source inode to hardlink from 1141 * 1142 * Return false if at least one of the following conditions: 1143 * - inode is not a regular file 1144 * - inode is setuid 1145 * - inode is setgid and group-exec 1146 * - access failure for read and write 1147 * 1148 * Otherwise returns true. 1149 */ 1150 static bool safe_hardlink_source(struct mnt_idmap *idmap, 1151 struct inode *inode) 1152 { 1153 umode_t mode = inode->i_mode; 1154 1155 /* Special files should not get pinned to the filesystem. */ 1156 if (!S_ISREG(mode)) 1157 return false; 1158 1159 /* Setuid files should not get pinned to the filesystem. */ 1160 if (mode & S_ISUID) 1161 return false; 1162 1163 /* Executable setgid files should not get pinned to the filesystem. */ 1164 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 1165 return false; 1166 1167 /* Hardlinking to unreadable or unwritable sources is dangerous. */ 1168 if (inode_permission(idmap, inode, MAY_READ | MAY_WRITE)) 1169 return false; 1170 1171 return true; 1172 } 1173 1174 /** 1175 * may_linkat - Check permissions for creating a hardlink 1176 * @idmap: idmap of the mount the inode was found from 1177 * @link: the source to hardlink from 1178 * 1179 * Block hardlink when all of: 1180 * - sysctl_protected_hardlinks enabled 1181 * - fsuid does not match inode 1182 * - hardlink source is unsafe (see safe_hardlink_source() above) 1183 * - not CAP_FOWNER in a namespace with the inode owner uid mapped 1184 * 1185 * If the inode has been found through an idmapped mount the idmap of 1186 * the vfsmount must be passed through @idmap. This function will then take 1187 * care to map the inode according to @idmap before checking permissions. 1188 * On non-idmapped mounts or if permission checking is to be performed on the 1189 * raw inode simply pass @nop_mnt_idmap. 1190 * 1191 * Returns 0 if successful, -ve on error. 1192 */ 1193 int may_linkat(struct mnt_idmap *idmap, const struct path *link) 1194 { 1195 struct inode *inode = link->dentry->d_inode; 1196 1197 /* Inode writeback is not safe when the uid or gid are invalid. */ 1198 if (!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) || 1199 !vfsgid_valid(i_gid_into_vfsgid(idmap, inode))) 1200 return -EOVERFLOW; 1201 1202 if (!sysctl_protected_hardlinks) 1203 return 0; 1204 1205 /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 1206 * otherwise, it must be a safe source. 1207 */ 1208 if (safe_hardlink_source(idmap, inode) || 1209 inode_owner_or_capable(idmap, inode)) 1210 return 0; 1211 1212 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); 1213 return -EPERM; 1214 } 1215 1216 /** 1217 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory 1218 * should be allowed, or not, on files that already 1219 * exist. 1220 * @idmap: idmap of the mount the inode was found from 1221 * @nd: nameidata pathwalk data 1222 * @inode: the inode of the file to open 1223 * 1224 * Block an O_CREAT open of a FIFO (or a regular file) when: 1225 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled 1226 * - the file already exists 1227 * - we are in a sticky directory 1228 * - we don't own the file 1229 * - the owner of the directory doesn't own the file 1230 * - the directory is world writable 1231 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 1232 * the directory doesn't have to be world writable: being group writable will 1233 * be enough. 1234 * 1235 * If the inode has been found through an idmapped mount the idmap of 1236 * the vfsmount must be passed through @idmap. This function will then take 1237 * care to map the inode according to @idmap before checking permissions. 1238 * On non-idmapped mounts or if permission checking is to be performed on the 1239 * raw inode simply pass @nop_mnt_idmap. 1240 * 1241 * Returns 0 if the open is allowed, -ve on error. 1242 */ 1243 static int may_create_in_sticky(struct mnt_idmap *idmap, struct nameidata *nd, 1244 struct inode *const inode) 1245 { 1246 umode_t dir_mode = nd->dir_mode; 1247 vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; 1248 1249 if (likely(!(dir_mode & S_ISVTX))) 1250 return 0; 1251 1252 if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) 1253 return 0; 1254 1255 if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) 1256 return 0; 1257 1258 i_vfsuid = i_uid_into_vfsuid(idmap, inode); 1259 1260 if (vfsuid_eq(i_vfsuid, dir_vfsuid)) 1261 return 0; 1262 1263 if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) 1264 return 0; 1265 1266 if (likely(dir_mode & 0002)) { 1267 audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); 1268 return -EACCES; 1269 } 1270 1271 if (dir_mode & 0020) { 1272 if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { 1273 audit_log_path_denied(AUDIT_ANOM_CREAT, 1274 "sticky_create_fifo"); 1275 return -EACCES; 1276 } 1277 1278 if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { 1279 audit_log_path_denied(AUDIT_ANOM_CREAT, 1280 "sticky_create_regular"); 1281 return -EACCES; 1282 } 1283 } 1284 1285 return 0; 1286 } 1287 1288 /* 1289 * follow_up - Find the mountpoint of path's vfsmount 1290 * 1291 * Given a path, find the mountpoint of its source file system. 1292 * Replace @path with the path of the mountpoint in the parent mount. 1293 * Up is towards /. 1294 * 1295 * Return 1 if we went up a level and 0 if we were already at the 1296 * root. 1297 */ 1298 int follow_up(struct path *path) 1299 { 1300 struct mount *mnt = real_mount(path->mnt); 1301 struct mount *parent; 1302 struct dentry *mountpoint; 1303 1304 read_seqlock_excl(&mount_lock); 1305 parent = mnt->mnt_parent; 1306 if (parent == mnt) { 1307 read_sequnlock_excl(&mount_lock); 1308 return 0; 1309 } 1310 mntget(&parent->mnt); 1311 mountpoint = dget(mnt->mnt_mountpoint); 1312 read_sequnlock_excl(&mount_lock); 1313 dput(path->dentry); 1314 path->dentry = mountpoint; 1315 mntput(path->mnt); 1316 path->mnt = &parent->mnt; 1317 return 1; 1318 } 1319 EXPORT_SYMBOL(follow_up); 1320 1321 static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, 1322 struct path *path, unsigned *seqp) 1323 { 1324 while (mnt_has_parent(m)) { 1325 struct dentry *mountpoint = m->mnt_mountpoint; 1326 1327 m = m->mnt_parent; 1328 if (unlikely(root->dentry == mountpoint && 1329 root->mnt == &m->mnt)) 1330 break; 1331 if (mountpoint != m->mnt.mnt_root) { 1332 path->mnt = &m->mnt; 1333 path->dentry = mountpoint; 1334 *seqp = read_seqcount_begin(&mountpoint->d_seq); 1335 return true; 1336 } 1337 } 1338 return false; 1339 } 1340 1341 static bool choose_mountpoint(struct mount *m, const struct path *root, 1342 struct path *path) 1343 { 1344 bool found; 1345 1346 rcu_read_lock(); 1347 while (1) { 1348 unsigned seq, mseq = read_seqbegin(&mount_lock); 1349 1350 found = choose_mountpoint_rcu(m, root, path, &seq); 1351 if (unlikely(!found)) { 1352 if (!read_seqretry(&mount_lock, mseq)) 1353 break; 1354 } else { 1355 if (likely(__legitimize_path(path, seq, mseq))) 1356 break; 1357 rcu_read_unlock(); 1358 path_put(path); 1359 rcu_read_lock(); 1360 } 1361 } 1362 rcu_read_unlock(); 1363 return found; 1364 } 1365 1366 /* 1367 * Perform an automount 1368 * - return -EISDIR to tell follow_managed() to stop and return the path we 1369 * were called with. 1370 */ 1371 static int follow_automount(struct path *path, int *count, unsigned lookup_flags) 1372 { 1373 struct dentry *dentry = path->dentry; 1374 1375 /* We don't want to mount if someone's just doing a stat - 1376 * unless they're stat'ing a directory and appended a '/' to 1377 * the name. 1378 * 1379 * We do, however, want to mount if someone wants to open or 1380 * create a file of any type under the mountpoint, wants to 1381 * traverse through the mountpoint or wants to open the 1382 * mounted directory. Also, autofs may mark negative dentries 1383 * as being automount points. These will need the attentions 1384 * of the daemon to instantiate them before they can be used. 1385 */ 1386 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 1387 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 1388 dentry->d_inode) 1389 return -EISDIR; 1390 1391 if (count && (*count)++ >= MAXSYMLINKS) 1392 return -ELOOP; 1393 1394 return finish_automount(dentry->d_op->d_automount(path), path); 1395 } 1396 1397 /* 1398 * mount traversal - out-of-line part. One note on ->d_flags accesses - 1399 * dentries are pinned but not locked here, so negative dentry can go 1400 * positive right under us. Use of smp_load_acquire() provides a barrier 1401 * sufficient for ->d_inode and ->d_flags consistency. 1402 */ 1403 static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, 1404 int *count, unsigned lookup_flags) 1405 { 1406 struct vfsmount *mnt = path->mnt; 1407 bool need_mntput = false; 1408 int ret = 0; 1409 1410 while (flags & DCACHE_MANAGED_DENTRY) { 1411 /* Allow the filesystem to manage the transit without i_mutex 1412 * being held. */ 1413 if (flags & DCACHE_MANAGE_TRANSIT) { 1414 ret = path->dentry->d_op->d_manage(path, false); 1415 flags = smp_load_acquire(&path->dentry->d_flags); 1416 if (ret < 0) 1417 break; 1418 } 1419 1420 if (flags & DCACHE_MOUNTED) { // something's mounted on it.. 1421 struct vfsmount *mounted = lookup_mnt(path); 1422 if (mounted) { // ... in our namespace 1423 dput(path->dentry); 1424 if (need_mntput) 1425 mntput(path->mnt); 1426 path->mnt = mounted; 1427 path->dentry = dget(mounted->mnt_root); 1428 // here we know it's positive 1429 flags = path->dentry->d_flags; 1430 need_mntput = true; 1431 continue; 1432 } 1433 } 1434 1435 if (!(flags & DCACHE_NEED_AUTOMOUNT)) 1436 break; 1437 1438 // uncovered automount point 1439 ret = follow_automount(path, count, lookup_flags); 1440 flags = smp_load_acquire(&path->dentry->d_flags); 1441 if (ret < 0) 1442 break; 1443 } 1444 1445 if (ret == -EISDIR) 1446 ret = 0; 1447 // possible if you race with several mount --move 1448 if (need_mntput && path->mnt == mnt) 1449 mntput(path->mnt); 1450 if (!ret && unlikely(d_flags_negative(flags))) 1451 ret = -ENOENT; 1452 *jumped = need_mntput; 1453 return ret; 1454 } 1455 1456 static inline int traverse_mounts(struct path *path, bool *jumped, 1457 int *count, unsigned lookup_flags) 1458 { 1459 unsigned flags = smp_load_acquire(&path->dentry->d_flags); 1460 1461 /* fastpath */ 1462 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { 1463 *jumped = false; 1464 if (unlikely(d_flags_negative(flags))) 1465 return -ENOENT; 1466 return 0; 1467 } 1468 return __traverse_mounts(path, flags, jumped, count, lookup_flags); 1469 } 1470 1471 int follow_down_one(struct path *path) 1472 { 1473 struct vfsmount *mounted; 1474 1475 mounted = lookup_mnt(path); 1476 if (mounted) { 1477 dput(path->dentry); 1478 mntput(path->mnt); 1479 path->mnt = mounted; 1480 path->dentry = dget(mounted->mnt_root); 1481 return 1; 1482 } 1483 return 0; 1484 } 1485 EXPORT_SYMBOL(follow_down_one); 1486 1487 /* 1488 * Follow down to the covering mount currently visible to userspace. At each 1489 * point, the filesystem owning that dentry may be queried as to whether the 1490 * caller is permitted to proceed or not. 1491 */ 1492 int follow_down(struct path *path, unsigned int flags) 1493 { 1494 struct vfsmount *mnt = path->mnt; 1495 bool jumped; 1496 int ret = traverse_mounts(path, &jumped, NULL, flags); 1497 1498 if (path->mnt != mnt) 1499 mntput(mnt); 1500 return ret; 1501 } 1502 EXPORT_SYMBOL(follow_down); 1503 1504 /* 1505 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1506 * we meet a managed dentry that would need blocking. 1507 */ 1508 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path) 1509 { 1510 struct dentry *dentry = path->dentry; 1511 unsigned int flags = dentry->d_flags; 1512 1513 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) 1514 return true; 1515 1516 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1517 return false; 1518 1519 for (;;) { 1520 /* 1521 * Don't forget we might have a non-mountpoint managed dentry 1522 * that wants to block transit. 1523 */ 1524 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { 1525 int res = dentry->d_op->d_manage(path, true); 1526 if (res) 1527 return res == -EISDIR; 1528 flags = dentry->d_flags; 1529 } 1530 1531 if (flags & DCACHE_MOUNTED) { 1532 struct mount *mounted = __lookup_mnt(path->mnt, dentry); 1533 if (mounted) { 1534 path->mnt = &mounted->mnt; 1535 dentry = path->dentry = mounted->mnt.mnt_root; 1536 nd->state |= ND_JUMPED; 1537 nd->next_seq = read_seqcount_begin(&dentry->d_seq); 1538 flags = dentry->d_flags; 1539 // makes sure that non-RCU pathwalk could reach 1540 // this state. 1541 if (read_seqretry(&mount_lock, nd->m_seq)) 1542 return false; 1543 continue; 1544 } 1545 if (read_seqretry(&mount_lock, nd->m_seq)) 1546 return false; 1547 } 1548 return !(flags & DCACHE_NEED_AUTOMOUNT); 1549 } 1550 } 1551 1552 static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, 1553 struct path *path) 1554 { 1555 bool jumped; 1556 int ret; 1557 1558 path->mnt = nd->path.mnt; 1559 path->dentry = dentry; 1560 if (nd->flags & LOOKUP_RCU) { 1561 unsigned int seq = nd->next_seq; 1562 if (likely(__follow_mount_rcu(nd, path))) 1563 return 0; 1564 // *path and nd->next_seq might've been clobbered 1565 path->mnt = nd->path.mnt; 1566 path->dentry = dentry; 1567 nd->next_seq = seq; 1568 if (!try_to_unlazy_next(nd, dentry)) 1569 return -ECHILD; 1570 } 1571 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); 1572 if (jumped) { 1573 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1574 ret = -EXDEV; 1575 else 1576 nd->state |= ND_JUMPED; 1577 } 1578 if (unlikely(ret)) { 1579 dput(path->dentry); 1580 if (path->mnt != nd->path.mnt) 1581 mntput(path->mnt); 1582 } 1583 return ret; 1584 } 1585 1586 /* 1587 * This looks up the name in dcache and possibly revalidates the found dentry. 1588 * NULL is returned if the dentry does not exist in the cache. 1589 */ 1590 static struct dentry *lookup_dcache(const struct qstr *name, 1591 struct dentry *dir, 1592 unsigned int flags) 1593 { 1594 struct dentry *dentry = d_lookup(dir, name); 1595 if (dentry) { 1596 int error = d_revalidate(dentry, flags); 1597 if (unlikely(error <= 0)) { 1598 if (!error) 1599 d_invalidate(dentry); 1600 dput(dentry); 1601 return ERR_PTR(error); 1602 } 1603 } 1604 return dentry; 1605 } 1606 1607 /* 1608 * Parent directory has inode locked exclusive. This is one 1609 * and only case when ->lookup() gets called on non in-lookup 1610 * dentries - as the matter of fact, this only gets called 1611 * when directory is guaranteed to have no in-lookup children 1612 * at all. 1613 */ 1614 struct dentry *lookup_one_qstr_excl(const struct qstr *name, 1615 struct dentry *base, 1616 unsigned int flags) 1617 { 1618 struct dentry *dentry = lookup_dcache(name, base, flags); 1619 struct dentry *old; 1620 struct inode *dir = base->d_inode; 1621 1622 if (dentry) 1623 return dentry; 1624 1625 /* Don't create child dentry for a dead directory. */ 1626 if (unlikely(IS_DEADDIR(dir))) 1627 return ERR_PTR(-ENOENT); 1628 1629 dentry = d_alloc(base, name); 1630 if (unlikely(!dentry)) 1631 return ERR_PTR(-ENOMEM); 1632 1633 old = dir->i_op->lookup(dir, dentry, flags); 1634 if (unlikely(old)) { 1635 dput(dentry); 1636 dentry = old; 1637 } 1638 return dentry; 1639 } 1640 EXPORT_SYMBOL(lookup_one_qstr_excl); 1641 1642 /** 1643 * lookup_fast - do fast lockless (but racy) lookup of a dentry 1644 * @nd: current nameidata 1645 * 1646 * Do a fast, but racy lookup in the dcache for the given dentry, and 1647 * revalidate it. Returns a valid dentry pointer or NULL if one wasn't 1648 * found. On error, an ERR_PTR will be returned. 1649 * 1650 * If this function returns a valid dentry and the walk is no longer 1651 * lazy, the dentry will carry a reference that must later be put. If 1652 * RCU mode is still in force, then this is not the case and the dentry 1653 * must be legitimized before use. If this returns NULL, then the walk 1654 * will no longer be in RCU mode. 1655 */ 1656 static struct dentry *lookup_fast(struct nameidata *nd) 1657 { 1658 struct dentry *dentry, *parent = nd->path.dentry; 1659 int status = 1; 1660 1661 /* 1662 * Rename seqlock is not required here because in the off chance 1663 * of a false negative due to a concurrent rename, the caller is 1664 * going to fall back to non-racy lookup. 1665 */ 1666 if (nd->flags & LOOKUP_RCU) { 1667 dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq); 1668 if (unlikely(!dentry)) { 1669 if (!try_to_unlazy(nd)) 1670 return ERR_PTR(-ECHILD); 1671 return NULL; 1672 } 1673 1674 /* 1675 * This sequence count validates that the parent had no 1676 * changes while we did the lookup of the dentry above. 1677 */ 1678 if (read_seqcount_retry(&parent->d_seq, nd->seq)) 1679 return ERR_PTR(-ECHILD); 1680 1681 status = d_revalidate(dentry, nd->flags); 1682 if (likely(status > 0)) 1683 return dentry; 1684 if (!try_to_unlazy_next(nd, dentry)) 1685 return ERR_PTR(-ECHILD); 1686 if (status == -ECHILD) 1687 /* we'd been told to redo it in non-rcu mode */ 1688 status = d_revalidate(dentry, nd->flags); 1689 } else { 1690 dentry = __d_lookup(parent, &nd->last); 1691 if (unlikely(!dentry)) 1692 return NULL; 1693 status = d_revalidate(dentry, nd->flags); 1694 } 1695 if (unlikely(status <= 0)) { 1696 if (!status) 1697 d_invalidate(dentry); 1698 dput(dentry); 1699 return ERR_PTR(status); 1700 } 1701 return dentry; 1702 } 1703 1704 /* Fast lookup failed, do it the slow way */ 1705 static struct dentry *__lookup_slow(const struct qstr *name, 1706 struct dentry *dir, 1707 unsigned int flags) 1708 { 1709 struct dentry *dentry, *old; 1710 struct inode *inode = dir->d_inode; 1711 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1712 1713 /* Don't go there if it's already dead */ 1714 if (unlikely(IS_DEADDIR(inode))) 1715 return ERR_PTR(-ENOENT); 1716 again: 1717 dentry = d_alloc_parallel(dir, name, &wq); 1718 if (IS_ERR(dentry)) 1719 return dentry; 1720 if (unlikely(!d_in_lookup(dentry))) { 1721 int error = d_revalidate(dentry, flags); 1722 if (unlikely(error <= 0)) { 1723 if (!error) { 1724 d_invalidate(dentry); 1725 dput(dentry); 1726 goto again; 1727 } 1728 dput(dentry); 1729 dentry = ERR_PTR(error); 1730 } 1731 } else { 1732 old = inode->i_op->lookup(inode, dentry, flags); 1733 d_lookup_done(dentry); 1734 if (unlikely(old)) { 1735 dput(dentry); 1736 dentry = old; 1737 } 1738 } 1739 return dentry; 1740 } 1741 1742 static struct dentry *lookup_slow(const struct qstr *name, 1743 struct dentry *dir, 1744 unsigned int flags) 1745 { 1746 struct inode *inode = dir->d_inode; 1747 struct dentry *res; 1748 inode_lock_shared(inode); 1749 res = __lookup_slow(name, dir, flags); 1750 inode_unlock_shared(inode); 1751 return res; 1752 } 1753 1754 static inline int may_lookup(struct mnt_idmap *idmap, 1755 struct nameidata *restrict nd) 1756 { 1757 int err, mask; 1758 1759 mask = nd->flags & LOOKUP_RCU ? MAY_NOT_BLOCK : 0; 1760 err = inode_permission(idmap, nd->inode, mask | MAY_EXEC); 1761 if (likely(!err)) 1762 return 0; 1763 1764 // If we failed, and we weren't in LOOKUP_RCU, it's final 1765 if (!(nd->flags & LOOKUP_RCU)) 1766 return err; 1767 1768 // Drop out of RCU mode to make sure it wasn't transient 1769 if (!try_to_unlazy(nd)) 1770 return -ECHILD; // redo it all non-lazy 1771 1772 if (err != -ECHILD) // hard error 1773 return err; 1774 1775 return inode_permission(idmap, nd->inode, MAY_EXEC); 1776 } 1777 1778 static int reserve_stack(struct nameidata *nd, struct path *link) 1779 { 1780 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) 1781 return -ELOOP; 1782 1783 if (likely(nd->depth != EMBEDDED_LEVELS)) 1784 return 0; 1785 if (likely(nd->stack != nd->internal)) 1786 return 0; 1787 if (likely(nd_alloc_stack(nd))) 1788 return 0; 1789 1790 if (nd->flags & LOOKUP_RCU) { 1791 // we need to grab link before we do unlazy. And we can't skip 1792 // unlazy even if we fail to grab the link - cleanup needs it 1793 bool grabbed_link = legitimize_path(nd, link, nd->next_seq); 1794 1795 if (!try_to_unlazy(nd) || !grabbed_link) 1796 return -ECHILD; 1797 1798 if (nd_alloc_stack(nd)) 1799 return 0; 1800 } 1801 return -ENOMEM; 1802 } 1803 1804 enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; 1805 1806 static const char *pick_link(struct nameidata *nd, struct path *link, 1807 struct inode *inode, int flags) 1808 { 1809 struct saved *last; 1810 const char *res; 1811 int error = reserve_stack(nd, link); 1812 1813 if (unlikely(error)) { 1814 if (!(nd->flags & LOOKUP_RCU)) 1815 path_put(link); 1816 return ERR_PTR(error); 1817 } 1818 last = nd->stack + nd->depth++; 1819 last->link = *link; 1820 clear_delayed_call(&last->done); 1821 last->seq = nd->next_seq; 1822 1823 if (flags & WALK_TRAILING) { 1824 error = may_follow_link(nd, inode); 1825 if (unlikely(error)) 1826 return ERR_PTR(error); 1827 } 1828 1829 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || 1830 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) 1831 return ERR_PTR(-ELOOP); 1832 1833 if (!(nd->flags & LOOKUP_RCU)) { 1834 touch_atime(&last->link); 1835 cond_resched(); 1836 } else if (atime_needs_update(&last->link, inode)) { 1837 if (!try_to_unlazy(nd)) 1838 return ERR_PTR(-ECHILD); 1839 touch_atime(&last->link); 1840 } 1841 1842 error = security_inode_follow_link(link->dentry, inode, 1843 nd->flags & LOOKUP_RCU); 1844 if (unlikely(error)) 1845 return ERR_PTR(error); 1846 1847 res = READ_ONCE(inode->i_link); 1848 if (!res) { 1849 const char * (*get)(struct dentry *, struct inode *, 1850 struct delayed_call *); 1851 get = inode->i_op->get_link; 1852 if (nd->flags & LOOKUP_RCU) { 1853 res = get(NULL, inode, &last->done); 1854 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) 1855 res = get(link->dentry, inode, &last->done); 1856 } else { 1857 res = get(link->dentry, inode, &last->done); 1858 } 1859 if (!res) 1860 goto all_done; 1861 if (IS_ERR(res)) 1862 return res; 1863 } 1864 if (*res == '/') { 1865 error = nd_jump_root(nd); 1866 if (unlikely(error)) 1867 return ERR_PTR(error); 1868 while (unlikely(*++res == '/')) 1869 ; 1870 } 1871 if (*res) 1872 return res; 1873 all_done: // pure jump 1874 put_link(nd); 1875 return NULL; 1876 } 1877 1878 /* 1879 * Do we need to follow links? We _really_ want to be able 1880 * to do this check without having to look at inode->i_op, 1881 * so we keep a cache of "no, this doesn't need follow_link" 1882 * for the common case. 1883 * 1884 * NOTE: dentry must be what nd->next_seq had been sampled from. 1885 */ 1886 static const char *step_into(struct nameidata *nd, int flags, 1887 struct dentry *dentry) 1888 { 1889 struct path path; 1890 struct inode *inode; 1891 int err = handle_mounts(nd, dentry, &path); 1892 1893 if (err < 0) 1894 return ERR_PTR(err); 1895 inode = path.dentry->d_inode; 1896 if (likely(!d_is_symlink(path.dentry)) || 1897 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || 1898 (flags & WALK_NOFOLLOW)) { 1899 /* not a symlink or should not follow */ 1900 if (nd->flags & LOOKUP_RCU) { 1901 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) 1902 return ERR_PTR(-ECHILD); 1903 if (unlikely(!inode)) 1904 return ERR_PTR(-ENOENT); 1905 } else { 1906 dput(nd->path.dentry); 1907 if (nd->path.mnt != path.mnt) 1908 mntput(nd->path.mnt); 1909 } 1910 nd->path = path; 1911 nd->inode = inode; 1912 nd->seq = nd->next_seq; 1913 return NULL; 1914 } 1915 if (nd->flags & LOOKUP_RCU) { 1916 /* make sure that d_is_symlink above matches inode */ 1917 if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) 1918 return ERR_PTR(-ECHILD); 1919 } else { 1920 if (path.mnt == nd->path.mnt) 1921 mntget(path.mnt); 1922 } 1923 return pick_link(nd, &path, inode, flags); 1924 } 1925 1926 static struct dentry *follow_dotdot_rcu(struct nameidata *nd) 1927 { 1928 struct dentry *parent, *old; 1929 1930 if (path_equal(&nd->path, &nd->root)) 1931 goto in_root; 1932 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 1933 struct path path; 1934 unsigned seq; 1935 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), 1936 &nd->root, &path, &seq)) 1937 goto in_root; 1938 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1939 return ERR_PTR(-ECHILD); 1940 nd->path = path; 1941 nd->inode = path.dentry->d_inode; 1942 nd->seq = seq; 1943 // makes sure that non-RCU pathwalk could reach this state 1944 if (read_seqretry(&mount_lock, nd->m_seq)) 1945 return ERR_PTR(-ECHILD); 1946 /* we know that mountpoint was pinned */ 1947 } 1948 old = nd->path.dentry; 1949 parent = old->d_parent; 1950 nd->next_seq = read_seqcount_begin(&parent->d_seq); 1951 // makes sure that non-RCU pathwalk could reach this state 1952 if (read_seqcount_retry(&old->d_seq, nd->seq)) 1953 return ERR_PTR(-ECHILD); 1954 if (unlikely(!path_connected(nd->path.mnt, parent))) 1955 return ERR_PTR(-ECHILD); 1956 return parent; 1957 in_root: 1958 if (read_seqretry(&mount_lock, nd->m_seq)) 1959 return ERR_PTR(-ECHILD); 1960 if (unlikely(nd->flags & LOOKUP_BENEATH)) 1961 return ERR_PTR(-ECHILD); 1962 nd->next_seq = nd->seq; 1963 return nd->path.dentry; 1964 } 1965 1966 static struct dentry *follow_dotdot(struct nameidata *nd) 1967 { 1968 struct dentry *parent; 1969 1970 if (path_equal(&nd->path, &nd->root)) 1971 goto in_root; 1972 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 1973 struct path path; 1974 1975 if (!choose_mountpoint(real_mount(nd->path.mnt), 1976 &nd->root, &path)) 1977 goto in_root; 1978 path_put(&nd->path); 1979 nd->path = path; 1980 nd->inode = path.dentry->d_inode; 1981 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1982 return ERR_PTR(-EXDEV); 1983 } 1984 /* rare case of legitimate dget_parent()... */ 1985 parent = dget_parent(nd->path.dentry); 1986 if (unlikely(!path_connected(nd->path.mnt, parent))) { 1987 dput(parent); 1988 return ERR_PTR(-ENOENT); 1989 } 1990 return parent; 1991 1992 in_root: 1993 if (unlikely(nd->flags & LOOKUP_BENEATH)) 1994 return ERR_PTR(-EXDEV); 1995 return dget(nd->path.dentry); 1996 } 1997 1998 static const char *handle_dots(struct nameidata *nd, int type) 1999 { 2000 if (type == LAST_DOTDOT) { 2001 const char *error = NULL; 2002 struct dentry *parent; 2003 2004 if (!nd->root.mnt) { 2005 error = ERR_PTR(set_root(nd)); 2006 if (error) 2007 return error; 2008 } 2009 if (nd->flags & LOOKUP_RCU) 2010 parent = follow_dotdot_rcu(nd); 2011 else 2012 parent = follow_dotdot(nd); 2013 if (IS_ERR(parent)) 2014 return ERR_CAST(parent); 2015 error = step_into(nd, WALK_NOFOLLOW, parent); 2016 if (unlikely(error)) 2017 return error; 2018 2019 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 2020 /* 2021 * If there was a racing rename or mount along our 2022 * path, then we can't be sure that ".." hasn't jumped 2023 * above nd->root (and so userspace should retry or use 2024 * some fallback). 2025 */ 2026 smp_rmb(); 2027 if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)) 2028 return ERR_PTR(-EAGAIN); 2029 if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)) 2030 return ERR_PTR(-EAGAIN); 2031 } 2032 } 2033 return NULL; 2034 } 2035 2036 static const char *walk_component(struct nameidata *nd, int flags) 2037 { 2038 struct dentry *dentry; 2039 /* 2040 * "." and ".." are special - ".." especially so because it has 2041 * to be able to know about the current root directory and 2042 * parent relationships. 2043 */ 2044 if (unlikely(nd->last_type != LAST_NORM)) { 2045 if (!(flags & WALK_MORE) && nd->depth) 2046 put_link(nd); 2047 return handle_dots(nd, nd->last_type); 2048 } 2049 dentry = lookup_fast(nd); 2050 if (IS_ERR(dentry)) 2051 return ERR_CAST(dentry); 2052 if (unlikely(!dentry)) { 2053 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); 2054 if (IS_ERR(dentry)) 2055 return ERR_CAST(dentry); 2056 } 2057 if (!(flags & WALK_MORE) && nd->depth) 2058 put_link(nd); 2059 return step_into(nd, flags, dentry); 2060 } 2061 2062 /* 2063 * We can do the critical dentry name comparison and hashing 2064 * operations one word at a time, but we are limited to: 2065 * 2066 * - Architectures with fast unaligned word accesses. We could 2067 * do a "get_unaligned()" if this helps and is sufficiently 2068 * fast. 2069 * 2070 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 2071 * do not trap on the (extremely unlikely) case of a page 2072 * crossing operation. 2073 * 2074 * - Furthermore, we need an efficient 64-bit compile for the 2075 * 64-bit case in order to generate the "number of bytes in 2076 * the final mask". Again, that could be replaced with a 2077 * efficient population count instruction or similar. 2078 */ 2079 #ifdef CONFIG_DCACHE_WORD_ACCESS 2080 2081 #include <asm/word-at-a-time.h> 2082 2083 #ifdef HASH_MIX 2084 2085 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ 2086 2087 #elif defined(CONFIG_64BIT) 2088 /* 2089 * Register pressure in the mixing function is an issue, particularly 2090 * on 32-bit x86, but almost any function requires one state value and 2091 * one temporary. Instead, use a function designed for two state values 2092 * and no temporaries. 2093 * 2094 * This function cannot create a collision in only two iterations, so 2095 * we have two iterations to achieve avalanche. In those two iterations, 2096 * we have six layers of mixing, which is enough to spread one bit's 2097 * influence out to 2^6 = 64 state bits. 2098 * 2099 * Rotate constants are scored by considering either 64 one-bit input 2100 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the 2101 * probability of that delta causing a change to each of the 128 output 2102 * bits, using a sample of random initial states. 2103 * 2104 * The Shannon entropy of the computed probabilities is then summed 2105 * to produce a score. Ideally, any input change has a 50% chance of 2106 * toggling any given output bit. 2107 * 2108 * Mixing scores (in bits) for (12,45): 2109 * Input delta: 1-bit 2-bit 2110 * 1 round: 713.3 42542.6 2111 * 2 rounds: 2753.7 140389.8 2112 * 3 rounds: 5954.1 233458.2 2113 * 4 rounds: 7862.6 256672.2 2114 * Perfect: 8192 258048 2115 * (64*128) (64*63/2 * 128) 2116 */ 2117 #define HASH_MIX(x, y, a) \ 2118 ( x ^= (a), \ 2119 y ^= x, x = rol64(x,12),\ 2120 x += y, y = rol64(y,45),\ 2121 y *= 9 ) 2122 2123 /* 2124 * Fold two longs into one 32-bit hash value. This must be fast, but 2125 * latency isn't quite as critical, as there is a fair bit of additional 2126 * work done before the hash value is used. 2127 */ 2128 static inline unsigned int fold_hash(unsigned long x, unsigned long y) 2129 { 2130 y ^= x * GOLDEN_RATIO_64; 2131 y *= GOLDEN_RATIO_64; 2132 return y >> 32; 2133 } 2134 2135 #else /* 32-bit case */ 2136 2137 /* 2138 * Mixing scores (in bits) for (7,20): 2139 * Input delta: 1-bit 2-bit 2140 * 1 round: 330.3 9201.6 2141 * 2 rounds: 1246.4 25475.4 2142 * 3 rounds: 1907.1 31295.1 2143 * 4 rounds: 2042.3 31718.6 2144 * Perfect: 2048 31744 2145 * (32*64) (32*31/2 * 64) 2146 */ 2147 #define HASH_MIX(x, y, a) \ 2148 ( x ^= (a), \ 2149 y ^= x, x = rol32(x, 7),\ 2150 x += y, y = rol32(y,20),\ 2151 y *= 9 ) 2152 2153 static inline unsigned int fold_hash(unsigned long x, unsigned long y) 2154 { 2155 /* Use arch-optimized multiply if one exists */ 2156 return __hash_32(y ^ __hash_32(x)); 2157 } 2158 2159 #endif 2160 2161 /* 2162 * Return the hash of a string of known length. This is carfully 2163 * designed to match hash_name(), which is the more critical function. 2164 * In particular, we must end by hashing a final word containing 0..7 2165 * payload bytes, to match the way that hash_name() iterates until it 2166 * finds the delimiter after the name. 2167 */ 2168 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 2169 { 2170 unsigned long a, x = 0, y = (unsigned long)salt; 2171 2172 for (;;) { 2173 if (!len) 2174 goto done; 2175 a = load_unaligned_zeropad(name); 2176 if (len < sizeof(unsigned long)) 2177 break; 2178 HASH_MIX(x, y, a); 2179 name += sizeof(unsigned long); 2180 len -= sizeof(unsigned long); 2181 } 2182 x ^= a & bytemask_from_count(len); 2183 done: 2184 return fold_hash(x, y); 2185 } 2186 EXPORT_SYMBOL(full_name_hash); 2187 2188 /* Return the "hash_len" (hash and length) of a null-terminated string */ 2189 u64 hashlen_string(const void *salt, const char *name) 2190 { 2191 unsigned long a = 0, x = 0, y = (unsigned long)salt; 2192 unsigned long adata, mask, len; 2193 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2194 2195 len = 0; 2196 goto inside; 2197 2198 do { 2199 HASH_MIX(x, y, a); 2200 len += sizeof(unsigned long); 2201 inside: 2202 a = load_unaligned_zeropad(name+len); 2203 } while (!has_zero(a, &adata, &constants)); 2204 2205 adata = prep_zero_mask(a, adata, &constants); 2206 mask = create_zero_mask(adata); 2207 x ^= a & zero_bytemask(mask); 2208 2209 return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2210 } 2211 EXPORT_SYMBOL(hashlen_string); 2212 2213 /* 2214 * Calculate the length and hash of the path component, and 2215 * return the length as the result. 2216 */ 2217 static inline const char *hash_name(struct nameidata *nd, 2218 const char *name, 2219 unsigned long *lastword) 2220 { 2221 unsigned long a, b, x, y = (unsigned long)nd->path.dentry; 2222 unsigned long adata, bdata, mask, len; 2223 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2224 2225 /* 2226 * The first iteration is special, because it can result in 2227 * '.' and '..' and has no mixing other than the final fold. 2228 */ 2229 a = load_unaligned_zeropad(name); 2230 b = a ^ REPEAT_BYTE('/'); 2231 if (has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)) { 2232 adata = prep_zero_mask(a, adata, &constants); 2233 bdata = prep_zero_mask(b, bdata, &constants); 2234 mask = create_zero_mask(adata | bdata); 2235 a &= zero_bytemask(mask); 2236 *lastword = a; 2237 len = find_zero(mask); 2238 nd->last.hash = fold_hash(a, y); 2239 nd->last.len = len; 2240 return name + len; 2241 } 2242 2243 len = 0; 2244 x = 0; 2245 do { 2246 HASH_MIX(x, y, a); 2247 len += sizeof(unsigned long); 2248 a = load_unaligned_zeropad(name+len); 2249 b = a ^ REPEAT_BYTE('/'); 2250 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 2251 2252 adata = prep_zero_mask(a, adata, &constants); 2253 bdata = prep_zero_mask(b, bdata, &constants); 2254 mask = create_zero_mask(adata | bdata); 2255 a &= zero_bytemask(mask); 2256 x ^= a; 2257 len += find_zero(mask); 2258 *lastword = 0; // Multi-word components cannot be DOT or DOTDOT 2259 2260 nd->last.hash = fold_hash(x, y); 2261 nd->last.len = len; 2262 return name + len; 2263 } 2264 2265 /* 2266 * Note that the 'last' word is always zero-masked, but 2267 * was loaded as a possibly big-endian word. 2268 */ 2269 #ifdef __BIG_ENDIAN 2270 #define LAST_WORD_IS_DOT (0x2eul << (BITS_PER_LONG-8)) 2271 #define LAST_WORD_IS_DOTDOT (0x2e2eul << (BITS_PER_LONG-16)) 2272 #endif 2273 2274 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ 2275 2276 /* Return the hash of a string of known length */ 2277 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 2278 { 2279 unsigned long hash = init_name_hash(salt); 2280 while (len--) 2281 hash = partial_name_hash((unsigned char)*name++, hash); 2282 return end_name_hash(hash); 2283 } 2284 EXPORT_SYMBOL(full_name_hash); 2285 2286 /* Return the "hash_len" (hash and length) of a null-terminated string */ 2287 u64 hashlen_string(const void *salt, const char *name) 2288 { 2289 unsigned long hash = init_name_hash(salt); 2290 unsigned long len = 0, c; 2291 2292 c = (unsigned char)*name; 2293 while (c) { 2294 len++; 2295 hash = partial_name_hash(c, hash); 2296 c = (unsigned char)name[len]; 2297 } 2298 return hashlen_create(end_name_hash(hash), len); 2299 } 2300 EXPORT_SYMBOL(hashlen_string); 2301 2302 /* 2303 * We know there's a real path component here of at least 2304 * one character. 2305 */ 2306 static inline const char *hash_name(struct nameidata *nd, const char *name, unsigned long *lastword) 2307 { 2308 unsigned long hash = init_name_hash(nd->path.dentry); 2309 unsigned long len = 0, c, last = 0; 2310 2311 c = (unsigned char)*name; 2312 do { 2313 last = (last << 8) + c; 2314 len++; 2315 hash = partial_name_hash(c, hash); 2316 c = (unsigned char)name[len]; 2317 } while (c && c != '/'); 2318 2319 // This is reliable for DOT or DOTDOT, since the component 2320 // cannot contain NUL characters - top bits being zero means 2321 // we cannot have had any other pathnames. 2322 *lastword = last; 2323 nd->last.hash = end_name_hash(hash); 2324 nd->last.len = len; 2325 return name + len; 2326 } 2327 2328 #endif 2329 2330 #ifndef LAST_WORD_IS_DOT 2331 #define LAST_WORD_IS_DOT 0x2e 2332 #define LAST_WORD_IS_DOTDOT 0x2e2e 2333 #endif 2334 2335 /* 2336 * Name resolution. 2337 * This is the basic name resolution function, turning a pathname into 2338 * the final dentry. We expect 'base' to be positive and a directory. 2339 * 2340 * Returns 0 and nd will have valid dentry and mnt on success. 2341 * Returns error and drops reference to input namei data on failure. 2342 */ 2343 static int link_path_walk(const char *name, struct nameidata *nd) 2344 { 2345 int depth = 0; // depth <= nd->depth 2346 int err; 2347 2348 nd->last_type = LAST_ROOT; 2349 nd->flags |= LOOKUP_PARENT; 2350 if (IS_ERR(name)) 2351 return PTR_ERR(name); 2352 while (*name=='/') 2353 name++; 2354 if (!*name) { 2355 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy 2356 return 0; 2357 } 2358 2359 /* At this point we know we have a real path component. */ 2360 for(;;) { 2361 struct mnt_idmap *idmap; 2362 const char *link; 2363 unsigned long lastword; 2364 2365 idmap = mnt_idmap(nd->path.mnt); 2366 err = may_lookup(idmap, nd); 2367 if (err) 2368 return err; 2369 2370 nd->last.name = name; 2371 name = hash_name(nd, name, &lastword); 2372 2373 switch(lastword) { 2374 case LAST_WORD_IS_DOTDOT: 2375 nd->last_type = LAST_DOTDOT; 2376 nd->state |= ND_JUMPED; 2377 break; 2378 2379 case LAST_WORD_IS_DOT: 2380 nd->last_type = LAST_DOT; 2381 break; 2382 2383 default: 2384 nd->last_type = LAST_NORM; 2385 nd->state &= ~ND_JUMPED; 2386 2387 struct dentry *parent = nd->path.dentry; 2388 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 2389 err = parent->d_op->d_hash(parent, &nd->last); 2390 if (err < 0) 2391 return err; 2392 } 2393 } 2394 2395 if (!*name) 2396 goto OK; 2397 /* 2398 * If it wasn't NUL, we know it was '/'. Skip that 2399 * slash, and continue until no more slashes. 2400 */ 2401 do { 2402 name++; 2403 } while (unlikely(*name == '/')); 2404 if (unlikely(!*name)) { 2405 OK: 2406 /* pathname or trailing symlink, done */ 2407 if (!depth) { 2408 nd->dir_vfsuid = i_uid_into_vfsuid(idmap, nd->inode); 2409 nd->dir_mode = nd->inode->i_mode; 2410 nd->flags &= ~LOOKUP_PARENT; 2411 return 0; 2412 } 2413 /* last component of nested symlink */ 2414 name = nd->stack[--depth].name; 2415 link = walk_component(nd, 0); 2416 } else { 2417 /* not the last component */ 2418 link = walk_component(nd, WALK_MORE); 2419 } 2420 if (unlikely(link)) { 2421 if (IS_ERR(link)) 2422 return PTR_ERR(link); 2423 /* a symlink to follow */ 2424 nd->stack[depth++].name = name; 2425 name = link; 2426 continue; 2427 } 2428 if (unlikely(!d_can_lookup(nd->path.dentry))) { 2429 if (nd->flags & LOOKUP_RCU) { 2430 if (!try_to_unlazy(nd)) 2431 return -ECHILD; 2432 } 2433 return -ENOTDIR; 2434 } 2435 } 2436 } 2437 2438 /* must be paired with terminate_walk() */ 2439 static const char *path_init(struct nameidata *nd, unsigned flags) 2440 { 2441 int error; 2442 const char *s = nd->name->name; 2443 2444 /* LOOKUP_CACHED requires RCU, ask caller to retry */ 2445 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) 2446 return ERR_PTR(-EAGAIN); 2447 2448 if (!*s) 2449 flags &= ~LOOKUP_RCU; 2450 if (flags & LOOKUP_RCU) 2451 rcu_read_lock(); 2452 else 2453 nd->seq = nd->next_seq = 0; 2454 2455 nd->flags = flags; 2456 nd->state |= ND_JUMPED; 2457 2458 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); 2459 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); 2460 smp_rmb(); 2461 2462 if (nd->state & ND_ROOT_PRESET) { 2463 struct dentry *root = nd->root.dentry; 2464 struct inode *inode = root->d_inode; 2465 if (*s && unlikely(!d_can_lookup(root))) 2466 return ERR_PTR(-ENOTDIR); 2467 nd->path = nd->root; 2468 nd->inode = inode; 2469 if (flags & LOOKUP_RCU) { 2470 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 2471 nd->root_seq = nd->seq; 2472 } else { 2473 path_get(&nd->path); 2474 } 2475 return s; 2476 } 2477 2478 nd->root.mnt = NULL; 2479 2480 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ 2481 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { 2482 error = nd_jump_root(nd); 2483 if (unlikely(error)) 2484 return ERR_PTR(error); 2485 return s; 2486 } 2487 2488 /* Relative pathname -- get the starting-point it is relative to. */ 2489 if (nd->dfd == AT_FDCWD) { 2490 if (flags & LOOKUP_RCU) { 2491 struct fs_struct *fs = current->fs; 2492 unsigned seq; 2493 2494 do { 2495 seq = read_seqcount_begin(&fs->seq); 2496 nd->path = fs->pwd; 2497 nd->inode = nd->path.dentry->d_inode; 2498 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 2499 } while (read_seqcount_retry(&fs->seq, seq)); 2500 } else { 2501 get_fs_pwd(current->fs, &nd->path); 2502 nd->inode = nd->path.dentry->d_inode; 2503 } 2504 } else { 2505 /* Caller must check execute permissions on the starting path component */ 2506 CLASS(fd_raw, f)(nd->dfd); 2507 struct dentry *dentry; 2508 2509 if (fd_empty(f)) 2510 return ERR_PTR(-EBADF); 2511 2512 if (flags & LOOKUP_LINKAT_EMPTY) { 2513 if (fd_file(f)->f_cred != current_cred() && 2514 !ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH)) 2515 return ERR_PTR(-ENOENT); 2516 } 2517 2518 dentry = fd_file(f)->f_path.dentry; 2519 2520 if (*s && unlikely(!d_can_lookup(dentry))) 2521 return ERR_PTR(-ENOTDIR); 2522 2523 nd->path = fd_file(f)->f_path; 2524 if (flags & LOOKUP_RCU) { 2525 nd->inode = nd->path.dentry->d_inode; 2526 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 2527 } else { 2528 path_get(&nd->path); 2529 nd->inode = nd->path.dentry->d_inode; 2530 } 2531 } 2532 2533 /* For scoped-lookups we need to set the root to the dirfd as well. */ 2534 if (flags & LOOKUP_IS_SCOPED) { 2535 nd->root = nd->path; 2536 if (flags & LOOKUP_RCU) { 2537 nd->root_seq = nd->seq; 2538 } else { 2539 path_get(&nd->root); 2540 nd->state |= ND_ROOT_GRABBED; 2541 } 2542 } 2543 return s; 2544 } 2545 2546 static inline const char *lookup_last(struct nameidata *nd) 2547 { 2548 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 2549 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2550 2551 return walk_component(nd, WALK_TRAILING); 2552 } 2553 2554 static int handle_lookup_down(struct nameidata *nd) 2555 { 2556 if (!(nd->flags & LOOKUP_RCU)) 2557 dget(nd->path.dentry); 2558 nd->next_seq = nd->seq; 2559 return PTR_ERR(step_into(nd, WALK_NOFOLLOW, nd->path.dentry)); 2560 } 2561 2562 /* Returns 0 and nd will be valid on success; Returns error, otherwise. */ 2563 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) 2564 { 2565 const char *s = path_init(nd, flags); 2566 int err; 2567 2568 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { 2569 err = handle_lookup_down(nd); 2570 if (unlikely(err < 0)) 2571 s = ERR_PTR(err); 2572 } 2573 2574 while (!(err = link_path_walk(s, nd)) && 2575 (s = lookup_last(nd)) != NULL) 2576 ; 2577 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { 2578 err = handle_lookup_down(nd); 2579 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... 2580 } 2581 if (!err) 2582 err = complete_walk(nd); 2583 2584 if (!err && nd->flags & LOOKUP_DIRECTORY) 2585 if (!d_can_lookup(nd->path.dentry)) 2586 err = -ENOTDIR; 2587 if (!err) { 2588 *path = nd->path; 2589 nd->path.mnt = NULL; 2590 nd->path.dentry = NULL; 2591 } 2592 terminate_walk(nd); 2593 return err; 2594 } 2595 2596 int filename_lookup(int dfd, struct filename *name, unsigned flags, 2597 struct path *path, struct path *root) 2598 { 2599 int retval; 2600 struct nameidata nd; 2601 if (IS_ERR(name)) 2602 return PTR_ERR(name); 2603 set_nameidata(&nd, dfd, name, root); 2604 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); 2605 if (unlikely(retval == -ECHILD)) 2606 retval = path_lookupat(&nd, flags, path); 2607 if (unlikely(retval == -ESTALE)) 2608 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); 2609 2610 if (likely(!retval)) 2611 audit_inode(name, path->dentry, 2612 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); 2613 restore_nameidata(); 2614 return retval; 2615 } 2616 2617 /* Returns 0 and nd will be valid on success; Returns error, otherwise. */ 2618 static int path_parentat(struct nameidata *nd, unsigned flags, 2619 struct path *parent) 2620 { 2621 const char *s = path_init(nd, flags); 2622 int err = link_path_walk(s, nd); 2623 if (!err) 2624 err = complete_walk(nd); 2625 if (!err) { 2626 *parent = nd->path; 2627 nd->path.mnt = NULL; 2628 nd->path.dentry = NULL; 2629 } 2630 terminate_walk(nd); 2631 return err; 2632 } 2633 2634 /* Note: this does not consume "name" */ 2635 static int __filename_parentat(int dfd, struct filename *name, 2636 unsigned int flags, struct path *parent, 2637 struct qstr *last, int *type, 2638 const struct path *root) 2639 { 2640 int retval; 2641 struct nameidata nd; 2642 2643 if (IS_ERR(name)) 2644 return PTR_ERR(name); 2645 set_nameidata(&nd, dfd, name, root); 2646 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); 2647 if (unlikely(retval == -ECHILD)) 2648 retval = path_parentat(&nd, flags, parent); 2649 if (unlikely(retval == -ESTALE)) 2650 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); 2651 if (likely(!retval)) { 2652 *last = nd.last; 2653 *type = nd.last_type; 2654 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); 2655 } 2656 restore_nameidata(); 2657 return retval; 2658 } 2659 2660 static int filename_parentat(int dfd, struct filename *name, 2661 unsigned int flags, struct path *parent, 2662 struct qstr *last, int *type) 2663 { 2664 return __filename_parentat(dfd, name, flags, parent, last, type, NULL); 2665 } 2666 2667 /* does lookup, returns the object with parent locked */ 2668 static struct dentry *__kern_path_locked(int dfd, struct filename *name, struct path *path) 2669 { 2670 struct dentry *d; 2671 struct qstr last; 2672 int type, error; 2673 2674 error = filename_parentat(dfd, name, 0, path, &last, &type); 2675 if (error) 2676 return ERR_PTR(error); 2677 if (unlikely(type != LAST_NORM)) { 2678 path_put(path); 2679 return ERR_PTR(-EINVAL); 2680 } 2681 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 2682 d = lookup_one_qstr_excl(&last, path->dentry, 0); 2683 if (IS_ERR(d)) { 2684 inode_unlock(path->dentry->d_inode); 2685 path_put(path); 2686 } 2687 return d; 2688 } 2689 2690 struct dentry *kern_path_locked(const char *name, struct path *path) 2691 { 2692 struct filename *filename = getname_kernel(name); 2693 struct dentry *res = __kern_path_locked(AT_FDCWD, filename, path); 2694 2695 putname(filename); 2696 return res; 2697 } 2698 2699 struct dentry *user_path_locked_at(int dfd, const char __user *name, struct path *path) 2700 { 2701 struct filename *filename = getname(name); 2702 struct dentry *res = __kern_path_locked(dfd, filename, path); 2703 2704 putname(filename); 2705 return res; 2706 } 2707 EXPORT_SYMBOL(user_path_locked_at); 2708 2709 int kern_path(const char *name, unsigned int flags, struct path *path) 2710 { 2711 struct filename *filename = getname_kernel(name); 2712 int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL); 2713 2714 putname(filename); 2715 return ret; 2716 2717 } 2718 EXPORT_SYMBOL(kern_path); 2719 2720 /** 2721 * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair 2722 * @filename: filename structure 2723 * @flags: lookup flags 2724 * @parent: pointer to struct path to fill 2725 * @last: last component 2726 * @type: type of the last component 2727 * @root: pointer to struct path of the base directory 2728 */ 2729 int vfs_path_parent_lookup(struct filename *filename, unsigned int flags, 2730 struct path *parent, struct qstr *last, int *type, 2731 const struct path *root) 2732 { 2733 return __filename_parentat(AT_FDCWD, filename, flags, parent, last, 2734 type, root); 2735 } 2736 EXPORT_SYMBOL(vfs_path_parent_lookup); 2737 2738 /** 2739 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 2740 * @dentry: pointer to dentry of the base directory 2741 * @mnt: pointer to vfs mount of the base directory 2742 * @name: pointer to file name 2743 * @flags: lookup flags 2744 * @path: pointer to struct path to fill 2745 */ 2746 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 2747 const char *name, unsigned int flags, 2748 struct path *path) 2749 { 2750 struct filename *filename; 2751 struct path root = {.mnt = mnt, .dentry = dentry}; 2752 int ret; 2753 2754 filename = getname_kernel(name); 2755 /* the first argument of filename_lookup() is ignored with root */ 2756 ret = filename_lookup(AT_FDCWD, filename, flags, path, &root); 2757 putname(filename); 2758 return ret; 2759 } 2760 EXPORT_SYMBOL(vfs_path_lookup); 2761 2762 static int lookup_one_common(struct mnt_idmap *idmap, 2763 const char *name, struct dentry *base, int len, 2764 struct qstr *this) 2765 { 2766 this->name = name; 2767 this->len = len; 2768 this->hash = full_name_hash(base, name, len); 2769 if (!len) 2770 return -EACCES; 2771 2772 if (is_dot_dotdot(name, len)) 2773 return -EACCES; 2774 2775 while (len--) { 2776 unsigned int c = *(const unsigned char *)name++; 2777 if (c == '/' || c == '\0') 2778 return -EACCES; 2779 } 2780 /* 2781 * See if the low-level filesystem might want 2782 * to use its own hash.. 2783 */ 2784 if (base->d_flags & DCACHE_OP_HASH) { 2785 int err = base->d_op->d_hash(base, this); 2786 if (err < 0) 2787 return err; 2788 } 2789 2790 return inode_permission(idmap, base->d_inode, MAY_EXEC); 2791 } 2792 2793 /** 2794 * try_lookup_one_len - filesystem helper to lookup single pathname component 2795 * @name: pathname component to lookup 2796 * @base: base directory to lookup from 2797 * @len: maximum length @len should be interpreted to 2798 * 2799 * Look up a dentry by name in the dcache, returning NULL if it does not 2800 * currently exist. The function does not try to create a dentry. 2801 * 2802 * Note that this routine is purely a helper for filesystem usage and should 2803 * not be called by generic code. 2804 * 2805 * The caller must hold base->i_mutex. 2806 */ 2807 struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) 2808 { 2809 struct qstr this; 2810 int err; 2811 2812 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 2813 2814 err = lookup_one_common(&nop_mnt_idmap, name, base, len, &this); 2815 if (err) 2816 return ERR_PTR(err); 2817 2818 return lookup_dcache(&this, base, 0); 2819 } 2820 EXPORT_SYMBOL(try_lookup_one_len); 2821 2822 /** 2823 * lookup_one_len - filesystem helper to lookup single pathname component 2824 * @name: pathname component to lookup 2825 * @base: base directory to lookup from 2826 * @len: maximum length @len should be interpreted to 2827 * 2828 * Note that this routine is purely a helper for filesystem usage and should 2829 * not be called by generic code. 2830 * 2831 * The caller must hold base->i_mutex. 2832 */ 2833 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2834 { 2835 struct dentry *dentry; 2836 struct qstr this; 2837 int err; 2838 2839 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 2840 2841 err = lookup_one_common(&nop_mnt_idmap, name, base, len, &this); 2842 if (err) 2843 return ERR_PTR(err); 2844 2845 dentry = lookup_dcache(&this, base, 0); 2846 return dentry ? dentry : __lookup_slow(&this, base, 0); 2847 } 2848 EXPORT_SYMBOL(lookup_one_len); 2849 2850 /** 2851 * lookup_one - filesystem helper to lookup single pathname component 2852 * @idmap: idmap of the mount the lookup is performed from 2853 * @name: pathname component to lookup 2854 * @base: base directory to lookup from 2855 * @len: maximum length @len should be interpreted to 2856 * 2857 * Note that this routine is purely a helper for filesystem usage and should 2858 * not be called by generic code. 2859 * 2860 * The caller must hold base->i_mutex. 2861 */ 2862 struct dentry *lookup_one(struct mnt_idmap *idmap, const char *name, 2863 struct dentry *base, int len) 2864 { 2865 struct dentry *dentry; 2866 struct qstr this; 2867 int err; 2868 2869 WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 2870 2871 err = lookup_one_common(idmap, name, base, len, &this); 2872 if (err) 2873 return ERR_PTR(err); 2874 2875 dentry = lookup_dcache(&this, base, 0); 2876 return dentry ? dentry : __lookup_slow(&this, base, 0); 2877 } 2878 EXPORT_SYMBOL(lookup_one); 2879 2880 /** 2881 * lookup_one_unlocked - filesystem helper to lookup single pathname component 2882 * @idmap: idmap of the mount the lookup is performed from 2883 * @name: pathname component to lookup 2884 * @base: base directory to lookup from 2885 * @len: maximum length @len should be interpreted to 2886 * 2887 * Note that this routine is purely a helper for filesystem usage and should 2888 * not be called by generic code. 2889 * 2890 * Unlike lookup_one_len, it should be called without the parent 2891 * i_mutex held, and will take the i_mutex itself if necessary. 2892 */ 2893 struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap, 2894 const char *name, struct dentry *base, 2895 int len) 2896 { 2897 struct qstr this; 2898 int err; 2899 struct dentry *ret; 2900 2901 err = lookup_one_common(idmap, name, base, len, &this); 2902 if (err) 2903 return ERR_PTR(err); 2904 2905 ret = lookup_dcache(&this, base, 0); 2906 if (!ret) 2907 ret = lookup_slow(&this, base, 0); 2908 return ret; 2909 } 2910 EXPORT_SYMBOL(lookup_one_unlocked); 2911 2912 /** 2913 * lookup_one_positive_unlocked - filesystem helper to lookup single 2914 * pathname component 2915 * @idmap: idmap of the mount the lookup is performed from 2916 * @name: pathname component to lookup 2917 * @base: base directory to lookup from 2918 * @len: maximum length @len should be interpreted to 2919 * 2920 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns 2921 * known positive or ERR_PTR(). This is what most of the users want. 2922 * 2923 * Note that pinned negative with unlocked parent _can_ become positive at any 2924 * time, so callers of lookup_one_unlocked() need to be very careful; pinned 2925 * positives have >d_inode stable, so this one avoids such problems. 2926 * 2927 * Note that this routine is purely a helper for filesystem usage and should 2928 * not be called by generic code. 2929 * 2930 * The helper should be called without i_mutex held. 2931 */ 2932 struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap, 2933 const char *name, 2934 struct dentry *base, int len) 2935 { 2936 struct dentry *ret = lookup_one_unlocked(idmap, name, base, len); 2937 2938 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { 2939 dput(ret); 2940 ret = ERR_PTR(-ENOENT); 2941 } 2942 return ret; 2943 } 2944 EXPORT_SYMBOL(lookup_one_positive_unlocked); 2945 2946 /** 2947 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component 2948 * @name: pathname component to lookup 2949 * @base: base directory to lookup from 2950 * @len: maximum length @len should be interpreted to 2951 * 2952 * Note that this routine is purely a helper for filesystem usage and should 2953 * not be called by generic code. 2954 * 2955 * Unlike lookup_one_len, it should be called without the parent 2956 * i_mutex held, and will take the i_mutex itself if necessary. 2957 */ 2958 struct dentry *lookup_one_len_unlocked(const char *name, 2959 struct dentry *base, int len) 2960 { 2961 return lookup_one_unlocked(&nop_mnt_idmap, name, base, len); 2962 } 2963 EXPORT_SYMBOL(lookup_one_len_unlocked); 2964 2965 /* 2966 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) 2967 * on negatives. Returns known positive or ERR_PTR(); that's what 2968 * most of the users want. Note that pinned negative with unlocked parent 2969 * _can_ become positive at any time, so callers of lookup_one_len_unlocked() 2970 * need to be very careful; pinned positives have ->d_inode stable, so 2971 * this one avoids such problems. 2972 */ 2973 struct dentry *lookup_positive_unlocked(const char *name, 2974 struct dentry *base, int len) 2975 { 2976 return lookup_one_positive_unlocked(&nop_mnt_idmap, name, base, len); 2977 } 2978 EXPORT_SYMBOL(lookup_positive_unlocked); 2979 2980 #ifdef CONFIG_UNIX98_PTYS 2981 int path_pts(struct path *path) 2982 { 2983 /* Find something mounted on "pts" in the same directory as 2984 * the input path. 2985 */ 2986 struct dentry *parent = dget_parent(path->dentry); 2987 struct dentry *child; 2988 struct qstr this = QSTR_INIT("pts", 3); 2989 2990 if (unlikely(!path_connected(path->mnt, parent))) { 2991 dput(parent); 2992 return -ENOENT; 2993 } 2994 dput(path->dentry); 2995 path->dentry = parent; 2996 child = d_hash_and_lookup(parent, &this); 2997 if (IS_ERR_OR_NULL(child)) 2998 return -ENOENT; 2999 3000 path->dentry = child; 3001 dput(parent); 3002 follow_down(path, 0); 3003 return 0; 3004 } 3005 #endif 3006 3007 int user_path_at(int dfd, const char __user *name, unsigned flags, 3008 struct path *path) 3009 { 3010 struct filename *filename = getname_flags(name, flags); 3011 int ret = filename_lookup(dfd, filename, flags, path, NULL); 3012 3013 putname(filename); 3014 return ret; 3015 } 3016 EXPORT_SYMBOL(user_path_at); 3017 3018 int __check_sticky(struct mnt_idmap *idmap, struct inode *dir, 3019 struct inode *inode) 3020 { 3021 kuid_t fsuid = current_fsuid(); 3022 3023 if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), fsuid)) 3024 return 0; 3025 if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, dir), fsuid)) 3026 return 0; 3027 return !capable_wrt_inode_uidgid(idmap, inode, CAP_FOWNER); 3028 } 3029 EXPORT_SYMBOL(__check_sticky); 3030 3031 /* 3032 * Check whether we can remove a link victim from directory dir, check 3033 * whether the type of victim is right. 3034 * 1. We can't do it if dir is read-only (done in permission()) 3035 * 2. We should have write and exec permissions on dir 3036 * 3. We can't remove anything from append-only dir 3037 * 4. We can't do anything with immutable dir (done in permission()) 3038 * 5. If the sticky bit on dir is set we should either 3039 * a. be owner of dir, or 3040 * b. be owner of victim, or 3041 * c. have CAP_FOWNER capability 3042 * 6. If the victim is append-only or immutable we can't do antyhing with 3043 * links pointing to it. 3044 * 7. If the victim has an unknown uid or gid we can't change the inode. 3045 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. 3046 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. 3047 * 10. We can't remove a root or mountpoint. 3048 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by 3049 * nfs_async_unlink(). 3050 */ 3051 static int may_delete(struct mnt_idmap *idmap, struct inode *dir, 3052 struct dentry *victim, bool isdir) 3053 { 3054 struct inode *inode = d_backing_inode(victim); 3055 int error; 3056 3057 if (d_is_negative(victim)) 3058 return -ENOENT; 3059 BUG_ON(!inode); 3060 3061 BUG_ON(victim->d_parent->d_inode != dir); 3062 3063 /* Inode writeback is not safe when the uid or gid are invalid. */ 3064 if (!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) || 3065 !vfsgid_valid(i_gid_into_vfsgid(idmap, inode))) 3066 return -EOVERFLOW; 3067 3068 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 3069 3070 error = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); 3071 if (error) 3072 return error; 3073 if (IS_APPEND(dir)) 3074 return -EPERM; 3075 3076 if (check_sticky(idmap, dir, inode) || IS_APPEND(inode) || 3077 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || 3078 HAS_UNMAPPED_ID(idmap, inode)) 3079 return -EPERM; 3080 if (isdir) { 3081 if (!d_is_dir(victim)) 3082 return -ENOTDIR; 3083 if (IS_ROOT(victim)) 3084 return -EBUSY; 3085 } else if (d_is_dir(victim)) 3086 return -EISDIR; 3087 if (IS_DEADDIR(dir)) 3088 return -ENOENT; 3089 if (victim->d_flags & DCACHE_NFSFS_RENAMED) 3090 return -EBUSY; 3091 return 0; 3092 } 3093 3094 /* Check whether we can create an object with dentry child in directory 3095 * dir. 3096 * 1. We can't do it if child already exists (open has special treatment for 3097 * this case, but since we are inlined it's OK) 3098 * 2. We can't do it if dir is read-only (done in permission()) 3099 * 3. We can't do it if the fs can't represent the fsuid or fsgid. 3100 * 4. We should have write and exec permissions on dir 3101 * 5. We can't do it if dir is immutable (done in permission()) 3102 */ 3103 static inline int may_create(struct mnt_idmap *idmap, 3104 struct inode *dir, struct dentry *child) 3105 { 3106 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); 3107 if (child->d_inode) 3108 return -EEXIST; 3109 if (IS_DEADDIR(dir)) 3110 return -ENOENT; 3111 if (!fsuidgid_has_mapping(dir->i_sb, idmap)) 3112 return -EOVERFLOW; 3113 3114 return inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); 3115 } 3116 3117 // p1 != p2, both are on the same filesystem, ->s_vfs_rename_mutex is held 3118 static struct dentry *lock_two_directories(struct dentry *p1, struct dentry *p2) 3119 { 3120 struct dentry *p = p1, *q = p2, *r; 3121 3122 while ((r = p->d_parent) != p2 && r != p) 3123 p = r; 3124 if (r == p2) { 3125 // p is a child of p2 and an ancestor of p1 or p1 itself 3126 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); 3127 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT2); 3128 return p; 3129 } 3130 // p is the root of connected component that contains p1 3131 // p2 does not occur on the path from p to p1 3132 while ((r = q->d_parent) != p1 && r != p && r != q) 3133 q = r; 3134 if (r == p1) { 3135 // q is a child of p1 and an ancestor of p2 or p2 itself 3136 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 3137 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); 3138 return q; 3139 } else if (likely(r == p)) { 3140 // both p2 and p1 are descendents of p 3141 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 3142 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); 3143 return NULL; 3144 } else { // no common ancestor at the time we'd been called 3145 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); 3146 return ERR_PTR(-EXDEV); 3147 } 3148 } 3149 3150 /* 3151 * p1 and p2 should be directories on the same fs. 3152 */ 3153 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 3154 { 3155 if (p1 == p2) { 3156 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 3157 return NULL; 3158 } 3159 3160 mutex_lock(&p1->d_sb->s_vfs_rename_mutex); 3161 return lock_two_directories(p1, p2); 3162 } 3163 EXPORT_SYMBOL(lock_rename); 3164 3165 /* 3166 * c1 and p2 should be on the same fs. 3167 */ 3168 struct dentry *lock_rename_child(struct dentry *c1, struct dentry *p2) 3169 { 3170 if (READ_ONCE(c1->d_parent) == p2) { 3171 /* 3172 * hopefully won't need to touch ->s_vfs_rename_mutex at all. 3173 */ 3174 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); 3175 /* 3176 * now that p2 is locked, nobody can move in or out of it, 3177 * so the test below is safe. 3178 */ 3179 if (likely(c1->d_parent == p2)) 3180 return NULL; 3181 3182 /* 3183 * c1 got moved out of p2 while we'd been taking locks; 3184 * unlock and fall back to slow case. 3185 */ 3186 inode_unlock(p2->d_inode); 3187 } 3188 3189 mutex_lock(&c1->d_sb->s_vfs_rename_mutex); 3190 /* 3191 * nobody can move out of any directories on this fs. 3192 */ 3193 if (likely(c1->d_parent != p2)) 3194 return lock_two_directories(c1->d_parent, p2); 3195 3196 /* 3197 * c1 got moved into p2 while we were taking locks; 3198 * we need p2 locked and ->s_vfs_rename_mutex unlocked, 3199 * for consistency with lock_rename(). 3200 */ 3201 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); 3202 mutex_unlock(&c1->d_sb->s_vfs_rename_mutex); 3203 return NULL; 3204 } 3205 EXPORT_SYMBOL(lock_rename_child); 3206 3207 void unlock_rename(struct dentry *p1, struct dentry *p2) 3208 { 3209 inode_unlock(p1->d_inode); 3210 if (p1 != p2) { 3211 inode_unlock(p2->d_inode); 3212 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); 3213 } 3214 } 3215 EXPORT_SYMBOL(unlock_rename); 3216 3217 /** 3218 * vfs_prepare_mode - prepare the mode to be used for a new inode 3219 * @idmap: idmap of the mount the inode was found from 3220 * @dir: parent directory of the new inode 3221 * @mode: mode of the new inode 3222 * @mask_perms: allowed permission by the vfs 3223 * @type: type of file to be created 3224 * 3225 * This helper consolidates and enforces vfs restrictions on the @mode of a new 3226 * object to be created. 3227 * 3228 * Umask stripping depends on whether the filesystem supports POSIX ACLs (see 3229 * the kernel documentation for mode_strip_umask()). Moving umask stripping 3230 * after setgid stripping allows the same ordering for both non-POSIX ACL and 3231 * POSIX ACL supporting filesystems. 3232 * 3233 * Note that it's currently valid for @type to be 0 if a directory is created. 3234 * Filesystems raise that flag individually and we need to check whether each 3235 * filesystem can deal with receiving S_IFDIR from the vfs before we enforce a 3236 * non-zero type. 3237 * 3238 * Returns: mode to be passed to the filesystem 3239 */ 3240 static inline umode_t vfs_prepare_mode(struct mnt_idmap *idmap, 3241 const struct inode *dir, umode_t mode, 3242 umode_t mask_perms, umode_t type) 3243 { 3244 mode = mode_strip_sgid(idmap, dir, mode); 3245 mode = mode_strip_umask(dir, mode); 3246 3247 /* 3248 * Apply the vfs mandated allowed permission mask and set the type of 3249 * file to be created before we call into the filesystem. 3250 */ 3251 mode &= (mask_perms & ~S_IFMT); 3252 mode |= (type & S_IFMT); 3253 3254 return mode; 3255 } 3256 3257 /** 3258 * vfs_create - create new file 3259 * @idmap: idmap of the mount the inode was found from 3260 * @dir: inode of the parent directory 3261 * @dentry: dentry of the child file 3262 * @mode: mode of the child file 3263 * @want_excl: whether the file must not yet exist 3264 * 3265 * Create a new file. 3266 * 3267 * If the inode has been found through an idmapped mount the idmap of 3268 * the vfsmount must be passed through @idmap. This function will then take 3269 * care to map the inode according to @idmap before checking permissions. 3270 * On non-idmapped mounts or if permission checking is to be performed on the 3271 * raw inode simply pass @nop_mnt_idmap. 3272 */ 3273 int vfs_create(struct mnt_idmap *idmap, struct inode *dir, 3274 struct dentry *dentry, umode_t mode, bool want_excl) 3275 { 3276 int error; 3277 3278 error = may_create(idmap, dir, dentry); 3279 if (error) 3280 return error; 3281 3282 if (!dir->i_op->create) 3283 return -EACCES; /* shouldn't it be ENOSYS? */ 3284 3285 mode = vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG); 3286 error = security_inode_create(dir, dentry, mode); 3287 if (error) 3288 return error; 3289 error = dir->i_op->create(idmap, dir, dentry, mode, want_excl); 3290 if (!error) 3291 fsnotify_create(dir, dentry); 3292 return error; 3293 } 3294 EXPORT_SYMBOL(vfs_create); 3295 3296 int vfs_mkobj(struct dentry *dentry, umode_t mode, 3297 int (*f)(struct dentry *, umode_t, void *), 3298 void *arg) 3299 { 3300 struct inode *dir = dentry->d_parent->d_inode; 3301 int error = may_create(&nop_mnt_idmap, dir, dentry); 3302 if (error) 3303 return error; 3304 3305 mode &= S_IALLUGO; 3306 mode |= S_IFREG; 3307 error = security_inode_create(dir, dentry, mode); 3308 if (error) 3309 return error; 3310 error = f(dentry, mode, arg); 3311 if (!error) 3312 fsnotify_create(dir, dentry); 3313 return error; 3314 } 3315 EXPORT_SYMBOL(vfs_mkobj); 3316 3317 bool may_open_dev(const struct path *path) 3318 { 3319 return !(path->mnt->mnt_flags & MNT_NODEV) && 3320 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); 3321 } 3322 3323 static int may_open(struct mnt_idmap *idmap, const struct path *path, 3324 int acc_mode, int flag) 3325 { 3326 struct dentry *dentry = path->dentry; 3327 struct inode *inode = dentry->d_inode; 3328 int error; 3329 3330 if (!inode) 3331 return -ENOENT; 3332 3333 switch (inode->i_mode & S_IFMT) { 3334 case S_IFLNK: 3335 return -ELOOP; 3336 case S_IFDIR: 3337 if (acc_mode & MAY_WRITE) 3338 return -EISDIR; 3339 if (acc_mode & MAY_EXEC) 3340 return -EACCES; 3341 break; 3342 case S_IFBLK: 3343 case S_IFCHR: 3344 if (!may_open_dev(path)) 3345 return -EACCES; 3346 fallthrough; 3347 case S_IFIFO: 3348 case S_IFSOCK: 3349 if (acc_mode & MAY_EXEC) 3350 return -EACCES; 3351 flag &= ~O_TRUNC; 3352 break; 3353 case S_IFREG: 3354 if ((acc_mode & MAY_EXEC) && path_noexec(path)) 3355 return -EACCES; 3356 break; 3357 } 3358 3359 error = inode_permission(idmap, inode, MAY_OPEN | acc_mode); 3360 if (error) 3361 return error; 3362 3363 /* 3364 * An append-only file must be opened in append mode for writing. 3365 */ 3366 if (IS_APPEND(inode)) { 3367 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 3368 return -EPERM; 3369 if (flag & O_TRUNC) 3370 return -EPERM; 3371 } 3372 3373 /* O_NOATIME can only be set by the owner or superuser */ 3374 if (flag & O_NOATIME && !inode_owner_or_capable(idmap, inode)) 3375 return -EPERM; 3376 3377 return 0; 3378 } 3379 3380 static int handle_truncate(struct mnt_idmap *idmap, struct file *filp) 3381 { 3382 const struct path *path = &filp->f_path; 3383 struct inode *inode = path->dentry->d_inode; 3384 int error = get_write_access(inode); 3385 if (error) 3386 return error; 3387 3388 error = security_file_truncate(filp); 3389 if (!error) { 3390 error = do_truncate(idmap, path->dentry, 0, 3391 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 3392 filp); 3393 } 3394 put_write_access(inode); 3395 return error; 3396 } 3397 3398 static inline int open_to_namei_flags(int flag) 3399 { 3400 if ((flag & O_ACCMODE) == 3) 3401 flag--; 3402 return flag; 3403 } 3404 3405 static int may_o_create(struct mnt_idmap *idmap, 3406 const struct path *dir, struct dentry *dentry, 3407 umode_t mode) 3408 { 3409 int error = security_path_mknod(dir, dentry, mode, 0); 3410 if (error) 3411 return error; 3412 3413 if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap)) 3414 return -EOVERFLOW; 3415 3416 error = inode_permission(idmap, dir->dentry->d_inode, 3417 MAY_WRITE | MAY_EXEC); 3418 if (error) 3419 return error; 3420 3421 return security_inode_create(dir->dentry->d_inode, dentry, mode); 3422 } 3423 3424 /* 3425 * Attempt to atomically look up, create and open a file from a negative 3426 * dentry. 3427 * 3428 * Returns 0 if successful. The file will have been created and attached to 3429 * @file by the filesystem calling finish_open(). 3430 * 3431 * If the file was looked up only or didn't need creating, FMODE_OPENED won't 3432 * be set. The caller will need to perform the open themselves. @path will 3433 * have been updated to point to the new dentry. This may be negative. 3434 * 3435 * Returns an error code otherwise. 3436 */ 3437 static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, 3438 struct file *file, 3439 int open_flag, umode_t mode) 3440 { 3441 struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 3442 struct inode *dir = nd->path.dentry->d_inode; 3443 int error; 3444 3445 if (nd->flags & LOOKUP_DIRECTORY) 3446 open_flag |= O_DIRECTORY; 3447 3448 file->f_path.dentry = DENTRY_NOT_SET; 3449 file->f_path.mnt = nd->path.mnt; 3450 error = dir->i_op->atomic_open(dir, dentry, file, 3451 open_to_namei_flags(open_flag), mode); 3452 d_lookup_done(dentry); 3453 if (!error) { 3454 if (file->f_mode & FMODE_OPENED) { 3455 if (unlikely(dentry != file->f_path.dentry)) { 3456 dput(dentry); 3457 dentry = dget(file->f_path.dentry); 3458 } 3459 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 3460 error = -EIO; 3461 } else { 3462 if (file->f_path.dentry) { 3463 dput(dentry); 3464 dentry = file->f_path.dentry; 3465 } 3466 if (unlikely(d_is_negative(dentry))) 3467 error = -ENOENT; 3468 } 3469 } 3470 if (error) { 3471 dput(dentry); 3472 dentry = ERR_PTR(error); 3473 } 3474 return dentry; 3475 } 3476 3477 /* 3478 * Look up and maybe create and open the last component. 3479 * 3480 * Must be called with parent locked (exclusive in O_CREAT case). 3481 * 3482 * Returns 0 on success, that is, if 3483 * the file was successfully atomically created (if necessary) and opened, or 3484 * the file was not completely opened at this time, though lookups and 3485 * creations were performed. 3486 * These case are distinguished by presence of FMODE_OPENED on file->f_mode. 3487 * In the latter case dentry returned in @path might be negative if O_CREAT 3488 * hadn't been specified. 3489 * 3490 * An error code is returned on failure. 3491 */ 3492 static struct dentry *lookup_open(struct nameidata *nd, struct file *file, 3493 const struct open_flags *op, 3494 bool got_write) 3495 { 3496 struct mnt_idmap *idmap; 3497 struct dentry *dir = nd->path.dentry; 3498 struct inode *dir_inode = dir->d_inode; 3499 int open_flag = op->open_flag; 3500 struct dentry *dentry; 3501 int error, create_error = 0; 3502 umode_t mode = op->mode; 3503 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 3504 3505 if (unlikely(IS_DEADDIR(dir_inode))) 3506 return ERR_PTR(-ENOENT); 3507 3508 file->f_mode &= ~FMODE_CREATED; 3509 dentry = d_lookup(dir, &nd->last); 3510 for (;;) { 3511 if (!dentry) { 3512 dentry = d_alloc_parallel(dir, &nd->last, &wq); 3513 if (IS_ERR(dentry)) 3514 return dentry; 3515 } 3516 if (d_in_lookup(dentry)) 3517 break; 3518 3519 error = d_revalidate(dentry, nd->flags); 3520 if (likely(error > 0)) 3521 break; 3522 if (error) 3523 goto out_dput; 3524 d_invalidate(dentry); 3525 dput(dentry); 3526 dentry = NULL; 3527 } 3528 if (dentry->d_inode) { 3529 /* Cached positive dentry: will open in f_op->open */ 3530 return dentry; 3531 } 3532 3533 if (open_flag & O_CREAT) 3534 audit_inode(nd->name, dir, AUDIT_INODE_PARENT); 3535 3536 /* 3537 * Checking write permission is tricky, bacuse we don't know if we are 3538 * going to actually need it: O_CREAT opens should work as long as the 3539 * file exists. But checking existence breaks atomicity. The trick is 3540 * to check access and if not granted clear O_CREAT from the flags. 3541 * 3542 * Another problem is returing the "right" error value (e.g. for an 3543 * O_EXCL open we want to return EEXIST not EROFS). 3544 */ 3545 if (unlikely(!got_write)) 3546 open_flag &= ~O_TRUNC; 3547 idmap = mnt_idmap(nd->path.mnt); 3548 if (open_flag & O_CREAT) { 3549 if (open_flag & O_EXCL) 3550 open_flag &= ~O_TRUNC; 3551 mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode); 3552 if (likely(got_write)) 3553 create_error = may_o_create(idmap, &nd->path, 3554 dentry, mode); 3555 else 3556 create_error = -EROFS; 3557 } 3558 if (create_error) 3559 open_flag &= ~O_CREAT; 3560 if (dir_inode->i_op->atomic_open) { 3561 dentry = atomic_open(nd, dentry, file, open_flag, mode); 3562 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) 3563 dentry = ERR_PTR(create_error); 3564 return dentry; 3565 } 3566 3567 if (d_in_lookup(dentry)) { 3568 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, 3569 nd->flags); 3570 d_lookup_done(dentry); 3571 if (unlikely(res)) { 3572 if (IS_ERR(res)) { 3573 error = PTR_ERR(res); 3574 goto out_dput; 3575 } 3576 dput(dentry); 3577 dentry = res; 3578 } 3579 } 3580 3581 /* Negative dentry, just create the file */ 3582 if (!dentry->d_inode && (open_flag & O_CREAT)) { 3583 file->f_mode |= FMODE_CREATED; 3584 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); 3585 if (!dir_inode->i_op->create) { 3586 error = -EACCES; 3587 goto out_dput; 3588 } 3589 3590 error = dir_inode->i_op->create(idmap, dir_inode, dentry, 3591 mode, open_flag & O_EXCL); 3592 if (error) 3593 goto out_dput; 3594 } 3595 if (unlikely(create_error) && !dentry->d_inode) { 3596 error = create_error; 3597 goto out_dput; 3598 } 3599 return dentry; 3600 3601 out_dput: 3602 dput(dentry); 3603 return ERR_PTR(error); 3604 } 3605 3606 static inline bool trailing_slashes(struct nameidata *nd) 3607 { 3608 return (bool)nd->last.name[nd->last.len]; 3609 } 3610 3611 static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag) 3612 { 3613 struct dentry *dentry; 3614 3615 if (open_flag & O_CREAT) { 3616 if (trailing_slashes(nd)) 3617 return ERR_PTR(-EISDIR); 3618 3619 /* Don't bother on an O_EXCL create */ 3620 if (open_flag & O_EXCL) 3621 return NULL; 3622 } 3623 3624 if (trailing_slashes(nd)) 3625 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 3626 3627 dentry = lookup_fast(nd); 3628 if (IS_ERR_OR_NULL(dentry)) 3629 return dentry; 3630 3631 if (open_flag & O_CREAT) { 3632 /* Discard negative dentries. Need inode_lock to do the create */ 3633 if (!dentry->d_inode) { 3634 if (!(nd->flags & LOOKUP_RCU)) 3635 dput(dentry); 3636 dentry = NULL; 3637 } 3638 } 3639 return dentry; 3640 } 3641 3642 static const char *open_last_lookups(struct nameidata *nd, 3643 struct file *file, const struct open_flags *op) 3644 { 3645 struct dentry *dir = nd->path.dentry; 3646 int open_flag = op->open_flag; 3647 bool got_write = false; 3648 struct dentry *dentry; 3649 const char *res; 3650 3651 nd->flags |= op->intent; 3652 3653 if (nd->last_type != LAST_NORM) { 3654 if (nd->depth) 3655 put_link(nd); 3656 return handle_dots(nd, nd->last_type); 3657 } 3658 3659 /* We _can_ be in RCU mode here */ 3660 dentry = lookup_fast_for_open(nd, open_flag); 3661 if (IS_ERR(dentry)) 3662 return ERR_CAST(dentry); 3663 3664 if (likely(dentry)) 3665 goto finish_lookup; 3666 3667 if (!(open_flag & O_CREAT)) { 3668 if (WARN_ON_ONCE(nd->flags & LOOKUP_RCU)) 3669 return ERR_PTR(-ECHILD); 3670 } else { 3671 if (nd->flags & LOOKUP_RCU) { 3672 if (!try_to_unlazy(nd)) 3673 return ERR_PTR(-ECHILD); 3674 } 3675 } 3676 3677 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 3678 got_write = !mnt_want_write(nd->path.mnt); 3679 /* 3680 * do _not_ fail yet - we might not need that or fail with 3681 * a different error; let lookup_open() decide; we'll be 3682 * dropping this one anyway. 3683 */ 3684 } 3685 if (open_flag & O_CREAT) 3686 inode_lock(dir->d_inode); 3687 else 3688 inode_lock_shared(dir->d_inode); 3689 dentry = lookup_open(nd, file, op, got_write); 3690 if (!IS_ERR(dentry)) { 3691 if (file->f_mode & FMODE_CREATED) 3692 fsnotify_create(dir->d_inode, dentry); 3693 if (file->f_mode & FMODE_OPENED) 3694 fsnotify_open(file); 3695 } 3696 if (open_flag & O_CREAT) 3697 inode_unlock(dir->d_inode); 3698 else 3699 inode_unlock_shared(dir->d_inode); 3700 3701 if (got_write) 3702 mnt_drop_write(nd->path.mnt); 3703 3704 if (IS_ERR(dentry)) 3705 return ERR_CAST(dentry); 3706 3707 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { 3708 dput(nd->path.dentry); 3709 nd->path.dentry = dentry; 3710 return NULL; 3711 } 3712 3713 finish_lookup: 3714 if (nd->depth) 3715 put_link(nd); 3716 res = step_into(nd, WALK_TRAILING, dentry); 3717 if (unlikely(res)) 3718 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3719 return res; 3720 } 3721 3722 /* 3723 * Handle the last step of open() 3724 */ 3725 static int do_open(struct nameidata *nd, 3726 struct file *file, const struct open_flags *op) 3727 { 3728 struct mnt_idmap *idmap; 3729 int open_flag = op->open_flag; 3730 bool do_truncate; 3731 int acc_mode; 3732 int error; 3733 3734 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { 3735 error = complete_walk(nd); 3736 if (error) 3737 return error; 3738 } 3739 if (!(file->f_mode & FMODE_CREATED)) 3740 audit_inode(nd->name, nd->path.dentry, 0); 3741 idmap = mnt_idmap(nd->path.mnt); 3742 if (open_flag & O_CREAT) { 3743 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) 3744 return -EEXIST; 3745 if (d_is_dir(nd->path.dentry)) 3746 return -EISDIR; 3747 error = may_create_in_sticky(idmap, nd, 3748 d_backing_inode(nd->path.dentry)); 3749 if (unlikely(error)) 3750 return error; 3751 } 3752 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) 3753 return -ENOTDIR; 3754 3755 do_truncate = false; 3756 acc_mode = op->acc_mode; 3757 if (file->f_mode & FMODE_CREATED) { 3758 /* Don't check for write permission, don't truncate */ 3759 open_flag &= ~O_TRUNC; 3760 acc_mode = 0; 3761 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { 3762 error = mnt_want_write(nd->path.mnt); 3763 if (error) 3764 return error; 3765 do_truncate = true; 3766 } 3767 error = may_open(idmap, &nd->path, acc_mode, open_flag); 3768 if (!error && !(file->f_mode & FMODE_OPENED)) 3769 error = vfs_open(&nd->path, file); 3770 if (!error) 3771 error = security_file_post_open(file, op->acc_mode); 3772 if (!error && do_truncate) 3773 error = handle_truncate(idmap, file); 3774 if (unlikely(error > 0)) { 3775 WARN_ON(1); 3776 error = -EINVAL; 3777 } 3778 if (do_truncate) 3779 mnt_drop_write(nd->path.mnt); 3780 return error; 3781 } 3782 3783 /** 3784 * vfs_tmpfile - create tmpfile 3785 * @idmap: idmap of the mount the inode was found from 3786 * @parentpath: pointer to the path of the base directory 3787 * @file: file descriptor of the new tmpfile 3788 * @mode: mode of the new tmpfile 3789 * 3790 * Create a temporary file. 3791 * 3792 * If the inode has been found through an idmapped mount the idmap of 3793 * the vfsmount must be passed through @idmap. This function will then take 3794 * care to map the inode according to @idmap before checking permissions. 3795 * On non-idmapped mounts or if permission checking is to be performed on the 3796 * raw inode simply pass @nop_mnt_idmap. 3797 */ 3798 int vfs_tmpfile(struct mnt_idmap *idmap, 3799 const struct path *parentpath, 3800 struct file *file, umode_t mode) 3801 { 3802 struct dentry *child; 3803 struct inode *dir = d_inode(parentpath->dentry); 3804 struct inode *inode; 3805 int error; 3806 int open_flag = file->f_flags; 3807 3808 /* we want directory to be writable */ 3809 error = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); 3810 if (error) 3811 return error; 3812 if (!dir->i_op->tmpfile) 3813 return -EOPNOTSUPP; 3814 child = d_alloc(parentpath->dentry, &slash_name); 3815 if (unlikely(!child)) 3816 return -ENOMEM; 3817 file->f_path.mnt = parentpath->mnt; 3818 file->f_path.dentry = child; 3819 mode = vfs_prepare_mode(idmap, dir, mode, mode, mode); 3820 error = dir->i_op->tmpfile(idmap, dir, file, mode); 3821 dput(child); 3822 if (file->f_mode & FMODE_OPENED) 3823 fsnotify_open(file); 3824 if (error) 3825 return error; 3826 /* Don't check for other permissions, the inode was just created */ 3827 error = may_open(idmap, &file->f_path, 0, file->f_flags); 3828 if (error) 3829 return error; 3830 inode = file_inode(file); 3831 if (!(open_flag & O_EXCL)) { 3832 spin_lock(&inode->i_lock); 3833 inode->i_state |= I_LINKABLE; 3834 spin_unlock(&inode->i_lock); 3835 } 3836 security_inode_post_create_tmpfile(idmap, inode); 3837 return 0; 3838 } 3839 3840 /** 3841 * kernel_tmpfile_open - open a tmpfile for kernel internal use 3842 * @idmap: idmap of the mount the inode was found from 3843 * @parentpath: path of the base directory 3844 * @mode: mode of the new tmpfile 3845 * @open_flag: flags 3846 * @cred: credentials for open 3847 * 3848 * Create and open a temporary file. The file is not accounted in nr_files, 3849 * hence this is only for kernel internal use, and must not be installed into 3850 * file tables or such. 3851 */ 3852 struct file *kernel_tmpfile_open(struct mnt_idmap *idmap, 3853 const struct path *parentpath, 3854 umode_t mode, int open_flag, 3855 const struct cred *cred) 3856 { 3857 struct file *file; 3858 int error; 3859 3860 file = alloc_empty_file_noaccount(open_flag, cred); 3861 if (IS_ERR(file)) 3862 return file; 3863 3864 error = vfs_tmpfile(idmap, parentpath, file, mode); 3865 if (error) { 3866 fput(file); 3867 file = ERR_PTR(error); 3868 } 3869 return file; 3870 } 3871 EXPORT_SYMBOL(kernel_tmpfile_open); 3872 3873 static int do_tmpfile(struct nameidata *nd, unsigned flags, 3874 const struct open_flags *op, 3875 struct file *file) 3876 { 3877 struct path path; 3878 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); 3879 3880 if (unlikely(error)) 3881 return error; 3882 error = mnt_want_write(path.mnt); 3883 if (unlikely(error)) 3884 goto out; 3885 error = vfs_tmpfile(mnt_idmap(path.mnt), &path, file, op->mode); 3886 if (error) 3887 goto out2; 3888 audit_inode(nd->name, file->f_path.dentry, 0); 3889 out2: 3890 mnt_drop_write(path.mnt); 3891 out: 3892 path_put(&path); 3893 return error; 3894 } 3895 3896 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) 3897 { 3898 struct path path; 3899 int error = path_lookupat(nd, flags, &path); 3900 if (!error) { 3901 audit_inode(nd->name, path.dentry, 0); 3902 error = vfs_open(&path, file); 3903 path_put(&path); 3904 } 3905 return error; 3906 } 3907 3908 static struct file *path_openat(struct nameidata *nd, 3909 const struct open_flags *op, unsigned flags) 3910 { 3911 struct file *file; 3912 int error; 3913 3914 file = alloc_empty_file(op->open_flag, current_cred()); 3915 if (IS_ERR(file)) 3916 return file; 3917 3918 if (unlikely(file->f_flags & __O_TMPFILE)) { 3919 error = do_tmpfile(nd, flags, op, file); 3920 } else if (unlikely(file->f_flags & O_PATH)) { 3921 error = do_o_path(nd, flags, file); 3922 } else { 3923 const char *s = path_init(nd, flags); 3924 while (!(error = link_path_walk(s, nd)) && 3925 (s = open_last_lookups(nd, file, op)) != NULL) 3926 ; 3927 if (!error) 3928 error = do_open(nd, file, op); 3929 terminate_walk(nd); 3930 } 3931 if (likely(!error)) { 3932 if (likely(file->f_mode & FMODE_OPENED)) 3933 return file; 3934 WARN_ON(1); 3935 error = -EINVAL; 3936 } 3937 fput(file); 3938 if (error == -EOPENSTALE) { 3939 if (flags & LOOKUP_RCU) 3940 error = -ECHILD; 3941 else 3942 error = -ESTALE; 3943 } 3944 return ERR_PTR(error); 3945 } 3946 3947 struct file *do_filp_open(int dfd, struct filename *pathname, 3948 const struct open_flags *op) 3949 { 3950 struct nameidata nd; 3951 int flags = op->lookup_flags; 3952 struct file *filp; 3953 3954 set_nameidata(&nd, dfd, pathname, NULL); 3955 filp = path_openat(&nd, op, flags | LOOKUP_RCU); 3956 if (unlikely(filp == ERR_PTR(-ECHILD))) 3957 filp = path_openat(&nd, op, flags); 3958 if (unlikely(filp == ERR_PTR(-ESTALE))) 3959 filp = path_openat(&nd, op, flags | LOOKUP_REVAL); 3960 restore_nameidata(); 3961 return filp; 3962 } 3963 3964 struct file *do_file_open_root(const struct path *root, 3965 const char *name, const struct open_flags *op) 3966 { 3967 struct nameidata nd; 3968 struct file *file; 3969 struct filename *filename; 3970 int flags = op->lookup_flags; 3971 3972 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) 3973 return ERR_PTR(-ELOOP); 3974 3975 filename = getname_kernel(name); 3976 if (IS_ERR(filename)) 3977 return ERR_CAST(filename); 3978 3979 set_nameidata(&nd, -1, filename, root); 3980 file = path_openat(&nd, op, flags | LOOKUP_RCU); 3981 if (unlikely(file == ERR_PTR(-ECHILD))) 3982 file = path_openat(&nd, op, flags); 3983 if (unlikely(file == ERR_PTR(-ESTALE))) 3984 file = path_openat(&nd, op, flags | LOOKUP_REVAL); 3985 restore_nameidata(); 3986 putname(filename); 3987 return file; 3988 } 3989 3990 static struct dentry *filename_create(int dfd, struct filename *name, 3991 struct path *path, unsigned int lookup_flags) 3992 { 3993 struct dentry *dentry = ERR_PTR(-EEXIST); 3994 struct qstr last; 3995 bool want_dir = lookup_flags & LOOKUP_DIRECTORY; 3996 unsigned int reval_flag = lookup_flags & LOOKUP_REVAL; 3997 unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL; 3998 int type; 3999 int err2; 4000 int error; 4001 4002 error = filename_parentat(dfd, name, reval_flag, path, &last, &type); 4003 if (error) 4004 return ERR_PTR(error); 4005 4006 /* 4007 * Yucky last component or no last component at all? 4008 * (foo/., foo/.., /////) 4009 */ 4010 if (unlikely(type != LAST_NORM)) 4011 goto out; 4012 4013 /* don't fail immediately if it's r/o, at least try to report other errors */ 4014 err2 = mnt_want_write(path->mnt); 4015 /* 4016 * Do the final lookup. Suppress 'create' if there is a trailing 4017 * '/', and a directory wasn't requested. 4018 */ 4019 if (last.name[last.len] && !want_dir) 4020 create_flags = 0; 4021 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 4022 dentry = lookup_one_qstr_excl(&last, path->dentry, 4023 reval_flag | create_flags); 4024 if (IS_ERR(dentry)) 4025 goto unlock; 4026 4027 error = -EEXIST; 4028 if (d_is_positive(dentry)) 4029 goto fail; 4030 4031 /* 4032 * Special case - lookup gave negative, but... we had foo/bar/ 4033 * From the vfs_mknod() POV we just have a negative dentry - 4034 * all is fine. Let's be bastards - you had / on the end, you've 4035 * been asking for (non-existent) directory. -ENOENT for you. 4036 */ 4037 if (unlikely(!create_flags)) { 4038 error = -ENOENT; 4039 goto fail; 4040 } 4041 if (unlikely(err2)) { 4042 error = err2; 4043 goto fail; 4044 } 4045 return dentry; 4046 fail: 4047 dput(dentry); 4048 dentry = ERR_PTR(error); 4049 unlock: 4050 inode_unlock(path->dentry->d_inode); 4051 if (!err2) 4052 mnt_drop_write(path->mnt); 4053 out: 4054 path_put(path); 4055 return dentry; 4056 } 4057 4058 struct dentry *kern_path_create(int dfd, const char *pathname, 4059 struct path *path, unsigned int lookup_flags) 4060 { 4061 struct filename *filename = getname_kernel(pathname); 4062 struct dentry *res = filename_create(dfd, filename, path, lookup_flags); 4063 4064 putname(filename); 4065 return res; 4066 } 4067 EXPORT_SYMBOL(kern_path_create); 4068 4069 void done_path_create(struct path *path, struct dentry *dentry) 4070 { 4071 dput(dentry); 4072 inode_unlock(path->dentry->d_inode); 4073 mnt_drop_write(path->mnt); 4074 path_put(path); 4075 } 4076 EXPORT_SYMBOL(done_path_create); 4077 4078 inline struct dentry *user_path_create(int dfd, const char __user *pathname, 4079 struct path *path, unsigned int lookup_flags) 4080 { 4081 struct filename *filename = getname(pathname); 4082 struct dentry *res = filename_create(dfd, filename, path, lookup_flags); 4083 4084 putname(filename); 4085 return res; 4086 } 4087 EXPORT_SYMBOL(user_path_create); 4088 4089 /** 4090 * vfs_mknod - create device node or file 4091 * @idmap: idmap of the mount the inode was found from 4092 * @dir: inode of the parent directory 4093 * @dentry: dentry of the child device node 4094 * @mode: mode of the child device node 4095 * @dev: device number of device to create 4096 * 4097 * Create a device node or file. 4098 * 4099 * If the inode has been found through an idmapped mount the idmap of 4100 * the vfsmount must be passed through @idmap. This function will then take 4101 * care to map the inode according to @idmap before checking permissions. 4102 * On non-idmapped mounts or if permission checking is to be performed on the 4103 * raw inode simply pass @nop_mnt_idmap. 4104 */ 4105 int vfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 4106 struct dentry *dentry, umode_t mode, dev_t dev) 4107 { 4108 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; 4109 int error = may_create(idmap, dir, dentry); 4110 4111 if (error) 4112 return error; 4113 4114 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && 4115 !capable(CAP_MKNOD)) 4116 return -EPERM; 4117 4118 if (!dir->i_op->mknod) 4119 return -EPERM; 4120 4121 mode = vfs_prepare_mode(idmap, dir, mode, mode, mode); 4122 error = devcgroup_inode_mknod(mode, dev); 4123 if (error) 4124 return error; 4125 4126 error = security_inode_mknod(dir, dentry, mode, dev); 4127 if (error) 4128 return error; 4129 4130 error = dir->i_op->mknod(idmap, dir, dentry, mode, dev); 4131 if (!error) 4132 fsnotify_create(dir, dentry); 4133 return error; 4134 } 4135 EXPORT_SYMBOL(vfs_mknod); 4136 4137 static int may_mknod(umode_t mode) 4138 { 4139 switch (mode & S_IFMT) { 4140 case S_IFREG: 4141 case S_IFCHR: 4142 case S_IFBLK: 4143 case S_IFIFO: 4144 case S_IFSOCK: 4145 case 0: /* zero mode translates to S_IFREG */ 4146 return 0; 4147 case S_IFDIR: 4148 return -EPERM; 4149 default: 4150 return -EINVAL; 4151 } 4152 } 4153 4154 static int do_mknodat(int dfd, struct filename *name, umode_t mode, 4155 unsigned int dev) 4156 { 4157 struct mnt_idmap *idmap; 4158 struct dentry *dentry; 4159 struct path path; 4160 int error; 4161 unsigned int lookup_flags = 0; 4162 4163 error = may_mknod(mode); 4164 if (error) 4165 goto out1; 4166 retry: 4167 dentry = filename_create(dfd, name, &path, lookup_flags); 4168 error = PTR_ERR(dentry); 4169 if (IS_ERR(dentry)) 4170 goto out1; 4171 4172 error = security_path_mknod(&path, dentry, 4173 mode_strip_umask(path.dentry->d_inode, mode), dev); 4174 if (error) 4175 goto out2; 4176 4177 idmap = mnt_idmap(path.mnt); 4178 switch (mode & S_IFMT) { 4179 case 0: case S_IFREG: 4180 error = vfs_create(idmap, path.dentry->d_inode, 4181 dentry, mode, true); 4182 if (!error) 4183 security_path_post_mknod(idmap, dentry); 4184 break; 4185 case S_IFCHR: case S_IFBLK: 4186 error = vfs_mknod(idmap, path.dentry->d_inode, 4187 dentry, mode, new_decode_dev(dev)); 4188 break; 4189 case S_IFIFO: case S_IFSOCK: 4190 error = vfs_mknod(idmap, path.dentry->d_inode, 4191 dentry, mode, 0); 4192 break; 4193 } 4194 out2: 4195 done_path_create(&path, dentry); 4196 if (retry_estale(error, lookup_flags)) { 4197 lookup_flags |= LOOKUP_REVAL; 4198 goto retry; 4199 } 4200 out1: 4201 putname(name); 4202 return error; 4203 } 4204 4205 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 4206 unsigned int, dev) 4207 { 4208 return do_mknodat(dfd, getname(filename), mode, dev); 4209 } 4210 4211 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 4212 { 4213 return do_mknodat(AT_FDCWD, getname(filename), mode, dev); 4214 } 4215 4216 /** 4217 * vfs_mkdir - create directory 4218 * @idmap: idmap of the mount the inode was found from 4219 * @dir: inode of the parent directory 4220 * @dentry: dentry of the child directory 4221 * @mode: mode of the child directory 4222 * 4223 * Create a directory. 4224 * 4225 * If the inode has been found through an idmapped mount the idmap of 4226 * the vfsmount must be passed through @idmap. This function will then take 4227 * care to map the inode according to @idmap before checking permissions. 4228 * On non-idmapped mounts or if permission checking is to be performed on the 4229 * raw inode simply pass @nop_mnt_idmap. 4230 */ 4231 int vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 4232 struct dentry *dentry, umode_t mode) 4233 { 4234 int error; 4235 unsigned max_links = dir->i_sb->s_max_links; 4236 4237 error = may_create(idmap, dir, dentry); 4238 if (error) 4239 return error; 4240 4241 if (!dir->i_op->mkdir) 4242 return -EPERM; 4243 4244 mode = vfs_prepare_mode(idmap, dir, mode, S_IRWXUGO | S_ISVTX, 0); 4245 error = security_inode_mkdir(dir, dentry, mode); 4246 if (error) 4247 return error; 4248 4249 if (max_links && dir->i_nlink >= max_links) 4250 return -EMLINK; 4251 4252 error = dir->i_op->mkdir(idmap, dir, dentry, mode); 4253 if (!error) 4254 fsnotify_mkdir(dir, dentry); 4255 return error; 4256 } 4257 EXPORT_SYMBOL(vfs_mkdir); 4258 4259 int do_mkdirat(int dfd, struct filename *name, umode_t mode) 4260 { 4261 struct dentry *dentry; 4262 struct path path; 4263 int error; 4264 unsigned int lookup_flags = LOOKUP_DIRECTORY; 4265 4266 retry: 4267 dentry = filename_create(dfd, name, &path, lookup_flags); 4268 error = PTR_ERR(dentry); 4269 if (IS_ERR(dentry)) 4270 goto out_putname; 4271 4272 error = security_path_mkdir(&path, dentry, 4273 mode_strip_umask(path.dentry->d_inode, mode)); 4274 if (!error) { 4275 error = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode, 4276 dentry, mode); 4277 } 4278 done_path_create(&path, dentry); 4279 if (retry_estale(error, lookup_flags)) { 4280 lookup_flags |= LOOKUP_REVAL; 4281 goto retry; 4282 } 4283 out_putname: 4284 putname(name); 4285 return error; 4286 } 4287 4288 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 4289 { 4290 return do_mkdirat(dfd, getname(pathname), mode); 4291 } 4292 4293 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 4294 { 4295 return do_mkdirat(AT_FDCWD, getname(pathname), mode); 4296 } 4297 4298 /** 4299 * vfs_rmdir - remove directory 4300 * @idmap: idmap of the mount the inode was found from 4301 * @dir: inode of the parent directory 4302 * @dentry: dentry of the child directory 4303 * 4304 * Remove a directory. 4305 * 4306 * If the inode has been found through an idmapped mount the idmap of 4307 * the vfsmount must be passed through @idmap. This function will then take 4308 * care to map the inode according to @idmap before checking permissions. 4309 * On non-idmapped mounts or if permission checking is to be performed on the 4310 * raw inode simply pass @nop_mnt_idmap. 4311 */ 4312 int vfs_rmdir(struct mnt_idmap *idmap, struct inode *dir, 4313 struct dentry *dentry) 4314 { 4315 int error = may_delete(idmap, dir, dentry, 1); 4316 4317 if (error) 4318 return error; 4319 4320 if (!dir->i_op->rmdir) 4321 return -EPERM; 4322 4323 dget(dentry); 4324 inode_lock(dentry->d_inode); 4325 4326 error = -EBUSY; 4327 if (is_local_mountpoint(dentry) || 4328 (dentry->d_inode->i_flags & S_KERNEL_FILE)) 4329 goto out; 4330 4331 error = security_inode_rmdir(dir, dentry); 4332 if (error) 4333 goto out; 4334 4335 error = dir->i_op->rmdir(dir, dentry); 4336 if (error) 4337 goto out; 4338 4339 shrink_dcache_parent(dentry); 4340 dentry->d_inode->i_flags |= S_DEAD; 4341 dont_mount(dentry); 4342 detach_mounts(dentry); 4343 4344 out: 4345 inode_unlock(dentry->d_inode); 4346 dput(dentry); 4347 if (!error) 4348 d_delete_notify(dir, dentry); 4349 return error; 4350 } 4351 EXPORT_SYMBOL(vfs_rmdir); 4352 4353 int do_rmdir(int dfd, struct filename *name) 4354 { 4355 int error; 4356 struct dentry *dentry; 4357 struct path path; 4358 struct qstr last; 4359 int type; 4360 unsigned int lookup_flags = 0; 4361 retry: 4362 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 4363 if (error) 4364 goto exit1; 4365 4366 switch (type) { 4367 case LAST_DOTDOT: 4368 error = -ENOTEMPTY; 4369 goto exit2; 4370 case LAST_DOT: 4371 error = -EINVAL; 4372 goto exit2; 4373 case LAST_ROOT: 4374 error = -EBUSY; 4375 goto exit2; 4376 } 4377 4378 error = mnt_want_write(path.mnt); 4379 if (error) 4380 goto exit2; 4381 4382 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 4383 dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags); 4384 error = PTR_ERR(dentry); 4385 if (IS_ERR(dentry)) 4386 goto exit3; 4387 if (!dentry->d_inode) { 4388 error = -ENOENT; 4389 goto exit4; 4390 } 4391 error = security_path_rmdir(&path, dentry); 4392 if (error) 4393 goto exit4; 4394 error = vfs_rmdir(mnt_idmap(path.mnt), path.dentry->d_inode, dentry); 4395 exit4: 4396 dput(dentry); 4397 exit3: 4398 inode_unlock(path.dentry->d_inode); 4399 mnt_drop_write(path.mnt); 4400 exit2: 4401 path_put(&path); 4402 if (retry_estale(error, lookup_flags)) { 4403 lookup_flags |= LOOKUP_REVAL; 4404 goto retry; 4405 } 4406 exit1: 4407 putname(name); 4408 return error; 4409 } 4410 4411 SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 4412 { 4413 return do_rmdir(AT_FDCWD, getname(pathname)); 4414 } 4415 4416 /** 4417 * vfs_unlink - unlink a filesystem object 4418 * @idmap: idmap of the mount the inode was found from 4419 * @dir: parent directory 4420 * @dentry: victim 4421 * @delegated_inode: returns victim inode, if the inode is delegated. 4422 * 4423 * The caller must hold dir->i_mutex. 4424 * 4425 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and 4426 * return a reference to the inode in delegated_inode. The caller 4427 * should then break the delegation on that inode and retry. Because 4428 * breaking a delegation may take a long time, the caller should drop 4429 * dir->i_mutex before doing so. 4430 * 4431 * Alternatively, a caller may pass NULL for delegated_inode. This may 4432 * be appropriate for callers that expect the underlying filesystem not 4433 * to be NFS exported. 4434 * 4435 * If the inode has been found through an idmapped mount the idmap of 4436 * the vfsmount must be passed through @idmap. This function will then take 4437 * care to map the inode according to @idmap before checking permissions. 4438 * On non-idmapped mounts or if permission checking is to be performed on the 4439 * raw inode simply pass @nop_mnt_idmap. 4440 */ 4441 int vfs_unlink(struct mnt_idmap *idmap, struct inode *dir, 4442 struct dentry *dentry, struct inode **delegated_inode) 4443 { 4444 struct inode *target = dentry->d_inode; 4445 int error = may_delete(idmap, dir, dentry, 0); 4446 4447 if (error) 4448 return error; 4449 4450 if (!dir->i_op->unlink) 4451 return -EPERM; 4452 4453 inode_lock(target); 4454 if (IS_SWAPFILE(target)) 4455 error = -EPERM; 4456 else if (is_local_mountpoint(dentry)) 4457 error = -EBUSY; 4458 else { 4459 error = security_inode_unlink(dir, dentry); 4460 if (!error) { 4461 error = try_break_deleg(target, delegated_inode); 4462 if (error) 4463 goto out; 4464 error = dir->i_op->unlink(dir, dentry); 4465 if (!error) { 4466 dont_mount(dentry); 4467 detach_mounts(dentry); 4468 } 4469 } 4470 } 4471 out: 4472 inode_unlock(target); 4473 4474 /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 4475 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { 4476 fsnotify_unlink(dir, dentry); 4477 } else if (!error) { 4478 fsnotify_link_count(target); 4479 d_delete_notify(dir, dentry); 4480 } 4481 4482 return error; 4483 } 4484 EXPORT_SYMBOL(vfs_unlink); 4485 4486 /* 4487 * Make sure that the actual truncation of the file will occur outside its 4488 * directory's i_mutex. Truncate can take a long time if there is a lot of 4489 * writeout happening, and we don't want to prevent access to the directory 4490 * while waiting on the I/O. 4491 */ 4492 int do_unlinkat(int dfd, struct filename *name) 4493 { 4494 int error; 4495 struct dentry *dentry; 4496 struct path path; 4497 struct qstr last; 4498 int type; 4499 struct inode *inode = NULL; 4500 struct inode *delegated_inode = NULL; 4501 unsigned int lookup_flags = 0; 4502 retry: 4503 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 4504 if (error) 4505 goto exit1; 4506 4507 error = -EISDIR; 4508 if (type != LAST_NORM) 4509 goto exit2; 4510 4511 error = mnt_want_write(path.mnt); 4512 if (error) 4513 goto exit2; 4514 retry_deleg: 4515 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 4516 dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags); 4517 error = PTR_ERR(dentry); 4518 if (!IS_ERR(dentry)) { 4519 4520 /* Why not before? Because we want correct error value */ 4521 if (last.name[last.len] || d_is_negative(dentry)) 4522 goto slashes; 4523 inode = dentry->d_inode; 4524 ihold(inode); 4525 error = security_path_unlink(&path, dentry); 4526 if (error) 4527 goto exit3; 4528 error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode, 4529 dentry, &delegated_inode); 4530 exit3: 4531 dput(dentry); 4532 } 4533 inode_unlock(path.dentry->d_inode); 4534 if (inode) 4535 iput(inode); /* truncate the inode here */ 4536 inode = NULL; 4537 if (delegated_inode) { 4538 error = break_deleg_wait(&delegated_inode); 4539 if (!error) 4540 goto retry_deleg; 4541 } 4542 mnt_drop_write(path.mnt); 4543 exit2: 4544 path_put(&path); 4545 if (retry_estale(error, lookup_flags)) { 4546 lookup_flags |= LOOKUP_REVAL; 4547 inode = NULL; 4548 goto retry; 4549 } 4550 exit1: 4551 putname(name); 4552 return error; 4553 4554 slashes: 4555 if (d_is_negative(dentry)) 4556 error = -ENOENT; 4557 else if (d_is_dir(dentry)) 4558 error = -EISDIR; 4559 else 4560 error = -ENOTDIR; 4561 goto exit3; 4562 } 4563 4564 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 4565 { 4566 if ((flag & ~AT_REMOVEDIR) != 0) 4567 return -EINVAL; 4568 4569 if (flag & AT_REMOVEDIR) 4570 return do_rmdir(dfd, getname(pathname)); 4571 return do_unlinkat(dfd, getname(pathname)); 4572 } 4573 4574 SYSCALL_DEFINE1(unlink, const char __user *, pathname) 4575 { 4576 return do_unlinkat(AT_FDCWD, getname(pathname)); 4577 } 4578 4579 /** 4580 * vfs_symlink - create symlink 4581 * @idmap: idmap of the mount the inode was found from 4582 * @dir: inode of the parent directory 4583 * @dentry: dentry of the child symlink file 4584 * @oldname: name of the file to link to 4585 * 4586 * Create a symlink. 4587 * 4588 * If the inode has been found through an idmapped mount the idmap of 4589 * the vfsmount must be passed through @idmap. This function will then take 4590 * care to map the inode according to @idmap before checking permissions. 4591 * On non-idmapped mounts or if permission checking is to be performed on the 4592 * raw inode simply pass @nop_mnt_idmap. 4593 */ 4594 int vfs_symlink(struct mnt_idmap *idmap, struct inode *dir, 4595 struct dentry *dentry, const char *oldname) 4596 { 4597 int error; 4598 4599 error = may_create(idmap, dir, dentry); 4600 if (error) 4601 return error; 4602 4603 if (!dir->i_op->symlink) 4604 return -EPERM; 4605 4606 error = security_inode_symlink(dir, dentry, oldname); 4607 if (error) 4608 return error; 4609 4610 error = dir->i_op->symlink(idmap, dir, dentry, oldname); 4611 if (!error) 4612 fsnotify_create(dir, dentry); 4613 return error; 4614 } 4615 EXPORT_SYMBOL(vfs_symlink); 4616 4617 int do_symlinkat(struct filename *from, int newdfd, struct filename *to) 4618 { 4619 int error; 4620 struct dentry *dentry; 4621 struct path path; 4622 unsigned int lookup_flags = 0; 4623 4624 if (IS_ERR(from)) { 4625 error = PTR_ERR(from); 4626 goto out_putnames; 4627 } 4628 retry: 4629 dentry = filename_create(newdfd, to, &path, lookup_flags); 4630 error = PTR_ERR(dentry); 4631 if (IS_ERR(dentry)) 4632 goto out_putnames; 4633 4634 error = security_path_symlink(&path, dentry, from->name); 4635 if (!error) 4636 error = vfs_symlink(mnt_idmap(path.mnt), path.dentry->d_inode, 4637 dentry, from->name); 4638 done_path_create(&path, dentry); 4639 if (retry_estale(error, lookup_flags)) { 4640 lookup_flags |= LOOKUP_REVAL; 4641 goto retry; 4642 } 4643 out_putnames: 4644 putname(to); 4645 putname(from); 4646 return error; 4647 } 4648 4649 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 4650 int, newdfd, const char __user *, newname) 4651 { 4652 return do_symlinkat(getname(oldname), newdfd, getname(newname)); 4653 } 4654 4655 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 4656 { 4657 return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname)); 4658 } 4659 4660 /** 4661 * vfs_link - create a new link 4662 * @old_dentry: object to be linked 4663 * @idmap: idmap of the mount 4664 * @dir: new parent 4665 * @new_dentry: where to create the new link 4666 * @delegated_inode: returns inode needing a delegation break 4667 * 4668 * The caller must hold dir->i_mutex 4669 * 4670 * If vfs_link discovers a delegation on the to-be-linked file in need 4671 * of breaking, it will return -EWOULDBLOCK and return a reference to the 4672 * inode in delegated_inode. The caller should then break the delegation 4673 * and retry. Because breaking a delegation may take a long time, the 4674 * caller should drop the i_mutex before doing so. 4675 * 4676 * Alternatively, a caller may pass NULL for delegated_inode. This may 4677 * be appropriate for callers that expect the underlying filesystem not 4678 * to be NFS exported. 4679 * 4680 * If the inode has been found through an idmapped mount the idmap of 4681 * the vfsmount must be passed through @idmap. This function will then take 4682 * care to map the inode according to @idmap before checking permissions. 4683 * On non-idmapped mounts or if permission checking is to be performed on the 4684 * raw inode simply pass @nop_mnt_idmap. 4685 */ 4686 int vfs_link(struct dentry *old_dentry, struct mnt_idmap *idmap, 4687 struct inode *dir, struct dentry *new_dentry, 4688 struct inode **delegated_inode) 4689 { 4690 struct inode *inode = old_dentry->d_inode; 4691 unsigned max_links = dir->i_sb->s_max_links; 4692 int error; 4693 4694 if (!inode) 4695 return -ENOENT; 4696 4697 error = may_create(idmap, dir, new_dentry); 4698 if (error) 4699 return error; 4700 4701 if (dir->i_sb != inode->i_sb) 4702 return -EXDEV; 4703 4704 /* 4705 * A link to an append-only or immutable file cannot be created. 4706 */ 4707 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 4708 return -EPERM; 4709 /* 4710 * Updating the link count will likely cause i_uid and i_gid to 4711 * be writen back improperly if their true value is unknown to 4712 * the vfs. 4713 */ 4714 if (HAS_UNMAPPED_ID(idmap, inode)) 4715 return -EPERM; 4716 if (!dir->i_op->link) 4717 return -EPERM; 4718 if (S_ISDIR(inode->i_mode)) 4719 return -EPERM; 4720 4721 error = security_inode_link(old_dentry, dir, new_dentry); 4722 if (error) 4723 return error; 4724 4725 inode_lock(inode); 4726 /* Make sure we don't allow creating hardlink to an unlinked file */ 4727 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 4728 error = -ENOENT; 4729 else if (max_links && inode->i_nlink >= max_links) 4730 error = -EMLINK; 4731 else { 4732 error = try_break_deleg(inode, delegated_inode); 4733 if (!error) 4734 error = dir->i_op->link(old_dentry, dir, new_dentry); 4735 } 4736 4737 if (!error && (inode->i_state & I_LINKABLE)) { 4738 spin_lock(&inode->i_lock); 4739 inode->i_state &= ~I_LINKABLE; 4740 spin_unlock(&inode->i_lock); 4741 } 4742 inode_unlock(inode); 4743 if (!error) 4744 fsnotify_link(dir, inode, new_dentry); 4745 return error; 4746 } 4747 EXPORT_SYMBOL(vfs_link); 4748 4749 /* 4750 * Hardlinks are often used in delicate situations. We avoid 4751 * security-related surprises by not following symlinks on the 4752 * newname. --KAB 4753 * 4754 * We don't follow them on the oldname either to be compatible 4755 * with linux 2.0, and to avoid hard-linking to directories 4756 * and other special files. --ADM 4757 */ 4758 int do_linkat(int olddfd, struct filename *old, int newdfd, 4759 struct filename *new, int flags) 4760 { 4761 struct mnt_idmap *idmap; 4762 struct dentry *new_dentry; 4763 struct path old_path, new_path; 4764 struct inode *delegated_inode = NULL; 4765 int how = 0; 4766 int error; 4767 4768 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) { 4769 error = -EINVAL; 4770 goto out_putnames; 4771 } 4772 /* 4773 * To use null names we require CAP_DAC_READ_SEARCH or 4774 * that the open-time creds of the dfd matches current. 4775 * This ensures that not everyone will be able to create 4776 * a hardlink using the passed file descriptor. 4777 */ 4778 if (flags & AT_EMPTY_PATH) 4779 how |= LOOKUP_LINKAT_EMPTY; 4780 4781 if (flags & AT_SYMLINK_FOLLOW) 4782 how |= LOOKUP_FOLLOW; 4783 retry: 4784 error = filename_lookup(olddfd, old, how, &old_path, NULL); 4785 if (error) 4786 goto out_putnames; 4787 4788 new_dentry = filename_create(newdfd, new, &new_path, 4789 (how & LOOKUP_REVAL)); 4790 error = PTR_ERR(new_dentry); 4791 if (IS_ERR(new_dentry)) 4792 goto out_putpath; 4793 4794 error = -EXDEV; 4795 if (old_path.mnt != new_path.mnt) 4796 goto out_dput; 4797 idmap = mnt_idmap(new_path.mnt); 4798 error = may_linkat(idmap, &old_path); 4799 if (unlikely(error)) 4800 goto out_dput; 4801 error = security_path_link(old_path.dentry, &new_path, new_dentry); 4802 if (error) 4803 goto out_dput; 4804 error = vfs_link(old_path.dentry, idmap, new_path.dentry->d_inode, 4805 new_dentry, &delegated_inode); 4806 out_dput: 4807 done_path_create(&new_path, new_dentry); 4808 if (delegated_inode) { 4809 error = break_deleg_wait(&delegated_inode); 4810 if (!error) { 4811 path_put(&old_path); 4812 goto retry; 4813 } 4814 } 4815 if (retry_estale(error, how)) { 4816 path_put(&old_path); 4817 how |= LOOKUP_REVAL; 4818 goto retry; 4819 } 4820 out_putpath: 4821 path_put(&old_path); 4822 out_putnames: 4823 putname(old); 4824 putname(new); 4825 4826 return error; 4827 } 4828 4829 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 4830 int, newdfd, const char __user *, newname, int, flags) 4831 { 4832 return do_linkat(olddfd, getname_uflags(oldname, flags), 4833 newdfd, getname(newname), flags); 4834 } 4835 4836 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 4837 { 4838 return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0); 4839 } 4840 4841 /** 4842 * vfs_rename - rename a filesystem object 4843 * @rd: pointer to &struct renamedata info 4844 * 4845 * The caller must hold multiple mutexes--see lock_rename()). 4846 * 4847 * If vfs_rename discovers a delegation in need of breaking at either 4848 * the source or destination, it will return -EWOULDBLOCK and return a 4849 * reference to the inode in delegated_inode. The caller should then 4850 * break the delegation and retry. Because breaking a delegation may 4851 * take a long time, the caller should drop all locks before doing 4852 * so. 4853 * 4854 * Alternatively, a caller may pass NULL for delegated_inode. This may 4855 * be appropriate for callers that expect the underlying filesystem not 4856 * to be NFS exported. 4857 * 4858 * The worst of all namespace operations - renaming directory. "Perverted" 4859 * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 4860 * Problems: 4861 * 4862 * a) we can get into loop creation. 4863 * b) race potential - two innocent renames can create a loop together. 4864 * That's where 4.4BSD screws up. Current fix: serialization on 4865 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 4866 * story. 4867 * c) we may have to lock up to _four_ objects - parents and victim (if it exists), 4868 * and source (if it's a non-directory or a subdirectory that moves to 4869 * different parent). 4870 * And that - after we got ->i_mutex on parents (until then we don't know 4871 * whether the target exists). Solution: try to be smart with locking 4872 * order for inodes. We rely on the fact that tree topology may change 4873 * only under ->s_vfs_rename_mutex _and_ that parent of the object we 4874 * move will be locked. Thus we can rank directories by the tree 4875 * (ancestors first) and rank all non-directories after them. 4876 * That works since everybody except rename does "lock parent, lookup, 4877 * lock child" and rename is under ->s_vfs_rename_mutex. 4878 * HOWEVER, it relies on the assumption that any object with ->lookup() 4879 * has no more than 1 dentry. If "hybrid" objects will ever appear, 4880 * we'd better make sure that there's no link(2) for them. 4881 * d) conversion from fhandle to dentry may come in the wrong moment - when 4882 * we are removing the target. Solution: we will have to grab ->i_mutex 4883 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 4884 * ->i_mutex on parents, which works but leads to some truly excessive 4885 * locking]. 4886 */ 4887 int vfs_rename(struct renamedata *rd) 4888 { 4889 int error; 4890 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; 4891 struct dentry *old_dentry = rd->old_dentry; 4892 struct dentry *new_dentry = rd->new_dentry; 4893 struct inode **delegated_inode = rd->delegated_inode; 4894 unsigned int flags = rd->flags; 4895 bool is_dir = d_is_dir(old_dentry); 4896 struct inode *source = old_dentry->d_inode; 4897 struct inode *target = new_dentry->d_inode; 4898 bool new_is_dir = false; 4899 unsigned max_links = new_dir->i_sb->s_max_links; 4900 struct name_snapshot old_name; 4901 bool lock_old_subdir, lock_new_subdir; 4902 4903 if (source == target) 4904 return 0; 4905 4906 error = may_delete(rd->old_mnt_idmap, old_dir, old_dentry, is_dir); 4907 if (error) 4908 return error; 4909 4910 if (!target) { 4911 error = may_create(rd->new_mnt_idmap, new_dir, new_dentry); 4912 } else { 4913 new_is_dir = d_is_dir(new_dentry); 4914 4915 if (!(flags & RENAME_EXCHANGE)) 4916 error = may_delete(rd->new_mnt_idmap, new_dir, 4917 new_dentry, is_dir); 4918 else 4919 error = may_delete(rd->new_mnt_idmap, new_dir, 4920 new_dentry, new_is_dir); 4921 } 4922 if (error) 4923 return error; 4924 4925 if (!old_dir->i_op->rename) 4926 return -EPERM; 4927 4928 /* 4929 * If we are going to change the parent - check write permissions, 4930 * we'll need to flip '..'. 4931 */ 4932 if (new_dir != old_dir) { 4933 if (is_dir) { 4934 error = inode_permission(rd->old_mnt_idmap, source, 4935 MAY_WRITE); 4936 if (error) 4937 return error; 4938 } 4939 if ((flags & RENAME_EXCHANGE) && new_is_dir) { 4940 error = inode_permission(rd->new_mnt_idmap, target, 4941 MAY_WRITE); 4942 if (error) 4943 return error; 4944 } 4945 } 4946 4947 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, 4948 flags); 4949 if (error) 4950 return error; 4951 4952 take_dentry_name_snapshot(&old_name, old_dentry); 4953 dget(new_dentry); 4954 /* 4955 * Lock children. 4956 * The source subdirectory needs to be locked on cross-directory 4957 * rename or cross-directory exchange since its parent changes. 4958 * The target subdirectory needs to be locked on cross-directory 4959 * exchange due to parent change and on any rename due to becoming 4960 * a victim. 4961 * Non-directories need locking in all cases (for NFS reasons); 4962 * they get locked after any subdirectories (in inode address order). 4963 * 4964 * NOTE: WE ONLY LOCK UNRELATED DIRECTORIES IN CROSS-DIRECTORY CASE. 4965 * NEVER, EVER DO THAT WITHOUT ->s_vfs_rename_mutex. 4966 */ 4967 lock_old_subdir = new_dir != old_dir; 4968 lock_new_subdir = new_dir != old_dir || !(flags & RENAME_EXCHANGE); 4969 if (is_dir) { 4970 if (lock_old_subdir) 4971 inode_lock_nested(source, I_MUTEX_CHILD); 4972 if (target && (!new_is_dir || lock_new_subdir)) 4973 inode_lock(target); 4974 } else if (new_is_dir) { 4975 if (lock_new_subdir) 4976 inode_lock_nested(target, I_MUTEX_CHILD); 4977 inode_lock(source); 4978 } else { 4979 lock_two_nondirectories(source, target); 4980 } 4981 4982 error = -EPERM; 4983 if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target))) 4984 goto out; 4985 4986 error = -EBUSY; 4987 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) 4988 goto out; 4989 4990 if (max_links && new_dir != old_dir) { 4991 error = -EMLINK; 4992 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) 4993 goto out; 4994 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && 4995 old_dir->i_nlink >= max_links) 4996 goto out; 4997 } 4998 if (!is_dir) { 4999 error = try_break_deleg(source, delegated_inode); 5000 if (error) 5001 goto out; 5002 } 5003 if (target && !new_is_dir) { 5004 error = try_break_deleg(target, delegated_inode); 5005 if (error) 5006 goto out; 5007 } 5008 error = old_dir->i_op->rename(rd->new_mnt_idmap, old_dir, old_dentry, 5009 new_dir, new_dentry, flags); 5010 if (error) 5011 goto out; 5012 5013 if (!(flags & RENAME_EXCHANGE) && target) { 5014 if (is_dir) { 5015 shrink_dcache_parent(new_dentry); 5016 target->i_flags |= S_DEAD; 5017 } 5018 dont_mount(new_dentry); 5019 detach_mounts(new_dentry); 5020 } 5021 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { 5022 if (!(flags & RENAME_EXCHANGE)) 5023 d_move(old_dentry, new_dentry); 5024 else 5025 d_exchange(old_dentry, new_dentry); 5026 } 5027 out: 5028 if (!is_dir || lock_old_subdir) 5029 inode_unlock(source); 5030 if (target && (!new_is_dir || lock_new_subdir)) 5031 inode_unlock(target); 5032 dput(new_dentry); 5033 if (!error) { 5034 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, 5035 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); 5036 if (flags & RENAME_EXCHANGE) { 5037 fsnotify_move(new_dir, old_dir, &old_dentry->d_name, 5038 new_is_dir, NULL, new_dentry); 5039 } 5040 } 5041 release_dentry_name_snapshot(&old_name); 5042 5043 return error; 5044 } 5045 EXPORT_SYMBOL(vfs_rename); 5046 5047 int do_renameat2(int olddfd, struct filename *from, int newdfd, 5048 struct filename *to, unsigned int flags) 5049 { 5050 struct renamedata rd; 5051 struct dentry *old_dentry, *new_dentry; 5052 struct dentry *trap; 5053 struct path old_path, new_path; 5054 struct qstr old_last, new_last; 5055 int old_type, new_type; 5056 struct inode *delegated_inode = NULL; 5057 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; 5058 bool should_retry = false; 5059 int error = -EINVAL; 5060 5061 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 5062 goto put_names; 5063 5064 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && 5065 (flags & RENAME_EXCHANGE)) 5066 goto put_names; 5067 5068 if (flags & RENAME_EXCHANGE) 5069 target_flags = 0; 5070 5071 retry: 5072 error = filename_parentat(olddfd, from, lookup_flags, &old_path, 5073 &old_last, &old_type); 5074 if (error) 5075 goto put_names; 5076 5077 error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, 5078 &new_type); 5079 if (error) 5080 goto exit1; 5081 5082 error = -EXDEV; 5083 if (old_path.mnt != new_path.mnt) 5084 goto exit2; 5085 5086 error = -EBUSY; 5087 if (old_type != LAST_NORM) 5088 goto exit2; 5089 5090 if (flags & RENAME_NOREPLACE) 5091 error = -EEXIST; 5092 if (new_type != LAST_NORM) 5093 goto exit2; 5094 5095 error = mnt_want_write(old_path.mnt); 5096 if (error) 5097 goto exit2; 5098 5099 retry_deleg: 5100 trap = lock_rename(new_path.dentry, old_path.dentry); 5101 if (IS_ERR(trap)) { 5102 error = PTR_ERR(trap); 5103 goto exit_lock_rename; 5104 } 5105 5106 old_dentry = lookup_one_qstr_excl(&old_last, old_path.dentry, 5107 lookup_flags); 5108 error = PTR_ERR(old_dentry); 5109 if (IS_ERR(old_dentry)) 5110 goto exit3; 5111 /* source must exist */ 5112 error = -ENOENT; 5113 if (d_is_negative(old_dentry)) 5114 goto exit4; 5115 new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry, 5116 lookup_flags | target_flags); 5117 error = PTR_ERR(new_dentry); 5118 if (IS_ERR(new_dentry)) 5119 goto exit4; 5120 error = -EEXIST; 5121 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) 5122 goto exit5; 5123 if (flags & RENAME_EXCHANGE) { 5124 error = -ENOENT; 5125 if (d_is_negative(new_dentry)) 5126 goto exit5; 5127 5128 if (!d_is_dir(new_dentry)) { 5129 error = -ENOTDIR; 5130 if (new_last.name[new_last.len]) 5131 goto exit5; 5132 } 5133 } 5134 /* unless the source is a directory trailing slashes give -ENOTDIR */ 5135 if (!d_is_dir(old_dentry)) { 5136 error = -ENOTDIR; 5137 if (old_last.name[old_last.len]) 5138 goto exit5; 5139 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) 5140 goto exit5; 5141 } 5142 /* source should not be ancestor of target */ 5143 error = -EINVAL; 5144 if (old_dentry == trap) 5145 goto exit5; 5146 /* target should not be an ancestor of source */ 5147 if (!(flags & RENAME_EXCHANGE)) 5148 error = -ENOTEMPTY; 5149 if (new_dentry == trap) 5150 goto exit5; 5151 5152 error = security_path_rename(&old_path, old_dentry, 5153 &new_path, new_dentry, flags); 5154 if (error) 5155 goto exit5; 5156 5157 rd.old_dir = old_path.dentry->d_inode; 5158 rd.old_dentry = old_dentry; 5159 rd.old_mnt_idmap = mnt_idmap(old_path.mnt); 5160 rd.new_dir = new_path.dentry->d_inode; 5161 rd.new_dentry = new_dentry; 5162 rd.new_mnt_idmap = mnt_idmap(new_path.mnt); 5163 rd.delegated_inode = &delegated_inode; 5164 rd.flags = flags; 5165 error = vfs_rename(&rd); 5166 exit5: 5167 dput(new_dentry); 5168 exit4: 5169 dput(old_dentry); 5170 exit3: 5171 unlock_rename(new_path.dentry, old_path.dentry); 5172 exit_lock_rename: 5173 if (delegated_inode) { 5174 error = break_deleg_wait(&delegated_inode); 5175 if (!error) 5176 goto retry_deleg; 5177 } 5178 mnt_drop_write(old_path.mnt); 5179 exit2: 5180 if (retry_estale(error, lookup_flags)) 5181 should_retry = true; 5182 path_put(&new_path); 5183 exit1: 5184 path_put(&old_path); 5185 if (should_retry) { 5186 should_retry = false; 5187 lookup_flags |= LOOKUP_REVAL; 5188 goto retry; 5189 } 5190 put_names: 5191 putname(from); 5192 putname(to); 5193 return error; 5194 } 5195 5196 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, 5197 int, newdfd, const char __user *, newname, unsigned int, flags) 5198 { 5199 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 5200 flags); 5201 } 5202 5203 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 5204 int, newdfd, const char __user *, newname) 5205 { 5206 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 5207 0); 5208 } 5209 5210 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 5211 { 5212 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, 5213 getname(newname), 0); 5214 } 5215 5216 int readlink_copy(char __user *buffer, int buflen, const char *link) 5217 { 5218 int len = PTR_ERR(link); 5219 if (IS_ERR(link)) 5220 goto out; 5221 5222 len = strlen(link); 5223 if (len > (unsigned) buflen) 5224 len = buflen; 5225 if (copy_to_user(buffer, link, len)) 5226 len = -EFAULT; 5227 out: 5228 return len; 5229 } 5230 5231 /** 5232 * vfs_readlink - copy symlink body into userspace buffer 5233 * @dentry: dentry on which to get symbolic link 5234 * @buffer: user memory pointer 5235 * @buflen: size of buffer 5236 * 5237 * Does not touch atime. That's up to the caller if necessary 5238 * 5239 * Does not call security hook. 5240 */ 5241 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) 5242 { 5243 struct inode *inode = d_inode(dentry); 5244 DEFINE_DELAYED_CALL(done); 5245 const char *link; 5246 int res; 5247 5248 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { 5249 if (unlikely(inode->i_op->readlink)) 5250 return inode->i_op->readlink(dentry, buffer, buflen); 5251 5252 if (!d_is_symlink(dentry)) 5253 return -EINVAL; 5254 5255 spin_lock(&inode->i_lock); 5256 inode->i_opflags |= IOP_DEFAULT_READLINK; 5257 spin_unlock(&inode->i_lock); 5258 } 5259 5260 link = READ_ONCE(inode->i_link); 5261 if (!link) { 5262 link = inode->i_op->get_link(dentry, inode, &done); 5263 if (IS_ERR(link)) 5264 return PTR_ERR(link); 5265 } 5266 res = readlink_copy(buffer, buflen, link); 5267 do_delayed_call(&done); 5268 return res; 5269 } 5270 EXPORT_SYMBOL(vfs_readlink); 5271 5272 /** 5273 * vfs_get_link - get symlink body 5274 * @dentry: dentry on which to get symbolic link 5275 * @done: caller needs to free returned data with this 5276 * 5277 * Calls security hook and i_op->get_link() on the supplied inode. 5278 * 5279 * It does not touch atime. That's up to the caller if necessary. 5280 * 5281 * Does not work on "special" symlinks like /proc/$$/fd/N 5282 */ 5283 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) 5284 { 5285 const char *res = ERR_PTR(-EINVAL); 5286 struct inode *inode = d_inode(dentry); 5287 5288 if (d_is_symlink(dentry)) { 5289 res = ERR_PTR(security_inode_readlink(dentry)); 5290 if (!res) 5291 res = inode->i_op->get_link(dentry, inode, done); 5292 } 5293 return res; 5294 } 5295 EXPORT_SYMBOL(vfs_get_link); 5296 5297 /* get the link contents into pagecache */ 5298 const char *page_get_link(struct dentry *dentry, struct inode *inode, 5299 struct delayed_call *callback) 5300 { 5301 char *kaddr; 5302 struct page *page; 5303 struct address_space *mapping = inode->i_mapping; 5304 5305 if (!dentry) { 5306 page = find_get_page(mapping, 0); 5307 if (!page) 5308 return ERR_PTR(-ECHILD); 5309 if (!PageUptodate(page)) { 5310 put_page(page); 5311 return ERR_PTR(-ECHILD); 5312 } 5313 } else { 5314 page = read_mapping_page(mapping, 0, NULL); 5315 if (IS_ERR(page)) 5316 return (char*)page; 5317 } 5318 set_delayed_call(callback, page_put_link, page); 5319 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); 5320 kaddr = page_address(page); 5321 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); 5322 return kaddr; 5323 } 5324 5325 EXPORT_SYMBOL(page_get_link); 5326 5327 void page_put_link(void *arg) 5328 { 5329 put_page(arg); 5330 } 5331 EXPORT_SYMBOL(page_put_link); 5332 5333 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 5334 { 5335 DEFINE_DELAYED_CALL(done); 5336 int res = readlink_copy(buffer, buflen, 5337 page_get_link(dentry, d_inode(dentry), 5338 &done)); 5339 do_delayed_call(&done); 5340 return res; 5341 } 5342 EXPORT_SYMBOL(page_readlink); 5343 5344 int page_symlink(struct inode *inode, const char *symname, int len) 5345 { 5346 struct address_space *mapping = inode->i_mapping; 5347 const struct address_space_operations *aops = mapping->a_ops; 5348 bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS); 5349 struct folio *folio; 5350 void *fsdata = NULL; 5351 int err; 5352 unsigned int flags; 5353 5354 retry: 5355 if (nofs) 5356 flags = memalloc_nofs_save(); 5357 err = aops->write_begin(NULL, mapping, 0, len-1, &folio, &fsdata); 5358 if (nofs) 5359 memalloc_nofs_restore(flags); 5360 if (err) 5361 goto fail; 5362 5363 memcpy(folio_address(folio), symname, len - 1); 5364 5365 err = aops->write_end(NULL, mapping, 0, len - 1, len - 1, 5366 folio, fsdata); 5367 if (err < 0) 5368 goto fail; 5369 if (err < len-1) 5370 goto retry; 5371 5372 mark_inode_dirty(inode); 5373 return 0; 5374 fail: 5375 return err; 5376 } 5377 EXPORT_SYMBOL(page_symlink); 5378 5379 const struct inode_operations page_symlink_inode_operations = { 5380 .get_link = page_get_link, 5381 }; 5382 EXPORT_SYMBOL(page_symlink_inode_operations); 5383