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