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