Skip to contentSkip to conversation

io_uring: check file_slot early when accept use fix_file mode

PATCH In review Hao Xu 2 participants 6 messages Sep 7, 2021

Subsystems (2)
lore
Share & tools
lore

Hao Xu

author
#0 lore raw

PATCH 1/1 io_uring: check file_slot early when accept use fix_file mode

check file_slot early in io_accept_prep() to avoid wasted effort in failure cases.

---
 fs/io_uring.c | 2 ++
 1 file changed, 2 insertions(+)
fs/io_uring.c +2 -0
index 30d959416eba..917271bd80c5 100644
@@ -4868,6 +4868,8 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 	accept->nofile = rlimit(RLIMIT_NOFILE);
 
 	accept->file_slot = READ_ONCE(sqe->file_index);
+	if (accept->file_slot - 1 >= req->ctx->nr_user_files)
+		return -EINVAL;
 	if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
 				  (accept->flags & SOCK_CLOEXEC)))
 		return -EINVAL;
Signature · 1 line
2.24.4
maintainer
#1 lore raw
Quoted 2 lines · Hao Xu

check file_slot early in io_accept_prep() to avoid wasted effort in failure cases.

It's generally better to just let the failure cases deal with it instead of having checks in multiple places. This is a failure path, so we don't care about making it fail early. Optimizations should be for the hot path, which is not a malformed sqe.

Signature · 1 line
Jens Axboe
author
#2 lore raw
Quoted 8 lines · Jens Axboe
Quoted 2 lines · Hao Xu

check file_slot early in io_accept_prep() to avoid wasted effort in failure cases.

It's generally better to just let the failure cases deal with it instead of having checks in multiple places. This is a failure path, so we don't care about making it fail early. Optimizations should be for the hot path, which is not a malformed sqe.

I have a question here: if we do do_accept() and but fail in io_install_fixed_file(), do we lose the conn_fd return by do_accept() forever?

Quoted 1 line
maintainer
#3 lore raw
Quoted 5 lines · Hao Xu
Quoted 4 lines · Jens Axboe
Quoted 2 lines · Hao Xu

check file_slot early in io_accept_prep() to avoid wasted effort in failure cases.

Quoted 7 lines
Quoted 4 lines

It's generally better to just let the failure cases deal with it instead of having checks in multiple places. This is a failure path, so we don't care about making it fail early. Optimizations should be for the hot path, which is not a malformed sqe.

I have a question here: if we do do_accept() and but fail in io_install_fixed_file(), do we lose the conn_fd return by do_accept() forever?

We do. The file is put and everything, so we're not leaking anything. But the actual connection is lost as the accept request failed.

Signature · 1 line
Jens Axboe
author
#4 lore raw
Quoted 10 lines · Jens Axboe
Quoted 9 lines · Hao Xu
Quoted 8 lines · Jens Axboe
Quoted 2 lines · Hao Xu

check file_slot early in io_accept_prep() to avoid wasted effort in failure cases.

It's generally better to just let the failure cases deal with it instead of having checks in multiple places. This is a failure path, so we don't care about making it fail early. Optimizations should be for the hot path, which is not a malformed sqe.

Quoted 6 lines
Quoted 3 lines

I have a question here: if we do do_accept() and but fail in io_install_fixed_file(), do we lose the conn_fd return by do_accept() forever?

We do. The file is put and everything, so we're not leaking anything. But the actual connection is lost as the accept request failed.

Does that cause any problem, since from client's perspective, connection is builded, and there is no way for the server to close it.

Quoted 1 line
maintainer
#5 lore raw
Quoted 11 lines · Hao Xu
Quoted 10 lines · Jens Axboe
Quoted 9 lines · Hao Xu
Quoted 8 lines · Jens Axboe
Quoted 2 lines · Hao Xu

check file_slot early in io_accept_prep() to avoid wasted effort in failure cases.

It's generally better to just let the failure cases deal with it instead of having checks in multiple places. This is a failure path, so we don't care about making it fail early. Optimizations should be for the hot path, which is not a malformed sqe.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I have a question here: if we do do_accept() and but fail in io_install_fixed_file(), do we lose the conn_fd return by do_accept() forever?

We do. The file is put and everything, so we're not leaking anything. But the actual connection is lost as the accept request failed.

Does that cause any problem, since from client's perspective, connection is builded, and there is no way for the server to close it.

Maybe? But it's a program error, it's asking to accept a connection at a slot which is invalid. It kind of gets to keep the pieces in that particular case.

Signature · 1 line
Jens Axboe