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