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 { 25091ef658fSDmitry Kadashev if (IS_ERR_OR_NULL(name)) 25191ef658fSDmitry Kadashev return; 25291ef658fSDmitry Kadashev 25355422d0bSPaul Moore BUG_ON(name->refcnt <= 0); 25455422d0bSPaul Moore 25555422d0bSPaul Moore if (--name->refcnt > 0) 25655422d0bSPaul Moore return; 25755422d0bSPaul Moore 258fd2f7cb5SAl Viro if (name->name != name->iname) { 25955422d0bSPaul Moore __putname(name->name); 26055422d0bSPaul Moore kfree(name); 26155422d0bSPaul Moore } else 26255422d0bSPaul Moore __putname(name); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 26547291baaSChristian Brauner /** 26647291baaSChristian Brauner * check_acl - perform ACL permission checking 26747291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 26847291baaSChristian Brauner * @inode: inode to check permissions on 26947291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 27047291baaSChristian Brauner * 27147291baaSChristian Brauner * This function performs the ACL permission checking. Since this function 27247291baaSChristian Brauner * retrieve POSIX acls it needs to know whether it is called from a blocking or 27347291baaSChristian Brauner * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 27447291baaSChristian Brauner * 27547291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 27647291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 27747291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 27847291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 27947291baaSChristian Brauner * raw inode simply passs init_user_ns. 28047291baaSChristian Brauner */ 28147291baaSChristian Brauner static int check_acl(struct user_namespace *mnt_userns, 28247291baaSChristian Brauner struct inode *inode, int mask) 283e77819e5SLinus Torvalds { 28484635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 285e77819e5SLinus Torvalds struct posix_acl *acl; 286e77819e5SLinus Torvalds 287e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2883567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2893567866bSAl Viro if (!acl) 290e77819e5SLinus Torvalds return -EAGAIN; 2913567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 292b8a7a3a6SAndreas Gruenbacher if (is_uncached_acl(acl)) 293e77819e5SLinus Torvalds return -ECHILD; 29447291baaSChristian Brauner return posix_acl_permission(mnt_userns, inode, acl, mask); 295e77819e5SLinus Torvalds } 296e77819e5SLinus Torvalds 2972982baa2SChristoph Hellwig acl = get_acl(inode, ACL_TYPE_ACCESS); 2984e34e719SChristoph Hellwig if (IS_ERR(acl)) 2994e34e719SChristoph Hellwig return PTR_ERR(acl); 300e77819e5SLinus Torvalds if (acl) { 30147291baaSChristian Brauner int error = posix_acl_permission(mnt_userns, inode, acl, mask); 302e77819e5SLinus Torvalds posix_acl_release(acl); 303e77819e5SLinus Torvalds return error; 304e77819e5SLinus Torvalds } 30584635d68SLinus Torvalds #endif 306e77819e5SLinus Torvalds 307e77819e5SLinus Torvalds return -EAGAIN; 308e77819e5SLinus Torvalds } 309e77819e5SLinus Torvalds 31047291baaSChristian Brauner /** 31147291baaSChristian Brauner * acl_permission_check - perform basic UNIX permission checking 31247291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 31347291baaSChristian Brauner * @inode: inode to check permissions on 31447291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 3155fc475b7SLinus Torvalds * 31647291baaSChristian Brauner * This function performs the basic UNIX permission checking. Since this 31747291baaSChristian Brauner * function may retrieve POSIX acls it needs to know whether it is called from a 31847291baaSChristian Brauner * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 31947291baaSChristian Brauner * 32047291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 32147291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 32247291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 32347291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 32447291baaSChristian Brauner * raw inode simply passs init_user_ns. 3255909ccaaSLinus Torvalds */ 32647291baaSChristian Brauner static int acl_permission_check(struct user_namespace *mnt_userns, 32747291baaSChristian Brauner struct inode *inode, int mask) 3285909ccaaSLinus Torvalds { 32926cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 33047291baaSChristian Brauner kuid_t i_uid; 3315909ccaaSLinus Torvalds 3325fc475b7SLinus Torvalds /* Are we the owner? If so, ACL's don't matter */ 33347291baaSChristian Brauner i_uid = i_uid_into_mnt(mnt_userns, inode); 33447291baaSChristian Brauner if (likely(uid_eq(current_fsuid(), i_uid))) { 3355fc475b7SLinus Torvalds mask &= 7; 3365909ccaaSLinus Torvalds mode >>= 6; 3375fc475b7SLinus Torvalds return (mask & ~mode) ? -EACCES : 0; 3385fc475b7SLinus Torvalds } 3395fc475b7SLinus Torvalds 3405fc475b7SLinus Torvalds /* Do we have ACL's? */ 341e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 34247291baaSChristian Brauner int error = check_acl(mnt_userns, inode, mask); 3435909ccaaSLinus Torvalds if (error != -EAGAIN) 3445909ccaaSLinus Torvalds return error; 3455909ccaaSLinus Torvalds } 3465909ccaaSLinus Torvalds 3475fc475b7SLinus Torvalds /* Only RWX matters for group/other mode bits */ 3485fc475b7SLinus Torvalds mask &= 7; 3495fc475b7SLinus Torvalds 3505fc475b7SLinus Torvalds /* 3515fc475b7SLinus Torvalds * Are the group permissions different from 3525fc475b7SLinus Torvalds * the other permissions in the bits we care 3535fc475b7SLinus Torvalds * about? Need to check group ownership if so. 3545fc475b7SLinus Torvalds */ 3555fc475b7SLinus Torvalds if (mask & (mode ^ (mode >> 3))) { 35647291baaSChristian Brauner kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); 35747291baaSChristian Brauner if (in_group_p(kgid)) 3585909ccaaSLinus Torvalds mode >>= 3; 3595909ccaaSLinus Torvalds } 3605909ccaaSLinus Torvalds 3615fc475b7SLinus Torvalds /* Bits in 'mode' clear that we require? */ 3625fc475b7SLinus Torvalds return (mask & ~mode) ? -EACCES : 0; 3635909ccaaSLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /** 3661da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 36747291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 3681da177e4SLinus Torvalds * @inode: inode to check access rights for 3695fc475b7SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, 3705fc475b7SLinus Torvalds * %MAY_NOT_BLOCK ...) 3711da177e4SLinus Torvalds * 3721da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 3731da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 3741da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 375b74c79e9SNick Piggin * are used for other things. 376b74c79e9SNick Piggin * 377b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 378b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 379b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 38047291baaSChristian Brauner * 38147291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 38247291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 38347291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 38447291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 38547291baaSChristian Brauner * raw inode simply passs init_user_ns. 3861da177e4SLinus Torvalds */ 38747291baaSChristian Brauner int generic_permission(struct user_namespace *mnt_userns, struct inode *inode, 38847291baaSChristian Brauner int mask) 3891da177e4SLinus Torvalds { 3905909ccaaSLinus Torvalds int ret; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /* 393948409c7SAndreas Gruenbacher * Do the basic permission checks. 3941da177e4SLinus Torvalds */ 39547291baaSChristian Brauner ret = acl_permission_check(mnt_userns, inode, mask); 3965909ccaaSLinus Torvalds if (ret != -EACCES) 3975909ccaaSLinus Torvalds return ret; 3981da177e4SLinus Torvalds 399d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 400d594e7ecSAl Viro /* DACs are overridable for directories */ 401d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 40247291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 40323adbe12SAndy Lutomirski CAP_DAC_READ_SEARCH)) 404d594e7ecSAl Viro return 0; 40547291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4060558c1bfSChristian Brauner CAP_DAC_OVERRIDE)) 4071da177e4SLinus Torvalds return 0; 4082a4c2242SStephen Smalley return -EACCES; 4092a4c2242SStephen Smalley } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* 4121da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 4131da177e4SLinus Torvalds */ 4147ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 415d594e7ecSAl Viro if (mask == MAY_READ) 41647291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4170558c1bfSChristian Brauner CAP_DAC_READ_SEARCH)) 4181da177e4SLinus Torvalds return 0; 4192a4c2242SStephen Smalley /* 4202a4c2242SStephen Smalley * Read/write DACs are always overridable. 4212a4c2242SStephen Smalley * Executable DACs are overridable when there is 4222a4c2242SStephen Smalley * at least one exec bit set. 4232a4c2242SStephen Smalley */ 4242a4c2242SStephen Smalley if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 42547291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4260558c1bfSChristian Brauner CAP_DAC_OVERRIDE)) 4272a4c2242SStephen Smalley return 0; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds return -EACCES; 4301da177e4SLinus Torvalds } 4314d359507SAl Viro EXPORT_SYMBOL(generic_permission); 4321da177e4SLinus Torvalds 43347291baaSChristian Brauner /** 43447291baaSChristian Brauner * do_inode_permission - UNIX permission checking 43547291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 43647291baaSChristian Brauner * @inode: inode to check permissions on 43747291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 43847291baaSChristian Brauner * 4393ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 4403ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 4413ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 4423ddcd056SLinus Torvalds * permission function, use the fast case". 4433ddcd056SLinus Torvalds */ 44447291baaSChristian Brauner static inline int do_inode_permission(struct user_namespace *mnt_userns, 44547291baaSChristian Brauner struct inode *inode, int mask) 4463ddcd056SLinus Torvalds { 4473ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 4483ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 449549c7297SChristian Brauner return inode->i_op->permission(mnt_userns, inode, mask); 4503ddcd056SLinus Torvalds 4513ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 4523ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 4533ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 4543ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 4553ddcd056SLinus Torvalds } 45647291baaSChristian Brauner return generic_permission(mnt_userns, inode, mask); 4573ddcd056SLinus Torvalds } 4583ddcd056SLinus Torvalds 459cb23beb5SChristoph Hellwig /** 4600bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 4610bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 46255852635SRandy Dunlap * @inode: Inode to check permission on 4630bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4640bdaea90SDavid Howells * 4650bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 4660bdaea90SDavid Howells */ 4670bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 4680bdaea90SDavid Howells { 4690bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 4700bdaea90SDavid Howells umode_t mode = inode->i_mode; 4710bdaea90SDavid Howells 4720bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 473bc98a42cSDavid Howells if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 4740bdaea90SDavid Howells return -EROFS; 4750bdaea90SDavid Howells } 4760bdaea90SDavid Howells return 0; 4770bdaea90SDavid Howells } 4780bdaea90SDavid Howells 4790bdaea90SDavid Howells /** 4800bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 48147291baaSChristian Brauner * @mnt_userns: User namespace of the mount the inode was found from 4820bdaea90SDavid Howells * @inode: Inode to check permission on 4830bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4840bdaea90SDavid Howells * 4850bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4860bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4870bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4880bdaea90SDavid Howells * 4890bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4900bdaea90SDavid Howells */ 49147291baaSChristian Brauner int inode_permission(struct user_namespace *mnt_userns, 49247291baaSChristian Brauner struct inode *inode, int mask) 4930bdaea90SDavid Howells { 4940bdaea90SDavid Howells int retval; 4950bdaea90SDavid Howells 4960bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4970bdaea90SDavid Howells if (retval) 4980bdaea90SDavid Howells return retval; 4994bfd054aSEric Biggers 5004bfd054aSEric Biggers if (unlikely(mask & MAY_WRITE)) { 5014bfd054aSEric Biggers /* 5024bfd054aSEric Biggers * Nobody gets write access to an immutable file. 5034bfd054aSEric Biggers */ 5044bfd054aSEric Biggers if (IS_IMMUTABLE(inode)) 5054bfd054aSEric Biggers return -EPERM; 5064bfd054aSEric Biggers 5074bfd054aSEric Biggers /* 5084bfd054aSEric Biggers * Updating mtime will likely cause i_uid and i_gid to be 5094bfd054aSEric Biggers * written back improperly if their true value is unknown 5104bfd054aSEric Biggers * to the vfs. 5114bfd054aSEric Biggers */ 512ba73d987SChristian Brauner if (HAS_UNMAPPED_ID(mnt_userns, inode)) 5134bfd054aSEric Biggers return -EACCES; 5144bfd054aSEric Biggers } 5154bfd054aSEric Biggers 51647291baaSChristian Brauner retval = do_inode_permission(mnt_userns, inode, mask); 5174bfd054aSEric Biggers if (retval) 5184bfd054aSEric Biggers return retval; 5194bfd054aSEric Biggers 5204bfd054aSEric Biggers retval = devcgroup_inode_permission(inode, mask); 5214bfd054aSEric Biggers if (retval) 5224bfd054aSEric Biggers return retval; 5234bfd054aSEric Biggers 5244bfd054aSEric Biggers return security_inode_permission(inode, mask); 5250bdaea90SDavid Howells } 5264d359507SAl Viro EXPORT_SYMBOL(inode_permission); 5270bdaea90SDavid Howells 5280bdaea90SDavid Howells /** 5295dd784d0SJan Blunck * path_get - get a reference to a path 5305dd784d0SJan Blunck * @path: path to get the reference to 5315dd784d0SJan Blunck * 5325dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 5335dd784d0SJan Blunck */ 534dcf787f3SAl Viro void path_get(const struct path *path) 5355dd784d0SJan Blunck { 5365dd784d0SJan Blunck mntget(path->mnt); 5375dd784d0SJan Blunck dget(path->dentry); 5385dd784d0SJan Blunck } 5395dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 5405dd784d0SJan Blunck 5415dd784d0SJan Blunck /** 5421d957f9bSJan Blunck * path_put - put a reference to a path 5431d957f9bSJan Blunck * @path: path to put the reference to 5441d957f9bSJan Blunck * 5451d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 5461d957f9bSJan Blunck */ 547dcf787f3SAl Viro void path_put(const struct path *path) 5481da177e4SLinus Torvalds { 5491d957f9bSJan Blunck dput(path->dentry); 5501d957f9bSJan Blunck mntput(path->mnt); 5511da177e4SLinus Torvalds } 5521d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 5531da177e4SLinus Torvalds 554894bc8c4SAl Viro #define EMBEDDED_LEVELS 2 5551f55a6ecSAl Viro struct nameidata { 5561f55a6ecSAl Viro struct path path; 5571f55a6ecSAl Viro struct qstr last; 5581f55a6ecSAl Viro struct path root; 5591f55a6ecSAl Viro struct inode *inode; /* path.dentry.d_inode */ 560bcba1e7dSAl Viro unsigned int flags, state; 561ab87f9a5SAleksa Sarai unsigned seq, m_seq, r_seq; 5621f55a6ecSAl Viro int last_type; 5631f55a6ecSAl Viro unsigned depth; 564756daf26SNeilBrown int total_link_count; 565697fc6caSAl Viro struct saved { 566697fc6caSAl Viro struct path link; 567fceef393SAl Viro struct delayed_call done; 568697fc6caSAl Viro const char *name; 5690450b2d1SAl Viro unsigned seq; 570894bc8c4SAl Viro } *stack, internal[EMBEDDED_LEVELS]; 5719883d185SAl Viro struct filename *name; 5729883d185SAl Viro struct nameidata *saved; 5739883d185SAl Viro unsigned root_seq; 5749883d185SAl Viro int dfd; 5750f705953SAl Viro kuid_t dir_uid; 5760f705953SAl Viro umode_t dir_mode; 5773859a271SKees Cook } __randomize_layout; 5781f55a6ecSAl Viro 579bcba1e7dSAl Viro #define ND_ROOT_PRESET 1 580bcba1e7dSAl Viro #define ND_ROOT_GRABBED 2 581bcba1e7dSAl Viro #define ND_JUMPED 4 582bcba1e7dSAl Viro 58306422964SAl Viro static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) 584894bc8c4SAl Viro { 585756daf26SNeilBrown struct nameidata *old = current->nameidata; 586756daf26SNeilBrown p->stack = p->internal; 5877962c7d1SAl Viro p->depth = 0; 588c8a53ee5SAl Viro p->dfd = dfd; 589c8a53ee5SAl Viro p->name = name; 5907d01ef75SAl Viro p->path.mnt = NULL; 5917d01ef75SAl Viro p->path.dentry = NULL; 592756daf26SNeilBrown p->total_link_count = old ? old->total_link_count : 0; 5939883d185SAl Viro p->saved = old; 594756daf26SNeilBrown current->nameidata = p; 595894bc8c4SAl Viro } 596894bc8c4SAl Viro 59706422964SAl Viro static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name, 59806422964SAl Viro const struct path *root) 59906422964SAl Viro { 60006422964SAl Viro __set_nameidata(p, dfd, name); 60106422964SAl Viro p->state = 0; 60206422964SAl Viro if (unlikely(root)) { 60306422964SAl Viro p->state = ND_ROOT_PRESET; 60406422964SAl Viro p->root = *root; 60506422964SAl Viro } 60606422964SAl Viro } 60706422964SAl Viro 6089883d185SAl Viro static void restore_nameidata(void) 609894bc8c4SAl Viro { 6109883d185SAl Viro struct nameidata *now = current->nameidata, *old = now->saved; 611756daf26SNeilBrown 612756daf26SNeilBrown current->nameidata = old; 613756daf26SNeilBrown if (old) 614756daf26SNeilBrown old->total_link_count = now->total_link_count; 615e1a63bbcSAl Viro if (now->stack != now->internal) 616756daf26SNeilBrown kfree(now->stack); 617894bc8c4SAl Viro } 618894bc8c4SAl Viro 61960ef60c7SAl Viro static bool nd_alloc_stack(struct nameidata *nd) 620894bc8c4SAl Viro { 621bc40aee0SAl Viro struct saved *p; 622bc40aee0SAl Viro 6236da2ec56SKees Cook p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), 62460ef60c7SAl Viro nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); 625bc40aee0SAl Viro if (unlikely(!p)) 62660ef60c7SAl Viro return false; 627894bc8c4SAl Viro memcpy(p, nd->internal, sizeof(nd->internal)); 628894bc8c4SAl Viro nd->stack = p; 62960ef60c7SAl Viro return true; 630894bc8c4SAl Viro } 631894bc8c4SAl Viro 632397d425dSEric W. Biederman /** 6336b03f7edSAl Viro * path_connected - Verify that a dentry is below mnt.mnt_root 634397d425dSEric W. Biederman * 635397d425dSEric W. Biederman * Rename can sometimes move a file or directory outside of a bind 636397d425dSEric W. Biederman * mount, path_connected allows those cases to be detected. 637397d425dSEric W. Biederman */ 6386b03f7edSAl Viro static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) 639397d425dSEric W. Biederman { 64095dd7758SEric W. Biederman struct super_block *sb = mnt->mnt_sb; 641397d425dSEric W. Biederman 642402dd2cfSChristoph Hellwig /* Bind mounts can have disconnected paths */ 643402dd2cfSChristoph Hellwig if (mnt->mnt_root == sb->s_root) 644397d425dSEric W. Biederman return true; 645397d425dSEric W. Biederman 6466b03f7edSAl Viro return is_subdir(dentry, mnt->mnt_root); 647397d425dSEric W. Biederman } 648397d425dSEric W. Biederman 6497973387aSAl Viro static void drop_links(struct nameidata *nd) 6507973387aSAl Viro { 6517973387aSAl Viro int i = nd->depth; 6527973387aSAl Viro while (i--) { 6537973387aSAl Viro struct saved *last = nd->stack + i; 654fceef393SAl Viro do_delayed_call(&last->done); 655fceef393SAl Viro clear_delayed_call(&last->done); 6567973387aSAl Viro } 6577973387aSAl Viro } 6587973387aSAl Viro 6597973387aSAl Viro static void terminate_walk(struct nameidata *nd) 6607973387aSAl Viro { 6617973387aSAl Viro drop_links(nd); 6627973387aSAl Viro if (!(nd->flags & LOOKUP_RCU)) { 6637973387aSAl Viro int i; 6647973387aSAl Viro path_put(&nd->path); 6657973387aSAl Viro for (i = 0; i < nd->depth; i++) 6667973387aSAl Viro path_put(&nd->stack[i].link); 667bcba1e7dSAl Viro if (nd->state & ND_ROOT_GRABBED) { 668102b8af2SAl Viro path_put(&nd->root); 669bcba1e7dSAl Viro nd->state &= ~ND_ROOT_GRABBED; 670102b8af2SAl Viro } 6717973387aSAl Viro } else { 6727973387aSAl Viro nd->flags &= ~LOOKUP_RCU; 6737973387aSAl Viro rcu_read_unlock(); 6747973387aSAl Viro } 6757973387aSAl Viro nd->depth = 0; 6767d01ef75SAl Viro nd->path.mnt = NULL; 6777d01ef75SAl Viro nd->path.dentry = NULL; 6787973387aSAl Viro } 6797973387aSAl Viro 6807973387aSAl Viro /* path_put is needed afterwards regardless of success or failure */ 6812aa38470SAl Viro static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) 6827973387aSAl Viro { 6832aa38470SAl Viro int res = __legitimize_mnt(path->mnt, mseq); 6847973387aSAl Viro if (unlikely(res)) { 6857973387aSAl Viro if (res > 0) 6867973387aSAl Viro path->mnt = NULL; 6877973387aSAl Viro path->dentry = NULL; 6887973387aSAl Viro return false; 6897973387aSAl Viro } 6907973387aSAl Viro if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { 6917973387aSAl Viro path->dentry = NULL; 6927973387aSAl Viro return false; 6937973387aSAl Viro } 6947973387aSAl Viro return !read_seqcount_retry(&path->dentry->d_seq, seq); 6957973387aSAl Viro } 6967973387aSAl Viro 6972aa38470SAl Viro static inline bool legitimize_path(struct nameidata *nd, 6982aa38470SAl Viro struct path *path, unsigned seq) 6992aa38470SAl Viro { 7005bd73286SAl Viro return __legitimize_path(path, seq, nd->m_seq); 7012aa38470SAl Viro } 7022aa38470SAl Viro 7037973387aSAl Viro static bool legitimize_links(struct nameidata *nd) 7047973387aSAl Viro { 7057973387aSAl Viro int i; 706eacd9aa8SAl Viro if (unlikely(nd->flags & LOOKUP_CACHED)) { 707eacd9aa8SAl Viro drop_links(nd); 708eacd9aa8SAl Viro nd->depth = 0; 709eacd9aa8SAl Viro return false; 710eacd9aa8SAl Viro } 7117973387aSAl Viro for (i = 0; i < nd->depth; i++) { 7127973387aSAl Viro struct saved *last = nd->stack + i; 7137973387aSAl Viro if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { 7147973387aSAl Viro drop_links(nd); 7157973387aSAl Viro nd->depth = i + 1; 7167973387aSAl Viro return false; 7177973387aSAl Viro } 7187973387aSAl Viro } 7197973387aSAl Viro return true; 7207973387aSAl Viro } 7217973387aSAl Viro 722ee594bffSAl Viro static bool legitimize_root(struct nameidata *nd) 723ee594bffSAl Viro { 724adb21d2bSAleksa Sarai /* 725adb21d2bSAleksa Sarai * For scoped-lookups (where nd->root has been zeroed), we need to 726adb21d2bSAleksa Sarai * restart the whole lookup from scratch -- because set_root() is wrong 727adb21d2bSAleksa Sarai * for these lookups (nd->dfd is the root, not the filesystem root). 728adb21d2bSAleksa Sarai */ 729adb21d2bSAleksa Sarai if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) 730adb21d2bSAleksa Sarai return false; 731adb21d2bSAleksa Sarai /* Nothing to do if nd->root is zero or is managed by the VFS user. */ 732bcba1e7dSAl Viro if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) 733ee594bffSAl Viro return true; 734bcba1e7dSAl Viro nd->state |= ND_ROOT_GRABBED; 735ee594bffSAl Viro return legitimize_path(nd, &nd->root, nd->root_seq); 736ee594bffSAl Viro } 737ee594bffSAl Viro 73819660af7SAl Viro /* 73931e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 74019660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 74119660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 74257e3715cSMike Marshall * normal reference counts on dentries and vfsmounts to transition to ref-walk 74319660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 74419660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 74519660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 74619660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 74731e6b01fSNick Piggin */ 74831e6b01fSNick Piggin 74931e6b01fSNick Piggin /** 750e36cffedSJens Axboe * try_to_unlazy - try to switch to ref-walk mode. 75119660af7SAl Viro * @nd: nameidata pathwalk data 752e36cffedSJens Axboe * Returns: true on success, false on failure 75331e6b01fSNick Piggin * 754e36cffedSJens Axboe * try_to_unlazy attempts to legitimize the current nd->path and nd->root 7554675ac39SAl Viro * for ref-walk mode. 7564675ac39SAl Viro * Must be called from rcu-walk context. 757e36cffedSJens Axboe * Nothing should touch nameidata between try_to_unlazy() failure and 7587973387aSAl Viro * terminate_walk(). 75931e6b01fSNick Piggin */ 760e36cffedSJens Axboe static bool try_to_unlazy(struct nameidata *nd) 76131e6b01fSNick Piggin { 76231e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 76331e6b01fSNick Piggin 76431e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 765e5c832d5SLinus Torvalds 766e5c832d5SLinus Torvalds nd->flags &= ~LOOKUP_RCU; 7677973387aSAl Viro if (unlikely(!legitimize_links(nd))) 7684675ac39SAl Viro goto out1; 76984a2bd39SAl Viro if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) 77084a2bd39SAl Viro goto out; 771ee594bffSAl Viro if (unlikely(!legitimize_root(nd))) 7724675ac39SAl Viro goto out; 7734675ac39SAl Viro rcu_read_unlock(); 7744675ac39SAl Viro BUG_ON(nd->inode != parent->d_inode); 775e36cffedSJens Axboe return true; 7764675ac39SAl Viro 77784a2bd39SAl Viro out1: 7784675ac39SAl Viro nd->path.mnt = NULL; 7794675ac39SAl Viro nd->path.dentry = NULL; 7804675ac39SAl Viro out: 7814675ac39SAl Viro rcu_read_unlock(); 782e36cffedSJens Axboe return false; 7834675ac39SAl Viro } 7844675ac39SAl Viro 7854675ac39SAl Viro /** 786ae66db45SAl Viro * try_to_unlazy_next - try to switch to ref-walk mode. 7874675ac39SAl Viro * @nd: nameidata pathwalk data 788ae66db45SAl Viro * @dentry: next dentry to step into 789ae66db45SAl Viro * @seq: seq number to check @dentry against 790ae66db45SAl Viro * Returns: true on success, false on failure 7914675ac39SAl Viro * 792ae66db45SAl Viro * Similar to to try_to_unlazy(), but here we have the next dentry already 793ae66db45SAl Viro * picked by rcu-walk and want to legitimize that in addition to the current 794ae66db45SAl Viro * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. 795ae66db45SAl Viro * Nothing should touch nameidata between try_to_unlazy_next() failure and 7964675ac39SAl Viro * terminate_walk(). 7974675ac39SAl Viro */ 798ae66db45SAl Viro static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq) 7994675ac39SAl Viro { 8004675ac39SAl Viro BUG_ON(!(nd->flags & LOOKUP_RCU)); 8014675ac39SAl Viro 8024675ac39SAl Viro nd->flags &= ~LOOKUP_RCU; 8034675ac39SAl Viro if (unlikely(!legitimize_links(nd))) 8044675ac39SAl Viro goto out2; 8057973387aSAl Viro if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) 8067973387aSAl Viro goto out2; 8074675ac39SAl Viro if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) 8087973387aSAl Viro goto out1; 80948a066e7SAl Viro 81015570086SLinus Torvalds /* 8114675ac39SAl Viro * We need to move both the parent and the dentry from the RCU domain 8124675ac39SAl Viro * to be properly refcounted. And the sequence number in the dentry 8134675ac39SAl Viro * validates *both* dentry counters, since we checked the sequence 8144675ac39SAl Viro * number of the parent after we got the child sequence number. So we 8154675ac39SAl Viro * know the parent must still be valid if the child sequence number is 81615570086SLinus Torvalds */ 8174675ac39SAl Viro if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) 818e5c832d5SLinus Torvalds goto out; 81984a2bd39SAl Viro if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) 82084a2bd39SAl Viro goto out_dput; 821e5c832d5SLinus Torvalds /* 822e5c832d5SLinus Torvalds * Sequence counts matched. Now make sure that the root is 823e5c832d5SLinus Torvalds * still valid and get it if required. 824e5c832d5SLinus Torvalds */ 82584a2bd39SAl Viro if (unlikely(!legitimize_root(nd))) 82684a2bd39SAl Viro goto out_dput; 8278b61e74fSAl Viro rcu_read_unlock(); 828ae66db45SAl Viro return true; 82919660af7SAl Viro 8307973387aSAl Viro out2: 8317973387aSAl Viro nd->path.mnt = NULL; 8327973387aSAl Viro out1: 8337973387aSAl Viro nd->path.dentry = NULL; 834e5c832d5SLinus Torvalds out: 8358b61e74fSAl Viro rcu_read_unlock(); 836ae66db45SAl Viro return false; 83784a2bd39SAl Viro out_dput: 83884a2bd39SAl Viro rcu_read_unlock(); 83984a2bd39SAl Viro dput(dentry); 840ae66db45SAl Viro return false; 84131e6b01fSNick Piggin } 84231e6b01fSNick Piggin 8434ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 84434286d66SNick Piggin { 845a89f8337SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) 8464ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 847a89f8337SAl Viro else 848a89f8337SAl Viro return 1; 84934286d66SNick Piggin } 85034286d66SNick Piggin 8519f1fafeeSAl Viro /** 8529f1fafeeSAl Viro * complete_walk - successful completion of path walk 8539f1fafeeSAl Viro * @nd: pointer nameidata 85439159de2SJeff Layton * 8559f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 8569f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 8579f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 8589f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 8599f1fafeeSAl Viro * need to drop nd->path. 86039159de2SJeff Layton */ 8619f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 86239159de2SJeff Layton { 86316c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 86439159de2SJeff Layton int status; 86539159de2SJeff Layton 8669f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 867adb21d2bSAleksa Sarai /* 868adb21d2bSAleksa Sarai * We don't want to zero nd->root for scoped-lookups or 869adb21d2bSAleksa Sarai * externally-managed nd->root. 870adb21d2bSAleksa Sarai */ 871bcba1e7dSAl Viro if (!(nd->state & ND_ROOT_PRESET)) 872bcba1e7dSAl Viro if (!(nd->flags & LOOKUP_IS_SCOPED)) 8739f1fafeeSAl Viro nd->root.mnt = NULL; 8746c6ec2b0SJens Axboe nd->flags &= ~LOOKUP_CACHED; 875e36cffedSJens Axboe if (!try_to_unlazy(nd)) 87648a066e7SAl Viro return -ECHILD; 87748a066e7SAl Viro } 8789f1fafeeSAl Viro 879adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 880adb21d2bSAleksa Sarai /* 881adb21d2bSAleksa Sarai * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't 882adb21d2bSAleksa Sarai * ever step outside the root during lookup" and should already 883adb21d2bSAleksa Sarai * be guaranteed by the rest of namei, we want to avoid a namei 884adb21d2bSAleksa Sarai * BUG resulting in userspace being given a path that was not 885adb21d2bSAleksa Sarai * scoped within the root at some point during the lookup. 886adb21d2bSAleksa Sarai * 887adb21d2bSAleksa Sarai * So, do a final sanity-check to make sure that in the 888adb21d2bSAleksa Sarai * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) 889adb21d2bSAleksa Sarai * we won't silently return an fd completely outside of the 890adb21d2bSAleksa Sarai * requested root to userspace. 891adb21d2bSAleksa Sarai * 892adb21d2bSAleksa Sarai * Userspace could move the path outside the root after this 893adb21d2bSAleksa Sarai * check, but as discussed elsewhere this is not a concern (the 894adb21d2bSAleksa Sarai * resolved file was inside the root at some point). 895adb21d2bSAleksa Sarai */ 896adb21d2bSAleksa Sarai if (!path_is_under(&nd->path, &nd->root)) 897adb21d2bSAleksa Sarai return -EXDEV; 898adb21d2bSAleksa Sarai } 899adb21d2bSAleksa Sarai 900bcba1e7dSAl Viro if (likely(!(nd->state & ND_JUMPED))) 90139159de2SJeff Layton return 0; 90239159de2SJeff Layton 903ecf3d1f1SJeff Layton if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 90416c2cd71SAl Viro return 0; 90516c2cd71SAl Viro 906ecf3d1f1SJeff Layton status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 90739159de2SJeff Layton if (status > 0) 90839159de2SJeff Layton return 0; 90939159de2SJeff Layton 91016c2cd71SAl Viro if (!status) 91139159de2SJeff Layton status = -ESTALE; 91216c2cd71SAl Viro 91339159de2SJeff Layton return status; 91439159de2SJeff Layton } 91539159de2SJeff Layton 916740a1678SAleksa Sarai static int set_root(struct nameidata *nd) 9172a737871SAl Viro { 91831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 9199e6697e2SAl Viro 920adb21d2bSAleksa Sarai /* 921adb21d2bSAleksa Sarai * Jumping to the real root in a scoped-lookup is a BUG in namei, but we 922adb21d2bSAleksa Sarai * still have to ensure it doesn't happen because it will cause a breakout 923adb21d2bSAleksa Sarai * from the dirfd. 924adb21d2bSAleksa Sarai */ 925adb21d2bSAleksa Sarai if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) 926adb21d2bSAleksa Sarai return -ENOTRECOVERABLE; 927adb21d2bSAleksa Sarai 9289e6697e2SAl Viro if (nd->flags & LOOKUP_RCU) { 9298f47a016SAl Viro unsigned seq; 930c28cc364SNick Piggin 931c28cc364SNick Piggin do { 932c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 93331e6b01fSNick Piggin nd->root = fs->root; 9348f47a016SAl Viro nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 935c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 9369e6697e2SAl Viro } else { 9379e6697e2SAl Viro get_fs_root(fs, &nd->root); 938bcba1e7dSAl Viro nd->state |= ND_ROOT_GRABBED; 9399e6697e2SAl Viro } 940740a1678SAleksa Sarai return 0; 94131e6b01fSNick Piggin } 94231e6b01fSNick Piggin 943248fb5b9SAl Viro static int nd_jump_root(struct nameidata *nd) 944248fb5b9SAl Viro { 945adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_BENEATH)) 946adb21d2bSAleksa Sarai return -EXDEV; 94772ba2929SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 94872ba2929SAleksa Sarai /* Absolute path arguments to path_init() are allowed. */ 94972ba2929SAleksa Sarai if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) 95072ba2929SAleksa Sarai return -EXDEV; 95172ba2929SAleksa Sarai } 952740a1678SAleksa Sarai if (!nd->root.mnt) { 953740a1678SAleksa Sarai int error = set_root(nd); 954740a1678SAleksa Sarai if (error) 955740a1678SAleksa Sarai return error; 956740a1678SAleksa Sarai } 957248fb5b9SAl Viro if (nd->flags & LOOKUP_RCU) { 958248fb5b9SAl Viro struct dentry *d; 959248fb5b9SAl Viro nd->path = nd->root; 960248fb5b9SAl Viro d = nd->path.dentry; 961248fb5b9SAl Viro nd->inode = d->d_inode; 962248fb5b9SAl Viro nd->seq = nd->root_seq; 963248fb5b9SAl Viro if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) 964248fb5b9SAl Viro return -ECHILD; 965248fb5b9SAl Viro } else { 966248fb5b9SAl Viro path_put(&nd->path); 967248fb5b9SAl Viro nd->path = nd->root; 968248fb5b9SAl Viro path_get(&nd->path); 969248fb5b9SAl Viro nd->inode = nd->path.dentry->d_inode; 970248fb5b9SAl Viro } 971bcba1e7dSAl Viro nd->state |= ND_JUMPED; 972248fb5b9SAl Viro return 0; 973248fb5b9SAl Viro } 974248fb5b9SAl Viro 975b5fb63c1SChristoph Hellwig /* 9766b255391SAl Viro * Helper to directly jump to a known parsed path from ->get_link, 977b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 978b5fb63c1SChristoph Hellwig */ 9791bc82070SAleksa Sarai int nd_jump_link(struct path *path) 980b5fb63c1SChristoph Hellwig { 9814b99d499SAleksa Sarai int error = -ELOOP; 9826e77137bSAl Viro struct nameidata *nd = current->nameidata; 983b5fb63c1SChristoph Hellwig 9844b99d499SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) 9854b99d499SAleksa Sarai goto err; 9864b99d499SAleksa Sarai 98772ba2929SAleksa Sarai error = -EXDEV; 98872ba2929SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 98972ba2929SAleksa Sarai if (nd->path.mnt != path->mnt) 99072ba2929SAleksa Sarai goto err; 99172ba2929SAleksa Sarai } 992adb21d2bSAleksa Sarai /* Not currently safe for scoped-lookups. */ 993adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) 994adb21d2bSAleksa Sarai goto err; 99572ba2929SAleksa Sarai 9964b99d499SAleksa Sarai path_put(&nd->path); 997b5fb63c1SChristoph Hellwig nd->path = *path; 998b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 999bcba1e7dSAl Viro nd->state |= ND_JUMPED; 10001bc82070SAleksa Sarai return 0; 10014b99d499SAleksa Sarai 10024b99d499SAleksa Sarai err: 10034b99d499SAleksa Sarai path_put(path); 10044b99d499SAleksa Sarai return error; 1005b5fb63c1SChristoph Hellwig } 1006b5fb63c1SChristoph Hellwig 1007b9ff4429SAl Viro static inline void put_link(struct nameidata *nd) 1008574197e0SAl Viro { 100921c3003dSAl Viro struct saved *last = nd->stack + --nd->depth; 1010fceef393SAl Viro do_delayed_call(&last->done); 10116548fae2SAl Viro if (!(nd->flags & LOOKUP_RCU)) 1012b9ff4429SAl Viro path_put(&last->link); 1013574197e0SAl Viro } 1014574197e0SAl Viro 1015561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 1016561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 101730aba665SSalvatore Mesoraca int sysctl_protected_fifos __read_mostly; 101830aba665SSalvatore Mesoraca int sysctl_protected_regular __read_mostly; 1019800179c9SKees Cook 1020800179c9SKees Cook /** 1021800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 102255852635SRandy Dunlap * @nd: nameidata pathwalk data 1023800179c9SKees Cook * 1024800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 1025800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 1026800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 1027800179c9SKees Cook * processes from failing races against path names that may change out 1028800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 1029800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 1030800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 1031800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 1032800179c9SKees Cook * 1033800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 1034800179c9SKees Cook */ 1035ad6cc4c3SAl Viro static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) 1036800179c9SKees Cook { 1037ba73d987SChristian Brauner struct user_namespace *mnt_userns; 1038ba73d987SChristian Brauner kuid_t i_uid; 1039ba73d987SChristian Brauner 1040800179c9SKees Cook if (!sysctl_protected_symlinks) 1041800179c9SKees Cook return 0; 1042800179c9SKees Cook 1043ba73d987SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 1044ba73d987SChristian Brauner i_uid = i_uid_into_mnt(mnt_userns, inode); 1045800179c9SKees Cook /* Allowed if owner and follower match. */ 1046ba73d987SChristian Brauner if (uid_eq(current_cred()->fsuid, i_uid)) 1047800179c9SKees Cook return 0; 1048800179c9SKees Cook 1049800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 10500f705953SAl Viro if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 1051800179c9SKees Cook return 0; 1052800179c9SKees Cook 1053800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 1054ba73d987SChristian Brauner if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid)) 1055800179c9SKees Cook return 0; 1056800179c9SKees Cook 105731956502SAl Viro if (nd->flags & LOOKUP_RCU) 105831956502SAl Viro return -ECHILD; 105931956502SAl Viro 1060ea841bafSRichard Guy Briggs audit_inode(nd->name, nd->stack[0].link.dentry, 0); 1061245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); 1062800179c9SKees Cook return -EACCES; 1063800179c9SKees Cook } 1064800179c9SKees Cook 1065800179c9SKees Cook /** 1066800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 1067ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1068800179c9SKees Cook * @inode: the source inode to hardlink from 1069800179c9SKees Cook * 1070800179c9SKees Cook * Return false if at least one of the following conditions: 1071800179c9SKees Cook * - inode is not a regular file 1072800179c9SKees Cook * - inode is setuid 1073800179c9SKees Cook * - inode is setgid and group-exec 1074800179c9SKees Cook * - access failure for read and write 1075800179c9SKees Cook * 1076800179c9SKees Cook * Otherwise returns true. 1077800179c9SKees Cook */ 1078ba73d987SChristian Brauner static bool safe_hardlink_source(struct user_namespace *mnt_userns, 1079ba73d987SChristian Brauner struct inode *inode) 1080800179c9SKees Cook { 1081800179c9SKees Cook umode_t mode = inode->i_mode; 1082800179c9SKees Cook 1083800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 1084800179c9SKees Cook if (!S_ISREG(mode)) 1085800179c9SKees Cook return false; 1086800179c9SKees Cook 1087800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 1088800179c9SKees Cook if (mode & S_ISUID) 1089800179c9SKees Cook return false; 1090800179c9SKees Cook 1091800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 1092800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 1093800179c9SKees Cook return false; 1094800179c9SKees Cook 1095800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 1096ba73d987SChristian Brauner if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE)) 1097800179c9SKees Cook return false; 1098800179c9SKees Cook 1099800179c9SKees Cook return true; 1100800179c9SKees Cook } 1101800179c9SKees Cook 1102800179c9SKees Cook /** 1103800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 1104ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1105800179c9SKees Cook * @link: the source to hardlink from 1106800179c9SKees Cook * 1107800179c9SKees Cook * Block hardlink when all of: 1108800179c9SKees Cook * - sysctl_protected_hardlinks enabled 1109800179c9SKees Cook * - fsuid does not match inode 1110800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 1111f2ca3796SDirk Steinmetz * - not CAP_FOWNER in a namespace with the inode owner uid mapped 1112800179c9SKees Cook * 1113ba73d987SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 1114ba73d987SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 1115ba73d987SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 1116ba73d987SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 1117ba73d987SChristian Brauner * raw inode simply passs init_user_ns. 1118ba73d987SChristian Brauner * 1119800179c9SKees Cook * Returns 0 if successful, -ve on error. 1120800179c9SKees Cook */ 1121ba73d987SChristian Brauner int may_linkat(struct user_namespace *mnt_userns, struct path *link) 1122800179c9SKees Cook { 1123593d1ce8SEric W. Biederman struct inode *inode = link->dentry->d_inode; 1124593d1ce8SEric W. Biederman 1125593d1ce8SEric W. Biederman /* Inode writeback is not safe when the uid or gid are invalid. */ 1126ba73d987SChristian Brauner if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || 1127ba73d987SChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 1128593d1ce8SEric W. Biederman return -EOVERFLOW; 1129800179c9SKees Cook 1130800179c9SKees Cook if (!sysctl_protected_hardlinks) 1131800179c9SKees Cook return 0; 1132800179c9SKees Cook 1133800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 1134800179c9SKees Cook * otherwise, it must be a safe source. 1135800179c9SKees Cook */ 1136ba73d987SChristian Brauner if (safe_hardlink_source(mnt_userns, inode) || 1137ba73d987SChristian Brauner inode_owner_or_capable(mnt_userns, inode)) 1138800179c9SKees Cook return 0; 1139800179c9SKees Cook 1140245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); 1141800179c9SKees Cook return -EPERM; 1142800179c9SKees Cook } 1143800179c9SKees Cook 114430aba665SSalvatore Mesoraca /** 114530aba665SSalvatore Mesoraca * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory 114630aba665SSalvatore Mesoraca * should be allowed, or not, on files that already 114730aba665SSalvatore Mesoraca * exist. 1148ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 11492111c3c0SRandy Dunlap * @nd: nameidata pathwalk data 115030aba665SSalvatore Mesoraca * @inode: the inode of the file to open 115130aba665SSalvatore Mesoraca * 115230aba665SSalvatore Mesoraca * Block an O_CREAT open of a FIFO (or a regular file) when: 115330aba665SSalvatore Mesoraca * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled 115430aba665SSalvatore Mesoraca * - the file already exists 115530aba665SSalvatore Mesoraca * - we are in a sticky directory 115630aba665SSalvatore Mesoraca * - we don't own the file 115730aba665SSalvatore Mesoraca * - the owner of the directory doesn't own the file 115830aba665SSalvatore Mesoraca * - the directory is world writable 115930aba665SSalvatore Mesoraca * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 116030aba665SSalvatore Mesoraca * the directory doesn't have to be world writable: being group writable will 116130aba665SSalvatore Mesoraca * be enough. 116230aba665SSalvatore Mesoraca * 1163ba73d987SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 1164ba73d987SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 1165ba73d987SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 1166ba73d987SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 1167ba73d987SChristian Brauner * raw inode simply passs init_user_ns. 1168ba73d987SChristian Brauner * 116930aba665SSalvatore Mesoraca * Returns 0 if the open is allowed, -ve on error. 117030aba665SSalvatore Mesoraca */ 1171ba73d987SChristian Brauner static int may_create_in_sticky(struct user_namespace *mnt_userns, 1172ba73d987SChristian Brauner struct nameidata *nd, struct inode *const inode) 117330aba665SSalvatore Mesoraca { 1174ba73d987SChristian Brauner umode_t dir_mode = nd->dir_mode; 1175ba73d987SChristian Brauner kuid_t dir_uid = nd->dir_uid; 1176ba73d987SChristian Brauner 117730aba665SSalvatore Mesoraca if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || 117830aba665SSalvatore Mesoraca (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || 1179d0cb5018SAl Viro likely(!(dir_mode & S_ISVTX)) || 1180ba73d987SChristian Brauner uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) || 1181ba73d987SChristian Brauner uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) 118230aba665SSalvatore Mesoraca return 0; 118330aba665SSalvatore Mesoraca 1184d0cb5018SAl Viro if (likely(dir_mode & 0002) || 1185d0cb5018SAl Viro (dir_mode & 0020 && 118630aba665SSalvatore Mesoraca ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || 118730aba665SSalvatore Mesoraca (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { 1188245d7369SKees Cook const char *operation = S_ISFIFO(inode->i_mode) ? 1189245d7369SKees Cook "sticky_create_fifo" : 1190245d7369SKees Cook "sticky_create_regular"; 1191245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_CREAT, operation); 119230aba665SSalvatore Mesoraca return -EACCES; 119330aba665SSalvatore Mesoraca } 119430aba665SSalvatore Mesoraca return 0; 119530aba665SSalvatore Mesoraca } 119630aba665SSalvatore Mesoraca 1197f015f126SDavid Howells /* 1198f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 1199f015f126SDavid Howells * 1200f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 1201f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 1202f015f126SDavid Howells * Up is towards /. 1203f015f126SDavid Howells * 1204f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 1205f015f126SDavid Howells * root. 1206f015f126SDavid Howells */ 1207bab77ebfSAl Viro int follow_up(struct path *path) 12081da177e4SLinus Torvalds { 12090714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 12100714a533SAl Viro struct mount *parent; 12111da177e4SLinus Torvalds struct dentry *mountpoint; 121299b7db7bSNick Piggin 121348a066e7SAl Viro read_seqlock_excl(&mount_lock); 12140714a533SAl Viro parent = mnt->mnt_parent; 12153c0a6163SAl Viro if (parent == mnt) { 121648a066e7SAl Viro read_sequnlock_excl(&mount_lock); 12171da177e4SLinus Torvalds return 0; 12181da177e4SLinus Torvalds } 12190714a533SAl Viro mntget(&parent->mnt); 1220a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 122148a066e7SAl Viro read_sequnlock_excl(&mount_lock); 1222bab77ebfSAl Viro dput(path->dentry); 1223bab77ebfSAl Viro path->dentry = mountpoint; 1224bab77ebfSAl Viro mntput(path->mnt); 12250714a533SAl Viro path->mnt = &parent->mnt; 12261da177e4SLinus Torvalds return 1; 12271da177e4SLinus Torvalds } 12284d359507SAl Viro EXPORT_SYMBOL(follow_up); 12291da177e4SLinus Torvalds 12307ef482faSAl Viro static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, 12317ef482faSAl Viro struct path *path, unsigned *seqp) 12327ef482faSAl Viro { 12337ef482faSAl Viro while (mnt_has_parent(m)) { 12347ef482faSAl Viro struct dentry *mountpoint = m->mnt_mountpoint; 12357ef482faSAl Viro 12367ef482faSAl Viro m = m->mnt_parent; 12377ef482faSAl Viro if (unlikely(root->dentry == mountpoint && 12387ef482faSAl Viro root->mnt == &m->mnt)) 12397ef482faSAl Viro break; 12407ef482faSAl Viro if (mountpoint != m->mnt.mnt_root) { 12417ef482faSAl Viro path->mnt = &m->mnt; 12427ef482faSAl Viro path->dentry = mountpoint; 12437ef482faSAl Viro *seqp = read_seqcount_begin(&mountpoint->d_seq); 12447ef482faSAl Viro return true; 12457ef482faSAl Viro } 12467ef482faSAl Viro } 12477ef482faSAl Viro return false; 12487ef482faSAl Viro } 12497ef482faSAl Viro 12502aa38470SAl Viro static bool choose_mountpoint(struct mount *m, const struct path *root, 12512aa38470SAl Viro struct path *path) 12522aa38470SAl Viro { 12532aa38470SAl Viro bool found; 12542aa38470SAl Viro 12552aa38470SAl Viro rcu_read_lock(); 12562aa38470SAl Viro while (1) { 12572aa38470SAl Viro unsigned seq, mseq = read_seqbegin(&mount_lock); 12582aa38470SAl Viro 12592aa38470SAl Viro found = choose_mountpoint_rcu(m, root, path, &seq); 12602aa38470SAl Viro if (unlikely(!found)) { 12612aa38470SAl Viro if (!read_seqretry(&mount_lock, mseq)) 12622aa38470SAl Viro break; 12632aa38470SAl Viro } else { 12642aa38470SAl Viro if (likely(__legitimize_path(path, seq, mseq))) 12652aa38470SAl Viro break; 12662aa38470SAl Viro rcu_read_unlock(); 12672aa38470SAl Viro path_put(path); 12682aa38470SAl Viro rcu_read_lock(); 12692aa38470SAl Viro } 12702aa38470SAl Viro } 12712aa38470SAl Viro rcu_read_unlock(); 12722aa38470SAl Viro return found; 12732aa38470SAl Viro } 12742aa38470SAl Viro 1275b5c84bf6SNick Piggin /* 12769875cf80SDavid Howells * Perform an automount 12779875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 12789875cf80SDavid Howells * were called with. 12791da177e4SLinus Torvalds */ 12801c9f5e06SAl Viro static int follow_automount(struct path *path, int *count, unsigned lookup_flags) 128131e6b01fSNick Piggin { 128225e195aaSAl Viro struct dentry *dentry = path->dentry; 12839875cf80SDavid Howells 12840ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 12850ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 12860ec26fd0SMiklos Szeredi * the name. 12870ec26fd0SMiklos Szeredi * 12880ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 12895a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 12900ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 12910ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 12920ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 12930ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 12945a30d8a2SDavid Howells */ 12951c9f5e06SAl Viro if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 12965d38f049SIan Kent LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 129725e195aaSAl Viro dentry->d_inode) 12989875cf80SDavid Howells return -EISDIR; 12990ec26fd0SMiklos Szeredi 13001c9f5e06SAl Viro if (count && (*count)++ >= MAXSYMLINKS) 13019875cf80SDavid Howells return -ELOOP; 13029875cf80SDavid Howells 130325e195aaSAl Viro return finish_automount(dentry->d_op->d_automount(path), path); 1304ea5b778aSDavid Howells } 13059875cf80SDavid Howells 13069875cf80SDavid Howells /* 13079deed3ebSAl Viro * mount traversal - out-of-line part. One note on ->d_flags accesses - 13089deed3ebSAl Viro * dentries are pinned but not locked here, so negative dentry can go 13099deed3ebSAl Viro * positive right under us. Use of smp_load_acquire() provides a barrier 13109deed3ebSAl Viro * sufficient for ->d_inode and ->d_flags consistency. 13119875cf80SDavid Howells */ 13129deed3ebSAl Viro static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, 13139deed3ebSAl Viro int *count, unsigned lookup_flags) 13149875cf80SDavid Howells { 13159deed3ebSAl Viro struct vfsmount *mnt = path->mnt; 13169875cf80SDavid Howells bool need_mntput = false; 13178aef1884SAl Viro int ret = 0; 13189875cf80SDavid Howells 13199deed3ebSAl Viro while (flags & DCACHE_MANAGED_DENTRY) { 1320cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1321cc53ce53SDavid Howells * being held. */ 1322d41efb52SAl Viro if (flags & DCACHE_MANAGE_TRANSIT) { 1323fb5f51c7SIan Kent ret = path->dentry->d_op->d_manage(path, false); 1324508c8772SAl Viro flags = smp_load_acquire(&path->dentry->d_flags); 1325cc53ce53SDavid Howells if (ret < 0) 13268aef1884SAl Viro break; 1327cc53ce53SDavid Howells } 1328cc53ce53SDavid Howells 13299deed3ebSAl Viro if (flags & DCACHE_MOUNTED) { // something's mounted on it.. 13309875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 13319deed3ebSAl Viro if (mounted) { // ... in our namespace 13329875cf80SDavid Howells dput(path->dentry); 13339875cf80SDavid Howells if (need_mntput) 1334463ffb2eSAl Viro mntput(path->mnt); 1335463ffb2eSAl Viro path->mnt = mounted; 1336463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 13379deed3ebSAl Viro // here we know it's positive 13389deed3ebSAl Viro flags = path->dentry->d_flags; 13399875cf80SDavid Howells need_mntput = true; 13409875cf80SDavid Howells continue; 1341463ffb2eSAl Viro } 13421da177e4SLinus Torvalds } 13439875cf80SDavid Howells 13449deed3ebSAl Viro if (!(flags & DCACHE_NEED_AUTOMOUNT)) 13459deed3ebSAl Viro break; 13469deed3ebSAl Viro 13479deed3ebSAl Viro // uncovered automount point 13489deed3ebSAl Viro ret = follow_automount(path, count, lookup_flags); 13499deed3ebSAl Viro flags = smp_load_acquire(&path->dentry->d_flags); 13509875cf80SDavid Howells if (ret < 0) 13518aef1884SAl Viro break; 13529875cf80SDavid Howells } 13539875cf80SDavid Howells 13549deed3ebSAl Viro if (ret == -EISDIR) 13559deed3ebSAl Viro ret = 0; 13569deed3ebSAl Viro // possible if you race with several mount --move 13579deed3ebSAl Viro if (need_mntput && path->mnt == mnt) 13588aef1884SAl Viro mntput(path->mnt); 13599deed3ebSAl Viro if (!ret && unlikely(d_flags_negative(flags))) 1360d41efb52SAl Viro ret = -ENOENT; 13619deed3ebSAl Viro *jumped = need_mntput; 13628402752eSAl Viro return ret; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13659deed3ebSAl Viro static inline int traverse_mounts(struct path *path, bool *jumped, 13669deed3ebSAl Viro int *count, unsigned lookup_flags) 13679deed3ebSAl Viro { 13689deed3ebSAl Viro unsigned flags = smp_load_acquire(&path->dentry->d_flags); 13699deed3ebSAl Viro 13709deed3ebSAl Viro /* fastpath */ 13719deed3ebSAl Viro if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { 13729deed3ebSAl Viro *jumped = false; 13739deed3ebSAl Viro if (unlikely(d_flags_negative(flags))) 13749deed3ebSAl Viro return -ENOENT; 13759deed3ebSAl Viro return 0; 13769deed3ebSAl Viro } 13779deed3ebSAl Viro return __traverse_mounts(path, flags, jumped, count, lookup_flags); 13789deed3ebSAl Viro } 13799deed3ebSAl Viro 1380cc53ce53SDavid Howells int follow_down_one(struct path *path) 13811da177e4SLinus Torvalds { 13821da177e4SLinus Torvalds struct vfsmount *mounted; 13831da177e4SLinus Torvalds 13841c755af4SAl Viro mounted = lookup_mnt(path); 13851da177e4SLinus Torvalds if (mounted) { 13869393bd07SAl Viro dput(path->dentry); 13879393bd07SAl Viro mntput(path->mnt); 13889393bd07SAl Viro path->mnt = mounted; 13899393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 13901da177e4SLinus Torvalds return 1; 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds return 0; 13931da177e4SLinus Torvalds } 13944d359507SAl Viro EXPORT_SYMBOL(follow_down_one); 13951da177e4SLinus Torvalds 13969875cf80SDavid Howells /* 13979deed3ebSAl Viro * Follow down to the covering mount currently visible to userspace. At each 13989deed3ebSAl Viro * point, the filesystem owning that dentry may be queried as to whether the 13999deed3ebSAl Viro * caller is permitted to proceed or not. 14009deed3ebSAl Viro */ 14019deed3ebSAl Viro int follow_down(struct path *path) 14029deed3ebSAl Viro { 14039deed3ebSAl Viro struct vfsmount *mnt = path->mnt; 14049deed3ebSAl Viro bool jumped; 14059deed3ebSAl Viro int ret = traverse_mounts(path, &jumped, NULL, 0); 14069deed3ebSAl Viro 14079deed3ebSAl Viro if (path->mnt != mnt) 14089deed3ebSAl Viro mntput(mnt); 14099deed3ebSAl Viro return ret; 14109deed3ebSAl Viro } 14119deed3ebSAl Viro EXPORT_SYMBOL(follow_down); 14129deed3ebSAl Viro 14139deed3ebSAl Viro /* 1414287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1415287548e4SAl Viro * we meet a managed dentry that would need blocking. 14169875cf80SDavid Howells */ 14179875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1418254cf582SAl Viro struct inode **inode, unsigned *seqp) 14199875cf80SDavid Howells { 1420ea936aebSAl Viro struct dentry *dentry = path->dentry; 1421ea936aebSAl Viro unsigned int flags = dentry->d_flags; 1422ea936aebSAl Viro 1423ea936aebSAl Viro if (likely(!(flags & DCACHE_MANAGED_DENTRY))) 1424ea936aebSAl Viro return true; 1425ea936aebSAl Viro 1426ea936aebSAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1427ea936aebSAl Viro return false; 1428ea936aebSAl Viro 142962a7375eSIan Kent for (;;) { 143062a7375eSIan Kent /* 143162a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 143262a7375eSIan Kent * that wants to block transit. 143362a7375eSIan Kent */ 1434ea936aebSAl Viro if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { 1435ea936aebSAl Viro int res = dentry->d_op->d_manage(path, true); 1436ea936aebSAl Viro if (res) 1437ea936aebSAl Viro return res == -EISDIR; 1438ea936aebSAl Viro flags = dentry->d_flags; 1439b8faf035SNeilBrown } 144062a7375eSIan Kent 1441ea936aebSAl Viro if (flags & DCACHE_MOUNTED) { 1442ea936aebSAl Viro struct mount *mounted = __lookup_mnt(path->mnt, dentry); 1443ea936aebSAl Viro if (mounted) { 1444c7105365SAl Viro path->mnt = &mounted->mnt; 1445ea936aebSAl Viro dentry = path->dentry = mounted->mnt.mnt_root; 1446bcba1e7dSAl Viro nd->state |= ND_JUMPED; 1447ea936aebSAl Viro *seqp = read_seqcount_begin(&dentry->d_seq); 1448ea936aebSAl Viro *inode = dentry->d_inode; 144959430262SLinus Torvalds /* 1450ea936aebSAl Viro * We don't need to re-check ->d_seq after this 1451ea936aebSAl Viro * ->d_inode read - there will be an RCU delay 1452ea936aebSAl Viro * between mount hash removal and ->mnt_root 1453ea936aebSAl Viro * becoming unpinned. 145459430262SLinus Torvalds */ 1455ea936aebSAl Viro flags = dentry->d_flags; 1456ea936aebSAl Viro continue; 14579875cf80SDavid Howells } 1458ea936aebSAl Viro if (read_seqretry(&mount_lock, nd->m_seq)) 1459ea936aebSAl Viro return false; 1460ea936aebSAl Viro } 1461ea936aebSAl Viro return !(flags & DCACHE_NEED_AUTOMOUNT); 1462ea936aebSAl Viro } 1463287548e4SAl Viro } 1464287548e4SAl Viro 1465db3c9adeSAl Viro static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, 1466db3c9adeSAl Viro struct path *path, struct inode **inode, 1467db3c9adeSAl Viro unsigned int *seqp) 1468bd7c4b50SAl Viro { 14699deed3ebSAl Viro bool jumped; 1470db3c9adeSAl Viro int ret; 1471bd7c4b50SAl Viro 1472db3c9adeSAl Viro path->mnt = nd->path.mnt; 1473db3c9adeSAl Viro path->dentry = dentry; 1474c153007bSAl Viro if (nd->flags & LOOKUP_RCU) { 1475c153007bSAl Viro unsigned int seq = *seqp; 1476c153007bSAl Viro if (unlikely(!*inode)) 1477c153007bSAl Viro return -ENOENT; 1478c153007bSAl Viro if (likely(__follow_mount_rcu(nd, path, inode, seqp))) 14799deed3ebSAl Viro return 0; 1480ae66db45SAl Viro if (!try_to_unlazy_next(nd, dentry, seq)) 1481c153007bSAl Viro return -ECHILD; 1482c153007bSAl Viro // *path might've been clobbered by __follow_mount_rcu() 1483c153007bSAl Viro path->mnt = nd->path.mnt; 1484c153007bSAl Viro path->dentry = dentry; 1485c153007bSAl Viro } 14869deed3ebSAl Viro ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); 14879deed3ebSAl Viro if (jumped) { 14889deed3ebSAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 14899deed3ebSAl Viro ret = -EXDEV; 14909deed3ebSAl Viro else 1491bcba1e7dSAl Viro nd->state |= ND_JUMPED; 14929deed3ebSAl Viro } 14939deed3ebSAl Viro if (unlikely(ret)) { 14949deed3ebSAl Viro dput(path->dentry); 14959deed3ebSAl Viro if (path->mnt != nd->path.mnt) 14969deed3ebSAl Viro mntput(path->mnt); 14979deed3ebSAl Viro } else { 1498bd7c4b50SAl Viro *inode = d_backing_inode(path->dentry); 1499bd7c4b50SAl Viro *seqp = 0; /* out of RCU mode, so the value doesn't matter */ 1500bd7c4b50SAl Viro } 1501bd7c4b50SAl Viro return ret; 1502bd7c4b50SAl Viro } 1503bd7c4b50SAl Viro 15049875cf80SDavid Howells /* 1505f4fdace9SOleg Drokin * This looks up the name in dcache and possibly revalidates the found dentry. 1506f4fdace9SOleg Drokin * NULL is returned if the dentry does not exist in the cache. 1507baa03890SNick Piggin */ 1508e3c13928SAl Viro static struct dentry *lookup_dcache(const struct qstr *name, 1509e3c13928SAl Viro struct dentry *dir, 15106c51e513SAl Viro unsigned int flags) 1511baa03890SNick Piggin { 1512a89f8337SAl Viro struct dentry *dentry = d_lookup(dir, name); 1513bad61189SMiklos Szeredi if (dentry) { 1514a89f8337SAl Viro int error = d_revalidate(dentry, flags); 1515bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 151674ff0ffcSAl Viro if (!error) 15175542aa2fSEric W. Biederman d_invalidate(dentry); 1518bad61189SMiklos Szeredi dput(dentry); 151974ff0ffcSAl Viro return ERR_PTR(error); 1520bad61189SMiklos Szeredi } 1521bad61189SMiklos Szeredi } 1522baa03890SNick Piggin return dentry; 1523baa03890SNick Piggin } 1524baa03890SNick Piggin 1525baa03890SNick Piggin /* 1526a03ece5fSAl Viro * Parent directory has inode locked exclusive. This is one 1527a03ece5fSAl Viro * and only case when ->lookup() gets called on non in-lookup 1528a03ece5fSAl Viro * dentries - as the matter of fact, this only gets called 1529a03ece5fSAl Viro * when directory is guaranteed to have no in-lookup children 1530a03ece5fSAl Viro * at all. 153144396f4bSJosef Bacik */ 1532a03ece5fSAl Viro static struct dentry *__lookup_hash(const struct qstr *name, 1533a03ece5fSAl Viro struct dentry *base, unsigned int flags) 153444396f4bSJosef Bacik { 1535a03ece5fSAl Viro struct dentry *dentry = lookup_dcache(name, base, flags); 153644396f4bSJosef Bacik struct dentry *old; 1537a03ece5fSAl Viro struct inode *dir = base->d_inode; 1538a03ece5fSAl Viro 1539a03ece5fSAl Viro if (dentry) 1540a03ece5fSAl Viro return dentry; 154144396f4bSJosef Bacik 154244396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1543a03ece5fSAl Viro if (unlikely(IS_DEADDIR(dir))) 154444396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1545a03ece5fSAl Viro 1546a03ece5fSAl Viro dentry = d_alloc(base, name); 1547a03ece5fSAl Viro if (unlikely(!dentry)) 1548a03ece5fSAl Viro return ERR_PTR(-ENOMEM); 154944396f4bSJosef Bacik 155072bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 155144396f4bSJosef Bacik if (unlikely(old)) { 155244396f4bSJosef Bacik dput(dentry); 155344396f4bSJosef Bacik dentry = old; 155444396f4bSJosef Bacik } 155544396f4bSJosef Bacik return dentry; 155644396f4bSJosef Bacik } 155744396f4bSJosef Bacik 155820e34357SAl Viro static struct dentry *lookup_fast(struct nameidata *nd, 155920e34357SAl Viro struct inode **inode, 1560254cf582SAl Viro unsigned *seqp) 15611da177e4SLinus Torvalds { 156231e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 15635a18fff2SAl Viro int status = 1; 15649875cf80SDavid Howells 15653cac260aSAl Viro /* 1566b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 15675d0f49c1SAl Viro * of a false negative due to a concurrent rename, the caller is 15685d0f49c1SAl Viro * going to fall back to non-racy lookup. 1569b04f784eSNick Piggin */ 157031e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 157131e6b01fSNick Piggin unsigned seq; 1572da53be12SLinus Torvalds dentry = __d_lookup_rcu(parent, &nd->last, &seq); 15735d0f49c1SAl Viro if (unlikely(!dentry)) { 1574e36cffedSJens Axboe if (!try_to_unlazy(nd)) 157520e34357SAl Viro return ERR_PTR(-ECHILD); 157620e34357SAl Viro return NULL; 15775d0f49c1SAl Viro } 15785a18fff2SAl Viro 157912f8ad4bSLinus Torvalds /* 158012f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 158112f8ad4bSLinus Torvalds * the dentry name information from lookup. 158212f8ad4bSLinus Torvalds */ 158363afdfc7SDavid Howells *inode = d_backing_inode(dentry); 15845d0f49c1SAl Viro if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) 158520e34357SAl Viro return ERR_PTR(-ECHILD); 158612f8ad4bSLinus Torvalds 158712f8ad4bSLinus Torvalds /* 158812f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 158912f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 159012f8ad4bSLinus Torvalds * 159112f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 159212f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 159312f8ad4bSLinus Torvalds */ 15945d0f49c1SAl Viro if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) 159520e34357SAl Viro return ERR_PTR(-ECHILD); 15965a18fff2SAl Viro 1597254cf582SAl Viro *seqp = seq; 15984ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 1599c153007bSAl Viro if (likely(status > 0)) 160020e34357SAl Viro return dentry; 1601ae66db45SAl Viro if (!try_to_unlazy_next(nd, dentry, seq)) 160220e34357SAl Viro return ERR_PTR(-ECHILD); 160326ddb45eSSteven Rostedt (VMware) if (status == -ECHILD) 1604209a7fb2SAl Viro /* we'd been told to redo it in non-rcu mode */ 1605209a7fb2SAl Viro status = d_revalidate(dentry, nd->flags); 16065a18fff2SAl Viro } else { 1607e97cdc87SAl Viro dentry = __d_lookup(parent, &nd->last); 160881e6f520SAl Viro if (unlikely(!dentry)) 160920e34357SAl Viro return NULL; 16104ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 16115d0f49c1SAl Viro } 16125a18fff2SAl Viro if (unlikely(status <= 0)) { 1613e9742b53SAl Viro if (!status) 16145d0f49c1SAl Viro d_invalidate(dentry); 16155a18fff2SAl Viro dput(dentry); 161620e34357SAl Viro return ERR_PTR(status); 16175a18fff2SAl Viro } 161820e34357SAl Viro return dentry; 1619697f514dSMiklos Szeredi } 1620697f514dSMiklos Szeredi 1621697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 162288d8331aSAl Viro static struct dentry *__lookup_slow(const struct qstr *name, 1623e3c13928SAl Viro struct dentry *dir, 1624e3c13928SAl Viro unsigned int flags) 1625697f514dSMiklos Szeredi { 162688d8331aSAl Viro struct dentry *dentry, *old; 16271936386eSAl Viro struct inode *inode = dir->d_inode; 1628d9171b93SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 16291936386eSAl Viro 16301936386eSAl Viro /* Don't go there if it's already dead */ 163194bdd655SAl Viro if (unlikely(IS_DEADDIR(inode))) 163288d8331aSAl Viro return ERR_PTR(-ENOENT); 163394bdd655SAl Viro again: 1634d9171b93SAl Viro dentry = d_alloc_parallel(dir, name, &wq); 163594bdd655SAl Viro if (IS_ERR(dentry)) 163688d8331aSAl Viro return dentry; 163794bdd655SAl Viro if (unlikely(!d_in_lookup(dentry))) { 1638949a852eSAl Viro int error = d_revalidate(dentry, flags); 1639949a852eSAl Viro if (unlikely(error <= 0)) { 164094bdd655SAl Viro if (!error) { 1641949a852eSAl Viro d_invalidate(dentry); 1642949a852eSAl Viro dput(dentry); 164394bdd655SAl Viro goto again; 164494bdd655SAl Viro } 164594bdd655SAl Viro dput(dentry); 1646949a852eSAl Viro dentry = ERR_PTR(error); 1647949a852eSAl Viro } 164894bdd655SAl Viro } else { 16491936386eSAl Viro old = inode->i_op->lookup(inode, dentry, flags); 165085c7f810SAl Viro d_lookup_done(dentry); 16511936386eSAl Viro if (unlikely(old)) { 16521936386eSAl Viro dput(dentry); 16531936386eSAl Viro dentry = old; 1654949a852eSAl Viro } 1655949a852eSAl Viro } 1656e3c13928SAl Viro return dentry; 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 165988d8331aSAl Viro static struct dentry *lookup_slow(const struct qstr *name, 166088d8331aSAl Viro struct dentry *dir, 166188d8331aSAl Viro unsigned int flags) 166288d8331aSAl Viro { 166388d8331aSAl Viro struct inode *inode = dir->d_inode; 166488d8331aSAl Viro struct dentry *res; 166588d8331aSAl Viro inode_lock_shared(inode); 166688d8331aSAl Viro res = __lookup_slow(name, dir, flags); 166788d8331aSAl Viro inode_unlock_shared(inode); 166888d8331aSAl Viro return res; 166988d8331aSAl Viro } 167088d8331aSAl Viro 1671ba73d987SChristian Brauner static inline int may_lookup(struct user_namespace *mnt_userns, 1672ba73d987SChristian Brauner struct nameidata *nd) 167352094c8aSAl Viro { 167452094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 16757d6beb71SLinus Torvalds int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 1676e36cffedSJens Axboe if (err != -ECHILD || !try_to_unlazy(nd)) 167752094c8aSAl Viro return err; 167852094c8aSAl Viro } 1679ba73d987SChristian Brauner return inode_permission(mnt_userns, nd->inode, MAY_EXEC); 168052094c8aSAl Viro } 168152094c8aSAl Viro 168249055906SAl Viro static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) 1683d63ff28fSAl Viro { 168449055906SAl Viro if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) 168549055906SAl Viro return -ELOOP; 16864542576bSAl Viro 16874542576bSAl Viro if (likely(nd->depth != EMBEDDED_LEVELS)) 16884542576bSAl Viro return 0; 16894542576bSAl Viro if (likely(nd->stack != nd->internal)) 16904542576bSAl Viro return 0; 169160ef60c7SAl Viro if (likely(nd_alloc_stack(nd))) 169249055906SAl Viro return 0; 169360ef60c7SAl Viro 169460ef60c7SAl Viro if (nd->flags & LOOKUP_RCU) { 169560ef60c7SAl Viro // we need to grab link before we do unlazy. And we can't skip 169660ef60c7SAl Viro // unlazy even if we fail to grab the link - cleanup needs it 1697aef9404dSAl Viro bool grabbed_link = legitimize_path(nd, link, seq); 169860ef60c7SAl Viro 1699e36cffedSJens Axboe if (!try_to_unlazy(nd) != 0 || !grabbed_link) 170060ef60c7SAl Viro return -ECHILD; 170160ef60c7SAl Viro 170260ef60c7SAl Viro if (nd_alloc_stack(nd)) 170360ef60c7SAl Viro return 0; 1704bc40aee0SAl Viro } 170560ef60c7SAl Viro return -ENOMEM; 170649055906SAl Viro } 170749055906SAl Viro 170849055906SAl Viro enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; 170949055906SAl Viro 171049055906SAl Viro static const char *pick_link(struct nameidata *nd, struct path *link, 171149055906SAl Viro struct inode *inode, unsigned seq, int flags) 171249055906SAl Viro { 171349055906SAl Viro struct saved *last; 171449055906SAl Viro const char *res; 171549055906SAl Viro int error = reserve_stack(nd, link, seq); 171649055906SAl Viro 171749055906SAl Viro if (unlikely(error)) { 171849055906SAl Viro if (!(nd->flags & LOOKUP_RCU)) 1719cd179f44SAl Viro path_put(link); 172006708adbSAl Viro return ERR_PTR(error); 1721626de996SAl Viro } 1722ab104923SAl Viro last = nd->stack + nd->depth++; 17231cf2665bSAl Viro last->link = *link; 1724fceef393SAl Viro clear_delayed_call(&last->done); 17250450b2d1SAl Viro last->seq = seq; 1726ad6cc4c3SAl Viro 1727b1a81972SAl Viro if (flags & WALK_TRAILING) { 1728ad6cc4c3SAl Viro error = may_follow_link(nd, inode); 1729ad6cc4c3SAl Viro if (unlikely(error)) 1730ad6cc4c3SAl Viro return ERR_PTR(error); 1731ad6cc4c3SAl Viro } 1732ad6cc4c3SAl Viro 1733dab741e0SMattias Nissler if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || 1734dab741e0SMattias Nissler unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) 1735ad6cc4c3SAl Viro return ERR_PTR(-ELOOP); 1736ad6cc4c3SAl Viro 1737ad6cc4c3SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1738ad6cc4c3SAl Viro touch_atime(&last->link); 1739ad6cc4c3SAl Viro cond_resched(); 1740ad6cc4c3SAl Viro } else if (atime_needs_update(&last->link, inode)) { 1741e36cffedSJens Axboe if (!try_to_unlazy(nd)) 1742ad6cc4c3SAl Viro return ERR_PTR(-ECHILD); 1743ad6cc4c3SAl Viro touch_atime(&last->link); 1744ad6cc4c3SAl Viro } 1745ad6cc4c3SAl Viro 1746ad6cc4c3SAl Viro error = security_inode_follow_link(link->dentry, inode, 1747ad6cc4c3SAl Viro nd->flags & LOOKUP_RCU); 1748ad6cc4c3SAl Viro if (unlikely(error)) 1749ad6cc4c3SAl Viro return ERR_PTR(error); 1750ad6cc4c3SAl Viro 1751ad6cc4c3SAl Viro res = READ_ONCE(inode->i_link); 1752ad6cc4c3SAl Viro if (!res) { 1753ad6cc4c3SAl Viro const char * (*get)(struct dentry *, struct inode *, 1754ad6cc4c3SAl Viro struct delayed_call *); 1755ad6cc4c3SAl Viro get = inode->i_op->get_link; 1756ad6cc4c3SAl Viro if (nd->flags & LOOKUP_RCU) { 1757ad6cc4c3SAl Viro res = get(NULL, inode, &last->done); 1758e36cffedSJens Axboe if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) 1759ad6cc4c3SAl Viro res = get(link->dentry, inode, &last->done); 1760ad6cc4c3SAl Viro } else { 1761ad6cc4c3SAl Viro res = get(link->dentry, inode, &last->done); 1762ad6cc4c3SAl Viro } 1763ad6cc4c3SAl Viro if (!res) 1764ad6cc4c3SAl Viro goto all_done; 1765ad6cc4c3SAl Viro if (IS_ERR(res)) 1766ad6cc4c3SAl Viro return res; 1767ad6cc4c3SAl Viro } 1768ad6cc4c3SAl Viro if (*res == '/') { 1769ad6cc4c3SAl Viro error = nd_jump_root(nd); 1770ad6cc4c3SAl Viro if (unlikely(error)) 1771ad6cc4c3SAl Viro return ERR_PTR(error); 1772ad6cc4c3SAl Viro while (unlikely(*++res == '/')) 1773ad6cc4c3SAl Viro ; 1774ad6cc4c3SAl Viro } 1775ad6cc4c3SAl Viro if (*res) 1776ad6cc4c3SAl Viro return res; 1777ad6cc4c3SAl Viro all_done: // pure jump 1778ad6cc4c3SAl Viro put_link(nd); 1779ad6cc4c3SAl Viro return NULL; 1780d63ff28fSAl Viro } 1781d63ff28fSAl Viro 17823ddcd056SLinus Torvalds /* 17833ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 17843ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 17853ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 17863ddcd056SLinus Torvalds * for the common case. 17873ddcd056SLinus Torvalds */ 1788b0417d2cSAl Viro static const char *step_into(struct nameidata *nd, int flags, 1789cbae4d12SAl Viro struct dentry *dentry, struct inode *inode, unsigned seq) 17903ddcd056SLinus Torvalds { 1791cbae4d12SAl Viro struct path path; 1792cbae4d12SAl Viro int err = handle_mounts(nd, dentry, &path, &inode, &seq); 1793cbae4d12SAl Viro 1794cbae4d12SAl Viro if (err < 0) 1795b0417d2cSAl Viro return ERR_PTR(err); 1796cbae4d12SAl Viro if (likely(!d_is_symlink(path.dentry)) || 17978c4efe22SAl Viro ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || 1798aca2903eSAl Viro (flags & WALK_NOFOLLOW)) { 17998f64fb1cSAl Viro /* not a symlink or should not follow */ 1800c99687a0SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1801c99687a0SAl Viro dput(nd->path.dentry); 1802c99687a0SAl Viro if (nd->path.mnt != path.mnt) 1803c99687a0SAl Viro mntput(nd->path.mnt); 1804c99687a0SAl Viro } 1805c99687a0SAl Viro nd->path = path; 18068f64fb1cSAl Viro nd->inode = inode; 18078f64fb1cSAl Viro nd->seq = seq; 1808b0417d2cSAl Viro return NULL; 18098f64fb1cSAl Viro } 1810a7f77542SAl Viro if (nd->flags & LOOKUP_RCU) { 181184f0cd9eSAl Viro /* make sure that d_is_symlink above matches inode */ 1812cbae4d12SAl Viro if (read_seqcount_retry(&path.dentry->d_seq, seq)) 1813b0417d2cSAl Viro return ERR_PTR(-ECHILD); 181484f0cd9eSAl Viro } else { 181584f0cd9eSAl Viro if (path.mnt == nd->path.mnt) 181684f0cd9eSAl Viro mntget(path.mnt); 1817a7f77542SAl Viro } 1818b1a81972SAl Viro return pick_link(nd, &path, inode, seq, flags); 18193ddcd056SLinus Torvalds } 18203ddcd056SLinus Torvalds 1821c2df1968SAl Viro static struct dentry *follow_dotdot_rcu(struct nameidata *nd, 1822c2df1968SAl Viro struct inode **inodep, 1823c2df1968SAl Viro unsigned *seqp) 1824957dd41dSAl Viro { 182512487f30SAl Viro struct dentry *parent, *old; 1826957dd41dSAl Viro 182712487f30SAl Viro if (path_equal(&nd->path, &nd->root)) 182812487f30SAl Viro goto in_root; 182912487f30SAl Viro if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 18307ef482faSAl Viro struct path path; 1831efe772d6SAl Viro unsigned seq; 18327ef482faSAl Viro if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), 18337ef482faSAl Viro &nd->root, &path, &seq)) 183412487f30SAl Viro goto in_root; 1835efe772d6SAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1836efe772d6SAl Viro return ERR_PTR(-ECHILD); 1837efe772d6SAl Viro nd->path = path; 1838efe772d6SAl Viro nd->inode = path.dentry->d_inode; 1839efe772d6SAl Viro nd->seq = seq; 1840efe772d6SAl Viro if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) 1841efe772d6SAl Viro return ERR_PTR(-ECHILD); 1842efe772d6SAl Viro /* we know that mountpoint was pinned */ 1843957dd41dSAl Viro } 184412487f30SAl Viro old = nd->path.dentry; 184512487f30SAl Viro parent = old->d_parent; 184612487f30SAl Viro *inodep = parent->d_inode; 184712487f30SAl Viro *seqp = read_seqcount_begin(&parent->d_seq); 184812487f30SAl Viro if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) 184912487f30SAl Viro return ERR_PTR(-ECHILD); 185012487f30SAl Viro if (unlikely(!path_connected(nd->path.mnt, parent))) 185112487f30SAl Viro return ERR_PTR(-ECHILD); 185212487f30SAl Viro return parent; 185312487f30SAl Viro in_root: 1854efe772d6SAl Viro if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) 1855efe772d6SAl Viro return ERR_PTR(-ECHILD); 1856957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_BENEATH)) 18577521f22bSAl Viro return ERR_PTR(-ECHILD); 1858c2df1968SAl Viro return NULL; 1859957dd41dSAl Viro } 1860957dd41dSAl Viro 1861c2df1968SAl Viro static struct dentry *follow_dotdot(struct nameidata *nd, 1862c2df1968SAl Viro struct inode **inodep, 1863c2df1968SAl Viro unsigned *seqp) 1864957dd41dSAl Viro { 186512487f30SAl Viro struct dentry *parent; 186612487f30SAl Viro 1867957dd41dSAl Viro if (path_equal(&nd->path, &nd->root)) 186812487f30SAl Viro goto in_root; 186912487f30SAl Viro if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 18702aa38470SAl Viro struct path path; 18712aa38470SAl Viro 18722aa38470SAl Viro if (!choose_mountpoint(real_mount(nd->path.mnt), 18732aa38470SAl Viro &nd->root, &path)) 187412487f30SAl Viro goto in_root; 1875165200d6SAl Viro path_put(&nd->path); 1876165200d6SAl Viro nd->path = path; 18772aa38470SAl Viro nd->inode = path.dentry->d_inode; 1878165200d6SAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1879165200d6SAl Viro return ERR_PTR(-EXDEV); 188012487f30SAl Viro } 1881957dd41dSAl Viro /* rare case of legitimate dget_parent()... */ 188212487f30SAl Viro parent = dget_parent(nd->path.dentry); 1883957dd41dSAl Viro if (unlikely(!path_connected(nd->path.mnt, parent))) { 1884957dd41dSAl Viro dput(parent); 18857521f22bSAl Viro return ERR_PTR(-ENOENT); 1886957dd41dSAl Viro } 1887c2df1968SAl Viro *seqp = 0; 1888c2df1968SAl Viro *inodep = parent->d_inode; 1889c2df1968SAl Viro return parent; 189012487f30SAl Viro 189112487f30SAl Viro in_root: 1892957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_BENEATH)) 18937521f22bSAl Viro return ERR_PTR(-EXDEV); 1894c2df1968SAl Viro dget(nd->path.dentry); 1895c2df1968SAl Viro return NULL; 1896957dd41dSAl Viro } 1897957dd41dSAl Viro 18987521f22bSAl Viro static const char *handle_dots(struct nameidata *nd, int type) 1899957dd41dSAl Viro { 1900957dd41dSAl Viro if (type == LAST_DOTDOT) { 19017521f22bSAl Viro const char *error = NULL; 1902c2df1968SAl Viro struct dentry *parent; 1903c2df1968SAl Viro struct inode *inode; 1904c2df1968SAl Viro unsigned seq; 1905957dd41dSAl Viro 1906957dd41dSAl Viro if (!nd->root.mnt) { 19077521f22bSAl Viro error = ERR_PTR(set_root(nd)); 1908957dd41dSAl Viro if (error) 1909957dd41dSAl Viro return error; 1910957dd41dSAl Viro } 1911957dd41dSAl Viro if (nd->flags & LOOKUP_RCU) 1912c2df1968SAl Viro parent = follow_dotdot_rcu(nd, &inode, &seq); 1913957dd41dSAl Viro else 1914c2df1968SAl Viro parent = follow_dotdot(nd, &inode, &seq); 1915c2df1968SAl Viro if (IS_ERR(parent)) 1916c2df1968SAl Viro return ERR_CAST(parent); 1917c2df1968SAl Viro if (unlikely(!parent)) 1918c2df1968SAl Viro error = step_into(nd, WALK_NOFOLLOW, 1919c2df1968SAl Viro nd->path.dentry, nd->inode, nd->seq); 1920c2df1968SAl Viro else 1921c2df1968SAl Viro error = step_into(nd, WALK_NOFOLLOW, 1922c2df1968SAl Viro parent, inode, seq); 1923c2df1968SAl Viro if (unlikely(error)) 1924957dd41dSAl Viro return error; 1925957dd41dSAl Viro 1926957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 1927957dd41dSAl Viro /* 1928957dd41dSAl Viro * If there was a racing rename or mount along our 1929957dd41dSAl Viro * path, then we can't be sure that ".." hasn't jumped 1930957dd41dSAl Viro * above nd->root (and so userspace should retry or use 1931957dd41dSAl Viro * some fallback). 1932957dd41dSAl Viro */ 1933957dd41dSAl Viro smp_rmb(); 1934957dd41dSAl Viro if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) 19357521f22bSAl Viro return ERR_PTR(-EAGAIN); 1936957dd41dSAl Viro if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) 19377521f22bSAl Viro return ERR_PTR(-EAGAIN); 1938957dd41dSAl Viro } 1939957dd41dSAl Viro } 19407521f22bSAl Viro return NULL; 1941957dd41dSAl Viro } 1942957dd41dSAl Viro 194392d27016SAl Viro static const char *walk_component(struct nameidata *nd, int flags) 1944ce57dfc1SAl Viro { 1945db3c9adeSAl Viro struct dentry *dentry; 1946ce57dfc1SAl Viro struct inode *inode; 1947254cf582SAl Viro unsigned seq; 1948ce57dfc1SAl Viro /* 1949ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1950ce57dfc1SAl Viro * to be able to know about the current root directory and 1951ce57dfc1SAl Viro * parent relationships. 1952ce57dfc1SAl Viro */ 19534693a547SAl Viro if (unlikely(nd->last_type != LAST_NORM)) { 19541c4ff1a8SAl Viro if (!(flags & WALK_MORE) && nd->depth) 19554693a547SAl Viro put_link(nd); 19567521f22bSAl Viro return handle_dots(nd, nd->last_type); 19574693a547SAl Viro } 195820e34357SAl Viro dentry = lookup_fast(nd, &inode, &seq); 195920e34357SAl Viro if (IS_ERR(dentry)) 196092d27016SAl Viro return ERR_CAST(dentry); 196120e34357SAl Viro if (unlikely(!dentry)) { 1962db3c9adeSAl Viro dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); 1963db3c9adeSAl Viro if (IS_ERR(dentry)) 196492d27016SAl Viro return ERR_CAST(dentry); 196520e34357SAl Viro } 196656676ec3SAl Viro if (!(flags & WALK_MORE) && nd->depth) 196756676ec3SAl Viro put_link(nd); 1968b0417d2cSAl Viro return step_into(nd, flags, dentry, inode, seq); 1969ce57dfc1SAl Viro } 1970ce57dfc1SAl Viro 19711da177e4SLinus Torvalds /* 1972bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1973bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1974bfcfaa77SLinus Torvalds * 1975bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1976bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1977bfcfaa77SLinus Torvalds * fast. 1978bfcfaa77SLinus Torvalds * 1979bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1980bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1981bfcfaa77SLinus Torvalds * crossing operation. 1982bfcfaa77SLinus Torvalds * 1983bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1984bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1985bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1986bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1987bfcfaa77SLinus Torvalds */ 1988bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1989bfcfaa77SLinus Torvalds 1990f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1991bfcfaa77SLinus Torvalds 1992468a9428SGeorge Spelvin #ifdef HASH_MIX 1993bfcfaa77SLinus Torvalds 1994468a9428SGeorge Spelvin /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ 1995468a9428SGeorge Spelvin 1996468a9428SGeorge Spelvin #elif defined(CONFIG_64BIT) 19972a18da7aSGeorge Spelvin /* 19982a18da7aSGeorge Spelvin * Register pressure in the mixing function is an issue, particularly 19992a18da7aSGeorge Spelvin * on 32-bit x86, but almost any function requires one state value and 20002a18da7aSGeorge Spelvin * one temporary. Instead, use a function designed for two state values 20012a18da7aSGeorge Spelvin * and no temporaries. 20022a18da7aSGeorge Spelvin * 20032a18da7aSGeorge Spelvin * This function cannot create a collision in only two iterations, so 20042a18da7aSGeorge Spelvin * we have two iterations to achieve avalanche. In those two iterations, 20052a18da7aSGeorge Spelvin * we have six layers of mixing, which is enough to spread one bit's 20062a18da7aSGeorge Spelvin * influence out to 2^6 = 64 state bits. 20072a18da7aSGeorge Spelvin * 20082a18da7aSGeorge Spelvin * Rotate constants are scored by considering either 64 one-bit input 20092a18da7aSGeorge Spelvin * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the 20102a18da7aSGeorge Spelvin * probability of that delta causing a change to each of the 128 output 20112a18da7aSGeorge Spelvin * bits, using a sample of random initial states. 20122a18da7aSGeorge Spelvin * 20132a18da7aSGeorge Spelvin * The Shannon entropy of the computed probabilities is then summed 20142a18da7aSGeorge Spelvin * to produce a score. Ideally, any input change has a 50% chance of 20152a18da7aSGeorge Spelvin * toggling any given output bit. 20162a18da7aSGeorge Spelvin * 20172a18da7aSGeorge Spelvin * Mixing scores (in bits) for (12,45): 20182a18da7aSGeorge Spelvin * Input delta: 1-bit 2-bit 20192a18da7aSGeorge Spelvin * 1 round: 713.3 42542.6 20202a18da7aSGeorge Spelvin * 2 rounds: 2753.7 140389.8 20212a18da7aSGeorge Spelvin * 3 rounds: 5954.1 233458.2 20222a18da7aSGeorge Spelvin * 4 rounds: 7862.6 256672.2 20232a18da7aSGeorge Spelvin * Perfect: 8192 258048 20242a18da7aSGeorge Spelvin * (64*128) (64*63/2 * 128) 20252a18da7aSGeorge Spelvin */ 20262a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a) \ 20272a18da7aSGeorge Spelvin ( x ^= (a), \ 20282a18da7aSGeorge Spelvin y ^= x, x = rol64(x,12),\ 20292a18da7aSGeorge Spelvin x += y, y = rol64(y,45),\ 20302a18da7aSGeorge Spelvin y *= 9 ) 2031bfcfaa77SLinus Torvalds 20320fed3ac8SGeorge Spelvin /* 20332a18da7aSGeorge Spelvin * Fold two longs into one 32-bit hash value. This must be fast, but 20342a18da7aSGeorge Spelvin * latency isn't quite as critical, as there is a fair bit of additional 20352a18da7aSGeorge Spelvin * work done before the hash value is used. 20360fed3ac8SGeorge Spelvin */ 20372a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y) 20380fed3ac8SGeorge Spelvin { 20392a18da7aSGeorge Spelvin y ^= x * GOLDEN_RATIO_64; 20402a18da7aSGeorge Spelvin y *= GOLDEN_RATIO_64; 20412a18da7aSGeorge Spelvin return y >> 32; 20420fed3ac8SGeorge Spelvin } 20430fed3ac8SGeorge Spelvin 2044bfcfaa77SLinus Torvalds #else /* 32-bit case */ 2045bfcfaa77SLinus Torvalds 20462a18da7aSGeorge Spelvin /* 20472a18da7aSGeorge Spelvin * Mixing scores (in bits) for (7,20): 20482a18da7aSGeorge Spelvin * Input delta: 1-bit 2-bit 20492a18da7aSGeorge Spelvin * 1 round: 330.3 9201.6 20502a18da7aSGeorge Spelvin * 2 rounds: 1246.4 25475.4 20512a18da7aSGeorge Spelvin * 3 rounds: 1907.1 31295.1 20522a18da7aSGeorge Spelvin * 4 rounds: 2042.3 31718.6 20532a18da7aSGeorge Spelvin * Perfect: 2048 31744 20542a18da7aSGeorge Spelvin * (32*64) (32*31/2 * 64) 20552a18da7aSGeorge Spelvin */ 20562a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a) \ 20572a18da7aSGeorge Spelvin ( x ^= (a), \ 20582a18da7aSGeorge Spelvin y ^= x, x = rol32(x, 7),\ 20592a18da7aSGeorge Spelvin x += y, y = rol32(y,20),\ 20602a18da7aSGeorge Spelvin y *= 9 ) 2061bfcfaa77SLinus Torvalds 20622a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y) 20630fed3ac8SGeorge Spelvin { 20642a18da7aSGeorge Spelvin /* Use arch-optimized multiply if one exists */ 20652a18da7aSGeorge Spelvin return __hash_32(y ^ __hash_32(x)); 20660fed3ac8SGeorge Spelvin } 20670fed3ac8SGeorge Spelvin 2068bfcfaa77SLinus Torvalds #endif 2069bfcfaa77SLinus Torvalds 20702a18da7aSGeorge Spelvin /* 20712a18da7aSGeorge Spelvin * Return the hash of a string of known length. This is carfully 20722a18da7aSGeorge Spelvin * designed to match hash_name(), which is the more critical function. 20732a18da7aSGeorge Spelvin * In particular, we must end by hashing a final word containing 0..7 20742a18da7aSGeorge Spelvin * payload bytes, to match the way that hash_name() iterates until it 20752a18da7aSGeorge Spelvin * finds the delimiter after the name. 20762a18da7aSGeorge Spelvin */ 20778387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 2078bfcfaa77SLinus Torvalds { 20798387ff25SLinus Torvalds unsigned long a, x = 0, y = (unsigned long)salt; 2080bfcfaa77SLinus Torvalds 2081bfcfaa77SLinus Torvalds for (;;) { 2082fcfd2fbfSGeorge Spelvin if (!len) 2083fcfd2fbfSGeorge Spelvin goto done; 2084e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 2085bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 2086bfcfaa77SLinus Torvalds break; 20872a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2088bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 2089bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 2090bfcfaa77SLinus Torvalds } 20912a18da7aSGeorge Spelvin x ^= a & bytemask_from_count(len); 2092bfcfaa77SLinus Torvalds done: 20932a18da7aSGeorge Spelvin return fold_hash(x, y); 2094bfcfaa77SLinus Torvalds } 2095bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 2096bfcfaa77SLinus Torvalds 2097fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */ 20988387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name) 2099fcfd2fbfSGeorge Spelvin { 21008387ff25SLinus Torvalds unsigned long a = 0, x = 0, y = (unsigned long)salt; 21018387ff25SLinus Torvalds unsigned long adata, mask, len; 2102fcfd2fbfSGeorge Spelvin const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2103fcfd2fbfSGeorge Spelvin 21048387ff25SLinus Torvalds len = 0; 21058387ff25SLinus Torvalds goto inside; 21068387ff25SLinus Torvalds 2107fcfd2fbfSGeorge Spelvin do { 21082a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2109fcfd2fbfSGeorge Spelvin len += sizeof(unsigned long); 21108387ff25SLinus Torvalds inside: 2111fcfd2fbfSGeorge Spelvin a = load_unaligned_zeropad(name+len); 2112fcfd2fbfSGeorge Spelvin } while (!has_zero(a, &adata, &constants)); 2113fcfd2fbfSGeorge Spelvin 2114fcfd2fbfSGeorge Spelvin adata = prep_zero_mask(a, adata, &constants); 2115fcfd2fbfSGeorge Spelvin mask = create_zero_mask(adata); 21162a18da7aSGeorge Spelvin x ^= a & zero_bytemask(mask); 2117fcfd2fbfSGeorge Spelvin 21182a18da7aSGeorge Spelvin return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2119fcfd2fbfSGeorge Spelvin } 2120fcfd2fbfSGeorge Spelvin EXPORT_SYMBOL(hashlen_string); 2121fcfd2fbfSGeorge Spelvin 2122bfcfaa77SLinus Torvalds /* 2123bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 2124d6bb3e90SLinus Torvalds * return the "hash_len" as the result. 2125bfcfaa77SLinus Torvalds */ 21268387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name) 2127bfcfaa77SLinus Torvalds { 21288387ff25SLinus Torvalds unsigned long a = 0, b, x = 0, y = (unsigned long)salt; 21298387ff25SLinus Torvalds unsigned long adata, bdata, mask, len; 213036126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2131bfcfaa77SLinus Torvalds 21328387ff25SLinus Torvalds len = 0; 21338387ff25SLinus Torvalds goto inside; 21348387ff25SLinus Torvalds 2135bfcfaa77SLinus Torvalds do { 21362a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2137bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 21388387ff25SLinus Torvalds inside: 2139e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 214036126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 214136126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 2142bfcfaa77SLinus Torvalds 214336126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 214436126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 214536126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 21462a18da7aSGeorge Spelvin x ^= a & zero_bytemask(mask); 214736126f8fSLinus Torvalds 21482a18da7aSGeorge Spelvin return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2149bfcfaa77SLinus Torvalds } 2150bfcfaa77SLinus Torvalds 21512a18da7aSGeorge Spelvin #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ 2152bfcfaa77SLinus Torvalds 2153fcfd2fbfSGeorge Spelvin /* Return the hash of a string of known length */ 21548387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 21550145acc2SLinus Torvalds { 21568387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 21570145acc2SLinus Torvalds while (len--) 2158fcfd2fbfSGeorge Spelvin hash = partial_name_hash((unsigned char)*name++, hash); 21590145acc2SLinus Torvalds return end_name_hash(hash); 21600145acc2SLinus Torvalds } 2161ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 21620145acc2SLinus Torvalds 2163fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */ 21648387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name) 2165fcfd2fbfSGeorge Spelvin { 21668387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 2167fcfd2fbfSGeorge Spelvin unsigned long len = 0, c; 2168fcfd2fbfSGeorge Spelvin 2169fcfd2fbfSGeorge Spelvin c = (unsigned char)*name; 2170e0ab7af9SGeorge Spelvin while (c) { 2171fcfd2fbfSGeorge Spelvin len++; 2172fcfd2fbfSGeorge Spelvin hash = partial_name_hash(c, hash); 2173fcfd2fbfSGeorge Spelvin c = (unsigned char)name[len]; 2174e0ab7af9SGeorge Spelvin } 2175fcfd2fbfSGeorge Spelvin return hashlen_create(end_name_hash(hash), len); 2176fcfd2fbfSGeorge Spelvin } 2177f2a031b6SGeorge Spelvin EXPORT_SYMBOL(hashlen_string); 2178fcfd2fbfSGeorge Spelvin 21793ddcd056SLinus Torvalds /* 2180200e9ef7SLinus Torvalds * We know there's a real path component here of at least 2181200e9ef7SLinus Torvalds * one character. 2182200e9ef7SLinus Torvalds */ 21838387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name) 2184200e9ef7SLinus Torvalds { 21858387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 2186200e9ef7SLinus Torvalds unsigned long len = 0, c; 2187200e9ef7SLinus Torvalds 2188200e9ef7SLinus Torvalds c = (unsigned char)*name; 2189200e9ef7SLinus Torvalds do { 2190200e9ef7SLinus Torvalds len++; 2191200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 2192200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 2193200e9ef7SLinus Torvalds } while (c && c != '/'); 2194d6bb3e90SLinus Torvalds return hashlen_create(end_name_hash(hash), len); 2195200e9ef7SLinus Torvalds } 2196200e9ef7SLinus Torvalds 2197bfcfaa77SLinus Torvalds #endif 2198bfcfaa77SLinus Torvalds 2199200e9ef7SLinus Torvalds /* 22001da177e4SLinus Torvalds * Name resolution. 2201ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 2202ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 22031da177e4SLinus Torvalds * 2204ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 2205ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 22061da177e4SLinus Torvalds */ 22076de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 22081da177e4SLinus Torvalds { 2209d8d4611aSAl Viro int depth = 0; // depth <= nd->depth 22101da177e4SLinus Torvalds int err; 22111da177e4SLinus Torvalds 2212b4c03536SAl Viro nd->last_type = LAST_ROOT; 2213c108837eSAl Viro nd->flags |= LOOKUP_PARENT; 22149b5858e9SAl Viro if (IS_ERR(name)) 22159b5858e9SAl Viro return PTR_ERR(name); 22161da177e4SLinus Torvalds while (*name=='/') 22171da177e4SLinus Torvalds name++; 22181a97d899SAl Viro if (!*name) { 22191a97d899SAl Viro nd->dir_mode = 0; // short-circuit the 'hardening' idiocy 22209e18f10aSAl Viro return 0; 22211a97d899SAl Viro } 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 22241da177e4SLinus Torvalds for(;;) { 2225549c7297SChristian Brauner struct user_namespace *mnt_userns; 222692d27016SAl Viro const char *link; 2227d6bb3e90SLinus Torvalds u64 hash_len; 2228fe479a58SAl Viro int type; 22291da177e4SLinus Torvalds 2230549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 2231549c7297SChristian Brauner err = may_lookup(mnt_userns, nd); 22321da177e4SLinus Torvalds if (err) 22333595e234SAl Viro return err; 22341da177e4SLinus Torvalds 22358387ff25SLinus Torvalds hash_len = hash_name(nd->path.dentry, name); 22361da177e4SLinus Torvalds 2237fe479a58SAl Viro type = LAST_NORM; 2238d6bb3e90SLinus Torvalds if (name[0] == '.') switch (hashlen_len(hash_len)) { 2239fe479a58SAl Viro case 2: 2240200e9ef7SLinus Torvalds if (name[1] == '.') { 2241fe479a58SAl Viro type = LAST_DOTDOT; 2242bcba1e7dSAl Viro nd->state |= ND_JUMPED; 224316c2cd71SAl Viro } 2244fe479a58SAl Viro break; 2245fe479a58SAl Viro case 1: 2246fe479a58SAl Viro type = LAST_DOT; 2247fe479a58SAl Viro } 22485a202bcdSAl Viro if (likely(type == LAST_NORM)) { 22495a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 2250bcba1e7dSAl Viro nd->state &= ~ND_JUMPED; 22515a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 2252a060dc50SJames Hogan struct qstr this = { { .hash_len = hash_len }, .name = name }; 2253da53be12SLinus Torvalds err = parent->d_op->d_hash(parent, &this); 22545a202bcdSAl Viro if (err < 0) 22553595e234SAl Viro return err; 2256d6bb3e90SLinus Torvalds hash_len = this.hash_len; 2257d6bb3e90SLinus Torvalds name = this.name; 22585a202bcdSAl Viro } 22595a202bcdSAl Viro } 2260fe479a58SAl Viro 2261d6bb3e90SLinus Torvalds nd->last.hash_len = hash_len; 2262d6bb3e90SLinus Torvalds nd->last.name = name; 22635f4a6a69SAl Viro nd->last_type = type; 22645f4a6a69SAl Viro 2265d6bb3e90SLinus Torvalds name += hashlen_len(hash_len); 2266d6bb3e90SLinus Torvalds if (!*name) 2267bdf6cbf1SAl Viro goto OK; 2268200e9ef7SLinus Torvalds /* 2269200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 2270200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 2271200e9ef7SLinus Torvalds */ 2272200e9ef7SLinus Torvalds do { 2273d6bb3e90SLinus Torvalds name++; 2274d6bb3e90SLinus Torvalds } while (unlikely(*name == '/')); 22758620c238SAl Viro if (unlikely(!*name)) { 22768620c238SAl Viro OK: 2277d8d4611aSAl Viro /* pathname or trailing symlink, done */ 2278c108837eSAl Viro if (!depth) { 2279549c7297SChristian Brauner nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode); 22800f705953SAl Viro nd->dir_mode = nd->inode->i_mode; 2281c108837eSAl Viro nd->flags &= ~LOOKUP_PARENT; 22828620c238SAl Viro return 0; 2283c108837eSAl Viro } 22848620c238SAl Viro /* last component of nested symlink */ 2285d8d4611aSAl Viro name = nd->stack[--depth].name; 22868c4efe22SAl Viro link = walk_component(nd, 0); 22871c4ff1a8SAl Viro } else { 22881c4ff1a8SAl Viro /* not the last component */ 22898c4efe22SAl Viro link = walk_component(nd, WALK_MORE); 22908620c238SAl Viro } 229192d27016SAl Viro if (unlikely(link)) { 229292d27016SAl Viro if (IS_ERR(link)) 229392d27016SAl Viro return PTR_ERR(link); 229492d27016SAl Viro /* a symlink to follow */ 2295d8d4611aSAl Viro nd->stack[depth++].name = name; 229692d27016SAl Viro name = link; 22979e18f10aSAl Viro continue; 229848c8b0c5SAl Viro } 229997242f99SAl Viro if (unlikely(!d_can_lookup(nd->path.dentry))) { 230097242f99SAl Viro if (nd->flags & LOOKUP_RCU) { 2301e36cffedSJens Axboe if (!try_to_unlazy(nd)) 230297242f99SAl Viro return -ECHILD; 230397242f99SAl Viro } 23043595e234SAl Viro return -ENOTDIR; 23055f4a6a69SAl Viro } 2306ce57dfc1SAl Viro } 230797242f99SAl Viro } 23081da177e4SLinus Torvalds 2309edc2b1daSAl Viro /* must be paired with terminate_walk() */ 2310c8a53ee5SAl Viro static const char *path_init(struct nameidata *nd, unsigned flags) 231131e6b01fSNick Piggin { 2312740a1678SAleksa Sarai int error; 2313c8a53ee5SAl Viro const char *s = nd->name->name; 231431e6b01fSNick Piggin 23156c6ec2b0SJens Axboe /* LOOKUP_CACHED requires RCU, ask caller to retry */ 23166c6ec2b0SJens Axboe if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) 23176c6ec2b0SJens Axboe return ERR_PTR(-EAGAIN); 23186c6ec2b0SJens Axboe 2319c0eb027eSLinus Torvalds if (!*s) 2320c0eb027eSLinus Torvalds flags &= ~LOOKUP_RCU; 2321edc2b1daSAl Viro if (flags & LOOKUP_RCU) 2322edc2b1daSAl Viro rcu_read_lock(); 2323c0eb027eSLinus Torvalds 2324bcba1e7dSAl Viro nd->flags = flags; 2325bcba1e7dSAl Viro nd->state |= ND_JUMPED; 2326ab87f9a5SAleksa Sarai 2327ab87f9a5SAleksa Sarai nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); 2328ab87f9a5SAleksa Sarai nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); 2329ab87f9a5SAleksa Sarai smp_rmb(); 2330ab87f9a5SAleksa Sarai 2331bcba1e7dSAl Viro if (nd->state & ND_ROOT_PRESET) { 2332b18825a7SDavid Howells struct dentry *root = nd->root.dentry; 2333b18825a7SDavid Howells struct inode *inode = root->d_inode; 233493893862SAl Viro if (*s && unlikely(!d_can_lookup(root))) 2335368ee9baSAl Viro return ERR_PTR(-ENOTDIR); 23365b6ca027SAl Viro nd->path = nd->root; 23375b6ca027SAl Viro nd->inode = inode; 23385b6ca027SAl Viro if (flags & LOOKUP_RCU) { 2339ab87f9a5SAleksa Sarai nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 23408f47a016SAl Viro nd->root_seq = nd->seq; 23415b6ca027SAl Viro } else { 23425b6ca027SAl Viro path_get(&nd->path); 23435b6ca027SAl Viro } 2344368ee9baSAl Viro return s; 23455b6ca027SAl Viro } 23465b6ca027SAl Viro 234731e6b01fSNick Piggin nd->root.mnt = NULL; 234831e6b01fSNick Piggin 23498db52c7eSAleksa Sarai /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ 23508db52c7eSAleksa Sarai if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { 2351740a1678SAleksa Sarai error = nd_jump_root(nd); 2352740a1678SAleksa Sarai if (unlikely(error)) 2353740a1678SAleksa Sarai return ERR_PTR(error); 2354ef55d917SAl Viro return s; 23558db52c7eSAleksa Sarai } 23568db52c7eSAleksa Sarai 23578db52c7eSAleksa Sarai /* Relative pathname -- get the starting-point it is relative to. */ 23588db52c7eSAleksa Sarai if (nd->dfd == AT_FDCWD) { 2359e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 236031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 2361c28cc364SNick Piggin unsigned seq; 236231e6b01fSNick Piggin 2363c28cc364SNick Piggin do { 2364c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 236531e6b01fSNick Piggin nd->path = fs->pwd; 2366ef55d917SAl Viro nd->inode = nd->path.dentry->d_inode; 2367c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 2368c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 2369e41f7d4eSAl Viro } else { 2370e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 2371ef55d917SAl Viro nd->inode = nd->path.dentry->d_inode; 2372e41f7d4eSAl Viro } 237331e6b01fSNick Piggin } else { 2374582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 2375c8a53ee5SAl Viro struct fd f = fdget_raw(nd->dfd); 237631e6b01fSNick Piggin struct dentry *dentry; 237731e6b01fSNick Piggin 23782903ff01SAl Viro if (!f.file) 2379368ee9baSAl Viro return ERR_PTR(-EBADF); 238031e6b01fSNick Piggin 23812903ff01SAl Viro dentry = f.file->f_path.dentry; 238231e6b01fSNick Piggin 2383edc2b1daSAl Viro if (*s && unlikely(!d_can_lookup(dentry))) { 23842903ff01SAl Viro fdput(f); 2385368ee9baSAl Viro return ERR_PTR(-ENOTDIR); 2386f52e0c11SAl Viro } 23872903ff01SAl Viro 23882903ff01SAl Viro nd->path = f.file->f_path; 2389e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 239034a26b99SAl Viro nd->inode = nd->path.dentry->d_inode; 239134a26b99SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 23925590ff0dSUlrich Drepper } else { 23932903ff01SAl Viro path_get(&nd->path); 239434a26b99SAl Viro nd->inode = nd->path.dentry->d_inode; 23951da177e4SLinus Torvalds } 239634a26b99SAl Viro fdput(f); 2397e41f7d4eSAl Viro } 23988db52c7eSAleksa Sarai 2399adb21d2bSAleksa Sarai /* For scoped-lookups we need to set the root to the dirfd as well. */ 2400adb21d2bSAleksa Sarai if (flags & LOOKUP_IS_SCOPED) { 2401adb21d2bSAleksa Sarai nd->root = nd->path; 2402adb21d2bSAleksa Sarai if (flags & LOOKUP_RCU) { 2403adb21d2bSAleksa Sarai nd->root_seq = nd->seq; 2404adb21d2bSAleksa Sarai } else { 2405adb21d2bSAleksa Sarai path_get(&nd->root); 2406bcba1e7dSAl Viro nd->state |= ND_ROOT_GRABBED; 2407adb21d2bSAleksa Sarai } 2408adb21d2bSAleksa Sarai } 2409adb21d2bSAleksa Sarai return s; 24109b4a9b14SAl Viro } 24119b4a9b14SAl Viro 24121ccac622SAl Viro static inline const char *lookup_last(struct nameidata *nd) 241395fa25d9SAl Viro { 2414bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 2415bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2416bd92d7feSAl Viro 2417c108837eSAl Viro return walk_component(nd, WALK_TRAILING); 2418bd92d7feSAl Viro } 2419bd92d7feSAl Viro 24204f757f3cSAl Viro static int handle_lookup_down(struct nameidata *nd) 24214f757f3cSAl Viro { 2422c153007bSAl Viro if (!(nd->flags & LOOKUP_RCU)) 2423db3c9adeSAl Viro dget(nd->path.dentry); 2424b0417d2cSAl Viro return PTR_ERR(step_into(nd, WALK_NOFOLLOW, 2425b0417d2cSAl Viro nd->path.dentry, nd->inode, nd->seq)); 24264f757f3cSAl Viro } 24274f757f3cSAl Viro 24289b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2429c8a53ee5SAl Viro static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) 24309b4a9b14SAl Viro { 2431c8a53ee5SAl Viro const char *s = path_init(nd, flags); 2432bd92d7feSAl Viro int err; 243331e6b01fSNick Piggin 24349b5858e9SAl Viro if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { 24354f757f3cSAl Viro err = handle_lookup_down(nd); 24365f336e72SAl Viro if (unlikely(err < 0)) 24375f336e72SAl Viro s = ERR_PTR(err); 24384f757f3cSAl Viro } 24394f757f3cSAl Viro 24401ccac622SAl Viro while (!(err = link_path_walk(s, nd)) && 24411ccac622SAl Viro (s = lookup_last(nd)) != NULL) 24421ccac622SAl Viro ; 24434f0ed93fSAl Viro if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { 24444f0ed93fSAl Viro err = handle_lookup_down(nd); 2445bcba1e7dSAl Viro nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... 24464f0ed93fSAl Viro } 24479f1fafeeSAl Viro if (!err) 24489f1fafeeSAl Viro err = complete_walk(nd); 2449bd92d7feSAl Viro 2450deb106c6SAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) 2451deb106c6SAl Viro if (!d_can_lookup(nd->path.dentry)) 2452bd23a539SAl Viro err = -ENOTDIR; 2453625b6d10SAl Viro if (!err) { 2454625b6d10SAl Viro *path = nd->path; 2455625b6d10SAl Viro nd->path.mnt = NULL; 2456625b6d10SAl Viro nd->path.dentry = NULL; 2457625b6d10SAl Viro } 2458deb106c6SAl Viro terminate_walk(nd); 2459bd92d7feSAl Viro return err; 246031e6b01fSNick Piggin } 246131e6b01fSNick Piggin 246231d921c7SDavid Howells int filename_lookup(int dfd, struct filename *name, unsigned flags, 24639ad1aaa6SAl Viro struct path *path, struct path *root) 2464873f1eedSJeff Layton { 2465894bc8c4SAl Viro int retval; 24669883d185SAl Viro struct nameidata nd; 2467abc9f5beSAl Viro if (IS_ERR(name)) 2468abc9f5beSAl Viro return PTR_ERR(name); 246906422964SAl Viro set_nameidata(&nd, dfd, name, root); 2470c8a53ee5SAl Viro retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); 2471873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2472c8a53ee5SAl Viro retval = path_lookupat(&nd, flags, path); 2473873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2474c8a53ee5SAl Viro retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); 2475873f1eedSJeff Layton 2476873f1eedSJeff Layton if (likely(!retval)) 2477161aff1dSAl Viro audit_inode(name, path->dentry, 2478161aff1dSAl Viro flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); 24799883d185SAl Viro restore_nameidata(); 2480e4bd1c1aSAl Viro putname(name); 2481873f1eedSJeff Layton return retval; 2482873f1eedSJeff Layton } 2483873f1eedSJeff Layton 24848bcb77faSAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2485c8a53ee5SAl Viro static int path_parentat(struct nameidata *nd, unsigned flags, 2486391172c4SAl Viro struct path *parent) 24878bcb77faSAl Viro { 2488c8a53ee5SAl Viro const char *s = path_init(nd, flags); 24899b5858e9SAl Viro int err = link_path_walk(s, nd); 24908bcb77faSAl Viro if (!err) 24918bcb77faSAl Viro err = complete_walk(nd); 2492391172c4SAl Viro if (!err) { 2493391172c4SAl Viro *parent = nd->path; 2494391172c4SAl Viro nd->path.mnt = NULL; 2495391172c4SAl Viro nd->path.dentry = NULL; 2496391172c4SAl Viro } 2497deb106c6SAl Viro terminate_walk(nd); 24988bcb77faSAl Viro return err; 24998bcb77faSAl Viro } 25008bcb77faSAl Viro 2501*0ee50b47SDmitry Kadashev static int __filename_parentat(int dfd, struct filename *name, 2502391172c4SAl Viro unsigned int flags, struct path *parent, 2503391172c4SAl Viro struct qstr *last, int *type) 25048bcb77faSAl Viro { 25058bcb77faSAl Viro int retval; 25069883d185SAl Viro struct nameidata nd; 25078bcb77faSAl Viro 25085c31b6ceSAl Viro if (IS_ERR(name)) 2509*0ee50b47SDmitry Kadashev return PTR_ERR(name); 251006422964SAl Viro set_nameidata(&nd, dfd, name, NULL); 2511c8a53ee5SAl Viro retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); 25128bcb77faSAl Viro if (unlikely(retval == -ECHILD)) 2513c8a53ee5SAl Viro retval = path_parentat(&nd, flags, parent); 25148bcb77faSAl Viro if (unlikely(retval == -ESTALE)) 2515c8a53ee5SAl Viro retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); 2516391172c4SAl Viro if (likely(!retval)) { 2517391172c4SAl Viro *last = nd.last; 2518391172c4SAl Viro *type = nd.last_type; 2519c9b07eabSAl Viro audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); 2520391172c4SAl Viro } 25219883d185SAl Viro restore_nameidata(); 2522*0ee50b47SDmitry Kadashev return retval; 2523*0ee50b47SDmitry Kadashev } 2524*0ee50b47SDmitry Kadashev 2525*0ee50b47SDmitry Kadashev static int filename_parentat(int dfd, struct filename *name, 2526*0ee50b47SDmitry Kadashev unsigned int flags, struct path *parent, 2527*0ee50b47SDmitry Kadashev struct qstr *last, int *type) 2528*0ee50b47SDmitry Kadashev { 2529*0ee50b47SDmitry Kadashev int retval = __filename_parentat(dfd, name, flags, parent, last, type); 2530*0ee50b47SDmitry Kadashev 2531*0ee50b47SDmitry Kadashev putname(name); 2532*0ee50b47SDmitry Kadashev return retval; 25338bcb77faSAl Viro } 25348bcb77faSAl Viro 253579714f72SAl Viro /* does lookup, returns the object with parent locked */ 253679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 25375590ff0dSUlrich Drepper { 25385c31b6ceSAl Viro struct dentry *d; 2539391172c4SAl Viro struct qstr last; 2540*0ee50b47SDmitry Kadashev int type, error; 254151689104SPaul Moore 2542*0ee50b47SDmitry Kadashev error = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, 25435c31b6ceSAl Viro &last, &type); 2544*0ee50b47SDmitry Kadashev if (error) 2545*0ee50b47SDmitry Kadashev return ERR_PTR(error); 25465c31b6ceSAl Viro if (unlikely(type != LAST_NORM)) { 2547391172c4SAl Viro path_put(path); 25485c31b6ceSAl Viro return ERR_PTR(-EINVAL); 254979714f72SAl Viro } 25505955102cSAl Viro inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 2551391172c4SAl Viro d = __lookup_hash(&last, path->dentry, 0); 255279714f72SAl Viro if (IS_ERR(d)) { 25535955102cSAl Viro inode_unlock(path->dentry->d_inode); 2554391172c4SAl Viro path_put(path); 255579714f72SAl Viro } 255679714f72SAl Viro return d; 25575590ff0dSUlrich Drepper } 25585590ff0dSUlrich Drepper 2559d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2560d1811465SAl Viro { 2561abc9f5beSAl Viro return filename_lookup(AT_FDCWD, getname_kernel(name), 2562abc9f5beSAl Viro flags, path, NULL); 2563d1811465SAl Viro } 25644d359507SAl Viro EXPORT_SYMBOL(kern_path); 2565d1811465SAl Viro 256616f18200SJosef 'Jeff' Sipek /** 256716f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 256816f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 256916f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 257016f18200SJosef 'Jeff' Sipek * @name: pointer to file name 257116f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2572e0a01249SAl Viro * @path: pointer to struct path to fill 257316f18200SJosef 'Jeff' Sipek */ 257416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 257516f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2576e0a01249SAl Viro struct path *path) 257716f18200SJosef 'Jeff' Sipek { 25789ad1aaa6SAl Viro struct path root = {.mnt = mnt, .dentry = dentry}; 25799ad1aaa6SAl Viro /* the first argument of filename_lookup() is ignored with root */ 2580abc9f5beSAl Viro return filename_lookup(AT_FDCWD, getname_kernel(name), 2581abc9f5beSAl Viro flags , path, &root); 258216f18200SJosef 'Jeff' Sipek } 25834d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup); 258416f18200SJosef 'Jeff' Sipek 25853c95f0dcSAl Viro static int lookup_one_len_common(const char *name, struct dentry *base, 25863c95f0dcSAl Viro int len, struct qstr *this) 25873c95f0dcSAl Viro { 25883c95f0dcSAl Viro this->name = name; 25893c95f0dcSAl Viro this->len = len; 25903c95f0dcSAl Viro this->hash = full_name_hash(base, name, len); 25913c95f0dcSAl Viro if (!len) 25923c95f0dcSAl Viro return -EACCES; 25933c95f0dcSAl Viro 25943c95f0dcSAl Viro if (unlikely(name[0] == '.')) { 25953c95f0dcSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 25963c95f0dcSAl Viro return -EACCES; 25973c95f0dcSAl Viro } 25983c95f0dcSAl Viro 25993c95f0dcSAl Viro while (len--) { 26003c95f0dcSAl Viro unsigned int c = *(const unsigned char *)name++; 26013c95f0dcSAl Viro if (c == '/' || c == '\0') 26023c95f0dcSAl Viro return -EACCES; 26033c95f0dcSAl Viro } 26043c95f0dcSAl Viro /* 26053c95f0dcSAl Viro * See if the low-level filesystem might want 26063c95f0dcSAl Viro * to use its own hash.. 26073c95f0dcSAl Viro */ 26083c95f0dcSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 26093c95f0dcSAl Viro int err = base->d_op->d_hash(base, this); 26103c95f0dcSAl Viro if (err < 0) 26113c95f0dcSAl Viro return err; 26123c95f0dcSAl Viro } 26133c95f0dcSAl Viro 261447291baaSChristian Brauner return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC); 26153c95f0dcSAl Viro } 26163c95f0dcSAl Viro 2617eead1911SChristoph Hellwig /** 26180da0b7fdSDavid Howells * try_lookup_one_len - filesystem helper to lookup single pathname component 26190da0b7fdSDavid Howells * @name: pathname component to lookup 26200da0b7fdSDavid Howells * @base: base directory to lookup from 26210da0b7fdSDavid Howells * @len: maximum length @len should be interpreted to 26220da0b7fdSDavid Howells * 26230da0b7fdSDavid Howells * Look up a dentry by name in the dcache, returning NULL if it does not 26240da0b7fdSDavid Howells * currently exist. The function does not try to create a dentry. 26250da0b7fdSDavid Howells * 26260da0b7fdSDavid Howells * Note that this routine is purely a helper for filesystem usage and should 26270da0b7fdSDavid Howells * not be called by generic code. 26280da0b7fdSDavid Howells * 26290da0b7fdSDavid Howells * The caller must hold base->i_mutex. 26300da0b7fdSDavid Howells */ 26310da0b7fdSDavid Howells struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) 26320da0b7fdSDavid Howells { 26330da0b7fdSDavid Howells struct qstr this; 26340da0b7fdSDavid Howells int err; 26350da0b7fdSDavid Howells 26360da0b7fdSDavid Howells WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 26370da0b7fdSDavid Howells 26380da0b7fdSDavid Howells err = lookup_one_len_common(name, base, len, &this); 26390da0b7fdSDavid Howells if (err) 26400da0b7fdSDavid Howells return ERR_PTR(err); 26410da0b7fdSDavid Howells 26420da0b7fdSDavid Howells return lookup_dcache(&this, base, 0); 26430da0b7fdSDavid Howells } 26440da0b7fdSDavid Howells EXPORT_SYMBOL(try_lookup_one_len); 26450da0b7fdSDavid Howells 26460da0b7fdSDavid Howells /** 2647a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2648eead1911SChristoph Hellwig * @name: pathname component to lookup 2649eead1911SChristoph Hellwig * @base: base directory to lookup from 2650eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2651eead1911SChristoph Hellwig * 2652a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 26539e7543e9SAl Viro * not be called by generic code. 2654bbddca8eSNeilBrown * 2655bbddca8eSNeilBrown * The caller must hold base->i_mutex. 2656eead1911SChristoph Hellwig */ 2657057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2658057f6c01SJames Morris { 26598613a209SAl Viro struct dentry *dentry; 2660057f6c01SJames Morris struct qstr this; 2661cda309deSMiklos Szeredi int err; 2662057f6c01SJames Morris 26635955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 26642f9092e1SDavid Woodhouse 26653c95f0dcSAl Viro err = lookup_one_len_common(name, base, len, &this); 2666cda309deSMiklos Szeredi if (err) 2667cda309deSMiklos Szeredi return ERR_PTR(err); 2668cda309deSMiklos Szeredi 26698613a209SAl Viro dentry = lookup_dcache(&this, base, 0); 26708613a209SAl Viro return dentry ? dentry : __lookup_slow(&this, base, 0); 2671057f6c01SJames Morris } 26724d359507SAl Viro EXPORT_SYMBOL(lookup_one_len); 2673057f6c01SJames Morris 2674bbddca8eSNeilBrown /** 2675bbddca8eSNeilBrown * lookup_one_len_unlocked - filesystem helper to lookup single pathname component 2676bbddca8eSNeilBrown * @name: pathname component to lookup 2677bbddca8eSNeilBrown * @base: base directory to lookup from 2678bbddca8eSNeilBrown * @len: maximum length @len should be interpreted to 2679bbddca8eSNeilBrown * 2680bbddca8eSNeilBrown * Note that this routine is purely a helper for filesystem usage and should 2681bbddca8eSNeilBrown * not be called by generic code. 2682bbddca8eSNeilBrown * 2683bbddca8eSNeilBrown * Unlike lookup_one_len, it should be called without the parent 2684bbddca8eSNeilBrown * i_mutex held, and will take the i_mutex itself if necessary. 2685bbddca8eSNeilBrown */ 2686bbddca8eSNeilBrown struct dentry *lookup_one_len_unlocked(const char *name, 2687bbddca8eSNeilBrown struct dentry *base, int len) 2688bbddca8eSNeilBrown { 2689bbddca8eSNeilBrown struct qstr this; 2690bbddca8eSNeilBrown int err; 269120d00ee8SLinus Torvalds struct dentry *ret; 2692bbddca8eSNeilBrown 26933c95f0dcSAl Viro err = lookup_one_len_common(name, base, len, &this); 2694bbddca8eSNeilBrown if (err) 2695bbddca8eSNeilBrown return ERR_PTR(err); 2696bbddca8eSNeilBrown 269720d00ee8SLinus Torvalds ret = lookup_dcache(&this, base, 0); 269820d00ee8SLinus Torvalds if (!ret) 269920d00ee8SLinus Torvalds ret = lookup_slow(&this, base, 0); 270020d00ee8SLinus Torvalds return ret; 2701bbddca8eSNeilBrown } 2702bbddca8eSNeilBrown EXPORT_SYMBOL(lookup_one_len_unlocked); 2703bbddca8eSNeilBrown 27046c2d4798SAl Viro /* 27056c2d4798SAl Viro * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) 27066c2d4798SAl Viro * on negatives. Returns known positive or ERR_PTR(); that's what 27076c2d4798SAl Viro * most of the users want. Note that pinned negative with unlocked parent 27086c2d4798SAl Viro * _can_ become positive at any time, so callers of lookup_one_len_unlocked() 27096c2d4798SAl Viro * need to be very careful; pinned positives have ->d_inode stable, so 27106c2d4798SAl Viro * this one avoids such problems. 27116c2d4798SAl Viro */ 27126c2d4798SAl Viro struct dentry *lookup_positive_unlocked(const char *name, 27136c2d4798SAl Viro struct dentry *base, int len) 27146c2d4798SAl Viro { 27156c2d4798SAl Viro struct dentry *ret = lookup_one_len_unlocked(name, base, len); 27162fa6b1e0SAl Viro if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { 27176c2d4798SAl Viro dput(ret); 27186c2d4798SAl Viro ret = ERR_PTR(-ENOENT); 27196c2d4798SAl Viro } 27206c2d4798SAl Viro return ret; 27216c2d4798SAl Viro } 27226c2d4798SAl Viro EXPORT_SYMBOL(lookup_positive_unlocked); 27236c2d4798SAl Viro 2724eedf265aSEric W. Biederman #ifdef CONFIG_UNIX98_PTYS 2725eedf265aSEric W. Biederman int path_pts(struct path *path) 2726eedf265aSEric W. Biederman { 2727eedf265aSEric W. Biederman /* Find something mounted on "pts" in the same directory as 2728eedf265aSEric W. Biederman * the input path. 2729eedf265aSEric W. Biederman */ 2730a6a7eb76SAl Viro struct dentry *parent = dget_parent(path->dentry); 2731a6a7eb76SAl Viro struct dentry *child; 273219f6028aSAl Viro struct qstr this = QSTR_INIT("pts", 3); 2733eedf265aSEric W. Biederman 2734a6a7eb76SAl Viro if (unlikely(!path_connected(path->mnt, parent))) { 2735a6a7eb76SAl Viro dput(parent); 273663b27720SAl Viro return -ENOENT; 2737a6a7eb76SAl Viro } 273863b27720SAl Viro dput(path->dentry); 273963b27720SAl Viro path->dentry = parent; 2740eedf265aSEric W. Biederman child = d_hash_and_lookup(parent, &this); 2741eedf265aSEric W. Biederman if (!child) 2742eedf265aSEric W. Biederman return -ENOENT; 2743eedf265aSEric W. Biederman 2744eedf265aSEric W. Biederman path->dentry = child; 2745eedf265aSEric W. Biederman dput(parent); 274619f6028aSAl Viro follow_down(path); 2747eedf265aSEric W. Biederman return 0; 2748eedf265aSEric W. Biederman } 2749eedf265aSEric W. Biederman #endif 2750eedf265aSEric W. Biederman 27511fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 27521fa1e7f6SAndy Whitcroft struct path *path, int *empty) 27531da177e4SLinus Torvalds { 2754abc9f5beSAl Viro return filename_lookup(dfd, getname_flags(name, flags, empty), 2755abc9f5beSAl Viro flags, path, NULL); 27561da177e4SLinus Torvalds } 2757b853a161SAl Viro EXPORT_SYMBOL(user_path_at_empty); 27581fa1e7f6SAndy Whitcroft 2759ba73d987SChristian Brauner int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir, 2760ba73d987SChristian Brauner struct inode *inode) 27611da177e4SLinus Torvalds { 27628e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2763da9592edSDavid Howells 2764ba73d987SChristian Brauner if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid)) 27651da177e4SLinus Torvalds return 0; 2766ba73d987SChristian Brauner if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid)) 27671da177e4SLinus Torvalds return 0; 2768ba73d987SChristian Brauner return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER); 27691da177e4SLinus Torvalds } 2770cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky); 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds /* 27731da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 27741da177e4SLinus Torvalds * whether the type of victim is right. 27751da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 27761da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 27771da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 27781da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 27791da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 27801da177e4SLinus Torvalds * a. be owner of dir, or 27811da177e4SLinus Torvalds * b. be owner of victim, or 27821da177e4SLinus Torvalds * c. have CAP_FOWNER capability 27831da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 27841da177e4SLinus Torvalds * links pointing to it. 27850bd23d09SEric W. Biederman * 7. If the victim has an unknown uid or gid we can't change the inode. 27860bd23d09SEric W. Biederman * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. 27870bd23d09SEric W. Biederman * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. 27880bd23d09SEric W. Biederman * 10. We can't remove a root or mountpoint. 27890bd23d09SEric W. Biederman * 11. We don't allow removal of NFS sillyrenamed files; it's handled by 27901da177e4SLinus Torvalds * nfs_async_unlink(). 27911da177e4SLinus Torvalds */ 2792ba73d987SChristian Brauner static int may_delete(struct user_namespace *mnt_userns, struct inode *dir, 2793ba73d987SChristian Brauner struct dentry *victim, bool isdir) 27941da177e4SLinus Torvalds { 279563afdfc7SDavid Howells struct inode *inode = d_backing_inode(victim); 27961da177e4SLinus Torvalds int error; 27971da177e4SLinus Torvalds 2798b18825a7SDavid Howells if (d_is_negative(victim)) 27991da177e4SLinus Torvalds return -ENOENT; 2800b18825a7SDavid Howells BUG_ON(!inode); 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 2803593d1ce8SEric W. Biederman 2804593d1ce8SEric W. Biederman /* Inode writeback is not safe when the uid or gid are invalid. */ 2805ba73d987SChristian Brauner if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || 2806ba73d987SChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 2807593d1ce8SEric W. Biederman return -EOVERFLOW; 2808593d1ce8SEric W. Biederman 28094fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 28101da177e4SLinus Torvalds 2811ba73d987SChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 28121da177e4SLinus Torvalds if (error) 28131da177e4SLinus Torvalds return error; 28141da177e4SLinus Torvalds if (IS_APPEND(dir)) 28151da177e4SLinus Torvalds return -EPERM; 2816b18825a7SDavid Howells 2817ba73d987SChristian Brauner if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) || 2818ba73d987SChristian Brauner IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || 2819ba73d987SChristian Brauner HAS_UNMAPPED_ID(mnt_userns, inode)) 28201da177e4SLinus Torvalds return -EPERM; 28211da177e4SLinus Torvalds if (isdir) { 282244b1d530SMiklos Szeredi if (!d_is_dir(victim)) 28231da177e4SLinus Torvalds return -ENOTDIR; 28241da177e4SLinus Torvalds if (IS_ROOT(victim)) 28251da177e4SLinus Torvalds return -EBUSY; 282644b1d530SMiklos Szeredi } else if (d_is_dir(victim)) 28271da177e4SLinus Torvalds return -EISDIR; 28281da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 28291da177e4SLinus Torvalds return -ENOENT; 28301da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 28311da177e4SLinus Torvalds return -EBUSY; 28321da177e4SLinus Torvalds return 0; 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 28361da177e4SLinus Torvalds * dir. 28371da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 28381da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 28391da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 2840036d5236SEric W. Biederman * 3. We can't do it if the fs can't represent the fsuid or fsgid. 2841036d5236SEric W. Biederman * 4. We should have write and exec permissions on dir 2842036d5236SEric W. Biederman * 5. We can't do it if dir is immutable (done in permission()) 28431da177e4SLinus Torvalds */ 2844ba73d987SChristian Brauner static inline int may_create(struct user_namespace *mnt_userns, 2845ba73d987SChristian Brauner struct inode *dir, struct dentry *child) 28461da177e4SLinus Torvalds { 284714e972b4SJeff Layton audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); 28481da177e4SLinus Torvalds if (child->d_inode) 28491da177e4SLinus Torvalds return -EEXIST; 28501da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 28511da177e4SLinus Torvalds return -ENOENT; 28528e538913SChristian Brauner if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) 2853036d5236SEric W. Biederman return -EOVERFLOW; 28548e538913SChristian Brauner 2855ba73d987SChristian Brauner return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 28561da177e4SLinus Torvalds } 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds /* 28591da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 28601da177e4SLinus Torvalds */ 28611da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 28621da177e4SLinus Torvalds { 28631da177e4SLinus Torvalds struct dentry *p; 28641da177e4SLinus Torvalds 28651da177e4SLinus Torvalds if (p1 == p2) { 28665955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 28671da177e4SLinus Torvalds return NULL; 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds 2870fc64005cSAl Viro mutex_lock(&p1->d_sb->s_vfs_rename_mutex); 28711da177e4SLinus Torvalds 2872e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2873e2761a11SOGAWA Hirofumi if (p) { 28745955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); 28755955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); 28761da177e4SLinus Torvalds return p; 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds 2879e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2880e2761a11SOGAWA Hirofumi if (p) { 28815955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 28825955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); 28831da177e4SLinus Torvalds return p; 28841da177e4SLinus Torvalds } 28851da177e4SLinus Torvalds 28865955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 28875955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); 28881da177e4SLinus Torvalds return NULL; 28891da177e4SLinus Torvalds } 28904d359507SAl Viro EXPORT_SYMBOL(lock_rename); 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 28931da177e4SLinus Torvalds { 28945955102cSAl Viro inode_unlock(p1->d_inode); 28951da177e4SLinus Torvalds if (p1 != p2) { 28965955102cSAl Viro inode_unlock(p2->d_inode); 2897fc64005cSAl Viro mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds } 29004d359507SAl Viro EXPORT_SYMBOL(unlock_rename); 29011da177e4SLinus Torvalds 29026521f891SChristian Brauner /** 29036521f891SChristian Brauner * vfs_create - create new file 29046521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 29056521f891SChristian Brauner * @dir: inode of @dentry 29066521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 29076521f891SChristian Brauner * @mode: mode of the new file 29086521f891SChristian Brauner * @want_excl: whether the file must not yet exist 29096521f891SChristian Brauner * 29106521f891SChristian Brauner * Create a new file. 29116521f891SChristian Brauner * 29126521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 29136521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 29146521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 29156521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 29166521f891SChristian Brauner * raw inode simply passs init_user_ns. 29176521f891SChristian Brauner */ 29186521f891SChristian Brauner int vfs_create(struct user_namespace *mnt_userns, struct inode *dir, 29196521f891SChristian Brauner struct dentry *dentry, umode_t mode, bool want_excl) 29201da177e4SLinus Torvalds { 29216521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 29221da177e4SLinus Torvalds if (error) 29231da177e4SLinus Torvalds return error; 29241da177e4SLinus Torvalds 2925acfa4380SAl Viro if (!dir->i_op->create) 29261da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 29271da177e4SLinus Torvalds mode &= S_IALLUGO; 29281da177e4SLinus Torvalds mode |= S_IFREG; 29291da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 29301da177e4SLinus Torvalds if (error) 29311da177e4SLinus Torvalds return error; 2932549c7297SChristian Brauner error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl); 2933a74574aaSStephen Smalley if (!error) 2934f38aa942SAmy Griffis fsnotify_create(dir, dentry); 29351da177e4SLinus Torvalds return error; 29361da177e4SLinus Torvalds } 29374d359507SAl Viro EXPORT_SYMBOL(vfs_create); 29381da177e4SLinus Torvalds 29398e6c848eSAl Viro int vfs_mkobj(struct dentry *dentry, umode_t mode, 29408e6c848eSAl Viro int (*f)(struct dentry *, umode_t, void *), 29418e6c848eSAl Viro void *arg) 29428e6c848eSAl Viro { 29438e6c848eSAl Viro struct inode *dir = dentry->d_parent->d_inode; 2944ba73d987SChristian Brauner int error = may_create(&init_user_ns, dir, dentry); 29458e6c848eSAl Viro if (error) 29468e6c848eSAl Viro return error; 29478e6c848eSAl Viro 29488e6c848eSAl Viro mode &= S_IALLUGO; 29498e6c848eSAl Viro mode |= S_IFREG; 29508e6c848eSAl Viro error = security_inode_create(dir, dentry, mode); 29518e6c848eSAl Viro if (error) 29528e6c848eSAl Viro return error; 29538e6c848eSAl Viro error = f(dentry, mode, arg); 29548e6c848eSAl Viro if (!error) 29558e6c848eSAl Viro fsnotify_create(dir, dentry); 29568e6c848eSAl Viro return error; 29578e6c848eSAl Viro } 29588e6c848eSAl Viro EXPORT_SYMBOL(vfs_mkobj); 29598e6c848eSAl Viro 2960a2982cc9SEric W. Biederman bool may_open_dev(const struct path *path) 2961a2982cc9SEric W. Biederman { 2962a2982cc9SEric W. Biederman return !(path->mnt->mnt_flags & MNT_NODEV) && 2963a2982cc9SEric W. Biederman !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); 2964a2982cc9SEric W. Biederman } 2965a2982cc9SEric W. Biederman 2966ba73d987SChristian Brauner static int may_open(struct user_namespace *mnt_userns, const struct path *path, 2967ba73d987SChristian Brauner int acc_mode, int flag) 29681da177e4SLinus Torvalds { 29693fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 29701da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 29711da177e4SLinus Torvalds int error; 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds if (!inode) 29741da177e4SLinus Torvalds return -ENOENT; 29751da177e4SLinus Torvalds 2976c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2977c8fe8f30SChristoph Hellwig case S_IFLNK: 29781da177e4SLinus Torvalds return -ELOOP; 2979c8fe8f30SChristoph Hellwig case S_IFDIR: 2980fc4177beSKees Cook if (acc_mode & MAY_WRITE) 29811da177e4SLinus Torvalds return -EISDIR; 2982fc4177beSKees Cook if (acc_mode & MAY_EXEC) 2983fc4177beSKees Cook return -EACCES; 2984c8fe8f30SChristoph Hellwig break; 2985c8fe8f30SChristoph Hellwig case S_IFBLK: 2986c8fe8f30SChristoph Hellwig case S_IFCHR: 2987a2982cc9SEric W. Biederman if (!may_open_dev(path)) 29881da177e4SLinus Torvalds return -EACCES; 2989633fb6acSKees Cook fallthrough; 2990c8fe8f30SChristoph Hellwig case S_IFIFO: 2991c8fe8f30SChristoph Hellwig case S_IFSOCK: 2992633fb6acSKees Cook if (acc_mode & MAY_EXEC) 2993633fb6acSKees Cook return -EACCES; 29941da177e4SLinus Torvalds flag &= ~O_TRUNC; 2995c8fe8f30SChristoph Hellwig break; 29960fd338b2SKees Cook case S_IFREG: 29970fd338b2SKees Cook if ((acc_mode & MAY_EXEC) && path_noexec(path)) 29980fd338b2SKees Cook return -EACCES; 29990fd338b2SKees Cook break; 30004a3fd211SDave Hansen } 3001b41572e9SDave Hansen 3002ba73d987SChristian Brauner error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode); 3003b41572e9SDave Hansen if (error) 3004b41572e9SDave Hansen return error; 30056146f0d5SMimi Zohar 30061da177e4SLinus Torvalds /* 30071da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 30081da177e4SLinus Torvalds */ 30091da177e4SLinus Torvalds if (IS_APPEND(inode)) { 30108737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 30117715b521SAl Viro return -EPERM; 30121da177e4SLinus Torvalds if (flag & O_TRUNC) 30137715b521SAl Viro return -EPERM; 30141da177e4SLinus Torvalds } 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 3017ba73d987SChristian Brauner if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode)) 30187715b521SAl Viro return -EPERM; 30191da177e4SLinus Torvalds 3020f3c7691eSJ. Bruce Fields return 0; 30217715b521SAl Viro } 30227715b521SAl Viro 3023549c7297SChristian Brauner static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) 30247715b521SAl Viro { 3025f0bb5aafSAl Viro const struct path *path = &filp->f_path; 30267715b521SAl Viro struct inode *inode = path->dentry->d_inode; 30277715b521SAl Viro int error = get_write_access(inode); 30281da177e4SLinus Torvalds if (error) 30297715b521SAl Viro return error; 30301da177e4SLinus Torvalds /* 30311da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 30321da177e4SLinus Torvalds */ 3033d7a06983SJeff Layton error = locks_verify_locked(filp); 3034be6d3e56SKentaro Takeda if (!error) 3035ea0d3ab2STetsuo Handa error = security_path_truncate(path); 30361da177e4SLinus Torvalds if (!error) { 3037549c7297SChristian Brauner error = do_truncate(mnt_userns, path->dentry, 0, 3038d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 3039e1181ee6SJeff Layton filp); 30401da177e4SLinus Torvalds } 30411da177e4SLinus Torvalds put_write_access(inode); 3042acd0c935SMimi Zohar return error; 30431da177e4SLinus Torvalds } 30441da177e4SLinus Torvalds 3045d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 3046d57999e1SDave Hansen { 30478a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 30488a5e929dSAl Viro flag--; 3049d57999e1SDave Hansen return flag; 3050d57999e1SDave Hansen } 3051d57999e1SDave Hansen 3052ba73d987SChristian Brauner static int may_o_create(struct user_namespace *mnt_userns, 3053ba73d987SChristian Brauner const struct path *dir, struct dentry *dentry, 3054ba73d987SChristian Brauner umode_t mode) 3055d18e9008SMiklos Szeredi { 3056d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 3057d18e9008SMiklos Szeredi if (error) 3058d18e9008SMiklos Szeredi return error; 3059d18e9008SMiklos Szeredi 30608e538913SChristian Brauner if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns)) 30611328c727SSeth Forshee return -EOVERFLOW; 30621328c727SSeth Forshee 3063ba73d987SChristian Brauner error = inode_permission(mnt_userns, dir->dentry->d_inode, 306447291baaSChristian Brauner MAY_WRITE | MAY_EXEC); 3065d18e9008SMiklos Szeredi if (error) 3066d18e9008SMiklos Szeredi return error; 3067d18e9008SMiklos Szeredi 3068d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 3069d18e9008SMiklos Szeredi } 3070d18e9008SMiklos Szeredi 30711acf0af9SDavid Howells /* 30721acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 30731acf0af9SDavid Howells * dentry. 30741acf0af9SDavid Howells * 30751acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 30761acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 30771acf0af9SDavid Howells * 307800a07c15SAl Viro * If the file was looked up only or didn't need creating, FMODE_OPENED won't 307900a07c15SAl Viro * be set. The caller will need to perform the open themselves. @path will 308000a07c15SAl Viro * have been updated to point to the new dentry. This may be negative. 30811acf0af9SDavid Howells * 30821acf0af9SDavid Howells * Returns an error code otherwise. 30831acf0af9SDavid Howells */ 3084239eb983SAl Viro static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, 3085239eb983SAl Viro struct file *file, 30863ec2eef1SAl Viro int open_flag, umode_t mode) 3087d18e9008SMiklos Szeredi { 3088d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 3089d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 3090d18e9008SMiklos Szeredi int error; 3091d18e9008SMiklos Szeredi 3092d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 3093d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 3094d18e9008SMiklos Szeredi 309530d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 309630d90494SAl Viro file->f_path.mnt = nd->path.mnt; 30970fb1ea09SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, 309844907d79SAl Viro open_to_namei_flags(open_flag), mode); 30996fbd0714SAl Viro d_lookup_done(dentry); 3100384f26e2SAl Viro if (!error) { 310164e1ac4dSAl Viro if (file->f_mode & FMODE_OPENED) { 31026fb968cdSAl Viro if (unlikely(dentry != file->f_path.dentry)) { 31036fb968cdSAl Viro dput(dentry); 31046fb968cdSAl Viro dentry = dget(file->f_path.dentry); 31056fb968cdSAl Viro } 310664e1ac4dSAl Viro } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 31072675a4ebSAl Viro error = -EIO; 3108384f26e2SAl Viro } else { 310930d90494SAl Viro if (file->f_path.dentry) { 3110d18e9008SMiklos Szeredi dput(dentry); 3111d18e9008SMiklos Szeredi dentry = file->f_path.dentry; 311210c64ceaSAl Viro } 3113239eb983SAl Viro if (unlikely(d_is_negative(dentry))) 3114a01e718fSAl Viro error = -ENOENT; 3115d18e9008SMiklos Szeredi } 3116d18e9008SMiklos Szeredi } 3117239eb983SAl Viro if (error) { 3118d18e9008SMiklos Szeredi dput(dentry); 3119239eb983SAl Viro dentry = ERR_PTR(error); 3120239eb983SAl Viro } 3121239eb983SAl Viro return dentry; 3122d18e9008SMiklos Szeredi } 3123d18e9008SMiklos Szeredi 312431e6b01fSNick Piggin /* 31251acf0af9SDavid Howells * Look up and maybe create and open the last component. 3126d58ffd35SMiklos Szeredi * 312700a07c15SAl Viro * Must be called with parent locked (exclusive in O_CREAT case). 3128d58ffd35SMiklos Szeredi * 312900a07c15SAl Viro * Returns 0 on success, that is, if 313000a07c15SAl Viro * the file was successfully atomically created (if necessary) and opened, or 313100a07c15SAl Viro * the file was not completely opened at this time, though lookups and 313200a07c15SAl Viro * creations were performed. 313300a07c15SAl Viro * These case are distinguished by presence of FMODE_OPENED on file->f_mode. 313400a07c15SAl Viro * In the latter case dentry returned in @path might be negative if O_CREAT 313500a07c15SAl Viro * hadn't been specified. 31361acf0af9SDavid Howells * 313700a07c15SAl Viro * An error code is returned on failure. 3138d58ffd35SMiklos Szeredi */ 3139da5ebf5aSAl Viro static struct dentry *lookup_open(struct nameidata *nd, struct file *file, 3140d58ffd35SMiklos Szeredi const struct open_flags *op, 31413ec2eef1SAl Viro bool got_write) 3142d58ffd35SMiklos Szeredi { 3143549c7297SChristian Brauner struct user_namespace *mnt_userns; 3144d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 314554ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 31461643b43fSAl Viro int open_flag = op->open_flag; 3147d58ffd35SMiklos Szeredi struct dentry *dentry; 31481643b43fSAl Viro int error, create_error = 0; 31491643b43fSAl Viro umode_t mode = op->mode; 31506fbd0714SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 3151d58ffd35SMiklos Szeredi 3152ce8644fcSAl Viro if (unlikely(IS_DEADDIR(dir_inode))) 3153da5ebf5aSAl Viro return ERR_PTR(-ENOENT); 3154d58ffd35SMiklos Szeredi 315573a09dd9SAl Viro file->f_mode &= ~FMODE_CREATED; 31566fbd0714SAl Viro dentry = d_lookup(dir, &nd->last); 31576fbd0714SAl Viro for (;;) { 31586fbd0714SAl Viro if (!dentry) { 31596fbd0714SAl Viro dentry = d_alloc_parallel(dir, &nd->last, &wq); 3160d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 3161da5ebf5aSAl Viro return dentry; 31626fbd0714SAl Viro } 31636fbd0714SAl Viro if (d_in_lookup(dentry)) 31646fbd0714SAl Viro break; 3165d58ffd35SMiklos Szeredi 31666fbd0714SAl Viro error = d_revalidate(dentry, nd->flags); 31676fbd0714SAl Viro if (likely(error > 0)) 31686fbd0714SAl Viro break; 31696fbd0714SAl Viro if (error) 31706fbd0714SAl Viro goto out_dput; 31716fbd0714SAl Viro d_invalidate(dentry); 31726fbd0714SAl Viro dput(dentry); 31736fbd0714SAl Viro dentry = NULL; 31746fbd0714SAl Viro } 31756fbd0714SAl Viro if (dentry->d_inode) { 3176d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 3177da5ebf5aSAl Viro return dentry; 31786c51e513SAl Viro } 3179d18e9008SMiklos Szeredi 31801643b43fSAl Viro /* 31811643b43fSAl Viro * Checking write permission is tricky, bacuse we don't know if we are 31821643b43fSAl Viro * going to actually need it: O_CREAT opens should work as long as the 31831643b43fSAl Viro * file exists. But checking existence breaks atomicity. The trick is 31841643b43fSAl Viro * to check access and if not granted clear O_CREAT from the flags. 31851643b43fSAl Viro * 31861643b43fSAl Viro * Another problem is returing the "right" error value (e.g. for an 31871643b43fSAl Viro * O_EXCL open we want to return EEXIST not EROFS). 31881643b43fSAl Viro */ 318999a4a90cSAl Viro if (unlikely(!got_write)) 319099a4a90cSAl Viro open_flag &= ~O_TRUNC; 3191549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 31921643b43fSAl Viro if (open_flag & O_CREAT) { 319399a4a90cSAl Viro if (open_flag & O_EXCL) 319499a4a90cSAl Viro open_flag &= ~O_TRUNC; 31951643b43fSAl Viro if (!IS_POSIXACL(dir->d_inode)) 31961643b43fSAl Viro mode &= ~current_umask(); 319799a4a90cSAl Viro if (likely(got_write)) 3198549c7297SChristian Brauner create_error = may_o_create(mnt_userns, &nd->path, 3199ba73d987SChristian Brauner dentry, mode); 320099a4a90cSAl Viro else 320199a4a90cSAl Viro create_error = -EROFS; 320299a4a90cSAl Viro } 320399a4a90cSAl Viro if (create_error) 32041643b43fSAl Viro open_flag &= ~O_CREAT; 32051643b43fSAl Viro if (dir_inode->i_op->atomic_open) { 3206d489cf9aSAl Viro dentry = atomic_open(nd, dentry, file, open_flag, mode); 3207da5ebf5aSAl Viro if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) 3208da5ebf5aSAl Viro dentry = ERR_PTR(create_error); 3209da5ebf5aSAl Viro return dentry; 3210239eb983SAl Viro } 321154ef4872SMiklos Szeredi 32126fbd0714SAl Viro if (d_in_lookup(dentry)) { 321312fa5e24SAl Viro struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, 321412fa5e24SAl Viro nd->flags); 32156fbd0714SAl Viro d_lookup_done(dentry); 321612fa5e24SAl Viro if (unlikely(res)) { 321712fa5e24SAl Viro if (IS_ERR(res)) { 321812fa5e24SAl Viro error = PTR_ERR(res); 321912fa5e24SAl Viro goto out_dput; 322012fa5e24SAl Viro } 322112fa5e24SAl Viro dput(dentry); 322212fa5e24SAl Viro dentry = res; 322312fa5e24SAl Viro } 322454ef4872SMiklos Szeredi } 322554ef4872SMiklos Szeredi 3226d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 32271643b43fSAl Viro if (!dentry->d_inode && (open_flag & O_CREAT)) { 322873a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 3229ce8644fcSAl Viro audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); 3230ce8644fcSAl Viro if (!dir_inode->i_op->create) { 3231ce8644fcSAl Viro error = -EACCES; 3232d58ffd35SMiklos Szeredi goto out_dput; 323364894cf8SAl Viro } 3234549c7297SChristian Brauner 3235549c7297SChristian Brauner error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry, 3236549c7297SChristian Brauner mode, open_flag & O_EXCL); 3237d58ffd35SMiklos Szeredi if (error) 3238d58ffd35SMiklos Szeredi goto out_dput; 3239d58ffd35SMiklos Szeredi } 32401643b43fSAl Viro if (unlikely(create_error) && !dentry->d_inode) { 32411643b43fSAl Viro error = create_error; 3242d58ffd35SMiklos Szeredi goto out_dput; 3243d58ffd35SMiklos Szeredi } 3244da5ebf5aSAl Viro return dentry; 3245d58ffd35SMiklos Szeredi 3246d58ffd35SMiklos Szeredi out_dput: 3247d58ffd35SMiklos Szeredi dput(dentry); 3248da5ebf5aSAl Viro return ERR_PTR(error); 3249d58ffd35SMiklos Szeredi } 3250d58ffd35SMiklos Szeredi 3251c981a482SAl Viro static const char *open_last_lookups(struct nameidata *nd, 32523ec2eef1SAl Viro struct file *file, const struct open_flags *op) 3253fb1cc555SAl Viro { 3254a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 3255ca344a89SAl Viro int open_flag = op->open_flag; 325664894cf8SAl Viro bool got_write = false; 3257254cf582SAl Viro unsigned seq; 3258a1eb3315SMiklos Szeredi struct inode *inode; 3259da5ebf5aSAl Viro struct dentry *dentry; 3260b0417d2cSAl Viro const char *res; 3261fb1cc555SAl Viro 3262c3e380b0SAl Viro nd->flags |= op->intent; 3263c3e380b0SAl Viro 3264bc77daa7SAl Viro if (nd->last_type != LAST_NORM) { 326556676ec3SAl Viro if (nd->depth) 326656676ec3SAl Viro put_link(nd); 3267ff326a32SAl Viro return handle_dots(nd, nd->last_type); 32681f36f774SAl Viro } 3269a2c36b45SAl Viro 3270ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 3271fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 3272fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 3273fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 327420e34357SAl Viro dentry = lookup_fast(nd, &inode, &seq); 327520e34357SAl Viro if (IS_ERR(dentry)) 32761ccac622SAl Viro return ERR_CAST(dentry); 327720e34357SAl Viro if (likely(dentry)) 327871574865SMiklos Szeredi goto finish_lookup; 327971574865SMiklos Szeredi 32806583fe22SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 3281b6183df7SMiklos Szeredi } else { 3282fe2d35ffSAl Viro /* create side of things */ 328372287417SAl Viro if (nd->flags & LOOKUP_RCU) { 3284e36cffedSJens Axboe if (!try_to_unlazy(nd)) 3285e36cffedSJens Axboe return ERR_PTR(-ECHILD); 328672287417SAl Viro } 3287c9b07eabSAl Viro audit_inode(nd->name, dir, AUDIT_INODE_PARENT); 32881f36f774SAl Viro /* trailing slashes? */ 3289deb106c6SAl Viro if (unlikely(nd->last.name[nd->last.len])) 32901ccac622SAl Viro return ERR_PTR(-EISDIR); 3291b6183df7SMiklos Szeredi } 32921f36f774SAl Viro 32939cf843e3SAl Viro if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 3294e36cffedSJens Axboe got_write = !mnt_want_write(nd->path.mnt); 329564894cf8SAl Viro /* 329664894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 329764894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 329864894cf8SAl Viro * dropping this one anyway. 329964894cf8SAl Viro */ 330064894cf8SAl Viro } 33019cf843e3SAl Viro if (open_flag & O_CREAT) 33025955102cSAl Viro inode_lock(dir->d_inode); 33039cf843e3SAl Viro else 33049cf843e3SAl Viro inode_lock_shared(dir->d_inode); 3305da5ebf5aSAl Viro dentry = lookup_open(nd, file, op, got_write); 3306f7bb959dSAl Viro if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) 3307f7bb959dSAl Viro fsnotify_create(dir->d_inode, dentry); 33089cf843e3SAl Viro if (open_flag & O_CREAT) 33095955102cSAl Viro inode_unlock(dir->d_inode); 33109cf843e3SAl Viro else 33119cf843e3SAl Viro inode_unlock_shared(dir->d_inode); 3312fb1cc555SAl Viro 3313c981a482SAl Viro if (got_write) 331459e96e65SAl Viro mnt_drop_write(nd->path.mnt); 33156c0d46c4SAl Viro 331659e96e65SAl Viro if (IS_ERR(dentry)) 331759e96e65SAl Viro return ERR_CAST(dentry); 331859e96e65SAl Viro 3319973d4b73SAl Viro if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { 3320e73cabffSAl Viro dput(nd->path.dentry); 3321e73cabffSAl Viro nd->path.dentry = dentry; 3322c981a482SAl Viro return NULL; 3323fb1cc555SAl Viro } 3324fb1cc555SAl Viro 332520e34357SAl Viro finish_lookup: 332656676ec3SAl Viro if (nd->depth) 332756676ec3SAl Viro put_link(nd); 33288c4efe22SAl Viro res = step_into(nd, WALK_TRAILING, dentry, inode, seq); 3329ff326a32SAl Viro if (unlikely(res)) 33301ccac622SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3331b0417d2cSAl Viro return res; 33321ccac622SAl Viro } 333331d1726dSAl Viro 3334c981a482SAl Viro /* 3335c981a482SAl Viro * Handle the last step of open() 3336c981a482SAl Viro */ 3337c5971b8cSAl Viro static int do_open(struct nameidata *nd, 3338c981a482SAl Viro struct file *file, const struct open_flags *op) 3339c981a482SAl Viro { 3340549c7297SChristian Brauner struct user_namespace *mnt_userns; 3341c981a482SAl Viro int open_flag = op->open_flag; 3342c981a482SAl Viro bool do_truncate; 3343c981a482SAl Viro int acc_mode; 3344c981a482SAl Viro int error; 3345c981a482SAl Viro 3346ff326a32SAl Viro if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { 3347ff326a32SAl Viro error = complete_walk(nd); 3348ff326a32SAl Viro if (error) 3349ff326a32SAl Viro return error; 3350ff326a32SAl Viro } 3351973d4b73SAl Viro if (!(file->f_mode & FMODE_CREATED)) 335276ae2a5aSAl Viro audit_inode(nd->name, nd->path.dentry, 0); 3353549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 335430aba665SSalvatore Mesoraca if (open_flag & O_CREAT) { 3355b94e0b32SAl Viro if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) 3356b94e0b32SAl Viro return -EEXIST; 335730aba665SSalvatore Mesoraca if (d_is_dir(nd->path.dentry)) 3358c5971b8cSAl Viro return -EISDIR; 3359549c7297SChristian Brauner error = may_create_in_sticky(mnt_userns, nd, 336030aba665SSalvatore Mesoraca d_backing_inode(nd->path.dentry)); 336130aba665SSalvatore Mesoraca if (unlikely(error)) 3362c5971b8cSAl Viro return error; 336330aba665SSalvatore Mesoraca } 336444b1d530SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) 3365c5971b8cSAl Viro return -ENOTDIR; 33666c0d46c4SAl Viro 33678795e7d4SAl Viro do_truncate = false; 33688795e7d4SAl Viro acc_mode = op->acc_mode; 33695a2d3eddSAl Viro if (file->f_mode & FMODE_CREATED) { 33705a2d3eddSAl Viro /* Don't check for write permission, don't truncate */ 33715a2d3eddSAl Viro open_flag &= ~O_TRUNC; 33725a2d3eddSAl Viro acc_mode = 0; 33738795e7d4SAl Viro } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { 33740f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 33750f9d1a10SAl Viro if (error) 3376c5971b8cSAl Viro return error; 33778795e7d4SAl Viro do_truncate = true; 33780f9d1a10SAl Viro } 3379549c7297SChristian Brauner error = may_open(mnt_userns, &nd->path, acc_mode, open_flag); 33808795e7d4SAl Viro if (!error && !(file->f_mode & FMODE_OPENED)) 3381ae2bb293SAl Viro error = vfs_open(&nd->path, file); 33828795e7d4SAl Viro if (!error) 33836035a27bSAl Viro error = ima_file_check(file, op->acc_mode); 33848795e7d4SAl Viro if (!error && do_truncate) 3385549c7297SChristian Brauner error = handle_truncate(mnt_userns, file); 3386c80567c8SAl Viro if (unlikely(error > 0)) { 3387c80567c8SAl Viro WARN_ON(1); 3388c80567c8SAl Viro error = -EINVAL; 3389c80567c8SAl Viro } 33908795e7d4SAl Viro if (do_truncate) 33910f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 3392c5971b8cSAl Viro return error; 3393fb1cc555SAl Viro } 3394fb1cc555SAl Viro 33956521f891SChristian Brauner /** 33966521f891SChristian Brauner * vfs_tmpfile - create tmpfile 33976521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 33986521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 33996521f891SChristian Brauner * @mode: mode of the new tmpfile 34002111c3c0SRandy Dunlap * @open_flag: flags 34016521f891SChristian Brauner * 34026521f891SChristian Brauner * Create a temporary file. 34036521f891SChristian Brauner * 34046521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 34056521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 34066521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 34076521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 34086521f891SChristian Brauner * raw inode simply passs init_user_ns. 34096521f891SChristian Brauner */ 34106521f891SChristian Brauner struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, 34116521f891SChristian Brauner struct dentry *dentry, umode_t mode, int open_flag) 3412af7bd4dcSAmir Goldstein { 3413af7bd4dcSAmir Goldstein struct dentry *child = NULL; 3414af7bd4dcSAmir Goldstein struct inode *dir = dentry->d_inode; 3415af7bd4dcSAmir Goldstein struct inode *inode; 3416af7bd4dcSAmir Goldstein int error; 3417af7bd4dcSAmir Goldstein 3418af7bd4dcSAmir Goldstein /* we want directory to be writable */ 34196521f891SChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 3420af7bd4dcSAmir Goldstein if (error) 3421af7bd4dcSAmir Goldstein goto out_err; 3422af7bd4dcSAmir Goldstein error = -EOPNOTSUPP; 3423af7bd4dcSAmir Goldstein if (!dir->i_op->tmpfile) 3424af7bd4dcSAmir Goldstein goto out_err; 3425af7bd4dcSAmir Goldstein error = -ENOMEM; 3426cdf01226SDavid Howells child = d_alloc(dentry, &slash_name); 3427af7bd4dcSAmir Goldstein if (unlikely(!child)) 3428af7bd4dcSAmir Goldstein goto out_err; 3429549c7297SChristian Brauner error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); 3430af7bd4dcSAmir Goldstein if (error) 3431af7bd4dcSAmir Goldstein goto out_err; 3432af7bd4dcSAmir Goldstein error = -ENOENT; 3433af7bd4dcSAmir Goldstein inode = child->d_inode; 3434af7bd4dcSAmir Goldstein if (unlikely(!inode)) 3435af7bd4dcSAmir Goldstein goto out_err; 3436af7bd4dcSAmir Goldstein if (!(open_flag & O_EXCL)) { 3437af7bd4dcSAmir Goldstein spin_lock(&inode->i_lock); 3438af7bd4dcSAmir Goldstein inode->i_state |= I_LINKABLE; 3439af7bd4dcSAmir Goldstein spin_unlock(&inode->i_lock); 3440af7bd4dcSAmir Goldstein } 3441a2d2329eSChristian Brauner ima_post_create_tmpfile(mnt_userns, inode); 3442af7bd4dcSAmir Goldstein return child; 3443af7bd4dcSAmir Goldstein 3444af7bd4dcSAmir Goldstein out_err: 3445af7bd4dcSAmir Goldstein dput(child); 3446af7bd4dcSAmir Goldstein return ERR_PTR(error); 3447af7bd4dcSAmir Goldstein } 3448af7bd4dcSAmir Goldstein EXPORT_SYMBOL(vfs_tmpfile); 3449af7bd4dcSAmir Goldstein 3450c8a53ee5SAl Viro static int do_tmpfile(struct nameidata *nd, unsigned flags, 345160545d0dSAl Viro const struct open_flags *op, 34523ec2eef1SAl Viro struct file *file) 345360545d0dSAl Viro { 34546521f891SChristian Brauner struct user_namespace *mnt_userns; 3455625b6d10SAl Viro struct dentry *child; 3456625b6d10SAl Viro struct path path; 3457c8a53ee5SAl Viro int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); 345860545d0dSAl Viro if (unlikely(error)) 345960545d0dSAl Viro return error; 3460625b6d10SAl Viro error = mnt_want_write(path.mnt); 346160545d0dSAl Viro if (unlikely(error)) 346260545d0dSAl Viro goto out; 34636521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 34646521f891SChristian Brauner child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); 3465af7bd4dcSAmir Goldstein error = PTR_ERR(child); 3466684e73beSHirofumi Nakagawa if (IS_ERR(child)) 346760545d0dSAl Viro goto out2; 3468625b6d10SAl Viro dput(path.dentry); 3469625b6d10SAl Viro path.dentry = child; 3470c8a53ee5SAl Viro audit_inode(nd->name, child, 0); 347169a91c23SEric Rannaud /* Don't check for other permissions, the inode was just created */ 3472549c7297SChristian Brauner error = may_open(mnt_userns, &path, 0, op->open_flag); 34731e8f44f1SAl Viro if (!error) 34741e8f44f1SAl Viro error = vfs_open(&path, file); 347560545d0dSAl Viro out2: 3476625b6d10SAl Viro mnt_drop_write(path.mnt); 347760545d0dSAl Viro out: 3478625b6d10SAl Viro path_put(&path); 347960545d0dSAl Viro return error; 348060545d0dSAl Viro } 348160545d0dSAl Viro 34826ac08709SAl Viro static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) 34836ac08709SAl Viro { 34846ac08709SAl Viro struct path path; 34856ac08709SAl Viro int error = path_lookupat(nd, flags, &path); 34866ac08709SAl Viro if (!error) { 34876ac08709SAl Viro audit_inode(nd->name, path.dentry, 0); 3488ae2bb293SAl Viro error = vfs_open(&path, file); 34896ac08709SAl Viro path_put(&path); 34906ac08709SAl Viro } 34916ac08709SAl Viro return error; 34926ac08709SAl Viro } 34936ac08709SAl Viro 3494c8a53ee5SAl Viro static struct file *path_openat(struct nameidata *nd, 3495c8a53ee5SAl Viro const struct open_flags *op, unsigned flags) 34961da177e4SLinus Torvalds { 349730d90494SAl Viro struct file *file; 349813aab428SAl Viro int error; 349931e6b01fSNick Piggin 3500ea73ea72SAl Viro file = alloc_empty_file(op->open_flag, current_cred()); 35011afc99beSAl Viro if (IS_ERR(file)) 35021afc99beSAl Viro return file; 350331e6b01fSNick Piggin 3504bb458c64SAl Viro if (unlikely(file->f_flags & __O_TMPFILE)) { 35053ec2eef1SAl Viro error = do_tmpfile(nd, flags, op, file); 35065f336e72SAl Viro } else if (unlikely(file->f_flags & O_PATH)) { 35076ac08709SAl Viro error = do_o_path(nd, flags, file); 35085f336e72SAl Viro } else { 35095f336e72SAl Viro const char *s = path_init(nd, flags); 35103bdba28bSAl Viro while (!(error = link_path_walk(s, nd)) && 3511c5971b8cSAl Viro (s = open_last_lookups(nd, file, op)) != NULL) 35121ccac622SAl Viro ; 3513c5971b8cSAl Viro if (!error) 3514c5971b8cSAl Viro error = do_open(nd, file, op); 3515deb106c6SAl Viro terminate_walk(nd); 35165f336e72SAl Viro } 35177c1c01ecSAl Viro if (likely(!error)) { 3518aad888f8SAl Viro if (likely(file->f_mode & FMODE_OPENED)) 35197c1c01ecSAl Viro return file; 35207c1c01ecSAl Viro WARN_ON(1); 35217c1c01ecSAl Viro error = -EINVAL; 3522015c3bbcSMiklos Szeredi } 35237c1c01ecSAl Viro fput(file); 35242675a4ebSAl Viro if (error == -EOPENSTALE) { 35252675a4ebSAl Viro if (flags & LOOKUP_RCU) 35262675a4ebSAl Viro error = -ECHILD; 35272675a4ebSAl Viro else 35282675a4ebSAl Viro error = -ESTALE; 35292675a4ebSAl Viro } 35307c1c01ecSAl Viro return ERR_PTR(error); 3531de459215SKirill Korotaev } 35321da177e4SLinus Torvalds 3533669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 3534f9652e10SAl Viro const struct open_flags *op) 353513aab428SAl Viro { 35369883d185SAl Viro struct nameidata nd; 3537f9652e10SAl Viro int flags = op->lookup_flags; 353813aab428SAl Viro struct file *filp; 353913aab428SAl Viro 354006422964SAl Viro set_nameidata(&nd, dfd, pathname, NULL); 3541c8a53ee5SAl Viro filp = path_openat(&nd, op, flags | LOOKUP_RCU); 354213aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 3543c8a53ee5SAl Viro filp = path_openat(&nd, op, flags); 354413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 3545c8a53ee5SAl Viro filp = path_openat(&nd, op, flags | LOOKUP_REVAL); 35469883d185SAl Viro restore_nameidata(); 354713aab428SAl Viro return filp; 354813aab428SAl Viro } 354913aab428SAl Viro 3550ffb37ca3SAl Viro struct file *do_file_open_root(const struct path *root, 3551f9652e10SAl Viro const char *name, const struct open_flags *op) 355273d049a4SAl Viro { 35539883d185SAl Viro struct nameidata nd; 355473d049a4SAl Viro struct file *file; 355551689104SPaul Moore struct filename *filename; 3556bcba1e7dSAl Viro int flags = op->lookup_flags; 355773d049a4SAl Viro 3558ffb37ca3SAl Viro if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) 355973d049a4SAl Viro return ERR_PTR(-ELOOP); 356073d049a4SAl Viro 356151689104SPaul Moore filename = getname_kernel(name); 3562a1c83681SViresh Kumar if (IS_ERR(filename)) 356351689104SPaul Moore return ERR_CAST(filename); 356451689104SPaul Moore 356506422964SAl Viro set_nameidata(&nd, -1, filename, root); 3566c8a53ee5SAl Viro file = path_openat(&nd, op, flags | LOOKUP_RCU); 356773d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3568c8a53ee5SAl Viro file = path_openat(&nd, op, flags); 356973d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3570c8a53ee5SAl Viro file = path_openat(&nd, op, flags | LOOKUP_REVAL); 35719883d185SAl Viro restore_nameidata(); 357251689104SPaul Moore putname(filename); 357373d049a4SAl Viro return file; 357473d049a4SAl Viro } 357573d049a4SAl Viro 3576fa14a0b8SAl Viro static struct dentry *filename_create(int dfd, struct filename *name, 35771ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 35781da177e4SLinus Torvalds { 3579c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3580391172c4SAl Viro struct qstr last; 3581391172c4SAl Viro int type; 3582c30dabfeSJan Kara int err2; 35831ac12b4bSJeff Layton int error; 35841ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 35851ac12b4bSJeff Layton 35861ac12b4bSJeff Layton /* 35871ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 35881ac12b4bSJeff Layton * other flags passed in are ignored! 35891ac12b4bSJeff Layton */ 35901ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 35911ac12b4bSJeff Layton 3592*0ee50b47SDmitry Kadashev error = filename_parentat(dfd, name, lookup_flags, path, &last, &type); 3593*0ee50b47SDmitry Kadashev if (error) 3594*0ee50b47SDmitry Kadashev return ERR_PTR(error); 35951da177e4SLinus Torvalds 3596c663e5d8SChristoph Hellwig /* 3597c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3598c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3599c663e5d8SChristoph Hellwig */ 36005c31b6ceSAl Viro if (unlikely(type != LAST_NORM)) 3601ed75e95dSAl Viro goto out; 3602c663e5d8SChristoph Hellwig 3603c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3604391172c4SAl Viro err2 = mnt_want_write(path->mnt); 3605c663e5d8SChristoph Hellwig /* 3606c663e5d8SChristoph Hellwig * Do the final lookup. 3607c663e5d8SChristoph Hellwig */ 3608391172c4SAl Viro lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; 36095955102cSAl Viro inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 3610391172c4SAl Viro dentry = __lookup_hash(&last, path->dentry, lookup_flags); 36111da177e4SLinus Torvalds if (IS_ERR(dentry)) 3612a8104a9fSAl Viro goto unlock; 3613c663e5d8SChristoph Hellwig 3614a8104a9fSAl Viro error = -EEXIST; 3615b18825a7SDavid Howells if (d_is_positive(dentry)) 3616a8104a9fSAl Viro goto fail; 3617b18825a7SDavid Howells 3618c663e5d8SChristoph Hellwig /* 3619c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3620c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3621c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3622c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3623c663e5d8SChristoph Hellwig */ 3624391172c4SAl Viro if (unlikely(!is_dir && last.name[last.len])) { 3625a8104a9fSAl Viro error = -ENOENT; 3626ed75e95dSAl Viro goto fail; 3627e9baf6e5SAl Viro } 3628c30dabfeSJan Kara if (unlikely(err2)) { 3629c30dabfeSJan Kara error = err2; 3630a8104a9fSAl Viro goto fail; 3631c30dabfeSJan Kara } 3632e9baf6e5SAl Viro return dentry; 36331da177e4SLinus Torvalds fail: 3634a8104a9fSAl Viro dput(dentry); 3635a8104a9fSAl Viro dentry = ERR_PTR(error); 3636a8104a9fSAl Viro unlock: 36375955102cSAl Viro inode_unlock(path->dentry->d_inode); 3638c30dabfeSJan Kara if (!err2) 3639391172c4SAl Viro mnt_drop_write(path->mnt); 3640ed75e95dSAl Viro out: 3641391172c4SAl Viro path_put(path); 3642ed75e95dSAl Viro return dentry; 3643dae6ad8fSAl Viro } 3644fa14a0b8SAl Viro 3645fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, 3646fa14a0b8SAl Viro struct path *path, unsigned int lookup_flags) 3647fa14a0b8SAl Viro { 3648181c37b6SAl Viro return filename_create(dfd, getname_kernel(pathname), 3649181c37b6SAl Viro path, lookup_flags); 3650fa14a0b8SAl Viro } 3651dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3652dae6ad8fSAl Viro 3653921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3654921a1650SAl Viro { 3655921a1650SAl Viro dput(dentry); 36565955102cSAl Viro inode_unlock(path->dentry->d_inode); 3657a8104a9fSAl Viro mnt_drop_write(path->mnt); 3658921a1650SAl Viro path_put(path); 3659921a1650SAl Viro } 3660921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3661921a1650SAl Viro 3662520ae687SAl Viro inline struct dentry *user_path_create(int dfd, const char __user *pathname, 36631ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3664dae6ad8fSAl Viro { 3665181c37b6SAl Viro return filename_create(dfd, getname(pathname), path, lookup_flags); 3666dae6ad8fSAl Viro } 3667dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3668dae6ad8fSAl Viro 36696521f891SChristian Brauner /** 36706521f891SChristian Brauner * vfs_mknod - create device node or file 36716521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 36726521f891SChristian Brauner * @dir: inode of @dentry 36736521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 36746521f891SChristian Brauner * @mode: mode of the new device node or file 36756521f891SChristian Brauner * @dev: device number of device to create 36766521f891SChristian Brauner * 36776521f891SChristian Brauner * Create a device node or file. 36786521f891SChristian Brauner * 36796521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 36806521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 36816521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 36826521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 36836521f891SChristian Brauner * raw inode simply passs init_user_ns. 36846521f891SChristian Brauner */ 36856521f891SChristian Brauner int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 36866521f891SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 36871da177e4SLinus Torvalds { 3688a3c751a5SMiklos Szeredi bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; 36896521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds if (error) 36921da177e4SLinus Torvalds return error; 36931da177e4SLinus Torvalds 3694a3c751a5SMiklos Szeredi if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && 3695a3c751a5SMiklos Szeredi !capable(CAP_MKNOD)) 36961da177e4SLinus Torvalds return -EPERM; 36971da177e4SLinus Torvalds 3698acfa4380SAl Viro if (!dir->i_op->mknod) 36991da177e4SLinus Torvalds return -EPERM; 37001da177e4SLinus Torvalds 370108ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 370208ce5f16SSerge E. Hallyn if (error) 370308ce5f16SSerge E. Hallyn return error; 370408ce5f16SSerge E. Hallyn 37051da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 37061da177e4SLinus Torvalds if (error) 37071da177e4SLinus Torvalds return error; 37081da177e4SLinus Torvalds 3709549c7297SChristian Brauner error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev); 3710a74574aaSStephen Smalley if (!error) 3711f38aa942SAmy Griffis fsnotify_create(dir, dentry); 37121da177e4SLinus Torvalds return error; 37131da177e4SLinus Torvalds } 37144d359507SAl Viro EXPORT_SYMBOL(vfs_mknod); 37151da177e4SLinus Torvalds 3716f69aac00SAl Viro static int may_mknod(umode_t mode) 3717463c3197SDave Hansen { 3718463c3197SDave Hansen switch (mode & S_IFMT) { 3719463c3197SDave Hansen case S_IFREG: 3720463c3197SDave Hansen case S_IFCHR: 3721463c3197SDave Hansen case S_IFBLK: 3722463c3197SDave Hansen case S_IFIFO: 3723463c3197SDave Hansen case S_IFSOCK: 3724463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3725463c3197SDave Hansen return 0; 3726463c3197SDave Hansen case S_IFDIR: 3727463c3197SDave Hansen return -EPERM; 3728463c3197SDave Hansen default: 3729463c3197SDave Hansen return -EINVAL; 3730463c3197SDave Hansen } 3731463c3197SDave Hansen } 3732463c3197SDave Hansen 37335fee64fcSChristoph Hellwig static long do_mknodat(int dfd, const char __user *filename, umode_t mode, 373487c4e192SDominik Brodowski unsigned int dev) 37351da177e4SLinus Torvalds { 37366521f891SChristian Brauner struct user_namespace *mnt_userns; 37371da177e4SLinus Torvalds struct dentry *dentry; 3738dae6ad8fSAl Viro struct path path; 3739dae6ad8fSAl Viro int error; 3740972567f1SJeff Layton unsigned int lookup_flags = 0; 37411da177e4SLinus Torvalds 37428e4bfca1SAl Viro error = may_mknod(mode); 37438e4bfca1SAl Viro if (error) 37448e4bfca1SAl Viro return error; 3745972567f1SJeff Layton retry: 3746972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3747dae6ad8fSAl Viro if (IS_ERR(dentry)) 3748dae6ad8fSAl Viro return PTR_ERR(dentry); 37492ad94ae6SAl Viro 3750dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3751ce3b0f8dSAl Viro mode &= ~current_umask(); 3752dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3753be6d3e56SKentaro Takeda if (error) 3754a8104a9fSAl Viro goto out; 37556521f891SChristian Brauner 37566521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 37571da177e4SLinus Torvalds switch (mode & S_IFMT) { 37581da177e4SLinus Torvalds case 0: case S_IFREG: 37596521f891SChristian Brauner error = vfs_create(mnt_userns, path.dentry->d_inode, 37606521f891SChristian Brauner dentry, mode, true); 376105d1a717SMimi Zohar if (!error) 3762a2d2329eSChristian Brauner ima_post_path_mknod(mnt_userns, dentry); 37631da177e4SLinus Torvalds break; 37641da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 37656521f891SChristian Brauner error = vfs_mknod(mnt_userns, path.dentry->d_inode, 37666521f891SChristian Brauner dentry, mode, new_decode_dev(dev)); 37671da177e4SLinus Torvalds break; 37681da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 37696521f891SChristian Brauner error = vfs_mknod(mnt_userns, path.dentry->d_inode, 37706521f891SChristian Brauner dentry, mode, 0); 37711da177e4SLinus Torvalds break; 37721da177e4SLinus Torvalds } 3773a8104a9fSAl Viro out: 3774921a1650SAl Viro done_path_create(&path, dentry); 3775972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3776972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3777972567f1SJeff Layton goto retry; 3778972567f1SJeff Layton } 37791da177e4SLinus Torvalds return error; 37801da177e4SLinus Torvalds } 37811da177e4SLinus Torvalds 378287c4e192SDominik Brodowski SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 378387c4e192SDominik Brodowski unsigned int, dev) 378487c4e192SDominik Brodowski { 378587c4e192SDominik Brodowski return do_mknodat(dfd, filename, mode, dev); 378687c4e192SDominik Brodowski } 378787c4e192SDominik Brodowski 37888208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 37895590ff0dSUlrich Drepper { 379087c4e192SDominik Brodowski return do_mknodat(AT_FDCWD, filename, mode, dev); 37915590ff0dSUlrich Drepper } 37925590ff0dSUlrich Drepper 37936521f891SChristian Brauner /** 37946521f891SChristian Brauner * vfs_mkdir - create directory 37956521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 37966521f891SChristian Brauner * @dir: inode of @dentry 37976521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 37986521f891SChristian Brauner * @mode: mode of the new directory 37996521f891SChristian Brauner * 38006521f891SChristian Brauner * Create a directory. 38016521f891SChristian Brauner * 38026521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 38036521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 38046521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 38056521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 38066521f891SChristian Brauner * raw inode simply passs init_user_ns. 38076521f891SChristian Brauner */ 38086521f891SChristian Brauner int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 38096521f891SChristian Brauner struct dentry *dentry, umode_t mode) 38101da177e4SLinus Torvalds { 38116521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 38128de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 38131da177e4SLinus Torvalds 38141da177e4SLinus Torvalds if (error) 38151da177e4SLinus Torvalds return error; 38161da177e4SLinus Torvalds 3817acfa4380SAl Viro if (!dir->i_op->mkdir) 38181da177e4SLinus Torvalds return -EPERM; 38191da177e4SLinus Torvalds 38201da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 38211da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 38221da177e4SLinus Torvalds if (error) 38231da177e4SLinus Torvalds return error; 38241da177e4SLinus Torvalds 38258de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 38268de52778SAl Viro return -EMLINK; 38278de52778SAl Viro 3828549c7297SChristian Brauner error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode); 3829a74574aaSStephen Smalley if (!error) 3830f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 38311da177e4SLinus Torvalds return error; 38321da177e4SLinus Torvalds } 38334d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir); 38341da177e4SLinus Torvalds 383583ff98c3SChristoph Hellwig static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) 38361da177e4SLinus Torvalds { 38376902d925SDave Hansen struct dentry *dentry; 3838dae6ad8fSAl Viro struct path path; 3839dae6ad8fSAl Viro int error; 3840b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 38411da177e4SLinus Torvalds 3842b76d8b82SJeff Layton retry: 3843b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 38446902d925SDave Hansen if (IS_ERR(dentry)) 3845dae6ad8fSAl Viro return PTR_ERR(dentry); 38466902d925SDave Hansen 3847dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3848ce3b0f8dSAl Viro mode &= ~current_umask(); 3849dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 38506521f891SChristian Brauner if (!error) { 38516521f891SChristian Brauner struct user_namespace *mnt_userns; 38526521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 3853549c7297SChristian Brauner error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry, 3854549c7297SChristian Brauner mode); 38556521f891SChristian Brauner } 3856921a1650SAl Viro done_path_create(&path, dentry); 3857b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3858b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3859b76d8b82SJeff Layton goto retry; 3860b76d8b82SJeff Layton } 38611da177e4SLinus Torvalds return error; 38621da177e4SLinus Torvalds } 38631da177e4SLinus Torvalds 38640101db7aSDominik Brodowski SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 38650101db7aSDominik Brodowski { 38660101db7aSDominik Brodowski return do_mkdirat(dfd, pathname, mode); 38670101db7aSDominik Brodowski } 38680101db7aSDominik Brodowski 3869a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 38705590ff0dSUlrich Drepper { 38710101db7aSDominik Brodowski return do_mkdirat(AT_FDCWD, pathname, mode); 38725590ff0dSUlrich Drepper } 38735590ff0dSUlrich Drepper 38746521f891SChristian Brauner /** 38756521f891SChristian Brauner * vfs_rmdir - remove directory 38766521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 38776521f891SChristian Brauner * @dir: inode of @dentry 38786521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 38796521f891SChristian Brauner * 38806521f891SChristian Brauner * Remove a directory. 38816521f891SChristian Brauner * 38826521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 38836521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 38846521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 38856521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 38866521f891SChristian Brauner * raw inode simply passs init_user_ns. 38876521f891SChristian Brauner */ 38886521f891SChristian Brauner int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, 38896521f891SChristian Brauner struct dentry *dentry) 38901da177e4SLinus Torvalds { 38916521f891SChristian Brauner int error = may_delete(mnt_userns, dir, dentry, 1); 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds if (error) 38941da177e4SLinus Torvalds return error; 38951da177e4SLinus Torvalds 3896acfa4380SAl Viro if (!dir->i_op->rmdir) 38971da177e4SLinus Torvalds return -EPERM; 38981da177e4SLinus Torvalds 38991d2ef590SAl Viro dget(dentry); 39005955102cSAl Viro inode_lock(dentry->d_inode); 3901912dbc15SSage Weil 39021da177e4SLinus Torvalds error = -EBUSY; 39037af1364fSEric W. Biederman if (is_local_mountpoint(dentry)) 3904912dbc15SSage Weil goto out; 3905912dbc15SSage Weil 39061da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3907912dbc15SSage Weil if (error) 3908912dbc15SSage Weil goto out; 3909912dbc15SSage Weil 39101da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3911912dbc15SSage Weil if (error) 3912912dbc15SSage Weil goto out; 3913912dbc15SSage Weil 39148767712fSAl Viro shrink_dcache_parent(dentry); 39151da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3916d83c49f3SAl Viro dont_mount(dentry); 39178ed936b5SEric W. Biederman detach_mounts(dentry); 3918116b9731SAmir Goldstein fsnotify_rmdir(dir, dentry); 39191da177e4SLinus Torvalds 3920912dbc15SSage Weil out: 39215955102cSAl Viro inode_unlock(dentry->d_inode); 39221d2ef590SAl Viro dput(dentry); 3923912dbc15SSage Weil if (!error) 3924912dbc15SSage Weil d_delete(dentry); 39251da177e4SLinus Torvalds return error; 39261da177e4SLinus Torvalds } 39274d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir); 39281da177e4SLinus Torvalds 3929e24ab0efSChristoph Hellwig long do_rmdir(int dfd, struct filename *name) 39301da177e4SLinus Torvalds { 39316521f891SChristian Brauner struct user_namespace *mnt_userns; 3932*0ee50b47SDmitry Kadashev int error; 39331da177e4SLinus Torvalds struct dentry *dentry; 3934f5beed75SAl Viro struct path path; 3935f5beed75SAl Viro struct qstr last; 3936f5beed75SAl Viro int type; 3937c6ee9206SJeff Layton unsigned int lookup_flags = 0; 3938c6ee9206SJeff Layton retry: 3939*0ee50b47SDmitry Kadashev error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 3940*0ee50b47SDmitry Kadashev if (error) 3941*0ee50b47SDmitry Kadashev goto exit1; 39421da177e4SLinus Torvalds 3943f5beed75SAl Viro switch (type) { 39441da177e4SLinus Torvalds case LAST_DOTDOT: 39451da177e4SLinus Torvalds error = -ENOTEMPTY; 3946*0ee50b47SDmitry Kadashev goto exit2; 39471da177e4SLinus Torvalds case LAST_DOT: 39481da177e4SLinus Torvalds error = -EINVAL; 3949*0ee50b47SDmitry Kadashev goto exit2; 39501da177e4SLinus Torvalds case LAST_ROOT: 39511da177e4SLinus Torvalds error = -EBUSY; 3952*0ee50b47SDmitry Kadashev goto exit2; 39531da177e4SLinus Torvalds } 39540612d9fbSOGAWA Hirofumi 3955f5beed75SAl Viro error = mnt_want_write(path.mnt); 3956c30dabfeSJan Kara if (error) 3957*0ee50b47SDmitry Kadashev goto exit2; 39580612d9fbSOGAWA Hirofumi 39595955102cSAl Viro inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 3960f5beed75SAl Viro dentry = __lookup_hash(&last, path.dentry, lookup_flags); 39611da177e4SLinus Torvalds error = PTR_ERR(dentry); 39626902d925SDave Hansen if (IS_ERR(dentry)) 3963*0ee50b47SDmitry Kadashev goto exit3; 3964e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3965e6bc45d6STheodore Ts'o error = -ENOENT; 3966*0ee50b47SDmitry Kadashev goto exit4; 3967e6bc45d6STheodore Ts'o } 3968f5beed75SAl Viro error = security_path_rmdir(&path, dentry); 3969be6d3e56SKentaro Takeda if (error) 3970*0ee50b47SDmitry Kadashev goto exit4; 39716521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 39726521f891SChristian Brauner error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); 3973*0ee50b47SDmitry Kadashev exit4: 39741da177e4SLinus Torvalds dput(dentry); 3975*0ee50b47SDmitry Kadashev exit3: 39765955102cSAl Viro inode_unlock(path.dentry->d_inode); 3977f5beed75SAl Viro mnt_drop_write(path.mnt); 3978*0ee50b47SDmitry Kadashev exit2: 3979f5beed75SAl Viro path_put(&path); 3980c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 3981c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 3982c6ee9206SJeff Layton goto retry; 3983c6ee9206SJeff Layton } 3984*0ee50b47SDmitry Kadashev exit1: 398524fb33d4SAl Viro putname(name); 39861da177e4SLinus Torvalds return error; 39871da177e4SLinus Torvalds } 39881da177e4SLinus Torvalds 39893cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 39905590ff0dSUlrich Drepper { 3991e24ab0efSChristoph Hellwig return do_rmdir(AT_FDCWD, getname(pathname)); 39925590ff0dSUlrich Drepper } 39935590ff0dSUlrich Drepper 3994b21996e3SJ. Bruce Fields /** 3995b21996e3SJ. Bruce Fields * vfs_unlink - unlink a filesystem object 39966521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 3997b21996e3SJ. Bruce Fields * @dir: parent directory 3998b21996e3SJ. Bruce Fields * @dentry: victim 3999b21996e3SJ. Bruce Fields * @delegated_inode: returns victim inode, if the inode is delegated. 4000b21996e3SJ. Bruce Fields * 4001b21996e3SJ. Bruce Fields * The caller must hold dir->i_mutex. 4002b21996e3SJ. Bruce Fields * 4003b21996e3SJ. Bruce Fields * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and 4004b21996e3SJ. Bruce Fields * return a reference to the inode in delegated_inode. The caller 4005b21996e3SJ. Bruce Fields * should then break the delegation on that inode and retry. Because 4006b21996e3SJ. Bruce Fields * breaking a delegation may take a long time, the caller should drop 4007b21996e3SJ. Bruce Fields * dir->i_mutex before doing so. 4008b21996e3SJ. Bruce Fields * 4009b21996e3SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 4010b21996e3SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 4011b21996e3SJ. Bruce Fields * to be NFS exported. 40126521f891SChristian Brauner * 40136521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 40146521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 40156521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 40166521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 40176521f891SChristian Brauner * raw inode simply passs init_user_ns. 4018b21996e3SJ. Bruce Fields */ 40196521f891SChristian Brauner int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, 40206521f891SChristian Brauner struct dentry *dentry, struct inode **delegated_inode) 40211da177e4SLinus Torvalds { 40229accbb97SJ. Bruce Fields struct inode *target = dentry->d_inode; 40236521f891SChristian Brauner int error = may_delete(mnt_userns, dir, dentry, 0); 40241da177e4SLinus Torvalds 40251da177e4SLinus Torvalds if (error) 40261da177e4SLinus Torvalds return error; 40271da177e4SLinus Torvalds 4028acfa4380SAl Viro if (!dir->i_op->unlink) 40291da177e4SLinus Torvalds return -EPERM; 40301da177e4SLinus Torvalds 40315955102cSAl Viro inode_lock(target); 40328ed936b5SEric W. Biederman if (is_local_mountpoint(dentry)) 40331da177e4SLinus Torvalds error = -EBUSY; 40341da177e4SLinus Torvalds else { 40351da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 4036bec1052eSAl Viro if (!error) { 40375a14696cSJ. Bruce Fields error = try_break_deleg(target, delegated_inode); 40385a14696cSJ. Bruce Fields if (error) 4039b21996e3SJ. Bruce Fields goto out; 40401da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 40418ed936b5SEric W. Biederman if (!error) { 4042d83c49f3SAl Viro dont_mount(dentry); 40438ed936b5SEric W. Biederman detach_mounts(dentry); 4044116b9731SAmir Goldstein fsnotify_unlink(dir, dentry); 40458ed936b5SEric W. Biederman } 4046bec1052eSAl Viro } 40471da177e4SLinus Torvalds } 4048b21996e3SJ. Bruce Fields out: 40495955102cSAl Viro inode_unlock(target); 40501da177e4SLinus Torvalds 40511da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 40521da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 40539accbb97SJ. Bruce Fields fsnotify_link_count(target); 40541da177e4SLinus Torvalds d_delete(dentry); 40551da177e4SLinus Torvalds } 40560eeca283SRobert Love 40571da177e4SLinus Torvalds return error; 40581da177e4SLinus Torvalds } 40594d359507SAl Viro EXPORT_SYMBOL(vfs_unlink); 40601da177e4SLinus Torvalds 40611da177e4SLinus Torvalds /* 40621da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 40631b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 40641da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 40651da177e4SLinus Torvalds * while waiting on the I/O. 40661da177e4SLinus Torvalds */ 4067da2f1362SChristoph Hellwig long do_unlinkat(int dfd, struct filename *name) 40681da177e4SLinus Torvalds { 40692ad94ae6SAl Viro int error; 40701da177e4SLinus Torvalds struct dentry *dentry; 4071f5beed75SAl Viro struct path path; 4072f5beed75SAl Viro struct qstr last; 4073f5beed75SAl Viro int type; 40741da177e4SLinus Torvalds struct inode *inode = NULL; 4075b21996e3SJ. Bruce Fields struct inode *delegated_inode = NULL; 40765d18f813SJeff Layton unsigned int lookup_flags = 0; 40775d18f813SJeff Layton retry: 4078*0ee50b47SDmitry Kadashev error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 4079*0ee50b47SDmitry Kadashev if (error) 4080*0ee50b47SDmitry Kadashev goto exit1; 40812ad94ae6SAl Viro 40821da177e4SLinus Torvalds error = -EISDIR; 4083f5beed75SAl Viro if (type != LAST_NORM) 4084*0ee50b47SDmitry Kadashev goto exit2; 40850612d9fbSOGAWA Hirofumi 4086f5beed75SAl Viro error = mnt_want_write(path.mnt); 4087c30dabfeSJan Kara if (error) 4088*0ee50b47SDmitry Kadashev goto exit2; 4089b21996e3SJ. Bruce Fields retry_deleg: 40905955102cSAl Viro inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 4091f5beed75SAl Viro dentry = __lookup_hash(&last, path.dentry, lookup_flags); 40921da177e4SLinus Torvalds error = PTR_ERR(dentry); 40931da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 40946521f891SChristian Brauner struct user_namespace *mnt_userns; 40956521f891SChristian Brauner 40961da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 4097f5beed75SAl Viro if (last.name[last.len]) 409850338b88STörök Edwin goto slashes; 40991da177e4SLinus Torvalds inode = dentry->d_inode; 4100b18825a7SDavid Howells if (d_is_negative(dentry)) 4101e6bc45d6STheodore Ts'o goto slashes; 41027de9c6eeSAl Viro ihold(inode); 4103f5beed75SAl Viro error = security_path_unlink(&path, dentry); 4104be6d3e56SKentaro Takeda if (error) 4105*0ee50b47SDmitry Kadashev goto exit3; 41066521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 4107549c7297SChristian Brauner error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, 4108549c7297SChristian Brauner &delegated_inode); 4109*0ee50b47SDmitry Kadashev exit3: 41101da177e4SLinus Torvalds dput(dentry); 41111da177e4SLinus Torvalds } 41125955102cSAl Viro inode_unlock(path.dentry->d_inode); 41131da177e4SLinus Torvalds if (inode) 41141da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 4115b21996e3SJ. Bruce Fields inode = NULL; 4116b21996e3SJ. Bruce Fields if (delegated_inode) { 41175a14696cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 4118b21996e3SJ. Bruce Fields if (!error) 4119b21996e3SJ. Bruce Fields goto retry_deleg; 4120b21996e3SJ. Bruce Fields } 4121f5beed75SAl Viro mnt_drop_write(path.mnt); 4122*0ee50b47SDmitry Kadashev exit2: 4123f5beed75SAl Viro path_put(&path); 41245d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 41255d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 41265d18f813SJeff Layton inode = NULL; 41275d18f813SJeff Layton goto retry; 41285d18f813SJeff Layton } 4129*0ee50b47SDmitry Kadashev exit1: 4130da2f1362SChristoph Hellwig putname(name); 41311da177e4SLinus Torvalds return error; 41321da177e4SLinus Torvalds 41331da177e4SLinus Torvalds slashes: 4134b18825a7SDavid Howells if (d_is_negative(dentry)) 4135b18825a7SDavid Howells error = -ENOENT; 413644b1d530SMiklos Szeredi else if (d_is_dir(dentry)) 4137b18825a7SDavid Howells error = -EISDIR; 4138b18825a7SDavid Howells else 4139b18825a7SDavid Howells error = -ENOTDIR; 4140*0ee50b47SDmitry Kadashev goto exit3; 41411da177e4SLinus Torvalds } 41421da177e4SLinus Torvalds 41432e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 41445590ff0dSUlrich Drepper { 41455590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 41465590ff0dSUlrich Drepper return -EINVAL; 41475590ff0dSUlrich Drepper 41485590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 4149e24ab0efSChristoph Hellwig return do_rmdir(dfd, getname(pathname)); 4150da2f1362SChristoph Hellwig return do_unlinkat(dfd, getname(pathname)); 41515590ff0dSUlrich Drepper } 41525590ff0dSUlrich Drepper 41533480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 41545590ff0dSUlrich Drepper { 4155da2f1362SChristoph Hellwig return do_unlinkat(AT_FDCWD, getname(pathname)); 41565590ff0dSUlrich Drepper } 41575590ff0dSUlrich Drepper 41586521f891SChristian Brauner /** 41596521f891SChristian Brauner * vfs_symlink - create symlink 41606521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 41616521f891SChristian Brauner * @dir: inode of @dentry 41626521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 41636521f891SChristian Brauner * @oldname: name of the file to link to 41646521f891SChristian Brauner * 41656521f891SChristian Brauner * Create a symlink. 41666521f891SChristian Brauner * 41676521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 41686521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 41696521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 41706521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 41716521f891SChristian Brauner * raw inode simply passs init_user_ns. 41726521f891SChristian Brauner */ 41736521f891SChristian Brauner int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 41746521f891SChristian Brauner struct dentry *dentry, const char *oldname) 41751da177e4SLinus Torvalds { 41766521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 41771da177e4SLinus Torvalds 41781da177e4SLinus Torvalds if (error) 41791da177e4SLinus Torvalds return error; 41801da177e4SLinus Torvalds 4181acfa4380SAl Viro if (!dir->i_op->symlink) 41821da177e4SLinus Torvalds return -EPERM; 41831da177e4SLinus Torvalds 41841da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 41851da177e4SLinus Torvalds if (error) 41861da177e4SLinus Torvalds return error; 41871da177e4SLinus Torvalds 4188549c7297SChristian Brauner error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname); 4189a74574aaSStephen Smalley if (!error) 4190f38aa942SAmy Griffis fsnotify_create(dir, dentry); 41911da177e4SLinus Torvalds return error; 41921da177e4SLinus Torvalds } 41934d359507SAl Viro EXPORT_SYMBOL(vfs_symlink); 41941da177e4SLinus Torvalds 4195cd3acb6aSChristoph Hellwig static long do_symlinkat(const char __user *oldname, int newdfd, 4196b724e846SDominik Brodowski const char __user *newname) 41971da177e4SLinus Torvalds { 41982ad94ae6SAl Viro int error; 419991a27b2aSJeff Layton struct filename *from; 42006902d925SDave Hansen struct dentry *dentry; 4201dae6ad8fSAl Viro struct path path; 4202f46d3567SJeff Layton unsigned int lookup_flags = 0; 42031da177e4SLinus Torvalds 42041da177e4SLinus Torvalds from = getname(oldname); 42051da177e4SLinus Torvalds if (IS_ERR(from)) 42061da177e4SLinus Torvalds return PTR_ERR(from); 4207f46d3567SJeff Layton retry: 4208f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 42091da177e4SLinus Torvalds error = PTR_ERR(dentry); 42106902d925SDave Hansen if (IS_ERR(dentry)) 4211dae6ad8fSAl Viro goto out_putname; 42126902d925SDave Hansen 421391a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 42146521f891SChristian Brauner if (!error) { 42156521f891SChristian Brauner struct user_namespace *mnt_userns; 42166521f891SChristian Brauner 42176521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 42186521f891SChristian Brauner error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry, 42196521f891SChristian Brauner from->name); 42206521f891SChristian Brauner } 4221921a1650SAl Viro done_path_create(&path, dentry); 4222f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 4223f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 4224f46d3567SJeff Layton goto retry; 4225f46d3567SJeff Layton } 42266902d925SDave Hansen out_putname: 42271da177e4SLinus Torvalds putname(from); 42281da177e4SLinus Torvalds return error; 42291da177e4SLinus Torvalds } 42301da177e4SLinus Torvalds 4231b724e846SDominik Brodowski SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 4232b724e846SDominik Brodowski int, newdfd, const char __user *, newname) 4233b724e846SDominik Brodowski { 4234b724e846SDominik Brodowski return do_symlinkat(oldname, newdfd, newname); 4235b724e846SDominik Brodowski } 4236b724e846SDominik Brodowski 42373480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 42385590ff0dSUlrich Drepper { 4239b724e846SDominik Brodowski return do_symlinkat(oldname, AT_FDCWD, newname); 42405590ff0dSUlrich Drepper } 42415590ff0dSUlrich Drepper 4242146a8595SJ. Bruce Fields /** 4243146a8595SJ. Bruce Fields * vfs_link - create a new link 4244146a8595SJ. Bruce Fields * @old_dentry: object to be linked 42456521f891SChristian Brauner * @mnt_userns: the user namespace of the mount 4246146a8595SJ. Bruce Fields * @dir: new parent 4247146a8595SJ. Bruce Fields * @new_dentry: where to create the new link 4248146a8595SJ. Bruce Fields * @delegated_inode: returns inode needing a delegation break 4249146a8595SJ. Bruce Fields * 4250146a8595SJ. Bruce Fields * The caller must hold dir->i_mutex 4251146a8595SJ. Bruce Fields * 4252146a8595SJ. Bruce Fields * If vfs_link discovers a delegation on the to-be-linked file in need 4253146a8595SJ. Bruce Fields * of breaking, it will return -EWOULDBLOCK and return a reference to the 4254146a8595SJ. Bruce Fields * inode in delegated_inode. The caller should then break the delegation 4255146a8595SJ. Bruce Fields * and retry. Because breaking a delegation may take a long time, the 4256146a8595SJ. Bruce Fields * caller should drop the i_mutex before doing so. 4257146a8595SJ. Bruce Fields * 4258146a8595SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 4259146a8595SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 4260146a8595SJ. Bruce Fields * to be NFS exported. 42616521f891SChristian Brauner * 42626521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 42636521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 42646521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 42656521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 42666521f891SChristian Brauner * raw inode simply passs init_user_ns. 4267146a8595SJ. Bruce Fields */ 42686521f891SChristian Brauner int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns, 42696521f891SChristian Brauner struct inode *dir, struct dentry *new_dentry, 42706521f891SChristian Brauner struct inode **delegated_inode) 42711da177e4SLinus Torvalds { 42721da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 42738de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 42741da177e4SLinus Torvalds int error; 42751da177e4SLinus Torvalds 42761da177e4SLinus Torvalds if (!inode) 42771da177e4SLinus Torvalds return -ENOENT; 42781da177e4SLinus Torvalds 42796521f891SChristian Brauner error = may_create(mnt_userns, dir, new_dentry); 42801da177e4SLinus Torvalds if (error) 42811da177e4SLinus Torvalds return error; 42821da177e4SLinus Torvalds 42831da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 42841da177e4SLinus Torvalds return -EXDEV; 42851da177e4SLinus Torvalds 42861da177e4SLinus Torvalds /* 42871da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 42881da177e4SLinus Torvalds */ 42891da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 42901da177e4SLinus Torvalds return -EPERM; 42910bd23d09SEric W. Biederman /* 42920bd23d09SEric W. Biederman * Updating the link count will likely cause i_uid and i_gid to 42930bd23d09SEric W. Biederman * be writen back improperly if their true value is unknown to 42940bd23d09SEric W. Biederman * the vfs. 42950bd23d09SEric W. Biederman */ 42966521f891SChristian Brauner if (HAS_UNMAPPED_ID(mnt_userns, inode)) 42970bd23d09SEric W. Biederman return -EPERM; 4298acfa4380SAl Viro if (!dir->i_op->link) 42991da177e4SLinus Torvalds return -EPERM; 43007e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 43011da177e4SLinus Torvalds return -EPERM; 43021da177e4SLinus Torvalds 43031da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 43041da177e4SLinus Torvalds if (error) 43051da177e4SLinus Torvalds return error; 43061da177e4SLinus Torvalds 43075955102cSAl Viro inode_lock(inode); 4308aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 4309f4e0c30cSAl Viro if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 4310aae8a97dSAneesh Kumar K.V error = -ENOENT; 43118de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 43128de52778SAl Viro error = -EMLINK; 4313146a8595SJ. Bruce Fields else { 4314146a8595SJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 4315146a8595SJ. Bruce Fields if (!error) 43161da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 4317146a8595SJ. Bruce Fields } 4318f4e0c30cSAl Viro 4319f4e0c30cSAl Viro if (!error && (inode->i_state & I_LINKABLE)) { 4320f4e0c30cSAl Viro spin_lock(&inode->i_lock); 4321f4e0c30cSAl Viro inode->i_state &= ~I_LINKABLE; 4322f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 4323f4e0c30cSAl Viro } 43245955102cSAl Viro inode_unlock(inode); 4325e31e14ecSStephen Smalley if (!error) 43267e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 43271da177e4SLinus Torvalds return error; 43281da177e4SLinus Torvalds } 43294d359507SAl Viro EXPORT_SYMBOL(vfs_link); 43301da177e4SLinus Torvalds 43311da177e4SLinus Torvalds /* 43321da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 43331da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 43341da177e4SLinus Torvalds * newname. --KAB 43351da177e4SLinus Torvalds * 43361da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 43371da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 43381da177e4SLinus Torvalds * and other special files. --ADM 43391da177e4SLinus Torvalds */ 4340812931d6SChristoph Hellwig static int do_linkat(int olddfd, const char __user *oldname, int newdfd, 434146ea89ebSDominik Brodowski const char __user *newname, int flags) 43421da177e4SLinus Torvalds { 43436521f891SChristian Brauner struct user_namespace *mnt_userns; 43441da177e4SLinus Torvalds struct dentry *new_dentry; 4345dae6ad8fSAl Viro struct path old_path, new_path; 4346146a8595SJ. Bruce Fields struct inode *delegated_inode = NULL; 434711a7b371SAneesh Kumar K.V int how = 0; 43481da177e4SLinus Torvalds int error; 43491da177e4SLinus Torvalds 435011a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 4351c04030e1SUlrich Drepper return -EINVAL; 435211a7b371SAneesh Kumar K.V /* 4353f0cc6ffbSLinus Torvalds * To use null names we require CAP_DAC_READ_SEARCH 4354f0cc6ffbSLinus Torvalds * This ensures that not everyone will be able to create 4355f0cc6ffbSLinus Torvalds * handlink using the passed filedescriptor. 435611a7b371SAneesh Kumar K.V */ 4357f0cc6ffbSLinus Torvalds if (flags & AT_EMPTY_PATH) { 4358f0cc6ffbSLinus Torvalds if (!capable(CAP_DAC_READ_SEARCH)) 4359f0cc6ffbSLinus Torvalds return -ENOENT; 436011a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 4361f0cc6ffbSLinus Torvalds } 4362c04030e1SUlrich Drepper 436311a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 436411a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 4365442e31caSJeff Layton retry: 436611a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 43671da177e4SLinus Torvalds if (error) 43682ad94ae6SAl Viro return error; 43692ad94ae6SAl Viro 4370442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 4371442e31caSJeff Layton (how & LOOKUP_REVAL)); 43721da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 43736902d925SDave Hansen if (IS_ERR(new_dentry)) 4374dae6ad8fSAl Viro goto out; 4375dae6ad8fSAl Viro 4376dae6ad8fSAl Viro error = -EXDEV; 4377dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 4378dae6ad8fSAl Viro goto out_dput; 4379549c7297SChristian Brauner mnt_userns = mnt_user_ns(new_path.mnt); 4380549c7297SChristian Brauner error = may_linkat(mnt_userns, &old_path); 4381800179c9SKees Cook if (unlikely(error)) 4382800179c9SKees Cook goto out_dput; 4383dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 4384be6d3e56SKentaro Takeda if (error) 4385a8104a9fSAl Viro goto out_dput; 43866521f891SChristian Brauner error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode, 43876521f891SChristian Brauner new_dentry, &delegated_inode); 438875c3f29dSDave Hansen out_dput: 4389921a1650SAl Viro done_path_create(&new_path, new_dentry); 4390146a8595SJ. Bruce Fields if (delegated_inode) { 4391146a8595SJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 4392d22e6338SOleg Drokin if (!error) { 4393d22e6338SOleg Drokin path_put(&old_path); 4394146a8595SJ. Bruce Fields goto retry; 4395146a8595SJ. Bruce Fields } 4396d22e6338SOleg Drokin } 4397442e31caSJeff Layton if (retry_estale(error, how)) { 4398d22e6338SOleg Drokin path_put(&old_path); 4399442e31caSJeff Layton how |= LOOKUP_REVAL; 4400442e31caSJeff Layton goto retry; 4401442e31caSJeff Layton } 44021da177e4SLinus Torvalds out: 44032d8f3038SAl Viro path_put(&old_path); 44041da177e4SLinus Torvalds 44051da177e4SLinus Torvalds return error; 44061da177e4SLinus Torvalds } 44071da177e4SLinus Torvalds 440846ea89ebSDominik Brodowski SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 440946ea89ebSDominik Brodowski int, newdfd, const char __user *, newname, int, flags) 441046ea89ebSDominik Brodowski { 441146ea89ebSDominik Brodowski return do_linkat(olddfd, oldname, newdfd, newname, flags); 441246ea89ebSDominik Brodowski } 441346ea89ebSDominik Brodowski 44143480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 44155590ff0dSUlrich Drepper { 441646ea89ebSDominik Brodowski return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 44175590ff0dSUlrich Drepper } 44185590ff0dSUlrich Drepper 4419bc27027aSMiklos Szeredi /** 4420bc27027aSMiklos Szeredi * vfs_rename - rename a filesystem object 44212111c3c0SRandy Dunlap * @rd: pointer to &struct renamedata info 4422bc27027aSMiklos Szeredi * 4423bc27027aSMiklos Szeredi * The caller must hold multiple mutexes--see lock_rename()). 4424bc27027aSMiklos Szeredi * 4425bc27027aSMiklos Szeredi * If vfs_rename discovers a delegation in need of breaking at either 4426bc27027aSMiklos Szeredi * the source or destination, it will return -EWOULDBLOCK and return a 4427bc27027aSMiklos Szeredi * reference to the inode in delegated_inode. The caller should then 4428bc27027aSMiklos Szeredi * break the delegation and retry. Because breaking a delegation may 4429bc27027aSMiklos Szeredi * take a long time, the caller should drop all locks before doing 4430bc27027aSMiklos Szeredi * so. 4431bc27027aSMiklos Szeredi * 4432bc27027aSMiklos Szeredi * Alternatively, a caller may pass NULL for delegated_inode. This may 4433bc27027aSMiklos Szeredi * be appropriate for callers that expect the underlying filesystem not 4434bc27027aSMiklos Szeredi * to be NFS exported. 4435bc27027aSMiklos Szeredi * 44361da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 44371da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 44381da177e4SLinus Torvalds * Problems: 44390117d427SMauro Carvalho Chehab * 4440d03b29a2SJ. Bruce Fields * a) we can get into loop creation. 44411da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 44421da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 4443a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 44441da177e4SLinus Torvalds * story. 44456cedba89SJ. Bruce Fields * c) we have to lock _four_ objects - parents and victim (if it exists), 44466cedba89SJ. Bruce Fields * and source (if it is not a directory). 44471b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 44481da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 44491da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 4450a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 44511da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 44521da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 44531da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 4454a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 44551da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 44561da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 44571da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 4458e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 44591b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 44601da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 4461c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 44621da177e4SLinus Torvalds * locking]. 44631da177e4SLinus Torvalds */ 44649fe61450SChristian Brauner int vfs_rename(struct renamedata *rd) 44651da177e4SLinus Torvalds { 44661da177e4SLinus Torvalds int error; 44679fe61450SChristian Brauner struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; 44689fe61450SChristian Brauner struct dentry *old_dentry = rd->old_dentry; 44699fe61450SChristian Brauner struct dentry *new_dentry = rd->new_dentry; 44709fe61450SChristian Brauner struct inode **delegated_inode = rd->delegated_inode; 44719fe61450SChristian Brauner unsigned int flags = rd->flags; 4472bc27027aSMiklos Szeredi bool is_dir = d_is_dir(old_dentry); 4473bc27027aSMiklos Szeredi struct inode *source = old_dentry->d_inode; 4474bc27027aSMiklos Szeredi struct inode *target = new_dentry->d_inode; 4475da1ce067SMiklos Szeredi bool new_is_dir = false; 4476da1ce067SMiklos Szeredi unsigned max_links = new_dir->i_sb->s_max_links; 447749d31c2fSAl Viro struct name_snapshot old_name; 44781da177e4SLinus Torvalds 44798d3e2936SMiklos Szeredi if (source == target) 44801da177e4SLinus Torvalds return 0; 44811da177e4SLinus Torvalds 44826521f891SChristian Brauner error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir); 44831da177e4SLinus Torvalds if (error) 44841da177e4SLinus Torvalds return error; 44851da177e4SLinus Torvalds 4486da1ce067SMiklos Szeredi if (!target) { 44876521f891SChristian Brauner error = may_create(rd->new_mnt_userns, new_dir, new_dentry); 4488da1ce067SMiklos Szeredi } else { 4489da1ce067SMiklos Szeredi new_is_dir = d_is_dir(new_dentry); 4490da1ce067SMiklos Szeredi 4491da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 44926521f891SChristian Brauner error = may_delete(rd->new_mnt_userns, new_dir, 44936521f891SChristian Brauner new_dentry, is_dir); 4494da1ce067SMiklos Szeredi else 44956521f891SChristian Brauner error = may_delete(rd->new_mnt_userns, new_dir, 44966521f891SChristian Brauner new_dentry, new_is_dir); 4497da1ce067SMiklos Szeredi } 44981da177e4SLinus Torvalds if (error) 44991da177e4SLinus Torvalds return error; 45001da177e4SLinus Torvalds 45012773bf00SMiklos Szeredi if (!old_dir->i_op->rename) 45021da177e4SLinus Torvalds return -EPERM; 45031da177e4SLinus Torvalds 4504bc27027aSMiklos Szeredi /* 4505bc27027aSMiklos Szeredi * If we are going to change the parent - check write permissions, 4506bc27027aSMiklos Szeredi * we'll need to flip '..'. 4507bc27027aSMiklos Szeredi */ 4508da1ce067SMiklos Szeredi if (new_dir != old_dir) { 4509da1ce067SMiklos Szeredi if (is_dir) { 45106521f891SChristian Brauner error = inode_permission(rd->old_mnt_userns, source, 451147291baaSChristian Brauner MAY_WRITE); 4512bc27027aSMiklos Szeredi if (error) 4513bc27027aSMiklos Szeredi return error; 4514bc27027aSMiklos Szeredi } 4515da1ce067SMiklos Szeredi if ((flags & RENAME_EXCHANGE) && new_is_dir) { 45166521f891SChristian Brauner error = inode_permission(rd->new_mnt_userns, target, 451747291baaSChristian Brauner MAY_WRITE); 4518da1ce067SMiklos Szeredi if (error) 4519da1ce067SMiklos Szeredi return error; 4520da1ce067SMiklos Szeredi } 4521da1ce067SMiklos Szeredi } 45220eeca283SRobert Love 45230b3974ebSMiklos Szeredi error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, 45240b3974ebSMiklos Szeredi flags); 4525bc27027aSMiklos Szeredi if (error) 4526bc27027aSMiklos Szeredi return error; 4527bc27027aSMiklos Szeredi 452849d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 4529bc27027aSMiklos Szeredi dget(new_dentry); 4530da1ce067SMiklos Szeredi if (!is_dir || (flags & RENAME_EXCHANGE)) 4531bc27027aSMiklos Szeredi lock_two_nondirectories(source, target); 4532bc27027aSMiklos Szeredi else if (target) 45335955102cSAl Viro inode_lock(target); 4534bc27027aSMiklos Szeredi 4535bc27027aSMiklos Szeredi error = -EBUSY; 45367af1364fSEric W. Biederman if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) 4537bc27027aSMiklos Szeredi goto out; 4538bc27027aSMiklos Szeredi 4539da1ce067SMiklos Szeredi if (max_links && new_dir != old_dir) { 4540bc27027aSMiklos Szeredi error = -EMLINK; 4541da1ce067SMiklos Szeredi if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) 4542bc27027aSMiklos Szeredi goto out; 4543da1ce067SMiklos Szeredi if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && 4544da1ce067SMiklos Szeredi old_dir->i_nlink >= max_links) 4545da1ce067SMiklos Szeredi goto out; 4546da1ce067SMiklos Szeredi } 4547da1ce067SMiklos Szeredi if (!is_dir) { 4548bc27027aSMiklos Szeredi error = try_break_deleg(source, delegated_inode); 4549bc27027aSMiklos Szeredi if (error) 4550bc27027aSMiklos Szeredi goto out; 4551da1ce067SMiklos Szeredi } 4552da1ce067SMiklos Szeredi if (target && !new_is_dir) { 4553bc27027aSMiklos Szeredi error = try_break_deleg(target, delegated_inode); 4554bc27027aSMiklos Szeredi if (error) 4555bc27027aSMiklos Szeredi goto out; 4556bc27027aSMiklos Szeredi } 4557549c7297SChristian Brauner error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry, 4558520c8b16SMiklos Szeredi new_dir, new_dentry, flags); 4559bc27027aSMiklos Szeredi if (error) 4560bc27027aSMiklos Szeredi goto out; 4561bc27027aSMiklos Szeredi 4562da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE) && target) { 45638767712fSAl Viro if (is_dir) { 45648767712fSAl Viro shrink_dcache_parent(new_dentry); 4565bc27027aSMiklos Szeredi target->i_flags |= S_DEAD; 45668767712fSAl Viro } 4567bc27027aSMiklos Szeredi dont_mount(new_dentry); 45688ed936b5SEric W. Biederman detach_mounts(new_dentry); 4569bc27027aSMiklos Szeredi } 4570da1ce067SMiklos Szeredi if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { 4571da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 4572bc27027aSMiklos Szeredi d_move(old_dentry, new_dentry); 4573da1ce067SMiklos Szeredi else 4574da1ce067SMiklos Szeredi d_exchange(old_dentry, new_dentry); 4575da1ce067SMiklos Szeredi } 4576bc27027aSMiklos Szeredi out: 4577da1ce067SMiklos Szeredi if (!is_dir || (flags & RENAME_EXCHANGE)) 4578bc27027aSMiklos Szeredi unlock_two_nondirectories(source, target); 4579bc27027aSMiklos Szeredi else if (target) 45805955102cSAl Viro inode_unlock(target); 4581bc27027aSMiklos Szeredi dput(new_dentry); 4582da1ce067SMiklos Szeredi if (!error) { 4583f4ec3a3dSAl Viro fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, 4584da1ce067SMiklos Szeredi !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); 4585da1ce067SMiklos Szeredi if (flags & RENAME_EXCHANGE) { 4586f4ec3a3dSAl Viro fsnotify_move(new_dir, old_dir, &old_dentry->d_name, 4587da1ce067SMiklos Szeredi new_is_dir, NULL, new_dentry); 4588da1ce067SMiklos Szeredi } 4589da1ce067SMiklos Szeredi } 459049d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 45910eeca283SRobert Love 45921da177e4SLinus Torvalds return error; 45931da177e4SLinus Torvalds } 45944d359507SAl Viro EXPORT_SYMBOL(vfs_rename); 45951da177e4SLinus Torvalds 4596e886663cSJens Axboe int do_renameat2(int olddfd, struct filename *from, int newdfd, 4597e886663cSJens Axboe struct filename *to, unsigned int flags) 45981da177e4SLinus Torvalds { 45999fe61450SChristian Brauner struct renamedata rd; 46001da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 46011da177e4SLinus Torvalds struct dentry *trap; 4602f5beed75SAl Viro struct path old_path, new_path; 4603f5beed75SAl Viro struct qstr old_last, new_last; 4604f5beed75SAl Viro int old_type, new_type; 46058e6d782cSJ. Bruce Fields struct inode *delegated_inode = NULL; 4606f5beed75SAl Viro unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; 4607c6a94284SJeff Layton bool should_retry = false; 4608e886663cSJens Axboe int error = -EINVAL; 4609520c8b16SMiklos Szeredi 46100d7a8555SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 4611*0ee50b47SDmitry Kadashev goto put_names; 4612da1ce067SMiklos Szeredi 46130d7a8555SMiklos Szeredi if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && 46140d7a8555SMiklos Szeredi (flags & RENAME_EXCHANGE)) 4615*0ee50b47SDmitry Kadashev goto put_names; 4616520c8b16SMiklos Szeredi 4617f5beed75SAl Viro if (flags & RENAME_EXCHANGE) 4618f5beed75SAl Viro target_flags = 0; 4619f5beed75SAl Viro 4620c6a94284SJeff Layton retry: 4621*0ee50b47SDmitry Kadashev error = __filename_parentat(olddfd, from, lookup_flags, &old_path, 4622e886663cSJens Axboe &old_last, &old_type); 4623*0ee50b47SDmitry Kadashev if (error) 4624*0ee50b47SDmitry Kadashev goto put_names; 46251da177e4SLinus Torvalds 4626*0ee50b47SDmitry Kadashev error = __filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, 4627e886663cSJens Axboe &new_type); 4628*0ee50b47SDmitry Kadashev if (error) 46291da177e4SLinus Torvalds goto exit1; 46301da177e4SLinus Torvalds 46311da177e4SLinus Torvalds error = -EXDEV; 4632f5beed75SAl Viro if (old_path.mnt != new_path.mnt) 46331da177e4SLinus Torvalds goto exit2; 46341da177e4SLinus Torvalds 46351da177e4SLinus Torvalds error = -EBUSY; 4636f5beed75SAl Viro if (old_type != LAST_NORM) 46371da177e4SLinus Torvalds goto exit2; 46381da177e4SLinus Torvalds 46390a7c3937SMiklos Szeredi if (flags & RENAME_NOREPLACE) 46400a7c3937SMiklos Szeredi error = -EEXIST; 4641f5beed75SAl Viro if (new_type != LAST_NORM) 46421da177e4SLinus Torvalds goto exit2; 46431da177e4SLinus Torvalds 4644f5beed75SAl Viro error = mnt_want_write(old_path.mnt); 4645c30dabfeSJan Kara if (error) 4646c30dabfeSJan Kara goto exit2; 4647c30dabfeSJan Kara 46488e6d782cSJ. Bruce Fields retry_deleg: 4649f5beed75SAl Viro trap = lock_rename(new_path.dentry, old_path.dentry); 46501da177e4SLinus Torvalds 4651f5beed75SAl Viro old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); 46521da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 46531da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 46541da177e4SLinus Torvalds goto exit3; 46551da177e4SLinus Torvalds /* source must exist */ 46561da177e4SLinus Torvalds error = -ENOENT; 4657b18825a7SDavid Howells if (d_is_negative(old_dentry)) 46581da177e4SLinus Torvalds goto exit4; 4659f5beed75SAl Viro new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); 46601da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 46611da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 46621da177e4SLinus Torvalds goto exit4; 46630a7c3937SMiklos Szeredi error = -EEXIST; 46640a7c3937SMiklos Szeredi if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) 46650a7c3937SMiklos Szeredi goto exit5; 4666da1ce067SMiklos Szeredi if (flags & RENAME_EXCHANGE) { 4667da1ce067SMiklos Szeredi error = -ENOENT; 4668da1ce067SMiklos Szeredi if (d_is_negative(new_dentry)) 4669da1ce067SMiklos Szeredi goto exit5; 4670da1ce067SMiklos Szeredi 4671da1ce067SMiklos Szeredi if (!d_is_dir(new_dentry)) { 4672da1ce067SMiklos Szeredi error = -ENOTDIR; 4673f5beed75SAl Viro if (new_last.name[new_last.len]) 4674da1ce067SMiklos Szeredi goto exit5; 4675da1ce067SMiklos Szeredi } 4676da1ce067SMiklos Szeredi } 46770a7c3937SMiklos Szeredi /* unless the source is a directory trailing slashes give -ENOTDIR */ 46780a7c3937SMiklos Szeredi if (!d_is_dir(old_dentry)) { 46790a7c3937SMiklos Szeredi error = -ENOTDIR; 4680f5beed75SAl Viro if (old_last.name[old_last.len]) 46810a7c3937SMiklos Szeredi goto exit5; 4682f5beed75SAl Viro if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) 46830a7c3937SMiklos Szeredi goto exit5; 46840a7c3937SMiklos Szeredi } 46850a7c3937SMiklos Szeredi /* source should not be ancestor of target */ 46860a7c3937SMiklos Szeredi error = -EINVAL; 46870a7c3937SMiklos Szeredi if (old_dentry == trap) 46880a7c3937SMiklos Szeredi goto exit5; 46891da177e4SLinus Torvalds /* target should not be an ancestor of source */ 4690da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 46911da177e4SLinus Torvalds error = -ENOTEMPTY; 46921da177e4SLinus Torvalds if (new_dentry == trap) 46931da177e4SLinus Torvalds goto exit5; 46941da177e4SLinus Torvalds 4695f5beed75SAl Viro error = security_path_rename(&old_path, old_dentry, 4696f5beed75SAl Viro &new_path, new_dentry, flags); 4697be6d3e56SKentaro Takeda if (error) 4698c30dabfeSJan Kara goto exit5; 46999fe61450SChristian Brauner 47009fe61450SChristian Brauner rd.old_dir = old_path.dentry->d_inode; 47019fe61450SChristian Brauner rd.old_dentry = old_dentry; 47026521f891SChristian Brauner rd.old_mnt_userns = mnt_user_ns(old_path.mnt); 47039fe61450SChristian Brauner rd.new_dir = new_path.dentry->d_inode; 47049fe61450SChristian Brauner rd.new_dentry = new_dentry; 47056521f891SChristian Brauner rd.new_mnt_userns = mnt_user_ns(new_path.mnt); 47069fe61450SChristian Brauner rd.delegated_inode = &delegated_inode; 47079fe61450SChristian Brauner rd.flags = flags; 47089fe61450SChristian Brauner error = vfs_rename(&rd); 47091da177e4SLinus Torvalds exit5: 47101da177e4SLinus Torvalds dput(new_dentry); 47111da177e4SLinus Torvalds exit4: 47121da177e4SLinus Torvalds dput(old_dentry); 47131da177e4SLinus Torvalds exit3: 4714f5beed75SAl Viro unlock_rename(new_path.dentry, old_path.dentry); 47158e6d782cSJ. Bruce Fields if (delegated_inode) { 47168e6d782cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 47178e6d782cSJ. Bruce Fields if (!error) 47188e6d782cSJ. Bruce Fields goto retry_deleg; 47198e6d782cSJ. Bruce Fields } 4720f5beed75SAl Viro mnt_drop_write(old_path.mnt); 47211da177e4SLinus Torvalds exit2: 4722c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 4723c6a94284SJeff Layton should_retry = true; 4724f5beed75SAl Viro path_put(&new_path); 47251da177e4SLinus Torvalds exit1: 4726f5beed75SAl Viro path_put(&old_path); 4727c6a94284SJeff Layton if (should_retry) { 4728c6a94284SJeff Layton should_retry = false; 4729c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 4730c6a94284SJeff Layton goto retry; 4731c6a94284SJeff Layton } 4732*0ee50b47SDmitry Kadashev put_names: 4733e886663cSJens Axboe putname(from); 4734e886663cSJens Axboe putname(to); 47351da177e4SLinus Torvalds return error; 47361da177e4SLinus Torvalds } 47371da177e4SLinus Torvalds 4738ee81feb6SDominik Brodowski SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, 4739ee81feb6SDominik Brodowski int, newdfd, const char __user *, newname, unsigned int, flags) 4740ee81feb6SDominik Brodowski { 4741e886663cSJens Axboe return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 4742e886663cSJens Axboe flags); 4743ee81feb6SDominik Brodowski } 4744ee81feb6SDominik Brodowski 4745520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 4746520c8b16SMiklos Szeredi int, newdfd, const char __user *, newname) 4747520c8b16SMiklos Szeredi { 4748e886663cSJens Axboe return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 4749e886663cSJens Axboe 0); 4750520c8b16SMiklos Szeredi } 4751520c8b16SMiklos Szeredi 4752a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 47535590ff0dSUlrich Drepper { 4754e886663cSJens Axboe return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, 4755e886663cSJens Axboe getname(newname), 0); 47565590ff0dSUlrich Drepper } 47575590ff0dSUlrich Drepper 47585d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link) 47591da177e4SLinus Torvalds { 47605d826c84SAl Viro int len = PTR_ERR(link); 47611da177e4SLinus Torvalds if (IS_ERR(link)) 47621da177e4SLinus Torvalds goto out; 47631da177e4SLinus Torvalds 47641da177e4SLinus Torvalds len = strlen(link); 47651da177e4SLinus Torvalds if (len > (unsigned) buflen) 47661da177e4SLinus Torvalds len = buflen; 47671da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 47681da177e4SLinus Torvalds len = -EFAULT; 47691da177e4SLinus Torvalds out: 47701da177e4SLinus Torvalds return len; 47711da177e4SLinus Torvalds } 47721da177e4SLinus Torvalds 4773d60874cdSMiklos Szeredi /** 4774fd4a0edfSMiklos Szeredi * vfs_readlink - copy symlink body into userspace buffer 4775fd4a0edfSMiklos Szeredi * @dentry: dentry on which to get symbolic link 4776fd4a0edfSMiklos Szeredi * @buffer: user memory pointer 4777fd4a0edfSMiklos Szeredi * @buflen: size of buffer 4778fd4a0edfSMiklos Szeredi * 4779fd4a0edfSMiklos Szeredi * Does not touch atime. That's up to the caller if necessary 4780fd4a0edfSMiklos Szeredi * 4781fd4a0edfSMiklos Szeredi * Does not call security hook. 4782fd4a0edfSMiklos Szeredi */ 4783fd4a0edfSMiklos Szeredi int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) 4784fd4a0edfSMiklos Szeredi { 4785fd4a0edfSMiklos Szeredi struct inode *inode = d_inode(dentry); 4786f2df5da6SAl Viro DEFINE_DELAYED_CALL(done); 4787f2df5da6SAl Viro const char *link; 4788f2df5da6SAl Viro int res; 4789fd4a0edfSMiklos Szeredi 479076fca90eSMiklos Szeredi if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { 479176fca90eSMiklos Szeredi if (unlikely(inode->i_op->readlink)) 479276fca90eSMiklos Szeredi return inode->i_op->readlink(dentry, buffer, buflen); 479376fca90eSMiklos Szeredi 479476fca90eSMiklos Szeredi if (!d_is_symlink(dentry)) 4795fd4a0edfSMiklos Szeredi return -EINVAL; 4796fd4a0edfSMiklos Szeredi 479776fca90eSMiklos Szeredi spin_lock(&inode->i_lock); 479876fca90eSMiklos Szeredi inode->i_opflags |= IOP_DEFAULT_READLINK; 479976fca90eSMiklos Szeredi spin_unlock(&inode->i_lock); 480076fca90eSMiklos Szeredi } 480176fca90eSMiklos Szeredi 48024c4f7c19SEric Biggers link = READ_ONCE(inode->i_link); 4803f2df5da6SAl Viro if (!link) { 4804f2df5da6SAl Viro link = inode->i_op->get_link(dentry, inode, &done); 4805f2df5da6SAl Viro if (IS_ERR(link)) 4806f2df5da6SAl Viro return PTR_ERR(link); 4807f2df5da6SAl Viro } 4808f2df5da6SAl Viro res = readlink_copy(buffer, buflen, link); 4809f2df5da6SAl Viro do_delayed_call(&done); 4810f2df5da6SAl Viro return res; 4811fd4a0edfSMiklos Szeredi } 4812fd4a0edfSMiklos Szeredi EXPORT_SYMBOL(vfs_readlink); 48131da177e4SLinus Torvalds 4814d60874cdSMiklos Szeredi /** 4815d60874cdSMiklos Szeredi * vfs_get_link - get symlink body 4816d60874cdSMiklos Szeredi * @dentry: dentry on which to get symbolic link 4817d60874cdSMiklos Szeredi * @done: caller needs to free returned data with this 4818d60874cdSMiklos Szeredi * 4819d60874cdSMiklos Szeredi * Calls security hook and i_op->get_link() on the supplied inode. 4820d60874cdSMiklos Szeredi * 4821d60874cdSMiklos Szeredi * It does not touch atime. That's up to the caller if necessary. 4822d60874cdSMiklos Szeredi * 4823d60874cdSMiklos Szeredi * Does not work on "special" symlinks like /proc/$$/fd/N 4824d60874cdSMiklos Szeredi */ 4825d60874cdSMiklos Szeredi const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) 4826d60874cdSMiklos Szeredi { 4827d60874cdSMiklos Szeredi const char *res = ERR_PTR(-EINVAL); 4828d60874cdSMiklos Szeredi struct inode *inode = d_inode(dentry); 4829d60874cdSMiklos Szeredi 4830d60874cdSMiklos Szeredi if (d_is_symlink(dentry)) { 4831d60874cdSMiklos Szeredi res = ERR_PTR(security_inode_readlink(dentry)); 4832d60874cdSMiklos Szeredi if (!res) 4833d60874cdSMiklos Szeredi res = inode->i_op->get_link(dentry, inode, done); 4834d60874cdSMiklos Szeredi } 4835d60874cdSMiklos Szeredi return res; 4836d60874cdSMiklos Szeredi } 4837d60874cdSMiklos Szeredi EXPORT_SYMBOL(vfs_get_link); 4838d60874cdSMiklos Szeredi 48391da177e4SLinus Torvalds /* get the link contents into pagecache */ 48406b255391SAl Viro const char *page_get_link(struct dentry *dentry, struct inode *inode, 4841fceef393SAl Viro struct delayed_call *callback) 48421da177e4SLinus Torvalds { 4843ebd09abbSDuane Griffin char *kaddr; 48441da177e4SLinus Torvalds struct page *page; 48456b255391SAl Viro struct address_space *mapping = inode->i_mapping; 48466b255391SAl Viro 4847d3883d4fSAl Viro if (!dentry) { 4848d3883d4fSAl Viro page = find_get_page(mapping, 0); 4849d3883d4fSAl Viro if (!page) 48506b255391SAl Viro return ERR_PTR(-ECHILD); 4851d3883d4fSAl Viro if (!PageUptodate(page)) { 4852d3883d4fSAl Viro put_page(page); 4853d3883d4fSAl Viro return ERR_PTR(-ECHILD); 4854d3883d4fSAl Viro } 4855d3883d4fSAl Viro } else { 4856090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 48571da177e4SLinus Torvalds if (IS_ERR(page)) 48586fe6900eSNick Piggin return (char*)page; 4859d3883d4fSAl Viro } 4860fceef393SAl Viro set_delayed_call(callback, page_put_link, page); 486121fc61c7SAl Viro BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); 486221fc61c7SAl Viro kaddr = page_address(page); 48636b255391SAl Viro nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); 4864ebd09abbSDuane Griffin return kaddr; 48651da177e4SLinus Torvalds } 48661da177e4SLinus Torvalds 48676b255391SAl Viro EXPORT_SYMBOL(page_get_link); 48681da177e4SLinus Torvalds 4869fceef393SAl Viro void page_put_link(void *arg) 48701da177e4SLinus Torvalds { 4871fceef393SAl Viro put_page(arg); 48721da177e4SLinus Torvalds } 48734d359507SAl Viro EXPORT_SYMBOL(page_put_link); 48741da177e4SLinus Torvalds 4875aa80deabSAl Viro int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 4876aa80deabSAl Viro { 4877fceef393SAl Viro DEFINE_DELAYED_CALL(done); 48786b255391SAl Viro int res = readlink_copy(buffer, buflen, 48796b255391SAl Viro page_get_link(dentry, d_inode(dentry), 4880fceef393SAl Viro &done)); 4881fceef393SAl Viro do_delayed_call(&done); 4882aa80deabSAl Viro return res; 4883aa80deabSAl Viro } 4884aa80deabSAl Viro EXPORT_SYMBOL(page_readlink); 4885aa80deabSAl Viro 488654566b2cSNick Piggin /* 488754566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 488854566b2cSNick Piggin */ 488954566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 48901da177e4SLinus Torvalds { 48911da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 48920adb25d2SKirill Korotaev struct page *page; 4893afddba49SNick Piggin void *fsdata; 4894beb497abSDmitriy Monakhov int err; 4895c718a975STetsuo Handa unsigned int flags = 0; 489654566b2cSNick Piggin if (nofs) 489754566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 48981da177e4SLinus Torvalds 48997e53cac4SNeilBrown retry: 4900afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 490154566b2cSNick Piggin flags, &page, &fsdata); 49021da177e4SLinus Torvalds if (err) 4903afddba49SNick Piggin goto fail; 4904afddba49SNick Piggin 490521fc61c7SAl Viro memcpy(page_address(page), symname, len-1); 4906afddba49SNick Piggin 4907afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4908afddba49SNick Piggin page, fsdata); 49091da177e4SLinus Torvalds if (err < 0) 49101da177e4SLinus Torvalds goto fail; 4911afddba49SNick Piggin if (err < len-1) 4912afddba49SNick Piggin goto retry; 4913afddba49SNick Piggin 49141da177e4SLinus Torvalds mark_inode_dirty(inode); 49151da177e4SLinus Torvalds return 0; 49161da177e4SLinus Torvalds fail: 49171da177e4SLinus Torvalds return err; 49181da177e4SLinus Torvalds } 49194d359507SAl Viro EXPORT_SYMBOL(__page_symlink); 49201da177e4SLinus Torvalds 49210adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 49220adb25d2SKirill Korotaev { 49230adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 4924c62d2555SMichal Hocko !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); 49250adb25d2SKirill Korotaev } 49264d359507SAl Viro EXPORT_SYMBOL(page_symlink); 49270adb25d2SKirill Korotaev 492892e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 49296b255391SAl Viro .get_link = page_get_link, 49301da177e4SLinus Torvalds }; 49311da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4932