Skip to content

Commit 0158005

Browse files
author
Al Viro
committed
replace do_getxattr() with saner helpers.
similar to do_setxattr() in the previous commit... Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 66d7ac6 commit 0158005

File tree

3 files changed

+61
-66
lines changed

3 files changed

+61
-66
lines changed

fs/internal.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,9 @@ struct kernel_xattr_ctx {
280280
unsigned int flags;
281281
};
282282

283-
284-
ssize_t do_getxattr(struct mnt_idmap *idmap,
285-
struct dentry *d,
286-
struct kernel_xattr_ctx *ctx);
287-
283+
ssize_t file_getxattr(struct file *file, struct kernel_xattr_ctx *ctx);
284+
ssize_t filename_getxattr(int dfd, struct filename *filename,
285+
unsigned int lookup_flags, struct kernel_xattr_ctx *ctx);
288286
int file_setxattr(struct file *file, struct kernel_xattr_ctx *ctx);
289287
int filename_setxattr(int dfd, struct filename *filename,
290288
unsigned int lookup_flags, struct kernel_xattr_ctx *ctx);

fs/xattr.c

+51-37
Original file line numberDiff line numberDiff line change
@@ -744,80 +744,85 @@ SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
744744
/*
745745
* Extended attribute GET operations
746746
*/
747-
ssize_t
747+
static ssize_t
748748
do_getxattr(struct mnt_idmap *idmap, struct dentry *d,
749749
struct kernel_xattr_ctx *ctx)
750750
{
751751
ssize_t error;
752752
char *kname = ctx->kname->name;
753+
void *kvalue = NULL;
753754

754755
if (ctx->size) {
755756
if (ctx->size > XATTR_SIZE_MAX)
756757
ctx->size = XATTR_SIZE_MAX;
757-
ctx->kvalue = kvzalloc(ctx->size, GFP_KERNEL);
758-
if (!ctx->kvalue)
758+
kvalue = kvzalloc(ctx->size, GFP_KERNEL);
759+
if (!kvalue)
759760
return -ENOMEM;
760761
}
761762

762-
if (is_posix_acl_xattr(ctx->kname->name))
763-
error = do_get_acl(idmap, d, kname, ctx->kvalue, ctx->size);
763+
if (is_posix_acl_xattr(kname))
764+
error = do_get_acl(idmap, d, kname, kvalue, ctx->size);
764765
else
765-
error = vfs_getxattr(idmap, d, kname, ctx->kvalue, ctx->size);
766+
error = vfs_getxattr(idmap, d, kname, kvalue, ctx->size);
766767
if (error > 0) {
767-
if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))
768+
if (ctx->size && copy_to_user(ctx->value, kvalue, error))
768769
error = -EFAULT;
769770
} else if (error == -ERANGE && ctx->size >= XATTR_SIZE_MAX) {
770771
/* The file system tried to returned a value bigger
771772
than XATTR_SIZE_MAX bytes. Not possible. */
772773
error = -E2BIG;
773774
}
774775

776+
kvfree(kvalue);
775777
return error;
776778
}
777779

778-
static ssize_t
779-
getxattr(struct mnt_idmap *idmap, struct dentry *d,
780-
const char __user *name, void __user *value, size_t size)
780+
ssize_t file_getxattr(struct file *f, struct kernel_xattr_ctx *ctx)
781781
{
782-
ssize_t error;
783-
struct xattr_name kname;
784-
struct kernel_xattr_ctx ctx = {
785-
.value = value,
786-
.kvalue = NULL,
787-
.size = size,
788-
.kname = &kname,
789-
.flags = 0,
790-
};
791-
792-
error = import_xattr_name(&kname, name);
793-
if (error)
794-
return error;
795-
796-
error = do_getxattr(idmap, d, &ctx);
797-
798-
kvfree(ctx.kvalue);
799-
return error;
782+
audit_file(f);
783+
return do_getxattr(file_mnt_idmap(f), f->f_path.dentry, ctx);
800784
}
801785

802-
static ssize_t path_getxattr(const char __user *pathname,
803-
const char __user *name, void __user *value,
804-
size_t size, unsigned int lookup_flags)
786+
/* unconditionally consumes filename */
787+
ssize_t filename_getxattr(int dfd, struct filename *filename,
788+
unsigned int lookup_flags, struct kernel_xattr_ctx *ctx)
805789
{
806790
struct path path;
807791
ssize_t error;
808792
retry:
809-
error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
793+
error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
810794
if (error)
811-
return error;
812-
error = getxattr(mnt_idmap(path.mnt), path.dentry, name, value, size);
795+
goto out;
796+
error = do_getxattr(mnt_idmap(path.mnt), path.dentry, ctx);
813797
path_put(&path);
814798
if (retry_estale(error, lookup_flags)) {
815799
lookup_flags |= LOOKUP_REVAL;
816800
goto retry;
817801
}
802+
out:
803+
putname(filename);
818804
return error;
819805
}
820806

807+
static ssize_t path_getxattr(const char __user *pathname,
808+
const char __user *name, void __user *value,
809+
size_t size, unsigned int lookup_flags)
810+
{
811+
ssize_t error;
812+
struct xattr_name kname;
813+
struct kernel_xattr_ctx ctx = {
814+
.value = value,
815+
.size = size,
816+
.kname = &kname,
817+
.flags = 0,
818+
};
819+
820+
error = import_xattr_name(&kname, name);
821+
if (error)
822+
return error;
823+
return filename_getxattr(AT_FDCWD, getname(pathname), lookup_flags, &ctx);
824+
}
825+
821826
SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
822827
const char __user *, name, void __user *, value, size_t, size)
823828
{
@@ -833,13 +838,22 @@ SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
833838
SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
834839
void __user *, value, size_t, size)
835840
{
841+
ssize_t error;
842+
struct xattr_name kname;
843+
struct kernel_xattr_ctx ctx = {
844+
.value = value,
845+
.size = size,
846+
.kname = &kname,
847+
.flags = 0,
848+
};
836849
CLASS(fd, f)(fd);
837850

838851
if (fd_empty(f))
839852
return -EBADF;
840-
audit_file(fd_file(f));
841-
return getxattr(file_mnt_idmap(fd_file(f)), fd_file(f)->f_path.dentry,
842-
name, value, size);
853+
error = import_xattr_name(&kname, name);
854+
if (error)
855+
return error;
856+
return file_getxattr(fd_file(f), &ctx);
843857
}
844858

845859
/*

io_uring/xattr.c

+7-24
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int __io_getxattr_prep(struct io_kiocb *req,
5151
ix->filename = NULL;
5252
ix->ctx.kvalue = NULL;
5353
name = u64_to_user_ptr(READ_ONCE(sqe->addr));
54-
ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
54+
ix->ctx.value = u64_to_user_ptr(READ_ONCE(sqe->addr2));
5555
ix->ctx.size = READ_ONCE(sqe->len);
5656
ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
5757

@@ -94,12 +94,10 @@ int io_getxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
9494
path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
9595

9696
ix->filename = getname(path);
97-
if (IS_ERR(ix->filename)) {
98-
ret = PTR_ERR(ix->filename);
99-
ix->filename = NULL;
100-
}
97+
if (IS_ERR(ix->filename))
98+
return PTR_ERR(ix->filename);
10199

102-
return ret;
100+
return 0;
103101
}
104102

105103
int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags)
@@ -109,35 +107,20 @@ int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags)
109107

110108
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
111109

112-
ret = do_getxattr(file_mnt_idmap(req->file),
113-
req->file->f_path.dentry,
114-
&ix->ctx);
115-
110+
ret = file_getxattr(req->file, &ix->ctx);
116111
io_xattr_finish(req, ret);
117112
return IOU_OK;
118113
}
119114

120115
int io_getxattr(struct io_kiocb *req, unsigned int issue_flags)
121116
{
122117
struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
123-
unsigned int lookup_flags = LOOKUP_FOLLOW;
124-
struct path path;
125118
int ret;
126119

127120
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
128121

129-
retry:
130-
ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
131-
if (!ret) {
132-
ret = do_getxattr(mnt_idmap(path.mnt), path.dentry, &ix->ctx);
133-
134-
path_put(&path);
135-
if (retry_estale(ret, lookup_flags)) {
136-
lookup_flags |= LOOKUP_REVAL;
137-
goto retry;
138-
}
139-
}
140-
122+
ret = filename_getxattr(AT_FDCWD, ix->filename, LOOKUP_FOLLOW, &ix->ctx);
123+
ix->filename = NULL;
141124
io_xattr_finish(req, ret);
142125
return IOU_OK;
143126
}

0 commit comments

Comments
 (0)