Merge tag 'for-5.15-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux
[linux-2.6-microblaze.git] / fs / namei.c
index e9358e6..d049d39 100644 (file)
@@ -203,6 +203,14 @@ getname_flags(const char __user *filename, int flags, int *empty)
        return result;
 }
 
+struct filename *
+getname_uflags(const char __user *filename, int uflags)
+{
+       int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
+
+       return getname_flags(filename, flags, NULL);
+}
+
 struct filename *
 getname(const char __user * filename)
 {
@@ -2459,7 +2467,7 @@ static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path
        return err;
 }
 
-int filename_lookup(int dfd, struct filename *name, unsigned flags,
+static int __filename_lookup(int dfd, struct filename *name, unsigned flags,
                    struct path *path, struct path *root)
 {
        int retval;
@@ -2477,6 +2485,14 @@ int filename_lookup(int dfd, struct filename *name, unsigned flags,
                audit_inode(name, path->dentry,
                            flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
        restore_nameidata();
+       return retval;
+}
+
+int filename_lookup(int dfd, struct filename *name, unsigned flags,
+                   struct path *path, struct path *root)
+{
+       int retval = __filename_lookup(dfd, name, flags, path, root);
+
        putname(name);
        return retval;
 }
@@ -2582,8 +2598,9 @@ int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
 }
 EXPORT_SYMBOL(vfs_path_lookup);
 
-static int lookup_one_len_common(const char *name, struct dentry *base,
-                                int len, struct qstr *this)
+static int lookup_one_common(struct user_namespace *mnt_userns,
+                            const char *name, struct dentry *base, int len,
+                            struct qstr *this)
 {
        this->name = name;
        this->len = len;
@@ -2611,7 +2628,7 @@ static int lookup_one_len_common(const char *name, struct dentry *base,
                        return err;
        }
 
-       return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC);
+       return inode_permission(mnt_userns, base->d_inode, MAY_EXEC);
 }
 
 /**
@@ -2635,7 +2652,7 @@ struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len
 
        WARN_ON_ONCE(!inode_is_locked(base->d_inode));
 
-       err = lookup_one_len_common(name, base, len, &this);
+       err = lookup_one_common(&init_user_ns, name, base, len, &this);
        if (err)
                return ERR_PTR(err);
 
@@ -2662,7 +2679,7 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
 
        WARN_ON_ONCE(!inode_is_locked(base->d_inode));
 
-       err = lookup_one_len_common(name, base, len, &this);
+       err = lookup_one_common(&init_user_ns, name, base, len, &this);
        if (err)
                return ERR_PTR(err);
 
@@ -2671,6 +2688,36 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
 }
 EXPORT_SYMBOL(lookup_one_len);
 
+/**
+ * lookup_one - filesystem helper to lookup single pathname component
+ * @mnt_userns:        user namespace of the mount the lookup is performed from
+ * @name:      pathname component to lookup
+ * @base:      base directory to lookup from
+ * @len:       maximum length @len should be interpreted to
+ *
+ * Note that this routine is purely a helper for filesystem usage and should
+ * not be called by generic code.
+ *
+ * The caller must hold base->i_mutex.
+ */
+struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
+                         struct dentry *base, int len)
+{
+       struct dentry *dentry;
+       struct qstr this;
+       int err;
+
+       WARN_ON_ONCE(!inode_is_locked(base->d_inode));
+
+       err = lookup_one_common(mnt_userns, name, base, len, &this);
+       if (err)
+               return ERR_PTR(err);
+
+       dentry = lookup_dcache(&this, base, 0);
+       return dentry ? dentry : __lookup_slow(&this, base, 0);
+}
+EXPORT_SYMBOL(lookup_one);
+
 /**
  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
  * @name:      pathname component to lookup
@@ -2690,7 +2737,7 @@ struct dentry *lookup_one_len_unlocked(const char *name,
        int err;
        struct dentry *ret;
 
-       err = lookup_one_len_common(name, base, len, &this);
+       err = lookup_one_common(&init_user_ns, name, base, len, &this);
        if (err)
                return ERR_PTR(err);
 
@@ -3030,9 +3077,7 @@ static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
        /*
         * Refuse to truncate files with mandatory locks held on them.
         */
-       error = locks_verify_locked(filp);
-       if (!error)
-               error = security_path_truncate(path);
+       error = security_path_truncate(path);
        if (!error) {
                error = do_truncate(mnt_userns, path->dentry, 0,
                                    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
@@ -3739,7 +3784,7 @@ static int may_mknod(umode_t mode)
        }
 }
 
-static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
+static int do_mknodat(int dfd, struct filename *name, umode_t mode,
                unsigned int dev)
 {
        struct user_namespace *mnt_userns;
@@ -3750,17 +3795,18 @@ static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
 
        error = may_mknod(mode);
        if (error)
-               return error;
+               goto out1;
 retry:
-       dentry = user_path_create(dfd, filename, &path, lookup_flags);
+       dentry = __filename_create(dfd, name, &path, lookup_flags);
+       error = PTR_ERR(dentry);
        if (IS_ERR(dentry))
-               return PTR_ERR(dentry);
+               goto out1;
 
        if (!IS_POSIXACL(path.dentry->d_inode))
                mode &= ~current_umask();
        error = security_path_mknod(&path, dentry, mode, dev);
        if (error)
-               goto out;
+               goto out2;
 
        mnt_userns = mnt_user_ns(path.mnt);
        switch (mode & S_IFMT) {
@@ -3779,24 +3825,26 @@ retry:
                                          dentry, mode, 0);
                        break;
        }
-out:
+out2:
        done_path_create(&path, dentry);
        if (retry_estale(error, lookup_flags)) {
                lookup_flags |= LOOKUP_REVAL;
                goto retry;
        }
+out1:
+       putname(name);
        return error;
 }
 
 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
                unsigned int, dev)
 {
-       return do_mknodat(dfd, filename, mode, dev);
+       return do_mknodat(dfd, getname(filename), mode, dev);
 }
 
 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
 {
-       return do_mknodat(AT_FDCWD, filename, mode, dev);
+       return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
 }
 
 /**
@@ -3841,7 +3889,7 @@ int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
 }
 EXPORT_SYMBOL(vfs_mkdir);
 
-long do_mkdirat(int dfd, struct filename *name, umode_t mode)
+int do_mkdirat(int dfd, struct filename *name, umode_t mode)
 {
        struct dentry *dentry;
        struct path path;
@@ -3938,7 +3986,7 @@ out:
 }
 EXPORT_SYMBOL(vfs_rmdir);
 
-long do_rmdir(int dfd, struct filename *name)
+int do_rmdir(int dfd, struct filename *name)
 {
        struct user_namespace *mnt_userns;
        int error;
@@ -4076,7 +4124,7 @@ EXPORT_SYMBOL(vfs_unlink);
  * writeout happening, and we don't want to prevent access to the directory
  * while waiting on the I/O.
  */
-long do_unlinkat(int dfd, struct filename *name)
+int do_unlinkat(int dfd, struct filename *name)
 {
        int error;
        struct dentry *dentry;
@@ -4204,23 +4252,22 @@ int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
 }
 EXPORT_SYMBOL(vfs_symlink);
 
-static long do_symlinkat(const char __user *oldname, int newdfd,
-                 const char __user *newname)
+int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
 {
        int error;
-       struct filename *from;
        struct dentry *dentry;
        struct path path;
        unsigned int lookup_flags = 0;
 
-       from = getname(oldname);
-       if (IS_ERR(from))
-               return PTR_ERR(from);
+       if (IS_ERR(from)) {
+               error = PTR_ERR(from);
+               goto out_putnames;
+       }
 retry:
-       dentry = user_path_create(newdfd, newname, &path, lookup_flags);
+       dentry = __filename_create(newdfd, to, &path, lookup_flags);
        error = PTR_ERR(dentry);
        if (IS_ERR(dentry))
-               goto out_putname;
+               goto out_putnames;
 
        error = security_path_symlink(&path, dentry, from->name);
        if (!error) {
@@ -4235,7 +4282,8 @@ retry:
                lookup_flags |= LOOKUP_REVAL;
                goto retry;
        }
-out_putname:
+out_putnames:
+       putname(to);
        putname(from);
        return error;
 }
@@ -4243,12 +4291,12 @@ out_putname:
 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
                int, newdfd, const char __user *, newname)
 {
-       return do_symlinkat(oldname, newdfd, newname);
+       return do_symlinkat(getname(oldname), newdfd, getname(newname));
 }
 
 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
 {
-       return do_symlinkat(oldname, AT_FDCWD, newname);
+       return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
 }
 
 /**
@@ -4349,8 +4397,8 @@ EXPORT_SYMBOL(vfs_link);
  * with linux 2.0, and to avoid hard-linking to directories
  * and other special files.  --ADM
  */
-static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
-             const char __user *newname, int flags)
+int do_linkat(int olddfd, struct filename *old, int newdfd,
+             struct filename *new, int flags)
 {
        struct user_namespace *mnt_userns;
        struct dentry *new_dentry;
@@ -4359,31 +4407,32 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
        int how = 0;
        int error;
 
-       if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
-               return -EINVAL;
+       if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
+               error = -EINVAL;
+               goto out_putnames;
+       }
        /*
         * To use null names we require CAP_DAC_READ_SEARCH
         * This ensures that not everyone will be able to create
         * handlink using the passed filedescriptor.
         */
-       if (flags & AT_EMPTY_PATH) {
-               if (!capable(CAP_DAC_READ_SEARCH))
-                       return -ENOENT;
-               how = LOOKUP_EMPTY;
+       if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
+               error = -ENOENT;
+               goto out_putnames;
        }
 
        if (flags & AT_SYMLINK_FOLLOW)
                how |= LOOKUP_FOLLOW;
 retry:
-       error = user_path_at(olddfd, oldname, how, &old_path);
+       error = __filename_lookup(olddfd, old, how, &old_path, NULL);
        if (error)
-               return error;
+               goto out_putnames;
 
-       new_dentry = user_path_create(newdfd, newname, &new_path,
+       new_dentry = __filename_create(newdfd, new, &new_path,
                                        (how & LOOKUP_REVAL));
        error = PTR_ERR(new_dentry);
        if (IS_ERR(new_dentry))
-               goto out;
+               goto out_putpath;
 
        error = -EXDEV;
        if (old_path.mnt != new_path.mnt)
@@ -4411,8 +4460,11 @@ out_dput:
                how |= LOOKUP_REVAL;
                goto retry;
        }
-out:
+out_putpath:
        path_put(&old_path);
+out_putnames:
+       putname(old);
+       putname(new);
 
        return error;
 }
@@ -4420,12 +4472,13 @@ out:
 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
                int, newdfd, const char __user *, newname, int, flags)
 {
-       return do_linkat(olddfd, oldname, newdfd, newname, flags);
+       return do_linkat(olddfd, getname_uflags(oldname, flags),
+               newdfd, getname(newname), flags);
 }
 
 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
 {
-       return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
+       return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
 }
 
 /**