nvme-[tcp|rdma] fix for possible use-after-free
Sagi Grimberg
maintainercover nvme-[tcp|rdma] fix for possible use-after-free
A few use-after-free reports were seen in the wild with nvme-tcp when testing ctrl reset and error recovery under load. Analysis shows that the exact same use-after-free can happen with nvme-rdma as well. This patch series addresses these issues for both.
Sagi Grimberg (4):
nvme-tcp: fix a possible use-after-free in controller reset during
load
nvme-tcp: fix possible use-after-free in transport error_recovery work
nvme-rdma: fix a possible use-after-free in controller reset during
load
nvme-rdma: fix possible use-after-free in transport error_recovery
work
drivers/nvme/host/rdma.c | 5 +++++ drivers/nvme/host/tcp.c | 5 +++++ 2 files changed, 10 insertions(+)
Signature · 1 line
2.30.2
Sagi Grimberg
maintainerPATCH 1/4 nvme-tcp: fix a possible use-after-free in controller reset during load
Unlike .queue_rq, .submit_async_event does not check the ctrl and queue readiness for submitting a AER. This may lead to a use-after-free condition in the following scenario:
1. nvme_tcp_reset_ctrl_work
2. -> nvme_stop_ctrl flushes ctrl async_event_work
3. ctrl sends AEN which is received by the host, which in turn schedules AEN handling
4. teardown admin queue (which releases the queue socket)
5. AEN processed, submits another AER, calling nvme-tcp driver to submit prepares the cmd and schedules io_work
6. io_work triggers and attempts to send the cmd
==> use-after-free
In order to fix that, add ctrl and queue state check to validate the driver is actually able to accept the socket send.
This solves the above race in the reset flow because the ctrl state is changed to RESETTING, then the async_event_work is flushed, hence from that point, any other AER command will find the ctrl state to be RESETTING and bail out without scheduling io_work.
--- drivers/nvme/host/tcp.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/nvme/host/tcp.c +4 -0
@@ -2211,6 +2211,10 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;struct nvme_command *cmd = &pdu->cmd;u8 hdgst =nvme_tcp_hdgst_len (queue); +bool queue_ready =test_bit (NVME_TCP_Q_LIVE , &queue->flags); + +if (ctrl->ctrl.state !=NVME_CTRL_LIVE || !queue_ready) +return ;memset (pdu,0 ,sizeof (*pdu)); pdu->hdr.type = nvme_tcp_cmd;
Signature · 1 line
2.30.2
Christoph Hellwig
maintainerRe: [PATCH 1/4] nvme-tcp: fix a possible use-after-free in controller reset during load
drivers/nvme/host/tcp.c static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg) · line 2216
Quoted 6 lines · Sagi Grimberg
drivers/nvme/host/tcp.c +3
-0struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;struct nvme_command *cmd = &pdu->cmd;u8 hdgst =nvme_tcp_hdgst_len (queue); +bool queue_ready =test_bit (NVME_TCP_Q_LIVE , &queue->flags); + +if (ctrl->ctrl.state !=NVME_CTRL_LIVE || !queue_ready)
Why do we need the local variable?
Also what prevents the controller or queue state to change just after this check?
Sagi Grimberg
maintainerRe: [PATCH 1/4] nvme-tcp: fix a possible use-after-free in controller reset during load
Quoted 9 lines
Review
Quoted 7 lines
struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu; struct nvme_command *cmd = &pdu->cmd; u8 hdgst = nvme_tcp_hdgst_len(queue);(quoted diff) +3
-0+ bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags); + + if (ctrl->ctrl.state != NVME_CTRL_LIVE || !queue_ready)Why do we need the local variable?
I can remove the local variable, didn't want to have a multiline if condition.
Quoted 2 lines
Also what prevents the controller or queue state to change just after this check?
The driver will make sure to flush ctrl->async_event_work _after_ changing the controller state (it is flushed in nvme_stop_ctrl). Only after that it will continue to free the admin queue. So if this check passed, it is safe to submit the aer command.
I think that the ctrl->state check should be sufficient. In fact, I think we can move it to the core instead of doing it in the drivers:
--
drivers/nvme/host/core.c +2 -1
@@ -4251,6 +4251,8 @@ static void nvme_async_event_work(struct work_struct *work) container_of(work, struct nvme_ctrl, async_event_work);nvme_aen_uevent (ctrl); +if (ctrl->state !=NVME_CTRL_LIVE ) +return ; ctrl->ops->submit_async_event (ctrl); } --
I'll send a revised patch.
Christoph Hellwig
maintainerRe: [PATCH 1/4] nvme-tcp: fix a possible use-after-free in controller reset during load
Quoted 7 lines · Sagi Grimberg
The driver will make sure to flush
ctrl->async_event_work_after_ changing the controller state (it is flushed innvme_stop_ctrl). Only after that it will continue to free the admin queue. So if this check passed, it is safe to submit the aer command.I think that the
ctrl->statecheck should be sufficient. In fact, I think we can move it to the core instead of doing it in the drivers:
Maybe through in a comment explaining this? Otherwise having less checks and having them in the core is always a good thing, so I'm in favour.
Sagi Grimberg
maintainerRe: [PATCH 1/4] nvme-tcp: fix a possible use-after-free in controller reset during load
Quoted 7 lines · Sagi Grimberg
Quoted 5 lines
The driver will make sure to flush
ctrl->async_event_work_after_ changing the controller state (it is flushed innvme_stop_ctrl). Only after that it will continue to free the admin queue. So if this check passed, it is safe to submit the aer command.
Quoted 6 lines
Quoted 2 lines
I think that the
ctrl->statecheck should be sufficient. In fact, I think we can move it to the core instead of doing it in the drivers:Maybe through in a comment explaining this? Otherwise having less checks and having them in the core is always a good thing, so I'm in favour.
I sent a v2 already. You want me to send a v3 with something like:
--
drivers/nvme/host/core.c +6 -1
@@ -4253,6 +4253,12 @@ static void nvme_async_event_work(struct work_struct *work)nvme_aen_uevent (ctrl);if (ctrl->state !=NVME_CTRL_LIVE )return ; +/* +* drivers must guarantee aer submission here is safe +* by flushing the ctrl async_event_work after changing +* the controller state from LIVE and before freeing the +* admin queue. +*/ ctrl->ops->submit_async_event (ctrl); } --
Christoph Hellwig
maintainerRe: [PATCH 1/4] nvme-tcp: fix a possible use-after-free in controller reset during load
Quoted 7 lines · Sagi Grimberg
Quoted 7 lines · Sagi Grimberg
Quoted 5 lines
The driver will make sure to flush
ctrl->async_event_work_after_ changing the controller state (it is flushed innvme_stop_ctrl). Only after that it will continue to free the admin queue. So if this check passed, it is safe to submit the aer command.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
I think that the
ctrl->statecheck should be sufficient. In fact, I think we can move it to the core instead of doing it in the drivers:Maybe through in a comment explaining this? Otherwise having less checks and having them in the core is always a good thing, so I'm in favour.
I sent a v2 already. You want me to send a v3 with something like:
I've added the comment manually and applied the v2 series, thanks!
Sagi Grimberg
maintainerPATCH 2/4 nvme-tcp: fix possible use-after-free in transport error_recovery work
In reply to Sagi GrimbergWhile nvme_tcp_submit_async_event_work is checking the ctrl and queue state before preparing the AER command and scheduling io_work, in order to fully prevent a race where this check is not reliable the error recovery work must flush async_event_work before continuing to destroy the admin queue after setting the ctrl state to RESETTING such that there is no race .submit_async_event and the error recovery handler itself changing the ctrl state.
--- drivers/nvme/host/tcp.c | 1 + 1 file changed, 1 insertion(+)
drivers/nvme/host/tcp.c +1 -0
@@ -2096,6 +2096,7 @@ static void nvme_tcp_error_recovery_work(struct work_struct *work)struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;nvme_stop_keep_alive (ctrl); +flush_work (&ctrl->async_event_work);nvme_tcp_teardown_io_queues (ctrl, false);/* unquiesce to fail fast pending requests */ nvme_start_queues (ctrl);
Signature · 1 line
2.30.2
Sagi Grimberg
maintainerPATCH 3/4 nvme-rdma: fix a possible use-after-free in controller reset during load
In reply to Sagi GrimbergUnlike .queue_rq, .submit_async_event does not check the ctrl and queue readiness for submitting a AER. This may lead to a use-after-free condition in the following scenario:
1. nvme_rdma_reset_ctrl_work
2. -> nvme_stop_ctrl flushes ctrl async_event_work
3. ctrl sends AEN which is received by the host, which in turn schedules AEN handling
4. teardown admin queue (which releases the rdma queue-pair)
5. AEN processed, submits another AER, calling nvme-rdma driver to submit prepares the cmd posts on the admin qp
==> use-after-free accessing an already freed rdma queue-pair
In order to fix that, add ctrl and queue state check to validate the driver is actually able to accept the rdma qp post.
This solves the above race in the reset flow because the ctrl state is changed to RESETTING, then the async_event_work is flushed, hence from that point, any other AER command will find the ctrl state to be RESETTING and bail out without posting the cmd on the rdma qp.
--- drivers/nvme/host/rdma.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/nvme/host/rdma.c +4 -0
@@ -1714,9 +1714,13 @@ static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)struct ib_device *dev = queue->device->dev;struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;struct nvme_command *cmd = sqe->data; +bool queue_ready =test_bit (NVME_RDMA_Q_LIVE , &queue->flags);struct ib_sge sge;int ret; +if (ctrl->ctrl.state !=NVME_CTRL_LIVE || !queue_ready) +return ; +ib_dma_sync_single_for_cpu (dev, sqe->dma,sizeof (*cmd),DMA_TO_DEVICE );memset (cmd,0 ,sizeof (*cmd));
Signature · 1 line
2.30.2
Christoph Hellwig
maintainerRe: [PATCH 3/4] nvme-rdma: fix a possible use-after-free in controller reset during load
Same comment as for the TCP version here.
Sagi Grimberg
maintainerPATCH 4/4 nvme-rdma: fix possible use-after-free in transport error_recovery work
In reply to Sagi GrimbergWhile nvme_rdma_submit_async_event_work is checking the ctrl and queue state before preparing the AER command and scheduling io_work, in order to fully prevent a race where this check is not reliable the error recovery work must flush async_event_work before continuing to destroy the admin queue after setting the ctrl state to RESETTING such that there is no race .submit_async_event and the error recovery handler itself changing the ctrl state.
--- drivers/nvme/host/rdma.c | 1 + 1 file changed, 1 insertion(+)
drivers/nvme/host/rdma.c +1 -0
@@ -1200,6 +1200,7 @@ static void nvme_rdma_error_recovery_work(struct work_struct *work) struct nvme_rdma_ctrl, err_work);nvme_stop_keep_alive (&ctrl->ctrl); +flush_work (&ctrl->ctrl.async_event_work);nvme_rdma_teardown_io_queues (ctrl, false);nvme_start_queues (&ctrl->ctrl);nvme_rdma_teardown_admin_queue (ctrl, false);
Signature · 1 line
2.30.2