1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/namei.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Some corrections by tytso. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 131da177e4SLinus Torvalds * lookup logic. 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/init.h> 19630d9c47SPaul Gortmaker #include <linux/export.h> 2044696908SDavid S. Miller #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/slab.h> 221da177e4SLinus Torvalds #include <linux/fs.h> 231da177e4SLinus Torvalds #include <linux/namei.h> 241da177e4SLinus Torvalds #include <linux/pagemap.h> 250eeca283SRobert Love #include <linux/fsnotify.h> 261da177e4SLinus Torvalds #include <linux/personality.h> 271da177e4SLinus Torvalds #include <linux/security.h> 286146f0d5SMimi Zohar #include <linux/ima.h> 291da177e4SLinus Torvalds #include <linux/syscalls.h> 301da177e4SLinus Torvalds #include <linux/mount.h> 311da177e4SLinus Torvalds #include <linux/audit.h> 3216f7e0feSRandy Dunlap #include <linux/capability.h> 33834f2a4aSTrond Myklebust #include <linux/file.h> 345590ff0dSUlrich Drepper #include <linux/fcntl.h> 3508ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 365ad4e53bSAl Viro #include <linux/fs_struct.h> 37e77819e5SLinus Torvalds #include <linux/posix_acl.h> 3899d263d4SLinus Torvalds #include <linux/hash.h> 392a18da7aSGeorge Spelvin #include <linux/bitops.h> 40aeaa4a79SEric W. Biederman #include <linux/init_task.h> 417c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 421da177e4SLinus Torvalds 43e81e3f4dSEric Paris #include "internal.h" 44c7105365SAl Viro #include "mount.h" 45e81e3f4dSEric Paris 461da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 471da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 481da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 491da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 501da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 531da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 541da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 551da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 561da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 571da177e4SLinus Torvalds * the special cases of the former code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 601da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 611da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 641da177e4SLinus Torvalds * resolution to correspond with current state of the code. 651da177e4SLinus Torvalds * 661da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 671da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 681da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 691da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 701da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 711da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 721da177e4SLinus Torvalds */ 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 751da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 761da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 771da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 781da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 791da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 8025985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 811da177e4SLinus Torvalds * 821da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 831da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 841da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 851da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 861da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 871da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 881da177e4SLinus Torvalds * and in the old Linux semantics. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 921da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 931da177e4SLinus Torvalds * 941da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 951da177e4SLinus Torvalds */ 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 981da177e4SLinus Torvalds * inside the path - always follow. 991da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 1001da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 1011da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 1021da177e4SLinus Torvalds * otherwise - don't follow. 1031da177e4SLinus Torvalds * (applied in that order). 1041da177e4SLinus Torvalds * 1051da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1061da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1071da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1081da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1091da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds /* 1121da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 113a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1141da177e4SLinus Torvalds * any extra contention... 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1181da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1191da177e4SLinus Torvalds * kernel data space before using them.. 1201da177e4SLinus Torvalds * 1211da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1221da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1231da177e4SLinus Torvalds */ 1247950e385SJeff Layton 125fd2f7cb5SAl Viro #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) 12691a27b2aSJeff Layton 12751f39a1fSDavid Drysdale struct filename * 12891a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty) 12991a27b2aSJeff Layton { 13094b5d262SAl Viro struct filename *result; 1317950e385SJeff Layton char *kname; 13294b5d262SAl Viro int len; 1331da177e4SLinus Torvalds 1347ac86265SJeff Layton result = audit_reusename(filename); 1357ac86265SJeff Layton if (result) 1367ac86265SJeff Layton return result; 1377ac86265SJeff Layton 1387950e385SJeff Layton result = __getname(); 1393f9f0aa6SLinus Torvalds if (unlikely(!result)) 1404043cde8SEric Paris return ERR_PTR(-ENOMEM); 1411da177e4SLinus Torvalds 1427950e385SJeff Layton /* 1437950e385SJeff Layton * First, try to embed the struct filename inside the names_cache 1447950e385SJeff Layton * allocation 1457950e385SJeff Layton */ 146fd2f7cb5SAl Viro kname = (char *)result->iname; 14791a27b2aSJeff Layton result->name = kname; 1487950e385SJeff Layton 14994b5d262SAl Viro len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); 15091a27b2aSJeff Layton if (unlikely(len < 0)) { 15194b5d262SAl Viro __putname(result); 15294b5d262SAl Viro return ERR_PTR(len); 15391a27b2aSJeff Layton } 1543f9f0aa6SLinus Torvalds 1557950e385SJeff Layton /* 1567950e385SJeff Layton * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a 1577950e385SJeff Layton * separate struct filename so we can dedicate the entire 1587950e385SJeff Layton * names_cache allocation for the pathname, and re-do the copy from 1597950e385SJeff Layton * userland. 1607950e385SJeff Layton */ 16194b5d262SAl Viro if (unlikely(len == EMBEDDED_NAME_MAX)) { 162fd2f7cb5SAl Viro const size_t size = offsetof(struct filename, iname[1]); 1637950e385SJeff Layton kname = (char *)result; 1647950e385SJeff Layton 165fd2f7cb5SAl Viro /* 166fd2f7cb5SAl Viro * size is chosen that way we to guarantee that 167fd2f7cb5SAl Viro * result->iname[0] is within the same object and that 168fd2f7cb5SAl Viro * kname can't be equal to result->iname, no matter what. 169fd2f7cb5SAl Viro */ 170fd2f7cb5SAl Viro result = kzalloc(size, GFP_KERNEL); 17194b5d262SAl Viro if (unlikely(!result)) { 17294b5d262SAl Viro __putname(kname); 17394b5d262SAl Viro return ERR_PTR(-ENOMEM); 1747950e385SJeff Layton } 1757950e385SJeff Layton result->name = kname; 17694b5d262SAl Viro len = strncpy_from_user(kname, filename, PATH_MAX); 17794b5d262SAl Viro if (unlikely(len < 0)) { 17894b5d262SAl Viro __putname(kname); 17994b5d262SAl Viro kfree(result); 18094b5d262SAl Viro return ERR_PTR(len); 18194b5d262SAl Viro } 18294b5d262SAl Viro if (unlikely(len == PATH_MAX)) { 18394b5d262SAl Viro __putname(kname); 18494b5d262SAl Viro kfree(result); 18594b5d262SAl Viro return ERR_PTR(-ENAMETOOLONG); 18694b5d262SAl Viro } 1877950e385SJeff Layton } 1887950e385SJeff Layton 18994b5d262SAl Viro result->refcnt = 1; 1903f9f0aa6SLinus Torvalds /* The empty path is special. */ 1913f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1923f9f0aa6SLinus Torvalds if (empty) 1931fa1e7f6SAndy Whitcroft *empty = 1; 19494b5d262SAl Viro if (!(flags & LOOKUP_EMPTY)) { 19594b5d262SAl Viro putname(result); 19694b5d262SAl Viro return ERR_PTR(-ENOENT); 1971da177e4SLinus Torvalds } 19894b5d262SAl Viro } 1997950e385SJeff Layton 2007950e385SJeff Layton result->uptr = filename; 201c4ad8f98SLinus Torvalds result->aname = NULL; 2021da177e4SLinus Torvalds audit_getname(result); 2031da177e4SLinus Torvalds return result; 2043f9f0aa6SLinus Torvalds } 2053f9f0aa6SLinus Torvalds 20691a27b2aSJeff Layton struct filename * 20791a27b2aSJeff Layton getname(const char __user * filename) 208f52e0c11SAl Viro { 209f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 210f52e0c11SAl Viro } 211f52e0c11SAl Viro 212c4ad8f98SLinus Torvalds struct filename * 213c4ad8f98SLinus Torvalds getname_kernel(const char * filename) 214c4ad8f98SLinus Torvalds { 215c4ad8f98SLinus Torvalds struct filename *result; 21608518549SPaul Moore int len = strlen(filename) + 1; 217c4ad8f98SLinus Torvalds 218c4ad8f98SLinus Torvalds result = __getname(); 219c4ad8f98SLinus Torvalds if (unlikely(!result)) 220c4ad8f98SLinus Torvalds return ERR_PTR(-ENOMEM); 221c4ad8f98SLinus Torvalds 22208518549SPaul Moore if (len <= EMBEDDED_NAME_MAX) { 223fd2f7cb5SAl Viro result->name = (char *)result->iname; 22408518549SPaul Moore } else if (len <= PATH_MAX) { 22530ce4d19SAl Viro const size_t size = offsetof(struct filename, iname[1]); 22608518549SPaul Moore struct filename *tmp; 22708518549SPaul Moore 22830ce4d19SAl Viro tmp = kmalloc(size, GFP_KERNEL); 22908518549SPaul Moore if (unlikely(!tmp)) { 23008518549SPaul Moore __putname(result); 23108518549SPaul Moore return ERR_PTR(-ENOMEM); 23208518549SPaul Moore } 23308518549SPaul Moore tmp->name = (char *)result; 23408518549SPaul Moore result = tmp; 23508518549SPaul Moore } else { 23608518549SPaul Moore __putname(result); 23708518549SPaul Moore return ERR_PTR(-ENAMETOOLONG); 23808518549SPaul Moore } 23908518549SPaul Moore memcpy((char *)result->name, filename, len); 240c4ad8f98SLinus Torvalds result->uptr = NULL; 241c4ad8f98SLinus Torvalds result->aname = NULL; 24255422d0bSPaul Moore result->refcnt = 1; 243fd3522fdSPaul Moore audit_getname(result); 244c4ad8f98SLinus Torvalds 245c4ad8f98SLinus Torvalds return result; 246c4ad8f98SLinus Torvalds } 247c4ad8f98SLinus Torvalds 24891a27b2aSJeff Layton void putname(struct filename *name) 2491da177e4SLinus Torvalds { 25055422d0bSPaul Moore BUG_ON(name->refcnt <= 0); 25155422d0bSPaul Moore 25255422d0bSPaul Moore if (--name->refcnt > 0) 25355422d0bSPaul Moore return; 25455422d0bSPaul Moore 255fd2f7cb5SAl Viro if (name->name != name->iname) { 25655422d0bSPaul Moore __putname(name->name); 25755422d0bSPaul Moore kfree(name); 25855422d0bSPaul Moore } else 25955422d0bSPaul Moore __putname(name); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 26247291baaSChristian Brauner /** 26347291baaSChristian Brauner * check_acl - perform ACL permission checking 26447291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 26547291baaSChristian Brauner * @inode: inode to check permissions on 26647291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 26747291baaSChristian Brauner * 26847291baaSChristian Brauner * This function performs the ACL permission checking. Since this function 26947291baaSChristian Brauner * retrieve POSIX acls it needs to know whether it is called from a blocking or 27047291baaSChristian Brauner * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 27147291baaSChristian Brauner * 27247291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 27347291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 27447291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 27547291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 27647291baaSChristian Brauner * raw inode simply passs init_user_ns. 27747291baaSChristian Brauner */ 27847291baaSChristian Brauner static int check_acl(struct user_namespace *mnt_userns, 27947291baaSChristian Brauner struct inode *inode, int mask) 280e77819e5SLinus Torvalds { 28184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 282e77819e5SLinus Torvalds struct posix_acl *acl; 283e77819e5SLinus Torvalds 284e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2853567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2863567866bSAl Viro if (!acl) 287e77819e5SLinus Torvalds return -EAGAIN; 2883567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 289b8a7a3a6SAndreas Gruenbacher if (is_uncached_acl(acl)) 290e77819e5SLinus Torvalds return -ECHILD; 29147291baaSChristian Brauner return posix_acl_permission(mnt_userns, inode, acl, mask); 292e77819e5SLinus Torvalds } 293e77819e5SLinus Torvalds 2942982baa2SChristoph Hellwig acl = get_acl(inode, ACL_TYPE_ACCESS); 2954e34e719SChristoph Hellwig if (IS_ERR(acl)) 2964e34e719SChristoph Hellwig return PTR_ERR(acl); 297e77819e5SLinus Torvalds if (acl) { 29847291baaSChristian Brauner int error = posix_acl_permission(mnt_userns, inode, acl, mask); 299e77819e5SLinus Torvalds posix_acl_release(acl); 300e77819e5SLinus Torvalds return error; 301e77819e5SLinus Torvalds } 30284635d68SLinus Torvalds #endif 303e77819e5SLinus Torvalds 304e77819e5SLinus Torvalds return -EAGAIN; 305e77819e5SLinus Torvalds } 306e77819e5SLinus Torvalds 30747291baaSChristian Brauner /** 30847291baaSChristian Brauner * acl_permission_check - perform basic UNIX permission checking 30947291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 31047291baaSChristian Brauner * @inode: inode to check permissions on 31147291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 3125fc475b7SLinus Torvalds * 31347291baaSChristian Brauner * This function performs the basic UNIX permission checking. Since this 31447291baaSChristian Brauner * function may retrieve POSIX acls it needs to know whether it is called from a 31547291baaSChristian Brauner * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 31647291baaSChristian Brauner * 31747291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 31847291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 31947291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 32047291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 32147291baaSChristian Brauner * raw inode simply passs init_user_ns. 3225909ccaaSLinus Torvalds */ 32347291baaSChristian Brauner static int acl_permission_check(struct user_namespace *mnt_userns, 32447291baaSChristian Brauner struct inode *inode, int mask) 3255909ccaaSLinus Torvalds { 32626cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 32747291baaSChristian Brauner kuid_t i_uid; 3285909ccaaSLinus Torvalds 3295fc475b7SLinus Torvalds /* Are we the owner? If so, ACL's don't matter */ 33047291baaSChristian Brauner i_uid = i_uid_into_mnt(mnt_userns, inode); 33147291baaSChristian Brauner if (likely(uid_eq(current_fsuid(), i_uid))) { 3325fc475b7SLinus Torvalds mask &= 7; 3335909ccaaSLinus Torvalds mode >>= 6; 3345fc475b7SLinus Torvalds return (mask & ~mode) ? -EACCES : 0; 3355fc475b7SLinus Torvalds } 3365fc475b7SLinus Torvalds 3375fc475b7SLinus Torvalds /* Do we have ACL's? */ 338e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 33947291baaSChristian Brauner int error = check_acl(mnt_userns, inode, mask); 3405909ccaaSLinus Torvalds if (error != -EAGAIN) 3415909ccaaSLinus Torvalds return error; 3425909ccaaSLinus Torvalds } 3435909ccaaSLinus Torvalds 3445fc475b7SLinus Torvalds /* Only RWX matters for group/other mode bits */ 3455fc475b7SLinus Torvalds mask &= 7; 3465fc475b7SLinus Torvalds 3475fc475b7SLinus Torvalds /* 3485fc475b7SLinus Torvalds * Are the group permissions different from 3495fc475b7SLinus Torvalds * the other permissions in the bits we care 3505fc475b7SLinus Torvalds * about? Need to check group ownership if so. 3515fc475b7SLinus Torvalds */ 3525fc475b7SLinus Torvalds if (mask & (mode ^ (mode >> 3))) { 35347291baaSChristian Brauner kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); 35447291baaSChristian Brauner if (in_group_p(kgid)) 3555909ccaaSLinus Torvalds mode >>= 3; 3565909ccaaSLinus Torvalds } 3575909ccaaSLinus Torvalds 3585fc475b7SLinus Torvalds /* Bits in 'mode' clear that we require? */ 3595fc475b7SLinus Torvalds return (mask & ~mode) ? -EACCES : 0; 3605909ccaaSLinus Torvalds } 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds /** 3631da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 36447291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 3651da177e4SLinus Torvalds * @inode: inode to check access rights for 3665fc475b7SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, 3675fc475b7SLinus Torvalds * %MAY_NOT_BLOCK ...) 3681da177e4SLinus Torvalds * 3691da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 3701da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 3711da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 372b74c79e9SNick Piggin * are used for other things. 373b74c79e9SNick Piggin * 374b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 375b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 376b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 37747291baaSChristian Brauner * 37847291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 37947291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 38047291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 38147291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 38247291baaSChristian Brauner * raw inode simply passs init_user_ns. 3831da177e4SLinus Torvalds */ 38447291baaSChristian Brauner int generic_permission(struct user_namespace *mnt_userns, struct inode *inode, 38547291baaSChristian Brauner int mask) 3861da177e4SLinus Torvalds { 3875909ccaaSLinus Torvalds int ret; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* 390948409c7SAndreas Gruenbacher * Do the basic permission checks. 3911da177e4SLinus Torvalds */ 39247291baaSChristian Brauner ret = acl_permission_check(mnt_userns, inode, mask); 3935909ccaaSLinus Torvalds if (ret != -EACCES) 3945909ccaaSLinus Torvalds return ret; 3951da177e4SLinus Torvalds 396d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 397d594e7ecSAl Viro /* DACs are overridable for directories */ 398d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 39947291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 40023adbe12SAndy Lutomirski CAP_DAC_READ_SEARCH)) 401d594e7ecSAl Viro return 0; 40247291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4030558c1bfSChristian Brauner CAP_DAC_OVERRIDE)) 4041da177e4SLinus Torvalds return 0; 4052a4c2242SStephen Smalley return -EACCES; 4062a4c2242SStephen Smalley } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* 4091da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 4101da177e4SLinus Torvalds */ 4117ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 412d594e7ecSAl Viro if (mask == MAY_READ) 41347291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4140558c1bfSChristian Brauner CAP_DAC_READ_SEARCH)) 4151da177e4SLinus Torvalds return 0; 4162a4c2242SStephen Smalley /* 4172a4c2242SStephen Smalley * Read/write DACs are always overridable. 4182a4c2242SStephen Smalley * Executable DACs are overridable when there is 4192a4c2242SStephen Smalley * at least one exec bit set. 4202a4c2242SStephen Smalley */ 4212a4c2242SStephen Smalley if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 42247291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4230558c1bfSChristian Brauner CAP_DAC_OVERRIDE)) 4242a4c2242SStephen Smalley return 0; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds return -EACCES; 4271da177e4SLinus Torvalds } 4284d359507SAl Viro EXPORT_SYMBOL(generic_permission); 4291da177e4SLinus Torvalds 43047291baaSChristian Brauner /** 43147291baaSChristian Brauner * do_inode_permission - UNIX permission checking 43247291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 43347291baaSChristian Brauner * @inode: inode to check permissions on 43447291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 43547291baaSChristian Brauner * 4363ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 4373ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 4383ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 4393ddcd056SLinus Torvalds * permission function, use the fast case". 4403ddcd056SLinus Torvalds */ 44147291baaSChristian Brauner static inline int do_inode_permission(struct user_namespace *mnt_userns, 44247291baaSChristian Brauner struct inode *inode, int mask) 4433ddcd056SLinus Torvalds { 4443ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 4453ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 446549c7297SChristian Brauner return inode->i_op->permission(mnt_userns, inode, mask); 4473ddcd056SLinus Torvalds 4483ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 4493ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 4503ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 4513ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 4523ddcd056SLinus Torvalds } 45347291baaSChristian Brauner return generic_permission(mnt_userns, inode, mask); 4543ddcd056SLinus Torvalds } 4553ddcd056SLinus Torvalds 456cb23beb5SChristoph Hellwig /** 4570bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 4580bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 45955852635SRandy Dunlap * @inode: Inode to check permission on 4600bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4610bdaea90SDavid Howells * 4620bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 4630bdaea90SDavid Howells */ 4640bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 4650bdaea90SDavid Howells { 4660bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 4670bdaea90SDavid Howells umode_t mode = inode->i_mode; 4680bdaea90SDavid Howells 4690bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 470bc98a42cSDavid Howells if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 4710bdaea90SDavid Howells return -EROFS; 4720bdaea90SDavid Howells } 4730bdaea90SDavid Howells return 0; 4740bdaea90SDavid Howells } 4750bdaea90SDavid Howells 4760bdaea90SDavid Howells /** 4770bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 47847291baaSChristian Brauner * @mnt_userns: User namespace of the mount the inode was found from 4790bdaea90SDavid Howells * @inode: Inode to check permission on 4800bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4810bdaea90SDavid Howells * 4820bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4830bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4840bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4850bdaea90SDavid Howells * 4860bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4870bdaea90SDavid Howells */ 48847291baaSChristian Brauner int inode_permission(struct user_namespace *mnt_userns, 48947291baaSChristian Brauner struct inode *inode, int mask) 4900bdaea90SDavid Howells { 4910bdaea90SDavid Howells int retval; 4920bdaea90SDavid Howells 4930bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4940bdaea90SDavid Howells if (retval) 4950bdaea90SDavid Howells return retval; 4964bfd054aSEric Biggers 4974bfd054aSEric Biggers if (unlikely(mask & MAY_WRITE)) { 4984bfd054aSEric Biggers /* 4994bfd054aSEric Biggers * Nobody gets write access to an immutable file. 5004bfd054aSEric Biggers */ 5014bfd054aSEric Biggers if (IS_IMMUTABLE(inode)) 5024bfd054aSEric Biggers return -EPERM; 5034bfd054aSEric Biggers 5044bfd054aSEric Biggers /* 5054bfd054aSEric Biggers * Updating mtime will likely cause i_uid and i_gid to be 5064bfd054aSEric Biggers * written back improperly if their true value is unknown 5074bfd054aSEric Biggers * to the vfs. 5084bfd054aSEric Biggers */ 509ba73d987SChristian Brauner if (HAS_UNMAPPED_ID(mnt_userns, inode)) 5104bfd054aSEric Biggers return -EACCES; 5114bfd054aSEric Biggers } 5124bfd054aSEric Biggers 51347291baaSChristian Brauner retval = do_inode_permission(mnt_userns, inode, mask); 5144bfd054aSEric Biggers if (retval) 5154bfd054aSEric Biggers return retval; 5164bfd054aSEric Biggers 5174bfd054aSEric Biggers retval = devcgroup_inode_permission(inode, mask); 5184bfd054aSEric Biggers if (retval) 5194bfd054aSEric Biggers return retval; 5204bfd054aSEric Biggers 5214bfd054aSEric Biggers return security_inode_permission(inode, mask); 5220bdaea90SDavid Howells } 5234d359507SAl Viro EXPORT_SYMBOL(inode_permission); 5240bdaea90SDavid Howells 5250bdaea90SDavid Howells /** 5265dd784d0SJan Blunck * path_get - get a reference to a path 5275dd784d0SJan Blunck * @path: path to get the reference to 5285dd784d0SJan Blunck * 5295dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 5305dd784d0SJan Blunck */ 531dcf787f3SAl Viro void path_get(const struct path *path) 5325dd784d0SJan Blunck { 5335dd784d0SJan Blunck mntget(path->mnt); 5345dd784d0SJan Blunck dget(path->dentry); 5355dd784d0SJan Blunck } 5365dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 5375dd784d0SJan Blunck 5385dd784d0SJan Blunck /** 5391d957f9bSJan Blunck * path_put - put a reference to a path 5401d957f9bSJan Blunck * @path: path to put the reference to 5411d957f9bSJan Blunck * 5421d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 5431d957f9bSJan Blunck */ 544dcf787f3SAl Viro void path_put(const struct path *path) 5451da177e4SLinus Torvalds { 5461d957f9bSJan Blunck dput(path->dentry); 5471d957f9bSJan Blunck mntput(path->mnt); 5481da177e4SLinus Torvalds } 5491d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 5501da177e4SLinus Torvalds 551894bc8c4SAl Viro #define EMBEDDED_LEVELS 2 5521f55a6ecSAl Viro struct nameidata { 5531f55a6ecSAl Viro struct path path; 5541f55a6ecSAl Viro struct qstr last; 5551f55a6ecSAl Viro struct path root; 5561f55a6ecSAl Viro struct inode *inode; /* path.dentry.d_inode */ 5571f55a6ecSAl Viro unsigned int flags; 558ab87f9a5SAleksa Sarai unsigned seq, m_seq, r_seq; 5591f55a6ecSAl Viro int last_type; 5601f55a6ecSAl Viro unsigned depth; 561756daf26SNeilBrown int total_link_count; 562697fc6caSAl Viro struct saved { 563697fc6caSAl Viro struct path link; 564fceef393SAl Viro struct delayed_call done; 565697fc6caSAl Viro const char *name; 5660450b2d1SAl Viro unsigned seq; 567894bc8c4SAl Viro } *stack, internal[EMBEDDED_LEVELS]; 5689883d185SAl Viro struct filename *name; 5699883d185SAl Viro struct nameidata *saved; 5709883d185SAl Viro unsigned root_seq; 5719883d185SAl Viro int dfd; 5720f705953SAl Viro kuid_t dir_uid; 5730f705953SAl Viro umode_t dir_mode; 5743859a271SKees Cook } __randomize_layout; 5751f55a6ecSAl Viro 5769883d185SAl Viro static void set_nameidata(struct nameidata *p, int dfd, struct filename *name) 577894bc8c4SAl Viro { 578756daf26SNeilBrown struct nameidata *old = current->nameidata; 579756daf26SNeilBrown p->stack = p->internal; 580c8a53ee5SAl Viro p->dfd = dfd; 581c8a53ee5SAl Viro p->name = name; 582756daf26SNeilBrown p->total_link_count = old ? old->total_link_count : 0; 5839883d185SAl Viro p->saved = old; 584756daf26SNeilBrown current->nameidata = p; 585894bc8c4SAl Viro } 586894bc8c4SAl Viro 5879883d185SAl Viro static void restore_nameidata(void) 588894bc8c4SAl Viro { 5899883d185SAl Viro struct nameidata *now = current->nameidata, *old = now->saved; 590756daf26SNeilBrown 591756daf26SNeilBrown current->nameidata = old; 592756daf26SNeilBrown if (old) 593756daf26SNeilBrown old->total_link_count = now->total_link_count; 594e1a63bbcSAl Viro if (now->stack != now->internal) 595756daf26SNeilBrown kfree(now->stack); 596894bc8c4SAl Viro } 597894bc8c4SAl Viro 59860ef60c7SAl Viro static bool nd_alloc_stack(struct nameidata *nd) 599894bc8c4SAl Viro { 600bc40aee0SAl Viro struct saved *p; 601bc40aee0SAl Viro 6026da2ec56SKees Cook p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), 60360ef60c7SAl Viro nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); 604bc40aee0SAl Viro if (unlikely(!p)) 60560ef60c7SAl Viro return false; 606894bc8c4SAl Viro memcpy(p, nd->internal, sizeof(nd->internal)); 607894bc8c4SAl Viro nd->stack = p; 60860ef60c7SAl Viro return true; 609894bc8c4SAl Viro } 610894bc8c4SAl Viro 611397d425dSEric W. Biederman /** 6126b03f7edSAl Viro * path_connected - Verify that a dentry is below mnt.mnt_root 613397d425dSEric W. Biederman * 614397d425dSEric W. Biederman * Rename can sometimes move a file or directory outside of a bind 615397d425dSEric W. Biederman * mount, path_connected allows those cases to be detected. 616397d425dSEric W. Biederman */ 6176b03f7edSAl Viro static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) 618397d425dSEric W. Biederman { 61995dd7758SEric W. Biederman struct super_block *sb = mnt->mnt_sb; 620397d425dSEric W. Biederman 621402dd2cfSChristoph Hellwig /* Bind mounts can have disconnected paths */ 622402dd2cfSChristoph Hellwig if (mnt->mnt_root == sb->s_root) 623397d425dSEric W. Biederman return true; 624397d425dSEric W. Biederman 6256b03f7edSAl Viro return is_subdir(dentry, mnt->mnt_root); 626397d425dSEric W. Biederman } 627397d425dSEric W. Biederman 6287973387aSAl Viro static void drop_links(struct nameidata *nd) 6297973387aSAl Viro { 6307973387aSAl Viro int i = nd->depth; 6317973387aSAl Viro while (i--) { 6327973387aSAl Viro struct saved *last = nd->stack + i; 633fceef393SAl Viro do_delayed_call(&last->done); 634fceef393SAl Viro clear_delayed_call(&last->done); 6357973387aSAl Viro } 6367973387aSAl Viro } 6377973387aSAl Viro 6387973387aSAl Viro static void terminate_walk(struct nameidata *nd) 6397973387aSAl Viro { 6407973387aSAl Viro drop_links(nd); 6417973387aSAl Viro if (!(nd->flags & LOOKUP_RCU)) { 6427973387aSAl Viro int i; 6437973387aSAl Viro path_put(&nd->path); 6447973387aSAl Viro for (i = 0; i < nd->depth; i++) 6457973387aSAl Viro path_put(&nd->stack[i].link); 64684a2bd39SAl Viro if (nd->flags & LOOKUP_ROOT_GRABBED) { 647102b8af2SAl Viro path_put(&nd->root); 64884a2bd39SAl Viro nd->flags &= ~LOOKUP_ROOT_GRABBED; 649102b8af2SAl Viro } 6507973387aSAl Viro } else { 6517973387aSAl Viro nd->flags &= ~LOOKUP_RCU; 6527973387aSAl Viro rcu_read_unlock(); 6537973387aSAl Viro } 6547973387aSAl Viro nd->depth = 0; 6557973387aSAl Viro } 6567973387aSAl Viro 6577973387aSAl Viro /* path_put is needed afterwards regardless of success or failure */ 6582aa38470SAl Viro static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) 6597973387aSAl Viro { 6602aa38470SAl Viro int res = __legitimize_mnt(path->mnt, mseq); 6617973387aSAl Viro if (unlikely(res)) { 6627973387aSAl Viro if (res > 0) 6637973387aSAl Viro path->mnt = NULL; 6647973387aSAl Viro path->dentry = NULL; 6657973387aSAl Viro return false; 6667973387aSAl Viro } 6677973387aSAl Viro if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { 6687973387aSAl Viro path->dentry = NULL; 6697973387aSAl Viro return false; 6707973387aSAl Viro } 6717973387aSAl Viro return !read_seqcount_retry(&path->dentry->d_seq, seq); 6727973387aSAl Viro } 6737973387aSAl Viro 6742aa38470SAl Viro static inline bool legitimize_path(struct nameidata *nd, 6752aa38470SAl Viro struct path *path, unsigned seq) 6762aa38470SAl Viro { 6775bd73286SAl Viro return __legitimize_path(path, seq, nd->m_seq); 6782aa38470SAl Viro } 6792aa38470SAl Viro 6807973387aSAl Viro static bool legitimize_links(struct nameidata *nd) 6817973387aSAl Viro { 6827973387aSAl Viro int i; 683eacd9aa8SAl Viro if (unlikely(nd->flags & LOOKUP_CACHED)) { 684eacd9aa8SAl Viro drop_links(nd); 685eacd9aa8SAl Viro nd->depth = 0; 686eacd9aa8SAl Viro return false; 687eacd9aa8SAl Viro } 6887973387aSAl Viro for (i = 0; i < nd->depth; i++) { 6897973387aSAl Viro struct saved *last = nd->stack + i; 6907973387aSAl Viro if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { 6917973387aSAl Viro drop_links(nd); 6927973387aSAl Viro nd->depth = i + 1; 6937973387aSAl Viro return false; 6947973387aSAl Viro } 6957973387aSAl Viro } 6967973387aSAl Viro return true; 6977973387aSAl Viro } 6987973387aSAl Viro 699ee594bffSAl Viro static bool legitimize_root(struct nameidata *nd) 700ee594bffSAl Viro { 701adb21d2bSAleksa Sarai /* 702adb21d2bSAleksa Sarai * For scoped-lookups (where nd->root has been zeroed), we need to 703adb21d2bSAleksa Sarai * restart the whole lookup from scratch -- because set_root() is wrong 704adb21d2bSAleksa Sarai * for these lookups (nd->dfd is the root, not the filesystem root). 705adb21d2bSAleksa Sarai */ 706adb21d2bSAleksa Sarai if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) 707adb21d2bSAleksa Sarai return false; 708adb21d2bSAleksa Sarai /* Nothing to do if nd->root is zero or is managed by the VFS user. */ 709ee594bffSAl Viro if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT)) 710ee594bffSAl Viro return true; 71184a2bd39SAl Viro nd->flags |= LOOKUP_ROOT_GRABBED; 712ee594bffSAl Viro return legitimize_path(nd, &nd->root, nd->root_seq); 713ee594bffSAl Viro } 714ee594bffSAl Viro 71519660af7SAl Viro /* 71631e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 71719660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 71819660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 71957e3715cSMike Marshall * normal reference counts on dentries and vfsmounts to transition to ref-walk 72019660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 72119660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 72219660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 72319660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 72431e6b01fSNick Piggin */ 72531e6b01fSNick Piggin 72631e6b01fSNick Piggin /** 727e36cffedSJens Axboe * try_to_unlazy - try to switch to ref-walk mode. 72819660af7SAl Viro * @nd: nameidata pathwalk data 729e36cffedSJens Axboe * Returns: true on success, false on failure 73031e6b01fSNick Piggin * 731e36cffedSJens Axboe * try_to_unlazy attempts to legitimize the current nd->path and nd->root 7324675ac39SAl Viro * for ref-walk mode. 7334675ac39SAl Viro * Must be called from rcu-walk context. 734e36cffedSJens Axboe * Nothing should touch nameidata between try_to_unlazy() failure and 7357973387aSAl Viro * terminate_walk(). 73631e6b01fSNick Piggin */ 737e36cffedSJens Axboe static bool try_to_unlazy(struct nameidata *nd) 73831e6b01fSNick Piggin { 73931e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 74031e6b01fSNick Piggin 74131e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 742e5c832d5SLinus Torvalds 743e5c832d5SLinus Torvalds nd->flags &= ~LOOKUP_RCU; 7447973387aSAl Viro if (unlikely(!legitimize_links(nd))) 7454675ac39SAl Viro goto out1; 74684a2bd39SAl Viro if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) 74784a2bd39SAl Viro goto out; 748ee594bffSAl Viro if (unlikely(!legitimize_root(nd))) 7494675ac39SAl Viro goto out; 7504675ac39SAl Viro rcu_read_unlock(); 7514675ac39SAl Viro BUG_ON(nd->inode != parent->d_inode); 752e36cffedSJens Axboe return true; 7534675ac39SAl Viro 75484a2bd39SAl Viro out1: 7554675ac39SAl Viro nd->path.mnt = NULL; 7564675ac39SAl Viro nd->path.dentry = NULL; 7574675ac39SAl Viro out: 7584675ac39SAl Viro rcu_read_unlock(); 759e36cffedSJens Axboe return false; 7604675ac39SAl Viro } 7614675ac39SAl Viro 7624675ac39SAl Viro /** 763ae66db45SAl Viro * try_to_unlazy_next - try to switch to ref-walk mode. 7644675ac39SAl Viro * @nd: nameidata pathwalk data 765ae66db45SAl Viro * @dentry: next dentry to step into 766ae66db45SAl Viro * @seq: seq number to check @dentry against 767ae66db45SAl Viro * Returns: true on success, false on failure 7684675ac39SAl Viro * 769ae66db45SAl Viro * Similar to to try_to_unlazy(), but here we have the next dentry already 770ae66db45SAl Viro * picked by rcu-walk and want to legitimize that in addition to the current 771ae66db45SAl Viro * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. 772ae66db45SAl Viro * Nothing should touch nameidata between try_to_unlazy_next() failure and 7734675ac39SAl Viro * terminate_walk(). 7744675ac39SAl Viro */ 775ae66db45SAl Viro static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq) 7764675ac39SAl Viro { 7774675ac39SAl Viro BUG_ON(!(nd->flags & LOOKUP_RCU)); 7784675ac39SAl Viro 7794675ac39SAl Viro nd->flags &= ~LOOKUP_RCU; 7804675ac39SAl Viro if (unlikely(!legitimize_links(nd))) 7814675ac39SAl Viro goto out2; 7827973387aSAl Viro if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) 7837973387aSAl Viro goto out2; 7844675ac39SAl Viro if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) 7857973387aSAl Viro goto out1; 78648a066e7SAl Viro 78715570086SLinus Torvalds /* 7884675ac39SAl Viro * We need to move both the parent and the dentry from the RCU domain 7894675ac39SAl Viro * to be properly refcounted. And the sequence number in the dentry 7904675ac39SAl Viro * validates *both* dentry counters, since we checked the sequence 7914675ac39SAl Viro * number of the parent after we got the child sequence number. So we 7924675ac39SAl Viro * know the parent must still be valid if the child sequence number is 79315570086SLinus Torvalds */ 7944675ac39SAl Viro if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) 795e5c832d5SLinus Torvalds goto out; 79684a2bd39SAl Viro if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) 79784a2bd39SAl Viro goto out_dput; 798e5c832d5SLinus Torvalds /* 799e5c832d5SLinus Torvalds * Sequence counts matched. Now make sure that the root is 800e5c832d5SLinus Torvalds * still valid and get it if required. 801e5c832d5SLinus Torvalds */ 80284a2bd39SAl Viro if (unlikely(!legitimize_root(nd))) 80384a2bd39SAl Viro goto out_dput; 8048b61e74fSAl Viro rcu_read_unlock(); 805ae66db45SAl Viro return true; 80619660af7SAl Viro 8077973387aSAl Viro out2: 8087973387aSAl Viro nd->path.mnt = NULL; 8097973387aSAl Viro out1: 8107973387aSAl Viro nd->path.dentry = NULL; 811e5c832d5SLinus Torvalds out: 8128b61e74fSAl Viro rcu_read_unlock(); 813ae66db45SAl Viro return false; 81484a2bd39SAl Viro out_dput: 81584a2bd39SAl Viro rcu_read_unlock(); 81684a2bd39SAl Viro dput(dentry); 817ae66db45SAl Viro return false; 81831e6b01fSNick Piggin } 81931e6b01fSNick Piggin 8204ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 82134286d66SNick Piggin { 822a89f8337SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) 8234ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 824a89f8337SAl Viro else 825a89f8337SAl Viro return 1; 82634286d66SNick Piggin } 82734286d66SNick Piggin 8289f1fafeeSAl Viro /** 8299f1fafeeSAl Viro * complete_walk - successful completion of path walk 8309f1fafeeSAl Viro * @nd: pointer nameidata 83139159de2SJeff Layton * 8329f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 8339f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 8349f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 8359f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 8369f1fafeeSAl Viro * need to drop nd->path. 83739159de2SJeff Layton */ 8389f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 83939159de2SJeff Layton { 84016c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 84139159de2SJeff Layton int status; 84239159de2SJeff Layton 8439f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 844adb21d2bSAleksa Sarai /* 845adb21d2bSAleksa Sarai * We don't want to zero nd->root for scoped-lookups or 846adb21d2bSAleksa Sarai * externally-managed nd->root. 847adb21d2bSAleksa Sarai */ 848adb21d2bSAleksa Sarai if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) 8499f1fafeeSAl Viro nd->root.mnt = NULL; 8506c6ec2b0SJens Axboe nd->flags &= ~LOOKUP_CACHED; 851e36cffedSJens Axboe if (!try_to_unlazy(nd)) 85248a066e7SAl Viro return -ECHILD; 85348a066e7SAl Viro } 8549f1fafeeSAl Viro 855adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 856adb21d2bSAleksa Sarai /* 857adb21d2bSAleksa Sarai * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't 858adb21d2bSAleksa Sarai * ever step outside the root during lookup" and should already 859adb21d2bSAleksa Sarai * be guaranteed by the rest of namei, we want to avoid a namei 860adb21d2bSAleksa Sarai * BUG resulting in userspace being given a path that was not 861adb21d2bSAleksa Sarai * scoped within the root at some point during the lookup. 862adb21d2bSAleksa Sarai * 863adb21d2bSAleksa Sarai * So, do a final sanity-check to make sure that in the 864adb21d2bSAleksa Sarai * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) 865adb21d2bSAleksa Sarai * we won't silently return an fd completely outside of the 866adb21d2bSAleksa Sarai * requested root to userspace. 867adb21d2bSAleksa Sarai * 868adb21d2bSAleksa Sarai * Userspace could move the path outside the root after this 869adb21d2bSAleksa Sarai * check, but as discussed elsewhere this is not a concern (the 870adb21d2bSAleksa Sarai * resolved file was inside the root at some point). 871adb21d2bSAleksa Sarai */ 872adb21d2bSAleksa Sarai if (!path_is_under(&nd->path, &nd->root)) 873adb21d2bSAleksa Sarai return -EXDEV; 874adb21d2bSAleksa Sarai } 875adb21d2bSAleksa Sarai 87616c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 87739159de2SJeff Layton return 0; 87839159de2SJeff Layton 879ecf3d1f1SJeff Layton if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 88016c2cd71SAl Viro return 0; 88116c2cd71SAl Viro 882ecf3d1f1SJeff Layton status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 88339159de2SJeff Layton if (status > 0) 88439159de2SJeff Layton return 0; 88539159de2SJeff Layton 88616c2cd71SAl Viro if (!status) 88739159de2SJeff Layton status = -ESTALE; 88816c2cd71SAl Viro 88939159de2SJeff Layton return status; 89039159de2SJeff Layton } 89139159de2SJeff Layton 892740a1678SAleksa Sarai static int set_root(struct nameidata *nd) 8932a737871SAl Viro { 89431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 8959e6697e2SAl Viro 896adb21d2bSAleksa Sarai /* 897adb21d2bSAleksa Sarai * Jumping to the real root in a scoped-lookup is a BUG in namei, but we 898adb21d2bSAleksa Sarai * still have to ensure it doesn't happen because it will cause a breakout 899adb21d2bSAleksa Sarai * from the dirfd. 900adb21d2bSAleksa Sarai */ 901adb21d2bSAleksa Sarai if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) 902adb21d2bSAleksa Sarai return -ENOTRECOVERABLE; 903adb21d2bSAleksa Sarai 9049e6697e2SAl Viro if (nd->flags & LOOKUP_RCU) { 9058f47a016SAl Viro unsigned seq; 906c28cc364SNick Piggin 907c28cc364SNick Piggin do { 908c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 90931e6b01fSNick Piggin nd->root = fs->root; 9108f47a016SAl Viro nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 911c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 9129e6697e2SAl Viro } else { 9139e6697e2SAl Viro get_fs_root(fs, &nd->root); 91484a2bd39SAl Viro nd->flags |= LOOKUP_ROOT_GRABBED; 9159e6697e2SAl Viro } 916740a1678SAleksa Sarai return 0; 91731e6b01fSNick Piggin } 91831e6b01fSNick Piggin 919248fb5b9SAl Viro static int nd_jump_root(struct nameidata *nd) 920248fb5b9SAl Viro { 921adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_BENEATH)) 922adb21d2bSAleksa Sarai return -EXDEV; 92372ba2929SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 92472ba2929SAleksa Sarai /* Absolute path arguments to path_init() are allowed. */ 92572ba2929SAleksa Sarai if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) 92672ba2929SAleksa Sarai return -EXDEV; 92772ba2929SAleksa Sarai } 928740a1678SAleksa Sarai if (!nd->root.mnt) { 929740a1678SAleksa Sarai int error = set_root(nd); 930740a1678SAleksa Sarai if (error) 931740a1678SAleksa Sarai return error; 932740a1678SAleksa Sarai } 933248fb5b9SAl Viro if (nd->flags & LOOKUP_RCU) { 934248fb5b9SAl Viro struct dentry *d; 935248fb5b9SAl Viro nd->path = nd->root; 936248fb5b9SAl Viro d = nd->path.dentry; 937248fb5b9SAl Viro nd->inode = d->d_inode; 938248fb5b9SAl Viro nd->seq = nd->root_seq; 939248fb5b9SAl Viro if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) 940248fb5b9SAl Viro return -ECHILD; 941248fb5b9SAl Viro } else { 942248fb5b9SAl Viro path_put(&nd->path); 943248fb5b9SAl Viro nd->path = nd->root; 944248fb5b9SAl Viro path_get(&nd->path); 945248fb5b9SAl Viro nd->inode = nd->path.dentry->d_inode; 946248fb5b9SAl Viro } 947248fb5b9SAl Viro nd->flags |= LOOKUP_JUMPED; 948248fb5b9SAl Viro return 0; 949248fb5b9SAl Viro } 950248fb5b9SAl Viro 951b5fb63c1SChristoph Hellwig /* 9526b255391SAl Viro * Helper to directly jump to a known parsed path from ->get_link, 953b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 954b5fb63c1SChristoph Hellwig */ 9551bc82070SAleksa Sarai int nd_jump_link(struct path *path) 956b5fb63c1SChristoph Hellwig { 9574b99d499SAleksa Sarai int error = -ELOOP; 9586e77137bSAl Viro struct nameidata *nd = current->nameidata; 959b5fb63c1SChristoph Hellwig 9604b99d499SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) 9614b99d499SAleksa Sarai goto err; 9624b99d499SAleksa Sarai 96372ba2929SAleksa Sarai error = -EXDEV; 96472ba2929SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 96572ba2929SAleksa Sarai if (nd->path.mnt != path->mnt) 96672ba2929SAleksa Sarai goto err; 96772ba2929SAleksa Sarai } 968adb21d2bSAleksa Sarai /* Not currently safe for scoped-lookups. */ 969adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) 970adb21d2bSAleksa Sarai goto err; 97172ba2929SAleksa Sarai 9724b99d499SAleksa Sarai path_put(&nd->path); 973b5fb63c1SChristoph Hellwig nd->path = *path; 974b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 975b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 9761bc82070SAleksa Sarai return 0; 9774b99d499SAleksa Sarai 9784b99d499SAleksa Sarai err: 9794b99d499SAleksa Sarai path_put(path); 9804b99d499SAleksa Sarai return error; 981b5fb63c1SChristoph Hellwig } 982b5fb63c1SChristoph Hellwig 983b9ff4429SAl Viro static inline void put_link(struct nameidata *nd) 984574197e0SAl Viro { 98521c3003dSAl Viro struct saved *last = nd->stack + --nd->depth; 986fceef393SAl Viro do_delayed_call(&last->done); 9876548fae2SAl Viro if (!(nd->flags & LOOKUP_RCU)) 988b9ff4429SAl Viro path_put(&last->link); 989574197e0SAl Viro } 990574197e0SAl Viro 991561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 992561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 99330aba665SSalvatore Mesoraca int sysctl_protected_fifos __read_mostly; 99430aba665SSalvatore Mesoraca int sysctl_protected_regular __read_mostly; 995800179c9SKees Cook 996800179c9SKees Cook /** 997800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 99855852635SRandy Dunlap * @nd: nameidata pathwalk data 999800179c9SKees Cook * 1000800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 1001800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 1002800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 1003800179c9SKees Cook * processes from failing races against path names that may change out 1004800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 1005800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 1006800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 1007800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 1008800179c9SKees Cook * 1009800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 1010800179c9SKees Cook */ 1011ad6cc4c3SAl Viro static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) 1012800179c9SKees Cook { 1013ba73d987SChristian Brauner struct user_namespace *mnt_userns; 1014ba73d987SChristian Brauner kuid_t i_uid; 1015ba73d987SChristian Brauner 1016800179c9SKees Cook if (!sysctl_protected_symlinks) 1017800179c9SKees Cook return 0; 1018800179c9SKees Cook 1019ba73d987SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 1020ba73d987SChristian Brauner i_uid = i_uid_into_mnt(mnt_userns, inode); 1021800179c9SKees Cook /* Allowed if owner and follower match. */ 1022ba73d987SChristian Brauner if (uid_eq(current_cred()->fsuid, i_uid)) 1023800179c9SKees Cook return 0; 1024800179c9SKees Cook 1025800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 10260f705953SAl Viro if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 1027800179c9SKees Cook return 0; 1028800179c9SKees Cook 1029800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 1030ba73d987SChristian Brauner if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid)) 1031800179c9SKees Cook return 0; 1032800179c9SKees Cook 103331956502SAl Viro if (nd->flags & LOOKUP_RCU) 103431956502SAl Viro return -ECHILD; 103531956502SAl Viro 1036ea841bafSRichard Guy Briggs audit_inode(nd->name, nd->stack[0].link.dentry, 0); 1037245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); 1038800179c9SKees Cook return -EACCES; 1039800179c9SKees Cook } 1040800179c9SKees Cook 1041800179c9SKees Cook /** 1042800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 1043ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1044800179c9SKees Cook * @inode: the source inode to hardlink from 1045800179c9SKees Cook * 1046800179c9SKees Cook * Return false if at least one of the following conditions: 1047800179c9SKees Cook * - inode is not a regular file 1048800179c9SKees Cook * - inode is setuid 1049800179c9SKees Cook * - inode is setgid and group-exec 1050800179c9SKees Cook * - access failure for read and write 1051800179c9SKees Cook * 1052800179c9SKees Cook * Otherwise returns true. 1053800179c9SKees Cook */ 1054ba73d987SChristian Brauner static bool safe_hardlink_source(struct user_namespace *mnt_userns, 1055ba73d987SChristian Brauner struct inode *inode) 1056800179c9SKees Cook { 1057800179c9SKees Cook umode_t mode = inode->i_mode; 1058800179c9SKees Cook 1059800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 1060800179c9SKees Cook if (!S_ISREG(mode)) 1061800179c9SKees Cook return false; 1062800179c9SKees Cook 1063800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 1064800179c9SKees Cook if (mode & S_ISUID) 1065800179c9SKees Cook return false; 1066800179c9SKees Cook 1067800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 1068800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 1069800179c9SKees Cook return false; 1070800179c9SKees Cook 1071800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 1072ba73d987SChristian Brauner if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE)) 1073800179c9SKees Cook return false; 1074800179c9SKees Cook 1075800179c9SKees Cook return true; 1076800179c9SKees Cook } 1077800179c9SKees Cook 1078800179c9SKees Cook /** 1079800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 1080ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1081800179c9SKees Cook * @link: the source to hardlink from 1082800179c9SKees Cook * 1083800179c9SKees Cook * Block hardlink when all of: 1084800179c9SKees Cook * - sysctl_protected_hardlinks enabled 1085800179c9SKees Cook * - fsuid does not match inode 1086800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 1087f2ca3796SDirk Steinmetz * - not CAP_FOWNER in a namespace with the inode owner uid mapped 1088800179c9SKees Cook * 1089ba73d987SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 1090ba73d987SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 1091ba73d987SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 1092ba73d987SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 1093ba73d987SChristian Brauner * raw inode simply passs init_user_ns. 1094ba73d987SChristian Brauner * 1095800179c9SKees Cook * Returns 0 if successful, -ve on error. 1096800179c9SKees Cook */ 1097ba73d987SChristian Brauner int may_linkat(struct user_namespace *mnt_userns, struct path *link) 1098800179c9SKees Cook { 1099593d1ce8SEric W. Biederman struct inode *inode = link->dentry->d_inode; 1100593d1ce8SEric W. Biederman 1101593d1ce8SEric W. Biederman /* Inode writeback is not safe when the uid or gid are invalid. */ 1102ba73d987SChristian Brauner if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || 1103ba73d987SChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 1104593d1ce8SEric W. Biederman return -EOVERFLOW; 1105800179c9SKees Cook 1106800179c9SKees Cook if (!sysctl_protected_hardlinks) 1107800179c9SKees Cook return 0; 1108800179c9SKees Cook 1109800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 1110800179c9SKees Cook * otherwise, it must be a safe source. 1111800179c9SKees Cook */ 1112ba73d987SChristian Brauner if (safe_hardlink_source(mnt_userns, inode) || 1113ba73d987SChristian Brauner inode_owner_or_capable(mnt_userns, inode)) 1114800179c9SKees Cook return 0; 1115800179c9SKees Cook 1116245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); 1117800179c9SKees Cook return -EPERM; 1118800179c9SKees Cook } 1119800179c9SKees Cook 112030aba665SSalvatore Mesoraca /** 112130aba665SSalvatore Mesoraca * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory 112230aba665SSalvatore Mesoraca * should be allowed, or not, on files that already 112330aba665SSalvatore Mesoraca * exist. 1124ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1125*2111c3c0SRandy Dunlap * @nd: nameidata pathwalk data 112630aba665SSalvatore Mesoraca * @inode: the inode of the file to open 112730aba665SSalvatore Mesoraca * 112830aba665SSalvatore Mesoraca * Block an O_CREAT open of a FIFO (or a regular file) when: 112930aba665SSalvatore Mesoraca * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled 113030aba665SSalvatore Mesoraca * - the file already exists 113130aba665SSalvatore Mesoraca * - we are in a sticky directory 113230aba665SSalvatore Mesoraca * - we don't own the file 113330aba665SSalvatore Mesoraca * - the owner of the directory doesn't own the file 113430aba665SSalvatore Mesoraca * - the directory is world writable 113530aba665SSalvatore Mesoraca * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 113630aba665SSalvatore Mesoraca * the directory doesn't have to be world writable: being group writable will 113730aba665SSalvatore Mesoraca * be enough. 113830aba665SSalvatore Mesoraca * 1139ba73d987SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 1140ba73d987SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 1141ba73d987SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 1142ba73d987SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 1143ba73d987SChristian Brauner * raw inode simply passs init_user_ns. 1144ba73d987SChristian Brauner * 114530aba665SSalvatore Mesoraca * Returns 0 if the open is allowed, -ve on error. 114630aba665SSalvatore Mesoraca */ 1147ba73d987SChristian Brauner static int may_create_in_sticky(struct user_namespace *mnt_userns, 1148ba73d987SChristian Brauner struct nameidata *nd, struct inode *const inode) 114930aba665SSalvatore Mesoraca { 1150ba73d987SChristian Brauner umode_t dir_mode = nd->dir_mode; 1151ba73d987SChristian Brauner kuid_t dir_uid = nd->dir_uid; 1152ba73d987SChristian Brauner 115330aba665SSalvatore Mesoraca if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || 115430aba665SSalvatore Mesoraca (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || 1155d0cb5018SAl Viro likely(!(dir_mode & S_ISVTX)) || 1156ba73d987SChristian Brauner uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) || 1157ba73d987SChristian Brauner uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) 115830aba665SSalvatore Mesoraca return 0; 115930aba665SSalvatore Mesoraca 1160d0cb5018SAl Viro if (likely(dir_mode & 0002) || 1161d0cb5018SAl Viro (dir_mode & 0020 && 116230aba665SSalvatore Mesoraca ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || 116330aba665SSalvatore Mesoraca (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { 1164245d7369SKees Cook const char *operation = S_ISFIFO(inode->i_mode) ? 1165245d7369SKees Cook "sticky_create_fifo" : 1166245d7369SKees Cook "sticky_create_regular"; 1167245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_CREAT, operation); 116830aba665SSalvatore Mesoraca return -EACCES; 116930aba665SSalvatore Mesoraca } 117030aba665SSalvatore Mesoraca return 0; 117130aba665SSalvatore Mesoraca } 117230aba665SSalvatore Mesoraca 1173f015f126SDavid Howells /* 1174f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 1175f015f126SDavid Howells * 1176f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 1177f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 1178f015f126SDavid Howells * Up is towards /. 1179f015f126SDavid Howells * 1180f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 1181f015f126SDavid Howells * root. 1182f015f126SDavid Howells */ 1183bab77ebfSAl Viro int follow_up(struct path *path) 11841da177e4SLinus Torvalds { 11850714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 11860714a533SAl Viro struct mount *parent; 11871da177e4SLinus Torvalds struct dentry *mountpoint; 118899b7db7bSNick Piggin 118948a066e7SAl Viro read_seqlock_excl(&mount_lock); 11900714a533SAl Viro parent = mnt->mnt_parent; 11913c0a6163SAl Viro if (parent == mnt) { 119248a066e7SAl Viro read_sequnlock_excl(&mount_lock); 11931da177e4SLinus Torvalds return 0; 11941da177e4SLinus Torvalds } 11950714a533SAl Viro mntget(&parent->mnt); 1196a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 119748a066e7SAl Viro read_sequnlock_excl(&mount_lock); 1198bab77ebfSAl Viro dput(path->dentry); 1199bab77ebfSAl Viro path->dentry = mountpoint; 1200bab77ebfSAl Viro mntput(path->mnt); 12010714a533SAl Viro path->mnt = &parent->mnt; 12021da177e4SLinus Torvalds return 1; 12031da177e4SLinus Torvalds } 12044d359507SAl Viro EXPORT_SYMBOL(follow_up); 12051da177e4SLinus Torvalds 12067ef482faSAl Viro static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, 12077ef482faSAl Viro struct path *path, unsigned *seqp) 12087ef482faSAl Viro { 12097ef482faSAl Viro while (mnt_has_parent(m)) { 12107ef482faSAl Viro struct dentry *mountpoint = m->mnt_mountpoint; 12117ef482faSAl Viro 12127ef482faSAl Viro m = m->mnt_parent; 12137ef482faSAl Viro if (unlikely(root->dentry == mountpoint && 12147ef482faSAl Viro root->mnt == &m->mnt)) 12157ef482faSAl Viro break; 12167ef482faSAl Viro if (mountpoint != m->mnt.mnt_root) { 12177ef482faSAl Viro path->mnt = &m->mnt; 12187ef482faSAl Viro path->dentry = mountpoint; 12197ef482faSAl Viro *seqp = read_seqcount_begin(&mountpoint->d_seq); 12207ef482faSAl Viro return true; 12217ef482faSAl Viro } 12227ef482faSAl Viro } 12237ef482faSAl Viro return false; 12247ef482faSAl Viro } 12257ef482faSAl Viro 12262aa38470SAl Viro static bool choose_mountpoint(struct mount *m, const struct path *root, 12272aa38470SAl Viro struct path *path) 12282aa38470SAl Viro { 12292aa38470SAl Viro bool found; 12302aa38470SAl Viro 12312aa38470SAl Viro rcu_read_lock(); 12322aa38470SAl Viro while (1) { 12332aa38470SAl Viro unsigned seq, mseq = read_seqbegin(&mount_lock); 12342aa38470SAl Viro 12352aa38470SAl Viro found = choose_mountpoint_rcu(m, root, path, &seq); 12362aa38470SAl Viro if (unlikely(!found)) { 12372aa38470SAl Viro if (!read_seqretry(&mount_lock, mseq)) 12382aa38470SAl Viro break; 12392aa38470SAl Viro } else { 12402aa38470SAl Viro if (likely(__legitimize_path(path, seq, mseq))) 12412aa38470SAl Viro break; 12422aa38470SAl Viro rcu_read_unlock(); 12432aa38470SAl Viro path_put(path); 12442aa38470SAl Viro rcu_read_lock(); 12452aa38470SAl Viro } 12462aa38470SAl Viro } 12472aa38470SAl Viro rcu_read_unlock(); 12482aa38470SAl Viro return found; 12492aa38470SAl Viro } 12502aa38470SAl Viro 1251b5c84bf6SNick Piggin /* 12529875cf80SDavid Howells * Perform an automount 12539875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 12549875cf80SDavid Howells * were called with. 12551da177e4SLinus Torvalds */ 12561c9f5e06SAl Viro static int follow_automount(struct path *path, int *count, unsigned lookup_flags) 125731e6b01fSNick Piggin { 125825e195aaSAl Viro struct dentry *dentry = path->dentry; 12599875cf80SDavid Howells 12600ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 12610ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 12620ec26fd0SMiklos Szeredi * the name. 12630ec26fd0SMiklos Szeredi * 12640ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 12655a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 12660ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 12670ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 12680ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 12690ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 12705a30d8a2SDavid Howells */ 12711c9f5e06SAl Viro if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 12725d38f049SIan Kent LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 127325e195aaSAl Viro dentry->d_inode) 12749875cf80SDavid Howells return -EISDIR; 12750ec26fd0SMiklos Szeredi 12761c9f5e06SAl Viro if (count && (*count)++ >= MAXSYMLINKS) 12779875cf80SDavid Howells return -ELOOP; 12789875cf80SDavid Howells 127925e195aaSAl Viro return finish_automount(dentry->d_op->d_automount(path), path); 1280ea5b778aSDavid Howells } 12819875cf80SDavid Howells 12829875cf80SDavid Howells /* 12839deed3ebSAl Viro * mount traversal - out-of-line part. One note on ->d_flags accesses - 12849deed3ebSAl Viro * dentries are pinned but not locked here, so negative dentry can go 12859deed3ebSAl Viro * positive right under us. Use of smp_load_acquire() provides a barrier 12869deed3ebSAl Viro * sufficient for ->d_inode and ->d_flags consistency. 12879875cf80SDavid Howells */ 12889deed3ebSAl Viro static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, 12899deed3ebSAl Viro int *count, unsigned lookup_flags) 12909875cf80SDavid Howells { 12919deed3ebSAl Viro struct vfsmount *mnt = path->mnt; 12929875cf80SDavid Howells bool need_mntput = false; 12938aef1884SAl Viro int ret = 0; 12949875cf80SDavid Howells 12959deed3ebSAl Viro while (flags & DCACHE_MANAGED_DENTRY) { 1296cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1297cc53ce53SDavid Howells * being held. */ 1298d41efb52SAl Viro if (flags & DCACHE_MANAGE_TRANSIT) { 1299fb5f51c7SIan Kent ret = path->dentry->d_op->d_manage(path, false); 1300508c8772SAl Viro flags = smp_load_acquire(&path->dentry->d_flags); 1301cc53ce53SDavid Howells if (ret < 0) 13028aef1884SAl Viro break; 1303cc53ce53SDavid Howells } 1304cc53ce53SDavid Howells 13059deed3ebSAl Viro if (flags & DCACHE_MOUNTED) { // something's mounted on it.. 13069875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 13079deed3ebSAl Viro if (mounted) { // ... in our namespace 13089875cf80SDavid Howells dput(path->dentry); 13099875cf80SDavid Howells if (need_mntput) 1310463ffb2eSAl Viro mntput(path->mnt); 1311463ffb2eSAl Viro path->mnt = mounted; 1312463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 13139deed3ebSAl Viro // here we know it's positive 13149deed3ebSAl Viro flags = path->dentry->d_flags; 13159875cf80SDavid Howells need_mntput = true; 13169875cf80SDavid Howells continue; 1317463ffb2eSAl Viro } 13181da177e4SLinus Torvalds } 13199875cf80SDavid Howells 13209deed3ebSAl Viro if (!(flags & DCACHE_NEED_AUTOMOUNT)) 13219deed3ebSAl Viro break; 13229deed3ebSAl Viro 13239deed3ebSAl Viro // uncovered automount point 13249deed3ebSAl Viro ret = follow_automount(path, count, lookup_flags); 13259deed3ebSAl Viro flags = smp_load_acquire(&path->dentry->d_flags); 13269875cf80SDavid Howells if (ret < 0) 13278aef1884SAl Viro break; 13289875cf80SDavid Howells } 13299875cf80SDavid Howells 13309deed3ebSAl Viro if (ret == -EISDIR) 13319deed3ebSAl Viro ret = 0; 13329deed3ebSAl Viro // possible if you race with several mount --move 13339deed3ebSAl Viro if (need_mntput && path->mnt == mnt) 13348aef1884SAl Viro mntput(path->mnt); 13359deed3ebSAl Viro if (!ret && unlikely(d_flags_negative(flags))) 1336d41efb52SAl Viro ret = -ENOENT; 13379deed3ebSAl Viro *jumped = need_mntput; 13388402752eSAl Viro return ret; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13419deed3ebSAl Viro static inline int traverse_mounts(struct path *path, bool *jumped, 13429deed3ebSAl Viro int *count, unsigned lookup_flags) 13439deed3ebSAl Viro { 13449deed3ebSAl Viro unsigned flags = smp_load_acquire(&path->dentry->d_flags); 13459deed3ebSAl Viro 13469deed3ebSAl Viro /* fastpath */ 13479deed3ebSAl Viro if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { 13489deed3ebSAl Viro *jumped = false; 13499deed3ebSAl Viro if (unlikely(d_flags_negative(flags))) 13509deed3ebSAl Viro return -ENOENT; 13519deed3ebSAl Viro return 0; 13529deed3ebSAl Viro } 13539deed3ebSAl Viro return __traverse_mounts(path, flags, jumped, count, lookup_flags); 13549deed3ebSAl Viro } 13559deed3ebSAl Viro 1356cc53ce53SDavid Howells int follow_down_one(struct path *path) 13571da177e4SLinus Torvalds { 13581da177e4SLinus Torvalds struct vfsmount *mounted; 13591da177e4SLinus Torvalds 13601c755af4SAl Viro mounted = lookup_mnt(path); 13611da177e4SLinus Torvalds if (mounted) { 13629393bd07SAl Viro dput(path->dentry); 13639393bd07SAl Viro mntput(path->mnt); 13649393bd07SAl Viro path->mnt = mounted; 13659393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 13661da177e4SLinus Torvalds return 1; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds return 0; 13691da177e4SLinus Torvalds } 13704d359507SAl Viro EXPORT_SYMBOL(follow_down_one); 13711da177e4SLinus Torvalds 13729875cf80SDavid Howells /* 13739deed3ebSAl Viro * Follow down to the covering mount currently visible to userspace. At each 13749deed3ebSAl Viro * point, the filesystem owning that dentry may be queried as to whether the 13759deed3ebSAl Viro * caller is permitted to proceed or not. 13769deed3ebSAl Viro */ 13779deed3ebSAl Viro int follow_down(struct path *path) 13789deed3ebSAl Viro { 13799deed3ebSAl Viro struct vfsmount *mnt = path->mnt; 13809deed3ebSAl Viro bool jumped; 13819deed3ebSAl Viro int ret = traverse_mounts(path, &jumped, NULL, 0); 13829deed3ebSAl Viro 13839deed3ebSAl Viro if (path->mnt != mnt) 13849deed3ebSAl Viro mntput(mnt); 13859deed3ebSAl Viro return ret; 13869deed3ebSAl Viro } 13879deed3ebSAl Viro EXPORT_SYMBOL(follow_down); 13889deed3ebSAl Viro 13899deed3ebSAl Viro /* 1390287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1391287548e4SAl Viro * we meet a managed dentry that would need blocking. 13929875cf80SDavid Howells */ 13939875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1394254cf582SAl Viro struct inode **inode, unsigned *seqp) 13959875cf80SDavid Howells { 1396ea936aebSAl Viro struct dentry *dentry = path->dentry; 1397ea936aebSAl Viro unsigned int flags = dentry->d_flags; 1398ea936aebSAl Viro 1399ea936aebSAl Viro if (likely(!(flags & DCACHE_MANAGED_DENTRY))) 1400ea936aebSAl Viro return true; 1401ea936aebSAl Viro 1402ea936aebSAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1403ea936aebSAl Viro return false; 1404ea936aebSAl Viro 140562a7375eSIan Kent for (;;) { 140662a7375eSIan Kent /* 140762a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 140862a7375eSIan Kent * that wants to block transit. 140962a7375eSIan Kent */ 1410ea936aebSAl Viro if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { 1411ea936aebSAl Viro int res = dentry->d_op->d_manage(path, true); 1412ea936aebSAl Viro if (res) 1413ea936aebSAl Viro return res == -EISDIR; 1414ea936aebSAl Viro flags = dentry->d_flags; 1415b8faf035SNeilBrown } 141662a7375eSIan Kent 1417ea936aebSAl Viro if (flags & DCACHE_MOUNTED) { 1418ea936aebSAl Viro struct mount *mounted = __lookup_mnt(path->mnt, dentry); 1419ea936aebSAl Viro if (mounted) { 1420c7105365SAl Viro path->mnt = &mounted->mnt; 1421ea936aebSAl Viro dentry = path->dentry = mounted->mnt.mnt_root; 1422a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 1423ea936aebSAl Viro *seqp = read_seqcount_begin(&dentry->d_seq); 1424ea936aebSAl Viro *inode = dentry->d_inode; 142559430262SLinus Torvalds /* 1426ea936aebSAl Viro * We don't need to re-check ->d_seq after this 1427ea936aebSAl Viro * ->d_inode read - there will be an RCU delay 1428ea936aebSAl Viro * between mount hash removal and ->mnt_root 1429ea936aebSAl Viro * becoming unpinned. 143059430262SLinus Torvalds */ 1431ea936aebSAl Viro flags = dentry->d_flags; 1432ea936aebSAl Viro continue; 14339875cf80SDavid Howells } 1434ea936aebSAl Viro if (read_seqretry(&mount_lock, nd->m_seq)) 1435ea936aebSAl Viro return false; 1436ea936aebSAl Viro } 1437ea936aebSAl Viro return !(flags & DCACHE_NEED_AUTOMOUNT); 1438ea936aebSAl Viro } 1439287548e4SAl Viro } 1440287548e4SAl Viro 1441db3c9adeSAl Viro static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, 1442db3c9adeSAl Viro struct path *path, struct inode **inode, 1443db3c9adeSAl Viro unsigned int *seqp) 1444bd7c4b50SAl Viro { 14459deed3ebSAl Viro bool jumped; 1446db3c9adeSAl Viro int ret; 1447bd7c4b50SAl Viro 1448db3c9adeSAl Viro path->mnt = nd->path.mnt; 1449db3c9adeSAl Viro path->dentry = dentry; 1450c153007bSAl Viro if (nd->flags & LOOKUP_RCU) { 1451c153007bSAl Viro unsigned int seq = *seqp; 1452c153007bSAl Viro if (unlikely(!*inode)) 1453c153007bSAl Viro return -ENOENT; 1454c153007bSAl Viro if (likely(__follow_mount_rcu(nd, path, inode, seqp))) 14559deed3ebSAl Viro return 0; 1456ae66db45SAl Viro if (!try_to_unlazy_next(nd, dentry, seq)) 1457c153007bSAl Viro return -ECHILD; 1458c153007bSAl Viro // *path might've been clobbered by __follow_mount_rcu() 1459c153007bSAl Viro path->mnt = nd->path.mnt; 1460c153007bSAl Viro path->dentry = dentry; 1461c153007bSAl Viro } 14629deed3ebSAl Viro ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); 14639deed3ebSAl Viro if (jumped) { 14649deed3ebSAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 14659deed3ebSAl Viro ret = -EXDEV; 14669deed3ebSAl Viro else 14679deed3ebSAl Viro nd->flags |= LOOKUP_JUMPED; 14689deed3ebSAl Viro } 14699deed3ebSAl Viro if (unlikely(ret)) { 14709deed3ebSAl Viro dput(path->dentry); 14719deed3ebSAl Viro if (path->mnt != nd->path.mnt) 14729deed3ebSAl Viro mntput(path->mnt); 14739deed3ebSAl Viro } else { 1474bd7c4b50SAl Viro *inode = d_backing_inode(path->dentry); 1475bd7c4b50SAl Viro *seqp = 0; /* out of RCU mode, so the value doesn't matter */ 1476bd7c4b50SAl Viro } 1477bd7c4b50SAl Viro return ret; 1478bd7c4b50SAl Viro } 1479bd7c4b50SAl Viro 14809875cf80SDavid Howells /* 1481f4fdace9SOleg Drokin * This looks up the name in dcache and possibly revalidates the found dentry. 1482f4fdace9SOleg Drokin * NULL is returned if the dentry does not exist in the cache. 1483baa03890SNick Piggin */ 1484e3c13928SAl Viro static struct dentry *lookup_dcache(const struct qstr *name, 1485e3c13928SAl Viro struct dentry *dir, 14866c51e513SAl Viro unsigned int flags) 1487baa03890SNick Piggin { 1488a89f8337SAl Viro struct dentry *dentry = d_lookup(dir, name); 1489bad61189SMiklos Szeredi if (dentry) { 1490a89f8337SAl Viro int error = d_revalidate(dentry, flags); 1491bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 149274ff0ffcSAl Viro if (!error) 14935542aa2fSEric W. Biederman d_invalidate(dentry); 1494bad61189SMiklos Szeredi dput(dentry); 149574ff0ffcSAl Viro return ERR_PTR(error); 1496bad61189SMiklos Szeredi } 1497bad61189SMiklos Szeredi } 1498baa03890SNick Piggin return dentry; 1499baa03890SNick Piggin } 1500baa03890SNick Piggin 1501baa03890SNick Piggin /* 1502a03ece5fSAl Viro * Parent directory has inode locked exclusive. This is one 1503a03ece5fSAl Viro * and only case when ->lookup() gets called on non in-lookup 1504a03ece5fSAl Viro * dentries - as the matter of fact, this only gets called 1505a03ece5fSAl Viro * when directory is guaranteed to have no in-lookup children 1506a03ece5fSAl Viro * at all. 150744396f4bSJosef Bacik */ 1508a03ece5fSAl Viro static struct dentry *__lookup_hash(const struct qstr *name, 1509a03ece5fSAl Viro struct dentry *base, unsigned int flags) 151044396f4bSJosef Bacik { 1511a03ece5fSAl Viro struct dentry *dentry = lookup_dcache(name, base, flags); 151244396f4bSJosef Bacik struct dentry *old; 1513a03ece5fSAl Viro struct inode *dir = base->d_inode; 1514a03ece5fSAl Viro 1515a03ece5fSAl Viro if (dentry) 1516a03ece5fSAl Viro return dentry; 151744396f4bSJosef Bacik 151844396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1519a03ece5fSAl Viro if (unlikely(IS_DEADDIR(dir))) 152044396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1521a03ece5fSAl Viro 1522a03ece5fSAl Viro dentry = d_alloc(base, name); 1523a03ece5fSAl Viro if (unlikely(!dentry)) 1524a03ece5fSAl Viro return ERR_PTR(-ENOMEM); 152544396f4bSJosef Bacik 152672bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 152744396f4bSJosef Bacik if (unlikely(old)) { 152844396f4bSJosef Bacik dput(dentry); 152944396f4bSJosef Bacik dentry = old; 153044396f4bSJosef Bacik } 153144396f4bSJosef Bacik return dentry; 153244396f4bSJosef Bacik } 153344396f4bSJosef Bacik 153420e34357SAl Viro static struct dentry *lookup_fast(struct nameidata *nd, 153520e34357SAl Viro struct inode **inode, 1536254cf582SAl Viro unsigned *seqp) 15371da177e4SLinus Torvalds { 153831e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 15395a18fff2SAl Viro int status = 1; 15409875cf80SDavid Howells 15413cac260aSAl Viro /* 1542b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 15435d0f49c1SAl Viro * of a false negative due to a concurrent rename, the caller is 15445d0f49c1SAl Viro * going to fall back to non-racy lookup. 1545b04f784eSNick Piggin */ 154631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 154731e6b01fSNick Piggin unsigned seq; 1548da53be12SLinus Torvalds dentry = __d_lookup_rcu(parent, &nd->last, &seq); 15495d0f49c1SAl Viro if (unlikely(!dentry)) { 1550e36cffedSJens Axboe if (!try_to_unlazy(nd)) 155120e34357SAl Viro return ERR_PTR(-ECHILD); 155220e34357SAl Viro return NULL; 15535d0f49c1SAl Viro } 15545a18fff2SAl Viro 155512f8ad4bSLinus Torvalds /* 155612f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 155712f8ad4bSLinus Torvalds * the dentry name information from lookup. 155812f8ad4bSLinus Torvalds */ 155963afdfc7SDavid Howells *inode = d_backing_inode(dentry); 15605d0f49c1SAl Viro if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) 156120e34357SAl Viro return ERR_PTR(-ECHILD); 156212f8ad4bSLinus Torvalds 156312f8ad4bSLinus Torvalds /* 156412f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 156512f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 156612f8ad4bSLinus Torvalds * 156712f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 156812f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 156912f8ad4bSLinus Torvalds */ 15705d0f49c1SAl Viro if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) 157120e34357SAl Viro return ERR_PTR(-ECHILD); 15725a18fff2SAl Viro 1573254cf582SAl Viro *seqp = seq; 15744ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 1575c153007bSAl Viro if (likely(status > 0)) 157620e34357SAl Viro return dentry; 1577ae66db45SAl Viro if (!try_to_unlazy_next(nd, dentry, seq)) 157820e34357SAl Viro return ERR_PTR(-ECHILD); 157926ddb45eSSteven Rostedt (VMware) if (status == -ECHILD) 1580209a7fb2SAl Viro /* we'd been told to redo it in non-rcu mode */ 1581209a7fb2SAl Viro status = d_revalidate(dentry, nd->flags); 15825a18fff2SAl Viro } else { 1583e97cdc87SAl Viro dentry = __d_lookup(parent, &nd->last); 158481e6f520SAl Viro if (unlikely(!dentry)) 158520e34357SAl Viro return NULL; 15864ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 15875d0f49c1SAl Viro } 15885a18fff2SAl Viro if (unlikely(status <= 0)) { 1589e9742b53SAl Viro if (!status) 15905d0f49c1SAl Viro d_invalidate(dentry); 15915a18fff2SAl Viro dput(dentry); 159220e34357SAl Viro return ERR_PTR(status); 15935a18fff2SAl Viro } 159420e34357SAl Viro return dentry; 1595697f514dSMiklos Szeredi } 1596697f514dSMiklos Szeredi 1597697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 159888d8331aSAl Viro static struct dentry *__lookup_slow(const struct qstr *name, 1599e3c13928SAl Viro struct dentry *dir, 1600e3c13928SAl Viro unsigned int flags) 1601697f514dSMiklos Szeredi { 160288d8331aSAl Viro struct dentry *dentry, *old; 16031936386eSAl Viro struct inode *inode = dir->d_inode; 1604d9171b93SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 16051936386eSAl Viro 16061936386eSAl Viro /* Don't go there if it's already dead */ 160794bdd655SAl Viro if (unlikely(IS_DEADDIR(inode))) 160888d8331aSAl Viro return ERR_PTR(-ENOENT); 160994bdd655SAl Viro again: 1610d9171b93SAl Viro dentry = d_alloc_parallel(dir, name, &wq); 161194bdd655SAl Viro if (IS_ERR(dentry)) 161288d8331aSAl Viro return dentry; 161394bdd655SAl Viro if (unlikely(!d_in_lookup(dentry))) { 1614949a852eSAl Viro int error = d_revalidate(dentry, flags); 1615949a852eSAl Viro if (unlikely(error <= 0)) { 161694bdd655SAl Viro if (!error) { 1617949a852eSAl Viro d_invalidate(dentry); 1618949a852eSAl Viro dput(dentry); 161994bdd655SAl Viro goto again; 162094bdd655SAl Viro } 162194bdd655SAl Viro dput(dentry); 1622949a852eSAl Viro dentry = ERR_PTR(error); 1623949a852eSAl Viro } 162494bdd655SAl Viro } else { 16251936386eSAl Viro old = inode->i_op->lookup(inode, dentry, flags); 162685c7f810SAl Viro d_lookup_done(dentry); 16271936386eSAl Viro if (unlikely(old)) { 16281936386eSAl Viro dput(dentry); 16291936386eSAl Viro dentry = old; 1630949a852eSAl Viro } 1631949a852eSAl Viro } 1632e3c13928SAl Viro return dentry; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 163588d8331aSAl Viro static struct dentry *lookup_slow(const struct qstr *name, 163688d8331aSAl Viro struct dentry *dir, 163788d8331aSAl Viro unsigned int flags) 163888d8331aSAl Viro { 163988d8331aSAl Viro struct inode *inode = dir->d_inode; 164088d8331aSAl Viro struct dentry *res; 164188d8331aSAl Viro inode_lock_shared(inode); 164288d8331aSAl Viro res = __lookup_slow(name, dir, flags); 164388d8331aSAl Viro inode_unlock_shared(inode); 164488d8331aSAl Viro return res; 164588d8331aSAl Viro } 164688d8331aSAl Viro 1647ba73d987SChristian Brauner static inline int may_lookup(struct user_namespace *mnt_userns, 1648ba73d987SChristian Brauner struct nameidata *nd) 164952094c8aSAl Viro { 165052094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 16517d6beb71SLinus Torvalds int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 1652e36cffedSJens Axboe if (err != -ECHILD || !try_to_unlazy(nd)) 165352094c8aSAl Viro return err; 165452094c8aSAl Viro } 1655ba73d987SChristian Brauner return inode_permission(mnt_userns, nd->inode, MAY_EXEC); 165652094c8aSAl Viro } 165752094c8aSAl Viro 165849055906SAl Viro static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) 1659d63ff28fSAl Viro { 166049055906SAl Viro if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) 166149055906SAl Viro return -ELOOP; 16624542576bSAl Viro 16634542576bSAl Viro if (likely(nd->depth != EMBEDDED_LEVELS)) 16644542576bSAl Viro return 0; 16654542576bSAl Viro if (likely(nd->stack != nd->internal)) 16664542576bSAl Viro return 0; 166760ef60c7SAl Viro if (likely(nd_alloc_stack(nd))) 166849055906SAl Viro return 0; 166960ef60c7SAl Viro 167060ef60c7SAl Viro if (nd->flags & LOOKUP_RCU) { 167160ef60c7SAl Viro // we need to grab link before we do unlazy. And we can't skip 167260ef60c7SAl Viro // unlazy even if we fail to grab the link - cleanup needs it 1673aef9404dSAl Viro bool grabbed_link = legitimize_path(nd, link, seq); 167460ef60c7SAl Viro 1675e36cffedSJens Axboe if (!try_to_unlazy(nd) != 0 || !grabbed_link) 167660ef60c7SAl Viro return -ECHILD; 167760ef60c7SAl Viro 167860ef60c7SAl Viro if (nd_alloc_stack(nd)) 167960ef60c7SAl Viro return 0; 1680bc40aee0SAl Viro } 168160ef60c7SAl Viro return -ENOMEM; 168249055906SAl Viro } 168349055906SAl Viro 168449055906SAl Viro enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; 168549055906SAl Viro 168649055906SAl Viro static const char *pick_link(struct nameidata *nd, struct path *link, 168749055906SAl Viro struct inode *inode, unsigned seq, int flags) 168849055906SAl Viro { 168949055906SAl Viro struct saved *last; 169049055906SAl Viro const char *res; 169149055906SAl Viro int error = reserve_stack(nd, link, seq); 169249055906SAl Viro 169349055906SAl Viro if (unlikely(error)) { 169449055906SAl Viro if (!(nd->flags & LOOKUP_RCU)) 1695cd179f44SAl Viro path_put(link); 169606708adbSAl Viro return ERR_PTR(error); 1697626de996SAl Viro } 1698ab104923SAl Viro last = nd->stack + nd->depth++; 16991cf2665bSAl Viro last->link = *link; 1700fceef393SAl Viro clear_delayed_call(&last->done); 17010450b2d1SAl Viro last->seq = seq; 1702ad6cc4c3SAl Viro 1703b1a81972SAl Viro if (flags & WALK_TRAILING) { 1704ad6cc4c3SAl Viro error = may_follow_link(nd, inode); 1705ad6cc4c3SAl Viro if (unlikely(error)) 1706ad6cc4c3SAl Viro return ERR_PTR(error); 1707ad6cc4c3SAl Viro } 1708ad6cc4c3SAl Viro 1709dab741e0SMattias Nissler if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || 1710dab741e0SMattias Nissler unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) 1711ad6cc4c3SAl Viro return ERR_PTR(-ELOOP); 1712ad6cc4c3SAl Viro 1713ad6cc4c3SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1714ad6cc4c3SAl Viro touch_atime(&last->link); 1715ad6cc4c3SAl Viro cond_resched(); 1716ad6cc4c3SAl Viro } else if (atime_needs_update(&last->link, inode)) { 1717e36cffedSJens Axboe if (!try_to_unlazy(nd)) 1718ad6cc4c3SAl Viro return ERR_PTR(-ECHILD); 1719ad6cc4c3SAl Viro touch_atime(&last->link); 1720ad6cc4c3SAl Viro } 1721ad6cc4c3SAl Viro 1722ad6cc4c3SAl Viro error = security_inode_follow_link(link->dentry, inode, 1723ad6cc4c3SAl Viro nd->flags & LOOKUP_RCU); 1724ad6cc4c3SAl Viro if (unlikely(error)) 1725ad6cc4c3SAl Viro return ERR_PTR(error); 1726ad6cc4c3SAl Viro 1727ad6cc4c3SAl Viro res = READ_ONCE(inode->i_link); 1728ad6cc4c3SAl Viro if (!res) { 1729ad6cc4c3SAl Viro const char * (*get)(struct dentry *, struct inode *, 1730ad6cc4c3SAl Viro struct delayed_call *); 1731ad6cc4c3SAl Viro get = inode->i_op->get_link; 1732ad6cc4c3SAl Viro if (nd->flags & LOOKUP_RCU) { 1733ad6cc4c3SAl Viro res = get(NULL, inode, &last->done); 1734e36cffedSJens Axboe if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) 1735ad6cc4c3SAl Viro res = get(link->dentry, inode, &last->done); 1736ad6cc4c3SAl Viro } else { 1737ad6cc4c3SAl Viro res = get(link->dentry, inode, &last->done); 1738ad6cc4c3SAl Viro } 1739ad6cc4c3SAl Viro if (!res) 1740ad6cc4c3SAl Viro goto all_done; 1741ad6cc4c3SAl Viro if (IS_ERR(res)) 1742ad6cc4c3SAl Viro return res; 1743ad6cc4c3SAl Viro } 1744ad6cc4c3SAl Viro if (*res == '/') { 1745ad6cc4c3SAl Viro error = nd_jump_root(nd); 1746ad6cc4c3SAl Viro if (unlikely(error)) 1747ad6cc4c3SAl Viro return ERR_PTR(error); 1748ad6cc4c3SAl Viro while (unlikely(*++res == '/')) 1749ad6cc4c3SAl Viro ; 1750ad6cc4c3SAl Viro } 1751ad6cc4c3SAl Viro if (*res) 1752ad6cc4c3SAl Viro return res; 1753ad6cc4c3SAl Viro all_done: // pure jump 1754ad6cc4c3SAl Viro put_link(nd); 1755ad6cc4c3SAl Viro return NULL; 1756d63ff28fSAl Viro } 1757d63ff28fSAl Viro 17583ddcd056SLinus Torvalds /* 17593ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 17603ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 17613ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 17623ddcd056SLinus Torvalds * for the common case. 17633ddcd056SLinus Torvalds */ 1764b0417d2cSAl Viro static const char *step_into(struct nameidata *nd, int flags, 1765cbae4d12SAl Viro struct dentry *dentry, struct inode *inode, unsigned seq) 17663ddcd056SLinus Torvalds { 1767cbae4d12SAl Viro struct path path; 1768cbae4d12SAl Viro int err = handle_mounts(nd, dentry, &path, &inode, &seq); 1769cbae4d12SAl Viro 1770cbae4d12SAl Viro if (err < 0) 1771b0417d2cSAl Viro return ERR_PTR(err); 1772cbae4d12SAl Viro if (likely(!d_is_symlink(path.dentry)) || 17738c4efe22SAl Viro ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || 1774aca2903eSAl Viro (flags & WALK_NOFOLLOW)) { 17758f64fb1cSAl Viro /* not a symlink or should not follow */ 1776c99687a0SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1777c99687a0SAl Viro dput(nd->path.dentry); 1778c99687a0SAl Viro if (nd->path.mnt != path.mnt) 1779c99687a0SAl Viro mntput(nd->path.mnt); 1780c99687a0SAl Viro } 1781c99687a0SAl Viro nd->path = path; 17828f64fb1cSAl Viro nd->inode = inode; 17838f64fb1cSAl Viro nd->seq = seq; 1784b0417d2cSAl Viro return NULL; 17858f64fb1cSAl Viro } 1786a7f77542SAl Viro if (nd->flags & LOOKUP_RCU) { 178784f0cd9eSAl Viro /* make sure that d_is_symlink above matches inode */ 1788cbae4d12SAl Viro if (read_seqcount_retry(&path.dentry->d_seq, seq)) 1789b0417d2cSAl Viro return ERR_PTR(-ECHILD); 179084f0cd9eSAl Viro } else { 179184f0cd9eSAl Viro if (path.mnt == nd->path.mnt) 179284f0cd9eSAl Viro mntget(path.mnt); 1793a7f77542SAl Viro } 1794b1a81972SAl Viro return pick_link(nd, &path, inode, seq, flags); 17953ddcd056SLinus Torvalds } 17963ddcd056SLinus Torvalds 1797c2df1968SAl Viro static struct dentry *follow_dotdot_rcu(struct nameidata *nd, 1798c2df1968SAl Viro struct inode **inodep, 1799c2df1968SAl Viro unsigned *seqp) 1800957dd41dSAl Viro { 180112487f30SAl Viro struct dentry *parent, *old; 1802957dd41dSAl Viro 180312487f30SAl Viro if (path_equal(&nd->path, &nd->root)) 180412487f30SAl Viro goto in_root; 180512487f30SAl Viro if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 18067ef482faSAl Viro struct path path; 1807efe772d6SAl Viro unsigned seq; 18087ef482faSAl Viro if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), 18097ef482faSAl Viro &nd->root, &path, &seq)) 181012487f30SAl Viro goto in_root; 1811efe772d6SAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1812efe772d6SAl Viro return ERR_PTR(-ECHILD); 1813efe772d6SAl Viro nd->path = path; 1814efe772d6SAl Viro nd->inode = path.dentry->d_inode; 1815efe772d6SAl Viro nd->seq = seq; 1816efe772d6SAl Viro if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) 1817efe772d6SAl Viro return ERR_PTR(-ECHILD); 1818efe772d6SAl Viro /* we know that mountpoint was pinned */ 1819957dd41dSAl Viro } 182012487f30SAl Viro old = nd->path.dentry; 182112487f30SAl Viro parent = old->d_parent; 182212487f30SAl Viro *inodep = parent->d_inode; 182312487f30SAl Viro *seqp = read_seqcount_begin(&parent->d_seq); 182412487f30SAl Viro if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) 182512487f30SAl Viro return ERR_PTR(-ECHILD); 182612487f30SAl Viro if (unlikely(!path_connected(nd->path.mnt, parent))) 182712487f30SAl Viro return ERR_PTR(-ECHILD); 182812487f30SAl Viro return parent; 182912487f30SAl Viro in_root: 1830efe772d6SAl Viro if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) 1831efe772d6SAl Viro return ERR_PTR(-ECHILD); 1832957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_BENEATH)) 18337521f22bSAl Viro return ERR_PTR(-ECHILD); 1834c2df1968SAl Viro return NULL; 1835957dd41dSAl Viro } 1836957dd41dSAl Viro 1837c2df1968SAl Viro static struct dentry *follow_dotdot(struct nameidata *nd, 1838c2df1968SAl Viro struct inode **inodep, 1839c2df1968SAl Viro unsigned *seqp) 1840957dd41dSAl Viro { 184112487f30SAl Viro struct dentry *parent; 184212487f30SAl Viro 1843957dd41dSAl Viro if (path_equal(&nd->path, &nd->root)) 184412487f30SAl Viro goto in_root; 184512487f30SAl Viro if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 18462aa38470SAl Viro struct path path; 18472aa38470SAl Viro 18482aa38470SAl Viro if (!choose_mountpoint(real_mount(nd->path.mnt), 18492aa38470SAl Viro &nd->root, &path)) 185012487f30SAl Viro goto in_root; 1851165200d6SAl Viro path_put(&nd->path); 1852165200d6SAl Viro nd->path = path; 18532aa38470SAl Viro nd->inode = path.dentry->d_inode; 1854165200d6SAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1855165200d6SAl Viro return ERR_PTR(-EXDEV); 185612487f30SAl Viro } 1857957dd41dSAl Viro /* rare case of legitimate dget_parent()... */ 185812487f30SAl Viro parent = dget_parent(nd->path.dentry); 1859957dd41dSAl Viro if (unlikely(!path_connected(nd->path.mnt, parent))) { 1860957dd41dSAl Viro dput(parent); 18617521f22bSAl Viro return ERR_PTR(-ENOENT); 1862957dd41dSAl Viro } 1863c2df1968SAl Viro *seqp = 0; 1864c2df1968SAl Viro *inodep = parent->d_inode; 1865c2df1968SAl Viro return parent; 186612487f30SAl Viro 186712487f30SAl Viro in_root: 1868957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_BENEATH)) 18697521f22bSAl Viro return ERR_PTR(-EXDEV); 1870c2df1968SAl Viro dget(nd->path.dentry); 1871c2df1968SAl Viro return NULL; 1872957dd41dSAl Viro } 1873957dd41dSAl Viro 18747521f22bSAl Viro static const char *handle_dots(struct nameidata *nd, int type) 1875957dd41dSAl Viro { 1876957dd41dSAl Viro if (type == LAST_DOTDOT) { 18777521f22bSAl Viro const char *error = NULL; 1878c2df1968SAl Viro struct dentry *parent; 1879c2df1968SAl Viro struct inode *inode; 1880c2df1968SAl Viro unsigned seq; 1881957dd41dSAl Viro 1882957dd41dSAl Viro if (!nd->root.mnt) { 18837521f22bSAl Viro error = ERR_PTR(set_root(nd)); 1884957dd41dSAl Viro if (error) 1885957dd41dSAl Viro return error; 1886957dd41dSAl Viro } 1887957dd41dSAl Viro if (nd->flags & LOOKUP_RCU) 1888c2df1968SAl Viro parent = follow_dotdot_rcu(nd, &inode, &seq); 1889957dd41dSAl Viro else 1890c2df1968SAl Viro parent = follow_dotdot(nd, &inode, &seq); 1891c2df1968SAl Viro if (IS_ERR(parent)) 1892c2df1968SAl Viro return ERR_CAST(parent); 1893c2df1968SAl Viro if (unlikely(!parent)) 1894c2df1968SAl Viro error = step_into(nd, WALK_NOFOLLOW, 1895c2df1968SAl Viro nd->path.dentry, nd->inode, nd->seq); 1896c2df1968SAl Viro else 1897c2df1968SAl Viro error = step_into(nd, WALK_NOFOLLOW, 1898c2df1968SAl Viro parent, inode, seq); 1899c2df1968SAl Viro if (unlikely(error)) 1900957dd41dSAl Viro return error; 1901957dd41dSAl Viro 1902957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 1903957dd41dSAl Viro /* 1904957dd41dSAl Viro * If there was a racing rename or mount along our 1905957dd41dSAl Viro * path, then we can't be sure that ".." hasn't jumped 1906957dd41dSAl Viro * above nd->root (and so userspace should retry or use 1907957dd41dSAl Viro * some fallback). 1908957dd41dSAl Viro */ 1909957dd41dSAl Viro smp_rmb(); 1910957dd41dSAl Viro if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) 19117521f22bSAl Viro return ERR_PTR(-EAGAIN); 1912957dd41dSAl Viro if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) 19137521f22bSAl Viro return ERR_PTR(-EAGAIN); 1914957dd41dSAl Viro } 1915957dd41dSAl Viro } 19167521f22bSAl Viro return NULL; 1917957dd41dSAl Viro } 1918957dd41dSAl Viro 191992d27016SAl Viro static const char *walk_component(struct nameidata *nd, int flags) 1920ce57dfc1SAl Viro { 1921db3c9adeSAl Viro struct dentry *dentry; 1922ce57dfc1SAl Viro struct inode *inode; 1923254cf582SAl Viro unsigned seq; 1924ce57dfc1SAl Viro /* 1925ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1926ce57dfc1SAl Viro * to be able to know about the current root directory and 1927ce57dfc1SAl Viro * parent relationships. 1928ce57dfc1SAl Viro */ 19294693a547SAl Viro if (unlikely(nd->last_type != LAST_NORM)) { 19301c4ff1a8SAl Viro if (!(flags & WALK_MORE) && nd->depth) 19314693a547SAl Viro put_link(nd); 19327521f22bSAl Viro return handle_dots(nd, nd->last_type); 19334693a547SAl Viro } 193420e34357SAl Viro dentry = lookup_fast(nd, &inode, &seq); 193520e34357SAl Viro if (IS_ERR(dentry)) 193692d27016SAl Viro return ERR_CAST(dentry); 193720e34357SAl Viro if (unlikely(!dentry)) { 1938db3c9adeSAl Viro dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); 1939db3c9adeSAl Viro if (IS_ERR(dentry)) 194092d27016SAl Viro return ERR_CAST(dentry); 194120e34357SAl Viro } 194256676ec3SAl Viro if (!(flags & WALK_MORE) && nd->depth) 194356676ec3SAl Viro put_link(nd); 1944b0417d2cSAl Viro return step_into(nd, flags, dentry, inode, seq); 1945ce57dfc1SAl Viro } 1946ce57dfc1SAl Viro 19471da177e4SLinus Torvalds /* 1948bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1949bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1950bfcfaa77SLinus Torvalds * 1951bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1952bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1953bfcfaa77SLinus Torvalds * fast. 1954bfcfaa77SLinus Torvalds * 1955bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1956bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1957bfcfaa77SLinus Torvalds * crossing operation. 1958bfcfaa77SLinus Torvalds * 1959bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1960bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1961bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1962bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1963bfcfaa77SLinus Torvalds */ 1964bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1965bfcfaa77SLinus Torvalds 1966f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1967bfcfaa77SLinus Torvalds 1968468a9428SGeorge Spelvin #ifdef HASH_MIX 1969bfcfaa77SLinus Torvalds 1970468a9428SGeorge Spelvin /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ 1971468a9428SGeorge Spelvin 1972468a9428SGeorge Spelvin #elif defined(CONFIG_64BIT) 19732a18da7aSGeorge Spelvin /* 19742a18da7aSGeorge Spelvin * Register pressure in the mixing function is an issue, particularly 19752a18da7aSGeorge Spelvin * on 32-bit x86, but almost any function requires one state value and 19762a18da7aSGeorge Spelvin * one temporary. Instead, use a function designed for two state values 19772a18da7aSGeorge Spelvin * and no temporaries. 19782a18da7aSGeorge Spelvin * 19792a18da7aSGeorge Spelvin * This function cannot create a collision in only two iterations, so 19802a18da7aSGeorge Spelvin * we have two iterations to achieve avalanche. In those two iterations, 19812a18da7aSGeorge Spelvin * we have six layers of mixing, which is enough to spread one bit's 19822a18da7aSGeorge Spelvin * influence out to 2^6 = 64 state bits. 19832a18da7aSGeorge Spelvin * 19842a18da7aSGeorge Spelvin * Rotate constants are scored by considering either 64 one-bit input 19852a18da7aSGeorge Spelvin * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the 19862a18da7aSGeorge Spelvin * probability of that delta causing a change to each of the 128 output 19872a18da7aSGeorge Spelvin * bits, using a sample of random initial states. 19882a18da7aSGeorge Spelvin * 19892a18da7aSGeorge Spelvin * The Shannon entropy of the computed probabilities is then summed 19902a18da7aSGeorge Spelvin * to produce a score. Ideally, any input change has a 50% chance of 19912a18da7aSGeorge Spelvin * toggling any given output bit. 19922a18da7aSGeorge Spelvin * 19932a18da7aSGeorge Spelvin * Mixing scores (in bits) for (12,45): 19942a18da7aSGeorge Spelvin * Input delta: 1-bit 2-bit 19952a18da7aSGeorge Spelvin * 1 round: 713.3 42542.6 19962a18da7aSGeorge Spelvin * 2 rounds: 2753.7 140389.8 19972a18da7aSGeorge Spelvin * 3 rounds: 5954.1 233458.2 19982a18da7aSGeorge Spelvin * 4 rounds: 7862.6 256672.2 19992a18da7aSGeorge Spelvin * Perfect: 8192 258048 20002a18da7aSGeorge Spelvin * (64*128) (64*63/2 * 128) 20012a18da7aSGeorge Spelvin */ 20022a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a) \ 20032a18da7aSGeorge Spelvin ( x ^= (a), \ 20042a18da7aSGeorge Spelvin y ^= x, x = rol64(x,12),\ 20052a18da7aSGeorge Spelvin x += y, y = rol64(y,45),\ 20062a18da7aSGeorge Spelvin y *= 9 ) 2007bfcfaa77SLinus Torvalds 20080fed3ac8SGeorge Spelvin /* 20092a18da7aSGeorge Spelvin * Fold two longs into one 32-bit hash value. This must be fast, but 20102a18da7aSGeorge Spelvin * latency isn't quite as critical, as there is a fair bit of additional 20112a18da7aSGeorge Spelvin * work done before the hash value is used. 20120fed3ac8SGeorge Spelvin */ 20132a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y) 20140fed3ac8SGeorge Spelvin { 20152a18da7aSGeorge Spelvin y ^= x * GOLDEN_RATIO_64; 20162a18da7aSGeorge Spelvin y *= GOLDEN_RATIO_64; 20172a18da7aSGeorge Spelvin return y >> 32; 20180fed3ac8SGeorge Spelvin } 20190fed3ac8SGeorge Spelvin 2020bfcfaa77SLinus Torvalds #else /* 32-bit case */ 2021bfcfaa77SLinus Torvalds 20222a18da7aSGeorge Spelvin /* 20232a18da7aSGeorge Spelvin * Mixing scores (in bits) for (7,20): 20242a18da7aSGeorge Spelvin * Input delta: 1-bit 2-bit 20252a18da7aSGeorge Spelvin * 1 round: 330.3 9201.6 20262a18da7aSGeorge Spelvin * 2 rounds: 1246.4 25475.4 20272a18da7aSGeorge Spelvin * 3 rounds: 1907.1 31295.1 20282a18da7aSGeorge Spelvin * 4 rounds: 2042.3 31718.6 20292a18da7aSGeorge Spelvin * Perfect: 2048 31744 20302a18da7aSGeorge Spelvin * (32*64) (32*31/2 * 64) 20312a18da7aSGeorge Spelvin */ 20322a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a) \ 20332a18da7aSGeorge Spelvin ( x ^= (a), \ 20342a18da7aSGeorge Spelvin y ^= x, x = rol32(x, 7),\ 20352a18da7aSGeorge Spelvin x += y, y = rol32(y,20),\ 20362a18da7aSGeorge Spelvin y *= 9 ) 2037bfcfaa77SLinus Torvalds 20382a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y) 20390fed3ac8SGeorge Spelvin { 20402a18da7aSGeorge Spelvin /* Use arch-optimized multiply if one exists */ 20412a18da7aSGeorge Spelvin return __hash_32(y ^ __hash_32(x)); 20420fed3ac8SGeorge Spelvin } 20430fed3ac8SGeorge Spelvin 2044bfcfaa77SLinus Torvalds #endif 2045bfcfaa77SLinus Torvalds 20462a18da7aSGeorge Spelvin /* 20472a18da7aSGeorge Spelvin * Return the hash of a string of known length. This is carfully 20482a18da7aSGeorge Spelvin * designed to match hash_name(), which is the more critical function. 20492a18da7aSGeorge Spelvin * In particular, we must end by hashing a final word containing 0..7 20502a18da7aSGeorge Spelvin * payload bytes, to match the way that hash_name() iterates until it 20512a18da7aSGeorge Spelvin * finds the delimiter after the name. 20522a18da7aSGeorge Spelvin */ 20538387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 2054bfcfaa77SLinus Torvalds { 20558387ff25SLinus Torvalds unsigned long a, x = 0, y = (unsigned long)salt; 2056bfcfaa77SLinus Torvalds 2057bfcfaa77SLinus Torvalds for (;;) { 2058fcfd2fbfSGeorge Spelvin if (!len) 2059fcfd2fbfSGeorge Spelvin goto done; 2060e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 2061bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 2062bfcfaa77SLinus Torvalds break; 20632a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2064bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 2065bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 2066bfcfaa77SLinus Torvalds } 20672a18da7aSGeorge Spelvin x ^= a & bytemask_from_count(len); 2068bfcfaa77SLinus Torvalds done: 20692a18da7aSGeorge Spelvin return fold_hash(x, y); 2070bfcfaa77SLinus Torvalds } 2071bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 2072bfcfaa77SLinus Torvalds 2073fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */ 20748387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name) 2075fcfd2fbfSGeorge Spelvin { 20768387ff25SLinus Torvalds unsigned long a = 0, x = 0, y = (unsigned long)salt; 20778387ff25SLinus Torvalds unsigned long adata, mask, len; 2078fcfd2fbfSGeorge Spelvin const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2079fcfd2fbfSGeorge Spelvin 20808387ff25SLinus Torvalds len = 0; 20818387ff25SLinus Torvalds goto inside; 20828387ff25SLinus Torvalds 2083fcfd2fbfSGeorge Spelvin do { 20842a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2085fcfd2fbfSGeorge Spelvin len += sizeof(unsigned long); 20868387ff25SLinus Torvalds inside: 2087fcfd2fbfSGeorge Spelvin a = load_unaligned_zeropad(name+len); 2088fcfd2fbfSGeorge Spelvin } while (!has_zero(a, &adata, &constants)); 2089fcfd2fbfSGeorge Spelvin 2090fcfd2fbfSGeorge Spelvin adata = prep_zero_mask(a, adata, &constants); 2091fcfd2fbfSGeorge Spelvin mask = create_zero_mask(adata); 20922a18da7aSGeorge Spelvin x ^= a & zero_bytemask(mask); 2093fcfd2fbfSGeorge Spelvin 20942a18da7aSGeorge Spelvin return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2095fcfd2fbfSGeorge Spelvin } 2096fcfd2fbfSGeorge Spelvin EXPORT_SYMBOL(hashlen_string); 2097fcfd2fbfSGeorge Spelvin 2098bfcfaa77SLinus Torvalds /* 2099bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 2100d6bb3e90SLinus Torvalds * return the "hash_len" as the result. 2101bfcfaa77SLinus Torvalds */ 21028387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name) 2103bfcfaa77SLinus Torvalds { 21048387ff25SLinus Torvalds unsigned long a = 0, b, x = 0, y = (unsigned long)salt; 21058387ff25SLinus Torvalds unsigned long adata, bdata, mask, len; 210636126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2107bfcfaa77SLinus Torvalds 21088387ff25SLinus Torvalds len = 0; 21098387ff25SLinus Torvalds goto inside; 21108387ff25SLinus Torvalds 2111bfcfaa77SLinus Torvalds do { 21122a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2113bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 21148387ff25SLinus Torvalds inside: 2115e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 211636126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 211736126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 2118bfcfaa77SLinus Torvalds 211936126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 212036126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 212136126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 21222a18da7aSGeorge Spelvin x ^= a & zero_bytemask(mask); 212336126f8fSLinus Torvalds 21242a18da7aSGeorge Spelvin return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2125bfcfaa77SLinus Torvalds } 2126bfcfaa77SLinus Torvalds 21272a18da7aSGeorge Spelvin #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ 2128bfcfaa77SLinus Torvalds 2129fcfd2fbfSGeorge Spelvin /* Return the hash of a string of known length */ 21308387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 21310145acc2SLinus Torvalds { 21328387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 21330145acc2SLinus Torvalds while (len--) 2134fcfd2fbfSGeorge Spelvin hash = partial_name_hash((unsigned char)*name++, hash); 21350145acc2SLinus Torvalds return end_name_hash(hash); 21360145acc2SLinus Torvalds } 2137ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 21380145acc2SLinus Torvalds 2139fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */ 21408387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name) 2141fcfd2fbfSGeorge Spelvin { 21428387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 2143fcfd2fbfSGeorge Spelvin unsigned long len = 0, c; 2144fcfd2fbfSGeorge Spelvin 2145fcfd2fbfSGeorge Spelvin c = (unsigned char)*name; 2146e0ab7af9SGeorge Spelvin while (c) { 2147fcfd2fbfSGeorge Spelvin len++; 2148fcfd2fbfSGeorge Spelvin hash = partial_name_hash(c, hash); 2149fcfd2fbfSGeorge Spelvin c = (unsigned char)name[len]; 2150e0ab7af9SGeorge Spelvin } 2151fcfd2fbfSGeorge Spelvin return hashlen_create(end_name_hash(hash), len); 2152fcfd2fbfSGeorge Spelvin } 2153f2a031b6SGeorge Spelvin EXPORT_SYMBOL(hashlen_string); 2154fcfd2fbfSGeorge Spelvin 21553ddcd056SLinus Torvalds /* 2156200e9ef7SLinus Torvalds * We know there's a real path component here of at least 2157200e9ef7SLinus Torvalds * one character. 2158200e9ef7SLinus Torvalds */ 21598387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name) 2160200e9ef7SLinus Torvalds { 21618387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 2162200e9ef7SLinus Torvalds unsigned long len = 0, c; 2163200e9ef7SLinus Torvalds 2164200e9ef7SLinus Torvalds c = (unsigned char)*name; 2165200e9ef7SLinus Torvalds do { 2166200e9ef7SLinus Torvalds len++; 2167200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 2168200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 2169200e9ef7SLinus Torvalds } while (c && c != '/'); 2170d6bb3e90SLinus Torvalds return hashlen_create(end_name_hash(hash), len); 2171200e9ef7SLinus Torvalds } 2172200e9ef7SLinus Torvalds 2173bfcfaa77SLinus Torvalds #endif 2174bfcfaa77SLinus Torvalds 2175200e9ef7SLinus Torvalds /* 21761da177e4SLinus Torvalds * Name resolution. 2177ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 2178ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 21791da177e4SLinus Torvalds * 2180ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 2181ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 21821da177e4SLinus Torvalds */ 21836de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 21841da177e4SLinus Torvalds { 2185d8d4611aSAl Viro int depth = 0; // depth <= nd->depth 21861da177e4SLinus Torvalds int err; 21871da177e4SLinus Torvalds 2188b4c03536SAl Viro nd->last_type = LAST_ROOT; 2189c108837eSAl Viro nd->flags |= LOOKUP_PARENT; 21909b5858e9SAl Viro if (IS_ERR(name)) 21919b5858e9SAl Viro return PTR_ERR(name); 21921da177e4SLinus Torvalds while (*name=='/') 21931da177e4SLinus Torvalds name++; 21941a97d899SAl Viro if (!*name) { 21951a97d899SAl Viro nd->dir_mode = 0; // short-circuit the 'hardening' idiocy 21969e18f10aSAl Viro return 0; 21971a97d899SAl Viro } 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 22001da177e4SLinus Torvalds for(;;) { 2201549c7297SChristian Brauner struct user_namespace *mnt_userns; 220292d27016SAl Viro const char *link; 2203d6bb3e90SLinus Torvalds u64 hash_len; 2204fe479a58SAl Viro int type; 22051da177e4SLinus Torvalds 2206549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 2207549c7297SChristian Brauner err = may_lookup(mnt_userns, nd); 22081da177e4SLinus Torvalds if (err) 22093595e234SAl Viro return err; 22101da177e4SLinus Torvalds 22118387ff25SLinus Torvalds hash_len = hash_name(nd->path.dentry, name); 22121da177e4SLinus Torvalds 2213fe479a58SAl Viro type = LAST_NORM; 2214d6bb3e90SLinus Torvalds if (name[0] == '.') switch (hashlen_len(hash_len)) { 2215fe479a58SAl Viro case 2: 2216200e9ef7SLinus Torvalds if (name[1] == '.') { 2217fe479a58SAl Viro type = LAST_DOTDOT; 221816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 221916c2cd71SAl Viro } 2220fe479a58SAl Viro break; 2221fe479a58SAl Viro case 1: 2222fe479a58SAl Viro type = LAST_DOT; 2223fe479a58SAl Viro } 22245a202bcdSAl Viro if (likely(type == LAST_NORM)) { 22255a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 222616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 22275a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 2228a060dc50SJames Hogan struct qstr this = { { .hash_len = hash_len }, .name = name }; 2229da53be12SLinus Torvalds err = parent->d_op->d_hash(parent, &this); 22305a202bcdSAl Viro if (err < 0) 22313595e234SAl Viro return err; 2232d6bb3e90SLinus Torvalds hash_len = this.hash_len; 2233d6bb3e90SLinus Torvalds name = this.name; 22345a202bcdSAl Viro } 22355a202bcdSAl Viro } 2236fe479a58SAl Viro 2237d6bb3e90SLinus Torvalds nd->last.hash_len = hash_len; 2238d6bb3e90SLinus Torvalds nd->last.name = name; 22395f4a6a69SAl Viro nd->last_type = type; 22405f4a6a69SAl Viro 2241d6bb3e90SLinus Torvalds name += hashlen_len(hash_len); 2242d6bb3e90SLinus Torvalds if (!*name) 2243bdf6cbf1SAl Viro goto OK; 2244200e9ef7SLinus Torvalds /* 2245200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 2246200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 2247200e9ef7SLinus Torvalds */ 2248200e9ef7SLinus Torvalds do { 2249d6bb3e90SLinus Torvalds name++; 2250d6bb3e90SLinus Torvalds } while (unlikely(*name == '/')); 22518620c238SAl Viro if (unlikely(!*name)) { 22528620c238SAl Viro OK: 2253d8d4611aSAl Viro /* pathname or trailing symlink, done */ 2254c108837eSAl Viro if (!depth) { 2255549c7297SChristian Brauner nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode); 22560f705953SAl Viro nd->dir_mode = nd->inode->i_mode; 2257c108837eSAl Viro nd->flags &= ~LOOKUP_PARENT; 22588620c238SAl Viro return 0; 2259c108837eSAl Viro } 22608620c238SAl Viro /* last component of nested symlink */ 2261d8d4611aSAl Viro name = nd->stack[--depth].name; 22628c4efe22SAl Viro link = walk_component(nd, 0); 22631c4ff1a8SAl Viro } else { 22641c4ff1a8SAl Viro /* not the last component */ 22658c4efe22SAl Viro link = walk_component(nd, WALK_MORE); 22668620c238SAl Viro } 226792d27016SAl Viro if (unlikely(link)) { 226892d27016SAl Viro if (IS_ERR(link)) 226992d27016SAl Viro return PTR_ERR(link); 227092d27016SAl Viro /* a symlink to follow */ 2271d8d4611aSAl Viro nd->stack[depth++].name = name; 227292d27016SAl Viro name = link; 22739e18f10aSAl Viro continue; 227448c8b0c5SAl Viro } 227597242f99SAl Viro if (unlikely(!d_can_lookup(nd->path.dentry))) { 227697242f99SAl Viro if (nd->flags & LOOKUP_RCU) { 2277e36cffedSJens Axboe if (!try_to_unlazy(nd)) 227897242f99SAl Viro return -ECHILD; 227997242f99SAl Viro } 22803595e234SAl Viro return -ENOTDIR; 22815f4a6a69SAl Viro } 2282ce57dfc1SAl Viro } 228397242f99SAl Viro } 22841da177e4SLinus Torvalds 2285edc2b1daSAl Viro /* must be paired with terminate_walk() */ 2286c8a53ee5SAl Viro static const char *path_init(struct nameidata *nd, unsigned flags) 228731e6b01fSNick Piggin { 2288740a1678SAleksa Sarai int error; 2289c8a53ee5SAl Viro const char *s = nd->name->name; 229031e6b01fSNick Piggin 22916c6ec2b0SJens Axboe /* LOOKUP_CACHED requires RCU, ask caller to retry */ 22926c6ec2b0SJens Axboe if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) 22936c6ec2b0SJens Axboe return ERR_PTR(-EAGAIN); 22946c6ec2b0SJens Axboe 2295c0eb027eSLinus Torvalds if (!*s) 2296c0eb027eSLinus Torvalds flags &= ~LOOKUP_RCU; 2297edc2b1daSAl Viro if (flags & LOOKUP_RCU) 2298edc2b1daSAl Viro rcu_read_lock(); 2299c0eb027eSLinus Torvalds 2300c108837eSAl Viro nd->flags = flags | LOOKUP_JUMPED; 230131e6b01fSNick Piggin nd->depth = 0; 2302ab87f9a5SAleksa Sarai 2303ab87f9a5SAleksa Sarai nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); 2304ab87f9a5SAleksa Sarai nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); 2305ab87f9a5SAleksa Sarai smp_rmb(); 2306ab87f9a5SAleksa Sarai 23075b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 2308b18825a7SDavid Howells struct dentry *root = nd->root.dentry; 2309b18825a7SDavid Howells struct inode *inode = root->d_inode; 231093893862SAl Viro if (*s && unlikely(!d_can_lookup(root))) 2311368ee9baSAl Viro return ERR_PTR(-ENOTDIR); 23125b6ca027SAl Viro nd->path = nd->root; 23135b6ca027SAl Viro nd->inode = inode; 23145b6ca027SAl Viro if (flags & LOOKUP_RCU) { 2315ab87f9a5SAleksa Sarai nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 23168f47a016SAl Viro nd->root_seq = nd->seq; 23175b6ca027SAl Viro } else { 23185b6ca027SAl Viro path_get(&nd->path); 23195b6ca027SAl Viro } 2320368ee9baSAl Viro return s; 23215b6ca027SAl Viro } 23225b6ca027SAl Viro 232331e6b01fSNick Piggin nd->root.mnt = NULL; 2324248fb5b9SAl Viro nd->path.mnt = NULL; 2325248fb5b9SAl Viro nd->path.dentry = NULL; 232631e6b01fSNick Piggin 23278db52c7eSAleksa Sarai /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ 23288db52c7eSAleksa Sarai if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { 2329740a1678SAleksa Sarai error = nd_jump_root(nd); 2330740a1678SAleksa Sarai if (unlikely(error)) 2331740a1678SAleksa Sarai return ERR_PTR(error); 2332ef55d917SAl Viro return s; 23338db52c7eSAleksa Sarai } 23348db52c7eSAleksa Sarai 23358db52c7eSAleksa Sarai /* Relative pathname -- get the starting-point it is relative to. */ 23368db52c7eSAleksa Sarai if (nd->dfd == AT_FDCWD) { 2337e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 233831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 2339c28cc364SNick Piggin unsigned seq; 234031e6b01fSNick Piggin 2341c28cc364SNick Piggin do { 2342c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 234331e6b01fSNick Piggin nd->path = fs->pwd; 2344ef55d917SAl Viro nd->inode = nd->path.dentry->d_inode; 2345c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 2346c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 2347e41f7d4eSAl Viro } else { 2348e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 2349ef55d917SAl Viro nd->inode = nd->path.dentry->d_inode; 2350e41f7d4eSAl Viro } 235131e6b01fSNick Piggin } else { 2352582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 2353c8a53ee5SAl Viro struct fd f = fdget_raw(nd->dfd); 235431e6b01fSNick Piggin struct dentry *dentry; 235531e6b01fSNick Piggin 23562903ff01SAl Viro if (!f.file) 2357368ee9baSAl Viro return ERR_PTR(-EBADF); 235831e6b01fSNick Piggin 23592903ff01SAl Viro dentry = f.file->f_path.dentry; 236031e6b01fSNick Piggin 2361edc2b1daSAl Viro if (*s && unlikely(!d_can_lookup(dentry))) { 23622903ff01SAl Viro fdput(f); 2363368ee9baSAl Viro return ERR_PTR(-ENOTDIR); 2364f52e0c11SAl Viro } 23652903ff01SAl Viro 23662903ff01SAl Viro nd->path = f.file->f_path; 2367e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 236834a26b99SAl Viro nd->inode = nd->path.dentry->d_inode; 236934a26b99SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 23705590ff0dSUlrich Drepper } else { 23712903ff01SAl Viro path_get(&nd->path); 237234a26b99SAl Viro nd->inode = nd->path.dentry->d_inode; 23731da177e4SLinus Torvalds } 237434a26b99SAl Viro fdput(f); 2375e41f7d4eSAl Viro } 23768db52c7eSAleksa Sarai 2377adb21d2bSAleksa Sarai /* For scoped-lookups we need to set the root to the dirfd as well. */ 2378adb21d2bSAleksa Sarai if (flags & LOOKUP_IS_SCOPED) { 2379adb21d2bSAleksa Sarai nd->root = nd->path; 2380adb21d2bSAleksa Sarai if (flags & LOOKUP_RCU) { 2381adb21d2bSAleksa Sarai nd->root_seq = nd->seq; 2382adb21d2bSAleksa Sarai } else { 2383adb21d2bSAleksa Sarai path_get(&nd->root); 2384adb21d2bSAleksa Sarai nd->flags |= LOOKUP_ROOT_GRABBED; 2385adb21d2bSAleksa Sarai } 2386adb21d2bSAleksa Sarai } 2387adb21d2bSAleksa Sarai return s; 23889b4a9b14SAl Viro } 23899b4a9b14SAl Viro 23901ccac622SAl Viro static inline const char *lookup_last(struct nameidata *nd) 239195fa25d9SAl Viro { 2392bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 2393bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2394bd92d7feSAl Viro 2395c108837eSAl Viro return walk_component(nd, WALK_TRAILING); 2396bd92d7feSAl Viro } 2397bd92d7feSAl Viro 23984f757f3cSAl Viro static int handle_lookup_down(struct nameidata *nd) 23994f757f3cSAl Viro { 2400c153007bSAl Viro if (!(nd->flags & LOOKUP_RCU)) 2401db3c9adeSAl Viro dget(nd->path.dentry); 2402b0417d2cSAl Viro return PTR_ERR(step_into(nd, WALK_NOFOLLOW, 2403b0417d2cSAl Viro nd->path.dentry, nd->inode, nd->seq)); 24044f757f3cSAl Viro } 24054f757f3cSAl Viro 24069b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2407c8a53ee5SAl Viro static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) 24089b4a9b14SAl Viro { 2409c8a53ee5SAl Viro const char *s = path_init(nd, flags); 2410bd92d7feSAl Viro int err; 241131e6b01fSNick Piggin 24129b5858e9SAl Viro if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { 24134f757f3cSAl Viro err = handle_lookup_down(nd); 24145f336e72SAl Viro if (unlikely(err < 0)) 24155f336e72SAl Viro s = ERR_PTR(err); 24164f757f3cSAl Viro } 24174f757f3cSAl Viro 24181ccac622SAl Viro while (!(err = link_path_walk(s, nd)) && 24191ccac622SAl Viro (s = lookup_last(nd)) != NULL) 24201ccac622SAl Viro ; 24219f1fafeeSAl Viro if (!err) 24229f1fafeeSAl Viro err = complete_walk(nd); 2423bd92d7feSAl Viro 2424deb106c6SAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) 2425deb106c6SAl Viro if (!d_can_lookup(nd->path.dentry)) 2426bd23a539SAl Viro err = -ENOTDIR; 2427161aff1dSAl Viro if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { 2428161aff1dSAl Viro err = handle_lookup_down(nd); 2429161aff1dSAl Viro nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please... 2430161aff1dSAl Viro } 2431625b6d10SAl Viro if (!err) { 2432625b6d10SAl Viro *path = nd->path; 2433625b6d10SAl Viro nd->path.mnt = NULL; 2434625b6d10SAl Viro nd->path.dentry = NULL; 2435625b6d10SAl Viro } 2436deb106c6SAl Viro terminate_walk(nd); 2437bd92d7feSAl Viro return err; 243831e6b01fSNick Piggin } 243931e6b01fSNick Piggin 244031d921c7SDavid Howells int filename_lookup(int dfd, struct filename *name, unsigned flags, 24419ad1aaa6SAl Viro struct path *path, struct path *root) 2442873f1eedSJeff Layton { 2443894bc8c4SAl Viro int retval; 24449883d185SAl Viro struct nameidata nd; 2445abc9f5beSAl Viro if (IS_ERR(name)) 2446abc9f5beSAl Viro return PTR_ERR(name); 24479ad1aaa6SAl Viro if (unlikely(root)) { 24489ad1aaa6SAl Viro nd.root = *root; 24499ad1aaa6SAl Viro flags |= LOOKUP_ROOT; 24509ad1aaa6SAl Viro } 24519883d185SAl Viro set_nameidata(&nd, dfd, name); 2452c8a53ee5SAl Viro retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); 2453873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2454c8a53ee5SAl Viro retval = path_lookupat(&nd, flags, path); 2455873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2456c8a53ee5SAl Viro retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); 2457873f1eedSJeff Layton 2458873f1eedSJeff Layton if (likely(!retval)) 2459161aff1dSAl Viro audit_inode(name, path->dentry, 2460161aff1dSAl Viro flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); 24619883d185SAl Viro restore_nameidata(); 2462e4bd1c1aSAl Viro putname(name); 2463873f1eedSJeff Layton return retval; 2464873f1eedSJeff Layton } 2465873f1eedSJeff Layton 24668bcb77faSAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2467c8a53ee5SAl Viro static int path_parentat(struct nameidata *nd, unsigned flags, 2468391172c4SAl Viro struct path *parent) 24698bcb77faSAl Viro { 2470c8a53ee5SAl Viro const char *s = path_init(nd, flags); 24719b5858e9SAl Viro int err = link_path_walk(s, nd); 24728bcb77faSAl Viro if (!err) 24738bcb77faSAl Viro err = complete_walk(nd); 2474391172c4SAl Viro if (!err) { 2475391172c4SAl Viro *parent = nd->path; 2476391172c4SAl Viro nd->path.mnt = NULL; 2477391172c4SAl Viro nd->path.dentry = NULL; 2478391172c4SAl Viro } 2479deb106c6SAl Viro terminate_walk(nd); 24808bcb77faSAl Viro return err; 24818bcb77faSAl Viro } 24828bcb77faSAl Viro 24835c31b6ceSAl Viro static struct filename *filename_parentat(int dfd, struct filename *name, 2484391172c4SAl Viro unsigned int flags, struct path *parent, 2485391172c4SAl Viro struct qstr *last, int *type) 24868bcb77faSAl Viro { 24878bcb77faSAl Viro int retval; 24889883d185SAl Viro struct nameidata nd; 24898bcb77faSAl Viro 24905c31b6ceSAl Viro if (IS_ERR(name)) 24915c31b6ceSAl Viro return name; 24929883d185SAl Viro set_nameidata(&nd, dfd, name); 2493c8a53ee5SAl Viro retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); 24948bcb77faSAl Viro if (unlikely(retval == -ECHILD)) 2495c8a53ee5SAl Viro retval = path_parentat(&nd, flags, parent); 24968bcb77faSAl Viro if (unlikely(retval == -ESTALE)) 2497c8a53ee5SAl Viro retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); 2498391172c4SAl Viro if (likely(!retval)) { 2499391172c4SAl Viro *last = nd.last; 2500391172c4SAl Viro *type = nd.last_type; 2501c9b07eabSAl Viro audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); 25025c31b6ceSAl Viro } else { 25035c31b6ceSAl Viro putname(name); 25045c31b6ceSAl Viro name = ERR_PTR(retval); 2505391172c4SAl Viro } 25069883d185SAl Viro restore_nameidata(); 25075c31b6ceSAl Viro return name; 25088bcb77faSAl Viro } 25098bcb77faSAl Viro 251079714f72SAl Viro /* does lookup, returns the object with parent locked */ 251179714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 25125590ff0dSUlrich Drepper { 25135c31b6ceSAl Viro struct filename *filename; 25145c31b6ceSAl Viro struct dentry *d; 2515391172c4SAl Viro struct qstr last; 2516391172c4SAl Viro int type; 251751689104SPaul Moore 25185c31b6ceSAl Viro filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, 25195c31b6ceSAl Viro &last, &type); 252051689104SPaul Moore if (IS_ERR(filename)) 252151689104SPaul Moore return ERR_CAST(filename); 25225c31b6ceSAl Viro if (unlikely(type != LAST_NORM)) { 2523391172c4SAl Viro path_put(path); 25245c31b6ceSAl Viro putname(filename); 25255c31b6ceSAl Viro return ERR_PTR(-EINVAL); 252679714f72SAl Viro } 25275955102cSAl Viro inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 2528391172c4SAl Viro d = __lookup_hash(&last, path->dentry, 0); 252979714f72SAl Viro if (IS_ERR(d)) { 25305955102cSAl Viro inode_unlock(path->dentry->d_inode); 2531391172c4SAl Viro path_put(path); 253279714f72SAl Viro } 253351689104SPaul Moore putname(filename); 253479714f72SAl Viro return d; 25355590ff0dSUlrich Drepper } 25365590ff0dSUlrich Drepper 2537d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2538d1811465SAl Viro { 2539abc9f5beSAl Viro return filename_lookup(AT_FDCWD, getname_kernel(name), 2540abc9f5beSAl Viro flags, path, NULL); 2541d1811465SAl Viro } 25424d359507SAl Viro EXPORT_SYMBOL(kern_path); 2543d1811465SAl Viro 254416f18200SJosef 'Jeff' Sipek /** 254516f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 254616f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 254716f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 254816f18200SJosef 'Jeff' Sipek * @name: pointer to file name 254916f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2550e0a01249SAl Viro * @path: pointer to struct path to fill 255116f18200SJosef 'Jeff' Sipek */ 255216f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 255316f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2554e0a01249SAl Viro struct path *path) 255516f18200SJosef 'Jeff' Sipek { 25569ad1aaa6SAl Viro struct path root = {.mnt = mnt, .dentry = dentry}; 25579ad1aaa6SAl Viro /* the first argument of filename_lookup() is ignored with root */ 2558abc9f5beSAl Viro return filename_lookup(AT_FDCWD, getname_kernel(name), 2559abc9f5beSAl Viro flags , path, &root); 256016f18200SJosef 'Jeff' Sipek } 25614d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup); 256216f18200SJosef 'Jeff' Sipek 25633c95f0dcSAl Viro static int lookup_one_len_common(const char *name, struct dentry *base, 25643c95f0dcSAl Viro int len, struct qstr *this) 25653c95f0dcSAl Viro { 25663c95f0dcSAl Viro this->name = name; 25673c95f0dcSAl Viro this->len = len; 25683c95f0dcSAl Viro this->hash = full_name_hash(base, name, len); 25693c95f0dcSAl Viro if (!len) 25703c95f0dcSAl Viro return -EACCES; 25713c95f0dcSAl Viro 25723c95f0dcSAl Viro if (unlikely(name[0] == '.')) { 25733c95f0dcSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 25743c95f0dcSAl Viro return -EACCES; 25753c95f0dcSAl Viro } 25763c95f0dcSAl Viro 25773c95f0dcSAl Viro while (len--) { 25783c95f0dcSAl Viro unsigned int c = *(const unsigned char *)name++; 25793c95f0dcSAl Viro if (c == '/' || c == '\0') 25803c95f0dcSAl Viro return -EACCES; 25813c95f0dcSAl Viro } 25823c95f0dcSAl Viro /* 25833c95f0dcSAl Viro * See if the low-level filesystem might want 25843c95f0dcSAl Viro * to use its own hash.. 25853c95f0dcSAl Viro */ 25863c95f0dcSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 25873c95f0dcSAl Viro int err = base->d_op->d_hash(base, this); 25883c95f0dcSAl Viro if (err < 0) 25893c95f0dcSAl Viro return err; 25903c95f0dcSAl Viro } 25913c95f0dcSAl Viro 259247291baaSChristian Brauner return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC); 25933c95f0dcSAl Viro } 25943c95f0dcSAl Viro 2595eead1911SChristoph Hellwig /** 25960da0b7fdSDavid Howells * try_lookup_one_len - filesystem helper to lookup single pathname component 25970da0b7fdSDavid Howells * @name: pathname component to lookup 25980da0b7fdSDavid Howells * @base: base directory to lookup from 25990da0b7fdSDavid Howells * @len: maximum length @len should be interpreted to 26000da0b7fdSDavid Howells * 26010da0b7fdSDavid Howells * Look up a dentry by name in the dcache, returning NULL if it does not 26020da0b7fdSDavid Howells * currently exist. The function does not try to create a dentry. 26030da0b7fdSDavid Howells * 26040da0b7fdSDavid Howells * Note that this routine is purely a helper for filesystem usage and should 26050da0b7fdSDavid Howells * not be called by generic code. 26060da0b7fdSDavid Howells * 26070da0b7fdSDavid Howells * The caller must hold base->i_mutex. 26080da0b7fdSDavid Howells */ 26090da0b7fdSDavid Howells struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) 26100da0b7fdSDavid Howells { 26110da0b7fdSDavid Howells struct qstr this; 26120da0b7fdSDavid Howells int err; 26130da0b7fdSDavid Howells 26140da0b7fdSDavid Howells WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 26150da0b7fdSDavid Howells 26160da0b7fdSDavid Howells err = lookup_one_len_common(name, base, len, &this); 26170da0b7fdSDavid Howells if (err) 26180da0b7fdSDavid Howells return ERR_PTR(err); 26190da0b7fdSDavid Howells 26200da0b7fdSDavid Howells return lookup_dcache(&this, base, 0); 26210da0b7fdSDavid Howells } 26220da0b7fdSDavid Howells EXPORT_SYMBOL(try_lookup_one_len); 26230da0b7fdSDavid Howells 26240da0b7fdSDavid Howells /** 2625a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2626eead1911SChristoph Hellwig * @name: pathname component to lookup 2627eead1911SChristoph Hellwig * @base: base directory to lookup from 2628eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2629eead1911SChristoph Hellwig * 2630a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 26319e7543e9SAl Viro * not be called by generic code. 2632bbddca8eSNeilBrown * 2633bbddca8eSNeilBrown * The caller must hold base->i_mutex. 2634eead1911SChristoph Hellwig */ 2635057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2636057f6c01SJames Morris { 26378613a209SAl Viro struct dentry *dentry; 2638057f6c01SJames Morris struct qstr this; 2639cda309deSMiklos Szeredi int err; 2640057f6c01SJames Morris 26415955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 26422f9092e1SDavid Woodhouse 26433c95f0dcSAl Viro err = lookup_one_len_common(name, base, len, &this); 2644cda309deSMiklos Szeredi if (err) 2645cda309deSMiklos Szeredi return ERR_PTR(err); 2646cda309deSMiklos Szeredi 26478613a209SAl Viro dentry = lookup_dcache(&this, base, 0); 26488613a209SAl Viro return dentry ? dentry : __lookup_slow(&this, base, 0); 2649057f6c01SJames Morris } 26504d359507SAl Viro EXPORT_SYMBOL(lookup_one_len); 2651057f6c01SJames Morris 2652bbddca8eSNeilBrown /** 2653bbddca8eSNeilBrown * lookup_one_len_unlocked - filesystem helper to lookup single pathname component 2654bbddca8eSNeilBrown * @name: pathname component to lookup 2655bbddca8eSNeilBrown * @base: base directory to lookup from 2656bbddca8eSNeilBrown * @len: maximum length @len should be interpreted to 2657bbddca8eSNeilBrown * 2658bbddca8eSNeilBrown * Note that this routine is purely a helper for filesystem usage and should 2659bbddca8eSNeilBrown * not be called by generic code. 2660bbddca8eSNeilBrown * 2661bbddca8eSNeilBrown * Unlike lookup_one_len, it should be called without the parent 2662bbddca8eSNeilBrown * i_mutex held, and will take the i_mutex itself if necessary. 2663bbddca8eSNeilBrown */ 2664bbddca8eSNeilBrown struct dentry *lookup_one_len_unlocked(const char *name, 2665bbddca8eSNeilBrown struct dentry *base, int len) 2666bbddca8eSNeilBrown { 2667bbddca8eSNeilBrown struct qstr this; 2668bbddca8eSNeilBrown int err; 266920d00ee8SLinus Torvalds struct dentry *ret; 2670bbddca8eSNeilBrown 26713c95f0dcSAl Viro err = lookup_one_len_common(name, base, len, &this); 2672bbddca8eSNeilBrown if (err) 2673bbddca8eSNeilBrown return ERR_PTR(err); 2674bbddca8eSNeilBrown 267520d00ee8SLinus Torvalds ret = lookup_dcache(&this, base, 0); 267620d00ee8SLinus Torvalds if (!ret) 267720d00ee8SLinus Torvalds ret = lookup_slow(&this, base, 0); 267820d00ee8SLinus Torvalds return ret; 2679bbddca8eSNeilBrown } 2680bbddca8eSNeilBrown EXPORT_SYMBOL(lookup_one_len_unlocked); 2681bbddca8eSNeilBrown 26826c2d4798SAl Viro /* 26836c2d4798SAl Viro * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) 26846c2d4798SAl Viro * on negatives. Returns known positive or ERR_PTR(); that's what 26856c2d4798SAl Viro * most of the users want. Note that pinned negative with unlocked parent 26866c2d4798SAl Viro * _can_ become positive at any time, so callers of lookup_one_len_unlocked() 26876c2d4798SAl Viro * need to be very careful; pinned positives have ->d_inode stable, so 26886c2d4798SAl Viro * this one avoids such problems. 26896c2d4798SAl Viro */ 26906c2d4798SAl Viro struct dentry *lookup_positive_unlocked(const char *name, 26916c2d4798SAl Viro struct dentry *base, int len) 26926c2d4798SAl Viro { 26936c2d4798SAl Viro struct dentry *ret = lookup_one_len_unlocked(name, base, len); 26942fa6b1e0SAl Viro if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { 26956c2d4798SAl Viro dput(ret); 26966c2d4798SAl Viro ret = ERR_PTR(-ENOENT); 26976c2d4798SAl Viro } 26986c2d4798SAl Viro return ret; 26996c2d4798SAl Viro } 27006c2d4798SAl Viro EXPORT_SYMBOL(lookup_positive_unlocked); 27016c2d4798SAl Viro 2702eedf265aSEric W. Biederman #ifdef CONFIG_UNIX98_PTYS 2703eedf265aSEric W. Biederman int path_pts(struct path *path) 2704eedf265aSEric W. Biederman { 2705eedf265aSEric W. Biederman /* Find something mounted on "pts" in the same directory as 2706eedf265aSEric W. Biederman * the input path. 2707eedf265aSEric W. Biederman */ 2708a6a7eb76SAl Viro struct dentry *parent = dget_parent(path->dentry); 2709a6a7eb76SAl Viro struct dentry *child; 271019f6028aSAl Viro struct qstr this = QSTR_INIT("pts", 3); 2711eedf265aSEric W. Biederman 2712a6a7eb76SAl Viro if (unlikely(!path_connected(path->mnt, parent))) { 2713a6a7eb76SAl Viro dput(parent); 271463b27720SAl Viro return -ENOENT; 2715a6a7eb76SAl Viro } 271663b27720SAl Viro dput(path->dentry); 271763b27720SAl Viro path->dentry = parent; 2718eedf265aSEric W. Biederman child = d_hash_and_lookup(parent, &this); 2719eedf265aSEric W. Biederman if (!child) 2720eedf265aSEric W. Biederman return -ENOENT; 2721eedf265aSEric W. Biederman 2722eedf265aSEric W. Biederman path->dentry = child; 2723eedf265aSEric W. Biederman dput(parent); 272419f6028aSAl Viro follow_down(path); 2725eedf265aSEric W. Biederman return 0; 2726eedf265aSEric W. Biederman } 2727eedf265aSEric W. Biederman #endif 2728eedf265aSEric W. Biederman 27291fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 27301fa1e7f6SAndy Whitcroft struct path *path, int *empty) 27311da177e4SLinus Torvalds { 2732abc9f5beSAl Viro return filename_lookup(dfd, getname_flags(name, flags, empty), 2733abc9f5beSAl Viro flags, path, NULL); 27341da177e4SLinus Torvalds } 2735b853a161SAl Viro EXPORT_SYMBOL(user_path_at_empty); 27361fa1e7f6SAndy Whitcroft 2737ba73d987SChristian Brauner int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir, 2738ba73d987SChristian Brauner struct inode *inode) 27391da177e4SLinus Torvalds { 27408e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2741da9592edSDavid Howells 2742ba73d987SChristian Brauner if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid)) 27431da177e4SLinus Torvalds return 0; 2744ba73d987SChristian Brauner if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid)) 27451da177e4SLinus Torvalds return 0; 2746ba73d987SChristian Brauner return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER); 27471da177e4SLinus Torvalds } 2748cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky); 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds /* 27511da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 27521da177e4SLinus Torvalds * whether the type of victim is right. 27531da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 27541da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 27551da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 27561da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 27571da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 27581da177e4SLinus Torvalds * a. be owner of dir, or 27591da177e4SLinus Torvalds * b. be owner of victim, or 27601da177e4SLinus Torvalds * c. have CAP_FOWNER capability 27611da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 27621da177e4SLinus Torvalds * links pointing to it. 27630bd23d09SEric W. Biederman * 7. If the victim has an unknown uid or gid we can't change the inode. 27640bd23d09SEric W. Biederman * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. 27650bd23d09SEric W. Biederman * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. 27660bd23d09SEric W. Biederman * 10. We can't remove a root or mountpoint. 27670bd23d09SEric W. Biederman * 11. We don't allow removal of NFS sillyrenamed files; it's handled by 27681da177e4SLinus Torvalds * nfs_async_unlink(). 27691da177e4SLinus Torvalds */ 2770ba73d987SChristian Brauner static int may_delete(struct user_namespace *mnt_userns, struct inode *dir, 2771ba73d987SChristian Brauner struct dentry *victim, bool isdir) 27721da177e4SLinus Torvalds { 277363afdfc7SDavid Howells struct inode *inode = d_backing_inode(victim); 27741da177e4SLinus Torvalds int error; 27751da177e4SLinus Torvalds 2776b18825a7SDavid Howells if (d_is_negative(victim)) 27771da177e4SLinus Torvalds return -ENOENT; 2778b18825a7SDavid Howells BUG_ON(!inode); 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 2781593d1ce8SEric W. Biederman 2782593d1ce8SEric W. Biederman /* Inode writeback is not safe when the uid or gid are invalid. */ 2783ba73d987SChristian Brauner if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || 2784ba73d987SChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 2785593d1ce8SEric W. Biederman return -EOVERFLOW; 2786593d1ce8SEric W. Biederman 27874fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 27881da177e4SLinus Torvalds 2789ba73d987SChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 27901da177e4SLinus Torvalds if (error) 27911da177e4SLinus Torvalds return error; 27921da177e4SLinus Torvalds if (IS_APPEND(dir)) 27931da177e4SLinus Torvalds return -EPERM; 2794b18825a7SDavid Howells 2795ba73d987SChristian Brauner if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) || 2796ba73d987SChristian Brauner IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || 2797ba73d987SChristian Brauner HAS_UNMAPPED_ID(mnt_userns, inode)) 27981da177e4SLinus Torvalds return -EPERM; 27991da177e4SLinus Torvalds if (isdir) { 280044b1d530SMiklos Szeredi if (!d_is_dir(victim)) 28011da177e4SLinus Torvalds return -ENOTDIR; 28021da177e4SLinus Torvalds if (IS_ROOT(victim)) 28031da177e4SLinus Torvalds return -EBUSY; 280444b1d530SMiklos Szeredi } else if (d_is_dir(victim)) 28051da177e4SLinus Torvalds return -EISDIR; 28061da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 28071da177e4SLinus Torvalds return -ENOENT; 28081da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 28091da177e4SLinus Torvalds return -EBUSY; 28101da177e4SLinus Torvalds return 0; 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 28141da177e4SLinus Torvalds * dir. 28151da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 28161da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 28171da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 2818036d5236SEric W. Biederman * 3. We can't do it if the fs can't represent the fsuid or fsgid. 2819036d5236SEric W. Biederman * 4. We should have write and exec permissions on dir 2820036d5236SEric W. Biederman * 5. We can't do it if dir is immutable (done in permission()) 28211da177e4SLinus Torvalds */ 2822ba73d987SChristian Brauner static inline int may_create(struct user_namespace *mnt_userns, 2823ba73d987SChristian Brauner struct inode *dir, struct dentry *child) 28241da177e4SLinus Torvalds { 2825036d5236SEric W. Biederman struct user_namespace *s_user_ns; 282614e972b4SJeff Layton audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); 28271da177e4SLinus Torvalds if (child->d_inode) 28281da177e4SLinus Torvalds return -EEXIST; 28291da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 28301da177e4SLinus Torvalds return -ENOENT; 2831036d5236SEric W. Biederman s_user_ns = dir->i_sb->s_user_ns; 2832ba73d987SChristian Brauner if (!kuid_has_mapping(s_user_ns, fsuid_into_mnt(mnt_userns)) || 2833ba73d987SChristian Brauner !kgid_has_mapping(s_user_ns, fsgid_into_mnt(mnt_userns))) 2834036d5236SEric W. Biederman return -EOVERFLOW; 2835ba73d987SChristian Brauner return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 28361da177e4SLinus Torvalds } 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds /* 28391da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 28401da177e4SLinus Torvalds */ 28411da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 28421da177e4SLinus Torvalds { 28431da177e4SLinus Torvalds struct dentry *p; 28441da177e4SLinus Torvalds 28451da177e4SLinus Torvalds if (p1 == p2) { 28465955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 28471da177e4SLinus Torvalds return NULL; 28481da177e4SLinus Torvalds } 28491da177e4SLinus Torvalds 2850fc64005cSAl Viro mutex_lock(&p1->d_sb->s_vfs_rename_mutex); 28511da177e4SLinus Torvalds 2852e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2853e2761a11SOGAWA Hirofumi if (p) { 28545955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); 28555955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); 28561da177e4SLinus Torvalds return p; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 2859e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2860e2761a11SOGAWA Hirofumi if (p) { 28615955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 28625955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); 28631da177e4SLinus Torvalds return p; 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds 28665955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 28675955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); 28681da177e4SLinus Torvalds return NULL; 28691da177e4SLinus Torvalds } 28704d359507SAl Viro EXPORT_SYMBOL(lock_rename); 28711da177e4SLinus Torvalds 28721da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 28731da177e4SLinus Torvalds { 28745955102cSAl Viro inode_unlock(p1->d_inode); 28751da177e4SLinus Torvalds if (p1 != p2) { 28765955102cSAl Viro inode_unlock(p2->d_inode); 2877fc64005cSAl Viro mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); 28781da177e4SLinus Torvalds } 28791da177e4SLinus Torvalds } 28804d359507SAl Viro EXPORT_SYMBOL(unlock_rename); 28811da177e4SLinus Torvalds 28826521f891SChristian Brauner /** 28836521f891SChristian Brauner * vfs_create - create new file 28846521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 28856521f891SChristian Brauner * @dir: inode of @dentry 28866521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 28876521f891SChristian Brauner * @mode: mode of the new file 28886521f891SChristian Brauner * @want_excl: whether the file must not yet exist 28896521f891SChristian Brauner * 28906521f891SChristian Brauner * Create a new file. 28916521f891SChristian Brauner * 28926521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 28936521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 28946521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 28956521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 28966521f891SChristian Brauner * raw inode simply passs init_user_ns. 28976521f891SChristian Brauner */ 28986521f891SChristian Brauner int vfs_create(struct user_namespace *mnt_userns, struct inode *dir, 28996521f891SChristian Brauner struct dentry *dentry, umode_t mode, bool want_excl) 29001da177e4SLinus Torvalds { 29016521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 29021da177e4SLinus Torvalds if (error) 29031da177e4SLinus Torvalds return error; 29041da177e4SLinus Torvalds 2905acfa4380SAl Viro if (!dir->i_op->create) 29061da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 29071da177e4SLinus Torvalds mode &= S_IALLUGO; 29081da177e4SLinus Torvalds mode |= S_IFREG; 29091da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 29101da177e4SLinus Torvalds if (error) 29111da177e4SLinus Torvalds return error; 2912549c7297SChristian Brauner error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl); 2913a74574aaSStephen Smalley if (!error) 2914f38aa942SAmy Griffis fsnotify_create(dir, dentry); 29151da177e4SLinus Torvalds return error; 29161da177e4SLinus Torvalds } 29174d359507SAl Viro EXPORT_SYMBOL(vfs_create); 29181da177e4SLinus Torvalds 29198e6c848eSAl Viro int vfs_mkobj(struct dentry *dentry, umode_t mode, 29208e6c848eSAl Viro int (*f)(struct dentry *, umode_t, void *), 29218e6c848eSAl Viro void *arg) 29228e6c848eSAl Viro { 29238e6c848eSAl Viro struct inode *dir = dentry->d_parent->d_inode; 2924ba73d987SChristian Brauner int error = may_create(&init_user_ns, dir, dentry); 29258e6c848eSAl Viro if (error) 29268e6c848eSAl Viro return error; 29278e6c848eSAl Viro 29288e6c848eSAl Viro mode &= S_IALLUGO; 29298e6c848eSAl Viro mode |= S_IFREG; 29308e6c848eSAl Viro error = security_inode_create(dir, dentry, mode); 29318e6c848eSAl Viro if (error) 29328e6c848eSAl Viro return error; 29338e6c848eSAl Viro error = f(dentry, mode, arg); 29348e6c848eSAl Viro if (!error) 29358e6c848eSAl Viro fsnotify_create(dir, dentry); 29368e6c848eSAl Viro return error; 29378e6c848eSAl Viro } 29388e6c848eSAl Viro EXPORT_SYMBOL(vfs_mkobj); 29398e6c848eSAl Viro 2940a2982cc9SEric W. Biederman bool may_open_dev(const struct path *path) 2941a2982cc9SEric W. Biederman { 2942a2982cc9SEric W. Biederman return !(path->mnt->mnt_flags & MNT_NODEV) && 2943a2982cc9SEric W. Biederman !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); 2944a2982cc9SEric W. Biederman } 2945a2982cc9SEric W. Biederman 2946ba73d987SChristian Brauner static int may_open(struct user_namespace *mnt_userns, const struct path *path, 2947ba73d987SChristian Brauner int acc_mode, int flag) 29481da177e4SLinus Torvalds { 29493fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 29501da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 29511da177e4SLinus Torvalds int error; 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds if (!inode) 29541da177e4SLinus Torvalds return -ENOENT; 29551da177e4SLinus Torvalds 2956c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2957c8fe8f30SChristoph Hellwig case S_IFLNK: 29581da177e4SLinus Torvalds return -ELOOP; 2959c8fe8f30SChristoph Hellwig case S_IFDIR: 2960fc4177beSKees Cook if (acc_mode & MAY_WRITE) 29611da177e4SLinus Torvalds return -EISDIR; 2962fc4177beSKees Cook if (acc_mode & MAY_EXEC) 2963fc4177beSKees Cook return -EACCES; 2964c8fe8f30SChristoph Hellwig break; 2965c8fe8f30SChristoph Hellwig case S_IFBLK: 2966c8fe8f30SChristoph Hellwig case S_IFCHR: 2967a2982cc9SEric W. Biederman if (!may_open_dev(path)) 29681da177e4SLinus Torvalds return -EACCES; 2969633fb6acSKees Cook fallthrough; 2970c8fe8f30SChristoph Hellwig case S_IFIFO: 2971c8fe8f30SChristoph Hellwig case S_IFSOCK: 2972633fb6acSKees Cook if (acc_mode & MAY_EXEC) 2973633fb6acSKees Cook return -EACCES; 29741da177e4SLinus Torvalds flag &= ~O_TRUNC; 2975c8fe8f30SChristoph Hellwig break; 29760fd338b2SKees Cook case S_IFREG: 29770fd338b2SKees Cook if ((acc_mode & MAY_EXEC) && path_noexec(path)) 29780fd338b2SKees Cook return -EACCES; 29790fd338b2SKees Cook break; 29804a3fd211SDave Hansen } 2981b41572e9SDave Hansen 2982ba73d987SChristian Brauner error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode); 2983b41572e9SDave Hansen if (error) 2984b41572e9SDave Hansen return error; 29856146f0d5SMimi Zohar 29861da177e4SLinus Torvalds /* 29871da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 29881da177e4SLinus Torvalds */ 29891da177e4SLinus Torvalds if (IS_APPEND(inode)) { 29908737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 29917715b521SAl Viro return -EPERM; 29921da177e4SLinus Torvalds if (flag & O_TRUNC) 29937715b521SAl Viro return -EPERM; 29941da177e4SLinus Torvalds } 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 2997ba73d987SChristian Brauner if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode)) 29987715b521SAl Viro return -EPERM; 29991da177e4SLinus Torvalds 3000f3c7691eSJ. Bruce Fields return 0; 30017715b521SAl Viro } 30027715b521SAl Viro 3003549c7297SChristian Brauner static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) 30047715b521SAl Viro { 3005f0bb5aafSAl Viro const struct path *path = &filp->f_path; 30067715b521SAl Viro struct inode *inode = path->dentry->d_inode; 30077715b521SAl Viro int error = get_write_access(inode); 30081da177e4SLinus Torvalds if (error) 30097715b521SAl Viro return error; 30101da177e4SLinus Torvalds /* 30111da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 30121da177e4SLinus Torvalds */ 3013d7a06983SJeff Layton error = locks_verify_locked(filp); 3014be6d3e56SKentaro Takeda if (!error) 3015ea0d3ab2STetsuo Handa error = security_path_truncate(path); 30161da177e4SLinus Torvalds if (!error) { 3017549c7297SChristian Brauner error = do_truncate(mnt_userns, path->dentry, 0, 3018d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 3019e1181ee6SJeff Layton filp); 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds put_write_access(inode); 3022acd0c935SMimi Zohar return error; 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds 3025d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 3026d57999e1SDave Hansen { 30278a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 30288a5e929dSAl Viro flag--; 3029d57999e1SDave Hansen return flag; 3030d57999e1SDave Hansen } 3031d57999e1SDave Hansen 3032ba73d987SChristian Brauner static int may_o_create(struct user_namespace *mnt_userns, 3033ba73d987SChristian Brauner const struct path *dir, struct dentry *dentry, 3034ba73d987SChristian Brauner umode_t mode) 3035d18e9008SMiklos Szeredi { 30361328c727SSeth Forshee struct user_namespace *s_user_ns; 3037d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 3038d18e9008SMiklos Szeredi if (error) 3039d18e9008SMiklos Szeredi return error; 3040d18e9008SMiklos Szeredi 30411328c727SSeth Forshee s_user_ns = dir->dentry->d_sb->s_user_ns; 3042ba73d987SChristian Brauner if (!kuid_has_mapping(s_user_ns, fsuid_into_mnt(mnt_userns)) || 3043ba73d987SChristian Brauner !kgid_has_mapping(s_user_ns, fsgid_into_mnt(mnt_userns))) 30441328c727SSeth Forshee return -EOVERFLOW; 30451328c727SSeth Forshee 3046ba73d987SChristian Brauner error = inode_permission(mnt_userns, dir->dentry->d_inode, 304747291baaSChristian Brauner MAY_WRITE | MAY_EXEC); 3048d18e9008SMiklos Szeredi if (error) 3049d18e9008SMiklos Szeredi return error; 3050d18e9008SMiklos Szeredi 3051d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 3052d18e9008SMiklos Szeredi } 3053d18e9008SMiklos Szeredi 30541acf0af9SDavid Howells /* 30551acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 30561acf0af9SDavid Howells * dentry. 30571acf0af9SDavid Howells * 30581acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 30591acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 30601acf0af9SDavid Howells * 306100a07c15SAl Viro * If the file was looked up only or didn't need creating, FMODE_OPENED won't 306200a07c15SAl Viro * be set. The caller will need to perform the open themselves. @path will 306300a07c15SAl Viro * have been updated to point to the new dentry. This may be negative. 30641acf0af9SDavid Howells * 30651acf0af9SDavid Howells * Returns an error code otherwise. 30661acf0af9SDavid Howells */ 3067239eb983SAl Viro static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, 3068239eb983SAl Viro struct file *file, 30693ec2eef1SAl Viro int open_flag, umode_t mode) 3070d18e9008SMiklos Szeredi { 3071d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 3072d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 3073d18e9008SMiklos Szeredi int error; 3074d18e9008SMiklos Szeredi 3075d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 3076d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 3077d18e9008SMiklos Szeredi 307830d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 307930d90494SAl Viro file->f_path.mnt = nd->path.mnt; 30800fb1ea09SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, 308144907d79SAl Viro open_to_namei_flags(open_flag), mode); 30826fbd0714SAl Viro d_lookup_done(dentry); 3083384f26e2SAl Viro if (!error) { 308464e1ac4dSAl Viro if (file->f_mode & FMODE_OPENED) { 30856fb968cdSAl Viro if (unlikely(dentry != file->f_path.dentry)) { 30866fb968cdSAl Viro dput(dentry); 30876fb968cdSAl Viro dentry = dget(file->f_path.dentry); 30886fb968cdSAl Viro } 308964e1ac4dSAl Viro } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 30902675a4ebSAl Viro error = -EIO; 3091384f26e2SAl Viro } else { 309230d90494SAl Viro if (file->f_path.dentry) { 3093d18e9008SMiklos Szeredi dput(dentry); 3094d18e9008SMiklos Szeredi dentry = file->f_path.dentry; 309510c64ceaSAl Viro } 3096239eb983SAl Viro if (unlikely(d_is_negative(dentry))) 3097a01e718fSAl Viro error = -ENOENT; 3098d18e9008SMiklos Szeredi } 3099d18e9008SMiklos Szeredi } 3100239eb983SAl Viro if (error) { 3101d18e9008SMiklos Szeredi dput(dentry); 3102239eb983SAl Viro dentry = ERR_PTR(error); 3103239eb983SAl Viro } 3104239eb983SAl Viro return dentry; 3105d18e9008SMiklos Szeredi } 3106d18e9008SMiklos Szeredi 310731e6b01fSNick Piggin /* 31081acf0af9SDavid Howells * Look up and maybe create and open the last component. 3109d58ffd35SMiklos Szeredi * 311000a07c15SAl Viro * Must be called with parent locked (exclusive in O_CREAT case). 3111d58ffd35SMiklos Szeredi * 311200a07c15SAl Viro * Returns 0 on success, that is, if 311300a07c15SAl Viro * the file was successfully atomically created (if necessary) and opened, or 311400a07c15SAl Viro * the file was not completely opened at this time, though lookups and 311500a07c15SAl Viro * creations were performed. 311600a07c15SAl Viro * These case are distinguished by presence of FMODE_OPENED on file->f_mode. 311700a07c15SAl Viro * In the latter case dentry returned in @path might be negative if O_CREAT 311800a07c15SAl Viro * hadn't been specified. 31191acf0af9SDavid Howells * 312000a07c15SAl Viro * An error code is returned on failure. 3121d58ffd35SMiklos Szeredi */ 3122da5ebf5aSAl Viro static struct dentry *lookup_open(struct nameidata *nd, struct file *file, 3123d58ffd35SMiklos Szeredi const struct open_flags *op, 31243ec2eef1SAl Viro bool got_write) 3125d58ffd35SMiklos Szeredi { 3126549c7297SChristian Brauner struct user_namespace *mnt_userns; 3127d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 312854ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 31291643b43fSAl Viro int open_flag = op->open_flag; 3130d58ffd35SMiklos Szeredi struct dentry *dentry; 31311643b43fSAl Viro int error, create_error = 0; 31321643b43fSAl Viro umode_t mode = op->mode; 31336fbd0714SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 3134d58ffd35SMiklos Szeredi 3135ce8644fcSAl Viro if (unlikely(IS_DEADDIR(dir_inode))) 3136da5ebf5aSAl Viro return ERR_PTR(-ENOENT); 3137d58ffd35SMiklos Szeredi 313873a09dd9SAl Viro file->f_mode &= ~FMODE_CREATED; 31396fbd0714SAl Viro dentry = d_lookup(dir, &nd->last); 31406fbd0714SAl Viro for (;;) { 31416fbd0714SAl Viro if (!dentry) { 31426fbd0714SAl Viro dentry = d_alloc_parallel(dir, &nd->last, &wq); 3143d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 3144da5ebf5aSAl Viro return dentry; 31456fbd0714SAl Viro } 31466fbd0714SAl Viro if (d_in_lookup(dentry)) 31476fbd0714SAl Viro break; 3148d58ffd35SMiklos Szeredi 31496fbd0714SAl Viro error = d_revalidate(dentry, nd->flags); 31506fbd0714SAl Viro if (likely(error > 0)) 31516fbd0714SAl Viro break; 31526fbd0714SAl Viro if (error) 31536fbd0714SAl Viro goto out_dput; 31546fbd0714SAl Viro d_invalidate(dentry); 31556fbd0714SAl Viro dput(dentry); 31566fbd0714SAl Viro dentry = NULL; 31576fbd0714SAl Viro } 31586fbd0714SAl Viro if (dentry->d_inode) { 3159d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 3160da5ebf5aSAl Viro return dentry; 31616c51e513SAl Viro } 3162d18e9008SMiklos Szeredi 31631643b43fSAl Viro /* 31641643b43fSAl Viro * Checking write permission is tricky, bacuse we don't know if we are 31651643b43fSAl Viro * going to actually need it: O_CREAT opens should work as long as the 31661643b43fSAl Viro * file exists. But checking existence breaks atomicity. The trick is 31671643b43fSAl Viro * to check access and if not granted clear O_CREAT from the flags. 31681643b43fSAl Viro * 31691643b43fSAl Viro * Another problem is returing the "right" error value (e.g. for an 31701643b43fSAl Viro * O_EXCL open we want to return EEXIST not EROFS). 31711643b43fSAl Viro */ 317299a4a90cSAl Viro if (unlikely(!got_write)) 317399a4a90cSAl Viro open_flag &= ~O_TRUNC; 3174549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 31751643b43fSAl Viro if (open_flag & O_CREAT) { 317699a4a90cSAl Viro if (open_flag & O_EXCL) 317799a4a90cSAl Viro open_flag &= ~O_TRUNC; 31781643b43fSAl Viro if (!IS_POSIXACL(dir->d_inode)) 31791643b43fSAl Viro mode &= ~current_umask(); 318099a4a90cSAl Viro if (likely(got_write)) 3181549c7297SChristian Brauner create_error = may_o_create(mnt_userns, &nd->path, 3182ba73d987SChristian Brauner dentry, mode); 318399a4a90cSAl Viro else 318499a4a90cSAl Viro create_error = -EROFS; 318599a4a90cSAl Viro } 318699a4a90cSAl Viro if (create_error) 31871643b43fSAl Viro open_flag &= ~O_CREAT; 31881643b43fSAl Viro if (dir_inode->i_op->atomic_open) { 3189d489cf9aSAl Viro dentry = atomic_open(nd, dentry, file, open_flag, mode); 3190da5ebf5aSAl Viro if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) 3191da5ebf5aSAl Viro dentry = ERR_PTR(create_error); 3192da5ebf5aSAl Viro return dentry; 3193239eb983SAl Viro } 319454ef4872SMiklos Szeredi 31956fbd0714SAl Viro if (d_in_lookup(dentry)) { 319612fa5e24SAl Viro struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, 319712fa5e24SAl Viro nd->flags); 31986fbd0714SAl Viro d_lookup_done(dentry); 319912fa5e24SAl Viro if (unlikely(res)) { 320012fa5e24SAl Viro if (IS_ERR(res)) { 320112fa5e24SAl Viro error = PTR_ERR(res); 320212fa5e24SAl Viro goto out_dput; 320312fa5e24SAl Viro } 320412fa5e24SAl Viro dput(dentry); 320512fa5e24SAl Viro dentry = res; 320612fa5e24SAl Viro } 320754ef4872SMiklos Szeredi } 320854ef4872SMiklos Szeredi 3209d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 32101643b43fSAl Viro if (!dentry->d_inode && (open_flag & O_CREAT)) { 321173a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 3212ce8644fcSAl Viro audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); 3213ce8644fcSAl Viro if (!dir_inode->i_op->create) { 3214ce8644fcSAl Viro error = -EACCES; 3215d58ffd35SMiklos Szeredi goto out_dput; 321664894cf8SAl Viro } 3217549c7297SChristian Brauner 3218549c7297SChristian Brauner error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry, 3219549c7297SChristian Brauner mode, open_flag & O_EXCL); 3220d58ffd35SMiklos Szeredi if (error) 3221d58ffd35SMiklos Szeredi goto out_dput; 3222d58ffd35SMiklos Szeredi } 32231643b43fSAl Viro if (unlikely(create_error) && !dentry->d_inode) { 32241643b43fSAl Viro error = create_error; 3225d58ffd35SMiklos Szeredi goto out_dput; 3226d58ffd35SMiklos Szeredi } 3227da5ebf5aSAl Viro return dentry; 3228d58ffd35SMiklos Szeredi 3229d58ffd35SMiklos Szeredi out_dput: 3230d58ffd35SMiklos Szeredi dput(dentry); 3231da5ebf5aSAl Viro return ERR_PTR(error); 3232d58ffd35SMiklos Szeredi } 3233d58ffd35SMiklos Szeredi 3234c981a482SAl Viro static const char *open_last_lookups(struct nameidata *nd, 32353ec2eef1SAl Viro struct file *file, const struct open_flags *op) 3236fb1cc555SAl Viro { 3237a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 3238ca344a89SAl Viro int open_flag = op->open_flag; 323964894cf8SAl Viro bool got_write = false; 3240254cf582SAl Viro unsigned seq; 3241a1eb3315SMiklos Szeredi struct inode *inode; 3242da5ebf5aSAl Viro struct dentry *dentry; 3243b0417d2cSAl Viro const char *res; 3244fb1cc555SAl Viro 3245c3e380b0SAl Viro nd->flags |= op->intent; 3246c3e380b0SAl Viro 3247bc77daa7SAl Viro if (nd->last_type != LAST_NORM) { 324856676ec3SAl Viro if (nd->depth) 324956676ec3SAl Viro put_link(nd); 3250ff326a32SAl Viro return handle_dots(nd, nd->last_type); 32511f36f774SAl Viro } 3252a2c36b45SAl Viro 3253ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 3254fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 3255fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 3256fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 325720e34357SAl Viro dentry = lookup_fast(nd, &inode, &seq); 325820e34357SAl Viro if (IS_ERR(dentry)) 32591ccac622SAl Viro return ERR_CAST(dentry); 326020e34357SAl Viro if (likely(dentry)) 326171574865SMiklos Szeredi goto finish_lookup; 326271574865SMiklos Szeredi 32636583fe22SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 3264b6183df7SMiklos Szeredi } else { 3265fe2d35ffSAl Viro /* create side of things */ 326672287417SAl Viro if (nd->flags & LOOKUP_RCU) { 3267e36cffedSJens Axboe if (!try_to_unlazy(nd)) 3268e36cffedSJens Axboe return ERR_PTR(-ECHILD); 326972287417SAl Viro } 3270c9b07eabSAl Viro audit_inode(nd->name, dir, AUDIT_INODE_PARENT); 32711f36f774SAl Viro /* trailing slashes? */ 3272deb106c6SAl Viro if (unlikely(nd->last.name[nd->last.len])) 32731ccac622SAl Viro return ERR_PTR(-EISDIR); 3274b6183df7SMiklos Szeredi } 32751f36f774SAl Viro 32769cf843e3SAl Viro if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 3277e36cffedSJens Axboe got_write = !mnt_want_write(nd->path.mnt); 327864894cf8SAl Viro /* 327964894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 328064894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 328164894cf8SAl Viro * dropping this one anyway. 328264894cf8SAl Viro */ 328364894cf8SAl Viro } 32849cf843e3SAl Viro if (open_flag & O_CREAT) 32855955102cSAl Viro inode_lock(dir->d_inode); 32869cf843e3SAl Viro else 32879cf843e3SAl Viro inode_lock_shared(dir->d_inode); 3288da5ebf5aSAl Viro dentry = lookup_open(nd, file, op, got_write); 3289f7bb959dSAl Viro if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) 3290f7bb959dSAl Viro fsnotify_create(dir->d_inode, dentry); 32919cf843e3SAl Viro if (open_flag & O_CREAT) 32925955102cSAl Viro inode_unlock(dir->d_inode); 32939cf843e3SAl Viro else 32949cf843e3SAl Viro inode_unlock_shared(dir->d_inode); 3295fb1cc555SAl Viro 3296c981a482SAl Viro if (got_write) 329759e96e65SAl Viro mnt_drop_write(nd->path.mnt); 32986c0d46c4SAl Viro 329959e96e65SAl Viro if (IS_ERR(dentry)) 330059e96e65SAl Viro return ERR_CAST(dentry); 330159e96e65SAl Viro 3302973d4b73SAl Viro if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { 3303e73cabffSAl Viro dput(nd->path.dentry); 3304e73cabffSAl Viro nd->path.dentry = dentry; 3305c981a482SAl Viro return NULL; 3306fb1cc555SAl Viro } 3307fb1cc555SAl Viro 330820e34357SAl Viro finish_lookup: 330956676ec3SAl Viro if (nd->depth) 331056676ec3SAl Viro put_link(nd); 33118c4efe22SAl Viro res = step_into(nd, WALK_TRAILING, dentry, inode, seq); 3312ff326a32SAl Viro if (unlikely(res)) 33131ccac622SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3314b0417d2cSAl Viro return res; 33151ccac622SAl Viro } 331631d1726dSAl Viro 3317c981a482SAl Viro /* 3318c981a482SAl Viro * Handle the last step of open() 3319c981a482SAl Viro */ 3320c5971b8cSAl Viro static int do_open(struct nameidata *nd, 3321c981a482SAl Viro struct file *file, const struct open_flags *op) 3322c981a482SAl Viro { 3323549c7297SChristian Brauner struct user_namespace *mnt_userns; 3324c981a482SAl Viro int open_flag = op->open_flag; 3325c981a482SAl Viro bool do_truncate; 3326c981a482SAl Viro int acc_mode; 3327c981a482SAl Viro int error; 3328c981a482SAl Viro 3329ff326a32SAl Viro if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { 3330ff326a32SAl Viro error = complete_walk(nd); 3331ff326a32SAl Viro if (error) 3332ff326a32SAl Viro return error; 3333ff326a32SAl Viro } 3334973d4b73SAl Viro if (!(file->f_mode & FMODE_CREATED)) 333576ae2a5aSAl Viro audit_inode(nd->name, nd->path.dentry, 0); 3336549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 333730aba665SSalvatore Mesoraca if (open_flag & O_CREAT) { 3338b94e0b32SAl Viro if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) 3339b94e0b32SAl Viro return -EEXIST; 334030aba665SSalvatore Mesoraca if (d_is_dir(nd->path.dentry)) 3341c5971b8cSAl Viro return -EISDIR; 3342549c7297SChristian Brauner error = may_create_in_sticky(mnt_userns, nd, 334330aba665SSalvatore Mesoraca d_backing_inode(nd->path.dentry)); 334430aba665SSalvatore Mesoraca if (unlikely(error)) 3345c5971b8cSAl Viro return error; 334630aba665SSalvatore Mesoraca } 334744b1d530SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) 3348c5971b8cSAl Viro return -ENOTDIR; 33496c0d46c4SAl Viro 33508795e7d4SAl Viro do_truncate = false; 33518795e7d4SAl Viro acc_mode = op->acc_mode; 33525a2d3eddSAl Viro if (file->f_mode & FMODE_CREATED) { 33535a2d3eddSAl Viro /* Don't check for write permission, don't truncate */ 33545a2d3eddSAl Viro open_flag &= ~O_TRUNC; 33555a2d3eddSAl Viro acc_mode = 0; 33568795e7d4SAl Viro } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { 33570f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 33580f9d1a10SAl Viro if (error) 3359c5971b8cSAl Viro return error; 33608795e7d4SAl Viro do_truncate = true; 33610f9d1a10SAl Viro } 3362549c7297SChristian Brauner error = may_open(mnt_userns, &nd->path, acc_mode, open_flag); 33638795e7d4SAl Viro if (!error && !(file->f_mode & FMODE_OPENED)) 3364ae2bb293SAl Viro error = vfs_open(&nd->path, file); 33658795e7d4SAl Viro if (!error) 33666035a27bSAl Viro error = ima_file_check(file, op->acc_mode); 33678795e7d4SAl Viro if (!error && do_truncate) 3368549c7297SChristian Brauner error = handle_truncate(mnt_userns, file); 3369c80567c8SAl Viro if (unlikely(error > 0)) { 3370c80567c8SAl Viro WARN_ON(1); 3371c80567c8SAl Viro error = -EINVAL; 3372c80567c8SAl Viro } 33738795e7d4SAl Viro if (do_truncate) 33740f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 3375c5971b8cSAl Viro return error; 3376fb1cc555SAl Viro } 3377fb1cc555SAl Viro 33786521f891SChristian Brauner /** 33796521f891SChristian Brauner * vfs_tmpfile - create tmpfile 33806521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 33816521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 33826521f891SChristian Brauner * @mode: mode of the new tmpfile 3383*2111c3c0SRandy Dunlap * @open_flag: flags 33846521f891SChristian Brauner * 33856521f891SChristian Brauner * Create a temporary file. 33866521f891SChristian Brauner * 33876521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 33886521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 33896521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 33906521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 33916521f891SChristian Brauner * raw inode simply passs init_user_ns. 33926521f891SChristian Brauner */ 33936521f891SChristian Brauner struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, 33946521f891SChristian Brauner struct dentry *dentry, umode_t mode, int open_flag) 3395af7bd4dcSAmir Goldstein { 3396af7bd4dcSAmir Goldstein struct dentry *child = NULL; 3397af7bd4dcSAmir Goldstein struct inode *dir = dentry->d_inode; 3398af7bd4dcSAmir Goldstein struct inode *inode; 3399af7bd4dcSAmir Goldstein int error; 3400af7bd4dcSAmir Goldstein 3401af7bd4dcSAmir Goldstein /* we want directory to be writable */ 34026521f891SChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 3403af7bd4dcSAmir Goldstein if (error) 3404af7bd4dcSAmir Goldstein goto out_err; 3405af7bd4dcSAmir Goldstein error = -EOPNOTSUPP; 3406af7bd4dcSAmir Goldstein if (!dir->i_op->tmpfile) 3407af7bd4dcSAmir Goldstein goto out_err; 3408af7bd4dcSAmir Goldstein error = -ENOMEM; 3409cdf01226SDavid Howells child = d_alloc(dentry, &slash_name); 3410af7bd4dcSAmir Goldstein if (unlikely(!child)) 3411af7bd4dcSAmir Goldstein goto out_err; 3412549c7297SChristian Brauner error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); 3413af7bd4dcSAmir Goldstein if (error) 3414af7bd4dcSAmir Goldstein goto out_err; 3415af7bd4dcSAmir Goldstein error = -ENOENT; 3416af7bd4dcSAmir Goldstein inode = child->d_inode; 3417af7bd4dcSAmir Goldstein if (unlikely(!inode)) 3418af7bd4dcSAmir Goldstein goto out_err; 3419af7bd4dcSAmir Goldstein if (!(open_flag & O_EXCL)) { 3420af7bd4dcSAmir Goldstein spin_lock(&inode->i_lock); 3421af7bd4dcSAmir Goldstein inode->i_state |= I_LINKABLE; 3422af7bd4dcSAmir Goldstein spin_unlock(&inode->i_lock); 3423af7bd4dcSAmir Goldstein } 3424a2d2329eSChristian Brauner ima_post_create_tmpfile(mnt_userns, inode); 3425af7bd4dcSAmir Goldstein return child; 3426af7bd4dcSAmir Goldstein 3427af7bd4dcSAmir Goldstein out_err: 3428af7bd4dcSAmir Goldstein dput(child); 3429af7bd4dcSAmir Goldstein return ERR_PTR(error); 3430af7bd4dcSAmir Goldstein } 3431af7bd4dcSAmir Goldstein EXPORT_SYMBOL(vfs_tmpfile); 3432af7bd4dcSAmir Goldstein 3433c8a53ee5SAl Viro static int do_tmpfile(struct nameidata *nd, unsigned flags, 343460545d0dSAl Viro const struct open_flags *op, 34353ec2eef1SAl Viro struct file *file) 343660545d0dSAl Viro { 34376521f891SChristian Brauner struct user_namespace *mnt_userns; 3438625b6d10SAl Viro struct dentry *child; 3439625b6d10SAl Viro struct path path; 3440c8a53ee5SAl Viro int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); 344160545d0dSAl Viro if (unlikely(error)) 344260545d0dSAl Viro return error; 3443625b6d10SAl Viro error = mnt_want_write(path.mnt); 344460545d0dSAl Viro if (unlikely(error)) 344560545d0dSAl Viro goto out; 34466521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 34476521f891SChristian Brauner child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); 3448af7bd4dcSAmir Goldstein error = PTR_ERR(child); 3449684e73beSHirofumi Nakagawa if (IS_ERR(child)) 345060545d0dSAl Viro goto out2; 3451625b6d10SAl Viro dput(path.dentry); 3452625b6d10SAl Viro path.dentry = child; 3453c8a53ee5SAl Viro audit_inode(nd->name, child, 0); 345469a91c23SEric Rannaud /* Don't check for other permissions, the inode was just created */ 3455549c7297SChristian Brauner error = may_open(mnt_userns, &path, 0, op->open_flag); 34561e8f44f1SAl Viro if (!error) 34571e8f44f1SAl Viro error = vfs_open(&path, file); 345860545d0dSAl Viro out2: 3459625b6d10SAl Viro mnt_drop_write(path.mnt); 346060545d0dSAl Viro out: 3461625b6d10SAl Viro path_put(&path); 346260545d0dSAl Viro return error; 346360545d0dSAl Viro } 346460545d0dSAl Viro 34656ac08709SAl Viro static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) 34666ac08709SAl Viro { 34676ac08709SAl Viro struct path path; 34686ac08709SAl Viro int error = path_lookupat(nd, flags, &path); 34696ac08709SAl Viro if (!error) { 34706ac08709SAl Viro audit_inode(nd->name, path.dentry, 0); 3471ae2bb293SAl Viro error = vfs_open(&path, file); 34726ac08709SAl Viro path_put(&path); 34736ac08709SAl Viro } 34746ac08709SAl Viro return error; 34756ac08709SAl Viro } 34766ac08709SAl Viro 3477c8a53ee5SAl Viro static struct file *path_openat(struct nameidata *nd, 3478c8a53ee5SAl Viro const struct open_flags *op, unsigned flags) 34791da177e4SLinus Torvalds { 348030d90494SAl Viro struct file *file; 348113aab428SAl Viro int error; 348231e6b01fSNick Piggin 3483ea73ea72SAl Viro file = alloc_empty_file(op->open_flag, current_cred()); 34841afc99beSAl Viro if (IS_ERR(file)) 34851afc99beSAl Viro return file; 348631e6b01fSNick Piggin 3487bb458c64SAl Viro if (unlikely(file->f_flags & __O_TMPFILE)) { 34883ec2eef1SAl Viro error = do_tmpfile(nd, flags, op, file); 34895f336e72SAl Viro } else if (unlikely(file->f_flags & O_PATH)) { 34906ac08709SAl Viro error = do_o_path(nd, flags, file); 34915f336e72SAl Viro } else { 34925f336e72SAl Viro const char *s = path_init(nd, flags); 34933bdba28bSAl Viro while (!(error = link_path_walk(s, nd)) && 3494c5971b8cSAl Viro (s = open_last_lookups(nd, file, op)) != NULL) 34951ccac622SAl Viro ; 3496c5971b8cSAl Viro if (!error) 3497c5971b8cSAl Viro error = do_open(nd, file, op); 3498deb106c6SAl Viro terminate_walk(nd); 34995f336e72SAl Viro } 35007c1c01ecSAl Viro if (likely(!error)) { 3501aad888f8SAl Viro if (likely(file->f_mode & FMODE_OPENED)) 35027c1c01ecSAl Viro return file; 35037c1c01ecSAl Viro WARN_ON(1); 35047c1c01ecSAl Viro error = -EINVAL; 3505015c3bbcSMiklos Szeredi } 35067c1c01ecSAl Viro fput(file); 35072675a4ebSAl Viro if (error == -EOPENSTALE) { 35082675a4ebSAl Viro if (flags & LOOKUP_RCU) 35092675a4ebSAl Viro error = -ECHILD; 35102675a4ebSAl Viro else 35112675a4ebSAl Viro error = -ESTALE; 35122675a4ebSAl Viro } 35137c1c01ecSAl Viro return ERR_PTR(error); 3514de459215SKirill Korotaev } 35151da177e4SLinus Torvalds 3516669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 3517f9652e10SAl Viro const struct open_flags *op) 351813aab428SAl Viro { 35199883d185SAl Viro struct nameidata nd; 3520f9652e10SAl Viro int flags = op->lookup_flags; 352113aab428SAl Viro struct file *filp; 352213aab428SAl Viro 35239883d185SAl Viro set_nameidata(&nd, dfd, pathname); 3524c8a53ee5SAl Viro filp = path_openat(&nd, op, flags | LOOKUP_RCU); 352513aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 3526c8a53ee5SAl Viro filp = path_openat(&nd, op, flags); 352713aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 3528c8a53ee5SAl Viro filp = path_openat(&nd, op, flags | LOOKUP_REVAL); 35299883d185SAl Viro restore_nameidata(); 353013aab428SAl Viro return filp; 353113aab428SAl Viro } 353213aab428SAl Viro 353373d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 3534f9652e10SAl Viro const char *name, const struct open_flags *op) 353573d049a4SAl Viro { 35369883d185SAl Viro struct nameidata nd; 353773d049a4SAl Viro struct file *file; 353851689104SPaul Moore struct filename *filename; 3539f9652e10SAl Viro int flags = op->lookup_flags | LOOKUP_ROOT; 354073d049a4SAl Viro 354173d049a4SAl Viro nd.root.mnt = mnt; 354273d049a4SAl Viro nd.root.dentry = dentry; 354373d049a4SAl Viro 3544b18825a7SDavid Howells if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) 354573d049a4SAl Viro return ERR_PTR(-ELOOP); 354673d049a4SAl Viro 354751689104SPaul Moore filename = getname_kernel(name); 3548a1c83681SViresh Kumar if (IS_ERR(filename)) 354951689104SPaul Moore return ERR_CAST(filename); 355051689104SPaul Moore 35519883d185SAl Viro set_nameidata(&nd, -1, filename); 3552c8a53ee5SAl Viro file = path_openat(&nd, op, flags | LOOKUP_RCU); 355373d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3554c8a53ee5SAl Viro file = path_openat(&nd, op, flags); 355573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3556c8a53ee5SAl Viro file = path_openat(&nd, op, flags | LOOKUP_REVAL); 35579883d185SAl Viro restore_nameidata(); 355851689104SPaul Moore putname(filename); 355973d049a4SAl Viro return file; 356073d049a4SAl Viro } 356173d049a4SAl Viro 3562fa14a0b8SAl Viro static struct dentry *filename_create(int dfd, struct filename *name, 35631ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 35641da177e4SLinus Torvalds { 3565c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3566391172c4SAl Viro struct qstr last; 3567391172c4SAl Viro int type; 3568c30dabfeSJan Kara int err2; 35691ac12b4bSJeff Layton int error; 35701ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 35711ac12b4bSJeff Layton 35721ac12b4bSJeff Layton /* 35731ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 35741ac12b4bSJeff Layton * other flags passed in are ignored! 35751ac12b4bSJeff Layton */ 35761ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 35771ac12b4bSJeff Layton 35785c31b6ceSAl Viro name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); 35795c31b6ceSAl Viro if (IS_ERR(name)) 35805c31b6ceSAl Viro return ERR_CAST(name); 35811da177e4SLinus Torvalds 3582c663e5d8SChristoph Hellwig /* 3583c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3584c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3585c663e5d8SChristoph Hellwig */ 35865c31b6ceSAl Viro if (unlikely(type != LAST_NORM)) 3587ed75e95dSAl Viro goto out; 3588c663e5d8SChristoph Hellwig 3589c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3590391172c4SAl Viro err2 = mnt_want_write(path->mnt); 3591c663e5d8SChristoph Hellwig /* 3592c663e5d8SChristoph Hellwig * Do the final lookup. 3593c663e5d8SChristoph Hellwig */ 3594391172c4SAl Viro lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; 35955955102cSAl Viro inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 3596391172c4SAl Viro dentry = __lookup_hash(&last, path->dentry, lookup_flags); 35971da177e4SLinus Torvalds if (IS_ERR(dentry)) 3598a8104a9fSAl Viro goto unlock; 3599c663e5d8SChristoph Hellwig 3600a8104a9fSAl Viro error = -EEXIST; 3601b18825a7SDavid Howells if (d_is_positive(dentry)) 3602a8104a9fSAl Viro goto fail; 3603b18825a7SDavid Howells 3604c663e5d8SChristoph Hellwig /* 3605c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3606c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3607c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3608c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3609c663e5d8SChristoph Hellwig */ 3610391172c4SAl Viro if (unlikely(!is_dir && last.name[last.len])) { 3611a8104a9fSAl Viro error = -ENOENT; 3612ed75e95dSAl Viro goto fail; 3613e9baf6e5SAl Viro } 3614c30dabfeSJan Kara if (unlikely(err2)) { 3615c30dabfeSJan Kara error = err2; 3616a8104a9fSAl Viro goto fail; 3617c30dabfeSJan Kara } 3618181c37b6SAl Viro putname(name); 3619e9baf6e5SAl Viro return dentry; 36201da177e4SLinus Torvalds fail: 3621a8104a9fSAl Viro dput(dentry); 3622a8104a9fSAl Viro dentry = ERR_PTR(error); 3623a8104a9fSAl Viro unlock: 36245955102cSAl Viro inode_unlock(path->dentry->d_inode); 3625c30dabfeSJan Kara if (!err2) 3626391172c4SAl Viro mnt_drop_write(path->mnt); 3627ed75e95dSAl Viro out: 3628391172c4SAl Viro path_put(path); 3629181c37b6SAl Viro putname(name); 3630ed75e95dSAl Viro return dentry; 3631dae6ad8fSAl Viro } 3632fa14a0b8SAl Viro 3633fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, 3634fa14a0b8SAl Viro struct path *path, unsigned int lookup_flags) 3635fa14a0b8SAl Viro { 3636181c37b6SAl Viro return filename_create(dfd, getname_kernel(pathname), 3637181c37b6SAl Viro path, lookup_flags); 3638fa14a0b8SAl Viro } 3639dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3640dae6ad8fSAl Viro 3641921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3642921a1650SAl Viro { 3643921a1650SAl Viro dput(dentry); 36445955102cSAl Viro inode_unlock(path->dentry->d_inode); 3645a8104a9fSAl Viro mnt_drop_write(path->mnt); 3646921a1650SAl Viro path_put(path); 3647921a1650SAl Viro } 3648921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3649921a1650SAl Viro 3650520ae687SAl Viro inline struct dentry *user_path_create(int dfd, const char __user *pathname, 36511ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3652dae6ad8fSAl Viro { 3653181c37b6SAl Viro return filename_create(dfd, getname(pathname), path, lookup_flags); 3654dae6ad8fSAl Viro } 3655dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3656dae6ad8fSAl Viro 36576521f891SChristian Brauner /** 36586521f891SChristian Brauner * vfs_mknod - create device node or file 36596521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 36606521f891SChristian Brauner * @dir: inode of @dentry 36616521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 36626521f891SChristian Brauner * @mode: mode of the new device node or file 36636521f891SChristian Brauner * @dev: device number of device to create 36646521f891SChristian Brauner * 36656521f891SChristian Brauner * Create a device node or file. 36666521f891SChristian Brauner * 36676521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 36686521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 36696521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 36706521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 36716521f891SChristian Brauner * raw inode simply passs init_user_ns. 36726521f891SChristian Brauner */ 36736521f891SChristian Brauner int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 36746521f891SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 36751da177e4SLinus Torvalds { 3676a3c751a5SMiklos Szeredi bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; 36776521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 36781da177e4SLinus Torvalds 36791da177e4SLinus Torvalds if (error) 36801da177e4SLinus Torvalds return error; 36811da177e4SLinus Torvalds 3682a3c751a5SMiklos Szeredi if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && 3683a3c751a5SMiklos Szeredi !capable(CAP_MKNOD)) 36841da177e4SLinus Torvalds return -EPERM; 36851da177e4SLinus Torvalds 3686acfa4380SAl Viro if (!dir->i_op->mknod) 36871da177e4SLinus Torvalds return -EPERM; 36881da177e4SLinus Torvalds 368908ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 369008ce5f16SSerge E. Hallyn if (error) 369108ce5f16SSerge E. Hallyn return error; 369208ce5f16SSerge E. Hallyn 36931da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 36941da177e4SLinus Torvalds if (error) 36951da177e4SLinus Torvalds return error; 36961da177e4SLinus Torvalds 3697549c7297SChristian Brauner error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev); 3698a74574aaSStephen Smalley if (!error) 3699f38aa942SAmy Griffis fsnotify_create(dir, dentry); 37001da177e4SLinus Torvalds return error; 37011da177e4SLinus Torvalds } 37024d359507SAl Viro EXPORT_SYMBOL(vfs_mknod); 37031da177e4SLinus Torvalds 3704f69aac00SAl Viro static int may_mknod(umode_t mode) 3705463c3197SDave Hansen { 3706463c3197SDave Hansen switch (mode & S_IFMT) { 3707463c3197SDave Hansen case S_IFREG: 3708463c3197SDave Hansen case S_IFCHR: 3709463c3197SDave Hansen case S_IFBLK: 3710463c3197SDave Hansen case S_IFIFO: 3711463c3197SDave Hansen case S_IFSOCK: 3712463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3713463c3197SDave Hansen return 0; 3714463c3197SDave Hansen case S_IFDIR: 3715463c3197SDave Hansen return -EPERM; 3716463c3197SDave Hansen default: 3717463c3197SDave Hansen return -EINVAL; 3718463c3197SDave Hansen } 3719463c3197SDave Hansen } 3720463c3197SDave Hansen 37215fee64fcSChristoph Hellwig static long do_mknodat(int dfd, const char __user *filename, umode_t mode, 372287c4e192SDominik Brodowski unsigned int dev) 37231da177e4SLinus Torvalds { 37246521f891SChristian Brauner struct user_namespace *mnt_userns; 37251da177e4SLinus Torvalds struct dentry *dentry; 3726dae6ad8fSAl Viro struct path path; 3727dae6ad8fSAl Viro int error; 3728972567f1SJeff Layton unsigned int lookup_flags = 0; 37291da177e4SLinus Torvalds 37308e4bfca1SAl Viro error = may_mknod(mode); 37318e4bfca1SAl Viro if (error) 37328e4bfca1SAl Viro return error; 3733972567f1SJeff Layton retry: 3734972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3735dae6ad8fSAl Viro if (IS_ERR(dentry)) 3736dae6ad8fSAl Viro return PTR_ERR(dentry); 37372ad94ae6SAl Viro 3738dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3739ce3b0f8dSAl Viro mode &= ~current_umask(); 3740dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3741be6d3e56SKentaro Takeda if (error) 3742a8104a9fSAl Viro goto out; 37436521f891SChristian Brauner 37446521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 37451da177e4SLinus Torvalds switch (mode & S_IFMT) { 37461da177e4SLinus Torvalds case 0: case S_IFREG: 37476521f891SChristian Brauner error = vfs_create(mnt_userns, path.dentry->d_inode, 37486521f891SChristian Brauner dentry, mode, true); 374905d1a717SMimi Zohar if (!error) 3750a2d2329eSChristian Brauner ima_post_path_mknod(mnt_userns, dentry); 37511da177e4SLinus Torvalds break; 37521da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 37536521f891SChristian Brauner error = vfs_mknod(mnt_userns, path.dentry->d_inode, 37546521f891SChristian Brauner dentry, mode, new_decode_dev(dev)); 37551da177e4SLinus Torvalds break; 37561da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 37576521f891SChristian Brauner error = vfs_mknod(mnt_userns, path.dentry->d_inode, 37586521f891SChristian Brauner dentry, mode, 0); 37591da177e4SLinus Torvalds break; 37601da177e4SLinus Torvalds } 3761a8104a9fSAl Viro out: 3762921a1650SAl Viro done_path_create(&path, dentry); 3763972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3764972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3765972567f1SJeff Layton goto retry; 3766972567f1SJeff Layton } 37671da177e4SLinus Torvalds return error; 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds 377087c4e192SDominik Brodowski SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 377187c4e192SDominik Brodowski unsigned int, dev) 377287c4e192SDominik Brodowski { 377387c4e192SDominik Brodowski return do_mknodat(dfd, filename, mode, dev); 377487c4e192SDominik Brodowski } 377587c4e192SDominik Brodowski 37768208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 37775590ff0dSUlrich Drepper { 377887c4e192SDominik Brodowski return do_mknodat(AT_FDCWD, filename, mode, dev); 37795590ff0dSUlrich Drepper } 37805590ff0dSUlrich Drepper 37816521f891SChristian Brauner /** 37826521f891SChristian Brauner * vfs_mkdir - create directory 37836521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 37846521f891SChristian Brauner * @dir: inode of @dentry 37856521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 37866521f891SChristian Brauner * @mode: mode of the new directory 37876521f891SChristian Brauner * 37886521f891SChristian Brauner * Create a directory. 37896521f891SChristian Brauner * 37906521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 37916521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 37926521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 37936521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 37946521f891SChristian Brauner * raw inode simply passs init_user_ns. 37956521f891SChristian Brauner */ 37966521f891SChristian Brauner int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 37976521f891SChristian Brauner struct dentry *dentry, umode_t mode) 37981da177e4SLinus Torvalds { 37996521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 38008de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 38011da177e4SLinus Torvalds 38021da177e4SLinus Torvalds if (error) 38031da177e4SLinus Torvalds return error; 38041da177e4SLinus Torvalds 3805acfa4380SAl Viro if (!dir->i_op->mkdir) 38061da177e4SLinus Torvalds return -EPERM; 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 38091da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 38101da177e4SLinus Torvalds if (error) 38111da177e4SLinus Torvalds return error; 38121da177e4SLinus Torvalds 38138de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 38148de52778SAl Viro return -EMLINK; 38158de52778SAl Viro 3816549c7297SChristian Brauner error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode); 3817a74574aaSStephen Smalley if (!error) 3818f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 38191da177e4SLinus Torvalds return error; 38201da177e4SLinus Torvalds } 38214d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir); 38221da177e4SLinus Torvalds 382383ff98c3SChristoph Hellwig static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) 38241da177e4SLinus Torvalds { 38256902d925SDave Hansen struct dentry *dentry; 3826dae6ad8fSAl Viro struct path path; 3827dae6ad8fSAl Viro int error; 3828b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 38291da177e4SLinus Torvalds 3830b76d8b82SJeff Layton retry: 3831b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 38326902d925SDave Hansen if (IS_ERR(dentry)) 3833dae6ad8fSAl Viro return PTR_ERR(dentry); 38346902d925SDave Hansen 3835dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3836ce3b0f8dSAl Viro mode &= ~current_umask(); 3837dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 38386521f891SChristian Brauner if (!error) { 38396521f891SChristian Brauner struct user_namespace *mnt_userns; 38406521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 3841549c7297SChristian Brauner error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry, 3842549c7297SChristian Brauner mode); 38436521f891SChristian Brauner } 3844921a1650SAl Viro done_path_create(&path, dentry); 3845b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3846b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3847b76d8b82SJeff Layton goto retry; 3848b76d8b82SJeff Layton } 38491da177e4SLinus Torvalds return error; 38501da177e4SLinus Torvalds } 38511da177e4SLinus Torvalds 38520101db7aSDominik Brodowski SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 38530101db7aSDominik Brodowski { 38540101db7aSDominik Brodowski return do_mkdirat(dfd, pathname, mode); 38550101db7aSDominik Brodowski } 38560101db7aSDominik Brodowski 3857a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 38585590ff0dSUlrich Drepper { 38590101db7aSDominik Brodowski return do_mkdirat(AT_FDCWD, pathname, mode); 38605590ff0dSUlrich Drepper } 38615590ff0dSUlrich Drepper 38626521f891SChristian Brauner /** 38636521f891SChristian Brauner * vfs_rmdir - remove directory 38646521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 38656521f891SChristian Brauner * @dir: inode of @dentry 38666521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 38676521f891SChristian Brauner * 38686521f891SChristian Brauner * Remove a directory. 38696521f891SChristian Brauner * 38706521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 38716521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 38726521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 38736521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 38746521f891SChristian Brauner * raw inode simply passs init_user_ns. 38756521f891SChristian Brauner */ 38766521f891SChristian Brauner int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, 38776521f891SChristian Brauner struct dentry *dentry) 38781da177e4SLinus Torvalds { 38796521f891SChristian Brauner int error = may_delete(mnt_userns, dir, dentry, 1); 38801da177e4SLinus Torvalds 38811da177e4SLinus Torvalds if (error) 38821da177e4SLinus Torvalds return error; 38831da177e4SLinus Torvalds 3884acfa4380SAl Viro if (!dir->i_op->rmdir) 38851da177e4SLinus Torvalds return -EPERM; 38861da177e4SLinus Torvalds 38871d2ef590SAl Viro dget(dentry); 38885955102cSAl Viro inode_lock(dentry->d_inode); 3889912dbc15SSage Weil 38901da177e4SLinus Torvalds error = -EBUSY; 38917af1364fSEric W. Biederman if (is_local_mountpoint(dentry)) 3892912dbc15SSage Weil goto out; 3893912dbc15SSage Weil 38941da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3895912dbc15SSage Weil if (error) 3896912dbc15SSage Weil goto out; 3897912dbc15SSage Weil 38981da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3899912dbc15SSage Weil if (error) 3900912dbc15SSage Weil goto out; 3901912dbc15SSage Weil 39028767712fSAl Viro shrink_dcache_parent(dentry); 39031da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3904d83c49f3SAl Viro dont_mount(dentry); 39058ed936b5SEric W. Biederman detach_mounts(dentry); 3906116b9731SAmir Goldstein fsnotify_rmdir(dir, dentry); 39071da177e4SLinus Torvalds 3908912dbc15SSage Weil out: 39095955102cSAl Viro inode_unlock(dentry->d_inode); 39101d2ef590SAl Viro dput(dentry); 3911912dbc15SSage Weil if (!error) 3912912dbc15SSage Weil d_delete(dentry); 39131da177e4SLinus Torvalds return error; 39141da177e4SLinus Torvalds } 39154d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir); 39161da177e4SLinus Torvalds 3917e24ab0efSChristoph Hellwig long do_rmdir(int dfd, struct filename *name) 39181da177e4SLinus Torvalds { 39196521f891SChristian Brauner struct user_namespace *mnt_userns; 39201da177e4SLinus Torvalds int error = 0; 39211da177e4SLinus Torvalds struct dentry *dentry; 3922f5beed75SAl Viro struct path path; 3923f5beed75SAl Viro struct qstr last; 3924f5beed75SAl Viro int type; 3925c6ee9206SJeff Layton unsigned int lookup_flags = 0; 3926c6ee9206SJeff Layton retry: 3927e24ab0efSChristoph Hellwig name = filename_parentat(dfd, name, lookup_flags, 3928c1d4dd27SAl Viro &path, &last, &type); 392991a27b2aSJeff Layton if (IS_ERR(name)) 393091a27b2aSJeff Layton return PTR_ERR(name); 39311da177e4SLinus Torvalds 3932f5beed75SAl Viro switch (type) { 39331da177e4SLinus Torvalds case LAST_DOTDOT: 39341da177e4SLinus Torvalds error = -ENOTEMPTY; 39351da177e4SLinus Torvalds goto exit1; 39361da177e4SLinus Torvalds case LAST_DOT: 39371da177e4SLinus Torvalds error = -EINVAL; 39381da177e4SLinus Torvalds goto exit1; 39391da177e4SLinus Torvalds case LAST_ROOT: 39401da177e4SLinus Torvalds error = -EBUSY; 39411da177e4SLinus Torvalds goto exit1; 39421da177e4SLinus Torvalds } 39430612d9fbSOGAWA Hirofumi 3944f5beed75SAl Viro error = mnt_want_write(path.mnt); 3945c30dabfeSJan Kara if (error) 3946c30dabfeSJan Kara goto exit1; 39470612d9fbSOGAWA Hirofumi 39485955102cSAl Viro inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 3949f5beed75SAl Viro dentry = __lookup_hash(&last, path.dentry, lookup_flags); 39501da177e4SLinus Torvalds error = PTR_ERR(dentry); 39516902d925SDave Hansen if (IS_ERR(dentry)) 39526902d925SDave Hansen goto exit2; 3953e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3954e6bc45d6STheodore Ts'o error = -ENOENT; 3955e6bc45d6STheodore Ts'o goto exit3; 3956e6bc45d6STheodore Ts'o } 3957f5beed75SAl Viro error = security_path_rmdir(&path, dentry); 3958be6d3e56SKentaro Takeda if (error) 3959c30dabfeSJan Kara goto exit3; 39606521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 39616521f891SChristian Brauner error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); 39620622753bSDave Hansen exit3: 39631da177e4SLinus Torvalds dput(dentry); 39646902d925SDave Hansen exit2: 39655955102cSAl Viro inode_unlock(path.dentry->d_inode); 3966f5beed75SAl Viro mnt_drop_write(path.mnt); 39671da177e4SLinus Torvalds exit1: 3968f5beed75SAl Viro path_put(&path); 3969c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 3970c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 3971c6ee9206SJeff Layton goto retry; 3972c6ee9206SJeff Layton } 397324fb33d4SAl Viro putname(name); 39741da177e4SLinus Torvalds return error; 39751da177e4SLinus Torvalds } 39761da177e4SLinus Torvalds 39773cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 39785590ff0dSUlrich Drepper { 3979e24ab0efSChristoph Hellwig return do_rmdir(AT_FDCWD, getname(pathname)); 39805590ff0dSUlrich Drepper } 39815590ff0dSUlrich Drepper 3982b21996e3SJ. Bruce Fields /** 3983b21996e3SJ. Bruce Fields * vfs_unlink - unlink a filesystem object 39846521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 3985b21996e3SJ. Bruce Fields * @dir: parent directory 3986b21996e3SJ. Bruce Fields * @dentry: victim 3987b21996e3SJ. Bruce Fields * @delegated_inode: returns victim inode, if the inode is delegated. 3988b21996e3SJ. Bruce Fields * 3989b21996e3SJ. Bruce Fields * The caller must hold dir->i_mutex. 3990b21996e3SJ. Bruce Fields * 3991b21996e3SJ. Bruce Fields * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and 3992b21996e3SJ. Bruce Fields * return a reference to the inode in delegated_inode. The caller 3993b21996e3SJ. Bruce Fields * should then break the delegation on that inode and retry. Because 3994b21996e3SJ. Bruce Fields * breaking a delegation may take a long time, the caller should drop 3995b21996e3SJ. Bruce Fields * dir->i_mutex before doing so. 3996b21996e3SJ. Bruce Fields * 3997b21996e3SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 3998b21996e3SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 3999b21996e3SJ. Bruce Fields * to be NFS exported. 40006521f891SChristian Brauner * 40016521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 40026521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 40036521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 40046521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 40056521f891SChristian Brauner * raw inode simply passs init_user_ns. 4006b21996e3SJ. Bruce Fields */ 40076521f891SChristian Brauner int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, 40086521f891SChristian Brauner struct dentry *dentry, struct inode **delegated_inode) 40091da177e4SLinus Torvalds { 40109accbb97SJ. Bruce Fields struct inode *target = dentry->d_inode; 40116521f891SChristian Brauner int error = may_delete(mnt_userns, dir, dentry, 0); 40121da177e4SLinus Torvalds 40131da177e4SLinus Torvalds if (error) 40141da177e4SLinus Torvalds return error; 40151da177e4SLinus Torvalds 4016acfa4380SAl Viro if (!dir->i_op->unlink) 40171da177e4SLinus Torvalds return -EPERM; 40181da177e4SLinus Torvalds 40195955102cSAl Viro inode_lock(target); 40208ed936b5SEric W. Biederman if (is_local_mountpoint(dentry)) 40211da177e4SLinus Torvalds error = -EBUSY; 40221da177e4SLinus Torvalds else { 40231da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 4024bec1052eSAl Viro if (!error) { 40255a14696cSJ. Bruce Fields error = try_break_deleg(target, delegated_inode); 40265a14696cSJ. Bruce Fields if (error) 4027b21996e3SJ. Bruce Fields goto out; 40281da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 40298ed936b5SEric W. Biederman if (!error) { 4030d83c49f3SAl Viro dont_mount(dentry); 40318ed936b5SEric W. Biederman detach_mounts(dentry); 4032116b9731SAmir Goldstein fsnotify_unlink(dir, dentry); 40338ed936b5SEric W. Biederman } 4034bec1052eSAl Viro } 40351da177e4SLinus Torvalds } 4036b21996e3SJ. Bruce Fields out: 40375955102cSAl Viro inode_unlock(target); 40381da177e4SLinus Torvalds 40391da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 40401da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 40419accbb97SJ. Bruce Fields fsnotify_link_count(target); 40421da177e4SLinus Torvalds d_delete(dentry); 40431da177e4SLinus Torvalds } 40440eeca283SRobert Love 40451da177e4SLinus Torvalds return error; 40461da177e4SLinus Torvalds } 40474d359507SAl Viro EXPORT_SYMBOL(vfs_unlink); 40481da177e4SLinus Torvalds 40491da177e4SLinus Torvalds /* 40501da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 40511b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 40521da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 40531da177e4SLinus Torvalds * while waiting on the I/O. 40541da177e4SLinus Torvalds */ 4055da2f1362SChristoph Hellwig long do_unlinkat(int dfd, struct filename *name) 40561da177e4SLinus Torvalds { 40572ad94ae6SAl Viro int error; 40581da177e4SLinus Torvalds struct dentry *dentry; 4059f5beed75SAl Viro struct path path; 4060f5beed75SAl Viro struct qstr last; 4061f5beed75SAl Viro int type; 40621da177e4SLinus Torvalds struct inode *inode = NULL; 4063b21996e3SJ. Bruce Fields struct inode *delegated_inode = NULL; 40645d18f813SJeff Layton unsigned int lookup_flags = 0; 40655d18f813SJeff Layton retry: 4066da2f1362SChristoph Hellwig name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 406791a27b2aSJeff Layton if (IS_ERR(name)) 406891a27b2aSJeff Layton return PTR_ERR(name); 40692ad94ae6SAl Viro 40701da177e4SLinus Torvalds error = -EISDIR; 4071f5beed75SAl Viro if (type != LAST_NORM) 40721da177e4SLinus Torvalds goto exit1; 40730612d9fbSOGAWA Hirofumi 4074f5beed75SAl Viro error = mnt_want_write(path.mnt); 4075c30dabfeSJan Kara if (error) 4076c30dabfeSJan Kara goto exit1; 4077b21996e3SJ. Bruce Fields retry_deleg: 40785955102cSAl Viro inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 4079f5beed75SAl Viro dentry = __lookup_hash(&last, path.dentry, lookup_flags); 40801da177e4SLinus Torvalds error = PTR_ERR(dentry); 40811da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 40826521f891SChristian Brauner struct user_namespace *mnt_userns; 40836521f891SChristian Brauner 40841da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 4085f5beed75SAl Viro if (last.name[last.len]) 408650338b88STörök Edwin goto slashes; 40871da177e4SLinus Torvalds inode = dentry->d_inode; 4088b18825a7SDavid Howells if (d_is_negative(dentry)) 4089e6bc45d6STheodore Ts'o goto slashes; 40907de9c6eeSAl Viro ihold(inode); 4091f5beed75SAl Viro error = security_path_unlink(&path, dentry); 4092be6d3e56SKentaro Takeda if (error) 4093c30dabfeSJan Kara goto exit2; 40946521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 4095549c7297SChristian Brauner error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, 4096549c7297SChristian Brauner &delegated_inode); 40971da177e4SLinus Torvalds exit2: 40981da177e4SLinus Torvalds dput(dentry); 40991da177e4SLinus Torvalds } 41005955102cSAl Viro inode_unlock(path.dentry->d_inode); 41011da177e4SLinus Torvalds if (inode) 41021da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 4103b21996e3SJ. Bruce Fields inode = NULL; 4104b21996e3SJ. Bruce Fields if (delegated_inode) { 41055a14696cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 4106b21996e3SJ. Bruce Fields if (!error) 4107b21996e3SJ. Bruce Fields goto retry_deleg; 4108b21996e3SJ. Bruce Fields } 4109f5beed75SAl Viro mnt_drop_write(path.mnt); 41101da177e4SLinus Torvalds exit1: 4111f5beed75SAl Viro path_put(&path); 41125d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 41135d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 41145d18f813SJeff Layton inode = NULL; 41155d18f813SJeff Layton goto retry; 41165d18f813SJeff Layton } 4117da2f1362SChristoph Hellwig putname(name); 41181da177e4SLinus Torvalds return error; 41191da177e4SLinus Torvalds 41201da177e4SLinus Torvalds slashes: 4121b18825a7SDavid Howells if (d_is_negative(dentry)) 4122b18825a7SDavid Howells error = -ENOENT; 412344b1d530SMiklos Szeredi else if (d_is_dir(dentry)) 4124b18825a7SDavid Howells error = -EISDIR; 4125b18825a7SDavid Howells else 4126b18825a7SDavid Howells error = -ENOTDIR; 41271da177e4SLinus Torvalds goto exit2; 41281da177e4SLinus Torvalds } 41291da177e4SLinus Torvalds 41302e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 41315590ff0dSUlrich Drepper { 41325590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 41335590ff0dSUlrich Drepper return -EINVAL; 41345590ff0dSUlrich Drepper 41355590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 4136e24ab0efSChristoph Hellwig return do_rmdir(dfd, getname(pathname)); 4137da2f1362SChristoph Hellwig return do_unlinkat(dfd, getname(pathname)); 41385590ff0dSUlrich Drepper } 41395590ff0dSUlrich Drepper 41403480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 41415590ff0dSUlrich Drepper { 4142da2f1362SChristoph Hellwig return do_unlinkat(AT_FDCWD, getname(pathname)); 41435590ff0dSUlrich Drepper } 41445590ff0dSUlrich Drepper 41456521f891SChristian Brauner /** 41466521f891SChristian Brauner * vfs_symlink - create symlink 41476521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 41486521f891SChristian Brauner * @dir: inode of @dentry 41496521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 41506521f891SChristian Brauner * @oldname: name of the file to link to 41516521f891SChristian Brauner * 41526521f891SChristian Brauner * Create a symlink. 41536521f891SChristian Brauner * 41546521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 41556521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 41566521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 41576521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 41586521f891SChristian Brauner * raw inode simply passs init_user_ns. 41596521f891SChristian Brauner */ 41606521f891SChristian Brauner int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 41616521f891SChristian Brauner struct dentry *dentry, const char *oldname) 41621da177e4SLinus Torvalds { 41636521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 41641da177e4SLinus Torvalds 41651da177e4SLinus Torvalds if (error) 41661da177e4SLinus Torvalds return error; 41671da177e4SLinus Torvalds 4168acfa4380SAl Viro if (!dir->i_op->symlink) 41691da177e4SLinus Torvalds return -EPERM; 41701da177e4SLinus Torvalds 41711da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 41721da177e4SLinus Torvalds if (error) 41731da177e4SLinus Torvalds return error; 41741da177e4SLinus Torvalds 4175549c7297SChristian Brauner error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname); 4176a74574aaSStephen Smalley if (!error) 4177f38aa942SAmy Griffis fsnotify_create(dir, dentry); 41781da177e4SLinus Torvalds return error; 41791da177e4SLinus Torvalds } 41804d359507SAl Viro EXPORT_SYMBOL(vfs_symlink); 41811da177e4SLinus Torvalds 4182cd3acb6aSChristoph Hellwig static long do_symlinkat(const char __user *oldname, int newdfd, 4183b724e846SDominik Brodowski const char __user *newname) 41841da177e4SLinus Torvalds { 41852ad94ae6SAl Viro int error; 418691a27b2aSJeff Layton struct filename *from; 41876902d925SDave Hansen struct dentry *dentry; 4188dae6ad8fSAl Viro struct path path; 4189f46d3567SJeff Layton unsigned int lookup_flags = 0; 41901da177e4SLinus Torvalds 41911da177e4SLinus Torvalds from = getname(oldname); 41921da177e4SLinus Torvalds if (IS_ERR(from)) 41931da177e4SLinus Torvalds return PTR_ERR(from); 4194f46d3567SJeff Layton retry: 4195f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 41961da177e4SLinus Torvalds error = PTR_ERR(dentry); 41976902d925SDave Hansen if (IS_ERR(dentry)) 4198dae6ad8fSAl Viro goto out_putname; 41996902d925SDave Hansen 420091a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 42016521f891SChristian Brauner if (!error) { 42026521f891SChristian Brauner struct user_namespace *mnt_userns; 42036521f891SChristian Brauner 42046521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 42056521f891SChristian Brauner error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry, 42066521f891SChristian Brauner from->name); 42076521f891SChristian Brauner } 4208921a1650SAl Viro done_path_create(&path, dentry); 4209f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 4210f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 4211f46d3567SJeff Layton goto retry; 4212f46d3567SJeff Layton } 42136902d925SDave Hansen out_putname: 42141da177e4SLinus Torvalds putname(from); 42151da177e4SLinus Torvalds return error; 42161da177e4SLinus Torvalds } 42171da177e4SLinus Torvalds 4218b724e846SDominik Brodowski SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 4219b724e846SDominik Brodowski int, newdfd, const char __user *, newname) 4220b724e846SDominik Brodowski { 4221b724e846SDominik Brodowski return do_symlinkat(oldname, newdfd, newname); 4222b724e846SDominik Brodowski } 4223b724e846SDominik Brodowski 42243480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 42255590ff0dSUlrich Drepper { 4226b724e846SDominik Brodowski return do_symlinkat(oldname, AT_FDCWD, newname); 42275590ff0dSUlrich Drepper } 42285590ff0dSUlrich Drepper 4229146a8595SJ. Bruce Fields /** 4230146a8595SJ. Bruce Fields * vfs_link - create a new link 4231146a8595SJ. Bruce Fields * @old_dentry: object to be linked 42326521f891SChristian Brauner * @mnt_userns: the user namespace of the mount 4233146a8595SJ. Bruce Fields * @dir: new parent 4234146a8595SJ. Bruce Fields * @new_dentry: where to create the new link 4235146a8595SJ. Bruce Fields * @delegated_inode: returns inode needing a delegation break 4236146a8595SJ. Bruce Fields * 4237146a8595SJ. Bruce Fields * The caller must hold dir->i_mutex 4238146a8595SJ. Bruce Fields * 4239146a8595SJ. Bruce Fields * If vfs_link discovers a delegation on the to-be-linked file in need 4240146a8595SJ. Bruce Fields * of breaking, it will return -EWOULDBLOCK and return a reference to the 4241146a8595SJ. Bruce Fields * inode in delegated_inode. The caller should then break the delegation 4242146a8595SJ. Bruce Fields * and retry. Because breaking a delegation may take a long time, the 4243146a8595SJ. Bruce Fields * caller should drop the i_mutex before doing so. 4244146a8595SJ. Bruce Fields * 4245146a8595SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 4246146a8595SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 4247146a8595SJ. Bruce Fields * to be NFS exported. 42486521f891SChristian Brauner * 42496521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 42506521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 42516521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 42526521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 42536521f891SChristian Brauner * raw inode simply passs init_user_ns. 4254146a8595SJ. Bruce Fields */ 42556521f891SChristian Brauner int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns, 42566521f891SChristian Brauner struct inode *dir, struct dentry *new_dentry, 42576521f891SChristian Brauner struct inode **delegated_inode) 42581da177e4SLinus Torvalds { 42591da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 42608de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 42611da177e4SLinus Torvalds int error; 42621da177e4SLinus Torvalds 42631da177e4SLinus Torvalds if (!inode) 42641da177e4SLinus Torvalds return -ENOENT; 42651da177e4SLinus Torvalds 42666521f891SChristian Brauner error = may_create(mnt_userns, dir, new_dentry); 42671da177e4SLinus Torvalds if (error) 42681da177e4SLinus Torvalds return error; 42691da177e4SLinus Torvalds 42701da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 42711da177e4SLinus Torvalds return -EXDEV; 42721da177e4SLinus Torvalds 42731da177e4SLinus Torvalds /* 42741da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 42751da177e4SLinus Torvalds */ 42761da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 42771da177e4SLinus Torvalds return -EPERM; 42780bd23d09SEric W. Biederman /* 42790bd23d09SEric W. Biederman * Updating the link count will likely cause i_uid and i_gid to 42800bd23d09SEric W. Biederman * be writen back improperly if their true value is unknown to 42810bd23d09SEric W. Biederman * the vfs. 42820bd23d09SEric W. Biederman */ 42836521f891SChristian Brauner if (HAS_UNMAPPED_ID(mnt_userns, inode)) 42840bd23d09SEric W. Biederman return -EPERM; 4285acfa4380SAl Viro if (!dir->i_op->link) 42861da177e4SLinus Torvalds return -EPERM; 42877e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 42881da177e4SLinus Torvalds return -EPERM; 42891da177e4SLinus Torvalds 42901da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 42911da177e4SLinus Torvalds if (error) 42921da177e4SLinus Torvalds return error; 42931da177e4SLinus Torvalds 42945955102cSAl Viro inode_lock(inode); 4295aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 4296f4e0c30cSAl Viro if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 4297aae8a97dSAneesh Kumar K.V error = -ENOENT; 42988de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 42998de52778SAl Viro error = -EMLINK; 4300146a8595SJ. Bruce Fields else { 4301146a8595SJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 4302146a8595SJ. Bruce Fields if (!error) 43031da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 4304146a8595SJ. Bruce Fields } 4305f4e0c30cSAl Viro 4306f4e0c30cSAl Viro if (!error && (inode->i_state & I_LINKABLE)) { 4307f4e0c30cSAl Viro spin_lock(&inode->i_lock); 4308f4e0c30cSAl Viro inode->i_state &= ~I_LINKABLE; 4309f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 4310f4e0c30cSAl Viro } 43115955102cSAl Viro inode_unlock(inode); 4312e31e14ecSStephen Smalley if (!error) 43137e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 43141da177e4SLinus Torvalds return error; 43151da177e4SLinus Torvalds } 43164d359507SAl Viro EXPORT_SYMBOL(vfs_link); 43171da177e4SLinus Torvalds 43181da177e4SLinus Torvalds /* 43191da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 43201da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 43211da177e4SLinus Torvalds * newname. --KAB 43221da177e4SLinus Torvalds * 43231da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 43241da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 43251da177e4SLinus Torvalds * and other special files. --ADM 43261da177e4SLinus Torvalds */ 4327812931d6SChristoph Hellwig static int do_linkat(int olddfd, const char __user *oldname, int newdfd, 432846ea89ebSDominik Brodowski const char __user *newname, int flags) 43291da177e4SLinus Torvalds { 43306521f891SChristian Brauner struct user_namespace *mnt_userns; 43311da177e4SLinus Torvalds struct dentry *new_dentry; 4332dae6ad8fSAl Viro struct path old_path, new_path; 4333146a8595SJ. Bruce Fields struct inode *delegated_inode = NULL; 433411a7b371SAneesh Kumar K.V int how = 0; 43351da177e4SLinus Torvalds int error; 43361da177e4SLinus Torvalds 433711a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 4338c04030e1SUlrich Drepper return -EINVAL; 433911a7b371SAneesh Kumar K.V /* 4340f0cc6ffbSLinus Torvalds * To use null names we require CAP_DAC_READ_SEARCH 4341f0cc6ffbSLinus Torvalds * This ensures that not everyone will be able to create 4342f0cc6ffbSLinus Torvalds * handlink using the passed filedescriptor. 434311a7b371SAneesh Kumar K.V */ 4344f0cc6ffbSLinus Torvalds if (flags & AT_EMPTY_PATH) { 4345f0cc6ffbSLinus Torvalds if (!capable(CAP_DAC_READ_SEARCH)) 4346f0cc6ffbSLinus Torvalds return -ENOENT; 434711a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 4348f0cc6ffbSLinus Torvalds } 4349c04030e1SUlrich Drepper 435011a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 435111a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 4352442e31caSJeff Layton retry: 435311a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 43541da177e4SLinus Torvalds if (error) 43552ad94ae6SAl Viro return error; 43562ad94ae6SAl Viro 4357442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 4358442e31caSJeff Layton (how & LOOKUP_REVAL)); 43591da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 43606902d925SDave Hansen if (IS_ERR(new_dentry)) 4361dae6ad8fSAl Viro goto out; 4362dae6ad8fSAl Viro 4363dae6ad8fSAl Viro error = -EXDEV; 4364dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 4365dae6ad8fSAl Viro goto out_dput; 4366549c7297SChristian Brauner mnt_userns = mnt_user_ns(new_path.mnt); 4367549c7297SChristian Brauner error = may_linkat(mnt_userns, &old_path); 4368800179c9SKees Cook if (unlikely(error)) 4369800179c9SKees Cook goto out_dput; 4370dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 4371be6d3e56SKentaro Takeda if (error) 4372a8104a9fSAl Viro goto out_dput; 43736521f891SChristian Brauner error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode, 43746521f891SChristian Brauner new_dentry, &delegated_inode); 437575c3f29dSDave Hansen out_dput: 4376921a1650SAl Viro done_path_create(&new_path, new_dentry); 4377146a8595SJ. Bruce Fields if (delegated_inode) { 4378146a8595SJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 4379d22e6338SOleg Drokin if (!error) { 4380d22e6338SOleg Drokin path_put(&old_path); 4381146a8595SJ. Bruce Fields goto retry; 4382146a8595SJ. Bruce Fields } 4383d22e6338SOleg Drokin } 4384442e31caSJeff Layton if (retry_estale(error, how)) { 4385d22e6338SOleg Drokin path_put(&old_path); 4386442e31caSJeff Layton how |= LOOKUP_REVAL; 4387442e31caSJeff Layton goto retry; 4388442e31caSJeff Layton } 43891da177e4SLinus Torvalds out: 43902d8f3038SAl Viro path_put(&old_path); 43911da177e4SLinus Torvalds 43921da177e4SLinus Torvalds return error; 43931da177e4SLinus Torvalds } 43941da177e4SLinus Torvalds 439546ea89ebSDominik Brodowski SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 439646ea89ebSDominik Brodowski int, newdfd, const char __user *, newname, int, flags) 439746ea89ebSDominik Brodowski { 439846ea89ebSDominik Brodowski return do_linkat(olddfd, oldname, newdfd, newname, flags); 439946ea89ebSDominik Brodowski } 440046ea89ebSDominik Brodowski 44013480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 44025590ff0dSUlrich Drepper { 440346ea89ebSDominik Brodowski return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 44045590ff0dSUlrich Drepper } 44055590ff0dSUlrich Drepper 4406bc27027aSMiklos Szeredi /** 4407bc27027aSMiklos Szeredi * vfs_rename - rename a filesystem object 4408*2111c3c0SRandy Dunlap * @rd: pointer to &struct renamedata info 4409bc27027aSMiklos Szeredi * 4410bc27027aSMiklos Szeredi * The caller must hold multiple mutexes--see lock_rename()). 4411bc27027aSMiklos Szeredi * 4412bc27027aSMiklos Szeredi * If vfs_rename discovers a delegation in need of breaking at either 4413bc27027aSMiklos Szeredi * the source or destination, it will return -EWOULDBLOCK and return a 4414bc27027aSMiklos Szeredi * reference to the inode in delegated_inode. The caller should then 4415bc27027aSMiklos Szeredi * break the delegation and retry. Because breaking a delegation may 4416bc27027aSMiklos Szeredi * take a long time, the caller should drop all locks before doing 4417bc27027aSMiklos Szeredi * so. 4418bc27027aSMiklos Szeredi * 4419bc27027aSMiklos Szeredi * Alternatively, a caller may pass NULL for delegated_inode. This may 4420bc27027aSMiklos Szeredi * be appropriate for callers that expect the underlying filesystem not 4421bc27027aSMiklos Szeredi * to be NFS exported. 4422bc27027aSMiklos Szeredi * 44231da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 44241da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 44251da177e4SLinus Torvalds * Problems: 44260117d427SMauro Carvalho Chehab * 4427d03b29a2SJ. Bruce Fields * a) we can get into loop creation. 44281da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 44291da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 4430a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 44311da177e4SLinus Torvalds * story. 44326cedba89SJ. Bruce Fields * c) we have to lock _four_ objects - parents and victim (if it exists), 44336cedba89SJ. Bruce Fields * and source (if it is not a directory). 44341b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 44351da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 44361da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 4437a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 44381da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 44391da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 44401da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 4441a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 44421da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 44431da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 44441da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 4445e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 44461b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 44471da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 4448c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 44491da177e4SLinus Torvalds * locking]. 44501da177e4SLinus Torvalds */ 44519fe61450SChristian Brauner int vfs_rename(struct renamedata *rd) 44521da177e4SLinus Torvalds { 44531da177e4SLinus Torvalds int error; 44549fe61450SChristian Brauner struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; 44559fe61450SChristian Brauner struct dentry *old_dentry = rd->old_dentry; 44569fe61450SChristian Brauner struct dentry *new_dentry = rd->new_dentry; 44579fe61450SChristian Brauner struct inode **delegated_inode = rd->delegated_inode; 44589fe61450SChristian Brauner unsigned int flags = rd->flags; 4459bc27027aSMiklos Szeredi bool is_dir = d_is_dir(old_dentry); 4460bc27027aSMiklos Szeredi struct inode *source = old_dentry->d_inode; 4461bc27027aSMiklos Szeredi struct inode *target = new_dentry->d_inode; 4462da1ce067SMiklos Szeredi bool new_is_dir = false; 4463da1ce067SMiklos Szeredi unsigned max_links = new_dir->i_sb->s_max_links; 446449d31c2fSAl Viro struct name_snapshot old_name; 44651da177e4SLinus Torvalds 44668d3e2936SMiklos Szeredi if (source == target) 44671da177e4SLinus Torvalds return 0; 44681da177e4SLinus Torvalds 44696521f891SChristian Brauner error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir); 44701da177e4SLinus Torvalds if (error) 44711da177e4SLinus Torvalds return error; 44721da177e4SLinus Torvalds 4473da1ce067SMiklos Szeredi if (!target) { 44746521f891SChristian Brauner error = may_create(rd->new_mnt_userns, new_dir, new_dentry); 4475da1ce067SMiklos Szeredi } else { 4476da1ce067SMiklos Szeredi new_is_dir = d_is_dir(new_dentry); 4477da1ce067SMiklos Szeredi 4478da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 44796521f891SChristian Brauner error = may_delete(rd->new_mnt_userns, new_dir, 44806521f891SChristian Brauner new_dentry, is_dir); 4481da1ce067SMiklos Szeredi else 44826521f891SChristian Brauner error = may_delete(rd->new_mnt_userns, new_dir, 44836521f891SChristian Brauner new_dentry, new_is_dir); 4484da1ce067SMiklos Szeredi } 44851da177e4SLinus Torvalds if (error) 44861da177e4SLinus Torvalds return error; 44871da177e4SLinus Torvalds 44882773bf00SMiklos Szeredi if (!old_dir->i_op->rename) 44891da177e4SLinus Torvalds return -EPERM; 44901da177e4SLinus Torvalds 4491bc27027aSMiklos Szeredi /* 4492bc27027aSMiklos Szeredi * If we are going to change the parent - check write permissions, 4493bc27027aSMiklos Szeredi * we'll need to flip '..'. 4494bc27027aSMiklos Szeredi */ 4495da1ce067SMiklos Szeredi if (new_dir != old_dir) { 4496da1ce067SMiklos Szeredi if (is_dir) { 44976521f891SChristian Brauner error = inode_permission(rd->old_mnt_userns, source, 449847291baaSChristian Brauner MAY_WRITE); 4499bc27027aSMiklos Szeredi if (error) 4500bc27027aSMiklos Szeredi return error; 4501bc27027aSMiklos Szeredi } 4502da1ce067SMiklos Szeredi if ((flags & RENAME_EXCHANGE) && new_is_dir) { 45036521f891SChristian Brauner error = inode_permission(rd->new_mnt_userns, target, 450447291baaSChristian Brauner MAY_WRITE); 4505da1ce067SMiklos Szeredi if (error) 4506da1ce067SMiklos Szeredi return error; 4507da1ce067SMiklos Szeredi } 4508da1ce067SMiklos Szeredi } 45090eeca283SRobert Love 45100b3974ebSMiklos Szeredi error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, 45110b3974ebSMiklos Szeredi flags); 4512bc27027aSMiklos Szeredi if (error) 4513bc27027aSMiklos Szeredi return error; 4514bc27027aSMiklos Szeredi 451549d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 4516bc27027aSMiklos Szeredi dget(new_dentry); 4517da1ce067SMiklos Szeredi if (!is_dir || (flags & RENAME_EXCHANGE)) 4518bc27027aSMiklos Szeredi lock_two_nondirectories(source, target); 4519bc27027aSMiklos Szeredi else if (target) 45205955102cSAl Viro inode_lock(target); 4521bc27027aSMiklos Szeredi 4522bc27027aSMiklos Szeredi error = -EBUSY; 45237af1364fSEric W. Biederman if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) 4524bc27027aSMiklos Szeredi goto out; 4525bc27027aSMiklos Szeredi 4526da1ce067SMiklos Szeredi if (max_links && new_dir != old_dir) { 4527bc27027aSMiklos Szeredi error = -EMLINK; 4528da1ce067SMiklos Szeredi if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) 4529bc27027aSMiklos Szeredi goto out; 4530da1ce067SMiklos Szeredi if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && 4531da1ce067SMiklos Szeredi old_dir->i_nlink >= max_links) 4532da1ce067SMiklos Szeredi goto out; 4533da1ce067SMiklos Szeredi } 4534da1ce067SMiklos Szeredi if (!is_dir) { 4535bc27027aSMiklos Szeredi error = try_break_deleg(source, delegated_inode); 4536bc27027aSMiklos Szeredi if (error) 4537bc27027aSMiklos Szeredi goto out; 4538da1ce067SMiklos Szeredi } 4539da1ce067SMiklos Szeredi if (target && !new_is_dir) { 4540bc27027aSMiklos Szeredi error = try_break_deleg(target, delegated_inode); 4541bc27027aSMiklos Szeredi if (error) 4542bc27027aSMiklos Szeredi goto out; 4543bc27027aSMiklos Szeredi } 4544549c7297SChristian Brauner error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry, 4545520c8b16SMiklos Szeredi new_dir, new_dentry, flags); 4546bc27027aSMiklos Szeredi if (error) 4547bc27027aSMiklos Szeredi goto out; 4548bc27027aSMiklos Szeredi 4549da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE) && target) { 45508767712fSAl Viro if (is_dir) { 45518767712fSAl Viro shrink_dcache_parent(new_dentry); 4552bc27027aSMiklos Szeredi target->i_flags |= S_DEAD; 45538767712fSAl Viro } 4554bc27027aSMiklos Szeredi dont_mount(new_dentry); 45558ed936b5SEric W. Biederman detach_mounts(new_dentry); 4556bc27027aSMiklos Szeredi } 4557da1ce067SMiklos Szeredi if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { 4558da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 4559bc27027aSMiklos Szeredi d_move(old_dentry, new_dentry); 4560da1ce067SMiklos Szeredi else 4561da1ce067SMiklos Szeredi d_exchange(old_dentry, new_dentry); 4562da1ce067SMiklos Szeredi } 4563bc27027aSMiklos Szeredi out: 4564da1ce067SMiklos Szeredi if (!is_dir || (flags & RENAME_EXCHANGE)) 4565bc27027aSMiklos Szeredi unlock_two_nondirectories(source, target); 4566bc27027aSMiklos Szeredi else if (target) 45675955102cSAl Viro inode_unlock(target); 4568bc27027aSMiklos Szeredi dput(new_dentry); 4569da1ce067SMiklos Szeredi if (!error) { 4570f4ec3a3dSAl Viro fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, 4571da1ce067SMiklos Szeredi !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); 4572da1ce067SMiklos Szeredi if (flags & RENAME_EXCHANGE) { 4573f4ec3a3dSAl Viro fsnotify_move(new_dir, old_dir, &old_dentry->d_name, 4574da1ce067SMiklos Szeredi new_is_dir, NULL, new_dentry); 4575da1ce067SMiklos Szeredi } 4576da1ce067SMiklos Szeredi } 457749d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 45780eeca283SRobert Love 45791da177e4SLinus Torvalds return error; 45801da177e4SLinus Torvalds } 45814d359507SAl Viro EXPORT_SYMBOL(vfs_rename); 45821da177e4SLinus Torvalds 4583e886663cSJens Axboe int do_renameat2(int olddfd, struct filename *from, int newdfd, 4584e886663cSJens Axboe struct filename *to, unsigned int flags) 45851da177e4SLinus Torvalds { 45869fe61450SChristian Brauner struct renamedata rd; 45871da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 45881da177e4SLinus Torvalds struct dentry *trap; 4589f5beed75SAl Viro struct path old_path, new_path; 4590f5beed75SAl Viro struct qstr old_last, new_last; 4591f5beed75SAl Viro int old_type, new_type; 45928e6d782cSJ. Bruce Fields struct inode *delegated_inode = NULL; 4593f5beed75SAl Viro unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; 4594c6a94284SJeff Layton bool should_retry = false; 4595e886663cSJens Axboe int error = -EINVAL; 4596520c8b16SMiklos Szeredi 45970d7a8555SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 4598e886663cSJens Axboe goto put_both; 4599da1ce067SMiklos Szeredi 46000d7a8555SMiklos Szeredi if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && 46010d7a8555SMiklos Szeredi (flags & RENAME_EXCHANGE)) 4602e886663cSJens Axboe goto put_both; 4603520c8b16SMiklos Szeredi 4604f5beed75SAl Viro if (flags & RENAME_EXCHANGE) 4605f5beed75SAl Viro target_flags = 0; 4606f5beed75SAl Viro 4607c6a94284SJeff Layton retry: 4608e886663cSJens Axboe from = filename_parentat(olddfd, from, lookup_flags, &old_path, 4609e886663cSJens Axboe &old_last, &old_type); 461091a27b2aSJeff Layton if (IS_ERR(from)) { 461191a27b2aSJeff Layton error = PTR_ERR(from); 4612e886663cSJens Axboe goto put_new; 461391a27b2aSJeff Layton } 46141da177e4SLinus Torvalds 4615e886663cSJens Axboe to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, 4616e886663cSJens Axboe &new_type); 461791a27b2aSJeff Layton if (IS_ERR(to)) { 461891a27b2aSJeff Layton error = PTR_ERR(to); 46191da177e4SLinus Torvalds goto exit1; 462091a27b2aSJeff Layton } 46211da177e4SLinus Torvalds 46221da177e4SLinus Torvalds error = -EXDEV; 4623f5beed75SAl Viro if (old_path.mnt != new_path.mnt) 46241da177e4SLinus Torvalds goto exit2; 46251da177e4SLinus Torvalds 46261da177e4SLinus Torvalds error = -EBUSY; 4627f5beed75SAl Viro if (old_type != LAST_NORM) 46281da177e4SLinus Torvalds goto exit2; 46291da177e4SLinus Torvalds 46300a7c3937SMiklos Szeredi if (flags & RENAME_NOREPLACE) 46310a7c3937SMiklos Szeredi error = -EEXIST; 4632f5beed75SAl Viro if (new_type != LAST_NORM) 46331da177e4SLinus Torvalds goto exit2; 46341da177e4SLinus Torvalds 4635f5beed75SAl Viro error = mnt_want_write(old_path.mnt); 4636c30dabfeSJan Kara if (error) 4637c30dabfeSJan Kara goto exit2; 4638c30dabfeSJan Kara 46398e6d782cSJ. Bruce Fields retry_deleg: 4640f5beed75SAl Viro trap = lock_rename(new_path.dentry, old_path.dentry); 46411da177e4SLinus Torvalds 4642f5beed75SAl Viro old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); 46431da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 46441da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 46451da177e4SLinus Torvalds goto exit3; 46461da177e4SLinus Torvalds /* source must exist */ 46471da177e4SLinus Torvalds error = -ENOENT; 4648b18825a7SDavid Howells if (d_is_negative(old_dentry)) 46491da177e4SLinus Torvalds goto exit4; 4650f5beed75SAl Viro new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); 46511da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 46521da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 46531da177e4SLinus Torvalds goto exit4; 46540a7c3937SMiklos Szeredi error = -EEXIST; 46550a7c3937SMiklos Szeredi if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) 46560a7c3937SMiklos Szeredi goto exit5; 4657da1ce067SMiklos Szeredi if (flags & RENAME_EXCHANGE) { 4658da1ce067SMiklos Szeredi error = -ENOENT; 4659da1ce067SMiklos Szeredi if (d_is_negative(new_dentry)) 4660da1ce067SMiklos Szeredi goto exit5; 4661da1ce067SMiklos Szeredi 4662da1ce067SMiklos Szeredi if (!d_is_dir(new_dentry)) { 4663da1ce067SMiklos Szeredi error = -ENOTDIR; 4664f5beed75SAl Viro if (new_last.name[new_last.len]) 4665da1ce067SMiklos Szeredi goto exit5; 4666da1ce067SMiklos Szeredi } 4667da1ce067SMiklos Szeredi } 46680a7c3937SMiklos Szeredi /* unless the source is a directory trailing slashes give -ENOTDIR */ 46690a7c3937SMiklos Szeredi if (!d_is_dir(old_dentry)) { 46700a7c3937SMiklos Szeredi error = -ENOTDIR; 4671f5beed75SAl Viro if (old_last.name[old_last.len]) 46720a7c3937SMiklos Szeredi goto exit5; 4673f5beed75SAl Viro if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) 46740a7c3937SMiklos Szeredi goto exit5; 46750a7c3937SMiklos Szeredi } 46760a7c3937SMiklos Szeredi /* source should not be ancestor of target */ 46770a7c3937SMiklos Szeredi error = -EINVAL; 46780a7c3937SMiklos Szeredi if (old_dentry == trap) 46790a7c3937SMiklos Szeredi goto exit5; 46801da177e4SLinus Torvalds /* target should not be an ancestor of source */ 4681da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 46821da177e4SLinus Torvalds error = -ENOTEMPTY; 46831da177e4SLinus Torvalds if (new_dentry == trap) 46841da177e4SLinus Torvalds goto exit5; 46851da177e4SLinus Torvalds 4686f5beed75SAl Viro error = security_path_rename(&old_path, old_dentry, 4687f5beed75SAl Viro &new_path, new_dentry, flags); 4688be6d3e56SKentaro Takeda if (error) 4689c30dabfeSJan Kara goto exit5; 46909fe61450SChristian Brauner 46919fe61450SChristian Brauner rd.old_dir = old_path.dentry->d_inode; 46929fe61450SChristian Brauner rd.old_dentry = old_dentry; 46936521f891SChristian Brauner rd.old_mnt_userns = mnt_user_ns(old_path.mnt); 46949fe61450SChristian Brauner rd.new_dir = new_path.dentry->d_inode; 46959fe61450SChristian Brauner rd.new_dentry = new_dentry; 46966521f891SChristian Brauner rd.new_mnt_userns = mnt_user_ns(new_path.mnt); 46979fe61450SChristian Brauner rd.delegated_inode = &delegated_inode; 46989fe61450SChristian Brauner rd.flags = flags; 46999fe61450SChristian Brauner error = vfs_rename(&rd); 47001da177e4SLinus Torvalds exit5: 47011da177e4SLinus Torvalds dput(new_dentry); 47021da177e4SLinus Torvalds exit4: 47031da177e4SLinus Torvalds dput(old_dentry); 47041da177e4SLinus Torvalds exit3: 4705f5beed75SAl Viro unlock_rename(new_path.dentry, old_path.dentry); 47068e6d782cSJ. Bruce Fields if (delegated_inode) { 47078e6d782cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 47088e6d782cSJ. Bruce Fields if (!error) 47098e6d782cSJ. Bruce Fields goto retry_deleg; 47108e6d782cSJ. Bruce Fields } 4711f5beed75SAl Viro mnt_drop_write(old_path.mnt); 47121da177e4SLinus Torvalds exit2: 4713c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 4714c6a94284SJeff Layton should_retry = true; 4715f5beed75SAl Viro path_put(&new_path); 47161da177e4SLinus Torvalds exit1: 4717f5beed75SAl Viro path_put(&old_path); 4718c6a94284SJeff Layton if (should_retry) { 4719c6a94284SJeff Layton should_retry = false; 4720c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 4721c6a94284SJeff Layton goto retry; 4722c6a94284SJeff Layton } 4723e886663cSJens Axboe put_both: 4724e886663cSJens Axboe if (!IS_ERR(from)) 4725e886663cSJens Axboe putname(from); 4726e886663cSJens Axboe put_new: 4727e886663cSJens Axboe if (!IS_ERR(to)) 4728e886663cSJens Axboe putname(to); 47291da177e4SLinus Torvalds return error; 47301da177e4SLinus Torvalds } 47311da177e4SLinus Torvalds 4732ee81feb6SDominik Brodowski SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, 4733ee81feb6SDominik Brodowski int, newdfd, const char __user *, newname, unsigned int, flags) 4734ee81feb6SDominik Brodowski { 4735e886663cSJens Axboe return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 4736e886663cSJens Axboe flags); 4737ee81feb6SDominik Brodowski } 4738ee81feb6SDominik Brodowski 4739520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 4740520c8b16SMiklos Szeredi int, newdfd, const char __user *, newname) 4741520c8b16SMiklos Szeredi { 4742e886663cSJens Axboe return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 4743e886663cSJens Axboe 0); 4744520c8b16SMiklos Szeredi } 4745520c8b16SMiklos Szeredi 4746a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 47475590ff0dSUlrich Drepper { 4748e886663cSJens Axboe return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, 4749e886663cSJens Axboe getname(newname), 0); 47505590ff0dSUlrich Drepper } 47515590ff0dSUlrich Drepper 47525d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link) 47531da177e4SLinus Torvalds { 47545d826c84SAl Viro int len = PTR_ERR(link); 47551da177e4SLinus Torvalds if (IS_ERR(link)) 47561da177e4SLinus Torvalds goto out; 47571da177e4SLinus Torvalds 47581da177e4SLinus Torvalds len = strlen(link); 47591da177e4SLinus Torvalds if (len > (unsigned) buflen) 47601da177e4SLinus Torvalds len = buflen; 47611da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 47621da177e4SLinus Torvalds len = -EFAULT; 47631da177e4SLinus Torvalds out: 47641da177e4SLinus Torvalds return len; 47651da177e4SLinus Torvalds } 47661da177e4SLinus Torvalds 4767d60874cdSMiklos Szeredi /** 4768fd4a0edfSMiklos Szeredi * vfs_readlink - copy symlink body into userspace buffer 4769fd4a0edfSMiklos Szeredi * @dentry: dentry on which to get symbolic link 4770fd4a0edfSMiklos Szeredi * @buffer: user memory pointer 4771fd4a0edfSMiklos Szeredi * @buflen: size of buffer 4772fd4a0edfSMiklos Szeredi * 4773fd4a0edfSMiklos Szeredi * Does not touch atime. That's up to the caller if necessary 4774fd4a0edfSMiklos Szeredi * 4775fd4a0edfSMiklos Szeredi * Does not call security hook. 4776fd4a0edfSMiklos Szeredi */ 4777fd4a0edfSMiklos Szeredi int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) 4778fd4a0edfSMiklos Szeredi { 4779fd4a0edfSMiklos Szeredi struct inode *inode = d_inode(dentry); 4780f2df5da6SAl Viro DEFINE_DELAYED_CALL(done); 4781f2df5da6SAl Viro const char *link; 4782f2df5da6SAl Viro int res; 4783fd4a0edfSMiklos Szeredi 478476fca90eSMiklos Szeredi if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { 478576fca90eSMiklos Szeredi if (unlikely(inode->i_op->readlink)) 478676fca90eSMiklos Szeredi return inode->i_op->readlink(dentry, buffer, buflen); 478776fca90eSMiklos Szeredi 478876fca90eSMiklos Szeredi if (!d_is_symlink(dentry)) 4789fd4a0edfSMiklos Szeredi return -EINVAL; 4790fd4a0edfSMiklos Szeredi 479176fca90eSMiklos Szeredi spin_lock(&inode->i_lock); 479276fca90eSMiklos Szeredi inode->i_opflags |= IOP_DEFAULT_READLINK; 479376fca90eSMiklos Szeredi spin_unlock(&inode->i_lock); 479476fca90eSMiklos Szeredi } 479576fca90eSMiklos Szeredi 47964c4f7c19SEric Biggers link = READ_ONCE(inode->i_link); 4797f2df5da6SAl Viro if (!link) { 4798f2df5da6SAl Viro link = inode->i_op->get_link(dentry, inode, &done); 4799f2df5da6SAl Viro if (IS_ERR(link)) 4800f2df5da6SAl Viro return PTR_ERR(link); 4801f2df5da6SAl Viro } 4802f2df5da6SAl Viro res = readlink_copy(buffer, buflen, link); 4803f2df5da6SAl Viro do_delayed_call(&done); 4804f2df5da6SAl Viro return res; 4805fd4a0edfSMiklos Szeredi } 4806fd4a0edfSMiklos Szeredi EXPORT_SYMBOL(vfs_readlink); 48071da177e4SLinus Torvalds 4808d60874cdSMiklos Szeredi /** 4809d60874cdSMiklos Szeredi * vfs_get_link - get symlink body 4810d60874cdSMiklos Szeredi * @dentry: dentry on which to get symbolic link 4811d60874cdSMiklos Szeredi * @done: caller needs to free returned data with this 4812d60874cdSMiklos Szeredi * 4813d60874cdSMiklos Szeredi * Calls security hook and i_op->get_link() on the supplied inode. 4814d60874cdSMiklos Szeredi * 4815d60874cdSMiklos Szeredi * It does not touch atime. That's up to the caller if necessary. 4816d60874cdSMiklos Szeredi * 4817d60874cdSMiklos Szeredi * Does not work on "special" symlinks like /proc/$$/fd/N 4818d60874cdSMiklos Szeredi */ 4819d60874cdSMiklos Szeredi const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) 4820d60874cdSMiklos Szeredi { 4821d60874cdSMiklos Szeredi const char *res = ERR_PTR(-EINVAL); 4822d60874cdSMiklos Szeredi struct inode *inode = d_inode(dentry); 4823d60874cdSMiklos Szeredi 4824d60874cdSMiklos Szeredi if (d_is_symlink(dentry)) { 4825d60874cdSMiklos Szeredi res = ERR_PTR(security_inode_readlink(dentry)); 4826d60874cdSMiklos Szeredi if (!res) 4827d60874cdSMiklos Szeredi res = inode->i_op->get_link(dentry, inode, done); 4828d60874cdSMiklos Szeredi } 4829d60874cdSMiklos Szeredi return res; 4830d60874cdSMiklos Szeredi } 4831d60874cdSMiklos Szeredi EXPORT_SYMBOL(vfs_get_link); 4832d60874cdSMiklos Szeredi 48331da177e4SLinus Torvalds /* get the link contents into pagecache */ 48346b255391SAl Viro const char *page_get_link(struct dentry *dentry, struct inode *inode, 4835fceef393SAl Viro struct delayed_call *callback) 48361da177e4SLinus Torvalds { 4837ebd09abbSDuane Griffin char *kaddr; 48381da177e4SLinus Torvalds struct page *page; 48396b255391SAl Viro struct address_space *mapping = inode->i_mapping; 48406b255391SAl Viro 4841d3883d4fSAl Viro if (!dentry) { 4842d3883d4fSAl Viro page = find_get_page(mapping, 0); 4843d3883d4fSAl Viro if (!page) 48446b255391SAl Viro return ERR_PTR(-ECHILD); 4845d3883d4fSAl Viro if (!PageUptodate(page)) { 4846d3883d4fSAl Viro put_page(page); 4847d3883d4fSAl Viro return ERR_PTR(-ECHILD); 4848d3883d4fSAl Viro } 4849d3883d4fSAl Viro } else { 4850090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 48511da177e4SLinus Torvalds if (IS_ERR(page)) 48526fe6900eSNick Piggin return (char*)page; 4853d3883d4fSAl Viro } 4854fceef393SAl Viro set_delayed_call(callback, page_put_link, page); 485521fc61c7SAl Viro BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); 485621fc61c7SAl Viro kaddr = page_address(page); 48576b255391SAl Viro nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); 4858ebd09abbSDuane Griffin return kaddr; 48591da177e4SLinus Torvalds } 48601da177e4SLinus Torvalds 48616b255391SAl Viro EXPORT_SYMBOL(page_get_link); 48621da177e4SLinus Torvalds 4863fceef393SAl Viro void page_put_link(void *arg) 48641da177e4SLinus Torvalds { 4865fceef393SAl Viro put_page(arg); 48661da177e4SLinus Torvalds } 48674d359507SAl Viro EXPORT_SYMBOL(page_put_link); 48681da177e4SLinus Torvalds 4869aa80deabSAl Viro int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 4870aa80deabSAl Viro { 4871fceef393SAl Viro DEFINE_DELAYED_CALL(done); 48726b255391SAl Viro int res = readlink_copy(buffer, buflen, 48736b255391SAl Viro page_get_link(dentry, d_inode(dentry), 4874fceef393SAl Viro &done)); 4875fceef393SAl Viro do_delayed_call(&done); 4876aa80deabSAl Viro return res; 4877aa80deabSAl Viro } 4878aa80deabSAl Viro EXPORT_SYMBOL(page_readlink); 4879aa80deabSAl Viro 488054566b2cSNick Piggin /* 488154566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 488254566b2cSNick Piggin */ 488354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 48841da177e4SLinus Torvalds { 48851da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 48860adb25d2SKirill Korotaev struct page *page; 4887afddba49SNick Piggin void *fsdata; 4888beb497abSDmitriy Monakhov int err; 4889c718a975STetsuo Handa unsigned int flags = 0; 489054566b2cSNick Piggin if (nofs) 489154566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 48921da177e4SLinus Torvalds 48937e53cac4SNeilBrown retry: 4894afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 489554566b2cSNick Piggin flags, &page, &fsdata); 48961da177e4SLinus Torvalds if (err) 4897afddba49SNick Piggin goto fail; 4898afddba49SNick Piggin 489921fc61c7SAl Viro memcpy(page_address(page), symname, len-1); 4900afddba49SNick Piggin 4901afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4902afddba49SNick Piggin page, fsdata); 49031da177e4SLinus Torvalds if (err < 0) 49041da177e4SLinus Torvalds goto fail; 4905afddba49SNick Piggin if (err < len-1) 4906afddba49SNick Piggin goto retry; 4907afddba49SNick Piggin 49081da177e4SLinus Torvalds mark_inode_dirty(inode); 49091da177e4SLinus Torvalds return 0; 49101da177e4SLinus Torvalds fail: 49111da177e4SLinus Torvalds return err; 49121da177e4SLinus Torvalds } 49134d359507SAl Viro EXPORT_SYMBOL(__page_symlink); 49141da177e4SLinus Torvalds 49150adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 49160adb25d2SKirill Korotaev { 49170adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 4918c62d2555SMichal Hocko !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); 49190adb25d2SKirill Korotaev } 49204d359507SAl Viro EXPORT_SYMBOL(page_symlink); 49210adb25d2SKirill Korotaev 492292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 49236b255391SAl Viro .get_link = page_get_link, 49241da177e4SLinus Torvalds }; 49251da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4926