ublk: reject FETCH from non-userspace context
Subsystems (2)
Ming Lei
maintainerPATCH 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.
--- drivers/block/ublk_drv.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/block/ublk_drv.c +11 -4
@@ -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
Jens Axboe
maintainerQuoted 8 lines · Ming Lei
__ublk_fetch()setsio->taskto current, which is later checked againstio_uring_cmd_get_task()inublk_uring_cmd_cancel_fn(). WithREQ_F_FORCE_ASYNC, the FETCHuring_cmdcan be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN incancel_fn.Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of
io_uringfallback.
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
Ming Lei
maintainerQuoted 7 lines · Jens Axboe
Quoted 6 lines · Ming Lei
__ublk_fetch()setsio->taskto current, which is later checked againstio_uring_cmd_get_task()inublk_uring_cmd_cancel_fn(). WithREQ_F_FORCE_ASYNC, the FETCHuring_cmdcan be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN incancel_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_uringfallback.I think this should be caught in
ublk_ch_uring_cmd_cb(), which should not hitublk_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
Jens Axboe
maintainerQuoted 8 lines · Ming Lei
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Ming Lei
__ublk_fetch()setsio->taskto current, which is later checked againstio_uring_cmd_get_task()inublk_uring_cmd_cancel_fn(). WithREQ_F_FORCE_ASYNC, the FETCHuring_cmdcan be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN incancel_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_uringfallback.I think this should be caught in
ublk_ch_uring_cmd_cb(), which should not hitublk_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
@@ -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
Ming Lei
maintainerQuoted 12 lines · Jens Axboe
Quoted 11 lines · Ming Lei
Quoted 10 lines · Jens Axboe
Quoted 8 lines · Ming Lei
__ublk_fetch()setsio->taskto current, which is later checked againstio_uring_cmd_get_task()inublk_uring_cmd_cancel_fn(). WithREQ_F_FORCE_ASYNC, the FETCHuring_cmdcan be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN incancel_fn.Reject FETCH if current is not a real userspace task, and it is reasonable for failing it in case of
io_uringfallback.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
I think this should be caught in
ublk_ch_uring_cmd_cb(), which should not hitublk_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@@ -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
-1unsigned 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
Jens Axboe
maintainerQuoted 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()setsio->taskto current, which is later checked againstio_uring_cmd_get_task()inublk_uring_cmd_cancel_fn(). WithREQ_F_FORCE_ASYNC, the FETCHuring_cmdcan be issued from task work, which can be run from io_uring's fallback workqueue, causing a task mismatch and triggering the WARN incancel_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_uringfallback.I think this should be caught in
ublk_ch_uring_cmd_cb(), which should not hitublk_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()forublk_cmd_tw_cb()andublk_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@@ -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