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