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> 252d878178SMatthew Wilcox (Oracle) #include <linux/sched/mm.h> 260eeca283SRobert Love #include <linux/fsnotify.h> 271da177e4SLinus Torvalds #include <linux/personality.h> 281da177e4SLinus Torvalds #include <linux/security.h> 296146f0d5SMimi Zohar #include <linux/ima.h> 301da177e4SLinus Torvalds #include <linux/syscalls.h> 311da177e4SLinus Torvalds #include <linux/mount.h> 321da177e4SLinus Torvalds #include <linux/audit.h> 3316f7e0feSRandy Dunlap #include <linux/capability.h> 34834f2a4aSTrond Myklebust #include <linux/file.h> 355590ff0dSUlrich Drepper #include <linux/fcntl.h> 3608ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 375ad4e53bSAl Viro #include <linux/fs_struct.h> 38e77819e5SLinus Torvalds #include <linux/posix_acl.h> 3999d263d4SLinus Torvalds #include <linux/hash.h> 402a18da7aSGeorge Spelvin #include <linux/bitops.h> 41aeaa4a79SEric W. Biederman #include <linux/init_task.h> 427c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 431da177e4SLinus Torvalds 44e81e3f4dSEric Paris #include "internal.h" 45c7105365SAl Viro #include "mount.h" 46e81e3f4dSEric Paris 471da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 481da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 491da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 501da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 511da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 541da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 551da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 561da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 571da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 581da177e4SLinus Torvalds * the special cases of the former code. 591da177e4SLinus Torvalds * 601da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 611da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 621da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 631da177e4SLinus Torvalds * 641da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 651da177e4SLinus Torvalds * resolution to correspond with current state of the code. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 681da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 691da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 701da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 711da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 721da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 761da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 771da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 781da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 791da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 801da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 8125985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 821da177e4SLinus Torvalds * 831da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 841da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 851da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 861da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 871da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 881da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 891da177e4SLinus Torvalds * and in the old Linux semantics. 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 931da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 941da177e4SLinus Torvalds * 951da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 961da177e4SLinus Torvalds */ 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 991da177e4SLinus Torvalds * inside the path - always follow. 1001da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 1011da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 1021da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 1031da177e4SLinus Torvalds * otherwise - don't follow. 1041da177e4SLinus Torvalds * (applied in that order). 1051da177e4SLinus Torvalds * 1061da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1071da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1081da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1091da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1101da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds /* 1131da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 114a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1151da177e4SLinus Torvalds * any extra contention... 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1191da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1201da177e4SLinus Torvalds * kernel data space before using them.. 1211da177e4SLinus Torvalds * 1221da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1231da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1241da177e4SLinus Torvalds */ 1257950e385SJeff Layton 126fd2f7cb5SAl Viro #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) 12791a27b2aSJeff Layton 12851f39a1fSDavid Drysdale struct filename * 12991a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty) 13091a27b2aSJeff Layton { 13194b5d262SAl Viro struct filename *result; 1327950e385SJeff Layton char *kname; 13394b5d262SAl Viro int len; 1341da177e4SLinus Torvalds 1357ac86265SJeff Layton result = audit_reusename(filename); 1367ac86265SJeff Layton if (result) 1377ac86265SJeff Layton return result; 1387ac86265SJeff Layton 1397950e385SJeff Layton result = __getname(); 1403f9f0aa6SLinus Torvalds if (unlikely(!result)) 1414043cde8SEric Paris return ERR_PTR(-ENOMEM); 1421da177e4SLinus Torvalds 1437950e385SJeff Layton /* 1447950e385SJeff Layton * First, try to embed the struct filename inside the names_cache 1457950e385SJeff Layton * allocation 1467950e385SJeff Layton */ 147fd2f7cb5SAl Viro kname = (char *)result->iname; 14891a27b2aSJeff Layton result->name = kname; 1497950e385SJeff Layton 15094b5d262SAl Viro len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); 15191a27b2aSJeff Layton if (unlikely(len < 0)) { 15294b5d262SAl Viro __putname(result); 15394b5d262SAl Viro return ERR_PTR(len); 15491a27b2aSJeff Layton } 1553f9f0aa6SLinus Torvalds 1567950e385SJeff Layton /* 1577950e385SJeff Layton * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a 1587950e385SJeff Layton * separate struct filename so we can dedicate the entire 1597950e385SJeff Layton * names_cache allocation for the pathname, and re-do the copy from 1607950e385SJeff Layton * userland. 1617950e385SJeff Layton */ 16294b5d262SAl Viro if (unlikely(len == EMBEDDED_NAME_MAX)) { 163fd2f7cb5SAl Viro const size_t size = offsetof(struct filename, iname[1]); 1647950e385SJeff Layton kname = (char *)result; 1657950e385SJeff Layton 166fd2f7cb5SAl Viro /* 167fd2f7cb5SAl Viro * size is chosen that way we to guarantee that 168fd2f7cb5SAl Viro * result->iname[0] is within the same object and that 169fd2f7cb5SAl Viro * kname can't be equal to result->iname, no matter what. 170fd2f7cb5SAl Viro */ 171fd2f7cb5SAl Viro result = kzalloc(size, GFP_KERNEL); 17294b5d262SAl Viro if (unlikely(!result)) { 17394b5d262SAl Viro __putname(kname); 17494b5d262SAl Viro return ERR_PTR(-ENOMEM); 1757950e385SJeff Layton } 1767950e385SJeff Layton result->name = kname; 17794b5d262SAl Viro len = strncpy_from_user(kname, filename, PATH_MAX); 17894b5d262SAl Viro if (unlikely(len < 0)) { 17994b5d262SAl Viro __putname(kname); 18094b5d262SAl Viro kfree(result); 18194b5d262SAl Viro return ERR_PTR(len); 18294b5d262SAl Viro } 18394b5d262SAl Viro if (unlikely(len == PATH_MAX)) { 18494b5d262SAl Viro __putname(kname); 18594b5d262SAl Viro kfree(result); 18694b5d262SAl Viro return ERR_PTR(-ENAMETOOLONG); 18794b5d262SAl Viro } 1887950e385SJeff Layton } 1897950e385SJeff Layton 19094b5d262SAl Viro result->refcnt = 1; 1913f9f0aa6SLinus Torvalds /* The empty path is special. */ 1923f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1933f9f0aa6SLinus Torvalds if (empty) 1941fa1e7f6SAndy Whitcroft *empty = 1; 19594b5d262SAl Viro if (!(flags & LOOKUP_EMPTY)) { 19694b5d262SAl Viro putname(result); 19794b5d262SAl Viro return ERR_PTR(-ENOENT); 1981da177e4SLinus Torvalds } 19994b5d262SAl Viro } 2007950e385SJeff Layton 2017950e385SJeff Layton result->uptr = filename; 202c4ad8f98SLinus Torvalds result->aname = NULL; 2031da177e4SLinus Torvalds audit_getname(result); 2041da177e4SLinus Torvalds return result; 2053f9f0aa6SLinus Torvalds } 2063f9f0aa6SLinus Torvalds 20791a27b2aSJeff Layton struct filename * 2088228e2c3SDmitry Kadashev getname_uflags(const char __user *filename, int uflags) 2098228e2c3SDmitry Kadashev { 2108228e2c3SDmitry Kadashev int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0; 2118228e2c3SDmitry Kadashev 2128228e2c3SDmitry Kadashev return getname_flags(filename, flags, NULL); 2138228e2c3SDmitry Kadashev } 2148228e2c3SDmitry Kadashev 2158228e2c3SDmitry Kadashev struct filename * 21691a27b2aSJeff Layton getname(const char __user * filename) 217f52e0c11SAl Viro { 218f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 219f52e0c11SAl Viro } 220f52e0c11SAl Viro 221c4ad8f98SLinus Torvalds struct filename * 222c4ad8f98SLinus Torvalds getname_kernel(const char * filename) 223c4ad8f98SLinus Torvalds { 224c4ad8f98SLinus Torvalds struct filename *result; 22508518549SPaul Moore int len = strlen(filename) + 1; 226c4ad8f98SLinus Torvalds 227c4ad8f98SLinus Torvalds result = __getname(); 228c4ad8f98SLinus Torvalds if (unlikely(!result)) 229c4ad8f98SLinus Torvalds return ERR_PTR(-ENOMEM); 230c4ad8f98SLinus Torvalds 23108518549SPaul Moore if (len <= EMBEDDED_NAME_MAX) { 232fd2f7cb5SAl Viro result->name = (char *)result->iname; 23308518549SPaul Moore } else if (len <= PATH_MAX) { 23430ce4d19SAl Viro const size_t size = offsetof(struct filename, iname[1]); 23508518549SPaul Moore struct filename *tmp; 23608518549SPaul Moore 23730ce4d19SAl Viro tmp = kmalloc(size, GFP_KERNEL); 23808518549SPaul Moore if (unlikely(!tmp)) { 23908518549SPaul Moore __putname(result); 24008518549SPaul Moore return ERR_PTR(-ENOMEM); 24108518549SPaul Moore } 24208518549SPaul Moore tmp->name = (char *)result; 24308518549SPaul Moore result = tmp; 24408518549SPaul Moore } else { 24508518549SPaul Moore __putname(result); 24608518549SPaul Moore return ERR_PTR(-ENAMETOOLONG); 24708518549SPaul Moore } 24808518549SPaul Moore memcpy((char *)result->name, filename, len); 249c4ad8f98SLinus Torvalds result->uptr = NULL; 250c4ad8f98SLinus Torvalds result->aname = NULL; 25155422d0bSPaul Moore result->refcnt = 1; 252fd3522fdSPaul Moore audit_getname(result); 253c4ad8f98SLinus Torvalds 254c4ad8f98SLinus Torvalds return result; 255c4ad8f98SLinus Torvalds } 256c4ad8f98SLinus Torvalds 25791a27b2aSJeff Layton void putname(struct filename *name) 2581da177e4SLinus Torvalds { 259ea47ab11SAl Viro if (IS_ERR(name)) 26091ef658fSDmitry Kadashev return; 26191ef658fSDmitry Kadashev 26255422d0bSPaul Moore BUG_ON(name->refcnt <= 0); 26355422d0bSPaul Moore 26455422d0bSPaul Moore if (--name->refcnt > 0) 26555422d0bSPaul Moore return; 26655422d0bSPaul Moore 267fd2f7cb5SAl Viro if (name->name != name->iname) { 26855422d0bSPaul Moore __putname(name->name); 26955422d0bSPaul Moore kfree(name); 27055422d0bSPaul Moore } else 27155422d0bSPaul Moore __putname(name); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 27447291baaSChristian Brauner /** 27547291baaSChristian Brauner * check_acl - perform ACL permission checking 27647291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 27747291baaSChristian Brauner * @inode: inode to check permissions on 27847291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 27947291baaSChristian Brauner * 28047291baaSChristian Brauner * This function performs the ACL permission checking. Since this function 28147291baaSChristian Brauner * retrieve POSIX acls it needs to know whether it is called from a blocking or 28247291baaSChristian Brauner * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 28347291baaSChristian Brauner * 28447291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 28547291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 28647291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 28747291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 28847291baaSChristian Brauner * raw inode simply passs init_user_ns. 28947291baaSChristian Brauner */ 29047291baaSChristian Brauner static int check_acl(struct user_namespace *mnt_userns, 29147291baaSChristian Brauner struct inode *inode, int mask) 292e77819e5SLinus Torvalds { 29384635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 294e77819e5SLinus Torvalds struct posix_acl *acl; 295e77819e5SLinus Torvalds 296e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2973567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2983567866bSAl Viro if (!acl) 299e77819e5SLinus Torvalds return -EAGAIN; 3003567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 301b8a7a3a6SAndreas Gruenbacher if (is_uncached_acl(acl)) 302e77819e5SLinus Torvalds return -ECHILD; 30347291baaSChristian Brauner return posix_acl_permission(mnt_userns, inode, acl, mask); 304e77819e5SLinus Torvalds } 305e77819e5SLinus Torvalds 3062982baa2SChristoph Hellwig acl = get_acl(inode, ACL_TYPE_ACCESS); 3074e34e719SChristoph Hellwig if (IS_ERR(acl)) 3084e34e719SChristoph Hellwig return PTR_ERR(acl); 309e77819e5SLinus Torvalds if (acl) { 31047291baaSChristian Brauner int error = posix_acl_permission(mnt_userns, inode, acl, mask); 311e77819e5SLinus Torvalds posix_acl_release(acl); 312e77819e5SLinus Torvalds return error; 313e77819e5SLinus Torvalds } 31484635d68SLinus Torvalds #endif 315e77819e5SLinus Torvalds 316e77819e5SLinus Torvalds return -EAGAIN; 317e77819e5SLinus Torvalds } 318e77819e5SLinus Torvalds 31947291baaSChristian Brauner /** 32047291baaSChristian Brauner * acl_permission_check - perform basic UNIX permission checking 32147291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 32247291baaSChristian Brauner * @inode: inode to check permissions on 32347291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 3245fc475b7SLinus Torvalds * 32547291baaSChristian Brauner * This function performs the basic UNIX permission checking. Since this 32647291baaSChristian Brauner * function may retrieve POSIX acls it needs to know whether it is called from a 32747291baaSChristian Brauner * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. 32847291baaSChristian Brauner * 32947291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 33047291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 33147291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 33247291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 33347291baaSChristian Brauner * raw inode simply passs init_user_ns. 3345909ccaaSLinus Torvalds */ 33547291baaSChristian Brauner static int acl_permission_check(struct user_namespace *mnt_userns, 33647291baaSChristian Brauner struct inode *inode, int mask) 3375909ccaaSLinus Torvalds { 33826cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 33947291baaSChristian Brauner kuid_t i_uid; 3405909ccaaSLinus Torvalds 3415fc475b7SLinus Torvalds /* Are we the owner? If so, ACL's don't matter */ 34247291baaSChristian Brauner i_uid = i_uid_into_mnt(mnt_userns, inode); 34347291baaSChristian Brauner if (likely(uid_eq(current_fsuid(), i_uid))) { 3445fc475b7SLinus Torvalds mask &= 7; 3455909ccaaSLinus Torvalds mode >>= 6; 3465fc475b7SLinus Torvalds return (mask & ~mode) ? -EACCES : 0; 3475fc475b7SLinus Torvalds } 3485fc475b7SLinus Torvalds 3495fc475b7SLinus Torvalds /* Do we have ACL's? */ 350e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 35147291baaSChristian Brauner int error = check_acl(mnt_userns, inode, mask); 3525909ccaaSLinus Torvalds if (error != -EAGAIN) 3535909ccaaSLinus Torvalds return error; 3545909ccaaSLinus Torvalds } 3555909ccaaSLinus Torvalds 3565fc475b7SLinus Torvalds /* Only RWX matters for group/other mode bits */ 3575fc475b7SLinus Torvalds mask &= 7; 3585fc475b7SLinus Torvalds 3595fc475b7SLinus Torvalds /* 3605fc475b7SLinus Torvalds * Are the group permissions different from 3615fc475b7SLinus Torvalds * the other permissions in the bits we care 3625fc475b7SLinus Torvalds * about? Need to check group ownership if so. 3635fc475b7SLinus Torvalds */ 3645fc475b7SLinus Torvalds if (mask & (mode ^ (mode >> 3))) { 36547291baaSChristian Brauner kgid_t kgid = i_gid_into_mnt(mnt_userns, inode); 36647291baaSChristian Brauner if (in_group_p(kgid)) 3675909ccaaSLinus Torvalds mode >>= 3; 3685909ccaaSLinus Torvalds } 3695909ccaaSLinus Torvalds 3705fc475b7SLinus Torvalds /* Bits in 'mode' clear that we require? */ 3715fc475b7SLinus Torvalds return (mask & ~mode) ? -EACCES : 0; 3725909ccaaSLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds /** 3751da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 37647291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 3771da177e4SLinus Torvalds * @inode: inode to check access rights for 3785fc475b7SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, 3795fc475b7SLinus Torvalds * %MAY_NOT_BLOCK ...) 3801da177e4SLinus Torvalds * 3811da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 3821da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 3831da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 384b74c79e9SNick Piggin * are used for other things. 385b74c79e9SNick Piggin * 386b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 387b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 388b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 38947291baaSChristian Brauner * 39047291baaSChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 39147291baaSChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 39247291baaSChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 39347291baaSChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 39447291baaSChristian Brauner * raw inode simply passs init_user_ns. 3951da177e4SLinus Torvalds */ 39647291baaSChristian Brauner int generic_permission(struct user_namespace *mnt_userns, struct inode *inode, 39747291baaSChristian Brauner int mask) 3981da177e4SLinus Torvalds { 3995909ccaaSLinus Torvalds int ret; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* 402948409c7SAndreas Gruenbacher * Do the basic permission checks. 4031da177e4SLinus Torvalds */ 40447291baaSChristian Brauner ret = acl_permission_check(mnt_userns, inode, mask); 4055909ccaaSLinus Torvalds if (ret != -EACCES) 4065909ccaaSLinus Torvalds return ret; 4071da177e4SLinus Torvalds 408d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 409d594e7ecSAl Viro /* DACs are overridable for directories */ 410d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 41147291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 41223adbe12SAndy Lutomirski CAP_DAC_READ_SEARCH)) 413d594e7ecSAl Viro return 0; 41447291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4150558c1bfSChristian Brauner CAP_DAC_OVERRIDE)) 4161da177e4SLinus Torvalds return 0; 4172a4c2242SStephen Smalley return -EACCES; 4182a4c2242SStephen Smalley } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds /* 4211da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 4221da177e4SLinus Torvalds */ 4237ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 424d594e7ecSAl Viro if (mask == MAY_READ) 42547291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4260558c1bfSChristian Brauner CAP_DAC_READ_SEARCH)) 4271da177e4SLinus Torvalds return 0; 4282a4c2242SStephen Smalley /* 4292a4c2242SStephen Smalley * Read/write DACs are always overridable. 4302a4c2242SStephen Smalley * Executable DACs are overridable when there is 4312a4c2242SStephen Smalley * at least one exec bit set. 4322a4c2242SStephen Smalley */ 4332a4c2242SStephen Smalley if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 43447291baaSChristian Brauner if (capable_wrt_inode_uidgid(mnt_userns, inode, 4350558c1bfSChristian Brauner CAP_DAC_OVERRIDE)) 4362a4c2242SStephen Smalley return 0; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds return -EACCES; 4391da177e4SLinus Torvalds } 4404d359507SAl Viro EXPORT_SYMBOL(generic_permission); 4411da177e4SLinus Torvalds 44247291baaSChristian Brauner /** 44347291baaSChristian Brauner * do_inode_permission - UNIX permission checking 44447291baaSChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 44547291baaSChristian Brauner * @inode: inode to check permissions on 44647291baaSChristian Brauner * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) 44747291baaSChristian Brauner * 4483ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 4493ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 4503ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 4513ddcd056SLinus Torvalds * permission function, use the fast case". 4523ddcd056SLinus Torvalds */ 45347291baaSChristian Brauner static inline int do_inode_permission(struct user_namespace *mnt_userns, 45447291baaSChristian Brauner struct inode *inode, int mask) 4553ddcd056SLinus Torvalds { 4563ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 4573ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 458549c7297SChristian Brauner return inode->i_op->permission(mnt_userns, inode, mask); 4593ddcd056SLinus Torvalds 4603ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 4613ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 4623ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 4633ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 4643ddcd056SLinus Torvalds } 46547291baaSChristian Brauner return generic_permission(mnt_userns, inode, mask); 4663ddcd056SLinus Torvalds } 4673ddcd056SLinus Torvalds 468cb23beb5SChristoph Hellwig /** 4690bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 4700bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 47155852635SRandy Dunlap * @inode: Inode to check permission on 4720bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4730bdaea90SDavid Howells * 4740bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 4750bdaea90SDavid Howells */ 4760bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 4770bdaea90SDavid Howells { 4780bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 4790bdaea90SDavid Howells umode_t mode = inode->i_mode; 4800bdaea90SDavid Howells 4810bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 482bc98a42cSDavid Howells if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 4830bdaea90SDavid Howells return -EROFS; 4840bdaea90SDavid Howells } 4850bdaea90SDavid Howells return 0; 4860bdaea90SDavid Howells } 4870bdaea90SDavid Howells 4880bdaea90SDavid Howells /** 4890bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 49047291baaSChristian Brauner * @mnt_userns: User namespace of the mount the inode was found from 4910bdaea90SDavid Howells * @inode: Inode to check permission on 4920bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4930bdaea90SDavid Howells * 4940bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4950bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4960bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4970bdaea90SDavid Howells * 4980bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4990bdaea90SDavid Howells */ 50047291baaSChristian Brauner int inode_permission(struct user_namespace *mnt_userns, 50147291baaSChristian Brauner struct inode *inode, int mask) 5020bdaea90SDavid Howells { 5030bdaea90SDavid Howells int retval; 5040bdaea90SDavid Howells 5050bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 5060bdaea90SDavid Howells if (retval) 5070bdaea90SDavid Howells return retval; 5084bfd054aSEric Biggers 5094bfd054aSEric Biggers if (unlikely(mask & MAY_WRITE)) { 5104bfd054aSEric Biggers /* 5114bfd054aSEric Biggers * Nobody gets write access to an immutable file. 5124bfd054aSEric Biggers */ 5134bfd054aSEric Biggers if (IS_IMMUTABLE(inode)) 5144bfd054aSEric Biggers return -EPERM; 5154bfd054aSEric Biggers 5164bfd054aSEric Biggers /* 5174bfd054aSEric Biggers * Updating mtime will likely cause i_uid and i_gid to be 5184bfd054aSEric Biggers * written back improperly if their true value is unknown 5194bfd054aSEric Biggers * to the vfs. 5204bfd054aSEric Biggers */ 521ba73d987SChristian Brauner if (HAS_UNMAPPED_ID(mnt_userns, inode)) 5224bfd054aSEric Biggers return -EACCES; 5234bfd054aSEric Biggers } 5244bfd054aSEric Biggers 52547291baaSChristian Brauner retval = do_inode_permission(mnt_userns, inode, mask); 5264bfd054aSEric Biggers if (retval) 5274bfd054aSEric Biggers return retval; 5284bfd054aSEric Biggers 5294bfd054aSEric Biggers retval = devcgroup_inode_permission(inode, mask); 5304bfd054aSEric Biggers if (retval) 5314bfd054aSEric Biggers return retval; 5324bfd054aSEric Biggers 5334bfd054aSEric Biggers return security_inode_permission(inode, mask); 5340bdaea90SDavid Howells } 5354d359507SAl Viro EXPORT_SYMBOL(inode_permission); 5360bdaea90SDavid Howells 5370bdaea90SDavid Howells /** 5385dd784d0SJan Blunck * path_get - get a reference to a path 5395dd784d0SJan Blunck * @path: path to get the reference to 5405dd784d0SJan Blunck * 5415dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 5425dd784d0SJan Blunck */ 543dcf787f3SAl Viro void path_get(const struct path *path) 5445dd784d0SJan Blunck { 5455dd784d0SJan Blunck mntget(path->mnt); 5465dd784d0SJan Blunck dget(path->dentry); 5475dd784d0SJan Blunck } 5485dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 5495dd784d0SJan Blunck 5505dd784d0SJan Blunck /** 5511d957f9bSJan Blunck * path_put - put a reference to a path 5521d957f9bSJan Blunck * @path: path to put the reference to 5531d957f9bSJan Blunck * 5541d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 5551d957f9bSJan Blunck */ 556dcf787f3SAl Viro void path_put(const struct path *path) 5571da177e4SLinus Torvalds { 5581d957f9bSJan Blunck dput(path->dentry); 5591d957f9bSJan Blunck mntput(path->mnt); 5601da177e4SLinus Torvalds } 5611d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 5621da177e4SLinus Torvalds 563894bc8c4SAl Viro #define EMBEDDED_LEVELS 2 5641f55a6ecSAl Viro struct nameidata { 5651f55a6ecSAl Viro struct path path; 5661f55a6ecSAl Viro struct qstr last; 5671f55a6ecSAl Viro struct path root; 5681f55a6ecSAl Viro struct inode *inode; /* path.dentry.d_inode */ 569bcba1e7dSAl Viro unsigned int flags, state; 570ab87f9a5SAleksa Sarai unsigned seq, m_seq, r_seq; 5711f55a6ecSAl Viro int last_type; 5721f55a6ecSAl Viro unsigned depth; 573756daf26SNeilBrown int total_link_count; 574697fc6caSAl Viro struct saved { 575697fc6caSAl Viro struct path link; 576fceef393SAl Viro struct delayed_call done; 577697fc6caSAl Viro const char *name; 5780450b2d1SAl Viro unsigned seq; 579894bc8c4SAl Viro } *stack, internal[EMBEDDED_LEVELS]; 5809883d185SAl Viro struct filename *name; 5819883d185SAl Viro struct nameidata *saved; 5829883d185SAl Viro unsigned root_seq; 5839883d185SAl Viro int dfd; 5840f705953SAl Viro kuid_t dir_uid; 5850f705953SAl Viro umode_t dir_mode; 5863859a271SKees Cook } __randomize_layout; 5871f55a6ecSAl Viro 588bcba1e7dSAl Viro #define ND_ROOT_PRESET 1 589bcba1e7dSAl Viro #define ND_ROOT_GRABBED 2 590bcba1e7dSAl Viro #define ND_JUMPED 4 591bcba1e7dSAl Viro 59206422964SAl Viro static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) 593894bc8c4SAl Viro { 594756daf26SNeilBrown struct nameidata *old = current->nameidata; 595756daf26SNeilBrown p->stack = p->internal; 5967962c7d1SAl Viro p->depth = 0; 597c8a53ee5SAl Viro p->dfd = dfd; 598c8a53ee5SAl Viro p->name = name; 5997d01ef75SAl Viro p->path.mnt = NULL; 6007d01ef75SAl Viro p->path.dentry = NULL; 601756daf26SNeilBrown p->total_link_count = old ? old->total_link_count : 0; 6029883d185SAl Viro p->saved = old; 603756daf26SNeilBrown current->nameidata = p; 604894bc8c4SAl Viro } 605894bc8c4SAl Viro 60606422964SAl Viro static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name, 60706422964SAl Viro const struct path *root) 60806422964SAl Viro { 60906422964SAl Viro __set_nameidata(p, dfd, name); 61006422964SAl Viro p->state = 0; 61106422964SAl Viro if (unlikely(root)) { 61206422964SAl Viro p->state = ND_ROOT_PRESET; 61306422964SAl Viro p->root = *root; 61406422964SAl Viro } 61506422964SAl Viro } 61606422964SAl Viro 6179883d185SAl Viro static void restore_nameidata(void) 618894bc8c4SAl Viro { 6199883d185SAl Viro struct nameidata *now = current->nameidata, *old = now->saved; 620756daf26SNeilBrown 621756daf26SNeilBrown current->nameidata = old; 622756daf26SNeilBrown if (old) 623756daf26SNeilBrown old->total_link_count = now->total_link_count; 624e1a63bbcSAl Viro if (now->stack != now->internal) 625756daf26SNeilBrown kfree(now->stack); 626894bc8c4SAl Viro } 627894bc8c4SAl Viro 62860ef60c7SAl Viro static bool nd_alloc_stack(struct nameidata *nd) 629894bc8c4SAl Viro { 630bc40aee0SAl Viro struct saved *p; 631bc40aee0SAl Viro 6326da2ec56SKees Cook p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), 63360ef60c7SAl Viro nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); 634bc40aee0SAl Viro if (unlikely(!p)) 63560ef60c7SAl Viro return false; 636894bc8c4SAl Viro memcpy(p, nd->internal, sizeof(nd->internal)); 637894bc8c4SAl Viro nd->stack = p; 63860ef60c7SAl Viro return true; 639894bc8c4SAl Viro } 640894bc8c4SAl Viro 641397d425dSEric W. Biederman /** 6426b03f7edSAl Viro * path_connected - Verify that a dentry is below mnt.mnt_root 643397d425dSEric W. Biederman * 644397d425dSEric W. Biederman * Rename can sometimes move a file or directory outside of a bind 645397d425dSEric W. Biederman * mount, path_connected allows those cases to be detected. 646397d425dSEric W. Biederman */ 6476b03f7edSAl Viro static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) 648397d425dSEric W. Biederman { 64995dd7758SEric W. Biederman struct super_block *sb = mnt->mnt_sb; 650397d425dSEric W. Biederman 651402dd2cfSChristoph Hellwig /* Bind mounts can have disconnected paths */ 652402dd2cfSChristoph Hellwig if (mnt->mnt_root == sb->s_root) 653397d425dSEric W. Biederman return true; 654397d425dSEric W. Biederman 6556b03f7edSAl Viro return is_subdir(dentry, mnt->mnt_root); 656397d425dSEric W. Biederman } 657397d425dSEric W. Biederman 6587973387aSAl Viro static void drop_links(struct nameidata *nd) 6597973387aSAl Viro { 6607973387aSAl Viro int i = nd->depth; 6617973387aSAl Viro while (i--) { 6627973387aSAl Viro struct saved *last = nd->stack + i; 663fceef393SAl Viro do_delayed_call(&last->done); 664fceef393SAl Viro clear_delayed_call(&last->done); 6657973387aSAl Viro } 6667973387aSAl Viro } 6677973387aSAl Viro 6687973387aSAl Viro static void terminate_walk(struct nameidata *nd) 6697973387aSAl Viro { 6707973387aSAl Viro drop_links(nd); 6717973387aSAl Viro if (!(nd->flags & LOOKUP_RCU)) { 6727973387aSAl Viro int i; 6737973387aSAl Viro path_put(&nd->path); 6747973387aSAl Viro for (i = 0; i < nd->depth; i++) 6757973387aSAl Viro path_put(&nd->stack[i].link); 676bcba1e7dSAl Viro if (nd->state & ND_ROOT_GRABBED) { 677102b8af2SAl Viro path_put(&nd->root); 678bcba1e7dSAl Viro nd->state &= ~ND_ROOT_GRABBED; 679102b8af2SAl Viro } 6807973387aSAl Viro } else { 6817973387aSAl Viro nd->flags &= ~LOOKUP_RCU; 6827973387aSAl Viro rcu_read_unlock(); 6837973387aSAl Viro } 6847973387aSAl Viro nd->depth = 0; 6857d01ef75SAl Viro nd->path.mnt = NULL; 6867d01ef75SAl Viro nd->path.dentry = NULL; 6877973387aSAl Viro } 6887973387aSAl Viro 6897973387aSAl Viro /* path_put is needed afterwards regardless of success or failure */ 6902aa38470SAl Viro static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) 6917973387aSAl Viro { 6922aa38470SAl Viro int res = __legitimize_mnt(path->mnt, mseq); 6937973387aSAl Viro if (unlikely(res)) { 6947973387aSAl Viro if (res > 0) 6957973387aSAl Viro path->mnt = NULL; 6967973387aSAl Viro path->dentry = NULL; 6977973387aSAl Viro return false; 6987973387aSAl Viro } 6997973387aSAl Viro if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { 7007973387aSAl Viro path->dentry = NULL; 7017973387aSAl Viro return false; 7027973387aSAl Viro } 7037973387aSAl Viro return !read_seqcount_retry(&path->dentry->d_seq, seq); 7047973387aSAl Viro } 7057973387aSAl Viro 7062aa38470SAl Viro static inline bool legitimize_path(struct nameidata *nd, 7072aa38470SAl Viro struct path *path, unsigned seq) 7082aa38470SAl Viro { 7095bd73286SAl Viro return __legitimize_path(path, seq, nd->m_seq); 7102aa38470SAl Viro } 7112aa38470SAl Viro 7127973387aSAl Viro static bool legitimize_links(struct nameidata *nd) 7137973387aSAl Viro { 7147973387aSAl Viro int i; 715eacd9aa8SAl Viro if (unlikely(nd->flags & LOOKUP_CACHED)) { 716eacd9aa8SAl Viro drop_links(nd); 717eacd9aa8SAl Viro nd->depth = 0; 718eacd9aa8SAl Viro return false; 719eacd9aa8SAl Viro } 7207973387aSAl Viro for (i = 0; i < nd->depth; i++) { 7217973387aSAl Viro struct saved *last = nd->stack + i; 7227973387aSAl Viro if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { 7237973387aSAl Viro drop_links(nd); 7247973387aSAl Viro nd->depth = i + 1; 7257973387aSAl Viro return false; 7267973387aSAl Viro } 7277973387aSAl Viro } 7287973387aSAl Viro return true; 7297973387aSAl Viro } 7307973387aSAl Viro 731ee594bffSAl Viro static bool legitimize_root(struct nameidata *nd) 732ee594bffSAl Viro { 733adb21d2bSAleksa Sarai /* Nothing to do if nd->root is zero or is managed by the VFS user. */ 734bcba1e7dSAl Viro if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) 735ee594bffSAl Viro return true; 736bcba1e7dSAl Viro nd->state |= ND_ROOT_GRABBED; 737ee594bffSAl Viro return legitimize_path(nd, &nd->root, nd->root_seq); 738ee594bffSAl Viro } 739ee594bffSAl Viro 74019660af7SAl Viro /* 74131e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 74219660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 74319660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 74457e3715cSMike Marshall * normal reference counts on dentries and vfsmounts to transition to ref-walk 74519660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 74619660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 74719660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 74819660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 74931e6b01fSNick Piggin */ 75031e6b01fSNick Piggin 75131e6b01fSNick Piggin /** 752e36cffedSJens Axboe * try_to_unlazy - try to switch to ref-walk mode. 75319660af7SAl Viro * @nd: nameidata pathwalk data 754e36cffedSJens Axboe * Returns: true on success, false on failure 75531e6b01fSNick Piggin * 756e36cffedSJens Axboe * try_to_unlazy attempts to legitimize the current nd->path and nd->root 7574675ac39SAl Viro * for ref-walk mode. 7584675ac39SAl Viro * Must be called from rcu-walk context. 759e36cffedSJens Axboe * Nothing should touch nameidata between try_to_unlazy() failure and 7607973387aSAl Viro * terminate_walk(). 76131e6b01fSNick Piggin */ 762e36cffedSJens Axboe static bool try_to_unlazy(struct nameidata *nd) 76331e6b01fSNick Piggin { 76431e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 76531e6b01fSNick Piggin 76631e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 767e5c832d5SLinus Torvalds 768e5c832d5SLinus Torvalds nd->flags &= ~LOOKUP_RCU; 7697973387aSAl Viro if (unlikely(!legitimize_links(nd))) 7704675ac39SAl Viro goto out1; 77184a2bd39SAl Viro if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) 77284a2bd39SAl Viro goto out; 773ee594bffSAl Viro if (unlikely(!legitimize_root(nd))) 7744675ac39SAl Viro goto out; 7754675ac39SAl Viro rcu_read_unlock(); 7764675ac39SAl Viro BUG_ON(nd->inode != parent->d_inode); 777e36cffedSJens Axboe return true; 7784675ac39SAl Viro 77984a2bd39SAl Viro out1: 7804675ac39SAl Viro nd->path.mnt = NULL; 7814675ac39SAl Viro nd->path.dentry = NULL; 7824675ac39SAl Viro out: 7834675ac39SAl Viro rcu_read_unlock(); 784e36cffedSJens Axboe return false; 7854675ac39SAl Viro } 7864675ac39SAl Viro 7874675ac39SAl Viro /** 788ae66db45SAl Viro * try_to_unlazy_next - try to switch to ref-walk mode. 7894675ac39SAl Viro * @nd: nameidata pathwalk data 790ae66db45SAl Viro * @dentry: next dentry to step into 791ae66db45SAl Viro * @seq: seq number to check @dentry against 792ae66db45SAl Viro * Returns: true on success, false on failure 7934675ac39SAl Viro * 79430476f7eSTom Rix * Similar to try_to_unlazy(), but here we have the next dentry already 795ae66db45SAl Viro * picked by rcu-walk and want to legitimize that in addition to the current 796ae66db45SAl Viro * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. 797ae66db45SAl Viro * Nothing should touch nameidata between try_to_unlazy_next() failure and 7984675ac39SAl Viro * terminate_walk(). 7994675ac39SAl Viro */ 800ae66db45SAl Viro static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq) 8014675ac39SAl Viro { 802*7e4745a0SAl Viro int res; 8034675ac39SAl Viro BUG_ON(!(nd->flags & LOOKUP_RCU)); 8044675ac39SAl Viro 8054675ac39SAl Viro nd->flags &= ~LOOKUP_RCU; 8064675ac39SAl Viro if (unlikely(!legitimize_links(nd))) 8074675ac39SAl Viro goto out2; 808*7e4745a0SAl Viro res = __legitimize_mnt(nd->path.mnt, nd->m_seq); 809*7e4745a0SAl Viro if (unlikely(res)) { 810*7e4745a0SAl Viro if (res > 0) 8117973387aSAl Viro goto out2; 812*7e4745a0SAl Viro goto out1; 813*7e4745a0SAl Viro } 8144675ac39SAl Viro if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) 8157973387aSAl Viro goto out1; 81648a066e7SAl Viro 81715570086SLinus Torvalds /* 8184675ac39SAl Viro * We need to move both the parent and the dentry from the RCU domain 8194675ac39SAl Viro * to be properly refcounted. And the sequence number in the dentry 8204675ac39SAl Viro * validates *both* dentry counters, since we checked the sequence 8214675ac39SAl Viro * number of the parent after we got the child sequence number. So we 8224675ac39SAl Viro * know the parent must still be valid if the child sequence number is 82315570086SLinus Torvalds */ 8244675ac39SAl Viro if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) 825e5c832d5SLinus Torvalds goto out; 82682ef0698SAl Viro if (read_seqcount_retry(&dentry->d_seq, seq)) 82784a2bd39SAl Viro goto out_dput; 828e5c832d5SLinus Torvalds /* 829e5c832d5SLinus Torvalds * Sequence counts matched. Now make sure that the root is 830e5c832d5SLinus Torvalds * still valid and get it if required. 831e5c832d5SLinus Torvalds */ 83284a2bd39SAl Viro if (unlikely(!legitimize_root(nd))) 83384a2bd39SAl Viro goto out_dput; 8348b61e74fSAl Viro rcu_read_unlock(); 835ae66db45SAl Viro return true; 83619660af7SAl Viro 8377973387aSAl Viro out2: 8387973387aSAl Viro nd->path.mnt = NULL; 8397973387aSAl Viro out1: 8407973387aSAl Viro nd->path.dentry = NULL; 841e5c832d5SLinus Torvalds out: 8428b61e74fSAl Viro rcu_read_unlock(); 843ae66db45SAl Viro return false; 84484a2bd39SAl Viro out_dput: 84584a2bd39SAl Viro rcu_read_unlock(); 84684a2bd39SAl Viro dput(dentry); 847ae66db45SAl Viro return false; 84831e6b01fSNick Piggin } 84931e6b01fSNick Piggin 8504ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 85134286d66SNick Piggin { 852a89f8337SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) 8534ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 854a89f8337SAl Viro else 855a89f8337SAl Viro return 1; 85634286d66SNick Piggin } 85734286d66SNick Piggin 8589f1fafeeSAl Viro /** 8599f1fafeeSAl Viro * complete_walk - successful completion of path walk 8609f1fafeeSAl Viro * @nd: pointer nameidata 86139159de2SJeff Layton * 8629f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 8639f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 8649f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 8659f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 8669f1fafeeSAl Viro * need to drop nd->path. 86739159de2SJeff Layton */ 8689f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 86939159de2SJeff Layton { 87016c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 87139159de2SJeff Layton int status; 87239159de2SJeff Layton 8739f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 874adb21d2bSAleksa Sarai /* 875adb21d2bSAleksa Sarai * We don't want to zero nd->root for scoped-lookups or 876adb21d2bSAleksa Sarai * externally-managed nd->root. 877adb21d2bSAleksa Sarai */ 878bcba1e7dSAl Viro if (!(nd->state & ND_ROOT_PRESET)) 879bcba1e7dSAl Viro if (!(nd->flags & LOOKUP_IS_SCOPED)) 8809f1fafeeSAl Viro nd->root.mnt = NULL; 8816c6ec2b0SJens Axboe nd->flags &= ~LOOKUP_CACHED; 882e36cffedSJens Axboe if (!try_to_unlazy(nd)) 88348a066e7SAl Viro return -ECHILD; 88448a066e7SAl Viro } 8859f1fafeeSAl Viro 886adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 887adb21d2bSAleksa Sarai /* 888adb21d2bSAleksa Sarai * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't 889adb21d2bSAleksa Sarai * ever step outside the root during lookup" and should already 890adb21d2bSAleksa Sarai * be guaranteed by the rest of namei, we want to avoid a namei 891adb21d2bSAleksa Sarai * BUG resulting in userspace being given a path that was not 892adb21d2bSAleksa Sarai * scoped within the root at some point during the lookup. 893adb21d2bSAleksa Sarai * 894adb21d2bSAleksa Sarai * So, do a final sanity-check to make sure that in the 895adb21d2bSAleksa Sarai * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) 896adb21d2bSAleksa Sarai * we won't silently return an fd completely outside of the 897adb21d2bSAleksa Sarai * requested root to userspace. 898adb21d2bSAleksa Sarai * 899adb21d2bSAleksa Sarai * Userspace could move the path outside the root after this 900adb21d2bSAleksa Sarai * check, but as discussed elsewhere this is not a concern (the 901adb21d2bSAleksa Sarai * resolved file was inside the root at some point). 902adb21d2bSAleksa Sarai */ 903adb21d2bSAleksa Sarai if (!path_is_under(&nd->path, &nd->root)) 904adb21d2bSAleksa Sarai return -EXDEV; 905adb21d2bSAleksa Sarai } 906adb21d2bSAleksa Sarai 907bcba1e7dSAl Viro if (likely(!(nd->state & ND_JUMPED))) 90839159de2SJeff Layton return 0; 90939159de2SJeff Layton 910ecf3d1f1SJeff Layton if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 91116c2cd71SAl Viro return 0; 91216c2cd71SAl Viro 913ecf3d1f1SJeff Layton status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 91439159de2SJeff Layton if (status > 0) 91539159de2SJeff Layton return 0; 91639159de2SJeff Layton 91716c2cd71SAl Viro if (!status) 91839159de2SJeff Layton status = -ESTALE; 91916c2cd71SAl Viro 92039159de2SJeff Layton return status; 92139159de2SJeff Layton } 92239159de2SJeff Layton 923740a1678SAleksa Sarai static int set_root(struct nameidata *nd) 9242a737871SAl Viro { 92531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 9269e6697e2SAl Viro 927adb21d2bSAleksa Sarai /* 928adb21d2bSAleksa Sarai * Jumping to the real root in a scoped-lookup is a BUG in namei, but we 929adb21d2bSAleksa Sarai * still have to ensure it doesn't happen because it will cause a breakout 930adb21d2bSAleksa Sarai * from the dirfd. 931adb21d2bSAleksa Sarai */ 932adb21d2bSAleksa Sarai if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) 933adb21d2bSAleksa Sarai return -ENOTRECOVERABLE; 934adb21d2bSAleksa Sarai 9359e6697e2SAl Viro if (nd->flags & LOOKUP_RCU) { 9368f47a016SAl Viro unsigned seq; 937c28cc364SNick Piggin 938c28cc364SNick Piggin do { 939c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 94031e6b01fSNick Piggin nd->root = fs->root; 9418f47a016SAl Viro nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 942c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 9439e6697e2SAl Viro } else { 9449e6697e2SAl Viro get_fs_root(fs, &nd->root); 945bcba1e7dSAl Viro nd->state |= ND_ROOT_GRABBED; 9469e6697e2SAl Viro } 947740a1678SAleksa Sarai return 0; 94831e6b01fSNick Piggin } 94931e6b01fSNick Piggin 950248fb5b9SAl Viro static int nd_jump_root(struct nameidata *nd) 951248fb5b9SAl Viro { 952adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_BENEATH)) 953adb21d2bSAleksa Sarai return -EXDEV; 95472ba2929SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 95572ba2929SAleksa Sarai /* Absolute path arguments to path_init() are allowed. */ 95672ba2929SAleksa Sarai if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) 95772ba2929SAleksa Sarai return -EXDEV; 95872ba2929SAleksa Sarai } 959740a1678SAleksa Sarai if (!nd->root.mnt) { 960740a1678SAleksa Sarai int error = set_root(nd); 961740a1678SAleksa Sarai if (error) 962740a1678SAleksa Sarai return error; 963740a1678SAleksa Sarai } 964248fb5b9SAl Viro if (nd->flags & LOOKUP_RCU) { 965248fb5b9SAl Viro struct dentry *d; 966248fb5b9SAl Viro nd->path = nd->root; 967248fb5b9SAl Viro d = nd->path.dentry; 968248fb5b9SAl Viro nd->inode = d->d_inode; 969248fb5b9SAl Viro nd->seq = nd->root_seq; 97082ef0698SAl Viro if (read_seqcount_retry(&d->d_seq, nd->seq)) 971248fb5b9SAl Viro return -ECHILD; 972248fb5b9SAl Viro } else { 973248fb5b9SAl Viro path_put(&nd->path); 974248fb5b9SAl Viro nd->path = nd->root; 975248fb5b9SAl Viro path_get(&nd->path); 976248fb5b9SAl Viro nd->inode = nd->path.dentry->d_inode; 977248fb5b9SAl Viro } 978bcba1e7dSAl Viro nd->state |= ND_JUMPED; 979248fb5b9SAl Viro return 0; 980248fb5b9SAl Viro } 981248fb5b9SAl Viro 982b5fb63c1SChristoph Hellwig /* 9836b255391SAl Viro * Helper to directly jump to a known parsed path from ->get_link, 984b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 985b5fb63c1SChristoph Hellwig */ 9861bc82070SAleksa Sarai int nd_jump_link(struct path *path) 987b5fb63c1SChristoph Hellwig { 9884b99d499SAleksa Sarai int error = -ELOOP; 9896e77137bSAl Viro struct nameidata *nd = current->nameidata; 990b5fb63c1SChristoph Hellwig 9914b99d499SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) 9924b99d499SAleksa Sarai goto err; 9934b99d499SAleksa Sarai 99472ba2929SAleksa Sarai error = -EXDEV; 99572ba2929SAleksa Sarai if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { 99672ba2929SAleksa Sarai if (nd->path.mnt != path->mnt) 99772ba2929SAleksa Sarai goto err; 99872ba2929SAleksa Sarai } 999adb21d2bSAleksa Sarai /* Not currently safe for scoped-lookups. */ 1000adb21d2bSAleksa Sarai if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) 1001adb21d2bSAleksa Sarai goto err; 100272ba2929SAleksa Sarai 10034b99d499SAleksa Sarai path_put(&nd->path); 1004b5fb63c1SChristoph Hellwig nd->path = *path; 1005b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 1006bcba1e7dSAl Viro nd->state |= ND_JUMPED; 10071bc82070SAleksa Sarai return 0; 10084b99d499SAleksa Sarai 10094b99d499SAleksa Sarai err: 10104b99d499SAleksa Sarai path_put(path); 10114b99d499SAleksa Sarai return error; 1012b5fb63c1SChristoph Hellwig } 1013b5fb63c1SChristoph Hellwig 1014b9ff4429SAl Viro static inline void put_link(struct nameidata *nd) 1015574197e0SAl Viro { 101621c3003dSAl Viro struct saved *last = nd->stack + --nd->depth; 1017fceef393SAl Viro do_delayed_call(&last->done); 10186548fae2SAl Viro if (!(nd->flags & LOOKUP_RCU)) 1019b9ff4429SAl Viro path_put(&last->link); 1020574197e0SAl Viro } 1021574197e0SAl Viro 10229c011be1SLuis Chamberlain static int sysctl_protected_symlinks __read_mostly; 10239c011be1SLuis Chamberlain static int sysctl_protected_hardlinks __read_mostly; 10249c011be1SLuis Chamberlain static int sysctl_protected_fifos __read_mostly; 10259c011be1SLuis Chamberlain static int sysctl_protected_regular __read_mostly; 10269c011be1SLuis Chamberlain 10279c011be1SLuis Chamberlain #ifdef CONFIG_SYSCTL 10289c011be1SLuis Chamberlain static struct ctl_table namei_sysctls[] = { 10299c011be1SLuis Chamberlain { 10309c011be1SLuis Chamberlain .procname = "protected_symlinks", 10319c011be1SLuis Chamberlain .data = &sysctl_protected_symlinks, 10329c011be1SLuis Chamberlain .maxlen = sizeof(int), 1033c7031c14SJulius Hemanth Pitti .mode = 0644, 10349c011be1SLuis Chamberlain .proc_handler = proc_dointvec_minmax, 10359c011be1SLuis Chamberlain .extra1 = SYSCTL_ZERO, 10369c011be1SLuis Chamberlain .extra2 = SYSCTL_ONE, 10379c011be1SLuis Chamberlain }, 10389c011be1SLuis Chamberlain { 10399c011be1SLuis Chamberlain .procname = "protected_hardlinks", 10409c011be1SLuis Chamberlain .data = &sysctl_protected_hardlinks, 10419c011be1SLuis Chamberlain .maxlen = sizeof(int), 1042c7031c14SJulius Hemanth Pitti .mode = 0644, 10439c011be1SLuis Chamberlain .proc_handler = proc_dointvec_minmax, 10449c011be1SLuis Chamberlain .extra1 = SYSCTL_ZERO, 10459c011be1SLuis Chamberlain .extra2 = SYSCTL_ONE, 10469c011be1SLuis Chamberlain }, 10479c011be1SLuis Chamberlain { 10489c011be1SLuis Chamberlain .procname = "protected_fifos", 10499c011be1SLuis Chamberlain .data = &sysctl_protected_fifos, 10509c011be1SLuis Chamberlain .maxlen = sizeof(int), 1051c7031c14SJulius Hemanth Pitti .mode = 0644, 10529c011be1SLuis Chamberlain .proc_handler = proc_dointvec_minmax, 10539c011be1SLuis Chamberlain .extra1 = SYSCTL_ZERO, 10549c011be1SLuis Chamberlain .extra2 = SYSCTL_TWO, 10559c011be1SLuis Chamberlain }, 10569c011be1SLuis Chamberlain { 10579c011be1SLuis Chamberlain .procname = "protected_regular", 10589c011be1SLuis Chamberlain .data = &sysctl_protected_regular, 10599c011be1SLuis Chamberlain .maxlen = sizeof(int), 1060c7031c14SJulius Hemanth Pitti .mode = 0644, 10619c011be1SLuis Chamberlain .proc_handler = proc_dointvec_minmax, 10629c011be1SLuis Chamberlain .extra1 = SYSCTL_ZERO, 10639c011be1SLuis Chamberlain .extra2 = SYSCTL_TWO, 10649c011be1SLuis Chamberlain }, 10659c011be1SLuis Chamberlain { } 10669c011be1SLuis Chamberlain }; 10679c011be1SLuis Chamberlain 10689c011be1SLuis Chamberlain static int __init init_fs_namei_sysctls(void) 10699c011be1SLuis Chamberlain { 10709c011be1SLuis Chamberlain register_sysctl_init("fs", namei_sysctls); 10719c011be1SLuis Chamberlain return 0; 10729c011be1SLuis Chamberlain } 10739c011be1SLuis Chamberlain fs_initcall(init_fs_namei_sysctls); 10749c011be1SLuis Chamberlain 10759c011be1SLuis Chamberlain #endif /* CONFIG_SYSCTL */ 1076800179c9SKees Cook 1077800179c9SKees Cook /** 1078800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 107955852635SRandy Dunlap * @nd: nameidata pathwalk data 1080800179c9SKees Cook * 1081800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 1082800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 1083800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 1084800179c9SKees Cook * processes from failing races against path names that may change out 1085800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 1086800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 1087800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 1088800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 1089800179c9SKees Cook * 1090800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 1091800179c9SKees Cook */ 1092ad6cc4c3SAl Viro static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) 1093800179c9SKees Cook { 1094ba73d987SChristian Brauner struct user_namespace *mnt_userns; 1095ba73d987SChristian Brauner kuid_t i_uid; 1096ba73d987SChristian Brauner 1097800179c9SKees Cook if (!sysctl_protected_symlinks) 1098800179c9SKees Cook return 0; 1099800179c9SKees Cook 1100ba73d987SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 1101ba73d987SChristian Brauner i_uid = i_uid_into_mnt(mnt_userns, inode); 1102800179c9SKees Cook /* Allowed if owner and follower match. */ 1103ba73d987SChristian Brauner if (uid_eq(current_cred()->fsuid, i_uid)) 1104800179c9SKees Cook return 0; 1105800179c9SKees Cook 1106800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 11070f705953SAl Viro if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 1108800179c9SKees Cook return 0; 1109800179c9SKees Cook 1110800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 1111ba73d987SChristian Brauner if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid)) 1112800179c9SKees Cook return 0; 1113800179c9SKees Cook 111431956502SAl Viro if (nd->flags & LOOKUP_RCU) 111531956502SAl Viro return -ECHILD; 111631956502SAl Viro 1117ea841bafSRichard Guy Briggs audit_inode(nd->name, nd->stack[0].link.dentry, 0); 1118245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); 1119800179c9SKees Cook return -EACCES; 1120800179c9SKees Cook } 1121800179c9SKees Cook 1122800179c9SKees Cook /** 1123800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 1124ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1125800179c9SKees Cook * @inode: the source inode to hardlink from 1126800179c9SKees Cook * 1127800179c9SKees Cook * Return false if at least one of the following conditions: 1128800179c9SKees Cook * - inode is not a regular file 1129800179c9SKees Cook * - inode is setuid 1130800179c9SKees Cook * - inode is setgid and group-exec 1131800179c9SKees Cook * - access failure for read and write 1132800179c9SKees Cook * 1133800179c9SKees Cook * Otherwise returns true. 1134800179c9SKees Cook */ 1135ba73d987SChristian Brauner static bool safe_hardlink_source(struct user_namespace *mnt_userns, 1136ba73d987SChristian Brauner struct inode *inode) 1137800179c9SKees Cook { 1138800179c9SKees Cook umode_t mode = inode->i_mode; 1139800179c9SKees Cook 1140800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 1141800179c9SKees Cook if (!S_ISREG(mode)) 1142800179c9SKees Cook return false; 1143800179c9SKees Cook 1144800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 1145800179c9SKees Cook if (mode & S_ISUID) 1146800179c9SKees Cook return false; 1147800179c9SKees Cook 1148800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 1149800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 1150800179c9SKees Cook return false; 1151800179c9SKees Cook 1152800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 1153ba73d987SChristian Brauner if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE)) 1154800179c9SKees Cook return false; 1155800179c9SKees Cook 1156800179c9SKees Cook return true; 1157800179c9SKees Cook } 1158800179c9SKees Cook 1159800179c9SKees Cook /** 1160800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 1161ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 1162800179c9SKees Cook * @link: the source to hardlink from 1163800179c9SKees Cook * 1164800179c9SKees Cook * Block hardlink when all of: 1165800179c9SKees Cook * - sysctl_protected_hardlinks enabled 1166800179c9SKees Cook * - fsuid does not match inode 1167800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 1168f2ca3796SDirk Steinmetz * - not CAP_FOWNER in a namespace with the inode owner uid mapped 1169800179c9SKees Cook * 1170ba73d987SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 1171ba73d987SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 1172ba73d987SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 1173ba73d987SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 1174ba73d987SChristian Brauner * raw inode simply passs init_user_ns. 1175ba73d987SChristian Brauner * 1176800179c9SKees Cook * Returns 0 if successful, -ve on error. 1177800179c9SKees Cook */ 1178ba73d987SChristian Brauner int may_linkat(struct user_namespace *mnt_userns, struct path *link) 1179800179c9SKees Cook { 1180593d1ce8SEric W. Biederman struct inode *inode = link->dentry->d_inode; 1181593d1ce8SEric W. Biederman 1182593d1ce8SEric W. Biederman /* Inode writeback is not safe when the uid or gid are invalid. */ 1183ba73d987SChristian Brauner if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || 1184ba73d987SChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 1185593d1ce8SEric W. Biederman return -EOVERFLOW; 1186800179c9SKees Cook 1187800179c9SKees Cook if (!sysctl_protected_hardlinks) 1188800179c9SKees Cook return 0; 1189800179c9SKees Cook 1190800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 1191800179c9SKees Cook * otherwise, it must be a safe source. 1192800179c9SKees Cook */ 1193ba73d987SChristian Brauner if (safe_hardlink_source(mnt_userns, inode) || 1194ba73d987SChristian Brauner inode_owner_or_capable(mnt_userns, inode)) 1195800179c9SKees Cook return 0; 1196800179c9SKees Cook 1197245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); 1198800179c9SKees Cook return -EPERM; 1199800179c9SKees Cook } 1200800179c9SKees Cook 120130aba665SSalvatore Mesoraca /** 120230aba665SSalvatore Mesoraca * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory 120330aba665SSalvatore Mesoraca * should be allowed, or not, on files that already 120430aba665SSalvatore Mesoraca * exist. 1205ba73d987SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 12062111c3c0SRandy Dunlap * @nd: nameidata pathwalk data 120730aba665SSalvatore Mesoraca * @inode: the inode of the file to open 120830aba665SSalvatore Mesoraca * 120930aba665SSalvatore Mesoraca * Block an O_CREAT open of a FIFO (or a regular file) when: 121030aba665SSalvatore Mesoraca * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled 121130aba665SSalvatore Mesoraca * - the file already exists 121230aba665SSalvatore Mesoraca * - we are in a sticky directory 121330aba665SSalvatore Mesoraca * - we don't own the file 121430aba665SSalvatore Mesoraca * - the owner of the directory doesn't own the file 121530aba665SSalvatore Mesoraca * - the directory is world writable 121630aba665SSalvatore Mesoraca * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 121730aba665SSalvatore Mesoraca * the directory doesn't have to be world writable: being group writable will 121830aba665SSalvatore Mesoraca * be enough. 121930aba665SSalvatore Mesoraca * 1220ba73d987SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 1221ba73d987SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 1222ba73d987SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 1223ba73d987SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 1224ba73d987SChristian Brauner * raw inode simply passs init_user_ns. 1225ba73d987SChristian Brauner * 122630aba665SSalvatore Mesoraca * Returns 0 if the open is allowed, -ve on error. 122730aba665SSalvatore Mesoraca */ 1228ba73d987SChristian Brauner static int may_create_in_sticky(struct user_namespace *mnt_userns, 1229ba73d987SChristian Brauner struct nameidata *nd, struct inode *const inode) 123030aba665SSalvatore Mesoraca { 1231ba73d987SChristian Brauner umode_t dir_mode = nd->dir_mode; 1232ba73d987SChristian Brauner kuid_t dir_uid = nd->dir_uid; 1233ba73d987SChristian Brauner 123430aba665SSalvatore Mesoraca if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || 123530aba665SSalvatore Mesoraca (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || 1236d0cb5018SAl Viro likely(!(dir_mode & S_ISVTX)) || 1237ba73d987SChristian Brauner uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) || 1238ba73d987SChristian Brauner uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) 123930aba665SSalvatore Mesoraca return 0; 124030aba665SSalvatore Mesoraca 1241d0cb5018SAl Viro if (likely(dir_mode & 0002) || 1242d0cb5018SAl Viro (dir_mode & 0020 && 124330aba665SSalvatore Mesoraca ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || 124430aba665SSalvatore Mesoraca (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { 1245245d7369SKees Cook const char *operation = S_ISFIFO(inode->i_mode) ? 1246245d7369SKees Cook "sticky_create_fifo" : 1247245d7369SKees Cook "sticky_create_regular"; 1248245d7369SKees Cook audit_log_path_denied(AUDIT_ANOM_CREAT, operation); 124930aba665SSalvatore Mesoraca return -EACCES; 125030aba665SSalvatore Mesoraca } 125130aba665SSalvatore Mesoraca return 0; 125230aba665SSalvatore Mesoraca } 125330aba665SSalvatore Mesoraca 1254f015f126SDavid Howells /* 1255f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 1256f015f126SDavid Howells * 1257f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 1258f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 1259f015f126SDavid Howells * Up is towards /. 1260f015f126SDavid Howells * 1261f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 1262f015f126SDavid Howells * root. 1263f015f126SDavid Howells */ 1264bab77ebfSAl Viro int follow_up(struct path *path) 12651da177e4SLinus Torvalds { 12660714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 12670714a533SAl Viro struct mount *parent; 12681da177e4SLinus Torvalds struct dentry *mountpoint; 126999b7db7bSNick Piggin 127048a066e7SAl Viro read_seqlock_excl(&mount_lock); 12710714a533SAl Viro parent = mnt->mnt_parent; 12723c0a6163SAl Viro if (parent == mnt) { 127348a066e7SAl Viro read_sequnlock_excl(&mount_lock); 12741da177e4SLinus Torvalds return 0; 12751da177e4SLinus Torvalds } 12760714a533SAl Viro mntget(&parent->mnt); 1277a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 127848a066e7SAl Viro read_sequnlock_excl(&mount_lock); 1279bab77ebfSAl Viro dput(path->dentry); 1280bab77ebfSAl Viro path->dentry = mountpoint; 1281bab77ebfSAl Viro mntput(path->mnt); 12820714a533SAl Viro path->mnt = &parent->mnt; 12831da177e4SLinus Torvalds return 1; 12841da177e4SLinus Torvalds } 12854d359507SAl Viro EXPORT_SYMBOL(follow_up); 12861da177e4SLinus Torvalds 12877ef482faSAl Viro static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, 12887ef482faSAl Viro struct path *path, unsigned *seqp) 12897ef482faSAl Viro { 12907ef482faSAl Viro while (mnt_has_parent(m)) { 12917ef482faSAl Viro struct dentry *mountpoint = m->mnt_mountpoint; 12927ef482faSAl Viro 12937ef482faSAl Viro m = m->mnt_parent; 12947ef482faSAl Viro if (unlikely(root->dentry == mountpoint && 12957ef482faSAl Viro root->mnt == &m->mnt)) 12967ef482faSAl Viro break; 12977ef482faSAl Viro if (mountpoint != m->mnt.mnt_root) { 12987ef482faSAl Viro path->mnt = &m->mnt; 12997ef482faSAl Viro path->dentry = mountpoint; 13007ef482faSAl Viro *seqp = read_seqcount_begin(&mountpoint->d_seq); 13017ef482faSAl Viro return true; 13027ef482faSAl Viro } 13037ef482faSAl Viro } 13047ef482faSAl Viro return false; 13057ef482faSAl Viro } 13067ef482faSAl Viro 13072aa38470SAl Viro static bool choose_mountpoint(struct mount *m, const struct path *root, 13082aa38470SAl Viro struct path *path) 13092aa38470SAl Viro { 13102aa38470SAl Viro bool found; 13112aa38470SAl Viro 13122aa38470SAl Viro rcu_read_lock(); 13132aa38470SAl Viro while (1) { 13142aa38470SAl Viro unsigned seq, mseq = read_seqbegin(&mount_lock); 13152aa38470SAl Viro 13162aa38470SAl Viro found = choose_mountpoint_rcu(m, root, path, &seq); 13172aa38470SAl Viro if (unlikely(!found)) { 13182aa38470SAl Viro if (!read_seqretry(&mount_lock, mseq)) 13192aa38470SAl Viro break; 13202aa38470SAl Viro } else { 13212aa38470SAl Viro if (likely(__legitimize_path(path, seq, mseq))) 13222aa38470SAl Viro break; 13232aa38470SAl Viro rcu_read_unlock(); 13242aa38470SAl Viro path_put(path); 13252aa38470SAl Viro rcu_read_lock(); 13262aa38470SAl Viro } 13272aa38470SAl Viro } 13282aa38470SAl Viro rcu_read_unlock(); 13292aa38470SAl Viro return found; 13302aa38470SAl Viro } 13312aa38470SAl Viro 1332b5c84bf6SNick Piggin /* 13339875cf80SDavid Howells * Perform an automount 13349875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 13359875cf80SDavid Howells * were called with. 13361da177e4SLinus Torvalds */ 13371c9f5e06SAl Viro static int follow_automount(struct path *path, int *count, unsigned lookup_flags) 133831e6b01fSNick Piggin { 133925e195aaSAl Viro struct dentry *dentry = path->dentry; 13409875cf80SDavid Howells 13410ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 13420ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 13430ec26fd0SMiklos Szeredi * the name. 13440ec26fd0SMiklos Szeredi * 13450ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 13465a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 13470ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 13480ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 13490ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 13500ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 13515a30d8a2SDavid Howells */ 13521c9f5e06SAl Viro if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 13535d38f049SIan Kent LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 135425e195aaSAl Viro dentry->d_inode) 13559875cf80SDavid Howells return -EISDIR; 13560ec26fd0SMiklos Szeredi 13571c9f5e06SAl Viro if (count && (*count)++ >= MAXSYMLINKS) 13589875cf80SDavid Howells return -ELOOP; 13599875cf80SDavid Howells 136025e195aaSAl Viro return finish_automount(dentry->d_op->d_automount(path), path); 1361ea5b778aSDavid Howells } 13629875cf80SDavid Howells 13639875cf80SDavid Howells /* 13649deed3ebSAl Viro * mount traversal - out-of-line part. One note on ->d_flags accesses - 13659deed3ebSAl Viro * dentries are pinned but not locked here, so negative dentry can go 13669deed3ebSAl Viro * positive right under us. Use of smp_load_acquire() provides a barrier 13679deed3ebSAl Viro * sufficient for ->d_inode and ->d_flags consistency. 13689875cf80SDavid Howells */ 13699deed3ebSAl Viro static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, 13709deed3ebSAl Viro int *count, unsigned lookup_flags) 13719875cf80SDavid Howells { 13729deed3ebSAl Viro struct vfsmount *mnt = path->mnt; 13739875cf80SDavid Howells bool need_mntput = false; 13748aef1884SAl Viro int ret = 0; 13759875cf80SDavid Howells 13769deed3ebSAl Viro while (flags & DCACHE_MANAGED_DENTRY) { 1377cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1378cc53ce53SDavid Howells * being held. */ 1379d41efb52SAl Viro if (flags & DCACHE_MANAGE_TRANSIT) { 1380fb5f51c7SIan Kent ret = path->dentry->d_op->d_manage(path, false); 1381508c8772SAl Viro flags = smp_load_acquire(&path->dentry->d_flags); 1382cc53ce53SDavid Howells if (ret < 0) 13838aef1884SAl Viro break; 1384cc53ce53SDavid Howells } 1385cc53ce53SDavid Howells 13869deed3ebSAl Viro if (flags & DCACHE_MOUNTED) { // something's mounted on it.. 13879875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 13889deed3ebSAl Viro if (mounted) { // ... in our namespace 13899875cf80SDavid Howells dput(path->dentry); 13909875cf80SDavid Howells if (need_mntput) 1391463ffb2eSAl Viro mntput(path->mnt); 1392463ffb2eSAl Viro path->mnt = mounted; 1393463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 13949deed3ebSAl Viro // here we know it's positive 13959deed3ebSAl Viro flags = path->dentry->d_flags; 13969875cf80SDavid Howells need_mntput = true; 13979875cf80SDavid Howells continue; 1398463ffb2eSAl Viro } 13991da177e4SLinus Torvalds } 14009875cf80SDavid Howells 14019deed3ebSAl Viro if (!(flags & DCACHE_NEED_AUTOMOUNT)) 14029deed3ebSAl Viro break; 14039deed3ebSAl Viro 14049deed3ebSAl Viro // uncovered automount point 14059deed3ebSAl Viro ret = follow_automount(path, count, lookup_flags); 14069deed3ebSAl Viro flags = smp_load_acquire(&path->dentry->d_flags); 14079875cf80SDavid Howells if (ret < 0) 14088aef1884SAl Viro break; 14099875cf80SDavid Howells } 14109875cf80SDavid Howells 14119deed3ebSAl Viro if (ret == -EISDIR) 14129deed3ebSAl Viro ret = 0; 14139deed3ebSAl Viro // possible if you race with several mount --move 14149deed3ebSAl Viro if (need_mntput && path->mnt == mnt) 14158aef1884SAl Viro mntput(path->mnt); 14169deed3ebSAl Viro if (!ret && unlikely(d_flags_negative(flags))) 1417d41efb52SAl Viro ret = -ENOENT; 14189deed3ebSAl Viro *jumped = need_mntput; 14198402752eSAl Viro return ret; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14229deed3ebSAl Viro static inline int traverse_mounts(struct path *path, bool *jumped, 14239deed3ebSAl Viro int *count, unsigned lookup_flags) 14249deed3ebSAl Viro { 14259deed3ebSAl Viro unsigned flags = smp_load_acquire(&path->dentry->d_flags); 14269deed3ebSAl Viro 14279deed3ebSAl Viro /* fastpath */ 14289deed3ebSAl Viro if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { 14299deed3ebSAl Viro *jumped = false; 14309deed3ebSAl Viro if (unlikely(d_flags_negative(flags))) 14319deed3ebSAl Viro return -ENOENT; 14329deed3ebSAl Viro return 0; 14339deed3ebSAl Viro } 14349deed3ebSAl Viro return __traverse_mounts(path, flags, jumped, count, lookup_flags); 14359deed3ebSAl Viro } 14369deed3ebSAl Viro 1437cc53ce53SDavid Howells int follow_down_one(struct path *path) 14381da177e4SLinus Torvalds { 14391da177e4SLinus Torvalds struct vfsmount *mounted; 14401da177e4SLinus Torvalds 14411c755af4SAl Viro mounted = lookup_mnt(path); 14421da177e4SLinus Torvalds if (mounted) { 14439393bd07SAl Viro dput(path->dentry); 14449393bd07SAl Viro mntput(path->mnt); 14459393bd07SAl Viro path->mnt = mounted; 14469393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 14471da177e4SLinus Torvalds return 1; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds return 0; 14501da177e4SLinus Torvalds } 14514d359507SAl Viro EXPORT_SYMBOL(follow_down_one); 14521da177e4SLinus Torvalds 14539875cf80SDavid Howells /* 14549deed3ebSAl Viro * Follow down to the covering mount currently visible to userspace. At each 14559deed3ebSAl Viro * point, the filesystem owning that dentry may be queried as to whether the 14569deed3ebSAl Viro * caller is permitted to proceed or not. 14579deed3ebSAl Viro */ 14589deed3ebSAl Viro int follow_down(struct path *path) 14599deed3ebSAl Viro { 14609deed3ebSAl Viro struct vfsmount *mnt = path->mnt; 14619deed3ebSAl Viro bool jumped; 14629deed3ebSAl Viro int ret = traverse_mounts(path, &jumped, NULL, 0); 14639deed3ebSAl Viro 14649deed3ebSAl Viro if (path->mnt != mnt) 14659deed3ebSAl Viro mntput(mnt); 14669deed3ebSAl Viro return ret; 14679deed3ebSAl Viro } 14689deed3ebSAl Viro EXPORT_SYMBOL(follow_down); 14699deed3ebSAl Viro 14709deed3ebSAl Viro /* 1471287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1472287548e4SAl Viro * we meet a managed dentry that would need blocking. 14739875cf80SDavid Howells */ 14749875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1475254cf582SAl Viro struct inode **inode, unsigned *seqp) 14769875cf80SDavid Howells { 1477ea936aebSAl Viro struct dentry *dentry = path->dentry; 1478ea936aebSAl Viro unsigned int flags = dentry->d_flags; 1479ea936aebSAl Viro 1480ea936aebSAl Viro if (likely(!(flags & DCACHE_MANAGED_DENTRY))) 1481ea936aebSAl Viro return true; 1482ea936aebSAl Viro 1483ea936aebSAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1484ea936aebSAl Viro return false; 1485ea936aebSAl Viro 148662a7375eSIan Kent for (;;) { 148762a7375eSIan Kent /* 148862a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 148962a7375eSIan Kent * that wants to block transit. 149062a7375eSIan Kent */ 1491ea936aebSAl Viro if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { 1492ea936aebSAl Viro int res = dentry->d_op->d_manage(path, true); 1493ea936aebSAl Viro if (res) 1494ea936aebSAl Viro return res == -EISDIR; 1495ea936aebSAl Viro flags = dentry->d_flags; 1496b8faf035SNeilBrown } 149762a7375eSIan Kent 1498ea936aebSAl Viro if (flags & DCACHE_MOUNTED) { 1499ea936aebSAl Viro struct mount *mounted = __lookup_mnt(path->mnt, dentry); 1500ea936aebSAl Viro if (mounted) { 1501c7105365SAl Viro path->mnt = &mounted->mnt; 1502ea936aebSAl Viro dentry = path->dentry = mounted->mnt.mnt_root; 1503bcba1e7dSAl Viro nd->state |= ND_JUMPED; 1504ea936aebSAl Viro *seqp = read_seqcount_begin(&dentry->d_seq); 1505ea936aebSAl Viro *inode = dentry->d_inode; 150659430262SLinus Torvalds /* 1507ea936aebSAl Viro * We don't need to re-check ->d_seq after this 1508ea936aebSAl Viro * ->d_inode read - there will be an RCU delay 1509ea936aebSAl Viro * between mount hash removal and ->mnt_root 1510ea936aebSAl Viro * becoming unpinned. 151159430262SLinus Torvalds */ 1512ea936aebSAl Viro flags = dentry->d_flags; 151320aac6c6SAl Viro if (read_seqretry(&mount_lock, nd->m_seq)) 151420aac6c6SAl Viro return false; 1515ea936aebSAl Viro continue; 15169875cf80SDavid Howells } 1517ea936aebSAl Viro if (read_seqretry(&mount_lock, nd->m_seq)) 1518ea936aebSAl Viro return false; 1519ea936aebSAl Viro } 1520ea936aebSAl Viro return !(flags & DCACHE_NEED_AUTOMOUNT); 1521ea936aebSAl Viro } 1522287548e4SAl Viro } 1523287548e4SAl Viro 1524db3c9adeSAl Viro static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, 1525db3c9adeSAl Viro struct path *path, struct inode **inode, 1526db3c9adeSAl Viro unsigned int *seqp) 1527bd7c4b50SAl Viro { 15289deed3ebSAl Viro bool jumped; 1529db3c9adeSAl Viro int ret; 1530bd7c4b50SAl Viro 1531db3c9adeSAl Viro path->mnt = nd->path.mnt; 1532db3c9adeSAl Viro path->dentry = dentry; 1533c153007bSAl Viro if (nd->flags & LOOKUP_RCU) { 1534c153007bSAl Viro unsigned int seq = *seqp; 1535c153007bSAl Viro if (unlikely(!*inode)) 1536c153007bSAl Viro return -ENOENT; 1537c153007bSAl Viro if (likely(__follow_mount_rcu(nd, path, inode, seqp))) 15389deed3ebSAl Viro return 0; 1539ae66db45SAl Viro if (!try_to_unlazy_next(nd, dentry, seq)) 1540c153007bSAl Viro return -ECHILD; 1541c153007bSAl Viro // *path might've been clobbered by __follow_mount_rcu() 1542c153007bSAl Viro path->mnt = nd->path.mnt; 1543c153007bSAl Viro path->dentry = dentry; 1544c153007bSAl Viro } 15459deed3ebSAl Viro ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); 15469deed3ebSAl Viro if (jumped) { 15479deed3ebSAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 15489deed3ebSAl Viro ret = -EXDEV; 15499deed3ebSAl Viro else 1550bcba1e7dSAl Viro nd->state |= ND_JUMPED; 15519deed3ebSAl Viro } 15529deed3ebSAl Viro if (unlikely(ret)) { 15539deed3ebSAl Viro dput(path->dentry); 15549deed3ebSAl Viro if (path->mnt != nd->path.mnt) 15559deed3ebSAl Viro mntput(path->mnt); 15569deed3ebSAl Viro } else { 1557bd7c4b50SAl Viro *inode = d_backing_inode(path->dentry); 1558bd7c4b50SAl Viro *seqp = 0; /* out of RCU mode, so the value doesn't matter */ 1559bd7c4b50SAl Viro } 1560bd7c4b50SAl Viro return ret; 1561bd7c4b50SAl Viro } 1562bd7c4b50SAl Viro 15639875cf80SDavid Howells /* 1564f4fdace9SOleg Drokin * This looks up the name in dcache and possibly revalidates the found dentry. 1565f4fdace9SOleg Drokin * NULL is returned if the dentry does not exist in the cache. 1566baa03890SNick Piggin */ 1567e3c13928SAl Viro static struct dentry *lookup_dcache(const struct qstr *name, 1568e3c13928SAl Viro struct dentry *dir, 15696c51e513SAl Viro unsigned int flags) 1570baa03890SNick Piggin { 1571a89f8337SAl Viro struct dentry *dentry = d_lookup(dir, name); 1572bad61189SMiklos Szeredi if (dentry) { 1573a89f8337SAl Viro int error = d_revalidate(dentry, flags); 1574bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 157574ff0ffcSAl Viro if (!error) 15765542aa2fSEric W. Biederman d_invalidate(dentry); 1577bad61189SMiklos Szeredi dput(dentry); 157874ff0ffcSAl Viro return ERR_PTR(error); 1579bad61189SMiklos Szeredi } 1580bad61189SMiklos Szeredi } 1581baa03890SNick Piggin return dentry; 1582baa03890SNick Piggin } 1583baa03890SNick Piggin 1584baa03890SNick Piggin /* 1585a03ece5fSAl Viro * Parent directory has inode locked exclusive. This is one 1586a03ece5fSAl Viro * and only case when ->lookup() gets called on non in-lookup 1587a03ece5fSAl Viro * dentries - as the matter of fact, this only gets called 1588a03ece5fSAl Viro * when directory is guaranteed to have no in-lookup children 1589a03ece5fSAl Viro * at all. 159044396f4bSJosef Bacik */ 1591a03ece5fSAl Viro static struct dentry *__lookup_hash(const struct qstr *name, 1592a03ece5fSAl Viro struct dentry *base, unsigned int flags) 159344396f4bSJosef Bacik { 1594a03ece5fSAl Viro struct dentry *dentry = lookup_dcache(name, base, flags); 159544396f4bSJosef Bacik struct dentry *old; 1596a03ece5fSAl Viro struct inode *dir = base->d_inode; 1597a03ece5fSAl Viro 1598a03ece5fSAl Viro if (dentry) 1599a03ece5fSAl Viro return dentry; 160044396f4bSJosef Bacik 160144396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1602a03ece5fSAl Viro if (unlikely(IS_DEADDIR(dir))) 160344396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1604a03ece5fSAl Viro 1605a03ece5fSAl Viro dentry = d_alloc(base, name); 1606a03ece5fSAl Viro if (unlikely(!dentry)) 1607a03ece5fSAl Viro return ERR_PTR(-ENOMEM); 160844396f4bSJosef Bacik 160972bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 161044396f4bSJosef Bacik if (unlikely(old)) { 161144396f4bSJosef Bacik dput(dentry); 161244396f4bSJosef Bacik dentry = old; 161344396f4bSJosef Bacik } 161444396f4bSJosef Bacik return dentry; 161544396f4bSJosef Bacik } 161644396f4bSJosef Bacik 161720e34357SAl Viro static struct dentry *lookup_fast(struct nameidata *nd, 161820e34357SAl Viro struct inode **inode, 1619254cf582SAl Viro unsigned *seqp) 16201da177e4SLinus Torvalds { 162131e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 16225a18fff2SAl Viro int status = 1; 16239875cf80SDavid Howells 16243cac260aSAl Viro /* 1625b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 16265d0f49c1SAl Viro * of a false negative due to a concurrent rename, the caller is 16275d0f49c1SAl Viro * going to fall back to non-racy lookup. 1628b04f784eSNick Piggin */ 162931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 163031e6b01fSNick Piggin unsigned seq; 1631da53be12SLinus Torvalds dentry = __d_lookup_rcu(parent, &nd->last, &seq); 16325d0f49c1SAl Viro if (unlikely(!dentry)) { 1633e36cffedSJens Axboe if (!try_to_unlazy(nd)) 163420e34357SAl Viro return ERR_PTR(-ECHILD); 163520e34357SAl Viro return NULL; 16365d0f49c1SAl Viro } 16375a18fff2SAl Viro 163812f8ad4bSLinus Torvalds /* 163912f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 164012f8ad4bSLinus Torvalds * the dentry name information from lookup. 164112f8ad4bSLinus Torvalds */ 164263afdfc7SDavid Howells *inode = d_backing_inode(dentry); 164382ef0698SAl Viro if (read_seqcount_retry(&dentry->d_seq, seq)) 164420e34357SAl Viro return ERR_PTR(-ECHILD); 164512f8ad4bSLinus Torvalds 164612f8ad4bSLinus Torvalds /* 164712f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 164812f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 164912f8ad4bSLinus Torvalds * 165012f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 165112f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 165212f8ad4bSLinus Torvalds */ 165382ef0698SAl Viro if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 165420e34357SAl Viro return ERR_PTR(-ECHILD); 16555a18fff2SAl Viro 1656254cf582SAl Viro *seqp = seq; 16574ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 1658c153007bSAl Viro if (likely(status > 0)) 165920e34357SAl Viro return dentry; 1660ae66db45SAl Viro if (!try_to_unlazy_next(nd, dentry, seq)) 166120e34357SAl Viro return ERR_PTR(-ECHILD); 166226ddb45eSSteven Rostedt (VMware) if (status == -ECHILD) 1663209a7fb2SAl Viro /* we'd been told to redo it in non-rcu mode */ 1664209a7fb2SAl Viro status = d_revalidate(dentry, nd->flags); 16655a18fff2SAl Viro } else { 1666e97cdc87SAl Viro dentry = __d_lookup(parent, &nd->last); 166781e6f520SAl Viro if (unlikely(!dentry)) 166820e34357SAl Viro return NULL; 16694ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 16705d0f49c1SAl Viro } 16715a18fff2SAl Viro if (unlikely(status <= 0)) { 1672e9742b53SAl Viro if (!status) 16735d0f49c1SAl Viro d_invalidate(dentry); 16745a18fff2SAl Viro dput(dentry); 167520e34357SAl Viro return ERR_PTR(status); 16765a18fff2SAl Viro } 167720e34357SAl Viro return dentry; 1678697f514dSMiklos Szeredi } 1679697f514dSMiklos Szeredi 1680697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 168188d8331aSAl Viro static struct dentry *__lookup_slow(const struct qstr *name, 1682e3c13928SAl Viro struct dentry *dir, 1683e3c13928SAl Viro unsigned int flags) 1684697f514dSMiklos Szeredi { 168588d8331aSAl Viro struct dentry *dentry, *old; 16861936386eSAl Viro struct inode *inode = dir->d_inode; 1687d9171b93SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 16881936386eSAl Viro 16891936386eSAl Viro /* Don't go there if it's already dead */ 169094bdd655SAl Viro if (unlikely(IS_DEADDIR(inode))) 169188d8331aSAl Viro return ERR_PTR(-ENOENT); 169294bdd655SAl Viro again: 1693d9171b93SAl Viro dentry = d_alloc_parallel(dir, name, &wq); 169494bdd655SAl Viro if (IS_ERR(dentry)) 169588d8331aSAl Viro return dentry; 169694bdd655SAl Viro if (unlikely(!d_in_lookup(dentry))) { 1697949a852eSAl Viro int error = d_revalidate(dentry, flags); 1698949a852eSAl Viro if (unlikely(error <= 0)) { 169994bdd655SAl Viro if (!error) { 1700949a852eSAl Viro d_invalidate(dentry); 1701949a852eSAl Viro dput(dentry); 170294bdd655SAl Viro goto again; 170394bdd655SAl Viro } 170494bdd655SAl Viro dput(dentry); 1705949a852eSAl Viro dentry = ERR_PTR(error); 1706949a852eSAl Viro } 170794bdd655SAl Viro } else { 17081936386eSAl Viro old = inode->i_op->lookup(inode, dentry, flags); 170985c7f810SAl Viro d_lookup_done(dentry); 17101936386eSAl Viro if (unlikely(old)) { 17111936386eSAl Viro dput(dentry); 17121936386eSAl Viro dentry = old; 1713949a852eSAl Viro } 1714949a852eSAl Viro } 1715e3c13928SAl Viro return dentry; 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds 171888d8331aSAl Viro static struct dentry *lookup_slow(const struct qstr *name, 171988d8331aSAl Viro struct dentry *dir, 172088d8331aSAl Viro unsigned int flags) 172188d8331aSAl Viro { 172288d8331aSAl Viro struct inode *inode = dir->d_inode; 172388d8331aSAl Viro struct dentry *res; 172488d8331aSAl Viro inode_lock_shared(inode); 172588d8331aSAl Viro res = __lookup_slow(name, dir, flags); 172688d8331aSAl Viro inode_unlock_shared(inode); 172788d8331aSAl Viro return res; 172888d8331aSAl Viro } 172988d8331aSAl Viro 1730ba73d987SChristian Brauner static inline int may_lookup(struct user_namespace *mnt_userns, 1731ba73d987SChristian Brauner struct nameidata *nd) 173252094c8aSAl Viro { 173352094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 17347d6beb71SLinus Torvalds int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 1735e36cffedSJens Axboe if (err != -ECHILD || !try_to_unlazy(nd)) 173652094c8aSAl Viro return err; 173752094c8aSAl Viro } 1738ba73d987SChristian Brauner return inode_permission(mnt_userns, nd->inode, MAY_EXEC); 173952094c8aSAl Viro } 174052094c8aSAl Viro 174149055906SAl Viro static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) 1742d63ff28fSAl Viro { 174349055906SAl Viro if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) 174449055906SAl Viro return -ELOOP; 17454542576bSAl Viro 17464542576bSAl Viro if (likely(nd->depth != EMBEDDED_LEVELS)) 17474542576bSAl Viro return 0; 17484542576bSAl Viro if (likely(nd->stack != nd->internal)) 17494542576bSAl Viro return 0; 175060ef60c7SAl Viro if (likely(nd_alloc_stack(nd))) 175149055906SAl Viro return 0; 175260ef60c7SAl Viro 175360ef60c7SAl Viro if (nd->flags & LOOKUP_RCU) { 175460ef60c7SAl Viro // we need to grab link before we do unlazy. And we can't skip 175560ef60c7SAl Viro // unlazy even if we fail to grab the link - cleanup needs it 1756aef9404dSAl Viro bool grabbed_link = legitimize_path(nd, link, seq); 175760ef60c7SAl Viro 1758e5ca024eSAl Viro if (!try_to_unlazy(nd) || !grabbed_link) 175960ef60c7SAl Viro return -ECHILD; 176060ef60c7SAl Viro 176160ef60c7SAl Viro if (nd_alloc_stack(nd)) 176260ef60c7SAl Viro return 0; 1763bc40aee0SAl Viro } 176460ef60c7SAl Viro return -ENOMEM; 176549055906SAl Viro } 176649055906SAl Viro 176749055906SAl Viro enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; 176849055906SAl Viro 176949055906SAl Viro static const char *pick_link(struct nameidata *nd, struct path *link, 177049055906SAl Viro struct inode *inode, unsigned seq, int flags) 177149055906SAl Viro { 177249055906SAl Viro struct saved *last; 177349055906SAl Viro const char *res; 177449055906SAl Viro int error = reserve_stack(nd, link, seq); 177549055906SAl Viro 177649055906SAl Viro if (unlikely(error)) { 177749055906SAl Viro if (!(nd->flags & LOOKUP_RCU)) 1778cd179f44SAl Viro path_put(link); 177906708adbSAl Viro return ERR_PTR(error); 1780626de996SAl Viro } 1781ab104923SAl Viro last = nd->stack + nd->depth++; 17821cf2665bSAl Viro last->link = *link; 1783fceef393SAl Viro clear_delayed_call(&last->done); 17840450b2d1SAl Viro last->seq = seq; 1785ad6cc4c3SAl Viro 1786b1a81972SAl Viro if (flags & WALK_TRAILING) { 1787ad6cc4c3SAl Viro error = may_follow_link(nd, inode); 1788ad6cc4c3SAl Viro if (unlikely(error)) 1789ad6cc4c3SAl Viro return ERR_PTR(error); 1790ad6cc4c3SAl Viro } 1791ad6cc4c3SAl Viro 1792dab741e0SMattias Nissler if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || 1793dab741e0SMattias Nissler unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) 1794ad6cc4c3SAl Viro return ERR_PTR(-ELOOP); 1795ad6cc4c3SAl Viro 1796ad6cc4c3SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1797ad6cc4c3SAl Viro touch_atime(&last->link); 1798ad6cc4c3SAl Viro cond_resched(); 1799ad6cc4c3SAl Viro } else if (atime_needs_update(&last->link, inode)) { 1800e36cffedSJens Axboe if (!try_to_unlazy(nd)) 1801ad6cc4c3SAl Viro return ERR_PTR(-ECHILD); 1802ad6cc4c3SAl Viro touch_atime(&last->link); 1803ad6cc4c3SAl Viro } 1804ad6cc4c3SAl Viro 1805ad6cc4c3SAl Viro error = security_inode_follow_link(link->dentry, inode, 1806ad6cc4c3SAl Viro nd->flags & LOOKUP_RCU); 1807ad6cc4c3SAl Viro if (unlikely(error)) 1808ad6cc4c3SAl Viro return ERR_PTR(error); 1809ad6cc4c3SAl Viro 1810ad6cc4c3SAl Viro res = READ_ONCE(inode->i_link); 1811ad6cc4c3SAl Viro if (!res) { 1812ad6cc4c3SAl Viro const char * (*get)(struct dentry *, struct inode *, 1813ad6cc4c3SAl Viro struct delayed_call *); 1814ad6cc4c3SAl Viro get = inode->i_op->get_link; 1815ad6cc4c3SAl Viro if (nd->flags & LOOKUP_RCU) { 1816ad6cc4c3SAl Viro res = get(NULL, inode, &last->done); 1817e36cffedSJens Axboe if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) 1818ad6cc4c3SAl Viro res = get(link->dentry, inode, &last->done); 1819ad6cc4c3SAl Viro } else { 1820ad6cc4c3SAl Viro res = get(link->dentry, inode, &last->done); 1821ad6cc4c3SAl Viro } 1822ad6cc4c3SAl Viro if (!res) 1823ad6cc4c3SAl Viro goto all_done; 1824ad6cc4c3SAl Viro if (IS_ERR(res)) 1825ad6cc4c3SAl Viro return res; 1826ad6cc4c3SAl Viro } 1827ad6cc4c3SAl Viro if (*res == '/') { 1828ad6cc4c3SAl Viro error = nd_jump_root(nd); 1829ad6cc4c3SAl Viro if (unlikely(error)) 1830ad6cc4c3SAl Viro return ERR_PTR(error); 1831ad6cc4c3SAl Viro while (unlikely(*++res == '/')) 1832ad6cc4c3SAl Viro ; 1833ad6cc4c3SAl Viro } 1834ad6cc4c3SAl Viro if (*res) 1835ad6cc4c3SAl Viro return res; 1836ad6cc4c3SAl Viro all_done: // pure jump 1837ad6cc4c3SAl Viro put_link(nd); 1838ad6cc4c3SAl Viro return NULL; 1839d63ff28fSAl Viro } 1840d63ff28fSAl Viro 18413ddcd056SLinus Torvalds /* 18423ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 18433ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 18443ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 18453ddcd056SLinus Torvalds * for the common case. 18463ddcd056SLinus Torvalds */ 1847b0417d2cSAl Viro static const char *step_into(struct nameidata *nd, int flags, 1848cbae4d12SAl Viro struct dentry *dentry, struct inode *inode, unsigned seq) 18493ddcd056SLinus Torvalds { 1850cbae4d12SAl Viro struct path path; 1851cbae4d12SAl Viro int err = handle_mounts(nd, dentry, &path, &inode, &seq); 1852cbae4d12SAl Viro 1853cbae4d12SAl Viro if (err < 0) 1854b0417d2cSAl Viro return ERR_PTR(err); 1855cbae4d12SAl Viro if (likely(!d_is_symlink(path.dentry)) || 18568c4efe22SAl Viro ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || 1857aca2903eSAl Viro (flags & WALK_NOFOLLOW)) { 18588f64fb1cSAl Viro /* not a symlink or should not follow */ 1859c99687a0SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1860c99687a0SAl Viro dput(nd->path.dentry); 1861c99687a0SAl Viro if (nd->path.mnt != path.mnt) 1862c99687a0SAl Viro mntput(nd->path.mnt); 1863c99687a0SAl Viro } 1864c99687a0SAl Viro nd->path = path; 18658f64fb1cSAl Viro nd->inode = inode; 18668f64fb1cSAl Viro nd->seq = seq; 1867b0417d2cSAl Viro return NULL; 18688f64fb1cSAl Viro } 1869a7f77542SAl Viro if (nd->flags & LOOKUP_RCU) { 187084f0cd9eSAl Viro /* make sure that d_is_symlink above matches inode */ 1871cbae4d12SAl Viro if (read_seqcount_retry(&path.dentry->d_seq, seq)) 1872b0417d2cSAl Viro return ERR_PTR(-ECHILD); 187384f0cd9eSAl Viro } else { 187484f0cd9eSAl Viro if (path.mnt == nd->path.mnt) 187584f0cd9eSAl Viro mntget(path.mnt); 1876a7f77542SAl Viro } 1877b1a81972SAl Viro return pick_link(nd, &path, inode, seq, flags); 18783ddcd056SLinus Torvalds } 18793ddcd056SLinus Torvalds 1880c2df1968SAl Viro static struct dentry *follow_dotdot_rcu(struct nameidata *nd, 1881c2df1968SAl Viro struct inode **inodep, 1882c2df1968SAl Viro unsigned *seqp) 1883957dd41dSAl Viro { 188412487f30SAl Viro struct dentry *parent, *old; 1885957dd41dSAl Viro 188612487f30SAl Viro if (path_equal(&nd->path, &nd->root)) 188712487f30SAl Viro goto in_root; 188812487f30SAl Viro if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 18897ef482faSAl Viro struct path path; 1890efe772d6SAl Viro unsigned seq; 18917ef482faSAl Viro if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), 18927ef482faSAl Viro &nd->root, &path, &seq)) 189312487f30SAl Viro goto in_root; 1894efe772d6SAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1895efe772d6SAl Viro return ERR_PTR(-ECHILD); 1896efe772d6SAl Viro nd->path = path; 1897efe772d6SAl Viro nd->inode = path.dentry->d_inode; 1898efe772d6SAl Viro nd->seq = seq; 189982ef0698SAl Viro if (read_seqretry(&mount_lock, nd->m_seq)) 1900efe772d6SAl Viro return ERR_PTR(-ECHILD); 1901efe772d6SAl Viro /* we know that mountpoint was pinned */ 1902957dd41dSAl Viro } 190312487f30SAl Viro old = nd->path.dentry; 190412487f30SAl Viro parent = old->d_parent; 190512487f30SAl Viro *inodep = parent->d_inode; 190612487f30SAl Viro *seqp = read_seqcount_begin(&parent->d_seq); 190782ef0698SAl Viro if (read_seqcount_retry(&old->d_seq, nd->seq)) 190812487f30SAl Viro return ERR_PTR(-ECHILD); 190912487f30SAl Viro if (unlikely(!path_connected(nd->path.mnt, parent))) 191012487f30SAl Viro return ERR_PTR(-ECHILD); 191112487f30SAl Viro return parent; 191212487f30SAl Viro in_root: 191382ef0698SAl Viro if (read_seqretry(&mount_lock, nd->m_seq)) 1914efe772d6SAl Viro return ERR_PTR(-ECHILD); 1915957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_BENEATH)) 19167521f22bSAl Viro return ERR_PTR(-ECHILD); 191751c6546cSAl Viro *seqp = nd->seq; 191851c6546cSAl Viro *inodep = nd->path.dentry->d_inode; 191951c6546cSAl Viro return nd->path.dentry; 1920957dd41dSAl Viro } 1921957dd41dSAl Viro 1922c2df1968SAl Viro static struct dentry *follow_dotdot(struct nameidata *nd, 1923c2df1968SAl Viro struct inode **inodep, 1924c2df1968SAl Viro unsigned *seqp) 1925957dd41dSAl Viro { 192612487f30SAl Viro struct dentry *parent; 192712487f30SAl Viro 1928957dd41dSAl Viro if (path_equal(&nd->path, &nd->root)) 192912487f30SAl Viro goto in_root; 193012487f30SAl Viro if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { 19312aa38470SAl Viro struct path path; 19322aa38470SAl Viro 19332aa38470SAl Viro if (!choose_mountpoint(real_mount(nd->path.mnt), 19342aa38470SAl Viro &nd->root, &path)) 193512487f30SAl Viro goto in_root; 1936165200d6SAl Viro path_put(&nd->path); 1937165200d6SAl Viro nd->path = path; 19382aa38470SAl Viro nd->inode = path.dentry->d_inode; 1939165200d6SAl Viro if (unlikely(nd->flags & LOOKUP_NO_XDEV)) 1940165200d6SAl Viro return ERR_PTR(-EXDEV); 194112487f30SAl Viro } 1942957dd41dSAl Viro /* rare case of legitimate dget_parent()... */ 194312487f30SAl Viro parent = dget_parent(nd->path.dentry); 1944957dd41dSAl Viro if (unlikely(!path_connected(nd->path.mnt, parent))) { 1945957dd41dSAl Viro dput(parent); 19467521f22bSAl Viro return ERR_PTR(-ENOENT); 1947957dd41dSAl Viro } 1948c2df1968SAl Viro *seqp = 0; 1949c2df1968SAl Viro *inodep = parent->d_inode; 1950c2df1968SAl Viro return parent; 195112487f30SAl Viro 195212487f30SAl Viro in_root: 1953957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_BENEATH)) 19547521f22bSAl Viro return ERR_PTR(-EXDEV); 195551c6546cSAl Viro *seqp = 0; 195651c6546cSAl Viro *inodep = nd->path.dentry->d_inode; 195751c6546cSAl Viro return dget(nd->path.dentry); 1958957dd41dSAl Viro } 1959957dd41dSAl Viro 19607521f22bSAl Viro static const char *handle_dots(struct nameidata *nd, int type) 1961957dd41dSAl Viro { 1962957dd41dSAl Viro if (type == LAST_DOTDOT) { 19637521f22bSAl Viro const char *error = NULL; 1964c2df1968SAl Viro struct dentry *parent; 1965c2df1968SAl Viro struct inode *inode; 1966c2df1968SAl Viro unsigned seq; 1967957dd41dSAl Viro 1968957dd41dSAl Viro if (!nd->root.mnt) { 19697521f22bSAl Viro error = ERR_PTR(set_root(nd)); 1970957dd41dSAl Viro if (error) 1971957dd41dSAl Viro return error; 1972957dd41dSAl Viro } 1973957dd41dSAl Viro if (nd->flags & LOOKUP_RCU) 1974c2df1968SAl Viro parent = follow_dotdot_rcu(nd, &inode, &seq); 1975957dd41dSAl Viro else 1976c2df1968SAl Viro parent = follow_dotdot(nd, &inode, &seq); 1977c2df1968SAl Viro if (IS_ERR(parent)) 1978c2df1968SAl Viro return ERR_CAST(parent); 197951c6546cSAl Viro error = step_into(nd, WALK_NOFOLLOW, parent, inode, seq); 1980c2df1968SAl Viro if (unlikely(error)) 1981957dd41dSAl Viro return error; 1982957dd41dSAl Viro 1983957dd41dSAl Viro if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { 1984957dd41dSAl Viro /* 1985957dd41dSAl Viro * If there was a racing rename or mount along our 1986957dd41dSAl Viro * path, then we can't be sure that ".." hasn't jumped 1987957dd41dSAl Viro * above nd->root (and so userspace should retry or use 1988957dd41dSAl Viro * some fallback). 1989957dd41dSAl Viro */ 1990957dd41dSAl Viro smp_rmb(); 199182ef0698SAl Viro if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)) 19927521f22bSAl Viro return ERR_PTR(-EAGAIN); 199382ef0698SAl Viro if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)) 19947521f22bSAl Viro return ERR_PTR(-EAGAIN); 1995957dd41dSAl Viro } 1996957dd41dSAl Viro } 19977521f22bSAl Viro return NULL; 1998957dd41dSAl Viro } 1999957dd41dSAl Viro 200092d27016SAl Viro static const char *walk_component(struct nameidata *nd, int flags) 2001ce57dfc1SAl Viro { 2002db3c9adeSAl Viro struct dentry *dentry; 2003ce57dfc1SAl Viro struct inode *inode; 2004254cf582SAl Viro unsigned seq; 2005ce57dfc1SAl Viro /* 2006ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 2007ce57dfc1SAl Viro * to be able to know about the current root directory and 2008ce57dfc1SAl Viro * parent relationships. 2009ce57dfc1SAl Viro */ 20104693a547SAl Viro if (unlikely(nd->last_type != LAST_NORM)) { 20111c4ff1a8SAl Viro if (!(flags & WALK_MORE) && nd->depth) 20124693a547SAl Viro put_link(nd); 20137521f22bSAl Viro return handle_dots(nd, nd->last_type); 20144693a547SAl Viro } 201520e34357SAl Viro dentry = lookup_fast(nd, &inode, &seq); 201620e34357SAl Viro if (IS_ERR(dentry)) 201792d27016SAl Viro return ERR_CAST(dentry); 201820e34357SAl Viro if (unlikely(!dentry)) { 2019db3c9adeSAl Viro dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); 2020db3c9adeSAl Viro if (IS_ERR(dentry)) 202192d27016SAl Viro return ERR_CAST(dentry); 202220e34357SAl Viro } 202356676ec3SAl Viro if (!(flags & WALK_MORE) && nd->depth) 202456676ec3SAl Viro put_link(nd); 2025b0417d2cSAl Viro return step_into(nd, flags, dentry, inode, seq); 2026ce57dfc1SAl Viro } 2027ce57dfc1SAl Viro 20281da177e4SLinus Torvalds /* 2029bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 2030bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 2031bfcfaa77SLinus Torvalds * 2032bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 2033bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 2034bfcfaa77SLinus Torvalds * fast. 2035bfcfaa77SLinus Torvalds * 2036bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 2037bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 2038bfcfaa77SLinus Torvalds * crossing operation. 2039bfcfaa77SLinus Torvalds * 2040bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 2041bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 2042bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 2043bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 2044bfcfaa77SLinus Torvalds */ 2045bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 2046bfcfaa77SLinus Torvalds 2047f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 2048bfcfaa77SLinus Torvalds 2049468a9428SGeorge Spelvin #ifdef HASH_MIX 2050bfcfaa77SLinus Torvalds 2051468a9428SGeorge Spelvin /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ 2052468a9428SGeorge Spelvin 2053468a9428SGeorge Spelvin #elif defined(CONFIG_64BIT) 20542a18da7aSGeorge Spelvin /* 20552a18da7aSGeorge Spelvin * Register pressure in the mixing function is an issue, particularly 20562a18da7aSGeorge Spelvin * on 32-bit x86, but almost any function requires one state value and 20572a18da7aSGeorge Spelvin * one temporary. Instead, use a function designed for two state values 20582a18da7aSGeorge Spelvin * and no temporaries. 20592a18da7aSGeorge Spelvin * 20602a18da7aSGeorge Spelvin * This function cannot create a collision in only two iterations, so 20612a18da7aSGeorge Spelvin * we have two iterations to achieve avalanche. In those two iterations, 20622a18da7aSGeorge Spelvin * we have six layers of mixing, which is enough to spread one bit's 20632a18da7aSGeorge Spelvin * influence out to 2^6 = 64 state bits. 20642a18da7aSGeorge Spelvin * 20652a18da7aSGeorge Spelvin * Rotate constants are scored by considering either 64 one-bit input 20662a18da7aSGeorge Spelvin * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the 20672a18da7aSGeorge Spelvin * probability of that delta causing a change to each of the 128 output 20682a18da7aSGeorge Spelvin * bits, using a sample of random initial states. 20692a18da7aSGeorge Spelvin * 20702a18da7aSGeorge Spelvin * The Shannon entropy of the computed probabilities is then summed 20712a18da7aSGeorge Spelvin * to produce a score. Ideally, any input change has a 50% chance of 20722a18da7aSGeorge Spelvin * toggling any given output bit. 20732a18da7aSGeorge Spelvin * 20742a18da7aSGeorge Spelvin * Mixing scores (in bits) for (12,45): 20752a18da7aSGeorge Spelvin * Input delta: 1-bit 2-bit 20762a18da7aSGeorge Spelvin * 1 round: 713.3 42542.6 20772a18da7aSGeorge Spelvin * 2 rounds: 2753.7 140389.8 20782a18da7aSGeorge Spelvin * 3 rounds: 5954.1 233458.2 20792a18da7aSGeorge Spelvin * 4 rounds: 7862.6 256672.2 20802a18da7aSGeorge Spelvin * Perfect: 8192 258048 20812a18da7aSGeorge Spelvin * (64*128) (64*63/2 * 128) 20822a18da7aSGeorge Spelvin */ 20832a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a) \ 20842a18da7aSGeorge Spelvin ( x ^= (a), \ 20852a18da7aSGeorge Spelvin y ^= x, x = rol64(x,12),\ 20862a18da7aSGeorge Spelvin x += y, y = rol64(y,45),\ 20872a18da7aSGeorge Spelvin y *= 9 ) 2088bfcfaa77SLinus Torvalds 20890fed3ac8SGeorge Spelvin /* 20902a18da7aSGeorge Spelvin * Fold two longs into one 32-bit hash value. This must be fast, but 20912a18da7aSGeorge Spelvin * latency isn't quite as critical, as there is a fair bit of additional 20922a18da7aSGeorge Spelvin * work done before the hash value is used. 20930fed3ac8SGeorge Spelvin */ 20942a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y) 20950fed3ac8SGeorge Spelvin { 20962a18da7aSGeorge Spelvin y ^= x * GOLDEN_RATIO_64; 20972a18da7aSGeorge Spelvin y *= GOLDEN_RATIO_64; 20982a18da7aSGeorge Spelvin return y >> 32; 20990fed3ac8SGeorge Spelvin } 21000fed3ac8SGeorge Spelvin 2101bfcfaa77SLinus Torvalds #else /* 32-bit case */ 2102bfcfaa77SLinus Torvalds 21032a18da7aSGeorge Spelvin /* 21042a18da7aSGeorge Spelvin * Mixing scores (in bits) for (7,20): 21052a18da7aSGeorge Spelvin * Input delta: 1-bit 2-bit 21062a18da7aSGeorge Spelvin * 1 round: 330.3 9201.6 21072a18da7aSGeorge Spelvin * 2 rounds: 1246.4 25475.4 21082a18da7aSGeorge Spelvin * 3 rounds: 1907.1 31295.1 21092a18da7aSGeorge Spelvin * 4 rounds: 2042.3 31718.6 21102a18da7aSGeorge Spelvin * Perfect: 2048 31744 21112a18da7aSGeorge Spelvin * (32*64) (32*31/2 * 64) 21122a18da7aSGeorge Spelvin */ 21132a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a) \ 21142a18da7aSGeorge Spelvin ( x ^= (a), \ 21152a18da7aSGeorge Spelvin y ^= x, x = rol32(x, 7),\ 21162a18da7aSGeorge Spelvin x += y, y = rol32(y,20),\ 21172a18da7aSGeorge Spelvin y *= 9 ) 2118bfcfaa77SLinus Torvalds 21192a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y) 21200fed3ac8SGeorge Spelvin { 21212a18da7aSGeorge Spelvin /* Use arch-optimized multiply if one exists */ 21222a18da7aSGeorge Spelvin return __hash_32(y ^ __hash_32(x)); 21230fed3ac8SGeorge Spelvin } 21240fed3ac8SGeorge Spelvin 2125bfcfaa77SLinus Torvalds #endif 2126bfcfaa77SLinus Torvalds 21272a18da7aSGeorge Spelvin /* 21282a18da7aSGeorge Spelvin * Return the hash of a string of known length. This is carfully 21292a18da7aSGeorge Spelvin * designed to match hash_name(), which is the more critical function. 21302a18da7aSGeorge Spelvin * In particular, we must end by hashing a final word containing 0..7 21312a18da7aSGeorge Spelvin * payload bytes, to match the way that hash_name() iterates until it 21322a18da7aSGeorge Spelvin * finds the delimiter after the name. 21332a18da7aSGeorge Spelvin */ 21348387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 2135bfcfaa77SLinus Torvalds { 21368387ff25SLinus Torvalds unsigned long a, x = 0, y = (unsigned long)salt; 2137bfcfaa77SLinus Torvalds 2138bfcfaa77SLinus Torvalds for (;;) { 2139fcfd2fbfSGeorge Spelvin if (!len) 2140fcfd2fbfSGeorge Spelvin goto done; 2141e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 2142bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 2143bfcfaa77SLinus Torvalds break; 21442a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2145bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 2146bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 2147bfcfaa77SLinus Torvalds } 21482a18da7aSGeorge Spelvin x ^= a & bytemask_from_count(len); 2149bfcfaa77SLinus Torvalds done: 21502a18da7aSGeorge Spelvin return fold_hash(x, y); 2151bfcfaa77SLinus Torvalds } 2152bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 2153bfcfaa77SLinus Torvalds 2154fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */ 21558387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name) 2156fcfd2fbfSGeorge Spelvin { 21578387ff25SLinus Torvalds unsigned long a = 0, x = 0, y = (unsigned long)salt; 21588387ff25SLinus Torvalds unsigned long adata, mask, len; 2159fcfd2fbfSGeorge Spelvin const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2160fcfd2fbfSGeorge Spelvin 21618387ff25SLinus Torvalds len = 0; 21628387ff25SLinus Torvalds goto inside; 21638387ff25SLinus Torvalds 2164fcfd2fbfSGeorge Spelvin do { 21652a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2166fcfd2fbfSGeorge Spelvin len += sizeof(unsigned long); 21678387ff25SLinus Torvalds inside: 2168fcfd2fbfSGeorge Spelvin a = load_unaligned_zeropad(name+len); 2169fcfd2fbfSGeorge Spelvin } while (!has_zero(a, &adata, &constants)); 2170fcfd2fbfSGeorge Spelvin 2171fcfd2fbfSGeorge Spelvin adata = prep_zero_mask(a, adata, &constants); 2172fcfd2fbfSGeorge Spelvin mask = create_zero_mask(adata); 21732a18da7aSGeorge Spelvin x ^= a & zero_bytemask(mask); 2174fcfd2fbfSGeorge Spelvin 21752a18da7aSGeorge Spelvin return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2176fcfd2fbfSGeorge Spelvin } 2177fcfd2fbfSGeorge Spelvin EXPORT_SYMBOL(hashlen_string); 2178fcfd2fbfSGeorge Spelvin 2179bfcfaa77SLinus Torvalds /* 2180bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 2181d6bb3e90SLinus Torvalds * return the "hash_len" as the result. 2182bfcfaa77SLinus Torvalds */ 21838387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name) 2184bfcfaa77SLinus Torvalds { 21858387ff25SLinus Torvalds unsigned long a = 0, b, x = 0, y = (unsigned long)salt; 21868387ff25SLinus Torvalds unsigned long adata, bdata, mask, len; 218736126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 2188bfcfaa77SLinus Torvalds 21898387ff25SLinus Torvalds len = 0; 21908387ff25SLinus Torvalds goto inside; 21918387ff25SLinus Torvalds 2192bfcfaa77SLinus Torvalds do { 21932a18da7aSGeorge Spelvin HASH_MIX(x, y, a); 2194bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 21958387ff25SLinus Torvalds inside: 2196e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 219736126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 219836126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 2199bfcfaa77SLinus Torvalds 220036126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 220136126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 220236126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 22032a18da7aSGeorge Spelvin x ^= a & zero_bytemask(mask); 220436126f8fSLinus Torvalds 22052a18da7aSGeorge Spelvin return hashlen_create(fold_hash(x, y), len + find_zero(mask)); 2206bfcfaa77SLinus Torvalds } 2207bfcfaa77SLinus Torvalds 22082a18da7aSGeorge Spelvin #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ 2209bfcfaa77SLinus Torvalds 2210fcfd2fbfSGeorge Spelvin /* Return the hash of a string of known length */ 22118387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) 22120145acc2SLinus Torvalds { 22138387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 22140145acc2SLinus Torvalds while (len--) 2215fcfd2fbfSGeorge Spelvin hash = partial_name_hash((unsigned char)*name++, hash); 22160145acc2SLinus Torvalds return end_name_hash(hash); 22170145acc2SLinus Torvalds } 2218ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 22190145acc2SLinus Torvalds 2220fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */ 22218387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name) 2222fcfd2fbfSGeorge Spelvin { 22238387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 2224fcfd2fbfSGeorge Spelvin unsigned long len = 0, c; 2225fcfd2fbfSGeorge Spelvin 2226fcfd2fbfSGeorge Spelvin c = (unsigned char)*name; 2227e0ab7af9SGeorge Spelvin while (c) { 2228fcfd2fbfSGeorge Spelvin len++; 2229fcfd2fbfSGeorge Spelvin hash = partial_name_hash(c, hash); 2230fcfd2fbfSGeorge Spelvin c = (unsigned char)name[len]; 2231e0ab7af9SGeorge Spelvin } 2232fcfd2fbfSGeorge Spelvin return hashlen_create(end_name_hash(hash), len); 2233fcfd2fbfSGeorge Spelvin } 2234f2a031b6SGeorge Spelvin EXPORT_SYMBOL(hashlen_string); 2235fcfd2fbfSGeorge Spelvin 22363ddcd056SLinus Torvalds /* 2237200e9ef7SLinus Torvalds * We know there's a real path component here of at least 2238200e9ef7SLinus Torvalds * one character. 2239200e9ef7SLinus Torvalds */ 22408387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name) 2241200e9ef7SLinus Torvalds { 22428387ff25SLinus Torvalds unsigned long hash = init_name_hash(salt); 2243200e9ef7SLinus Torvalds unsigned long len = 0, c; 2244200e9ef7SLinus Torvalds 2245200e9ef7SLinus Torvalds c = (unsigned char)*name; 2246200e9ef7SLinus Torvalds do { 2247200e9ef7SLinus Torvalds len++; 2248200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 2249200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 2250200e9ef7SLinus Torvalds } while (c && c != '/'); 2251d6bb3e90SLinus Torvalds return hashlen_create(end_name_hash(hash), len); 2252200e9ef7SLinus Torvalds } 2253200e9ef7SLinus Torvalds 2254bfcfaa77SLinus Torvalds #endif 2255bfcfaa77SLinus Torvalds 2256200e9ef7SLinus Torvalds /* 22571da177e4SLinus Torvalds * Name resolution. 2258ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 2259ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 22601da177e4SLinus Torvalds * 2261ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 2262ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 22631da177e4SLinus Torvalds */ 22646de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 22651da177e4SLinus Torvalds { 2266d8d4611aSAl Viro int depth = 0; // depth <= nd->depth 22671da177e4SLinus Torvalds int err; 22681da177e4SLinus Torvalds 2269b4c03536SAl Viro nd->last_type = LAST_ROOT; 2270c108837eSAl Viro nd->flags |= LOOKUP_PARENT; 22719b5858e9SAl Viro if (IS_ERR(name)) 22729b5858e9SAl Viro return PTR_ERR(name); 22731da177e4SLinus Torvalds while (*name=='/') 22741da177e4SLinus Torvalds name++; 22751a97d899SAl Viro if (!*name) { 22761a97d899SAl Viro nd->dir_mode = 0; // short-circuit the 'hardening' idiocy 22779e18f10aSAl Viro return 0; 22781a97d899SAl Viro } 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 22811da177e4SLinus Torvalds for(;;) { 2282549c7297SChristian Brauner struct user_namespace *mnt_userns; 228392d27016SAl Viro const char *link; 2284d6bb3e90SLinus Torvalds u64 hash_len; 2285fe479a58SAl Viro int type; 22861da177e4SLinus Torvalds 2287549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 2288549c7297SChristian Brauner err = may_lookup(mnt_userns, nd); 22891da177e4SLinus Torvalds if (err) 22903595e234SAl Viro return err; 22911da177e4SLinus Torvalds 22928387ff25SLinus Torvalds hash_len = hash_name(nd->path.dentry, name); 22931da177e4SLinus Torvalds 2294fe479a58SAl Viro type = LAST_NORM; 2295d6bb3e90SLinus Torvalds if (name[0] == '.') switch (hashlen_len(hash_len)) { 2296fe479a58SAl Viro case 2: 2297200e9ef7SLinus Torvalds if (name[1] == '.') { 2298fe479a58SAl Viro type = LAST_DOTDOT; 2299bcba1e7dSAl Viro nd->state |= ND_JUMPED; 230016c2cd71SAl Viro } 2301fe479a58SAl Viro break; 2302fe479a58SAl Viro case 1: 2303fe479a58SAl Viro type = LAST_DOT; 2304fe479a58SAl Viro } 23055a202bcdSAl Viro if (likely(type == LAST_NORM)) { 23065a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 2307bcba1e7dSAl Viro nd->state &= ~ND_JUMPED; 23085a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 2309a060dc50SJames Hogan struct qstr this = { { .hash_len = hash_len }, .name = name }; 2310da53be12SLinus Torvalds err = parent->d_op->d_hash(parent, &this); 23115a202bcdSAl Viro if (err < 0) 23123595e234SAl Viro return err; 2313d6bb3e90SLinus Torvalds hash_len = this.hash_len; 2314d6bb3e90SLinus Torvalds name = this.name; 23155a202bcdSAl Viro } 23165a202bcdSAl Viro } 2317fe479a58SAl Viro 2318d6bb3e90SLinus Torvalds nd->last.hash_len = hash_len; 2319d6bb3e90SLinus Torvalds nd->last.name = name; 23205f4a6a69SAl Viro nd->last_type = type; 23215f4a6a69SAl Viro 2322d6bb3e90SLinus Torvalds name += hashlen_len(hash_len); 2323d6bb3e90SLinus Torvalds if (!*name) 2324bdf6cbf1SAl Viro goto OK; 2325200e9ef7SLinus Torvalds /* 2326200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 2327200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 2328200e9ef7SLinus Torvalds */ 2329200e9ef7SLinus Torvalds do { 2330d6bb3e90SLinus Torvalds name++; 2331d6bb3e90SLinus Torvalds } while (unlikely(*name == '/')); 23328620c238SAl Viro if (unlikely(!*name)) { 23338620c238SAl Viro OK: 2334d8d4611aSAl Viro /* pathname or trailing symlink, done */ 2335c108837eSAl Viro if (!depth) { 2336549c7297SChristian Brauner nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode); 23370f705953SAl Viro nd->dir_mode = nd->inode->i_mode; 2338c108837eSAl Viro nd->flags &= ~LOOKUP_PARENT; 23398620c238SAl Viro return 0; 2340c108837eSAl Viro } 23418620c238SAl Viro /* last component of nested symlink */ 2342d8d4611aSAl Viro name = nd->stack[--depth].name; 23438c4efe22SAl Viro link = walk_component(nd, 0); 23441c4ff1a8SAl Viro } else { 23451c4ff1a8SAl Viro /* not the last component */ 23468c4efe22SAl Viro link = walk_component(nd, WALK_MORE); 23478620c238SAl Viro } 234892d27016SAl Viro if (unlikely(link)) { 234992d27016SAl Viro if (IS_ERR(link)) 235092d27016SAl Viro return PTR_ERR(link); 235192d27016SAl Viro /* a symlink to follow */ 2352d8d4611aSAl Viro nd->stack[depth++].name = name; 235392d27016SAl Viro name = link; 23549e18f10aSAl Viro continue; 235548c8b0c5SAl Viro } 235697242f99SAl Viro if (unlikely(!d_can_lookup(nd->path.dentry))) { 235797242f99SAl Viro if (nd->flags & LOOKUP_RCU) { 2358e36cffedSJens Axboe if (!try_to_unlazy(nd)) 235997242f99SAl Viro return -ECHILD; 236097242f99SAl Viro } 23613595e234SAl Viro return -ENOTDIR; 23625f4a6a69SAl Viro } 2363ce57dfc1SAl Viro } 236497242f99SAl Viro } 23651da177e4SLinus Torvalds 2366edc2b1daSAl Viro /* must be paired with terminate_walk() */ 2367c8a53ee5SAl Viro static const char *path_init(struct nameidata *nd, unsigned flags) 236831e6b01fSNick Piggin { 2369740a1678SAleksa Sarai int error; 2370c8a53ee5SAl Viro const char *s = nd->name->name; 237131e6b01fSNick Piggin 23726c6ec2b0SJens Axboe /* LOOKUP_CACHED requires RCU, ask caller to retry */ 23736c6ec2b0SJens Axboe if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) 23746c6ec2b0SJens Axboe return ERR_PTR(-EAGAIN); 23756c6ec2b0SJens Axboe 2376c0eb027eSLinus Torvalds if (!*s) 2377c0eb027eSLinus Torvalds flags &= ~LOOKUP_RCU; 2378edc2b1daSAl Viro if (flags & LOOKUP_RCU) 2379edc2b1daSAl Viro rcu_read_lock(); 2380c0eb027eSLinus Torvalds 2381bcba1e7dSAl Viro nd->flags = flags; 2382bcba1e7dSAl Viro nd->state |= ND_JUMPED; 2383ab87f9a5SAleksa Sarai 2384ab87f9a5SAleksa Sarai nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); 2385ab87f9a5SAleksa Sarai nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); 2386ab87f9a5SAleksa Sarai smp_rmb(); 2387ab87f9a5SAleksa Sarai 2388bcba1e7dSAl Viro if (nd->state & ND_ROOT_PRESET) { 2389b18825a7SDavid Howells struct dentry *root = nd->root.dentry; 2390b18825a7SDavid Howells struct inode *inode = root->d_inode; 239193893862SAl Viro if (*s && unlikely(!d_can_lookup(root))) 2392368ee9baSAl Viro return ERR_PTR(-ENOTDIR); 23935b6ca027SAl Viro nd->path = nd->root; 23945b6ca027SAl Viro nd->inode = inode; 23955b6ca027SAl Viro if (flags & LOOKUP_RCU) { 2396ab87f9a5SAleksa Sarai nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 23978f47a016SAl Viro nd->root_seq = nd->seq; 23985b6ca027SAl Viro } else { 23995b6ca027SAl Viro path_get(&nd->path); 24005b6ca027SAl Viro } 2401368ee9baSAl Viro return s; 24025b6ca027SAl Viro } 24035b6ca027SAl Viro 240431e6b01fSNick Piggin nd->root.mnt = NULL; 240531e6b01fSNick Piggin 24068db52c7eSAleksa Sarai /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ 24078db52c7eSAleksa Sarai if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { 2408740a1678SAleksa Sarai error = nd_jump_root(nd); 2409740a1678SAleksa Sarai if (unlikely(error)) 2410740a1678SAleksa Sarai return ERR_PTR(error); 2411ef55d917SAl Viro return s; 24128db52c7eSAleksa Sarai } 24138db52c7eSAleksa Sarai 24148db52c7eSAleksa Sarai /* Relative pathname -- get the starting-point it is relative to. */ 24158db52c7eSAleksa Sarai if (nd->dfd == AT_FDCWD) { 2416e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 241731e6b01fSNick Piggin struct fs_struct *fs = current->fs; 2418c28cc364SNick Piggin unsigned seq; 241931e6b01fSNick Piggin 2420c28cc364SNick Piggin do { 2421c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 242231e6b01fSNick Piggin nd->path = fs->pwd; 2423ef55d917SAl Viro nd->inode = nd->path.dentry->d_inode; 2424c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 2425c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 2426e41f7d4eSAl Viro } else { 2427e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 2428ef55d917SAl Viro nd->inode = nd->path.dentry->d_inode; 2429e41f7d4eSAl Viro } 243031e6b01fSNick Piggin } else { 2431582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 2432c8a53ee5SAl Viro struct fd f = fdget_raw(nd->dfd); 243331e6b01fSNick Piggin struct dentry *dentry; 243431e6b01fSNick Piggin 24352903ff01SAl Viro if (!f.file) 2436368ee9baSAl Viro return ERR_PTR(-EBADF); 243731e6b01fSNick Piggin 24382903ff01SAl Viro dentry = f.file->f_path.dentry; 243931e6b01fSNick Piggin 2440edc2b1daSAl Viro if (*s && unlikely(!d_can_lookup(dentry))) { 24412903ff01SAl Viro fdput(f); 2442368ee9baSAl Viro return ERR_PTR(-ENOTDIR); 2443f52e0c11SAl Viro } 24442903ff01SAl Viro 24452903ff01SAl Viro nd->path = f.file->f_path; 2446e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 244734a26b99SAl Viro nd->inode = nd->path.dentry->d_inode; 244834a26b99SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 24495590ff0dSUlrich Drepper } else { 24502903ff01SAl Viro path_get(&nd->path); 245134a26b99SAl Viro nd->inode = nd->path.dentry->d_inode; 24521da177e4SLinus Torvalds } 245334a26b99SAl Viro fdput(f); 2454e41f7d4eSAl Viro } 24558db52c7eSAleksa Sarai 2456adb21d2bSAleksa Sarai /* For scoped-lookups we need to set the root to the dirfd as well. */ 2457adb21d2bSAleksa Sarai if (flags & LOOKUP_IS_SCOPED) { 2458adb21d2bSAleksa Sarai nd->root = nd->path; 2459adb21d2bSAleksa Sarai if (flags & LOOKUP_RCU) { 2460adb21d2bSAleksa Sarai nd->root_seq = nd->seq; 2461adb21d2bSAleksa Sarai } else { 2462adb21d2bSAleksa Sarai path_get(&nd->root); 2463bcba1e7dSAl Viro nd->state |= ND_ROOT_GRABBED; 2464adb21d2bSAleksa Sarai } 2465adb21d2bSAleksa Sarai } 2466adb21d2bSAleksa Sarai return s; 24679b4a9b14SAl Viro } 24689b4a9b14SAl Viro 24691ccac622SAl Viro static inline const char *lookup_last(struct nameidata *nd) 247095fa25d9SAl Viro { 2471bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 2472bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2473bd92d7feSAl Viro 2474c108837eSAl Viro return walk_component(nd, WALK_TRAILING); 2475bd92d7feSAl Viro } 2476bd92d7feSAl Viro 24774f757f3cSAl Viro static int handle_lookup_down(struct nameidata *nd) 24784f757f3cSAl Viro { 2479c153007bSAl Viro if (!(nd->flags & LOOKUP_RCU)) 2480db3c9adeSAl Viro dget(nd->path.dentry); 2481b0417d2cSAl Viro return PTR_ERR(step_into(nd, WALK_NOFOLLOW, 2482b0417d2cSAl Viro nd->path.dentry, nd->inode, nd->seq)); 24834f757f3cSAl Viro } 24844f757f3cSAl Viro 24859b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2486c8a53ee5SAl Viro static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) 24879b4a9b14SAl Viro { 2488c8a53ee5SAl Viro const char *s = path_init(nd, flags); 2489bd92d7feSAl Viro int err; 249031e6b01fSNick Piggin 24919b5858e9SAl Viro if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { 24924f757f3cSAl Viro err = handle_lookup_down(nd); 24935f336e72SAl Viro if (unlikely(err < 0)) 24945f336e72SAl Viro s = ERR_PTR(err); 24954f757f3cSAl Viro } 24964f757f3cSAl Viro 24971ccac622SAl Viro while (!(err = link_path_walk(s, nd)) && 24981ccac622SAl Viro (s = lookup_last(nd)) != NULL) 24991ccac622SAl Viro ; 25004f0ed93fSAl Viro if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { 25014f0ed93fSAl Viro err = handle_lookup_down(nd); 2502bcba1e7dSAl Viro nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... 25034f0ed93fSAl Viro } 25049f1fafeeSAl Viro if (!err) 25059f1fafeeSAl Viro err = complete_walk(nd); 2506bd92d7feSAl Viro 2507deb106c6SAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) 2508deb106c6SAl Viro if (!d_can_lookup(nd->path.dentry)) 2509bd23a539SAl Viro err = -ENOTDIR; 2510625b6d10SAl Viro if (!err) { 2511625b6d10SAl Viro *path = nd->path; 2512625b6d10SAl Viro nd->path.mnt = NULL; 2513625b6d10SAl Viro nd->path.dentry = NULL; 2514625b6d10SAl Viro } 2515deb106c6SAl Viro terminate_walk(nd); 2516bd92d7feSAl Viro return err; 251731e6b01fSNick Piggin } 251831e6b01fSNick Piggin 2519794ebceaSStephen Brennan int filename_lookup(int dfd, struct filename *name, unsigned flags, 25209ad1aaa6SAl Viro struct path *path, struct path *root) 2521873f1eedSJeff Layton { 2522894bc8c4SAl Viro int retval; 25239883d185SAl Viro struct nameidata nd; 2524abc9f5beSAl Viro if (IS_ERR(name)) 2525abc9f5beSAl Viro return PTR_ERR(name); 252606422964SAl Viro set_nameidata(&nd, dfd, name, root); 2527c8a53ee5SAl Viro retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); 2528873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2529c8a53ee5SAl Viro retval = path_lookupat(&nd, flags, path); 2530873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2531c8a53ee5SAl Viro retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); 2532873f1eedSJeff Layton 2533873f1eedSJeff Layton if (likely(!retval)) 2534161aff1dSAl Viro audit_inode(name, path->dentry, 2535161aff1dSAl Viro flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); 25369883d185SAl Viro restore_nameidata(); 2537020250f3SDmitry Kadashev return retval; 2538020250f3SDmitry Kadashev } 2539020250f3SDmitry Kadashev 25408bcb77faSAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2541c8a53ee5SAl Viro static int path_parentat(struct nameidata *nd, unsigned flags, 2542391172c4SAl Viro struct path *parent) 25438bcb77faSAl Viro { 2544c8a53ee5SAl Viro const char *s = path_init(nd, flags); 25459b5858e9SAl Viro int err = link_path_walk(s, nd); 25468bcb77faSAl Viro if (!err) 25478bcb77faSAl Viro err = complete_walk(nd); 2548391172c4SAl Viro if (!err) { 2549391172c4SAl Viro *parent = nd->path; 2550391172c4SAl Viro nd->path.mnt = NULL; 2551391172c4SAl Viro nd->path.dentry = NULL; 2552391172c4SAl Viro } 2553deb106c6SAl Viro terminate_walk(nd); 25548bcb77faSAl Viro return err; 25558bcb77faSAl Viro } 25568bcb77faSAl Viro 25570766ec82SStephen Brennan /* Note: this does not consume "name" */ 2558c5f563f9SAl Viro static int filename_parentat(int dfd, struct filename *name, 2559391172c4SAl Viro unsigned int flags, struct path *parent, 2560391172c4SAl Viro struct qstr *last, int *type) 25618bcb77faSAl Viro { 25628bcb77faSAl Viro int retval; 25639883d185SAl Viro struct nameidata nd; 25648bcb77faSAl Viro 25655c31b6ceSAl Viro if (IS_ERR(name)) 25660ee50b47SDmitry Kadashev return PTR_ERR(name); 256706422964SAl Viro set_nameidata(&nd, dfd, name, NULL); 2568c8a53ee5SAl Viro retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); 25698bcb77faSAl Viro if (unlikely(retval == -ECHILD)) 2570c8a53ee5SAl Viro retval = path_parentat(&nd, flags, parent); 25718bcb77faSAl Viro if (unlikely(retval == -ESTALE)) 2572c8a53ee5SAl Viro retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); 2573391172c4SAl Viro if (likely(!retval)) { 2574391172c4SAl Viro *last = nd.last; 2575391172c4SAl Viro *type = nd.last_type; 2576c9b07eabSAl Viro audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); 2577391172c4SAl Viro } 25789883d185SAl Viro restore_nameidata(); 25790ee50b47SDmitry Kadashev return retval; 25800ee50b47SDmitry Kadashev } 25810ee50b47SDmitry Kadashev 258279714f72SAl Viro /* does lookup, returns the object with parent locked */ 25830766ec82SStephen Brennan static struct dentry *__kern_path_locked(struct filename *name, struct path *path) 25845590ff0dSUlrich Drepper { 25855c31b6ceSAl Viro struct dentry *d; 2586391172c4SAl Viro struct qstr last; 25870ee50b47SDmitry Kadashev int type, error; 258851689104SPaul Moore 2589c5f563f9SAl Viro error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type); 25900ee50b47SDmitry Kadashev if (error) 25910ee50b47SDmitry Kadashev return ERR_PTR(error); 25925c31b6ceSAl Viro if (unlikely(type != LAST_NORM)) { 2593391172c4SAl Viro path_put(path); 25945c31b6ceSAl Viro return ERR_PTR(-EINVAL); 259579714f72SAl Viro } 25965955102cSAl Viro inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 2597391172c4SAl Viro d = __lookup_hash(&last, path->dentry, 0); 259879714f72SAl Viro if (IS_ERR(d)) { 25995955102cSAl Viro inode_unlock(path->dentry->d_inode); 2600391172c4SAl Viro path_put(path); 260179714f72SAl Viro } 260279714f72SAl Viro return d; 26035590ff0dSUlrich Drepper } 26045590ff0dSUlrich Drepper 26050766ec82SStephen Brennan struct dentry *kern_path_locked(const char *name, struct path *path) 26060766ec82SStephen Brennan { 26070766ec82SStephen Brennan struct filename *filename = getname_kernel(name); 26080766ec82SStephen Brennan struct dentry *res = __kern_path_locked(filename, path); 26090766ec82SStephen Brennan 26100766ec82SStephen Brennan putname(filename); 26110766ec82SStephen Brennan return res; 26120766ec82SStephen Brennan } 26130766ec82SStephen Brennan 2614d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2615d1811465SAl Viro { 2616794ebceaSStephen Brennan struct filename *filename = getname_kernel(name); 2617794ebceaSStephen Brennan int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL); 2618794ebceaSStephen Brennan 2619794ebceaSStephen Brennan putname(filename); 2620794ebceaSStephen Brennan return ret; 2621794ebceaSStephen Brennan 2622d1811465SAl Viro } 26234d359507SAl Viro EXPORT_SYMBOL(kern_path); 2624d1811465SAl Viro 262516f18200SJosef 'Jeff' Sipek /** 262616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 262716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 262816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 262916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 263016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2631e0a01249SAl Viro * @path: pointer to struct path to fill 263216f18200SJosef 'Jeff' Sipek */ 263316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 263416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2635e0a01249SAl Viro struct path *path) 263616f18200SJosef 'Jeff' Sipek { 2637794ebceaSStephen Brennan struct filename *filename; 26389ad1aaa6SAl Viro struct path root = {.mnt = mnt, .dentry = dentry}; 2639794ebceaSStephen Brennan int ret; 2640794ebceaSStephen Brennan 2641794ebceaSStephen Brennan filename = getname_kernel(name); 26429ad1aaa6SAl Viro /* the first argument of filename_lookup() is ignored with root */ 2643794ebceaSStephen Brennan ret = filename_lookup(AT_FDCWD, filename, flags, path, &root); 2644794ebceaSStephen Brennan putname(filename); 2645794ebceaSStephen Brennan return ret; 264616f18200SJosef 'Jeff' Sipek } 26474d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup); 264816f18200SJosef 'Jeff' Sipek 2649c2fd68b6SChristian Brauner static int lookup_one_common(struct user_namespace *mnt_userns, 2650c2fd68b6SChristian Brauner const char *name, struct dentry *base, int len, 2651c2fd68b6SChristian Brauner struct qstr *this) 26523c95f0dcSAl Viro { 26533c95f0dcSAl Viro this->name = name; 26543c95f0dcSAl Viro this->len = len; 26553c95f0dcSAl Viro this->hash = full_name_hash(base, name, len); 26563c95f0dcSAl Viro if (!len) 26573c95f0dcSAl Viro return -EACCES; 26583c95f0dcSAl Viro 26593c95f0dcSAl Viro if (unlikely(name[0] == '.')) { 26603c95f0dcSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 26613c95f0dcSAl Viro return -EACCES; 26623c95f0dcSAl Viro } 26633c95f0dcSAl Viro 26643c95f0dcSAl Viro while (len--) { 26653c95f0dcSAl Viro unsigned int c = *(const unsigned char *)name++; 26663c95f0dcSAl Viro if (c == '/' || c == '\0') 26673c95f0dcSAl Viro return -EACCES; 26683c95f0dcSAl Viro } 26693c95f0dcSAl Viro /* 26703c95f0dcSAl Viro * See if the low-level filesystem might want 26713c95f0dcSAl Viro * to use its own hash.. 26723c95f0dcSAl Viro */ 26733c95f0dcSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 26743c95f0dcSAl Viro int err = base->d_op->d_hash(base, this); 26753c95f0dcSAl Viro if (err < 0) 26763c95f0dcSAl Viro return err; 26773c95f0dcSAl Viro } 26783c95f0dcSAl Viro 2679c2fd68b6SChristian Brauner return inode_permission(mnt_userns, base->d_inode, MAY_EXEC); 26803c95f0dcSAl Viro } 26813c95f0dcSAl Viro 2682eead1911SChristoph Hellwig /** 26830da0b7fdSDavid Howells * try_lookup_one_len - filesystem helper to lookup single pathname component 26840da0b7fdSDavid Howells * @name: pathname component to lookup 26850da0b7fdSDavid Howells * @base: base directory to lookup from 26860da0b7fdSDavid Howells * @len: maximum length @len should be interpreted to 26870da0b7fdSDavid Howells * 26880da0b7fdSDavid Howells * Look up a dentry by name in the dcache, returning NULL if it does not 26890da0b7fdSDavid Howells * currently exist. The function does not try to create a dentry. 26900da0b7fdSDavid Howells * 26910da0b7fdSDavid Howells * Note that this routine is purely a helper for filesystem usage and should 26920da0b7fdSDavid Howells * not be called by generic code. 26930da0b7fdSDavid Howells * 26940da0b7fdSDavid Howells * The caller must hold base->i_mutex. 26950da0b7fdSDavid Howells */ 26960da0b7fdSDavid Howells struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) 26970da0b7fdSDavid Howells { 26980da0b7fdSDavid Howells struct qstr this; 26990da0b7fdSDavid Howells int err; 27000da0b7fdSDavid Howells 27010da0b7fdSDavid Howells WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 27020da0b7fdSDavid Howells 2703c2fd68b6SChristian Brauner err = lookup_one_common(&init_user_ns, name, base, len, &this); 27040da0b7fdSDavid Howells if (err) 27050da0b7fdSDavid Howells return ERR_PTR(err); 27060da0b7fdSDavid Howells 27070da0b7fdSDavid Howells return lookup_dcache(&this, base, 0); 27080da0b7fdSDavid Howells } 27090da0b7fdSDavid Howells EXPORT_SYMBOL(try_lookup_one_len); 27100da0b7fdSDavid Howells 27110da0b7fdSDavid Howells /** 2712a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2713eead1911SChristoph Hellwig * @name: pathname component to lookup 2714eead1911SChristoph Hellwig * @base: base directory to lookup from 2715eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2716eead1911SChristoph Hellwig * 2717a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 27189e7543e9SAl Viro * not be called by generic code. 2719bbddca8eSNeilBrown * 2720bbddca8eSNeilBrown * The caller must hold base->i_mutex. 2721eead1911SChristoph Hellwig */ 2722057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2723057f6c01SJames Morris { 27248613a209SAl Viro struct dentry *dentry; 2725057f6c01SJames Morris struct qstr this; 2726cda309deSMiklos Szeredi int err; 2727057f6c01SJames Morris 27285955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 27292f9092e1SDavid Woodhouse 2730c2fd68b6SChristian Brauner err = lookup_one_common(&init_user_ns, name, base, len, &this); 2731cda309deSMiklos Szeredi if (err) 2732cda309deSMiklos Szeredi return ERR_PTR(err); 2733cda309deSMiklos Szeredi 27348613a209SAl Viro dentry = lookup_dcache(&this, base, 0); 27358613a209SAl Viro return dentry ? dentry : __lookup_slow(&this, base, 0); 2736057f6c01SJames Morris } 27374d359507SAl Viro EXPORT_SYMBOL(lookup_one_len); 2738057f6c01SJames Morris 2739bbddca8eSNeilBrown /** 2740c2fd68b6SChristian Brauner * lookup_one - filesystem helper to lookup single pathname component 2741c2fd68b6SChristian Brauner * @mnt_userns: user namespace of the mount the lookup is performed from 2742c2fd68b6SChristian Brauner * @name: pathname component to lookup 2743c2fd68b6SChristian Brauner * @base: base directory to lookup from 2744c2fd68b6SChristian Brauner * @len: maximum length @len should be interpreted to 2745c2fd68b6SChristian Brauner * 2746c2fd68b6SChristian Brauner * Note that this routine is purely a helper for filesystem usage and should 2747c2fd68b6SChristian Brauner * not be called by generic code. 2748c2fd68b6SChristian Brauner * 2749c2fd68b6SChristian Brauner * The caller must hold base->i_mutex. 2750c2fd68b6SChristian Brauner */ 2751c2fd68b6SChristian Brauner struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name, 2752c2fd68b6SChristian Brauner struct dentry *base, int len) 2753c2fd68b6SChristian Brauner { 2754c2fd68b6SChristian Brauner struct dentry *dentry; 2755c2fd68b6SChristian Brauner struct qstr this; 2756c2fd68b6SChristian Brauner int err; 2757c2fd68b6SChristian Brauner 2758c2fd68b6SChristian Brauner WARN_ON_ONCE(!inode_is_locked(base->d_inode)); 2759c2fd68b6SChristian Brauner 2760c2fd68b6SChristian Brauner err = lookup_one_common(mnt_userns, name, base, len, &this); 2761c2fd68b6SChristian Brauner if (err) 2762c2fd68b6SChristian Brauner return ERR_PTR(err); 2763c2fd68b6SChristian Brauner 2764c2fd68b6SChristian Brauner dentry = lookup_dcache(&this, base, 0); 2765c2fd68b6SChristian Brauner return dentry ? dentry : __lookup_slow(&this, base, 0); 2766c2fd68b6SChristian Brauner } 2767c2fd68b6SChristian Brauner EXPORT_SYMBOL(lookup_one); 2768c2fd68b6SChristian Brauner 2769c2fd68b6SChristian Brauner /** 277000675017SChristian Brauner * lookup_one_unlocked - filesystem helper to lookup single pathname component 277100675017SChristian Brauner * @mnt_userns: idmapping of the mount the lookup is performed from 277200675017SChristian Brauner * @name: pathname component to lookup 277300675017SChristian Brauner * @base: base directory to lookup from 277400675017SChristian Brauner * @len: maximum length @len should be interpreted to 277500675017SChristian Brauner * 277600675017SChristian Brauner * Note that this routine is purely a helper for filesystem usage and should 277700675017SChristian Brauner * not be called by generic code. 277800675017SChristian Brauner * 277900675017SChristian Brauner * Unlike lookup_one_len, it should be called without the parent 278000675017SChristian Brauner * i_mutex held, and will take the i_mutex itself if necessary. 278100675017SChristian Brauner */ 278200675017SChristian Brauner struct dentry *lookup_one_unlocked(struct user_namespace *mnt_userns, 278300675017SChristian Brauner const char *name, struct dentry *base, 278400675017SChristian Brauner int len) 278500675017SChristian Brauner { 278600675017SChristian Brauner struct qstr this; 278700675017SChristian Brauner int err; 278800675017SChristian Brauner struct dentry *ret; 278900675017SChristian Brauner 279000675017SChristian Brauner err = lookup_one_common(mnt_userns, name, base, len, &this); 279100675017SChristian Brauner if (err) 279200675017SChristian Brauner return ERR_PTR(err); 279300675017SChristian Brauner 279400675017SChristian Brauner ret = lookup_dcache(&this, base, 0); 279500675017SChristian Brauner if (!ret) 279600675017SChristian Brauner ret = lookup_slow(&this, base, 0); 279700675017SChristian Brauner return ret; 279800675017SChristian Brauner } 279900675017SChristian Brauner EXPORT_SYMBOL(lookup_one_unlocked); 280000675017SChristian Brauner 280100675017SChristian Brauner /** 280200675017SChristian Brauner * lookup_one_positive_unlocked - filesystem helper to lookup single 280300675017SChristian Brauner * pathname component 280400675017SChristian Brauner * @mnt_userns: idmapping of the mount the lookup is performed from 280500675017SChristian Brauner * @name: pathname component to lookup 280600675017SChristian Brauner * @base: base directory to lookup from 280700675017SChristian Brauner * @len: maximum length @len should be interpreted to 280800675017SChristian Brauner * 280900675017SChristian Brauner * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns 281000675017SChristian Brauner * known positive or ERR_PTR(). This is what most of the users want. 281100675017SChristian Brauner * 281200675017SChristian Brauner * Note that pinned negative with unlocked parent _can_ become positive at any 281300675017SChristian Brauner * time, so callers of lookup_one_unlocked() need to be very careful; pinned 281400675017SChristian Brauner * positives have >d_inode stable, so this one avoids such problems. 281500675017SChristian Brauner * 281600675017SChristian Brauner * Note that this routine is purely a helper for filesystem usage and should 281700675017SChristian Brauner * not be called by generic code. 281800675017SChristian Brauner * 281900675017SChristian Brauner * The helper should be called without i_mutex held. 282000675017SChristian Brauner */ 282100675017SChristian Brauner struct dentry *lookup_one_positive_unlocked(struct user_namespace *mnt_userns, 282200675017SChristian Brauner const char *name, 282300675017SChristian Brauner struct dentry *base, int len) 282400675017SChristian Brauner { 282500675017SChristian Brauner struct dentry *ret = lookup_one_unlocked(mnt_userns, name, base, len); 282600675017SChristian Brauner 282700675017SChristian Brauner if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { 282800675017SChristian Brauner dput(ret); 282900675017SChristian Brauner ret = ERR_PTR(-ENOENT); 283000675017SChristian Brauner } 283100675017SChristian Brauner return ret; 283200675017SChristian Brauner } 283300675017SChristian Brauner EXPORT_SYMBOL(lookup_one_positive_unlocked); 283400675017SChristian Brauner 283500675017SChristian Brauner /** 2836bbddca8eSNeilBrown * lookup_one_len_unlocked - filesystem helper to lookup single pathname component 2837bbddca8eSNeilBrown * @name: pathname component to lookup 2838bbddca8eSNeilBrown * @base: base directory to lookup from 2839bbddca8eSNeilBrown * @len: maximum length @len should be interpreted to 2840bbddca8eSNeilBrown * 2841bbddca8eSNeilBrown * Note that this routine is purely a helper for filesystem usage and should 2842bbddca8eSNeilBrown * not be called by generic code. 2843bbddca8eSNeilBrown * 2844bbddca8eSNeilBrown * Unlike lookup_one_len, it should be called without the parent 2845bbddca8eSNeilBrown * i_mutex held, and will take the i_mutex itself if necessary. 2846bbddca8eSNeilBrown */ 2847bbddca8eSNeilBrown struct dentry *lookup_one_len_unlocked(const char *name, 2848bbddca8eSNeilBrown struct dentry *base, int len) 2849bbddca8eSNeilBrown { 285000675017SChristian Brauner return lookup_one_unlocked(&init_user_ns, name, base, len); 2851bbddca8eSNeilBrown } 2852bbddca8eSNeilBrown EXPORT_SYMBOL(lookup_one_len_unlocked); 2853bbddca8eSNeilBrown 28546c2d4798SAl Viro /* 28556c2d4798SAl Viro * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) 28566c2d4798SAl Viro * on negatives. Returns known positive or ERR_PTR(); that's what 28576c2d4798SAl Viro * most of the users want. Note that pinned negative with unlocked parent 28586c2d4798SAl Viro * _can_ become positive at any time, so callers of lookup_one_len_unlocked() 28596c2d4798SAl Viro * need to be very careful; pinned positives have ->d_inode stable, so 28606c2d4798SAl Viro * this one avoids such problems. 28616c2d4798SAl Viro */ 28626c2d4798SAl Viro struct dentry *lookup_positive_unlocked(const char *name, 28636c2d4798SAl Viro struct dentry *base, int len) 28646c2d4798SAl Viro { 286500675017SChristian Brauner return lookup_one_positive_unlocked(&init_user_ns, name, base, len); 28666c2d4798SAl Viro } 28676c2d4798SAl Viro EXPORT_SYMBOL(lookup_positive_unlocked); 28686c2d4798SAl Viro 2869eedf265aSEric W. Biederman #ifdef CONFIG_UNIX98_PTYS 2870eedf265aSEric W. Biederman int path_pts(struct path *path) 2871eedf265aSEric W. Biederman { 2872eedf265aSEric W. Biederman /* Find something mounted on "pts" in the same directory as 2873eedf265aSEric W. Biederman * the input path. 2874eedf265aSEric W. Biederman */ 2875a6a7eb76SAl Viro struct dentry *parent = dget_parent(path->dentry); 2876a6a7eb76SAl Viro struct dentry *child; 287719f6028aSAl Viro struct qstr this = QSTR_INIT("pts", 3); 2878eedf265aSEric W. Biederman 2879a6a7eb76SAl Viro if (unlikely(!path_connected(path->mnt, parent))) { 2880a6a7eb76SAl Viro dput(parent); 288163b27720SAl Viro return -ENOENT; 2882a6a7eb76SAl Viro } 288363b27720SAl Viro dput(path->dentry); 288463b27720SAl Viro path->dentry = parent; 2885eedf265aSEric W. Biederman child = d_hash_and_lookup(parent, &this); 2886eedf265aSEric W. Biederman if (!child) 2887eedf265aSEric W. Biederman return -ENOENT; 2888eedf265aSEric W. Biederman 2889eedf265aSEric W. Biederman path->dentry = child; 2890eedf265aSEric W. Biederman dput(parent); 289119f6028aSAl Viro follow_down(path); 2892eedf265aSEric W. Biederman return 0; 2893eedf265aSEric W. Biederman } 2894eedf265aSEric W. Biederman #endif 2895eedf265aSEric W. Biederman 28961fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 28971fa1e7f6SAndy Whitcroft struct path *path, int *empty) 28981da177e4SLinus Torvalds { 2899794ebceaSStephen Brennan struct filename *filename = getname_flags(name, flags, empty); 2900794ebceaSStephen Brennan int ret = filename_lookup(dfd, filename, flags, path, NULL); 2901794ebceaSStephen Brennan 2902794ebceaSStephen Brennan putname(filename); 2903794ebceaSStephen Brennan return ret; 29041da177e4SLinus Torvalds } 2905b853a161SAl Viro EXPORT_SYMBOL(user_path_at_empty); 29061fa1e7f6SAndy Whitcroft 2907ba73d987SChristian Brauner int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir, 2908ba73d987SChristian Brauner struct inode *inode) 29091da177e4SLinus Torvalds { 29108e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2911da9592edSDavid Howells 2912ba73d987SChristian Brauner if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid)) 29131da177e4SLinus Torvalds return 0; 2914ba73d987SChristian Brauner if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid)) 29151da177e4SLinus Torvalds return 0; 2916ba73d987SChristian Brauner return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER); 29171da177e4SLinus Torvalds } 2918cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky); 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds /* 29211da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 29221da177e4SLinus Torvalds * whether the type of victim is right. 29231da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 29241da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 29251da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 29261da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 29271da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 29281da177e4SLinus Torvalds * a. be owner of dir, or 29291da177e4SLinus Torvalds * b. be owner of victim, or 29301da177e4SLinus Torvalds * c. have CAP_FOWNER capability 29311da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 29321da177e4SLinus Torvalds * links pointing to it. 29330bd23d09SEric W. Biederman * 7. If the victim has an unknown uid or gid we can't change the inode. 29340bd23d09SEric W. Biederman * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. 29350bd23d09SEric W. Biederman * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. 29360bd23d09SEric W. Biederman * 10. We can't remove a root or mountpoint. 29370bd23d09SEric W. Biederman * 11. We don't allow removal of NFS sillyrenamed files; it's handled by 29381da177e4SLinus Torvalds * nfs_async_unlink(). 29391da177e4SLinus Torvalds */ 2940ba73d987SChristian Brauner static int may_delete(struct user_namespace *mnt_userns, struct inode *dir, 2941ba73d987SChristian Brauner struct dentry *victim, bool isdir) 29421da177e4SLinus Torvalds { 294363afdfc7SDavid Howells struct inode *inode = d_backing_inode(victim); 29441da177e4SLinus Torvalds int error; 29451da177e4SLinus Torvalds 2946b18825a7SDavid Howells if (d_is_negative(victim)) 29471da177e4SLinus Torvalds return -ENOENT; 2948b18825a7SDavid Howells BUG_ON(!inode); 29491da177e4SLinus Torvalds 29501da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 2951593d1ce8SEric W. Biederman 2952593d1ce8SEric W. Biederman /* Inode writeback is not safe when the uid or gid are invalid. */ 2953ba73d987SChristian Brauner if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) || 2954ba73d987SChristian Brauner !gid_valid(i_gid_into_mnt(mnt_userns, inode))) 2955593d1ce8SEric W. Biederman return -EOVERFLOW; 2956593d1ce8SEric W. Biederman 29574fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 29581da177e4SLinus Torvalds 2959ba73d987SChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 29601da177e4SLinus Torvalds if (error) 29611da177e4SLinus Torvalds return error; 29621da177e4SLinus Torvalds if (IS_APPEND(dir)) 29631da177e4SLinus Torvalds return -EPERM; 2964b18825a7SDavid Howells 2965ba73d987SChristian Brauner if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) || 2966ba73d987SChristian Brauner IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || 2967ba73d987SChristian Brauner HAS_UNMAPPED_ID(mnt_userns, inode)) 29681da177e4SLinus Torvalds return -EPERM; 29691da177e4SLinus Torvalds if (isdir) { 297044b1d530SMiklos Szeredi if (!d_is_dir(victim)) 29711da177e4SLinus Torvalds return -ENOTDIR; 29721da177e4SLinus Torvalds if (IS_ROOT(victim)) 29731da177e4SLinus Torvalds return -EBUSY; 297444b1d530SMiklos Szeredi } else if (d_is_dir(victim)) 29751da177e4SLinus Torvalds return -EISDIR; 29761da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 29771da177e4SLinus Torvalds return -ENOENT; 29781da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 29791da177e4SLinus Torvalds return -EBUSY; 29801da177e4SLinus Torvalds return 0; 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 29841da177e4SLinus Torvalds * dir. 29851da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 29861da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 29871da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 2988036d5236SEric W. Biederman * 3. We can't do it if the fs can't represent the fsuid or fsgid. 2989036d5236SEric W. Biederman * 4. We should have write and exec permissions on dir 2990036d5236SEric W. Biederman * 5. We can't do it if dir is immutable (done in permission()) 29911da177e4SLinus Torvalds */ 2992ba73d987SChristian Brauner static inline int may_create(struct user_namespace *mnt_userns, 2993ba73d987SChristian Brauner struct inode *dir, struct dentry *child) 29941da177e4SLinus Torvalds { 299514e972b4SJeff Layton audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); 29961da177e4SLinus Torvalds if (child->d_inode) 29971da177e4SLinus Torvalds return -EEXIST; 29981da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 29991da177e4SLinus Torvalds return -ENOENT; 30008e538913SChristian Brauner if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) 3001036d5236SEric W. Biederman return -EOVERFLOW; 30028e538913SChristian Brauner 3003ba73d987SChristian Brauner return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 30061da177e4SLinus Torvalds /* 30071da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 30081da177e4SLinus Torvalds */ 30091da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 30101da177e4SLinus Torvalds { 30111da177e4SLinus Torvalds struct dentry *p; 30121da177e4SLinus Torvalds 30131da177e4SLinus Torvalds if (p1 == p2) { 30145955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 30151da177e4SLinus Torvalds return NULL; 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds 3018fc64005cSAl Viro mutex_lock(&p1->d_sb->s_vfs_rename_mutex); 30191da177e4SLinus Torvalds 3020e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 3021e2761a11SOGAWA Hirofumi if (p) { 30225955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); 30235955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); 30241da177e4SLinus Torvalds return p; 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds 3027e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 3028e2761a11SOGAWA Hirofumi if (p) { 30295955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 30305955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); 30311da177e4SLinus Torvalds return p; 30321da177e4SLinus Torvalds } 30331da177e4SLinus Torvalds 30345955102cSAl Viro inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); 30355955102cSAl Viro inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); 30361da177e4SLinus Torvalds return NULL; 30371da177e4SLinus Torvalds } 30384d359507SAl Viro EXPORT_SYMBOL(lock_rename); 30391da177e4SLinus Torvalds 30401da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 30411da177e4SLinus Torvalds { 30425955102cSAl Viro inode_unlock(p1->d_inode); 30431da177e4SLinus Torvalds if (p1 != p2) { 30445955102cSAl Viro inode_unlock(p2->d_inode); 3045fc64005cSAl Viro mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); 30461da177e4SLinus Torvalds } 30471da177e4SLinus Torvalds } 30484d359507SAl Viro EXPORT_SYMBOL(unlock_rename); 30491da177e4SLinus Torvalds 30506521f891SChristian Brauner /** 30516521f891SChristian Brauner * vfs_create - create new file 30526521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 30536521f891SChristian Brauner * @dir: inode of @dentry 30546521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 30556521f891SChristian Brauner * @mode: mode of the new file 30566521f891SChristian Brauner * @want_excl: whether the file must not yet exist 30576521f891SChristian Brauner * 30586521f891SChristian Brauner * Create a new file. 30596521f891SChristian Brauner * 30606521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 30616521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 30626521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 30636521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 30646521f891SChristian Brauner * raw inode simply passs init_user_ns. 30656521f891SChristian Brauner */ 30666521f891SChristian Brauner int vfs_create(struct user_namespace *mnt_userns, struct inode *dir, 30676521f891SChristian Brauner struct dentry *dentry, umode_t mode, bool want_excl) 30681da177e4SLinus Torvalds { 30696521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 30701da177e4SLinus Torvalds if (error) 30711da177e4SLinus Torvalds return error; 30721da177e4SLinus Torvalds 3073acfa4380SAl Viro if (!dir->i_op->create) 30741da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 30751da177e4SLinus Torvalds mode &= S_IALLUGO; 30761da177e4SLinus Torvalds mode |= S_IFREG; 30771da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 30781da177e4SLinus Torvalds if (error) 30791da177e4SLinus Torvalds return error; 3080549c7297SChristian Brauner error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl); 3081a74574aaSStephen Smalley if (!error) 3082f38aa942SAmy Griffis fsnotify_create(dir, dentry); 30831da177e4SLinus Torvalds return error; 30841da177e4SLinus Torvalds } 30854d359507SAl Viro EXPORT_SYMBOL(vfs_create); 30861da177e4SLinus Torvalds 30878e6c848eSAl Viro int vfs_mkobj(struct dentry *dentry, umode_t mode, 30888e6c848eSAl Viro int (*f)(struct dentry *, umode_t, void *), 30898e6c848eSAl Viro void *arg) 30908e6c848eSAl Viro { 30918e6c848eSAl Viro struct inode *dir = dentry->d_parent->d_inode; 3092ba73d987SChristian Brauner int error = may_create(&init_user_ns, dir, dentry); 30938e6c848eSAl Viro if (error) 30948e6c848eSAl Viro return error; 30958e6c848eSAl Viro 30968e6c848eSAl Viro mode &= S_IALLUGO; 30978e6c848eSAl Viro mode |= S_IFREG; 30988e6c848eSAl Viro error = security_inode_create(dir, dentry, mode); 30998e6c848eSAl Viro if (error) 31008e6c848eSAl Viro return error; 31018e6c848eSAl Viro error = f(dentry, mode, arg); 31028e6c848eSAl Viro if (!error) 31038e6c848eSAl Viro fsnotify_create(dir, dentry); 31048e6c848eSAl Viro return error; 31058e6c848eSAl Viro } 31068e6c848eSAl Viro EXPORT_SYMBOL(vfs_mkobj); 31078e6c848eSAl Viro 3108a2982cc9SEric W. Biederman bool may_open_dev(const struct path *path) 3109a2982cc9SEric W. Biederman { 3110a2982cc9SEric W. Biederman return !(path->mnt->mnt_flags & MNT_NODEV) && 3111a2982cc9SEric W. Biederman !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); 3112a2982cc9SEric W. Biederman } 3113a2982cc9SEric W. Biederman 3114ba73d987SChristian Brauner static int may_open(struct user_namespace *mnt_userns, const struct path *path, 3115ba73d987SChristian Brauner int acc_mode, int flag) 31161da177e4SLinus Torvalds { 31173fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 31181da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 31191da177e4SLinus Torvalds int error; 31201da177e4SLinus Torvalds 31211da177e4SLinus Torvalds if (!inode) 31221da177e4SLinus Torvalds return -ENOENT; 31231da177e4SLinus Torvalds 3124c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 3125c8fe8f30SChristoph Hellwig case S_IFLNK: 31261da177e4SLinus Torvalds return -ELOOP; 3127c8fe8f30SChristoph Hellwig case S_IFDIR: 3128fc4177beSKees Cook if (acc_mode & MAY_WRITE) 31291da177e4SLinus Torvalds return -EISDIR; 3130fc4177beSKees Cook if (acc_mode & MAY_EXEC) 3131fc4177beSKees Cook return -EACCES; 3132c8fe8f30SChristoph Hellwig break; 3133c8fe8f30SChristoph Hellwig case S_IFBLK: 3134c8fe8f30SChristoph Hellwig case S_IFCHR: 3135a2982cc9SEric W. Biederman if (!may_open_dev(path)) 31361da177e4SLinus Torvalds return -EACCES; 3137633fb6acSKees Cook fallthrough; 3138c8fe8f30SChristoph Hellwig case S_IFIFO: 3139c8fe8f30SChristoph Hellwig case S_IFSOCK: 3140633fb6acSKees Cook if (acc_mode & MAY_EXEC) 3141633fb6acSKees Cook return -EACCES; 31421da177e4SLinus Torvalds flag &= ~O_TRUNC; 3143c8fe8f30SChristoph Hellwig break; 31440fd338b2SKees Cook case S_IFREG: 31450fd338b2SKees Cook if ((acc_mode & MAY_EXEC) && path_noexec(path)) 31460fd338b2SKees Cook return -EACCES; 31470fd338b2SKees Cook break; 31484a3fd211SDave Hansen } 3149b41572e9SDave Hansen 3150ba73d987SChristian Brauner error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode); 3151b41572e9SDave Hansen if (error) 3152b41572e9SDave Hansen return error; 31536146f0d5SMimi Zohar 31541da177e4SLinus Torvalds /* 31551da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 31561da177e4SLinus Torvalds */ 31571da177e4SLinus Torvalds if (IS_APPEND(inode)) { 31588737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 31597715b521SAl Viro return -EPERM; 31601da177e4SLinus Torvalds if (flag & O_TRUNC) 31617715b521SAl Viro return -EPERM; 31621da177e4SLinus Torvalds } 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 3165ba73d987SChristian Brauner if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode)) 31667715b521SAl Viro return -EPERM; 31671da177e4SLinus Torvalds 3168f3c7691eSJ. Bruce Fields return 0; 31697715b521SAl Viro } 31707715b521SAl Viro 3171549c7297SChristian Brauner static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) 31727715b521SAl Viro { 3173f0bb5aafSAl Viro const struct path *path = &filp->f_path; 31747715b521SAl Viro struct inode *inode = path->dentry->d_inode; 31757715b521SAl Viro int error = get_write_access(inode); 31761da177e4SLinus Torvalds if (error) 31777715b521SAl Viro return error; 3178482e0007SJeff Layton 3179ea0d3ab2STetsuo Handa error = security_path_truncate(path); 31801da177e4SLinus Torvalds if (!error) { 3181549c7297SChristian Brauner error = do_truncate(mnt_userns, path->dentry, 0, 3182d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 3183e1181ee6SJeff Layton filp); 31841da177e4SLinus Torvalds } 31851da177e4SLinus Torvalds put_write_access(inode); 3186acd0c935SMimi Zohar return error; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds 3189d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 3190d57999e1SDave Hansen { 31918a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 31928a5e929dSAl Viro flag--; 3193d57999e1SDave Hansen return flag; 3194d57999e1SDave Hansen } 3195d57999e1SDave Hansen 3196ba73d987SChristian Brauner static int may_o_create(struct user_namespace *mnt_userns, 3197ba73d987SChristian Brauner const struct path *dir, struct dentry *dentry, 3198ba73d987SChristian Brauner umode_t mode) 3199d18e9008SMiklos Szeredi { 3200d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 3201d18e9008SMiklos Szeredi if (error) 3202d18e9008SMiklos Szeredi return error; 3203d18e9008SMiklos Szeredi 32048e538913SChristian Brauner if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns)) 32051328c727SSeth Forshee return -EOVERFLOW; 32061328c727SSeth Forshee 3207ba73d987SChristian Brauner error = inode_permission(mnt_userns, dir->dentry->d_inode, 320847291baaSChristian Brauner MAY_WRITE | MAY_EXEC); 3209d18e9008SMiklos Szeredi if (error) 3210d18e9008SMiklos Szeredi return error; 3211d18e9008SMiklos Szeredi 3212d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 3213d18e9008SMiklos Szeredi } 3214d18e9008SMiklos Szeredi 32151acf0af9SDavid Howells /* 32161acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 32171acf0af9SDavid Howells * dentry. 32181acf0af9SDavid Howells * 32191acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 32201acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 32211acf0af9SDavid Howells * 322200a07c15SAl Viro * If the file was looked up only or didn't need creating, FMODE_OPENED won't 322300a07c15SAl Viro * be set. The caller will need to perform the open themselves. @path will 322400a07c15SAl Viro * have been updated to point to the new dentry. This may be negative. 32251acf0af9SDavid Howells * 32261acf0af9SDavid Howells * Returns an error code otherwise. 32271acf0af9SDavid Howells */ 3228239eb983SAl Viro static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, 3229239eb983SAl Viro struct file *file, 32303ec2eef1SAl Viro int open_flag, umode_t mode) 3231d18e9008SMiklos Szeredi { 3232d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 3233d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 3234d18e9008SMiklos Szeredi int error; 3235d18e9008SMiklos Szeredi 3236d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 3237d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 3238d18e9008SMiklos Szeredi 323930d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 324030d90494SAl Viro file->f_path.mnt = nd->path.mnt; 32410fb1ea09SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, 324244907d79SAl Viro open_to_namei_flags(open_flag), mode); 32436fbd0714SAl Viro d_lookup_done(dentry); 3244384f26e2SAl Viro if (!error) { 324564e1ac4dSAl Viro if (file->f_mode & FMODE_OPENED) { 32466fb968cdSAl Viro if (unlikely(dentry != file->f_path.dentry)) { 32476fb968cdSAl Viro dput(dentry); 32486fb968cdSAl Viro dentry = dget(file->f_path.dentry); 32496fb968cdSAl Viro } 325064e1ac4dSAl Viro } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 32512675a4ebSAl Viro error = -EIO; 3252384f26e2SAl Viro } else { 325330d90494SAl Viro if (file->f_path.dentry) { 3254d18e9008SMiklos Szeredi dput(dentry); 3255d18e9008SMiklos Szeredi dentry = file->f_path.dentry; 325610c64ceaSAl Viro } 3257239eb983SAl Viro if (unlikely(d_is_negative(dentry))) 3258a01e718fSAl Viro error = -ENOENT; 3259d18e9008SMiklos Szeredi } 3260d18e9008SMiklos Szeredi } 3261239eb983SAl Viro if (error) { 3262d18e9008SMiklos Szeredi dput(dentry); 3263239eb983SAl Viro dentry = ERR_PTR(error); 3264239eb983SAl Viro } 3265239eb983SAl Viro return dentry; 3266d18e9008SMiklos Szeredi } 3267d18e9008SMiklos Szeredi 326831e6b01fSNick Piggin /* 32691acf0af9SDavid Howells * Look up and maybe create and open the last component. 3270d58ffd35SMiklos Szeredi * 327100a07c15SAl Viro * Must be called with parent locked (exclusive in O_CREAT case). 3272d58ffd35SMiklos Szeredi * 327300a07c15SAl Viro * Returns 0 on success, that is, if 327400a07c15SAl Viro * the file was successfully atomically created (if necessary) and opened, or 327500a07c15SAl Viro * the file was not completely opened at this time, though lookups and 327600a07c15SAl Viro * creations were performed. 327700a07c15SAl Viro * These case are distinguished by presence of FMODE_OPENED on file->f_mode. 327800a07c15SAl Viro * In the latter case dentry returned in @path might be negative if O_CREAT 327900a07c15SAl Viro * hadn't been specified. 32801acf0af9SDavid Howells * 328100a07c15SAl Viro * An error code is returned on failure. 3282d58ffd35SMiklos Szeredi */ 3283da5ebf5aSAl Viro static struct dentry *lookup_open(struct nameidata *nd, struct file *file, 3284d58ffd35SMiklos Szeredi const struct open_flags *op, 32853ec2eef1SAl Viro bool got_write) 3286d58ffd35SMiklos Szeredi { 3287549c7297SChristian Brauner struct user_namespace *mnt_userns; 3288d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 328954ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 32901643b43fSAl Viro int open_flag = op->open_flag; 3291d58ffd35SMiklos Szeredi struct dentry *dentry; 32921643b43fSAl Viro int error, create_error = 0; 32931643b43fSAl Viro umode_t mode = op->mode; 32946fbd0714SAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 3295d58ffd35SMiklos Szeredi 3296ce8644fcSAl Viro if (unlikely(IS_DEADDIR(dir_inode))) 3297da5ebf5aSAl Viro return ERR_PTR(-ENOENT); 3298d58ffd35SMiklos Szeredi 329973a09dd9SAl Viro file->f_mode &= ~FMODE_CREATED; 33006fbd0714SAl Viro dentry = d_lookup(dir, &nd->last); 33016fbd0714SAl Viro for (;;) { 33026fbd0714SAl Viro if (!dentry) { 33036fbd0714SAl Viro dentry = d_alloc_parallel(dir, &nd->last, &wq); 3304d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 3305da5ebf5aSAl Viro return dentry; 33066fbd0714SAl Viro } 33076fbd0714SAl Viro if (d_in_lookup(dentry)) 33086fbd0714SAl Viro break; 3309d58ffd35SMiklos Szeredi 33106fbd0714SAl Viro error = d_revalidate(dentry, nd->flags); 33116fbd0714SAl Viro if (likely(error > 0)) 33126fbd0714SAl Viro break; 33136fbd0714SAl Viro if (error) 33146fbd0714SAl Viro goto out_dput; 33156fbd0714SAl Viro d_invalidate(dentry); 33166fbd0714SAl Viro dput(dentry); 33176fbd0714SAl Viro dentry = NULL; 33186fbd0714SAl Viro } 33196fbd0714SAl Viro if (dentry->d_inode) { 3320d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 3321da5ebf5aSAl Viro return dentry; 33226c51e513SAl Viro } 3323d18e9008SMiklos Szeredi 33241643b43fSAl Viro /* 33251643b43fSAl Viro * Checking write permission is tricky, bacuse we don't know if we are 33261643b43fSAl Viro * going to actually need it: O_CREAT opens should work as long as the 33271643b43fSAl Viro * file exists. But checking existence breaks atomicity. The trick is 33281643b43fSAl Viro * to check access and if not granted clear O_CREAT from the flags. 33291643b43fSAl Viro * 33301643b43fSAl Viro * Another problem is returing the "right" error value (e.g. for an 33311643b43fSAl Viro * O_EXCL open we want to return EEXIST not EROFS). 33321643b43fSAl Viro */ 333399a4a90cSAl Viro if (unlikely(!got_write)) 333499a4a90cSAl Viro open_flag &= ~O_TRUNC; 3335549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 33361643b43fSAl Viro if (open_flag & O_CREAT) { 333799a4a90cSAl Viro if (open_flag & O_EXCL) 333899a4a90cSAl Viro open_flag &= ~O_TRUNC; 33391643b43fSAl Viro if (!IS_POSIXACL(dir->d_inode)) 33401643b43fSAl Viro mode &= ~current_umask(); 334199a4a90cSAl Viro if (likely(got_write)) 3342549c7297SChristian Brauner create_error = may_o_create(mnt_userns, &nd->path, 3343ba73d987SChristian Brauner dentry, mode); 334499a4a90cSAl Viro else 334599a4a90cSAl Viro create_error = -EROFS; 334699a4a90cSAl Viro } 334799a4a90cSAl Viro if (create_error) 33481643b43fSAl Viro open_flag &= ~O_CREAT; 33491643b43fSAl Viro if (dir_inode->i_op->atomic_open) { 3350d489cf9aSAl Viro dentry = atomic_open(nd, dentry, file, open_flag, mode); 3351da5ebf5aSAl Viro if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) 3352da5ebf5aSAl Viro dentry = ERR_PTR(create_error); 3353da5ebf5aSAl Viro return dentry; 3354239eb983SAl Viro } 335554ef4872SMiklos Szeredi 33566fbd0714SAl Viro if (d_in_lookup(dentry)) { 335712fa5e24SAl Viro struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, 335812fa5e24SAl Viro nd->flags); 33596fbd0714SAl Viro d_lookup_done(dentry); 336012fa5e24SAl Viro if (unlikely(res)) { 336112fa5e24SAl Viro if (IS_ERR(res)) { 336212fa5e24SAl Viro error = PTR_ERR(res); 336312fa5e24SAl Viro goto out_dput; 336412fa5e24SAl Viro } 336512fa5e24SAl Viro dput(dentry); 336612fa5e24SAl Viro dentry = res; 336712fa5e24SAl Viro } 336854ef4872SMiklos Szeredi } 336954ef4872SMiklos Szeredi 3370d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 33711643b43fSAl Viro if (!dentry->d_inode && (open_flag & O_CREAT)) { 337273a09dd9SAl Viro file->f_mode |= FMODE_CREATED; 3373ce8644fcSAl Viro audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); 3374ce8644fcSAl Viro if (!dir_inode->i_op->create) { 3375ce8644fcSAl Viro error = -EACCES; 3376d58ffd35SMiklos Szeredi goto out_dput; 337764894cf8SAl Viro } 3378549c7297SChristian Brauner 3379549c7297SChristian Brauner error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry, 3380549c7297SChristian Brauner mode, open_flag & O_EXCL); 3381d58ffd35SMiklos Szeredi if (error) 3382d58ffd35SMiklos Szeredi goto out_dput; 3383d58ffd35SMiklos Szeredi } 33841643b43fSAl Viro if (unlikely(create_error) && !dentry->d_inode) { 33851643b43fSAl Viro error = create_error; 3386d58ffd35SMiklos Szeredi goto out_dput; 3387d58ffd35SMiklos Szeredi } 3388da5ebf5aSAl Viro return dentry; 3389d58ffd35SMiklos Szeredi 3390d58ffd35SMiklos Szeredi out_dput: 3391d58ffd35SMiklos Szeredi dput(dentry); 3392da5ebf5aSAl Viro return ERR_PTR(error); 3393d58ffd35SMiklos Szeredi } 3394d58ffd35SMiklos Szeredi 3395c981a482SAl Viro static const char *open_last_lookups(struct nameidata *nd, 33963ec2eef1SAl Viro struct file *file, const struct open_flags *op) 3397fb1cc555SAl Viro { 3398a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 3399ca344a89SAl Viro int open_flag = op->open_flag; 340064894cf8SAl Viro bool got_write = false; 3401254cf582SAl Viro unsigned seq; 3402a1eb3315SMiklos Szeredi struct inode *inode; 3403da5ebf5aSAl Viro struct dentry *dentry; 3404b0417d2cSAl Viro const char *res; 3405fb1cc555SAl Viro 3406c3e380b0SAl Viro nd->flags |= op->intent; 3407c3e380b0SAl Viro 3408bc77daa7SAl Viro if (nd->last_type != LAST_NORM) { 340956676ec3SAl Viro if (nd->depth) 341056676ec3SAl Viro put_link(nd); 3411ff326a32SAl Viro return handle_dots(nd, nd->last_type); 34121f36f774SAl Viro } 3413a2c36b45SAl Viro 3414ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 3415fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 3416fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 3417fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 341820e34357SAl Viro dentry = lookup_fast(nd, &inode, &seq); 341920e34357SAl Viro if (IS_ERR(dentry)) 34201ccac622SAl Viro return ERR_CAST(dentry); 342120e34357SAl Viro if (likely(dentry)) 342271574865SMiklos Szeredi goto finish_lookup; 342371574865SMiklos Szeredi 34246583fe22SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 3425b6183df7SMiklos Szeredi } else { 3426fe2d35ffSAl Viro /* create side of things */ 342772287417SAl Viro if (nd->flags & LOOKUP_RCU) { 3428e36cffedSJens Axboe if (!try_to_unlazy(nd)) 3429e36cffedSJens Axboe return ERR_PTR(-ECHILD); 343072287417SAl Viro } 3431c9b07eabSAl Viro audit_inode(nd->name, dir, AUDIT_INODE_PARENT); 34321f36f774SAl Viro /* trailing slashes? */ 3433deb106c6SAl Viro if (unlikely(nd->last.name[nd->last.len])) 34341ccac622SAl Viro return ERR_PTR(-EISDIR); 3435b6183df7SMiklos Szeredi } 34361f36f774SAl Viro 34379cf843e3SAl Viro if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 3438e36cffedSJens Axboe got_write = !mnt_want_write(nd->path.mnt); 343964894cf8SAl Viro /* 344064894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 344164894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 344264894cf8SAl Viro * dropping this one anyway. 344364894cf8SAl Viro */ 344464894cf8SAl Viro } 34459cf843e3SAl Viro if (open_flag & O_CREAT) 34465955102cSAl Viro inode_lock(dir->d_inode); 34479cf843e3SAl Viro else 34489cf843e3SAl Viro inode_lock_shared(dir->d_inode); 3449da5ebf5aSAl Viro dentry = lookup_open(nd, file, op, got_write); 3450f7bb959dSAl Viro if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) 3451f7bb959dSAl Viro fsnotify_create(dir->d_inode, dentry); 34529cf843e3SAl Viro if (open_flag & O_CREAT) 34535955102cSAl Viro inode_unlock(dir->d_inode); 34549cf843e3SAl Viro else 34559cf843e3SAl Viro inode_unlock_shared(dir->d_inode); 3456fb1cc555SAl Viro 3457c981a482SAl Viro if (got_write) 345859e96e65SAl Viro mnt_drop_write(nd->path.mnt); 34596c0d46c4SAl Viro 346059e96e65SAl Viro if (IS_ERR(dentry)) 346159e96e65SAl Viro return ERR_CAST(dentry); 346259e96e65SAl Viro 3463973d4b73SAl Viro if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { 3464e73cabffSAl Viro dput(nd->path.dentry); 3465e73cabffSAl Viro nd->path.dentry = dentry; 3466c981a482SAl Viro return NULL; 3467fb1cc555SAl Viro } 3468fb1cc555SAl Viro 346920e34357SAl Viro finish_lookup: 347056676ec3SAl Viro if (nd->depth) 347156676ec3SAl Viro put_link(nd); 34728c4efe22SAl Viro res = step_into(nd, WALK_TRAILING, dentry, inode, seq); 3473ff326a32SAl Viro if (unlikely(res)) 34741ccac622SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3475b0417d2cSAl Viro return res; 34761ccac622SAl Viro } 347731d1726dSAl Viro 3478c981a482SAl Viro /* 3479c981a482SAl Viro * Handle the last step of open() 3480c981a482SAl Viro */ 3481c5971b8cSAl Viro static int do_open(struct nameidata *nd, 3482c981a482SAl Viro struct file *file, const struct open_flags *op) 3483c981a482SAl Viro { 3484549c7297SChristian Brauner struct user_namespace *mnt_userns; 3485c981a482SAl Viro int open_flag = op->open_flag; 3486c981a482SAl Viro bool do_truncate; 3487c981a482SAl Viro int acc_mode; 3488c981a482SAl Viro int error; 3489c981a482SAl Viro 3490ff326a32SAl Viro if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { 3491ff326a32SAl Viro error = complete_walk(nd); 3492ff326a32SAl Viro if (error) 3493ff326a32SAl Viro return error; 3494ff326a32SAl Viro } 3495973d4b73SAl Viro if (!(file->f_mode & FMODE_CREATED)) 349676ae2a5aSAl Viro audit_inode(nd->name, nd->path.dentry, 0); 3497549c7297SChristian Brauner mnt_userns = mnt_user_ns(nd->path.mnt); 349830aba665SSalvatore Mesoraca if (open_flag & O_CREAT) { 3499b94e0b32SAl Viro if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) 3500b94e0b32SAl Viro return -EEXIST; 350130aba665SSalvatore Mesoraca if (d_is_dir(nd->path.dentry)) 3502c5971b8cSAl Viro return -EISDIR; 3503549c7297SChristian Brauner error = may_create_in_sticky(mnt_userns, nd, 350430aba665SSalvatore Mesoraca d_backing_inode(nd->path.dentry)); 350530aba665SSalvatore Mesoraca if (unlikely(error)) 3506c5971b8cSAl Viro return error; 350730aba665SSalvatore Mesoraca } 350844b1d530SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) 3509c5971b8cSAl Viro return -ENOTDIR; 35106c0d46c4SAl Viro 35118795e7d4SAl Viro do_truncate = false; 35128795e7d4SAl Viro acc_mode = op->acc_mode; 35135a2d3eddSAl Viro if (file->f_mode & FMODE_CREATED) { 35145a2d3eddSAl Viro /* Don't check for write permission, don't truncate */ 35155a2d3eddSAl Viro open_flag &= ~O_TRUNC; 35165a2d3eddSAl Viro acc_mode = 0; 35178795e7d4SAl Viro } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { 35180f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 35190f9d1a10SAl Viro if (error) 3520c5971b8cSAl Viro return error; 35218795e7d4SAl Viro do_truncate = true; 35220f9d1a10SAl Viro } 3523549c7297SChristian Brauner error = may_open(mnt_userns, &nd->path, acc_mode, open_flag); 35248795e7d4SAl Viro if (!error && !(file->f_mode & FMODE_OPENED)) 3525ae2bb293SAl Viro error = vfs_open(&nd->path, file); 35268795e7d4SAl Viro if (!error) 35276035a27bSAl Viro error = ima_file_check(file, op->acc_mode); 35288795e7d4SAl Viro if (!error && do_truncate) 3529549c7297SChristian Brauner error = handle_truncate(mnt_userns, file); 3530c80567c8SAl Viro if (unlikely(error > 0)) { 3531c80567c8SAl Viro WARN_ON(1); 3532c80567c8SAl Viro error = -EINVAL; 3533c80567c8SAl Viro } 35348795e7d4SAl Viro if (do_truncate) 35350f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 3536c5971b8cSAl Viro return error; 3537fb1cc555SAl Viro } 3538fb1cc555SAl Viro 35396521f891SChristian Brauner /** 35406521f891SChristian Brauner * vfs_tmpfile - create tmpfile 35416521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 35426521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 35436521f891SChristian Brauner * @mode: mode of the new tmpfile 35442111c3c0SRandy Dunlap * @open_flag: flags 35456521f891SChristian Brauner * 35466521f891SChristian Brauner * Create a temporary file. 35476521f891SChristian Brauner * 35486521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 35496521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 35506521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 35516521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 35526521f891SChristian Brauner * raw inode simply passs init_user_ns. 35536521f891SChristian Brauner */ 35546521f891SChristian Brauner struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns, 35556521f891SChristian Brauner struct dentry *dentry, umode_t mode, int open_flag) 3556af7bd4dcSAmir Goldstein { 3557af7bd4dcSAmir Goldstein struct dentry *child = NULL; 3558af7bd4dcSAmir Goldstein struct inode *dir = dentry->d_inode; 3559af7bd4dcSAmir Goldstein struct inode *inode; 3560af7bd4dcSAmir Goldstein int error; 3561af7bd4dcSAmir Goldstein 3562af7bd4dcSAmir Goldstein /* we want directory to be writable */ 35636521f891SChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 3564af7bd4dcSAmir Goldstein if (error) 3565af7bd4dcSAmir Goldstein goto out_err; 3566af7bd4dcSAmir Goldstein error = -EOPNOTSUPP; 3567af7bd4dcSAmir Goldstein if (!dir->i_op->tmpfile) 3568af7bd4dcSAmir Goldstein goto out_err; 3569af7bd4dcSAmir Goldstein error = -ENOMEM; 3570cdf01226SDavid Howells child = d_alloc(dentry, &slash_name); 3571af7bd4dcSAmir Goldstein if (unlikely(!child)) 3572af7bd4dcSAmir Goldstein goto out_err; 3573549c7297SChristian Brauner error = dir->i_op->tmpfile(mnt_userns, dir, child, mode); 3574af7bd4dcSAmir Goldstein if (error) 3575af7bd4dcSAmir Goldstein goto out_err; 3576af7bd4dcSAmir Goldstein error = -ENOENT; 3577af7bd4dcSAmir Goldstein inode = child->d_inode; 3578af7bd4dcSAmir Goldstein if (unlikely(!inode)) 3579af7bd4dcSAmir Goldstein goto out_err; 3580af7bd4dcSAmir Goldstein if (!(open_flag & O_EXCL)) { 3581af7bd4dcSAmir Goldstein spin_lock(&inode->i_lock); 3582af7bd4dcSAmir Goldstein inode->i_state |= I_LINKABLE; 3583af7bd4dcSAmir Goldstein spin_unlock(&inode->i_lock); 3584af7bd4dcSAmir Goldstein } 3585a2d2329eSChristian Brauner ima_post_create_tmpfile(mnt_userns, inode); 3586af7bd4dcSAmir Goldstein return child; 3587af7bd4dcSAmir Goldstein 3588af7bd4dcSAmir Goldstein out_err: 3589af7bd4dcSAmir Goldstein dput(child); 3590af7bd4dcSAmir Goldstein return ERR_PTR(error); 3591af7bd4dcSAmir Goldstein } 3592af7bd4dcSAmir Goldstein EXPORT_SYMBOL(vfs_tmpfile); 3593af7bd4dcSAmir Goldstein 3594c8a53ee5SAl Viro static int do_tmpfile(struct nameidata *nd, unsigned flags, 359560545d0dSAl Viro const struct open_flags *op, 35963ec2eef1SAl Viro struct file *file) 359760545d0dSAl Viro { 35986521f891SChristian Brauner struct user_namespace *mnt_userns; 3599625b6d10SAl Viro struct dentry *child; 3600625b6d10SAl Viro struct path path; 3601c8a53ee5SAl Viro int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); 360260545d0dSAl Viro if (unlikely(error)) 360360545d0dSAl Viro return error; 3604625b6d10SAl Viro error = mnt_want_write(path.mnt); 360560545d0dSAl Viro if (unlikely(error)) 360660545d0dSAl Viro goto out; 36076521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 36086521f891SChristian Brauner child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag); 3609af7bd4dcSAmir Goldstein error = PTR_ERR(child); 3610684e73beSHirofumi Nakagawa if (IS_ERR(child)) 361160545d0dSAl Viro goto out2; 3612625b6d10SAl Viro dput(path.dentry); 3613625b6d10SAl Viro path.dentry = child; 3614c8a53ee5SAl Viro audit_inode(nd->name, child, 0); 361569a91c23SEric Rannaud /* Don't check for other permissions, the inode was just created */ 3616549c7297SChristian Brauner error = may_open(mnt_userns, &path, 0, op->open_flag); 36171e8f44f1SAl Viro if (!error) 36181e8f44f1SAl Viro error = vfs_open(&path, file); 361960545d0dSAl Viro out2: 3620625b6d10SAl Viro mnt_drop_write(path.mnt); 362160545d0dSAl Viro out: 3622625b6d10SAl Viro path_put(&path); 362360545d0dSAl Viro return error; 362460545d0dSAl Viro } 362560545d0dSAl Viro 36266ac08709SAl Viro static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) 36276ac08709SAl Viro { 36286ac08709SAl Viro struct path path; 36296ac08709SAl Viro int error = path_lookupat(nd, flags, &path); 36306ac08709SAl Viro if (!error) { 36316ac08709SAl Viro audit_inode(nd->name, path.dentry, 0); 3632ae2bb293SAl Viro error = vfs_open(&path, file); 36336ac08709SAl Viro path_put(&path); 36346ac08709SAl Viro } 36356ac08709SAl Viro return error; 36366ac08709SAl Viro } 36376ac08709SAl Viro 3638c8a53ee5SAl Viro static struct file *path_openat(struct nameidata *nd, 3639c8a53ee5SAl Viro const struct open_flags *op, unsigned flags) 36401da177e4SLinus Torvalds { 364130d90494SAl Viro struct file *file; 364213aab428SAl Viro int error; 364331e6b01fSNick Piggin 3644ea73ea72SAl Viro file = alloc_empty_file(op->open_flag, current_cred()); 36451afc99beSAl Viro if (IS_ERR(file)) 36461afc99beSAl Viro return file; 364731e6b01fSNick Piggin 3648bb458c64SAl Viro if (unlikely(file->f_flags & __O_TMPFILE)) { 36493ec2eef1SAl Viro error = do_tmpfile(nd, flags, op, file); 36505f336e72SAl Viro } else if (unlikely(file->f_flags & O_PATH)) { 36516ac08709SAl Viro error = do_o_path(nd, flags, file); 36525f336e72SAl Viro } else { 36535f336e72SAl Viro const char *s = path_init(nd, flags); 36543bdba28bSAl Viro while (!(error = link_path_walk(s, nd)) && 3655c5971b8cSAl Viro (s = open_last_lookups(nd, file, op)) != NULL) 36561ccac622SAl Viro ; 3657c5971b8cSAl Viro if (!error) 3658c5971b8cSAl Viro error = do_open(nd, file, op); 3659deb106c6SAl Viro terminate_walk(nd); 36605f336e72SAl Viro } 36617c1c01ecSAl Viro if (likely(!error)) { 3662aad888f8SAl Viro if (likely(file->f_mode & FMODE_OPENED)) 36637c1c01ecSAl Viro return file; 36647c1c01ecSAl Viro WARN_ON(1); 36657c1c01ecSAl Viro error = -EINVAL; 3666015c3bbcSMiklos Szeredi } 36677c1c01ecSAl Viro fput(file); 36682675a4ebSAl Viro if (error == -EOPENSTALE) { 36692675a4ebSAl Viro if (flags & LOOKUP_RCU) 36702675a4ebSAl Viro error = -ECHILD; 36712675a4ebSAl Viro else 36722675a4ebSAl Viro error = -ESTALE; 36732675a4ebSAl Viro } 36747c1c01ecSAl Viro return ERR_PTR(error); 3675de459215SKirill Korotaev } 36761da177e4SLinus Torvalds 3677669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 3678f9652e10SAl Viro const struct open_flags *op) 367913aab428SAl Viro { 36809883d185SAl Viro struct nameidata nd; 3681f9652e10SAl Viro int flags = op->lookup_flags; 368213aab428SAl Viro struct file *filp; 368313aab428SAl Viro 368406422964SAl Viro set_nameidata(&nd, dfd, pathname, NULL); 3685c8a53ee5SAl Viro filp = path_openat(&nd, op, flags | LOOKUP_RCU); 368613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 3687c8a53ee5SAl Viro filp = path_openat(&nd, op, flags); 368813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 3689c8a53ee5SAl Viro filp = path_openat(&nd, op, flags | LOOKUP_REVAL); 36909883d185SAl Viro restore_nameidata(); 369113aab428SAl Viro return filp; 369213aab428SAl Viro } 369313aab428SAl Viro 3694ffb37ca3SAl Viro struct file *do_file_open_root(const struct path *root, 3695f9652e10SAl Viro const char *name, const struct open_flags *op) 369673d049a4SAl Viro { 36979883d185SAl Viro struct nameidata nd; 369873d049a4SAl Viro struct file *file; 369951689104SPaul Moore struct filename *filename; 3700bcba1e7dSAl Viro int flags = op->lookup_flags; 370173d049a4SAl Viro 3702ffb37ca3SAl Viro if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) 370373d049a4SAl Viro return ERR_PTR(-ELOOP); 370473d049a4SAl Viro 370551689104SPaul Moore filename = getname_kernel(name); 3706a1c83681SViresh Kumar if (IS_ERR(filename)) 370751689104SPaul Moore return ERR_CAST(filename); 370851689104SPaul Moore 370906422964SAl Viro set_nameidata(&nd, -1, filename, root); 3710c8a53ee5SAl Viro file = path_openat(&nd, op, flags | LOOKUP_RCU); 371173d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3712c8a53ee5SAl Viro file = path_openat(&nd, op, flags); 371373d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3714c8a53ee5SAl Viro file = path_openat(&nd, op, flags | LOOKUP_REVAL); 37159883d185SAl Viro restore_nameidata(); 371651689104SPaul Moore putname(filename); 371773d049a4SAl Viro return file; 371873d049a4SAl Viro } 371973d049a4SAl Viro 3720b4a4f213SStephen Brennan static struct dentry *filename_create(int dfd, struct filename *name, 37211ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 37221da177e4SLinus Torvalds { 3723c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3724391172c4SAl Viro struct qstr last; 3725b3d4650dSNeilBrown bool want_dir = lookup_flags & LOOKUP_DIRECTORY; 3726b3d4650dSNeilBrown unsigned int reval_flag = lookup_flags & LOOKUP_REVAL; 3727b3d4650dSNeilBrown unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL; 3728391172c4SAl Viro int type; 3729c30dabfeSJan Kara int err2; 37301ac12b4bSJeff Layton int error; 37311ac12b4bSJeff Layton 3732b3d4650dSNeilBrown error = filename_parentat(dfd, name, reval_flag, path, &last, &type); 37330ee50b47SDmitry Kadashev if (error) 37340ee50b47SDmitry Kadashev return ERR_PTR(error); 37351da177e4SLinus Torvalds 3736c663e5d8SChristoph Hellwig /* 3737c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3738c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3739c663e5d8SChristoph Hellwig */ 37405c31b6ceSAl Viro if (unlikely(type != LAST_NORM)) 3741ed75e95dSAl Viro goto out; 3742c663e5d8SChristoph Hellwig 3743c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3744391172c4SAl Viro err2 = mnt_want_write(path->mnt); 3745c663e5d8SChristoph Hellwig /* 3746b3d4650dSNeilBrown * Do the final lookup. Suppress 'create' if there is a trailing 3747b3d4650dSNeilBrown * '/', and a directory wasn't requested. 3748c663e5d8SChristoph Hellwig */ 3749b3d4650dSNeilBrown if (last.name[last.len] && !want_dir) 3750b3d4650dSNeilBrown create_flags = 0; 37515955102cSAl Viro inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); 3752b3d4650dSNeilBrown dentry = __lookup_hash(&last, path->dentry, reval_flag | create_flags); 37531da177e4SLinus Torvalds if (IS_ERR(dentry)) 3754a8104a9fSAl Viro goto unlock; 3755c663e5d8SChristoph Hellwig 3756a8104a9fSAl Viro error = -EEXIST; 3757b18825a7SDavid Howells if (d_is_positive(dentry)) 3758a8104a9fSAl Viro goto fail; 3759b18825a7SDavid Howells 3760c663e5d8SChristoph Hellwig /* 3761c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3762c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3763c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3764c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3765c663e5d8SChristoph Hellwig */ 3766b3d4650dSNeilBrown if (unlikely(!create_flags)) { 3767a8104a9fSAl Viro error = -ENOENT; 3768ed75e95dSAl Viro goto fail; 3769e9baf6e5SAl Viro } 3770c30dabfeSJan Kara if (unlikely(err2)) { 3771c30dabfeSJan Kara error = err2; 3772a8104a9fSAl Viro goto fail; 3773c30dabfeSJan Kara } 3774e9baf6e5SAl Viro return dentry; 37751da177e4SLinus Torvalds fail: 3776a8104a9fSAl Viro dput(dentry); 3777a8104a9fSAl Viro dentry = ERR_PTR(error); 3778a8104a9fSAl Viro unlock: 37795955102cSAl Viro inode_unlock(path->dentry->d_inode); 3780c30dabfeSJan Kara if (!err2) 3781391172c4SAl Viro mnt_drop_write(path->mnt); 3782ed75e95dSAl Viro out: 3783391172c4SAl Viro path_put(path); 3784ed75e95dSAl Viro return dentry; 3785dae6ad8fSAl Viro } 3786fa14a0b8SAl Viro 3787fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, 3788fa14a0b8SAl Viro struct path *path, unsigned int lookup_flags) 3789fa14a0b8SAl Viro { 3790b4a4f213SStephen Brennan struct filename *filename = getname_kernel(pathname); 3791b4a4f213SStephen Brennan struct dentry *res = filename_create(dfd, filename, path, lookup_flags); 3792b4a4f213SStephen Brennan 3793b4a4f213SStephen Brennan putname(filename); 3794b4a4f213SStephen Brennan return res; 3795fa14a0b8SAl Viro } 3796dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3797dae6ad8fSAl Viro 3798921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3799921a1650SAl Viro { 3800921a1650SAl Viro dput(dentry); 38015955102cSAl Viro inode_unlock(path->dentry->d_inode); 3802a8104a9fSAl Viro mnt_drop_write(path->mnt); 3803921a1650SAl Viro path_put(path); 3804921a1650SAl Viro } 3805921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3806921a1650SAl Viro 3807520ae687SAl Viro inline struct dentry *user_path_create(int dfd, const char __user *pathname, 38081ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3809dae6ad8fSAl Viro { 3810b4a4f213SStephen Brennan struct filename *filename = getname(pathname); 3811b4a4f213SStephen Brennan struct dentry *res = filename_create(dfd, filename, path, lookup_flags); 3812b4a4f213SStephen Brennan 3813b4a4f213SStephen Brennan putname(filename); 3814b4a4f213SStephen Brennan return res; 3815dae6ad8fSAl Viro } 3816dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3817dae6ad8fSAl Viro 38186521f891SChristian Brauner /** 38196521f891SChristian Brauner * vfs_mknod - create device node or file 38206521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 38216521f891SChristian Brauner * @dir: inode of @dentry 38226521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 38236521f891SChristian Brauner * @mode: mode of the new device node or file 38246521f891SChristian Brauner * @dev: device number of device to create 38256521f891SChristian Brauner * 38266521f891SChristian Brauner * Create a device node or file. 38276521f891SChristian Brauner * 38286521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 38296521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 38306521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 38316521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 38326521f891SChristian Brauner * raw inode simply passs init_user_ns. 38336521f891SChristian Brauner */ 38346521f891SChristian Brauner int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 38356521f891SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 38361da177e4SLinus Torvalds { 3837a3c751a5SMiklos Szeredi bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; 38386521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 38391da177e4SLinus Torvalds 38401da177e4SLinus Torvalds if (error) 38411da177e4SLinus Torvalds return error; 38421da177e4SLinus Torvalds 3843a3c751a5SMiklos Szeredi if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && 3844a3c751a5SMiklos Szeredi !capable(CAP_MKNOD)) 38451da177e4SLinus Torvalds return -EPERM; 38461da177e4SLinus Torvalds 3847acfa4380SAl Viro if (!dir->i_op->mknod) 38481da177e4SLinus Torvalds return -EPERM; 38491da177e4SLinus Torvalds 385008ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 385108ce5f16SSerge E. Hallyn if (error) 385208ce5f16SSerge E. Hallyn return error; 385308ce5f16SSerge E. Hallyn 38541da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 38551da177e4SLinus Torvalds if (error) 38561da177e4SLinus Torvalds return error; 38571da177e4SLinus Torvalds 3858549c7297SChristian Brauner error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev); 3859a74574aaSStephen Smalley if (!error) 3860f38aa942SAmy Griffis fsnotify_create(dir, dentry); 38611da177e4SLinus Torvalds return error; 38621da177e4SLinus Torvalds } 38634d359507SAl Viro EXPORT_SYMBOL(vfs_mknod); 38641da177e4SLinus Torvalds 3865f69aac00SAl Viro static int may_mknod(umode_t mode) 3866463c3197SDave Hansen { 3867463c3197SDave Hansen switch (mode & S_IFMT) { 3868463c3197SDave Hansen case S_IFREG: 3869463c3197SDave Hansen case S_IFCHR: 3870463c3197SDave Hansen case S_IFBLK: 3871463c3197SDave Hansen case S_IFIFO: 3872463c3197SDave Hansen case S_IFSOCK: 3873463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3874463c3197SDave Hansen return 0; 3875463c3197SDave Hansen case S_IFDIR: 3876463c3197SDave Hansen return -EPERM; 3877463c3197SDave Hansen default: 3878463c3197SDave Hansen return -EINVAL; 3879463c3197SDave Hansen } 3880463c3197SDave Hansen } 3881463c3197SDave Hansen 388245f30dabSDmitry Kadashev static int do_mknodat(int dfd, struct filename *name, umode_t mode, 388387c4e192SDominik Brodowski unsigned int dev) 38841da177e4SLinus Torvalds { 38856521f891SChristian Brauner struct user_namespace *mnt_userns; 38861da177e4SLinus Torvalds struct dentry *dentry; 3887dae6ad8fSAl Viro struct path path; 3888dae6ad8fSAl Viro int error; 3889972567f1SJeff Layton unsigned int lookup_flags = 0; 38901da177e4SLinus Torvalds 38918e4bfca1SAl Viro error = may_mknod(mode); 38928e4bfca1SAl Viro if (error) 38937797251bSDmitry Kadashev goto out1; 3894972567f1SJeff Layton retry: 3895b4a4f213SStephen Brennan dentry = filename_create(dfd, name, &path, lookup_flags); 38967797251bSDmitry Kadashev error = PTR_ERR(dentry); 3897dae6ad8fSAl Viro if (IS_ERR(dentry)) 38987797251bSDmitry Kadashev goto out1; 38992ad94ae6SAl Viro 3900dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3901ce3b0f8dSAl Viro mode &= ~current_umask(); 3902dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3903be6d3e56SKentaro Takeda if (error) 39047797251bSDmitry Kadashev goto out2; 39056521f891SChristian Brauner 39066521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 39071da177e4SLinus Torvalds switch (mode & S_IFMT) { 39081da177e4SLinus Torvalds case 0: case S_IFREG: 39096521f891SChristian Brauner error = vfs_create(mnt_userns, path.dentry->d_inode, 39106521f891SChristian Brauner dentry, mode, true); 391105d1a717SMimi Zohar if (!error) 3912a2d2329eSChristian Brauner ima_post_path_mknod(mnt_userns, dentry); 39131da177e4SLinus Torvalds break; 39141da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 39156521f891SChristian Brauner error = vfs_mknod(mnt_userns, path.dentry->d_inode, 39166521f891SChristian Brauner dentry, mode, new_decode_dev(dev)); 39171da177e4SLinus Torvalds break; 39181da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 39196521f891SChristian Brauner error = vfs_mknod(mnt_userns, path.dentry->d_inode, 39206521f891SChristian Brauner dentry, mode, 0); 39211da177e4SLinus Torvalds break; 39221da177e4SLinus Torvalds } 39237797251bSDmitry Kadashev out2: 3924921a1650SAl Viro done_path_create(&path, dentry); 3925972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3926972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3927972567f1SJeff Layton goto retry; 3928972567f1SJeff Layton } 39297797251bSDmitry Kadashev out1: 39307797251bSDmitry Kadashev putname(name); 39311da177e4SLinus Torvalds return error; 39321da177e4SLinus Torvalds } 39331da177e4SLinus Torvalds 393487c4e192SDominik Brodowski SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 393587c4e192SDominik Brodowski unsigned int, dev) 393687c4e192SDominik Brodowski { 39377797251bSDmitry Kadashev return do_mknodat(dfd, getname(filename), mode, dev); 393887c4e192SDominik Brodowski } 393987c4e192SDominik Brodowski 39408208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 39415590ff0dSUlrich Drepper { 39427797251bSDmitry Kadashev return do_mknodat(AT_FDCWD, getname(filename), mode, dev); 39435590ff0dSUlrich Drepper } 39445590ff0dSUlrich Drepper 39456521f891SChristian Brauner /** 39466521f891SChristian Brauner * vfs_mkdir - create directory 39476521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 39486521f891SChristian Brauner * @dir: inode of @dentry 39496521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 39506521f891SChristian Brauner * @mode: mode of the new directory 39516521f891SChristian Brauner * 39526521f891SChristian Brauner * Create a directory. 39536521f891SChristian Brauner * 39546521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 39556521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 39566521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 39576521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 39586521f891SChristian Brauner * raw inode simply passs init_user_ns. 39596521f891SChristian Brauner */ 39606521f891SChristian Brauner int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 39616521f891SChristian Brauner struct dentry *dentry, umode_t mode) 39621da177e4SLinus Torvalds { 39636521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 39648de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 39651da177e4SLinus Torvalds 39661da177e4SLinus Torvalds if (error) 39671da177e4SLinus Torvalds return error; 39681da177e4SLinus Torvalds 3969acfa4380SAl Viro if (!dir->i_op->mkdir) 39701da177e4SLinus Torvalds return -EPERM; 39711da177e4SLinus Torvalds 39721da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 39731da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 39741da177e4SLinus Torvalds if (error) 39751da177e4SLinus Torvalds return error; 39761da177e4SLinus Torvalds 39778de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 39788de52778SAl Viro return -EMLINK; 39798de52778SAl Viro 3980549c7297SChristian Brauner error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode); 3981a74574aaSStephen Smalley if (!error) 3982f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 39831da177e4SLinus Torvalds return error; 39841da177e4SLinus Torvalds } 39854d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir); 39861da177e4SLinus Torvalds 398745f30dabSDmitry Kadashev int do_mkdirat(int dfd, struct filename *name, umode_t mode) 39881da177e4SLinus Torvalds { 39896902d925SDave Hansen struct dentry *dentry; 3990dae6ad8fSAl Viro struct path path; 3991dae6ad8fSAl Viro int error; 3992b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 39931da177e4SLinus Torvalds 3994b76d8b82SJeff Layton retry: 3995b4a4f213SStephen Brennan dentry = filename_create(dfd, name, &path, lookup_flags); 3996584d3226SDmitry Kadashev error = PTR_ERR(dentry); 39976902d925SDave Hansen if (IS_ERR(dentry)) 3998584d3226SDmitry Kadashev goto out_putname; 39996902d925SDave Hansen 4000dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 4001ce3b0f8dSAl Viro mode &= ~current_umask(); 4002dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 40036521f891SChristian Brauner if (!error) { 40046521f891SChristian Brauner struct user_namespace *mnt_userns; 40056521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 4006549c7297SChristian Brauner error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry, 4007549c7297SChristian Brauner mode); 40086521f891SChristian Brauner } 4009921a1650SAl Viro done_path_create(&path, dentry); 4010b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 4011b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 4012b76d8b82SJeff Layton goto retry; 4013b76d8b82SJeff Layton } 4014584d3226SDmitry Kadashev out_putname: 4015584d3226SDmitry Kadashev putname(name); 40161da177e4SLinus Torvalds return error; 40171da177e4SLinus Torvalds } 40181da177e4SLinus Torvalds 40190101db7aSDominik Brodowski SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 40200101db7aSDominik Brodowski { 4021584d3226SDmitry Kadashev return do_mkdirat(dfd, getname(pathname), mode); 40220101db7aSDominik Brodowski } 40230101db7aSDominik Brodowski 4024a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 40255590ff0dSUlrich Drepper { 4026584d3226SDmitry Kadashev return do_mkdirat(AT_FDCWD, getname(pathname), mode); 40275590ff0dSUlrich Drepper } 40285590ff0dSUlrich Drepper 40296521f891SChristian Brauner /** 40306521f891SChristian Brauner * vfs_rmdir - remove directory 40316521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 40326521f891SChristian Brauner * @dir: inode of @dentry 40336521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 40346521f891SChristian Brauner * 40356521f891SChristian Brauner * Remove a directory. 40366521f891SChristian Brauner * 40376521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 40386521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 40396521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 40406521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 40416521f891SChristian Brauner * raw inode simply passs init_user_ns. 40426521f891SChristian Brauner */ 40436521f891SChristian Brauner int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, 40446521f891SChristian Brauner struct dentry *dentry) 40451da177e4SLinus Torvalds { 40466521f891SChristian Brauner int error = may_delete(mnt_userns, dir, dentry, 1); 40471da177e4SLinus Torvalds 40481da177e4SLinus Torvalds if (error) 40491da177e4SLinus Torvalds return error; 40501da177e4SLinus Torvalds 4051acfa4380SAl Viro if (!dir->i_op->rmdir) 40521da177e4SLinus Torvalds return -EPERM; 40531da177e4SLinus Torvalds 40541d2ef590SAl Viro dget(dentry); 40555955102cSAl Viro inode_lock(dentry->d_inode); 4056912dbc15SSage Weil 40571da177e4SLinus Torvalds error = -EBUSY; 40581bd9c4e4SDavid Howells if (is_local_mountpoint(dentry) || 40591bd9c4e4SDavid Howells (dentry->d_inode->i_flags & S_KERNEL_FILE)) 4060912dbc15SSage Weil goto out; 4061912dbc15SSage Weil 40621da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 4063912dbc15SSage Weil if (error) 4064912dbc15SSage Weil goto out; 4065912dbc15SSage Weil 40661da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 4067912dbc15SSage Weil if (error) 4068912dbc15SSage Weil goto out; 4069912dbc15SSage Weil 40708767712fSAl Viro shrink_dcache_parent(dentry); 40711da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 4072d83c49f3SAl Viro dont_mount(dentry); 40738ed936b5SEric W. Biederman detach_mounts(dentry); 40741da177e4SLinus Torvalds 4075912dbc15SSage Weil out: 40765955102cSAl Viro inode_unlock(dentry->d_inode); 40771d2ef590SAl Viro dput(dentry); 4078912dbc15SSage Weil if (!error) 4079a37d9a17SAmir Goldstein d_delete_notify(dir, dentry); 40801da177e4SLinus Torvalds return error; 40811da177e4SLinus Torvalds } 40824d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir); 40831da177e4SLinus Torvalds 408445f30dabSDmitry Kadashev int do_rmdir(int dfd, struct filename *name) 40851da177e4SLinus Torvalds { 40866521f891SChristian Brauner struct user_namespace *mnt_userns; 40870ee50b47SDmitry Kadashev int error; 40881da177e4SLinus Torvalds struct dentry *dentry; 4089f5beed75SAl Viro struct path path; 4090f5beed75SAl Viro struct qstr last; 4091f5beed75SAl Viro int type; 4092c6ee9206SJeff Layton unsigned int lookup_flags = 0; 4093c6ee9206SJeff Layton retry: 4094c5f563f9SAl Viro error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 40950ee50b47SDmitry Kadashev if (error) 40960ee50b47SDmitry Kadashev goto exit1; 40971da177e4SLinus Torvalds 4098f5beed75SAl Viro switch (type) { 40991da177e4SLinus Torvalds case LAST_DOTDOT: 41001da177e4SLinus Torvalds error = -ENOTEMPTY; 41010ee50b47SDmitry Kadashev goto exit2; 41021da177e4SLinus Torvalds case LAST_DOT: 41031da177e4SLinus Torvalds error = -EINVAL; 41040ee50b47SDmitry Kadashev goto exit2; 41051da177e4SLinus Torvalds case LAST_ROOT: 41061da177e4SLinus Torvalds error = -EBUSY; 41070ee50b47SDmitry Kadashev goto exit2; 41081da177e4SLinus Torvalds } 41090612d9fbSOGAWA Hirofumi 4110f5beed75SAl Viro error = mnt_want_write(path.mnt); 4111c30dabfeSJan Kara if (error) 41120ee50b47SDmitry Kadashev goto exit2; 41130612d9fbSOGAWA Hirofumi 41145955102cSAl Viro inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 4115f5beed75SAl Viro dentry = __lookup_hash(&last, path.dentry, lookup_flags); 41161da177e4SLinus Torvalds error = PTR_ERR(dentry); 41176902d925SDave Hansen if (IS_ERR(dentry)) 41180ee50b47SDmitry Kadashev goto exit3; 4119e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 4120e6bc45d6STheodore Ts'o error = -ENOENT; 41210ee50b47SDmitry Kadashev goto exit4; 4122e6bc45d6STheodore Ts'o } 4123f5beed75SAl Viro error = security_path_rmdir(&path, dentry); 4124be6d3e56SKentaro Takeda if (error) 41250ee50b47SDmitry Kadashev goto exit4; 41266521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 41276521f891SChristian Brauner error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); 41280ee50b47SDmitry Kadashev exit4: 41291da177e4SLinus Torvalds dput(dentry); 41300ee50b47SDmitry Kadashev exit3: 41315955102cSAl Viro inode_unlock(path.dentry->d_inode); 4132f5beed75SAl Viro mnt_drop_write(path.mnt); 41330ee50b47SDmitry Kadashev exit2: 4134f5beed75SAl Viro path_put(&path); 4135c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 4136c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 4137c6ee9206SJeff Layton goto retry; 4138c6ee9206SJeff Layton } 41390ee50b47SDmitry Kadashev exit1: 414024fb33d4SAl Viro putname(name); 41411da177e4SLinus Torvalds return error; 41421da177e4SLinus Torvalds } 41431da177e4SLinus Torvalds 41443cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 41455590ff0dSUlrich Drepper { 4146e24ab0efSChristoph Hellwig return do_rmdir(AT_FDCWD, getname(pathname)); 41475590ff0dSUlrich Drepper } 41485590ff0dSUlrich Drepper 4149b21996e3SJ. Bruce Fields /** 4150b21996e3SJ. Bruce Fields * vfs_unlink - unlink a filesystem object 41516521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 4152b21996e3SJ. Bruce Fields * @dir: parent directory 4153b21996e3SJ. Bruce Fields * @dentry: victim 4154b21996e3SJ. Bruce Fields * @delegated_inode: returns victim inode, if the inode is delegated. 4155b21996e3SJ. Bruce Fields * 4156b21996e3SJ. Bruce Fields * The caller must hold dir->i_mutex. 4157b21996e3SJ. Bruce Fields * 4158b21996e3SJ. Bruce Fields * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and 4159b21996e3SJ. Bruce Fields * return a reference to the inode in delegated_inode. The caller 4160b21996e3SJ. Bruce Fields * should then break the delegation on that inode and retry. Because 4161b21996e3SJ. Bruce Fields * breaking a delegation may take a long time, the caller should drop 4162b21996e3SJ. Bruce Fields * dir->i_mutex before doing so. 4163b21996e3SJ. Bruce Fields * 4164b21996e3SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 4165b21996e3SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 4166b21996e3SJ. Bruce Fields * to be NFS exported. 41676521f891SChristian Brauner * 41686521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 41696521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 41706521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 41716521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 41726521f891SChristian Brauner * raw inode simply passs init_user_ns. 4173b21996e3SJ. Bruce Fields */ 41746521f891SChristian Brauner int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir, 41756521f891SChristian Brauner struct dentry *dentry, struct inode **delegated_inode) 41761da177e4SLinus Torvalds { 41779accbb97SJ. Bruce Fields struct inode *target = dentry->d_inode; 41786521f891SChristian Brauner int error = may_delete(mnt_userns, dir, dentry, 0); 41791da177e4SLinus Torvalds 41801da177e4SLinus Torvalds if (error) 41811da177e4SLinus Torvalds return error; 41821da177e4SLinus Torvalds 4183acfa4380SAl Viro if (!dir->i_op->unlink) 41841da177e4SLinus Torvalds return -EPERM; 41851da177e4SLinus Torvalds 41865955102cSAl Viro inode_lock(target); 418751cc3a66SHugh Dickins if (IS_SWAPFILE(target)) 418851cc3a66SHugh Dickins error = -EPERM; 418951cc3a66SHugh Dickins else if (is_local_mountpoint(dentry)) 41901da177e4SLinus Torvalds error = -EBUSY; 41911da177e4SLinus Torvalds else { 41921da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 4193bec1052eSAl Viro if (!error) { 41945a14696cSJ. Bruce Fields error = try_break_deleg(target, delegated_inode); 41955a14696cSJ. Bruce Fields if (error) 4196b21996e3SJ. Bruce Fields goto out; 41971da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 41988ed936b5SEric W. Biederman if (!error) { 4199d83c49f3SAl Viro dont_mount(dentry); 42008ed936b5SEric W. Biederman detach_mounts(dentry); 42018ed936b5SEric W. Biederman } 4202bec1052eSAl Viro } 42031da177e4SLinus Torvalds } 4204b21996e3SJ. Bruce Fields out: 42055955102cSAl Viro inode_unlock(target); 42061da177e4SLinus Torvalds 42071da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 4208a37d9a17SAmir Goldstein if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { 4209a37d9a17SAmir Goldstein fsnotify_unlink(dir, dentry); 4210a37d9a17SAmir Goldstein } else if (!error) { 42119accbb97SJ. Bruce Fields fsnotify_link_count(target); 4212a37d9a17SAmir Goldstein d_delete_notify(dir, dentry); 42131da177e4SLinus Torvalds } 42140eeca283SRobert Love 42151da177e4SLinus Torvalds return error; 42161da177e4SLinus Torvalds } 42174d359507SAl Viro EXPORT_SYMBOL(vfs_unlink); 42181da177e4SLinus Torvalds 42191da177e4SLinus Torvalds /* 42201da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 42211b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 42221da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 42231da177e4SLinus Torvalds * while waiting on the I/O. 42241da177e4SLinus Torvalds */ 422545f30dabSDmitry Kadashev int do_unlinkat(int dfd, struct filename *name) 42261da177e4SLinus Torvalds { 42272ad94ae6SAl Viro int error; 42281da177e4SLinus Torvalds struct dentry *dentry; 4229f5beed75SAl Viro struct path path; 4230f5beed75SAl Viro struct qstr last; 4231f5beed75SAl Viro int type; 42321da177e4SLinus Torvalds struct inode *inode = NULL; 4233b21996e3SJ. Bruce Fields struct inode *delegated_inode = NULL; 42345d18f813SJeff Layton unsigned int lookup_flags = 0; 42355d18f813SJeff Layton retry: 4236c5f563f9SAl Viro error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); 42370ee50b47SDmitry Kadashev if (error) 42380ee50b47SDmitry Kadashev goto exit1; 42392ad94ae6SAl Viro 42401da177e4SLinus Torvalds error = -EISDIR; 4241f5beed75SAl Viro if (type != LAST_NORM) 42420ee50b47SDmitry Kadashev goto exit2; 42430612d9fbSOGAWA Hirofumi 4244f5beed75SAl Viro error = mnt_want_write(path.mnt); 4245c30dabfeSJan Kara if (error) 42460ee50b47SDmitry Kadashev goto exit2; 4247b21996e3SJ. Bruce Fields retry_deleg: 42485955102cSAl Viro inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); 4249f5beed75SAl Viro dentry = __lookup_hash(&last, path.dentry, lookup_flags); 42501da177e4SLinus Torvalds error = PTR_ERR(dentry); 42511da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 42526521f891SChristian Brauner struct user_namespace *mnt_userns; 42536521f891SChristian Brauner 42541da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 4255f5beed75SAl Viro if (last.name[last.len]) 425650338b88STörök Edwin goto slashes; 42571da177e4SLinus Torvalds inode = dentry->d_inode; 4258b18825a7SDavid Howells if (d_is_negative(dentry)) 4259e6bc45d6STheodore Ts'o goto slashes; 42607de9c6eeSAl Viro ihold(inode); 4261f5beed75SAl Viro error = security_path_unlink(&path, dentry); 4262be6d3e56SKentaro Takeda if (error) 42630ee50b47SDmitry Kadashev goto exit3; 42646521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 4265549c7297SChristian Brauner error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, 4266549c7297SChristian Brauner &delegated_inode); 42670ee50b47SDmitry Kadashev exit3: 42681da177e4SLinus Torvalds dput(dentry); 42691da177e4SLinus Torvalds } 42705955102cSAl Viro inode_unlock(path.dentry->d_inode); 42711da177e4SLinus Torvalds if (inode) 42721da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 4273b21996e3SJ. Bruce Fields inode = NULL; 4274b21996e3SJ. Bruce Fields if (delegated_inode) { 42755a14696cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 4276b21996e3SJ. Bruce Fields if (!error) 4277b21996e3SJ. Bruce Fields goto retry_deleg; 4278b21996e3SJ. Bruce Fields } 4279f5beed75SAl Viro mnt_drop_write(path.mnt); 42800ee50b47SDmitry Kadashev exit2: 4281f5beed75SAl Viro path_put(&path); 42825d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 42835d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 42845d18f813SJeff Layton inode = NULL; 42855d18f813SJeff Layton goto retry; 42865d18f813SJeff Layton } 42870ee50b47SDmitry Kadashev exit1: 4288da2f1362SChristoph Hellwig putname(name); 42891da177e4SLinus Torvalds return error; 42901da177e4SLinus Torvalds 42911da177e4SLinus Torvalds slashes: 4292b18825a7SDavid Howells if (d_is_negative(dentry)) 4293b18825a7SDavid Howells error = -ENOENT; 429444b1d530SMiklos Szeredi else if (d_is_dir(dentry)) 4295b18825a7SDavid Howells error = -EISDIR; 4296b18825a7SDavid Howells else 4297b18825a7SDavid Howells error = -ENOTDIR; 42980ee50b47SDmitry Kadashev goto exit3; 42991da177e4SLinus Torvalds } 43001da177e4SLinus Torvalds 43012e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 43025590ff0dSUlrich Drepper { 43035590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 43045590ff0dSUlrich Drepper return -EINVAL; 43055590ff0dSUlrich Drepper 43065590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 4307e24ab0efSChristoph Hellwig return do_rmdir(dfd, getname(pathname)); 4308da2f1362SChristoph Hellwig return do_unlinkat(dfd, getname(pathname)); 43095590ff0dSUlrich Drepper } 43105590ff0dSUlrich Drepper 43113480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 43125590ff0dSUlrich Drepper { 4313da2f1362SChristoph Hellwig return do_unlinkat(AT_FDCWD, getname(pathname)); 43145590ff0dSUlrich Drepper } 43155590ff0dSUlrich Drepper 43166521f891SChristian Brauner /** 43176521f891SChristian Brauner * vfs_symlink - create symlink 43186521f891SChristian Brauner * @mnt_userns: user namespace of the mount the inode was found from 43196521f891SChristian Brauner * @dir: inode of @dentry 43206521f891SChristian Brauner * @dentry: pointer to dentry of the base directory 43216521f891SChristian Brauner * @oldname: name of the file to link to 43226521f891SChristian Brauner * 43236521f891SChristian Brauner * Create a symlink. 43246521f891SChristian Brauner * 43256521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 43266521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 43276521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 43286521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 43296521f891SChristian Brauner * raw inode simply passs init_user_ns. 43306521f891SChristian Brauner */ 43316521f891SChristian Brauner int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 43326521f891SChristian Brauner struct dentry *dentry, const char *oldname) 43331da177e4SLinus Torvalds { 43346521f891SChristian Brauner int error = may_create(mnt_userns, dir, dentry); 43351da177e4SLinus Torvalds 43361da177e4SLinus Torvalds if (error) 43371da177e4SLinus Torvalds return error; 43381da177e4SLinus Torvalds 4339acfa4380SAl Viro if (!dir->i_op->symlink) 43401da177e4SLinus Torvalds return -EPERM; 43411da177e4SLinus Torvalds 43421da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 43431da177e4SLinus Torvalds if (error) 43441da177e4SLinus Torvalds return error; 43451da177e4SLinus Torvalds 4346549c7297SChristian Brauner error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname); 4347a74574aaSStephen Smalley if (!error) 4348f38aa942SAmy Griffis fsnotify_create(dir, dentry); 43491da177e4SLinus Torvalds return error; 43501da177e4SLinus Torvalds } 43514d359507SAl Viro EXPORT_SYMBOL(vfs_symlink); 43521da177e4SLinus Torvalds 43537a8721f8SDmitry Kadashev int do_symlinkat(struct filename *from, int newdfd, struct filename *to) 43541da177e4SLinus Torvalds { 43552ad94ae6SAl Viro int error; 43566902d925SDave Hansen struct dentry *dentry; 4357dae6ad8fSAl Viro struct path path; 4358f46d3567SJeff Layton unsigned int lookup_flags = 0; 43591da177e4SLinus Torvalds 4360da2d0cedSDmitry Kadashev if (IS_ERR(from)) { 4361da2d0cedSDmitry Kadashev error = PTR_ERR(from); 4362da2d0cedSDmitry Kadashev goto out_putnames; 4363da2d0cedSDmitry Kadashev } 4364f46d3567SJeff Layton retry: 4365b4a4f213SStephen Brennan dentry = filename_create(newdfd, to, &path, lookup_flags); 43661da177e4SLinus Torvalds error = PTR_ERR(dentry); 43676902d925SDave Hansen if (IS_ERR(dentry)) 4368da2d0cedSDmitry Kadashev goto out_putnames; 43696902d925SDave Hansen 437091a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 43716521f891SChristian Brauner if (!error) { 43726521f891SChristian Brauner struct user_namespace *mnt_userns; 43736521f891SChristian Brauner 43746521f891SChristian Brauner mnt_userns = mnt_user_ns(path.mnt); 43756521f891SChristian Brauner error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry, 43766521f891SChristian Brauner from->name); 43776521f891SChristian Brauner } 4378921a1650SAl Viro done_path_create(&path, dentry); 4379f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 4380f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 4381f46d3567SJeff Layton goto retry; 4382f46d3567SJeff Layton } 4383da2d0cedSDmitry Kadashev out_putnames: 4384da2d0cedSDmitry Kadashev putname(to); 43851da177e4SLinus Torvalds putname(from); 43861da177e4SLinus Torvalds return error; 43871da177e4SLinus Torvalds } 43881da177e4SLinus Torvalds 4389b724e846SDominik Brodowski SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 4390b724e846SDominik Brodowski int, newdfd, const char __user *, newname) 4391b724e846SDominik Brodowski { 4392da2d0cedSDmitry Kadashev return do_symlinkat(getname(oldname), newdfd, getname(newname)); 4393b724e846SDominik Brodowski } 4394b724e846SDominik Brodowski 43953480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 43965590ff0dSUlrich Drepper { 4397da2d0cedSDmitry Kadashev return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname)); 43985590ff0dSUlrich Drepper } 43995590ff0dSUlrich Drepper 4400146a8595SJ. Bruce Fields /** 4401146a8595SJ. Bruce Fields * vfs_link - create a new link 4402146a8595SJ. Bruce Fields * @old_dentry: object to be linked 44036521f891SChristian Brauner * @mnt_userns: the user namespace of the mount 4404146a8595SJ. Bruce Fields * @dir: new parent 4405146a8595SJ. Bruce Fields * @new_dentry: where to create the new link 4406146a8595SJ. Bruce Fields * @delegated_inode: returns inode needing a delegation break 4407146a8595SJ. Bruce Fields * 4408146a8595SJ. Bruce Fields * The caller must hold dir->i_mutex 4409146a8595SJ. Bruce Fields * 4410146a8595SJ. Bruce Fields * If vfs_link discovers a delegation on the to-be-linked file in need 4411146a8595SJ. Bruce Fields * of breaking, it will return -EWOULDBLOCK and return a reference to the 4412146a8595SJ. Bruce Fields * inode in delegated_inode. The caller should then break the delegation 4413146a8595SJ. Bruce Fields * and retry. Because breaking a delegation may take a long time, the 4414146a8595SJ. Bruce Fields * caller should drop the i_mutex before doing so. 4415146a8595SJ. Bruce Fields * 4416146a8595SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 4417146a8595SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 4418146a8595SJ. Bruce Fields * to be NFS exported. 44196521f891SChristian Brauner * 44206521f891SChristian Brauner * If the inode has been found through an idmapped mount the user namespace of 44216521f891SChristian Brauner * the vfsmount must be passed through @mnt_userns. This function will then take 44226521f891SChristian Brauner * care to map the inode according to @mnt_userns before checking permissions. 44236521f891SChristian Brauner * On non-idmapped mounts or if permission checking is to be performed on the 44246521f891SChristian Brauner * raw inode simply passs init_user_ns. 4425146a8595SJ. Bruce Fields */ 44266521f891SChristian Brauner int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns, 44276521f891SChristian Brauner struct inode *dir, struct dentry *new_dentry, 44286521f891SChristian Brauner struct inode **delegated_inode) 44291da177e4SLinus Torvalds { 44301da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 44318de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 44321da177e4SLinus Torvalds int error; 44331da177e4SLinus Torvalds 44341da177e4SLinus Torvalds if (!inode) 44351da177e4SLinus Torvalds return -ENOENT; 44361da177e4SLinus Torvalds 44376521f891SChristian Brauner error = may_create(mnt_userns, dir, new_dentry); 44381da177e4SLinus Torvalds if (error) 44391da177e4SLinus Torvalds return error; 44401da177e4SLinus Torvalds 44411da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 44421da177e4SLinus Torvalds return -EXDEV; 44431da177e4SLinus Torvalds 44441da177e4SLinus Torvalds /* 44451da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 44461da177e4SLinus Torvalds */ 44471da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 44481da177e4SLinus Torvalds return -EPERM; 44490bd23d09SEric W. Biederman /* 44500bd23d09SEric W. Biederman * Updating the link count will likely cause i_uid and i_gid to 44510bd23d09SEric W. Biederman * be writen back improperly if their true value is unknown to 44520bd23d09SEric W. Biederman * the vfs. 44530bd23d09SEric W. Biederman */ 44546521f891SChristian Brauner if (HAS_UNMAPPED_ID(mnt_userns, inode)) 44550bd23d09SEric W. Biederman return -EPERM; 4456acfa4380SAl Viro if (!dir->i_op->link) 44571da177e4SLinus Torvalds return -EPERM; 44587e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 44591da177e4SLinus Torvalds return -EPERM; 44601da177e4SLinus Torvalds 44611da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 44621da177e4SLinus Torvalds if (error) 44631da177e4SLinus Torvalds return error; 44641da177e4SLinus Torvalds 44655955102cSAl Viro inode_lock(inode); 4466aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 4467f4e0c30cSAl Viro if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 4468aae8a97dSAneesh Kumar K.V error = -ENOENT; 44698de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 44708de52778SAl Viro error = -EMLINK; 4471146a8595SJ. Bruce Fields else { 4472146a8595SJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 4473146a8595SJ. Bruce Fields if (!error) 44741da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 4475146a8595SJ. Bruce Fields } 4476f4e0c30cSAl Viro 4477f4e0c30cSAl Viro if (!error && (inode->i_state & I_LINKABLE)) { 4478f4e0c30cSAl Viro spin_lock(&inode->i_lock); 4479f4e0c30cSAl Viro inode->i_state &= ~I_LINKABLE; 4480f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 4481f4e0c30cSAl Viro } 44825955102cSAl Viro inode_unlock(inode); 4483e31e14ecSStephen Smalley if (!error) 44847e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 44851da177e4SLinus Torvalds return error; 44861da177e4SLinus Torvalds } 44874d359507SAl Viro EXPORT_SYMBOL(vfs_link); 44881da177e4SLinus Torvalds 44891da177e4SLinus Torvalds /* 44901da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 44911da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 44921da177e4SLinus Torvalds * newname. --KAB 44931da177e4SLinus Torvalds * 44941da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 44951da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 44961da177e4SLinus Torvalds * and other special files. --ADM 44971da177e4SLinus Torvalds */ 4498cf30da90SDmitry Kadashev int do_linkat(int olddfd, struct filename *old, int newdfd, 4499020250f3SDmitry Kadashev struct filename *new, int flags) 45001da177e4SLinus Torvalds { 45016521f891SChristian Brauner struct user_namespace *mnt_userns; 45021da177e4SLinus Torvalds struct dentry *new_dentry; 4503dae6ad8fSAl Viro struct path old_path, new_path; 4504146a8595SJ. Bruce Fields struct inode *delegated_inode = NULL; 450511a7b371SAneesh Kumar K.V int how = 0; 45061da177e4SLinus Torvalds int error; 45071da177e4SLinus Torvalds 4508020250f3SDmitry Kadashev if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) { 4509020250f3SDmitry Kadashev error = -EINVAL; 4510020250f3SDmitry Kadashev goto out_putnames; 4511020250f3SDmitry Kadashev } 451211a7b371SAneesh Kumar K.V /* 4513f0cc6ffbSLinus Torvalds * To use null names we require CAP_DAC_READ_SEARCH 4514f0cc6ffbSLinus Torvalds * This ensures that not everyone will be able to create 4515f0cc6ffbSLinus Torvalds * handlink using the passed filedescriptor. 451611a7b371SAneesh Kumar K.V */ 4517020250f3SDmitry Kadashev if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) { 4518020250f3SDmitry Kadashev error = -ENOENT; 4519020250f3SDmitry Kadashev goto out_putnames; 4520f0cc6ffbSLinus Torvalds } 4521c04030e1SUlrich Drepper 452211a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 452311a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 4524442e31caSJeff Layton retry: 4525794ebceaSStephen Brennan error = filename_lookup(olddfd, old, how, &old_path, NULL); 45261da177e4SLinus Torvalds if (error) 4527020250f3SDmitry Kadashev goto out_putnames; 45282ad94ae6SAl Viro 4529b4a4f213SStephen Brennan new_dentry = filename_create(newdfd, new, &new_path, 4530442e31caSJeff Layton (how & LOOKUP_REVAL)); 45311da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 45326902d925SDave Hansen if (IS_ERR(new_dentry)) 4533020250f3SDmitry Kadashev goto out_putpath; 4534dae6ad8fSAl Viro 4535dae6ad8fSAl Viro error = -EXDEV; 4536dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 4537dae6ad8fSAl Viro goto out_dput; 4538549c7297SChristian Brauner mnt_userns = mnt_user_ns(new_path.mnt); 4539549c7297SChristian Brauner error = may_linkat(mnt_userns, &old_path); 4540800179c9SKees Cook if (unlikely(error)) 4541800179c9SKees Cook goto out_dput; 4542dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 4543be6d3e56SKentaro Takeda if (error) 4544a8104a9fSAl Viro goto out_dput; 45456521f891SChristian Brauner error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode, 45466521f891SChristian Brauner new_dentry, &delegated_inode); 454775c3f29dSDave Hansen out_dput: 4548921a1650SAl Viro done_path_create(&new_path, new_dentry); 4549146a8595SJ. Bruce Fields if (delegated_inode) { 4550146a8595SJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 4551d22e6338SOleg Drokin if (!error) { 4552d22e6338SOleg Drokin path_put(&old_path); 4553146a8595SJ. Bruce Fields goto retry; 4554146a8595SJ. Bruce Fields } 4555d22e6338SOleg Drokin } 4556442e31caSJeff Layton if (retry_estale(error, how)) { 4557d22e6338SOleg Drokin path_put(&old_path); 4558442e31caSJeff Layton how |= LOOKUP_REVAL; 4559442e31caSJeff Layton goto retry; 4560442e31caSJeff Layton } 4561020250f3SDmitry Kadashev out_putpath: 45622d8f3038SAl Viro path_put(&old_path); 4563020250f3SDmitry Kadashev out_putnames: 4564020250f3SDmitry Kadashev putname(old); 4565020250f3SDmitry Kadashev putname(new); 45661da177e4SLinus Torvalds 45671da177e4SLinus Torvalds return error; 45681da177e4SLinus Torvalds } 45691da177e4SLinus Torvalds 457046ea89ebSDominik Brodowski SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 457146ea89ebSDominik Brodowski int, newdfd, const char __user *, newname, int, flags) 457246ea89ebSDominik Brodowski { 4573020250f3SDmitry Kadashev return do_linkat(olddfd, getname_uflags(oldname, flags), 4574020250f3SDmitry Kadashev newdfd, getname(newname), flags); 457546ea89ebSDominik Brodowski } 457646ea89ebSDominik Brodowski 45773480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 45785590ff0dSUlrich Drepper { 4579020250f3SDmitry Kadashev return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0); 45805590ff0dSUlrich Drepper } 45815590ff0dSUlrich Drepper 4582bc27027aSMiklos Szeredi /** 4583bc27027aSMiklos Szeredi * vfs_rename - rename a filesystem object 45842111c3c0SRandy Dunlap * @rd: pointer to &struct renamedata info 4585bc27027aSMiklos Szeredi * 4586bc27027aSMiklos Szeredi * The caller must hold multiple mutexes--see lock_rename()). 4587bc27027aSMiklos Szeredi * 4588bc27027aSMiklos Szeredi * If vfs_rename discovers a delegation in need of breaking at either 4589bc27027aSMiklos Szeredi * the source or destination, it will return -EWOULDBLOCK and return a 4590bc27027aSMiklos Szeredi * reference to the inode in delegated_inode. The caller should then 4591bc27027aSMiklos Szeredi * break the delegation and retry. Because breaking a delegation may 4592bc27027aSMiklos Szeredi * take a long time, the caller should drop all locks before doing 4593bc27027aSMiklos Szeredi * so. 4594bc27027aSMiklos Szeredi * 4595bc27027aSMiklos Szeredi * Alternatively, a caller may pass NULL for delegated_inode. This may 4596bc27027aSMiklos Szeredi * be appropriate for callers that expect the underlying filesystem not 4597bc27027aSMiklos Szeredi * to be NFS exported. 4598bc27027aSMiklos Szeredi * 45991da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 46001da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 46011da177e4SLinus Torvalds * Problems: 46020117d427SMauro Carvalho Chehab * 4603d03b29a2SJ. Bruce Fields * a) we can get into loop creation. 46041da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 46051da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 4606a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 46071da177e4SLinus Torvalds * story. 46086cedba89SJ. Bruce Fields * c) we have to lock _four_ objects - parents and victim (if it exists), 46096cedba89SJ. Bruce Fields * and source (if it is not a directory). 46101b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 46111da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 46121da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 4613a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 46141da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 46151da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 46161da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 4617a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 46181da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 46191da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 46201da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 4621e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 46221b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 46231da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 4624c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 46251da177e4SLinus Torvalds * locking]. 46261da177e4SLinus Torvalds */ 46279fe61450SChristian Brauner int vfs_rename(struct renamedata *rd) 46281da177e4SLinus Torvalds { 46291da177e4SLinus Torvalds int error; 46309fe61450SChristian Brauner struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; 46319fe61450SChristian Brauner struct dentry *old_dentry = rd->old_dentry; 46329fe61450SChristian Brauner struct dentry *new_dentry = rd->new_dentry; 46339fe61450SChristian Brauner struct inode **delegated_inode = rd->delegated_inode; 46349fe61450SChristian Brauner unsigned int flags = rd->flags; 4635bc27027aSMiklos Szeredi bool is_dir = d_is_dir(old_dentry); 4636bc27027aSMiklos Szeredi struct inode *source = old_dentry->d_inode; 4637bc27027aSMiklos Szeredi struct inode *target = new_dentry->d_inode; 4638da1ce067SMiklos Szeredi bool new_is_dir = false; 4639da1ce067SMiklos Szeredi unsigned max_links = new_dir->i_sb->s_max_links; 464049d31c2fSAl Viro struct name_snapshot old_name; 46411da177e4SLinus Torvalds 46428d3e2936SMiklos Szeredi if (source == target) 46431da177e4SLinus Torvalds return 0; 46441da177e4SLinus Torvalds 46456521f891SChristian Brauner error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir); 46461da177e4SLinus Torvalds if (error) 46471da177e4SLinus Torvalds return error; 46481da177e4SLinus Torvalds 4649da1ce067SMiklos Szeredi if (!target) { 46506521f891SChristian Brauner error = may_create(rd->new_mnt_userns, new_dir, new_dentry); 4651da1ce067SMiklos Szeredi } else { 4652da1ce067SMiklos Szeredi new_is_dir = d_is_dir(new_dentry); 4653da1ce067SMiklos Szeredi 4654da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 46556521f891SChristian Brauner error = may_delete(rd->new_mnt_userns, new_dir, 46566521f891SChristian Brauner new_dentry, is_dir); 4657da1ce067SMiklos Szeredi else 46586521f891SChristian Brauner error = may_delete(rd->new_mnt_userns, new_dir, 46596521f891SChristian Brauner new_dentry, new_is_dir); 4660da1ce067SMiklos Szeredi } 46611da177e4SLinus Torvalds if (error) 46621da177e4SLinus Torvalds return error; 46631da177e4SLinus Torvalds 46642773bf00SMiklos Szeredi if (!old_dir->i_op->rename) 46651da177e4SLinus Torvalds return -EPERM; 46661da177e4SLinus Torvalds 4667bc27027aSMiklos Szeredi /* 4668bc27027aSMiklos Szeredi * If we are going to change the parent - check write permissions, 4669bc27027aSMiklos Szeredi * we'll need to flip '..'. 4670bc27027aSMiklos Szeredi */ 4671da1ce067SMiklos Szeredi if (new_dir != old_dir) { 4672da1ce067SMiklos Szeredi if (is_dir) { 46736521f891SChristian Brauner error = inode_permission(rd->old_mnt_userns, source, 467447291baaSChristian Brauner MAY_WRITE); 4675bc27027aSMiklos Szeredi if (error) 4676bc27027aSMiklos Szeredi return error; 4677bc27027aSMiklos Szeredi } 4678da1ce067SMiklos Szeredi if ((flags & RENAME_EXCHANGE) && new_is_dir) { 46796521f891SChristian Brauner error = inode_permission(rd->new_mnt_userns, target, 468047291baaSChristian Brauner MAY_WRITE); 4681da1ce067SMiklos Szeredi if (error) 4682da1ce067SMiklos Szeredi return error; 4683da1ce067SMiklos Szeredi } 4684da1ce067SMiklos Szeredi } 46850eeca283SRobert Love 46860b3974ebSMiklos Szeredi error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, 46870b3974ebSMiklos Szeredi flags); 4688bc27027aSMiklos Szeredi if (error) 4689bc27027aSMiklos Szeredi return error; 4690bc27027aSMiklos Szeredi 469149d31c2fSAl Viro take_dentry_name_snapshot(&old_name, old_dentry); 4692bc27027aSMiklos Szeredi dget(new_dentry); 4693da1ce067SMiklos Szeredi if (!is_dir || (flags & RENAME_EXCHANGE)) 4694bc27027aSMiklos Szeredi lock_two_nondirectories(source, target); 4695bc27027aSMiklos Szeredi else if (target) 46965955102cSAl Viro inode_lock(target); 4697bc27027aSMiklos Szeredi 469851cc3a66SHugh Dickins error = -EPERM; 469951cc3a66SHugh Dickins if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target))) 470051cc3a66SHugh Dickins goto out; 470151cc3a66SHugh Dickins 4702bc27027aSMiklos Szeredi error = -EBUSY; 47037af1364fSEric W. Biederman if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) 4704bc27027aSMiklos Szeredi goto out; 4705bc27027aSMiklos Szeredi 4706da1ce067SMiklos Szeredi if (max_links && new_dir != old_dir) { 4707bc27027aSMiklos Szeredi error = -EMLINK; 4708da1ce067SMiklos Szeredi if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) 4709bc27027aSMiklos Szeredi goto out; 4710da1ce067SMiklos Szeredi if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && 4711da1ce067SMiklos Szeredi old_dir->i_nlink >= max_links) 4712da1ce067SMiklos Szeredi goto out; 4713da1ce067SMiklos Szeredi } 4714da1ce067SMiklos Szeredi if (!is_dir) { 4715bc27027aSMiklos Szeredi error = try_break_deleg(source, delegated_inode); 4716bc27027aSMiklos Szeredi if (error) 4717bc27027aSMiklos Szeredi goto out; 4718da1ce067SMiklos Szeredi } 4719da1ce067SMiklos Szeredi if (target && !new_is_dir) { 4720bc27027aSMiklos Szeredi error = try_break_deleg(target, delegated_inode); 4721bc27027aSMiklos Szeredi if (error) 4722bc27027aSMiklos Szeredi goto out; 4723bc27027aSMiklos Szeredi } 4724549c7297SChristian Brauner error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry, 4725520c8b16SMiklos Szeredi new_dir, new_dentry, flags); 4726bc27027aSMiklos Szeredi if (error) 4727bc27027aSMiklos Szeredi goto out; 4728bc27027aSMiklos Szeredi 4729da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE) && target) { 47308767712fSAl Viro if (is_dir) { 47318767712fSAl Viro shrink_dcache_parent(new_dentry); 4732bc27027aSMiklos Szeredi target->i_flags |= S_DEAD; 47338767712fSAl Viro } 4734bc27027aSMiklos Szeredi dont_mount(new_dentry); 47358ed936b5SEric W. Biederman detach_mounts(new_dentry); 4736bc27027aSMiklos Szeredi } 4737da1ce067SMiklos Szeredi if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { 4738da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 4739bc27027aSMiklos Szeredi d_move(old_dentry, new_dentry); 4740da1ce067SMiklos Szeredi else 4741da1ce067SMiklos Szeredi d_exchange(old_dentry, new_dentry); 4742da1ce067SMiklos Szeredi } 4743bc27027aSMiklos Szeredi out: 4744da1ce067SMiklos Szeredi if (!is_dir || (flags & RENAME_EXCHANGE)) 4745bc27027aSMiklos Szeredi unlock_two_nondirectories(source, target); 4746bc27027aSMiklos Szeredi else if (target) 47475955102cSAl Viro inode_unlock(target); 4748bc27027aSMiklos Szeredi dput(new_dentry); 4749da1ce067SMiklos Szeredi if (!error) { 4750f4ec3a3dSAl Viro fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, 4751da1ce067SMiklos Szeredi !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); 4752da1ce067SMiklos Szeredi if (flags & RENAME_EXCHANGE) { 4753f4ec3a3dSAl Viro fsnotify_move(new_dir, old_dir, &old_dentry->d_name, 4754da1ce067SMiklos Szeredi new_is_dir, NULL, new_dentry); 4755da1ce067SMiklos Szeredi } 4756da1ce067SMiklos Szeredi } 475749d31c2fSAl Viro release_dentry_name_snapshot(&old_name); 47580eeca283SRobert Love 47591da177e4SLinus Torvalds return error; 47601da177e4SLinus Torvalds } 47614d359507SAl Viro EXPORT_SYMBOL(vfs_rename); 47621da177e4SLinus Torvalds 4763e886663cSJens Axboe int do_renameat2(int olddfd, struct filename *from, int newdfd, 4764e886663cSJens Axboe struct filename *to, unsigned int flags) 47651da177e4SLinus Torvalds { 47669fe61450SChristian Brauner struct renamedata rd; 47671da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 47681da177e4SLinus Torvalds struct dentry *trap; 4769f5beed75SAl Viro struct path old_path, new_path; 4770f5beed75SAl Viro struct qstr old_last, new_last; 4771f5beed75SAl Viro int old_type, new_type; 47728e6d782cSJ. Bruce Fields struct inode *delegated_inode = NULL; 4773f5beed75SAl Viro unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; 4774c6a94284SJeff Layton bool should_retry = false; 4775e886663cSJens Axboe int error = -EINVAL; 4776520c8b16SMiklos Szeredi 47770d7a8555SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 47780ee50b47SDmitry Kadashev goto put_names; 4779da1ce067SMiklos Szeredi 47800d7a8555SMiklos Szeredi if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && 47810d7a8555SMiklos Szeredi (flags & RENAME_EXCHANGE)) 47820ee50b47SDmitry Kadashev goto put_names; 4783520c8b16SMiklos Szeredi 4784f5beed75SAl Viro if (flags & RENAME_EXCHANGE) 4785f5beed75SAl Viro target_flags = 0; 4786f5beed75SAl Viro 4787c6a94284SJeff Layton retry: 4788c5f563f9SAl Viro error = filename_parentat(olddfd, from, lookup_flags, &old_path, 4789e886663cSJens Axboe &old_last, &old_type); 47900ee50b47SDmitry Kadashev if (error) 47910ee50b47SDmitry Kadashev goto put_names; 47921da177e4SLinus Torvalds 4793c5f563f9SAl Viro error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, 4794e886663cSJens Axboe &new_type); 47950ee50b47SDmitry Kadashev if (error) 47961da177e4SLinus Torvalds goto exit1; 47971da177e4SLinus Torvalds 47981da177e4SLinus Torvalds error = -EXDEV; 4799f5beed75SAl Viro if (old_path.mnt != new_path.mnt) 48001da177e4SLinus Torvalds goto exit2; 48011da177e4SLinus Torvalds 48021da177e4SLinus Torvalds error = -EBUSY; 4803f5beed75SAl Viro if (old_type != LAST_NORM) 48041da177e4SLinus Torvalds goto exit2; 48051da177e4SLinus Torvalds 48060a7c3937SMiklos Szeredi if (flags & RENAME_NOREPLACE) 48070a7c3937SMiklos Szeredi error = -EEXIST; 4808f5beed75SAl Viro if (new_type != LAST_NORM) 48091da177e4SLinus Torvalds goto exit2; 48101da177e4SLinus Torvalds 4811f5beed75SAl Viro error = mnt_want_write(old_path.mnt); 4812c30dabfeSJan Kara if (error) 4813c30dabfeSJan Kara goto exit2; 4814c30dabfeSJan Kara 48158e6d782cSJ. Bruce Fields retry_deleg: 4816f5beed75SAl Viro trap = lock_rename(new_path.dentry, old_path.dentry); 48171da177e4SLinus Torvalds 4818f5beed75SAl Viro old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); 48191da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 48201da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 48211da177e4SLinus Torvalds goto exit3; 48221da177e4SLinus Torvalds /* source must exist */ 48231da177e4SLinus Torvalds error = -ENOENT; 4824b18825a7SDavid Howells if (d_is_negative(old_dentry)) 48251da177e4SLinus Torvalds goto exit4; 4826f5beed75SAl Viro new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); 48271da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 48281da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 48291da177e4SLinus Torvalds goto exit4; 48300a7c3937SMiklos Szeredi error = -EEXIST; 48310a7c3937SMiklos Szeredi if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) 48320a7c3937SMiklos Szeredi goto exit5; 4833da1ce067SMiklos Szeredi if (flags & RENAME_EXCHANGE) { 4834da1ce067SMiklos Szeredi error = -ENOENT; 4835da1ce067SMiklos Szeredi if (d_is_negative(new_dentry)) 4836da1ce067SMiklos Szeredi goto exit5; 4837da1ce067SMiklos Szeredi 4838da1ce067SMiklos Szeredi if (!d_is_dir(new_dentry)) { 4839da1ce067SMiklos Szeredi error = -ENOTDIR; 4840f5beed75SAl Viro if (new_last.name[new_last.len]) 4841da1ce067SMiklos Szeredi goto exit5; 4842da1ce067SMiklos Szeredi } 4843da1ce067SMiklos Szeredi } 48440a7c3937SMiklos Szeredi /* unless the source is a directory trailing slashes give -ENOTDIR */ 48450a7c3937SMiklos Szeredi if (!d_is_dir(old_dentry)) { 48460a7c3937SMiklos Szeredi error = -ENOTDIR; 4847f5beed75SAl Viro if (old_last.name[old_last.len]) 48480a7c3937SMiklos Szeredi goto exit5; 4849f5beed75SAl Viro if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) 48500a7c3937SMiklos Szeredi goto exit5; 48510a7c3937SMiklos Szeredi } 48520a7c3937SMiklos Szeredi /* source should not be ancestor of target */ 48530a7c3937SMiklos Szeredi error = -EINVAL; 48540a7c3937SMiklos Szeredi if (old_dentry == trap) 48550a7c3937SMiklos Szeredi goto exit5; 48561da177e4SLinus Torvalds /* target should not be an ancestor of source */ 4857da1ce067SMiklos Szeredi if (!(flags & RENAME_EXCHANGE)) 48581da177e4SLinus Torvalds error = -ENOTEMPTY; 48591da177e4SLinus Torvalds if (new_dentry == trap) 48601da177e4SLinus Torvalds goto exit5; 48611da177e4SLinus Torvalds 4862f5beed75SAl Viro error = security_path_rename(&old_path, old_dentry, 4863f5beed75SAl Viro &new_path, new_dentry, flags); 4864be6d3e56SKentaro Takeda if (error) 4865c30dabfeSJan Kara goto exit5; 48669fe61450SChristian Brauner 48679fe61450SChristian Brauner rd.old_dir = old_path.dentry->d_inode; 48689fe61450SChristian Brauner rd.old_dentry = old_dentry; 48696521f891SChristian Brauner rd.old_mnt_userns = mnt_user_ns(old_path.mnt); 48709fe61450SChristian Brauner rd.new_dir = new_path.dentry->d_inode; 48719fe61450SChristian Brauner rd.new_dentry = new_dentry; 48726521f891SChristian Brauner rd.new_mnt_userns = mnt_user_ns(new_path.mnt); 48739fe61450SChristian Brauner rd.delegated_inode = &delegated_inode; 48749fe61450SChristian Brauner rd.flags = flags; 48759fe61450SChristian Brauner error = vfs_rename(&rd); 48761da177e4SLinus Torvalds exit5: 48771da177e4SLinus Torvalds dput(new_dentry); 48781da177e4SLinus Torvalds exit4: 48791da177e4SLinus Torvalds dput(old_dentry); 48801da177e4SLinus Torvalds exit3: 4881f5beed75SAl Viro unlock_rename(new_path.dentry, old_path.dentry); 48828e6d782cSJ. Bruce Fields if (delegated_inode) { 48838e6d782cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 48848e6d782cSJ. Bruce Fields if (!error) 48858e6d782cSJ. Bruce Fields goto retry_deleg; 48868e6d782cSJ. Bruce Fields } 4887f5beed75SAl Viro mnt_drop_write(old_path.mnt); 48881da177e4SLinus Torvalds exit2: 4889c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 4890c6a94284SJeff Layton should_retry = true; 4891f5beed75SAl Viro path_put(&new_path); 48921da177e4SLinus Torvalds exit1: 4893f5beed75SAl Viro path_put(&old_path); 4894c6a94284SJeff Layton if (should_retry) { 4895c6a94284SJeff Layton should_retry = false; 4896c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 4897c6a94284SJeff Layton goto retry; 4898c6a94284SJeff Layton } 48990ee50b47SDmitry Kadashev put_names: 4900e886663cSJens Axboe putname(from); 4901e886663cSJens Axboe putname(to); 49021da177e4SLinus Torvalds return error; 49031da177e4SLinus Torvalds } 49041da177e4SLinus Torvalds 4905ee81feb6SDominik Brodowski SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, 4906ee81feb6SDominik Brodowski int, newdfd, const char __user *, newname, unsigned int, flags) 4907ee81feb6SDominik Brodowski { 4908e886663cSJens Axboe return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 4909e886663cSJens Axboe flags); 4910ee81feb6SDominik Brodowski } 4911ee81feb6SDominik Brodowski 4912520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 4913520c8b16SMiklos Szeredi int, newdfd, const char __user *, newname) 4914520c8b16SMiklos Szeredi { 4915e886663cSJens Axboe return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), 4916e886663cSJens Axboe 0); 4917520c8b16SMiklos Szeredi } 4918520c8b16SMiklos Szeredi 4919a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 49205590ff0dSUlrich Drepper { 4921e886663cSJens Axboe return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, 4922e886663cSJens Axboe getname(newname), 0); 49235590ff0dSUlrich Drepper } 49245590ff0dSUlrich Drepper 49255d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link) 49261da177e4SLinus Torvalds { 49275d826c84SAl Viro int len = PTR_ERR(link); 49281da177e4SLinus Torvalds if (IS_ERR(link)) 49291da177e4SLinus Torvalds goto out; 49301da177e4SLinus Torvalds 49311da177e4SLinus Torvalds len = strlen(link); 49321da177e4SLinus Torvalds if (len > (unsigned) buflen) 49331da177e4SLinus Torvalds len = buflen; 49341da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 49351da177e4SLinus Torvalds len = -EFAULT; 49361da177e4SLinus Torvalds out: 49371da177e4SLinus Torvalds return len; 49381da177e4SLinus Torvalds } 49391da177e4SLinus Torvalds 4940d60874cdSMiklos Szeredi /** 4941fd4a0edfSMiklos Szeredi * vfs_readlink - copy symlink body into userspace buffer 4942fd4a0edfSMiklos Szeredi * @dentry: dentry on which to get symbolic link 4943fd4a0edfSMiklos Szeredi * @buffer: user memory pointer 4944fd4a0edfSMiklos Szeredi * @buflen: size of buffer 4945fd4a0edfSMiklos Szeredi * 4946fd4a0edfSMiklos Szeredi * Does not touch atime. That's up to the caller if necessary 4947fd4a0edfSMiklos Szeredi * 4948fd4a0edfSMiklos Szeredi * Does not call security hook. 4949fd4a0edfSMiklos Szeredi */ 4950fd4a0edfSMiklos Szeredi int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) 4951fd4a0edfSMiklos Szeredi { 4952fd4a0edfSMiklos Szeredi struct inode *inode = d_inode(dentry); 4953f2df5da6SAl Viro DEFINE_DELAYED_CALL(done); 4954f2df5da6SAl Viro const char *link; 4955f2df5da6SAl Viro int res; 4956fd4a0edfSMiklos Szeredi 495776fca90eSMiklos Szeredi if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { 495876fca90eSMiklos Szeredi if (unlikely(inode->i_op->readlink)) 495976fca90eSMiklos Szeredi return inode->i_op->readlink(dentry, buffer, buflen); 496076fca90eSMiklos Szeredi 496176fca90eSMiklos Szeredi if (!d_is_symlink(dentry)) 4962fd4a0edfSMiklos Szeredi return -EINVAL; 4963fd4a0edfSMiklos Szeredi 496476fca90eSMiklos Szeredi spin_lock(&inode->i_lock); 496576fca90eSMiklos Szeredi inode->i_opflags |= IOP_DEFAULT_READLINK; 496676fca90eSMiklos Szeredi spin_unlock(&inode->i_lock); 496776fca90eSMiklos Szeredi } 496876fca90eSMiklos Szeredi 49694c4f7c19SEric Biggers link = READ_ONCE(inode->i_link); 4970f2df5da6SAl Viro if (!link) { 4971f2df5da6SAl Viro link = inode->i_op->get_link(dentry, inode, &done); 4972f2df5da6SAl Viro if (IS_ERR(link)) 4973f2df5da6SAl Viro return PTR_ERR(link); 4974f2df5da6SAl Viro } 4975f2df5da6SAl Viro res = readlink_copy(buffer, buflen, link); 4976f2df5da6SAl Viro do_delayed_call(&done); 4977f2df5da6SAl Viro return res; 4978fd4a0edfSMiklos Szeredi } 4979fd4a0edfSMiklos Szeredi EXPORT_SYMBOL(vfs_readlink); 49801da177e4SLinus Torvalds 4981d60874cdSMiklos Szeredi /** 4982d60874cdSMiklos Szeredi * vfs_get_link - get symlink body 4983d60874cdSMiklos Szeredi * @dentry: dentry on which to get symbolic link 4984d60874cdSMiklos Szeredi * @done: caller needs to free returned data with this 4985d60874cdSMiklos Szeredi * 4986d60874cdSMiklos Szeredi * Calls security hook and i_op->get_link() on the supplied inode. 4987d60874cdSMiklos Szeredi * 4988d60874cdSMiklos Szeredi * It does not touch atime. That's up to the caller if necessary. 4989d60874cdSMiklos Szeredi * 4990d60874cdSMiklos Szeredi * Does not work on "special" symlinks like /proc/$$/fd/N 4991d60874cdSMiklos Szeredi */ 4992d60874cdSMiklos Szeredi const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) 4993d60874cdSMiklos Szeredi { 4994d60874cdSMiklos Szeredi const char *res = ERR_PTR(-EINVAL); 4995d60874cdSMiklos Szeredi struct inode *inode = d_inode(dentry); 4996d60874cdSMiklos Szeredi 4997d60874cdSMiklos Szeredi if (d_is_symlink(dentry)) { 4998d60874cdSMiklos Szeredi res = ERR_PTR(security_inode_readlink(dentry)); 4999d60874cdSMiklos Szeredi if (!res) 5000d60874cdSMiklos Szeredi res = inode->i_op->get_link(dentry, inode, done); 5001d60874cdSMiklos Szeredi } 5002d60874cdSMiklos Szeredi return res; 5003d60874cdSMiklos Szeredi } 5004d60874cdSMiklos Szeredi EXPORT_SYMBOL(vfs_get_link); 5005d60874cdSMiklos Szeredi 50061da177e4SLinus Torvalds /* get the link contents into pagecache */ 50076b255391SAl Viro const char *page_get_link(struct dentry *dentry, struct inode *inode, 5008fceef393SAl Viro struct delayed_call *callback) 50091da177e4SLinus Torvalds { 5010ebd09abbSDuane Griffin char *kaddr; 50111da177e4SLinus Torvalds struct page *page; 50126b255391SAl Viro struct address_space *mapping = inode->i_mapping; 50136b255391SAl Viro 5014d3883d4fSAl Viro if (!dentry) { 5015d3883d4fSAl Viro page = find_get_page(mapping, 0); 5016d3883d4fSAl Viro if (!page) 50176b255391SAl Viro return ERR_PTR(-ECHILD); 5018d3883d4fSAl Viro if (!PageUptodate(page)) { 5019d3883d4fSAl Viro put_page(page); 5020d3883d4fSAl Viro return ERR_PTR(-ECHILD); 5021d3883d4fSAl Viro } 5022d3883d4fSAl Viro } else { 5023090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 50241da177e4SLinus Torvalds if (IS_ERR(page)) 50256fe6900eSNick Piggin return (char*)page; 5026d3883d4fSAl Viro } 5027fceef393SAl Viro set_delayed_call(callback, page_put_link, page); 502821fc61c7SAl Viro BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); 502921fc61c7SAl Viro kaddr = page_address(page); 50306b255391SAl Viro nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); 5031ebd09abbSDuane Griffin return kaddr; 50321da177e4SLinus Torvalds } 50331da177e4SLinus Torvalds 50346b255391SAl Viro EXPORT_SYMBOL(page_get_link); 50351da177e4SLinus Torvalds 5036fceef393SAl Viro void page_put_link(void *arg) 50371da177e4SLinus Torvalds { 5038fceef393SAl Viro put_page(arg); 50391da177e4SLinus Torvalds } 50404d359507SAl Viro EXPORT_SYMBOL(page_put_link); 50411da177e4SLinus Torvalds 5042aa80deabSAl Viro int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 5043aa80deabSAl Viro { 5044fceef393SAl Viro DEFINE_DELAYED_CALL(done); 50456b255391SAl Viro int res = readlink_copy(buffer, buflen, 50466b255391SAl Viro page_get_link(dentry, d_inode(dentry), 5047fceef393SAl Viro &done)); 5048fceef393SAl Viro do_delayed_call(&done); 5049aa80deabSAl Viro return res; 5050aa80deabSAl Viro } 5051aa80deabSAl Viro EXPORT_SYMBOL(page_readlink); 5052aa80deabSAl Viro 505356f5746cSMatthew Wilcox (Oracle) int page_symlink(struct inode *inode, const char *symname, int len) 50541da177e4SLinus Torvalds { 50551da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 505627a77913SMatthew Wilcox (Oracle) const struct address_space_operations *aops = mapping->a_ops; 505756f5746cSMatthew Wilcox (Oracle) bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS); 50580adb25d2SKirill Korotaev struct page *page; 5059afddba49SNick Piggin void *fsdata; 5060beb497abSDmitriy Monakhov int err; 50612d878178SMatthew Wilcox (Oracle) unsigned int flags; 50621da177e4SLinus Torvalds 50637e53cac4SNeilBrown retry: 50642d878178SMatthew Wilcox (Oracle) if (nofs) 50652d878178SMatthew Wilcox (Oracle) flags = memalloc_nofs_save(); 506627a77913SMatthew Wilcox (Oracle) err = aops->write_begin(NULL, mapping, 0, len-1, &page, &fsdata); 50672d878178SMatthew Wilcox (Oracle) if (nofs) 50682d878178SMatthew Wilcox (Oracle) memalloc_nofs_restore(flags); 50691da177e4SLinus Torvalds if (err) 5070afddba49SNick Piggin goto fail; 5071afddba49SNick Piggin 507221fc61c7SAl Viro memcpy(page_address(page), symname, len-1); 5073afddba49SNick Piggin 507427a77913SMatthew Wilcox (Oracle) err = aops->write_end(NULL, mapping, 0, len-1, len-1, 5075afddba49SNick Piggin page, fsdata); 50761da177e4SLinus Torvalds if (err < 0) 50771da177e4SLinus Torvalds goto fail; 5078afddba49SNick Piggin if (err < len-1) 5079afddba49SNick Piggin goto retry; 5080afddba49SNick Piggin 50811da177e4SLinus Torvalds mark_inode_dirty(inode); 50821da177e4SLinus Torvalds return 0; 50831da177e4SLinus Torvalds fail: 50841da177e4SLinus Torvalds return err; 50851da177e4SLinus Torvalds } 50864d359507SAl Viro EXPORT_SYMBOL(page_symlink); 50870adb25d2SKirill Korotaev 508892e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 50896b255391SAl Viro .get_link = page_get_link, 50901da177e4SLinus Torvalds }; 50911da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 5092