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