Commit b91db6a0 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'for-5.15/io_uring-vfs-2021-08-30' of git://git.kernel.dk/linux-block

Pull io_uring mkdirat/symlinkat/linkat support from Jens Axboe:
 "This adds io_uring support for mkdirat, symlinkat, and linkat"

* tag 'for-5.15/io_uring-vfs-2021-08-30' of git://git.kernel.dk/linux-block:
  io_uring: add support for IORING_OP_LINKAT
  io_uring: add support for IORING_OP_SYMLINKAT
  io_uring: add support for IORING_OP_MKDIRAT
  namei: update do_*() helpers to return ints
  namei: make do_linkat() take struct filename
  namei: add getname_uflags()
  namei: make do_symlinkat() take struct filename
  namei: make do_mknodat() take struct filename
  namei: make do_mkdirat() take struct filename
  namei: change filename_parentat() calling conventions
  namei: ignore ERR/NULL names in putname()
parents 3b629f8d cf30da90
...@@ -2070,10 +2070,8 @@ SYSCALL_DEFINE5(execveat, ...@@ -2070,10 +2070,8 @@ SYSCALL_DEFINE5(execveat,
const char __user *const __user *, envp, const char __user *const __user *, envp,
int, flags) int, flags)
{ {
int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
return do_execveat(fd, return do_execveat(fd,
getname_flags(filename, lookup_flags, NULL), getname_uflags(filename, flags),
argv, envp, flags); argv, envp, flags);
} }
...@@ -2091,10 +2089,8 @@ COMPAT_SYSCALL_DEFINE5(execveat, int, fd, ...@@ -2091,10 +2089,8 @@ COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
const compat_uptr_t __user *, envp, const compat_uptr_t __user *, envp,
int, flags) int, flags)
{ {
int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
return compat_do_execveat(fd, return compat_do_execveat(fd,
getname_flags(filename, lookup_flags, NULL), getname_uflags(filename, flags),
argv, envp, flags); argv, envp, flags);
} }
#endif #endif
...@@ -71,11 +71,15 @@ extern int filename_lookup(int dfd, struct filename *name, unsigned flags, ...@@ -71,11 +71,15 @@ extern int filename_lookup(int dfd, struct filename *name, unsigned flags,
struct path *path, struct path *root); struct path *path, struct path *root);
extern int vfs_path_lookup(struct dentry *, struct vfsmount *, extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
const char *, unsigned int, struct path *); const char *, unsigned int, struct path *);
long do_rmdir(int dfd, struct filename *name); int do_rmdir(int dfd, struct filename *name);
long do_unlinkat(int dfd, struct filename *name); int do_unlinkat(int dfd, struct filename *name);
int may_linkat(struct user_namespace *mnt_userns, struct path *link); int may_linkat(struct user_namespace *mnt_userns, struct path *link);
int do_renameat2(int olddfd, struct filename *oldname, int newdfd, int do_renameat2(int olddfd, struct filename *oldname, int newdfd,
struct filename *newname, unsigned int flags); struct filename *newname, unsigned int flags);
int do_mkdirat(int dfd, struct filename *name, umode_t mode);
int do_symlinkat(struct filename *from, int newdfd, struct filename *to);
int do_linkat(int olddfd, struct filename *old, int newdfd,
struct filename *new, int flags);
/* /*
* namespace.c * namespace.c
......
...@@ -667,6 +667,29 @@ struct io_unlink { ...@@ -667,6 +667,29 @@ struct io_unlink {
struct filename *filename; struct filename *filename;
}; };
struct io_mkdir {
struct file *file;
int dfd;
umode_t mode;
struct filename *filename;
};
struct io_symlink {
struct file *file;
int new_dfd;
struct filename *oldpath;
struct filename *newpath;
};
struct io_hardlink {
struct file *file;
int old_dfd;
int new_dfd;
struct filename *oldpath;
struct filename *newpath;
int flags;
};
struct io_completion { struct io_completion {
struct file *file; struct file *file;
u32 cflags; u32 cflags;
...@@ -826,6 +849,9 @@ struct io_kiocb { ...@@ -826,6 +849,9 @@ struct io_kiocb {
struct io_shutdown shutdown; struct io_shutdown shutdown;
struct io_rename rename; struct io_rename rename;
struct io_unlink unlink; struct io_unlink unlink;
struct io_mkdir mkdir;
struct io_symlink symlink;
struct io_hardlink hardlink;
/* use only after cleaning per-op data, see io_clean_op() */ /* use only after cleaning per-op data, see io_clean_op() */
struct io_completion compl; struct io_completion compl;
}; };
...@@ -1037,6 +1063,9 @@ static const struct io_op_def io_op_defs[] = { ...@@ -1037,6 +1063,9 @@ static const struct io_op_def io_op_defs[] = {
}, },
[IORING_OP_RENAMEAT] = {}, [IORING_OP_RENAMEAT] = {},
[IORING_OP_UNLINKAT] = {}, [IORING_OP_UNLINKAT] = {},
[IORING_OP_MKDIRAT] = {},
[IORING_OP_SYMLINKAT] = {},
[IORING_OP_LINKAT] = {},
}; };
/* requests with any of those set should undergo io_disarm_next() */ /* requests with any of those set should undergo io_disarm_next() */
...@@ -3680,6 +3709,149 @@ static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) ...@@ -3680,6 +3709,149 @@ static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
return 0; return 0;
} }
static int io_mkdirat_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
struct io_mkdir *mkd = &req->mkdir;
const char __user *fname;
if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
return -EINVAL;
if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
sqe->splice_fd_in)
return -EINVAL;
if (unlikely(req->flags & REQ_F_FIXED_FILE))
return -EBADF;
mkd->dfd = READ_ONCE(sqe->fd);
mkd->mode = READ_ONCE(sqe->len);
fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
mkd->filename = getname(fname);
if (IS_ERR(mkd->filename))
return PTR_ERR(mkd->filename);
req->flags |= REQ_F_NEED_CLEANUP;
return 0;
}
static int io_mkdirat(struct io_kiocb *req, int issue_flags)
{
struct io_mkdir *mkd = &req->mkdir;
int ret;
if (issue_flags & IO_URING_F_NONBLOCK)
return -EAGAIN;
ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
req->flags &= ~REQ_F_NEED_CLEANUP;
if (ret < 0)
req_set_fail(req);
io_req_complete(req, ret);
return 0;
}
static int io_symlinkat_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
struct io_symlink *sl = &req->symlink;
const char __user *oldpath, *newpath;
if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
return -EINVAL;
if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
sqe->splice_fd_in)
return -EINVAL;
if (unlikely(req->flags & REQ_F_FIXED_FILE))
return -EBADF;
sl->new_dfd = READ_ONCE(sqe->fd);
oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
sl->oldpath = getname(oldpath);
if (IS_ERR(sl->oldpath))
return PTR_ERR(sl->oldpath);
sl->newpath = getname(newpath);
if (IS_ERR(sl->newpath)) {
putname(sl->oldpath);
return PTR_ERR(sl->newpath);
}
req->flags |= REQ_F_NEED_CLEANUP;
return 0;
}
static int io_symlinkat(struct io_kiocb *req, int issue_flags)
{
struct io_symlink *sl = &req->symlink;
int ret;
if (issue_flags & IO_URING_F_NONBLOCK)
return -EAGAIN;
ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
req->flags &= ~REQ_F_NEED_CLEANUP;
if (ret < 0)
req_set_fail(req);
io_req_complete(req, ret);
return 0;
}
static int io_linkat_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
struct io_hardlink *lnk = &req->hardlink;
const char __user *oldf, *newf;
if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
return -EINVAL;
if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
return -EINVAL;
if (unlikely(req->flags & REQ_F_FIXED_FILE))
return -EBADF;
lnk->old_dfd = READ_ONCE(sqe->fd);
lnk->new_dfd = READ_ONCE(sqe->len);
oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
lnk->flags = READ_ONCE(sqe->hardlink_flags);
lnk->oldpath = getname(oldf);
if (IS_ERR(lnk->oldpath))
return PTR_ERR(lnk->oldpath);
lnk->newpath = getname(newf);
if (IS_ERR(lnk->newpath)) {
putname(lnk->oldpath);
return PTR_ERR(lnk->newpath);
}
req->flags |= REQ_F_NEED_CLEANUP;
return 0;
}
static int io_linkat(struct io_kiocb *req, int issue_flags)
{
struct io_hardlink *lnk = &req->hardlink;
int ret;
if (issue_flags & IO_URING_F_NONBLOCK)
return -EAGAIN;
ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
lnk->newpath, lnk->flags);
req->flags &= ~REQ_F_NEED_CLEANUP;
if (ret < 0)
req_set_fail(req);
io_req_complete(req, ret);
return 0;
}
static int io_shutdown_prep(struct io_kiocb *req, static int io_shutdown_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe) const struct io_uring_sqe *sqe)
{ {
...@@ -6169,6 +6341,12 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) ...@@ -6169,6 +6341,12 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return io_renameat_prep(req, sqe); return io_renameat_prep(req, sqe);
case IORING_OP_UNLINKAT: case IORING_OP_UNLINKAT:
return io_unlinkat_prep(req, sqe); return io_unlinkat_prep(req, sqe);
case IORING_OP_MKDIRAT:
return io_mkdirat_prep(req, sqe);
case IORING_OP_SYMLINKAT:
return io_symlinkat_prep(req, sqe);
case IORING_OP_LINKAT:
return io_linkat_prep(req, sqe);
} }
printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
...@@ -6332,6 +6510,17 @@ static void io_clean_op(struct io_kiocb *req) ...@@ -6332,6 +6510,17 @@ static void io_clean_op(struct io_kiocb *req)
case IORING_OP_UNLINKAT: case IORING_OP_UNLINKAT:
putname(req->unlink.filename); putname(req->unlink.filename);
break; break;
case IORING_OP_MKDIRAT:
putname(req->mkdir.filename);
break;
case IORING_OP_SYMLINKAT:
putname(req->symlink.oldpath);
putname(req->symlink.newpath);
break;
case IORING_OP_LINKAT:
putname(req->hardlink.oldpath);
putname(req->hardlink.newpath);
break;
} }
} }
if ((req->flags & REQ_F_POLLED) && req->apoll) { if ((req->flags & REQ_F_POLLED) && req->apoll) {
...@@ -6460,6 +6649,15 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) ...@@ -6460,6 +6649,15 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
case IORING_OP_UNLINKAT: case IORING_OP_UNLINKAT:
ret = io_unlinkat(req, issue_flags); ret = io_unlinkat(req, issue_flags);
break; break;
case IORING_OP_MKDIRAT:
ret = io_mkdirat(req, issue_flags);
break;
case IORING_OP_SYMLINKAT:
ret = io_symlinkat(req, issue_flags);
break;
case IORING_OP_LINKAT:
ret = io_linkat(req, issue_flags);
break;
default: default:
ret = -EINVAL; ret = -EINVAL;
break; break;
......
...@@ -203,6 +203,14 @@ getname_flags(const char __user *filename, int flags, int *empty) ...@@ -203,6 +203,14 @@ getname_flags(const char __user *filename, int flags, int *empty)
return result; 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 * struct filename *
getname(const char __user * filename) getname(const char __user * filename)
{ {
...@@ -247,6 +255,9 @@ getname_kernel(const char * filename) ...@@ -247,6 +255,9 @@ getname_kernel(const char * filename)
void putname(struct filename *name) void putname(struct filename *name)
{ {
if (IS_ERR_OR_NULL(name))
return;
BUG_ON(name->refcnt <= 0); BUG_ON(name->refcnt <= 0);
if (--name->refcnt > 0) if (--name->refcnt > 0)
...@@ -2456,7 +2467,7 @@ static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path ...@@ -2456,7 +2467,7 @@ static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path
return err; 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) struct path *path, struct path *root)
{ {
int retval; int retval;
...@@ -2474,6 +2485,14 @@ int filename_lookup(int dfd, struct filename *name, unsigned flags, ...@@ -2474,6 +2485,14 @@ int filename_lookup(int dfd, struct filename *name, unsigned flags,
audit_inode(name, path->dentry, audit_inode(name, path->dentry,
flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
restore_nameidata(); 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); putname(name);
return retval; return retval;
} }
...@@ -2495,7 +2514,7 @@ static int path_parentat(struct nameidata *nd, unsigned flags, ...@@ -2495,7 +2514,7 @@ static int path_parentat(struct nameidata *nd, unsigned flags,
return err; return err;
} }
static struct filename *filename_parentat(int dfd, struct filename *name, static int __filename_parentat(int dfd, struct filename *name,
unsigned int flags, struct path *parent, unsigned int flags, struct path *parent,
struct qstr *last, int *type) struct qstr *last, int *type)
{ {
...@@ -2503,7 +2522,7 @@ static struct filename *filename_parentat(int dfd, struct filename *name, ...@@ -2503,7 +2522,7 @@ static struct filename *filename_parentat(int dfd, struct filename *name,
struct nameidata nd; struct nameidata nd;
if (IS_ERR(name)) if (IS_ERR(name))
return name; return PTR_ERR(name);
set_nameidata(&nd, dfd, name, NULL); set_nameidata(&nd, dfd, name, NULL);
retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
if (unlikely(retval == -ECHILD)) if (unlikely(retval == -ECHILD))
...@@ -2514,29 +2533,34 @@ static struct filename *filename_parentat(int dfd, struct filename *name, ...@@ -2514,29 +2533,34 @@ static struct filename *filename_parentat(int dfd, struct filename *name,
*last = nd.last; *last = nd.last;
*type = nd.last_type; *type = nd.last_type;
audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
} else {
putname(name);
name = ERR_PTR(retval);
} }
restore_nameidata(); restore_nameidata();
return name; return retval;
}
static int filename_parentat(int dfd, struct filename *name,
unsigned int flags, struct path *parent,
struct qstr *last, int *type)
{
int retval = __filename_parentat(dfd, name, flags, parent, last, type);
putname(name);
return retval;
} }
/* does lookup, returns the object with parent locked */ /* does lookup, returns the object with parent locked */
struct dentry *kern_path_locked(const char *name, struct path *path) struct dentry *kern_path_locked(const char *name, struct path *path)
{ {
struct filename *filename;
struct dentry *d; struct dentry *d;
struct qstr last; struct qstr last;
int type; int type, error;
filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, error = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
&last, &type); &last, &type);
if (IS_ERR(filename)) if (error)
return ERR_CAST(filename); return ERR_PTR(error);
if (unlikely(type != LAST_NORM)) { if (unlikely(type != LAST_NORM)) {
path_put(path); path_put(path);
putname(filename);
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
...@@ -2545,7 +2569,6 @@ struct dentry *kern_path_locked(const char *name, struct path *path) ...@@ -2545,7 +2569,6 @@ struct dentry *kern_path_locked(const char *name, struct path *path)
inode_unlock(path->dentry->d_inode); inode_unlock(path->dentry->d_inode);
path_put(path); path_put(path);
} }
putname(filename);
return d; return d;
} }
...@@ -3564,7 +3587,7 @@ struct file *do_file_open_root(const struct path *root, ...@@ -3564,7 +3587,7 @@ struct file *do_file_open_root(const struct path *root,
return file; return file;
} }
static struct dentry *filename_create(int dfd, struct filename *name, static struct dentry *__filename_create(int dfd, struct filename *name,
struct path *path, unsigned int lookup_flags) struct path *path, unsigned int lookup_flags)
{ {
struct dentry *dentry = ERR_PTR(-EEXIST); struct dentry *dentry = ERR_PTR(-EEXIST);
...@@ -3580,9 +3603,9 @@ static struct dentry *filename_create(int dfd, struct filename *name, ...@@ -3580,9 +3603,9 @@ static struct dentry *filename_create(int dfd, struct filename *name,
*/ */
lookup_flags &= LOOKUP_REVAL; lookup_flags &= LOOKUP_REVAL;
name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); error = __filename_parentat(dfd, name, lookup_flags, path, &last, &type);
if (IS_ERR(name)) if (error)
return ERR_CAST(name); return ERR_PTR(error);
/* /*
* Yucky last component or no last component at all? * Yucky last component or no last component at all?
...@@ -3620,7 +3643,6 @@ static struct dentry *filename_create(int dfd, struct filename *name, ...@@ -3620,7 +3643,6 @@ static struct dentry *filename_create(int dfd, struct filename *name,
error = err2; error = err2;
goto fail; goto fail;
} }
putname(name);
return dentry; return dentry;
fail: fail:
dput(dentry); dput(dentry);
...@@ -3631,10 +3653,18 @@ static struct dentry *filename_create(int dfd, struct filename *name, ...@@ -3631,10 +3653,18 @@ static struct dentry *filename_create(int dfd, struct filename *name,
mnt_drop_write(path->mnt); mnt_drop_write(path->mnt);
out: out:
path_put(path); path_put(path);
putname(name);
return dentry; return dentry;
} }
static inline struct dentry *filename_create(int dfd, struct filename *name,
struct path *path, unsigned int lookup_flags)
{
struct dentry *res = __filename_create(dfd, name, path, lookup_flags);
putname(name);
return res;
}
struct dentry *kern_path_create(int dfd, const char *pathname, struct dentry *kern_path_create(int dfd, const char *pathname,
struct path *path, unsigned int lookup_flags) struct path *path, unsigned int lookup_flags)
{ {
...@@ -3723,7 +3753,7 @@ static int may_mknod(umode_t mode) ...@@ -3723,7 +3753,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) unsigned int dev)
{ {
struct user_namespace *mnt_userns; struct user_namespace *mnt_userns;
...@@ -3734,17 +3764,18 @@ static long do_mknodat(int dfd, const char __user *filename, umode_t mode, ...@@ -3734,17 +3764,18 @@ static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
error = may_mknod(mode); error = may_mknod(mode);
if (error) if (error)
return error; goto out1;
retry: 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)) if (IS_ERR(dentry))
return PTR_ERR(dentry); goto out1;
if (!IS_POSIXACL(path.dentry->d_inode)) if (!IS_POSIXACL(path.dentry->d_inode))
mode &= ~current_umask(); mode &= ~current_umask();
error = security_path_mknod(&path, dentry, mode, dev); error = security_path_mknod(&path, dentry, mode, dev);
if (error) if (error)
goto out; goto out2;
mnt_userns = mnt_user_ns(path.mnt); mnt_userns = mnt_user_ns(path.mnt);
switch (mode & S_IFMT) { switch (mode & S_IFMT) {
...@@ -3763,24 +3794,26 @@ static long do_mknodat(int dfd, const char __user *filename, umode_t mode, ...@@ -3763,24 +3794,26 @@ static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
dentry, mode, 0); dentry, mode, 0);
break; break;
} }
out: out2:
done_path_create(&path, dentry); done_path_create(&path, dentry);
if (retry_estale(error, lookup_flags)) { if (retry_estale(error, lookup_flags)) {
lookup_flags |= LOOKUP_REVAL; lookup_flags |= LOOKUP_REVAL;
goto retry; goto retry;
} }
out1:
putname(name);
return error; return error;
} }
SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
unsigned int, dev) 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) 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);
} }
/** /**
...@@ -3825,7 +3858,7 @@ int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, ...@@ -3825,7 +3858,7 @@ int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
} }
EXPORT_SYMBOL(vfs_mkdir); EXPORT_SYMBOL(vfs_mkdir);
static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) int do_mkdirat(int dfd, struct filename *name, umode_t mode)
{ {
struct dentry *dentry; struct dentry *dentry;
struct path path; struct path path;
...@@ -3833,9 +3866,10 @@ static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) ...@@ -3833,9 +3866,10 @@ static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
unsigned int lookup_flags = LOOKUP_DIRECTORY; unsigned int lookup_flags = LOOKUP_DIRECTORY;
retry: retry:
dentry = user_path_create(dfd, pathname, &path, lookup_flags); dentry = __filename_create(dfd, name, &path, lookup_flags);
error = PTR_ERR(dentry);
if (IS_ERR(dentry)) if (IS_ERR(dentry))
return PTR_ERR(dentry); goto out_putname;
if (!IS_POSIXACL(path.dentry->d_inode)) if (!IS_POSIXACL(path.dentry->d_inode))
mode &= ~current_umask(); mode &= ~current_umask();
...@@ -3851,17 +3885,19 @@ static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) ...@@ -3851,17 +3885,19 @@ static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
lookup_flags |= LOOKUP_REVAL; lookup_flags |= LOOKUP_REVAL;
goto retry; goto retry;
} }
out_putname:
putname(name);
return error; return error;
} }
SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
{ {
return do_mkdirat(dfd, pathname, mode); return do_mkdirat(dfd, getname(pathname), mode);
} }
SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
{ {
return do_mkdirat(AT_FDCWD, pathname, mode); return do_mkdirat(AT_FDCWD, getname(pathname), mode);
} }
/** /**
...@@ -3919,62 +3955,62 @@ int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir, ...@@ -3919,62 +3955,62 @@ int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
} }
EXPORT_SYMBOL(vfs_rmdir); 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; struct user_namespace *mnt_userns;
int error = 0; int error;
struct dentry *dentry; struct dentry *dentry;
struct path path; struct path path;
struct qstr last; struct qstr last;
int type; int type;
unsigned int lookup_flags = 0; unsigned int lookup_flags = 0;
retry: retry:
name = filename_parentat(dfd, name, lookup_flags, error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
&path, &last, &type); if (error)
if (IS_ERR(name)) goto exit1;
return PTR_ERR(name);
switch (type) { switch (type) {
case LAST_DOTDOT: case LAST_DOTDOT:
error = -ENOTEMPTY; error = -ENOTEMPTY;
goto exit1; goto exit2;
case LAST_DOT: case LAST_DOT:
error = -EINVAL; error = -EINVAL;
goto exit1; goto exit2;
case LAST_ROOT: case LAST_ROOT:
error = -EBUSY; error = -EBUSY;
goto exit1; goto exit2;
} }
error = mnt_want_write(path.mnt); error = mnt_want_write(path.mnt);
if (error) if (error)
goto exit1; goto exit2;
inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
dentry = __lookup_hash(&last, path.dentry, lookup_flags); dentry = __lookup_hash(&last, path.dentry, lookup_flags);
error = PTR_ERR(dentry); error = PTR_ERR(dentry);
if (IS_ERR(dentry)) if (IS_ERR(dentry))
goto exit2; goto exit3;
if (!dentry->d_inode) { if (!dentry->d_inode) {
error = -ENOENT; error = -ENOENT;
goto exit3; goto exit4;
} }
error = security_path_rmdir(&path, dentry); error = security_path_rmdir(&path, dentry);
if (error) if (error)
goto exit3; goto exit4;
mnt_userns = mnt_user_ns(path.mnt); mnt_userns = mnt_user_ns(path.mnt);
error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry); error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
exit3: exit4:
dput(dentry); dput(dentry);
exit2: exit3:
inode_unlock(path.dentry->d_inode); inode_unlock(path.dentry->d_inode);
mnt_drop_write(path.mnt); mnt_drop_write(path.mnt);
exit1: exit2:
path_put(&path); path_put(&path);
if (retry_estale(error, lookup_flags)) { if (retry_estale(error, lookup_flags)) {
lookup_flags |= LOOKUP_REVAL; lookup_flags |= LOOKUP_REVAL;
goto retry; goto retry;
} }
exit1:
putname(name); putname(name);
return error; return error;
} }
...@@ -4057,7 +4093,7 @@ EXPORT_SYMBOL(vfs_unlink); ...@@ -4057,7 +4093,7 @@ EXPORT_SYMBOL(vfs_unlink);
* writeout happening, and we don't want to prevent access to the directory * writeout happening, and we don't want to prevent access to the directory
* while waiting on the I/O. * while waiting on the I/O.
*/ */
long do_unlinkat(int dfd, struct filename *name) int do_unlinkat(int dfd, struct filename *name)
{ {
int error; int error;
struct dentry *dentry; struct dentry *dentry;
...@@ -4068,17 +4104,17 @@ long do_unlinkat(int dfd, struct filename *name) ...@@ -4068,17 +4104,17 @@ long do_unlinkat(int dfd, struct filename *name)
struct inode *delegated_inode = NULL; struct inode *delegated_inode = NULL;
unsigned int lookup_flags = 0; unsigned int lookup_flags = 0;
retry: retry:
name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); error = __filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
if (IS_ERR(name)) if (error)
return PTR_ERR(name); goto exit1;
error = -EISDIR; error = -EISDIR;
if (type != LAST_NORM) if (type != LAST_NORM)
goto exit1; goto exit2;
error = mnt_want_write(path.mnt); error = mnt_want_write(path.mnt);
if (error) if (error)
goto exit1; goto exit2;
retry_deleg: retry_deleg:
inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
dentry = __lookup_hash(&last, path.dentry, lookup_flags); dentry = __lookup_hash(&last, path.dentry, lookup_flags);
...@@ -4095,11 +4131,11 @@ long do_unlinkat(int dfd, struct filename *name) ...@@ -4095,11 +4131,11 @@ long do_unlinkat(int dfd, struct filename *name)
ihold(inode); ihold(inode);
error = security_path_unlink(&path, dentry); error = security_path_unlink(&path, dentry);
if (error) if (error)
goto exit2; goto exit3;
mnt_userns = mnt_user_ns(path.mnt); mnt_userns = mnt_user_ns(path.mnt);
error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry, error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
&delegated_inode); &delegated_inode);
exit2: exit3:
dput(dentry); dput(dentry);
} }
inode_unlock(path.dentry->d_inode); inode_unlock(path.dentry->d_inode);
...@@ -4112,13 +4148,14 @@ long do_unlinkat(int dfd, struct filename *name) ...@@ -4112,13 +4148,14 @@ long do_unlinkat(int dfd, struct filename *name)
goto retry_deleg; goto retry_deleg;
} }
mnt_drop_write(path.mnt); mnt_drop_write(path.mnt);
exit1: exit2:
path_put(&path); path_put(&path);
if (retry_estale(error, lookup_flags)) { if (retry_estale(error, lookup_flags)) {
lookup_flags |= LOOKUP_REVAL; lookup_flags |= LOOKUP_REVAL;
inode = NULL; inode = NULL;
goto retry; goto retry;
} }
exit1:
putname(name); putname(name);
return error; return error;
...@@ -4129,7 +4166,7 @@ long do_unlinkat(int dfd, struct filename *name) ...@@ -4129,7 +4166,7 @@ long do_unlinkat(int dfd, struct filename *name)
error = -EISDIR; error = -EISDIR;
else else
error = -ENOTDIR; error = -ENOTDIR;
goto exit2; goto exit3;
} }
SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
...@@ -4184,23 +4221,22 @@ int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, ...@@ -4184,23 +4221,22 @@ int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
} }
EXPORT_SYMBOL(vfs_symlink); EXPORT_SYMBOL(vfs_symlink);
static long do_symlinkat(const char __user *oldname, int newdfd, int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
const char __user *newname)
{ {
int error; int error;
struct filename *from;
struct dentry *dentry; struct dentry *dentry;
struct path path; struct path path;
unsigned int lookup_flags = 0; unsigned int lookup_flags = 0;
from = getname(oldname); if (IS_ERR(from)) {
if (IS_ERR(from)) error = PTR_ERR(from);
return PTR_ERR(from); goto out_putnames;
}
retry: retry:
dentry = user_path_create(newdfd, newname, &path, lookup_flags); dentry = __filename_create(newdfd, to, &path, lookup_flags);
error = PTR_ERR(dentry); error = PTR_ERR(dentry);
if (IS_ERR(dentry)) if (IS_ERR(dentry))
goto out_putname; goto out_putnames;
error = security_path_symlink(&path, dentry, from->name); error = security_path_symlink(&path, dentry, from->name);
if (!error) { if (!error) {
...@@ -4215,7 +4251,8 @@ static long do_symlinkat(const char __user *oldname, int newdfd, ...@@ -4215,7 +4251,8 @@ static long do_symlinkat(const char __user *oldname, int newdfd,
lookup_flags |= LOOKUP_REVAL; lookup_flags |= LOOKUP_REVAL;
goto retry; goto retry;
} }
out_putname: out_putnames:
putname(to);
putname(from); putname(from);
return error; return error;
} }
...@@ -4223,12 +4260,12 @@ static long do_symlinkat(const char __user *oldname, int newdfd, ...@@ -4223,12 +4260,12 @@ static long do_symlinkat(const char __user *oldname, int newdfd,
SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
int, newdfd, const char __user *, newname) 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) 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));
} }
/** /**
...@@ -4329,8 +4366,8 @@ EXPORT_SYMBOL(vfs_link); ...@@ -4329,8 +4366,8 @@ EXPORT_SYMBOL(vfs_link);
* with linux 2.0, and to avoid hard-linking to directories * with linux 2.0, and to avoid hard-linking to directories
* and other special files. --ADM * and other special files. --ADM
*/ */
static int do_linkat(int olddfd, const char __user *oldname, int newdfd, int do_linkat(int olddfd, struct filename *old, int newdfd,
const char __user *newname, int flags) struct filename *new, int flags)
{ {
struct user_namespace *mnt_userns; struct user_namespace *mnt_userns;
struct dentry *new_dentry; struct dentry *new_dentry;
...@@ -4339,31 +4376,32 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd, ...@@ -4339,31 +4376,32 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
int how = 0; int how = 0;
int error; int error;
if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
return -EINVAL; error = -EINVAL;
goto out_putnames;
}
/* /*
* To use null names we require CAP_DAC_READ_SEARCH * To use null names we require CAP_DAC_READ_SEARCH
* This ensures that not everyone will be able to create * This ensures that not everyone will be able to create
* handlink using the passed filedescriptor. * handlink using the passed filedescriptor.
*/ */
if (flags & AT_EMPTY_PATH) { if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
if (!capable(CAP_DAC_READ_SEARCH)) error = -ENOENT;
return -ENOENT; goto out_putnames;
how = LOOKUP_EMPTY;
} }
if (flags & AT_SYMLINK_FOLLOW) if (flags & AT_SYMLINK_FOLLOW)
how |= LOOKUP_FOLLOW; how |= LOOKUP_FOLLOW;
retry: retry:
error = user_path_at(olddfd, oldname, how, &old_path); error = __filename_lookup(olddfd, old, how, &old_path, NULL);
if (error) 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)); (how & LOOKUP_REVAL));
error = PTR_ERR(new_dentry); error = PTR_ERR(new_dentry);
if (IS_ERR(new_dentry)) if (IS_ERR(new_dentry))
goto out; goto out_putpath;
error = -EXDEV; error = -EXDEV;
if (old_path.mnt != new_path.mnt) if (old_path.mnt != new_path.mnt)
...@@ -4391,8 +4429,11 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd, ...@@ -4391,8 +4429,11 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
how |= LOOKUP_REVAL; how |= LOOKUP_REVAL;
goto retry; goto retry;
} }
out: out_putpath:
path_put(&old_path); path_put(&old_path);
out_putnames:
putname(old);
putname(new);
return error; return error;
} }
...@@ -4400,12 +4441,13 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd, ...@@ -4400,12 +4441,13 @@ static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
int, newdfd, const char __user *, newname, int, flags) 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) 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);
} }
/** /**
...@@ -4600,29 +4642,25 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd, ...@@ -4600,29 +4642,25 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
int error = -EINVAL; int error = -EINVAL;
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
goto put_both; goto put_names;
if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
(flags & RENAME_EXCHANGE)) (flags & RENAME_EXCHANGE))
goto put_both; goto put_names;
if (flags & RENAME_EXCHANGE) if (flags & RENAME_EXCHANGE)
target_flags = 0; target_flags = 0;
retry: retry:
from = filename_parentat(olddfd, from, lookup_flags, &old_path, error = __filename_parentat(olddfd, from, lookup_flags, &old_path,
&old_last, &old_type); &old_last, &old_type);
if (IS_ERR(from)) { if (error)
error = PTR_ERR(from); goto put_names;
goto put_new;
}
to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, error = __filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
&new_type); &new_type);
if (IS_ERR(to)) { if (error)
error = PTR_ERR(to);
goto exit1; goto exit1;
}
error = -EXDEV; error = -EXDEV;
if (old_path.mnt != new_path.mnt) if (old_path.mnt != new_path.mnt)
...@@ -4725,12 +4763,9 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd, ...@@ -4725,12 +4763,9 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
lookup_flags |= LOOKUP_REVAL; lookup_flags |= LOOKUP_REVAL;
goto retry; goto retry;
} }
put_both: put_names:
if (!IS_ERR(from)) putname(from);
putname(from); putname(to);
put_new:
if (!IS_ERR(to))
putname(to);
return error; return error;
} }
......
...@@ -2746,6 +2746,7 @@ static inline struct file *file_clone_open(struct file *file) ...@@ -2746,6 +2746,7 @@ static inline struct file *file_clone_open(struct file *file)
extern int filp_close(struct file *, fl_owner_t id); extern int filp_close(struct file *, fl_owner_t id);
extern struct filename *getname_flags(const char __user *, int, int *); extern struct filename *getname_flags(const char __user *, int, int *);
extern struct filename *getname_uflags(const char __user *, int);
extern struct filename *getname(const char __user *); extern struct filename *getname(const char __user *);
extern struct filename *getname_kernel(const char *); extern struct filename *getname_kernel(const char *);
extern void putname(struct filename *name); extern void putname(struct filename *name);
......
...@@ -44,6 +44,7 @@ struct io_uring_sqe { ...@@ -44,6 +44,7 @@ struct io_uring_sqe {
__u32 splice_flags; __u32 splice_flags;
__u32 rename_flags; __u32 rename_flags;
__u32 unlink_flags; __u32 unlink_flags;
__u32 hardlink_flags;
}; };
__u64 user_data; /* data to be passed back at completion time */ __u64 user_data; /* data to be passed back at completion time */
/* pack this to avoid bogus arm OABI complaints */ /* pack this to avoid bogus arm OABI complaints */
...@@ -136,6 +137,9 @@ enum { ...@@ -136,6 +137,9 @@ enum {
IORING_OP_SHUTDOWN, IORING_OP_SHUTDOWN,
IORING_OP_RENAMEAT, IORING_OP_RENAMEAT,
IORING_OP_UNLINKAT, IORING_OP_UNLINKAT,
IORING_OP_MKDIRAT,
IORING_OP_SYMLINKAT,
IORING_OP_LINKAT,
/* this goes last, obviously */ /* this goes last, obviously */
IORING_OP_LAST, IORING_OP_LAST,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment