Changes in v3:
- v2 can't fix the problem thoroughly, add patch 3-4 to this series.
- modify descriptions.
- patch 5 is just a cleanup
Changes in v2:
- as Bart suggested, add a new helper function for drivers to get
request by tag.
Our syzkaller report a uaf in nbd_read_stat():
Call trace:
dump_backtrace+0x0/0x310 arch/arm64/kernel/time.c:78
show_stack+0x28/0x38 arch/arm64/kernel/traps.c:158
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x144/0x1b4 lib/dump_stack.c:118
print_address_description+0x68/0x2d0 mm/kasan/report.c:253
kasan_report_error mm/kasan/report.c:351 [inline]
kasan_report+0x134/0x2f0 mm/kasan/report.c:409
check_memory_region_inline mm/kasan/kasan.c:260 [inline]
__asan_load4+0x88/0xb0 mm/kasan/kasan.c:699
__read_once_size include/linux/compiler.h:193 [inline]
blk_mq_rq_state block/blk-mq.h:106 [inline]
blk_mq_request_started+0x24/0x40 block/blk-mq.c:644
nbd_read_stat drivers/block/nbd.c:670 [inline]
recv_work+0x1bc/0x890 drivers/block/nbd.c:749
process_one_work+0x3ec/0x9e0 kernel/workqueue.c:2147
worker_thread+0x80/0x9d0 kernel/workqueue.c:2302
kthread+0x1d8/0x1e0 kernel/kthread.c:255
ret_from_fork+0x10/0x18 arch/arm64/kernel/entry.S:1174
1) At first, a normal io is submitted and completed with scheduler:
internel_tag = blk_mq_get_tag -> get tag from sched_tags
blk_mq_rq_ctx_init
sched_tags->rq[internel_tag] = sched_tag->static_rq[internel_tag]
...
blk_mq_get_driver_tag
__blk_mq_get_driver_tag -> get tag from tags
tags->rq[tag] = sched_tag->static_rq[internel_tag]
So, both tags->rq[tag] and sched_tags->rq[internel_tag] are pointing
to the request: sched_tags->static_rq[internal_tag]. Even if the
io is finished.
2) nbd server send a reply with random tag directly:
recv_work
nbd_read_stat
blk_mq_tag_to_rq(tags, tag)
rq = tags->rq[tag]
3) if the sched_tags->static_rq is freed:
blk_mq_sched_free_requests
blk_mq_free_rqs(q->tag_set, hctx->sched_tags, i)
blk_mq_clear_rq_mapping(set, tags, hctx_idx);
4) Then, nbd continue to use the freed request in nbd_read_stat()
This patchset try to fix the problem by following apporch:
- add a new interface blk_mq_get_rq_by_tag() to replace blk_mq_tag_to_rq(),
it will make sure the return request is started and won't be freed.
- nbd client won't handle the reply if it didn't send the corresponding
request message.
- nbd won't complete a request multiple times
Yu Kuai (5):
blk-mq: add a new interface to get request by tag
nbd: convert to use blk_mq_get_rq_by_tag()
nbd: don't handle response without a corresponding request message
nbd: make sure request completion won't concurrent
nbd: don't start request if nbd_queue_rq() failed
block/blk-mq-tag.c | 37 +++++++++++++++++++++++++++++++++++++
block/blk-mq.c | 1 +
block/blk-mq.h | 1 -
drivers/block/nbd.c | 39 +++++++++++++++++++++++++++++----------
include/linux/blk-mq.h | 4 ++++
5 files changed, 71 insertions(+), 11 deletions(-)
--
2.31.1
While handling a response message from server, nbd_read_stat() will
try to get request by tag, and then complete the request. However,
this is problematic if nbd haven't sent a corresponding request
message:
t1 t2
submit_bio
nbd_queue_rq
blk_mq_start_request
recv_work
nbd_read_stat
blk_mq_get_rq_by_tag
blk_mq_complete_request
nbd_send_cmd
Thus add a new cmd flag 'NBD_CMD_INFLIGHT', it will be set in
nbd_send_cmd() and checked in nbd_read_stat().
Signed-off-by: Yu Kuai <redacted>
---
drivers/block/nbd.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
@@ -743,6 +748,12 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)cmd=blk_mq_rq_to_pdu(req);mutex_lock(&cmd->lock);+if(!test_bit(NBD_CMD_INFLIGHT,&cmd->flags)){+dev_err(disk_to_dev(nbd->disk),"NBD_CMD_INFLIGHT is not set %d\n",+tag);+ret=-ENOENT;+gotoout;+}if(cmd->cmd_cookie!=nbd_handle_to_cookie(handle)){dev_err(disk_to_dev(nbd->disk),"Double reply on req %p, cmd_cookie %u, handle cookie %u\n",req,cmd->cmd_cookie,nbd_handle_to_cookie(handle));
@@ -980,6 +991,8 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)*returnsEAGAINcanberetriedonadifferentsocket.*/ret=nbd_send_cmd(nbd,cmd,index);+if(!ret)+set_bit(NBD_CMD_INFLIGHT,&cmd->flags);if(ret==-EAGAIN){dev_err_ratelimited(disk_to_dev(nbd->disk),"Request send failed, requeueing\n");
Ming Lei had fixed the request uaf while iterating tags in
commit bd63141d585b ("blk-mq: clear stale request in tags->rq[] before
freeing one request pool").
However, hctx->tags->rq[] will point to hctx->sched_tags->static_rq[]
in blk_mq_get_driver_tag(), and blk_mq_tag_to_rq() can access such
request in some drivers. Generally it won't be a problem if the
driver can make sure to get drivet tag before calling
blk_mq_tag_to_rq(). However, nbd will do such thing once it receive
a reply message from server, and there isn't any mechanism to
protect that it won't handle the reply message without a corresponding
request message.
Thus add new interface to make sure it won't return a freed request,
and then nbd can check if it had sent the corresponding request message.
Signed-off-by: Yu Kuai <redacted>
---
block/blk-mq-tag.c | 37 +++++++++++++++++++++++++++++++++++++
block/blk-mq.c | 1 +
block/blk-mq.h | 1 -
include/linux/blk-mq.h | 4 ++++
4 files changed, 42 insertions(+), 1 deletion(-)
@@ -652,3 +652,40 @@ u32 blk_mq_unique_tag(struct request *rq)(rq->tag&BLK_MQ_UNIQUE_TAG_MASK);}EXPORT_SYMBOL(blk_mq_unique_tag);+++/**+*blk_mq_get_rq_by_tag-iftherequestthatisrepresentedbythetagis+*notidle,incrementit'sreferenceandthenreturnit.Otherwisereturn+*NULL.+*+*@tags:thetagswearelookingfrom+*@tag:thetagthatrepresentstherequest+*/+structrequest*blk_mq_get_rq_by_tag(structblk_mq_tags*tags,+unsignedinttag)+{+unsignedlongflags;+structrequest*rq;++/* hold lock to prevent accessing freed request by tag */+spin_lock_irqsave(&tags->lock,flags);+rq=blk_mq_tag_to_rq(tags,tag);+if(!rq)+gotoout_unlock;++if(!refcount_inc_not_zero(&rq->ref)){+rq=NULL;+gotoout_unlock;+}++if(!blk_mq_request_started(rq)){+blk_mq_put_rq_ref(rq);+rq=NULL;+}++out_unlock:+spin_unlock_irqrestore(&tags->lock,flags);+returnrq;+}+EXPORT_SYMBOL(blk_mq_get_rq_by_tag);
Currently, blk_mq_end_request() will be called if nbd_queue_rq()
failed, thus start request in such situation is useless.
Signed-off-by: Yu Kuai <redacted>
---
drivers/block/nbd.c | 3 ---
1 file changed, 3 deletions(-)
@@ -944,7 +944,6 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)if(!refcount_inc_not_zero(&nbd->config_refs)){dev_err_ratelimited(disk_to_dev(nbd->disk),"Socks array is empty\n");-blk_mq_start_request(req);return-EINVAL;}config=nbd->config;
@@ -953,7 +952,6 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)dev_err_ratelimited(disk_to_dev(nbd->disk),"Attempted send on invalid socket\n");nbd_config_put(nbd);-blk_mq_start_request(req);return-EINVAL;}cmd->status=BLK_STS_OK;
@@ -977,7 +975,6 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)*/sock_shutdown(nbd);nbd_config_put(nbd);-blk_mq_start_request(req);return-EIO;}gotoagain;
blk_mq_tag_to_rq() can only ensure to return valid request in
following situation:
1) client send request message to server first
submit_bio
...
blk_mq_get_tag
...
blk_mq_get_driver_tag
...
nbd_queue_rq
nbd_handle_cmd
nbd_send_cmd
2) client receive respond message from server
recv_work
nbd_read_stat
blk_mq_tag_to_rq
If step 1) is missing, blk_mq_tag_to_rq() will return a stale
request, which might be freed. Thus convert to use
blk_mq_get_rq_by_tag() to make sure the returned request is not
freed. However, there are still some problems if the request is
started, and this will be fixed in later patches.
Signed-off-by: Yu Kuai <redacted>
---
drivers/block/nbd.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
commit cddce0116058 ("nbd: Aovid double completion of a request")
try to fix that nbd_clear_que() and recv_work() can complete a
request concurrently. However, the problem still exists:
t1 t2 t3
nbd_disconnect_and_put
flush_workqueue
recv_work
blk_mq_complete_request
blk_mq_complete_request_remote -> this is true
WRITE_ONCE(rq->state, MQ_RQ_COMPLETE)
blk_mq_raise_softirq
blk_done_softirq
blk_complete_reqs
nbd_complete_rq
blk_mq_end_request
blk_mq_free_request
WRITE_ONCE(rq->state, MQ_RQ_IDLE)
nbd_clear_que
blk_mq_tagset_busy_iter
nbd_clear_req
__blk_mq_free_request
blk_mq_put_tag
blk_mq_complete_request
There are three places where request can be completed in nbd:
recv_work(), nbd_clear_que() and nbd_xmit_timeout(). Since they
all hold cmd->lock before completing the request, it's easy to
avoid the problem by setting and checking a cmd flag.
Signed-off-by: Yu Kuai <redacted>
---
drivers/block/nbd.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)