io_uring: fix filename leak in __io_openat_prep()
Subsystems (3)
PATCH 1/1 io_uring: fix filename leak in __io_openat_prep()
__io_openat_prep() allocates a struct filename using getname(), but it isn't freed in case the present file is installed in the fixed file table and simultaneously, it has the flag O_CLOEXEC set in the open->how.flags field.
This is an erroneous condition, since for a file installed in the fixed file table, it won't be installed in the normal file table, due to which the file cannot support close on exec. Earlier, the code just returned -EINVAL error code for this condition, however, the memory allocated for that struct filename wasn't freed, resulting in a memory leak.
Hence, the case of file being installed in the fixed file table as well as having O_CLOEXEC flag in open->how.flags set, is adressed by using putname() to release the memory allocated to the struct filename, then setting the field open->filename to NULL, and after that, returning -EINVAL.
--- io_uring/openclose.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
io_uring/openclose.c +4 -1
@@ -75,8 +75,11 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe } open->file_slot =READ_ONCE (sqe->file_index); -if (open->file_slot && (open->how.flags &O_CLOEXEC )) +if (open->file_slot && (open->how.flags &O_CLOEXEC )) { +putname (open->filename); + open->filename =NULL ;return -EINVAL ; + } open->nofile =rlimit (RLIMIT_NOFILE ); req->flags |=REQ_F_NEED_CLEANUP ;
base-commit: b927546677c876e26eba308550207c2ddf812a43
Signature · 1 line
2.34.1
Jens Axboe
maintainerQuoted 33 lines · Prithvi Tambewagh
__io_openat_prep()allocates a struct filename usinggetname(), but it isn't freed in case the present file is installed in the fixed file table and simultaneously, it has the flagO_CLOEXECset in theopen->how.flagsfield.This is an erroneous condition, since for a file installed in the fixed file table, it won't be installed in the normal file table, due to which the file cannot support close on exec. Earlier, the code just returned
-EINVALerror code for this condition, however, the memory allocated for that struct filename wasn't freed, resulting in a memory leak.Hence, the case of file being installed in the fixed file table as well as having
O_CLOEXECflag inopen->how.flagsset, is adressed by usingputname()to release the memory allocated to the struct filename, then setting the fieldopen->filenameto NULL, and after that, returning-EINVAL.Reported-by [email protected]Tested-by [email protected]Signed-off-by Prithvi Tambewagh <[email protected]>--- io_uring/openclose.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)io_uring/openclose.c +0
-0@@ -75,8 +75,11 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe }
io_uring/openclose.c static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe · line 85
Quoted 10 lines
io_uring/openclose.c +4
-1open->file_slot =READ_ONCE (sqe->file_index); -if (open->file_slot && (open->how.flags &O_CLOEXEC )) +if (open->file_slot && (open->how.flags &O_CLOEXEC )) { +putname (open->filename); + open->filename =NULL ;return -EINVAL ; + } open->nofile =rlimit (RLIMIT_NOFILE ); req->flags |=REQ_F_NEED_CLEANUP ;
You can probably fix it similarly by just having REQ_F_NEED_CLEANUP set earlier in the process, then everything that needs undoing will get undone as part of ending the request.
Signature · 1 line
Jens Axboe
Quoted 42 lines · Jens Axboe
Quoted 41 lines · Prithvi Tambewagh
__io_openat_prep()allocates a struct filename usinggetname(), but it isn't freed in case the present file is installed in the fixed file table and simultaneously, it has the flagO_CLOEXECset in theopen->how.flagsfield.This is an erroneous condition, since for a file installed in the fixed file table, it won't be installed in the normal file table, due to which the file cannot support close on exec. Earlier, the code just returned
-EINVALerror code for this condition, however, the memory allocated for that struct filename wasn't freed, resulting in a memory leak.Hence, the case of file being installed in the fixed file table as well as having
O_CLOEXECflag inopen->how.flagsset, is adressed by usingputname()to release the memory allocated to the struct filename, then setting the fieldopen->filenameto NULL, and after that, returning-EINVAL.Reported-by [email protected]Tested-by [email protected]Signed-off-by Prithvi Tambewagh <[email protected]>--- io_uring/openclose.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)io_uring/openclose.c +4
-1@@ -75,8 +75,11 @@ static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe } open->file_slot =READ_ONCE (sqe->file_index); -if (open->file_slot && (open->how.flags &O_CLOEXEC )) +if (open->file_slot && (open->how.flags &O_CLOEXEC )) { +putname (open->filename); + open->filename =NULL ;return -EINVAL ; + }
Quoted 9 lines
io_uring/openclose.c static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe · line 85
Quoted 2 lines
io_uring/openclose.c +0
-0open->nofile =rlimit (RLIMIT_NOFILE ); req->flags |=REQ_F_NEED_CLEANUP ;You can probably fix it similarly by just having
REQ_F_NEED_CLEANUPset earlier in the process, then everything that needs undoing will get undone as part of ending the request.Signature · 1 line
Jens Axboe
Sure, I will send v2 patch incorporating this change. Thanks for the feedback!
Best Regards,
Prithvi