Skip to contentSkip to conversation

ublk: reject FETCH from non-userspace context

PATCH In review Ming Lei 2 participants 6 messages May 1

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author maintainer
#0 lore raw

PATCH 1/1 ublk: reject FETCH from non-userspace context

__ublk_fetch() sets io->task to current, which is later checked against io_uring_cmd_get_task() in ublk_uring_cmd_cancel_fn(). With REQ_F_FORCE_ASYNC, the FETCH uring_cmd can be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN in cancel_fn.

Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of io_uring fallback.

Fixes 3421c7f68bba ("ublk: make sure io cmd handled in submitter task context")
---
 drivers/block/ublk_drv.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/block/ublk_drv.c +11 -4
index 8e5f3738c203..57abc0e9681f 100644
@@ -3251,12 +3251,19 @@ static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
 
 	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV);
 
-	ublk_fill_io_cmd(io, cmd);
-
-	if (ublk_dev_support_batch_io(ub))
+	if (ublk_dev_support_batch_io(ub)) {
 		WRITE_ONCE(io->task, NULL);
-	else
+	} else {
+		/*
+		 * FETCH must come from a real userspace task, not a
+		 * kworker is actually io_uring fallback workqueue.
+		 */
+		if (current->flags & (PF_KTHREAD | PF_WQ_WORKER))
+			return -EINVAL;
 		WRITE_ONCE(io->task, get_task_struct(current));
+	}
+
+	ublk_fill_io_cmd(io, cmd);
 
 	return 0;
 }
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 8 lines · Ming Lei

__ublk_fetch() sets io->task to current, which is later checked against io_uring_cmd_get_task() in ublk_uring_cmd_cancel_fn(). With REQ_F_FORCE_ASYNC, the FETCH uring_cmd can be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN in cancel_fn.

Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of io_uring fallback.

I think this should be caught in ublk_ch_uring_cmd_cb(), which should not hit ublk_ch_uring_cmd_local() if tw.cancel is true. fallback work should just be terminated, not be issued.

Signature · 1 line
Jens Axboe
author maintainer
#2 lore raw
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Ming Lei

__ublk_fetch() sets io->task to current, which is later checked against io_uring_cmd_get_task() in ublk_uring_cmd_cancel_fn(). With REQ_F_FORCE_ASYNC, the FETCH uring_cmd can be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN in cancel_fn.

Quoted 6 lines
Quoted 2 lines

Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of io_uring fallback.

I think this should be caught in ublk_ch_uring_cmd_cb(), which should not hit ublk_ch_uring_cmd_local() if tw.cancel is true. fallback work should just be terminated, not be issued.

Yeah, that is definitely better, will take it in V2.

Thanks,
Ming

maintainer
#3 lore raw
Quoted 8 lines · Ming Lei
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Ming Lei

__ublk_fetch() sets io->task to current, which is later checked against io_uring_cmd_get_task() in ublk_uring_cmd_cancel_fn(). With REQ_F_FORCE_ASYNC, the FETCH uring_cmd can be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN in cancel_fn.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of io_uring fallback.

I think this should be caught in ublk_ch_uring_cmd_cb(), which should not hit ublk_ch_uring_cmd_local() if tw.cancel is true. fallback work should just be terminated, not be issued.

Yeah, that is definitely better, will take it in V2.

Something like this, totally untested. Caveat: probably worth checking the other tw paths in ublk as well!

drivers/block/ublk_drv.c +3 -1
index 8e5f3738c203..d10460d29e4a 100644
@@ -3496,8 +3496,10 @@ static void ublk_ch_uring_cmd_cb(struct io_tw_req tw_req, io_tw_token_t tw)
 {
 	unsigned int issue_flags = IO_URING_CMD_TASK_WORK_ISSUE_FLAGS;
 	struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
-	int ret = ublk_ch_uring_cmd_local(cmd, issue_flags);
+	int ret = -ECANCELED;
 
+	if (!tw.cancel)
+		ret = ublk_ch_uring_cmd_local(cmd, issue_flags);
 	if (ret != -EIOCBQUEUED)
 		io_uring_cmd_done(cmd, ret, issue_flags);
 }
Signature · 1 line
Jens Axboe
author maintainer
#4 lore raw
Quoted 12 lines · Jens Axboe
Quoted 11 lines · Ming Lei
Quoted 10 lines · Jens Axboe
Quoted 8 lines · Ming Lei

__ublk_fetch() sets io->task to current, which is later checked against io_uring_cmd_get_task() in ublk_uring_cmd_cancel_fn(). With REQ_F_FORCE_ASYNC, the FETCH uring_cmd can be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN in cancel_fn.

Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of io_uring fallback.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I think this should be caught in ublk_ch_uring_cmd_cb(), which should not hit ublk_ch_uring_cmd_local() if tw.cancel is true. fallback work should just be terminated, not be issued.

Yeah, that is definitely better, will take it in V2.

Something like this, totally untested. Caveat: probably worth checking the other tw paths in ublk as well!

tw.cancel can replace the check in ublk_dispatch_req() for ublk_cmd_tw_cb() and ublk_cmd_list_tw_cb(), which is one cleanup.

ublk_batch_tw_cb() doesn't track io task, so it is just fine.

Quoted 7 lines
drivers/block/ublk_drv.c +0 -0
index 8e5f3738c203..d10460d29e4a 100644
@@ -3496,8 +3496,10 @@ static void ublk_ch_uring_cmd_cb(struct io_tw_req tw_req, io_tw_token_t tw)
 {

drivers/block/ublk_drv.c static void ublk_ch_uring_cmd_cb(struct io_tw_req tw_req, io_tw_token_t tw) · line 3505

Quoted 10 lines
drivers/block/ublk_drv.c +3 -1
 	unsigned int issue_flags = IO_URING_CMD_TASK_WORK_ISSUE_FLAGS;
 	struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
-	int ret = ublk_ch_uring_cmd_local(cmd, issue_flags);
+	int ret = -ECANCELED;
 
+	if (!tw.cancel)
+		ret = ublk_ch_uring_cmd_local(cmd, issue_flags);
 	if (ret != -EIOCBQUEUED)
 		io_uring_cmd_done(cmd, ret, issue_flags);
 }

This fix looks good.

Thanks,
Ming

maintainer
#5 lore raw
Quoted 19 lines · Ming Lei
Quoted 18 lines · Jens Axboe
Quoted 16 lines · Ming Lei
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Ming Lei

__ublk_fetch() sets io->task to current, which is later checked against io_uring_cmd_get_task() in ublk_uring_cmd_cancel_fn(). With REQ_F_FORCE_ASYNC, the FETCH uring_cmd can be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN in cancel_fn.

Quoted 6 lines
Quoted 2 lines

Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of io_uring fallback.

I think this should be caught in ublk_ch_uring_cmd_cb(), which should not hit ublk_ch_uring_cmd_local() if tw.cancel is true. fallback work should just be terminated, not be issued.

Yeah, that is definitely better, will take it in V2.

Quoted 7 lines
Quoted 2 lines

Something like this, totally untested. Caveat: probably worth checking the other tw paths in ublk as well!

tw.cancel can replace the check in ublk_dispatch_req() for ublk_cmd_tw_cb() and ublk_cmd_list_tw_cb(), which is one cleanup.

ublk_batch_tw_cb() doesn't track io task, so it is just fine.

Sounds good to me.

Quoted 11 lines
Quoted 11 lines
drivers/block/ublk_drv.c +1 -1
index 8e5f3738c203..d10460d29e4a 100644
@@ -3496,8 +3496,10 @@ static void ublk_ch_uring_cmd_cb(struct io_tw_req tw_req, io_tw_token_t tw)
 {
 	unsigned int issue_flags = IO_URING_CMD_TASK_WORK_ISSUE_FLAGS;
 	struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
-	int ret = ublk_ch_uring_cmd_local(cmd, issue_flags);
+	int ret = -ECANCELED;
 
Quoted 7 lines

drivers/block/ublk_drv.c static void ublk_ch_uring_cmd_cb(struct io_tw_req tw_req, io_tw_token_t tw) · line 3505

Quoted 5 lines
drivers/block/ublk_drv.c +2 -0
+	if (!tw.cancel)
+		ret = ublk_ch_uring_cmd_local(cmd, issue_flags);
 	if (ret != -EIOCBQUEUED)
 		io_uring_cmd_done(cmd, ret, issue_flags);
 }

This fix looks good.

Feel free to just run with it or make it part of a fixup series.

Signature · 1 line
Jens Axboe