nvmet-tcp: check INIT_FAILED before nvmet_req_uninit in digest error path
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): CA+ysrS+QrbGfOLk=e0=PZ-py-KO_xZgn827nEg7mh7hGhdUAAw@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): 2026031827-carving-overdrawn-5b1f@gregkh
PATCH 1/1 nvmet-tcp: check INIT_FAILED before nvmet_req_uninit in digest error path
In nvmet_tcp_try_recv_ddgst(), when a data digest mismatch is detected, nvmet_req_uninit() is called unconditionally. However, if the command arrived via the nvmet_tcp_handle_req_failure() path, nvmet_req_init() had returned false and percpu_ref_tryget_live() was never executed. The unconditional percpu_ref_put() inside nvmet_req_uninit() then causes a refcount underflow, leading to a WARNING in percpu_ref_switch_to_atomic_rcu, a use-after-free diagnostic, and eventually a permanent workqueue deadlock.
Check cmd->flags & NVMET_TCP_F_INIT_FAILED before calling nvmet_req_uninit(), matching the existing pattern in nvmet_tcp_execute_request().
--- drivers/nvme/target/tcp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/tcp.c +2 -1
@@ -1310,7 +1310,8 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) queue->idx, cmd->req.cmd->common.command_id, queue->pdu.cmd.hdr.type,le32_to_cpu (cmd->recv_ddgst), le32_to_cpu(cmd->exp_ddgst)); -nvmet_req_uninit (&cmd->req); +if (!(cmd->flags &NVMET_TCP_F_INIT_FAILED )) +nvmet_req_uninit (&cmd->req);nvmet_tcp_free_cmd_buffers (cmd);nvmet_tcp_fatal_error (queue); ret = -EPROTO ;
Signature · 1 line
2.34.1
Looks good to me:
Although I'd prefer if someone more familiar with the TCP code could also look over it.
Hi all,
Just following up on this patch.
Please let me know if there are any concerns or if any changes are needed.
Thanks,
Shivam Kumar
Quoted 8 lines · Christoph Hellwig
Looks good to me:
Reviewed-by Christoph Hellwig <[email protected]>Although I'd prefer if someone more familiar with the TCP code could also look over it.
Christoph Hellwig
maintainerQuoted 4 lines · Shivam Kumar
Hi all,
Just following up on this patch.
Please let me know if there are any concerns or if any changes are needed.
Well, if we can't get people more familiar with TCP to look over it there's not much we can do but include it.
Keith Busch
maintainerQuoted 8 lines · Christoph Hellwig
Quoted 4 lines · Shivam Kumar
Hi all,
Just following up on this patch.
Please let me know if there are any concerns or if any changes are needed.Well, if we can't get people more familiar with TCP to look over it there's not much we can do but include it.
Yep, let's just go forward with this. Patch applied to nvme-7.1. Thanks!
A message that is not in the archive (sent off-list or not yet received): CA+ysrSLcSJSgEC7i0GYXPiRY9sbbyEGT60Nh1SJ6PqXWENgSOw@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CA+ysrSL+3YVGvTKhJ=jL92XZPwjoDpt5D2BK-HeCU975vO-fCQ@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CA+ysrSJQ2tDiB=zL+GwF=ckhmO62v6GC2bWVUVVDVn7d8Km1mw@mail.gmail.com
Re: [Bug Report] nvmet-tcp: unbalanced percpu_ref_put on data digest error after nvmet_req_init failure causes refcount underflow, use-after-free, and permanent workqueue deadlock
+linux-nvme list as well.
Quoted 147 lines · Shivam Kumar
Hi Greg, Christoph, Sagi, Chaitanya
Following up on my previous report (
percpu_refunderflow innvmet_tcp_try_recv_ddgst). During testing with thepercpu_refpatch applied, I identified a separate vulnerability triggered by the same PoC. This one affects the queue kref (not the SQpercpu_ref) and has a different root cause.# Bug Overview
nvmet_tcp_handle_icreq()setsqueue->state=NVMET_TCP_Q_LIVEwithout holdingstate_lockand without checking whether teardown has already started. When a client closes the connection immediately after sending an ICReq and a malformed CapsuleCmd, the FIN can arrive and be processed beforeio_workreads the ICReq from the socket buffer. The FIN handler setsqueue->state=NVMET_TCP_Q_DISCONNECTINGand callskref_put(1->0). Thenio_workprocesses the ICReq andhandle_icreqblindly overwrites the state back toNVMET_TCP_Q_LIVE. This defeats the guard innvmet_tcp_schedule_release_queue(), which relies on state ==NVMET_TCP_Q_DISCONNECTINGto prevent a secondkref_put.When the subsequent digest error triggers
nvmet_tcp_fatal_error()->kernel_sock_shutdown(), the socket teardown synchronously processes any pending TCP packets in the socket backlog viarelease_sock()->tcp_done()->nvmet_tcp_state_change()->nvmet_tcp_schedule_release_queue(). Because the state was overwritten to LIVE, the guard passes andkref_putis called on an already-zero kref, triggering the underflow.# Root Cause
The vulnerable code in
nvmet_tcp_handle_icreq()(tcp.c):
queue->state=NVMET_TCP_Q_LIVE;nvmet_prepare_receive_pdu(queue);
return 0;This state transition is not protected by
state_lockand does not check the current state. All other state transitions toNVMET_TCP_Q_DISCONNECTINGinnvmet_tcp_schedule_release_queue()are protected by the spinlock, buthandle_icreqbypasses this synchronization entirely.# Race Sequence
The PoC sends ICReq + CapsuleCmd +
close()in rapid succession. On the wire:[TCP handshake] -> [ICReq] -> [CapsuleCmd] -> [FIN]
All data arrives in order in the socket buffer, but the FIN triggers a TCP state change callback (softirq context) that races with
io_work(process context) reading the application data.# ftrace evidence (queue 29)Step 1: FIN arrives, teardown begins (softirq context):
kworker/-74 1b..2. 125826488us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 0, kref=1, caller=tcp_fin+0x278/0x4f0
kworker/-74 1b..2. 125826492us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=1, caller=tcp_fin+0x278/0x4f0State transitions from CONNECTING(0) to DISCONNECTING(3), kref decremented 1->0.
Step 2:
io_workprocesses ICReq, overwrites state (process context):kworker/-74 1..... 125826529us :
nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pdubefore changing state is 3, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
kworker/-74 1..... 125826532us :nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pduafter changing state is 2, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
handle_icreqsee's state=DISCONNECTING(3) but overwrites it to LIVE(2) without holdingstate_lock. kref is already 0.Step 3: Digest error triggers
fatal_error->kernel_sock_shutdown(process context):kworker/-74 1..... 125831291us :
nvmet_tcp_fatal_error: DEBUG queue 29:fatal_error, state=2, ctrl=0000000000000000, caller=nvmet_tcp_try_recv_ddgst+0x74f/0x9a0Step 4:
kernel_sock_shutdown->release_sockprocesses backlog ->tcp_donere-entersstate_change, guard bypassed because state is LIVE(2):kworker/-74 1b..2. 125831626us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 2, kref=0, caller=tcp_done+0x1d7/0x350
kworker/-74 1b..2. 125831629us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=0, caller=tcp_done+0x1d7/0x350
schedule_release_queuesee's state=LIVE(2), guard passes,kref_putcalled on kref=0 -> underflow -> CRASH.The critical evidence is Step 2:
handle_icreqreads state=3(DISCONNECTING) and writes state=2(LIVE) — proving the unsynchronized overwrite.# Additional Observation: Double
fatal_errorCallThe ftrace also shows that
fatal_erroris called twice per error: once fromnvmet_tcp_try_recv_ddgst(digest mismatch) and again fromnvmet_tcp_socket_errorinnvmet_tcp_io_work(propagated error return). Both calls invokekernel_sock_shutdown, doubling the window for the reentrantstate_change.# Crash Trace (with
percpu_refpatch applied)
refcount_t: underflow; use-after-free.WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xc5/0x110 Workqueue: nvmet_tcp_wq nvmet_tcp_io_work Call Trace: <TASK> nvmet_tcp_state_change+0x3e6/0x470 tcp_done+0x1d7/0x350 tcp_rcv_state_process+0x392c/0x6d80 tcp_v4_do_rcv+0x1b1/0xa80 __release_sock+0x2ab/0x340 release_sock+0x58/0x1c0 inet_shutdown+0x1f6/0x3c0 nvmet_tcp_fatal_error+0x13c/0x1c0 nvmet_tcp_try_recv_ddgst+0x74f/0x9a0 nvmet_tcp_io_work+0xd43/0x1fc0 process_one_work+0x8cf/0x1a40# Affected Code
- Subsystem:
drivers/nvme/target/tcp.c
- Function:nvmet_tcp_handle_icreq()
- Tested on: Linux 7.0-rc3# Suggested Fix Direction
The state transition in
nvmet_tcp_handle_icreq()needs to be synchronized understate_lockwith a check forNVMET_TCP_Q_DISCONNECTING. If the queue is already tearing down,handle_icreqshould not set the state to LIVE and should stop processing. I'll defer to you on the exact error handling semantics (return value and cleanup path).This bug is independent of the
percpu_refunderflow in the previous report. Thepercpu_refpatch fixes the SQ refcount underflow; this bug affects the queue kref through an unsynchronized state transition.
Quoted 7 lines
I am available for further discussion and testing.
Thanks,
Shivam Kumar
PhD Student, Computer Science
SYNE Lab, Syracuse University
[email protected]
I'll try to have a look at this over the weekend.
-ck
Re: [Bug Report] nvmet-tcp: unbalanced percpu_ref_put on data digest error after nvmet_req_init failure causes refcount underflow, use-after-free, and permanent workqueue deadlock
Hi all,
Just following up on this report. Please let me know if there are any questions about the analysis or if any additional information would be helpful.
Thanks,
Shivam Kumar
Quoted 164 lines · Chaitanya Kulkarni
+linux-nvme list as well.
Quoted 147 lines · Shivam Kumar
Hi Greg, Christoph, Sagi, Chaitanya
Following up on my previous report (
percpu_refunderflow innvmet_tcp_try_recv_ddgst). During testing with thepercpu_refpatch applied, I identified a separate vulnerability triggered by the same PoC. This one affects the queue kref (not the SQpercpu_ref) and has a different root cause.# Bug Overview
nvmet_tcp_handle_icreq()setsqueue->state=NVMET_TCP_Q_LIVEwithout holdingstate_lockand without checking whether teardown has already started. When a client closes the connection immediately after sending an ICReq and a malformed CapsuleCmd, the FIN can arrive and be processed beforeio_workreads the ICReq from the socket buffer. The FIN handler setsqueue->state=NVMET_TCP_Q_DISCONNECTINGand callskref_put(1->0). Thenio_workprocesses the ICReq andhandle_icreqblindly overwrites the state back toNVMET_TCP_Q_LIVE. This defeats the guard innvmet_tcp_schedule_release_queue(), which relies on state ==NVMET_TCP_Q_DISCONNECTINGto prevent a secondkref_put.When the subsequent digest error triggers
nvmet_tcp_fatal_error()->kernel_sock_shutdown(), the socket teardown synchronously processes any pending TCP packets in the socket backlog viarelease_sock()->tcp_done()->nvmet_tcp_state_change()->nvmet_tcp_schedule_release_queue(). Because the state was overwritten to LIVE, the guard passes andkref_putis called on an already-zero kref, triggering the underflow.# Root Cause
The vulnerable code in
nvmet_tcp_handle_icreq()(tcp.c):
queue->state=NVMET_TCP_Q_LIVE;nvmet_prepare_receive_pdu(queue);
return 0;This state transition is not protected by
state_lockand does not check the current state. All other state transitions toNVMET_TCP_Q_DISCONNECTINGinnvmet_tcp_schedule_release_queue()are protected by the spinlock, buthandle_icreqbypasses this synchronization entirely.# Race Sequence
The PoC sends ICReq + CapsuleCmd +
close()in rapid succession. On the wire:[TCP handshake] -> [ICReq] -> [CapsuleCmd] -> [FIN]
All data arrives in order in the socket buffer, but the FIN triggers a TCP state change callback (softirq context) that races with
io_work(process context) reading the application data.# ftrace evidence (queue 29)Step 1: FIN arrives, teardown begins (softirq context):
kworker/-74 1b..2. 125826488us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 0, kref=1, caller=tcp_fin+0x278/0x4f0
kworker/-74 1b..2. 125826492us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=1, caller=tcp_fin+0x278/0x4f0State transitions from CONNECTING(0) to DISCONNECTING(3), kref decremented 1->0.
Step 2:
io_workprocesses ICReq, overwrites state (process context):kworker/-74 1..... 125826529us :
nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pdubefore changing state is 3, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
kworker/-74 1..... 125826532us :nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pduafter changing state is 2, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
handle_icreqsee's state=DISCONNECTING(3) but overwrites it to LIVE(2) without holdingstate_lock. kref is already 0.Step 3: Digest error triggers
fatal_error->kernel_sock_shutdown(process context):kworker/-74 1..... 125831291us :
nvmet_tcp_fatal_error: DEBUG queue 29:fatal_error, state=2, ctrl=0000000000000000, caller=nvmet_tcp_try_recv_ddgst+0x74f/0x9a0Step 4:
kernel_sock_shutdown->release_sockprocesses backlog ->tcp_donere-entersstate_change, guard bypassed because state is LIVE(2):kworker/-74 1b..2. 125831626us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 2, kref=0, caller=tcp_done+0x1d7/0x350
kworker/-74 1b..2. 125831629us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=0, caller=tcp_done+0x1d7/0x350
schedule_release_queuesee's state=LIVE(2), guard passes,kref_putcalled on kref=0 -> underflow -> CRASH.The critical evidence is Step 2:
handle_icreqreads state=3(DISCONNECTING) and writes state=2(LIVE) — proving the unsynchronized overwrite.# Additional Observation: Double
fatal_errorCallThe ftrace also shows that
fatal_erroris called twice per error: once fromnvmet_tcp_try_recv_ddgst(digest mismatch) and again fromnvmet_tcp_socket_errorinnvmet_tcp_io_work(propagated error return). Both calls invokekernel_sock_shutdown, doubling the window for the reentrantstate_change.# Crash Trace (with
percpu_refpatch applied)
refcount_t: underflow; use-after-free.WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xc5/0x110 Workqueue: nvmet_tcp_wq nvmet_tcp_io_work Call Trace: <TASK> nvmet_tcp_state_change+0x3e6/0x470 tcp_done+0x1d7/0x350 tcp_rcv_state_process+0x392c/0x6d80 tcp_v4_do_rcv+0x1b1/0xa80 __release_sock+0x2ab/0x340 release_sock+0x58/0x1c0 inet_shutdown+0x1f6/0x3c0 nvmet_tcp_fatal_error+0x13c/0x1c0 nvmet_tcp_try_recv_ddgst+0x74f/0x9a0 nvmet_tcp_io_work+0xd43/0x1fc0 process_one_work+0x8cf/0x1a40# Affected Code
- Subsystem:
drivers/nvme/target/tcp.c
- Function:nvmet_tcp_handle_icreq()
- Tested on: Linux 7.0-rc3# Suggested Fix Direction
The state transition in
nvmet_tcp_handle_icreq()needs to be synchronized understate_lockwith a check forNVMET_TCP_Q_DISCONNECTING. If the queue is already tearing down,handle_icreqshould not set the state to LIVE and should stop processing. I'll defer to you on the exact error handling semantics (return value and cleanup path).This bug is independent of the
percpu_refunderflow in the previous report. Thepercpu_refpatch fixes the SQ refcount underflow; this bug affects the queue kref through an unsynchronized state transition.Quoted 7 lines
I am available for further discussion and testing.
Thanks,
Shivam Kumar
PhD Student, Computer Science
SYNE Lab, Syracuse University
[email protected]I'll try to have a look at this over the weekend.
-ck
Re: [Bug Report] nvmet-tcp: unbalanced percpu_ref_put on data digest error after nvmet_req_init failure causes refcount underflow, use-after-free, and permanent workqueue deadlock
Shivam,
Quoted 6 lines · Shivam Kumar
Hi all,
Just following up on this report. Please let me know if there are any questions about the analysis or if any additional information would be helpful.
Quoted 167 lines
Thanks,
Shivam KumarQuoted 162 lines · Chaitanya Kulkarni
+linux-nvme list as well.
Quoted 147 lines · Shivam Kumar
Hi Greg, Christoph, Sagi, Chaitanya
Following up on my previous report (
percpu_refunderflow innvmet_tcp_try_recv_ddgst). During testing with thepercpu_refpatch applied, I identified a separate vulnerability triggered by the same PoC. This one affects the queue kref (not the SQpercpu_ref) and has a different root cause.# Bug Overview
nvmet_tcp_handle_icreq()setsqueue->state=NVMET_TCP_Q_LIVEwithout holdingstate_lockand without checking whether teardown has already started. When a client closes the connection immediately after sending an ICReq and a malformed CapsuleCmd, the FIN can arrive and be processed beforeio_workreads the ICReq from the socket buffer. The FIN handler setsqueue->state=NVMET_TCP_Q_DISCONNECTINGand callskref_put(1->0). Thenio_workprocesses the ICReq andhandle_icreqblindly overwrites the state back toNVMET_TCP_Q_LIVE. This defeats the guard innvmet_tcp_schedule_release_queue(), which relies on state ==NVMET_TCP_Q_DISCONNECTINGto prevent a secondkref_put.When the subsequent digest error triggers
nvmet_tcp_fatal_error()->kernel_sock_shutdown(), the socket teardown synchronously processes any pending TCP packets in the socket backlog viarelease_sock()->tcp_done()->nvmet_tcp_state_change()->nvmet_tcp_schedule_release_queue(). Because the state was overwritten to LIVE, the guard passes andkref_putis called on an already-zero kref, triggering the underflow.# Root Cause
The vulnerable code in
nvmet_tcp_handle_icreq()(tcp.c):
queue->state=NVMET_TCP_Q_LIVE;nvmet_prepare_receive_pdu(queue);
return 0;This state transition is not protected by
state_lockand does not check the current state. All other state transitions toNVMET_TCP_Q_DISCONNECTINGinnvmet_tcp_schedule_release_queue()are protected by the spinlock, buthandle_icreqbypasses this synchronization entirely.# Race Sequence
The PoC sends ICReq + CapsuleCmd +
close()in rapid succession. On the wire:[TCP handshake] -> [ICReq] -> [CapsuleCmd] -> [FIN]
All data arrives in order in the socket buffer, but the FIN triggers a TCP state change callback (softirq context) that races with
io_work(process context) reading the application data.# ftrace evidence (queue 29)Step 1: FIN arrives, teardown begins (softirq context):
kworker/-74 1b..2. 125826488us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 0, kref=1, caller=tcp_fin+0x278/0x4f0
kworker/-74 1b..2. 125826492us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=1, caller=tcp_fin+0x278/0x4f0State transitions from CONNECTING(0) to DISCONNECTING(3), kref decremented 1->0.
Step 2:
io_workprocesses ICReq, overwrites state (process context):kworker/-74 1..... 125826529us :
nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pdubefore changing state is 3, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
kworker/-74 1..... 125826532us :nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pduafter changing state is 2, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
handle_icreqsee's state=DISCONNECTING(3) but overwrites it to LIVE(2) without holdingstate_lock. kref is already 0.Step 3: Digest error triggers
fatal_error->kernel_sock_shutdown(process context):kworker/-74 1..... 125831291us :
nvmet_tcp_fatal_error: DEBUG queue 29:fatal_error, state=2, ctrl=0000000000000000, caller=nvmet_tcp_try_recv_ddgst+0x74f/0x9a0Step 4:
kernel_sock_shutdown->release_sockprocesses backlog ->tcp_donere-entersstate_change, guard bypassed because state is LIVE(2):kworker/-74 1b..2. 125831626us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 2, kref=0, caller=tcp_done+0x1d7/0x350
kworker/-74 1b..2. 125831629us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=0, caller=tcp_done+0x1d7/0x350
schedule_release_queuesee's state=LIVE(2), guard passes,kref_putcalled on kref=0 -> underflow -> CRASH.The critical evidence is Step 2:
handle_icreqreads state=3(DISCONNECTING) and writes state=2(LIVE) — proving the unsynchronized overwrite.# Additional Observation: Double
fatal_errorCallThe ftrace also shows that
fatal_erroris called twice per error: once fromnvmet_tcp_try_recv_ddgst(digest mismatch) and again fromnvmet_tcp_socket_errorinnvmet_tcp_io_work(propagated error return). Both calls invokekernel_sock_shutdown, doubling the window for the reentrantstate_change.# Crash Trace (with
percpu_refpatch applied)
refcount_t: underflow; use-after-free.WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xc5/0x110 Workqueue: nvmet_tcp_wq nvmet_tcp_io_work Call Trace: <TASK> nvmet_tcp_state_change+0x3e6/0x470 tcp_done+0x1d7/0x350 tcp_rcv_state_process+0x392c/0x6d80 tcp_v4_do_rcv+0x1b1/0xa80 __release_sock+0x2ab/0x340 release_sock+0x58/0x1c0 inet_shutdown+0x1f6/0x3c0 nvmet_tcp_fatal_error+0x13c/0x1c0 nvmet_tcp_try_recv_ddgst+0x74f/0x9a0 nvmet_tcp_io_work+0xd43/0x1fc0 process_one_work+0x8cf/0x1a40# Affected Code
- Subsystem:
drivers/nvme/target/tcp.c
- Function:nvmet_tcp_handle_icreq()
- Tested on: Linux 7.0-rc3# Suggested Fix Direction
The state transition in
nvmet_tcp_handle_icreq()needs to be synchronized understate_lockwith a check forNVMET_TCP_Q_DISCONNECTING. If the queue is already tearing down,handle_icreqshould not set the state to LIVE and should stop processing. I'll defer to you on the exact error handling semantics (return value and cleanup path).This bug is independent of the
percpu_refunderflow in the previous report. Thepercpu_refpatch fixes the SQ refcount underflow; this bug affects the queue kref through an unsynchronized state transition.Quoted 7 lines
I am available for further discussion and testing.
Thanks,
Shivam Kumar
PhD Student, Computer Science
SYNE Lab, Syracuse University
[email protected]I'll try to have a look at this over the weekend.
-ck
Let's avoid top posting. Sorry for the delay.
Can the following patch fix the ref count underflow issue ?
drivers/nvme/target/tcp.c +2 -1
@@ -1310,7 +1310,8 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) queue->idx, cmd->req.cmd->common.command_id, queue->pdu.cmd.hdr.type,le32_to_cpu (cmd->recv_ddgst), le32_to_cpu(cmd->exp_ddgst)); -nvmet_req_uninit (&cmd->req); +if (!(cmd->flags &NVMET_TCP_F_INIT_FAILED )) +nvmet_req_uninit (&cmd->req);nvmet_tcp_free_cmd_buffers (cmd);nvmet_tcp_fatal_error (queue); ret = -EPROTO ;
Signature · 4 lines
2.39.5 and something like following can fix the race between ICReq handling and queue teardown ?
drivers/nvme/target/tcp.c +13 -1
@@ -408,6 +408,8 @@ static void nvmet_tcp_fatal_error(struct nvmet_tcp_queue *queue)static void nvmet_tcp_socket_error (struct nvmet_tcp_queue *queue,int status) { queue->rcv_state =NVMET_TCP_RECV_ERR ; +if (status == -ESHUTDOWN ) +return ;if (status == -EPIPE || status == -ECONNRESET )kernel_sock_shutdown (queue->sock,SHUT_RDWR );else
@@ -922,11 +924,21 @@ static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue) iov.iov_len =sizeof (*icresp); ret =kernel_sendmsg (queue->sock, &msg, &iov,1 , iov.iov_len);if (ret <0 ) { - queue->state =NVMET_TCP_Q_FAILED ; +spin_lock_bh (&queue->state_lock); +if (queue->state !=NVMET_TCP_Q_DISCONNECTING ) + queue->state =NVMET_TCP_Q_FAILED ; +spin_unlock_bh (&queue->state_lock);return ret;/* queue removal will cleanup */ } +spin_lock_bh (&queue->state_lock); +if (queue->state ==NVMET_TCP_Q_DISCONNECTING ) { +spin_unlock_bh (&queue->state_lock); +/* Tell nvmet_tcp_socket_error() teardown is already in progress. */ +return -ESHUTDOWN ; + } queue->state =NVMET_TCP_Q_LIVE ; +spin_unlock_bh (&queue->state_lock);nvmet_prepare_receive_pdu (queue);return 0 ; }
Signature · 4 lines
2.39.5 -ck
Shivam Kumar
Tested-byRe: [Bug Report] nvmet-tcp: unbalanced percpu_ref_put on data digest error after nvmet_req_init failure causes refcount underflow, use-after-free, and permanent workqueue deadlock
Quoted 242 lines · Chaitanya Kulkarni
Shivam,
Quoted 6 lines · Shivam Kumar
Hi all,
Just following up on this report. Please let me know if there are any questions about the analysis or if any additional information would be helpful.
Quoted 167 lines
Thanks,
Shivam KumarQuoted 162 lines · Chaitanya Kulkarni
+linux-nvme list as well.
Quoted 147 lines · Shivam Kumar
Hi Greg, Christoph, Sagi, Chaitanya
Following up on my previous report (
percpu_refunderflow innvmet_tcp_try_recv_ddgst). During testing with thepercpu_refpatch applied, I identified a separate vulnerability triggered by the same PoC. This one affects the queue kref (not the SQpercpu_ref) and has a different root cause.# Bug Overview
nvmet_tcp_handle_icreq()setsqueue->state=NVMET_TCP_Q_LIVEwithout holdingstate_lockand without checking whether teardown has already started. When a client closes the connection immediately after sending an ICReq and a malformed CapsuleCmd, the FIN can arrive and be processed beforeio_workreads the ICReq from the socket buffer. The FIN handler setsqueue->state=NVMET_TCP_Q_DISCONNECTINGand callskref_put(1->0). Thenio_workprocesses the ICReq andhandle_icreqblindly overwrites the state back toNVMET_TCP_Q_LIVE. This defeats the guard innvmet_tcp_schedule_release_queue(), which relies on state ==NVMET_TCP_Q_DISCONNECTINGto prevent a secondkref_put.When the subsequent digest error triggers
nvmet_tcp_fatal_error()->kernel_sock_shutdown(), the socket teardown synchronously processes any pending TCP packets in the socket backlog viarelease_sock()->tcp_done()->nvmet_tcp_state_change()->nvmet_tcp_schedule_release_queue(). Because the state was overwritten to LIVE, the guard passes andkref_putis called on an already-zero kref, triggering the underflow.# Root Cause
The vulnerable code in
nvmet_tcp_handle_icreq()(tcp.c):
queue->state=NVMET_TCP_Q_LIVE;nvmet_prepare_receive_pdu(queue);
return 0;This state transition is not protected by
state_lockand does not check the current state. All other state transitions toNVMET_TCP_Q_DISCONNECTINGinnvmet_tcp_schedule_release_queue()are protected by the spinlock, buthandle_icreqbypasses this synchronization entirely.# Race Sequence
The PoC sends ICReq + CapsuleCmd +
close()in rapid succession. On the wire:[TCP handshake] -> [ICReq] -> [CapsuleCmd] -> [FIN]
All data arrives in order in the socket buffer, but the FIN triggers a TCP state change callback (softirq context) that races with
io_work(process context) reading the application data.# ftrace evidence (queue 29)Step 1: FIN arrives, teardown begins (softirq context):
kworker/-74 1b..2. 125826488us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 0, kref=1, caller=tcp_fin+0x278/0x4f0
kworker/-74 1b..2. 125826492us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=1, caller=tcp_fin+0x278/0x4f0State transitions from CONNECTING(0) to DISCONNECTING(3), kref decremented 1->0.
Step 2:
io_workprocesses ICReq, overwrites state (process context):kworker/-74 1..... 125826529us :
nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pdubefore changing state is 3, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
kworker/-74 1..... 125826532us :nvmet_tcp_try_recv_pdu: DEBUG queue 29: Insidenvmet_tcp_handle_icreq_pduafter changing state is 2, kref=0, caller=nvmet_tcp_io_work+0x12b/0x1fc0
handle_icreqsee's state=DISCONNECTING(3) but overwrites it to LIVE(2) without holdingstate_lock. kref is already 0.Step 3: Digest error triggers
fatal_error->kernel_sock_shutdown(process context):kworker/-74 1..... 125831291us :
nvmet_tcp_fatal_error: DEBUG queue 29:fatal_error, state=2, ctrl=0000000000000000, caller=nvmet_tcp_try_recv_ddgst+0x74f/0x9a0Step 4:
kernel_sock_shutdown->release_sockprocesses backlog ->tcp_donere-entersstate_change, guard bypassed because state is LIVE(2):kworker/-74 1b..2. 125831626us :
nvmet_tcp_state_change: DEBUG queue 29: Inschedule_releasebefore checking state variable state is 2, kref=0, caller=tcp_done+0x1d7/0x350
kworker/-74 1b..2. 125831629us :nvmet_tcp_state_change: DEBUG queue 29:kref_putinschedule_releasestate changed to 3, kref=0, caller=tcp_done+0x1d7/0x350
schedule_release_queuesee's state=LIVE(2), guard passes,kref_putcalled on kref=0 -> underflow -> CRASH.The critical evidence is Step 2:
handle_icreqreads state=3(DISCONNECTING) and writes state=2(LIVE) — proving the unsynchronized overwrite.# Additional Observation: Double
fatal_errorCallThe ftrace also shows that
fatal_erroris called twice per error: once fromnvmet_tcp_try_recv_ddgst(digest mismatch) and again fromnvmet_tcp_socket_errorinnvmet_tcp_io_work(propagated error return). Both calls invokekernel_sock_shutdown, doubling the window for the reentrantstate_change.# Crash Trace (with
percpu_refpatch applied)
refcount_t: underflow; use-after-free.WARNING: lib/refcount.c:28 at refcount_warn_saturate+0xc5/0x110 Workqueue: nvmet_tcp_wq nvmet_tcp_io_work Call Trace: <TASK> nvmet_tcp_state_change+0x3e6/0x470 tcp_done+0x1d7/0x350 tcp_rcv_state_process+0x392c/0x6d80 tcp_v4_do_rcv+0x1b1/0xa80 __release_sock+0x2ab/0x340 release_sock+0x58/0x1c0 inet_shutdown+0x1f6/0x3c0 nvmet_tcp_fatal_error+0x13c/0x1c0 nvmet_tcp_try_recv_ddgst+0x74f/0x9a0 nvmet_tcp_io_work+0xd43/0x1fc0 process_one_work+0x8cf/0x1a40# Affected Code
- Subsystem:
drivers/nvme/target/tcp.c
- Function:nvmet_tcp_handle_icreq()
- Tested on: Linux 7.0-rc3# Suggested Fix Direction
The state transition in
nvmet_tcp_handle_icreq()needs to be synchronized understate_lockwith a check forNVMET_TCP_Q_DISCONNECTING. If the queue is already tearing down,handle_icreqshould not set the state to LIVE and should stop processing. I'll defer to you on the exact error handling semantics (return value and cleanup path).This bug is independent of the
percpu_refunderflow in the previous report. Thepercpu_refpatch fixes the SQ refcount underflow; this bug affects the queue kref through an unsynchronized state transition.Quoted 7 lines
I am available for further discussion and testing.
Thanks,
Shivam Kumar
PhD Student, Computer Science
SYNE Lab, Syracuse University
[email protected]I'll try to have a look at this over the weekend.
-ck
Let's avoid top posting. Sorry for the delay.
Can the following patch fix the ref count underflow issue ?
drivers/nvme/target/tcp.c +2
-2@@ -1310,7 +1310,8 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) queue->idx, cmd->req.cmd->common.command_id, queue->pdu.cmd.hdr.type,le32_to_cpu (cmd->recv_ddgst), le32_to_cpu(cmd->exp_ddgst)); -nvmet_req_uninit (&cmd->req); +if (!(cmd->flags &NVMET_TCP_F_INIT_FAILED )) +nvmet_req_uninit (&cmd->req);nvmet_tcp_free_cmd_buffers (cmd);nvmet_tcp_fatal_error (queue); ret = -EPROTO ; --2.39.5
and something like following can fix the race between ICReq handling and queue teardown ?
drivers/nvme/target/tcp.c +13
-2@@ -408,6 +408,8 @@ static void nvmet_tcp_fatal_error(struct nvmet_tcp_queue *queue)static void nvmet_tcp_socket_error (struct nvmet_tcp_queue *queue,int status) { queue->rcv_state =NVMET_TCP_RECV_ERR ; +if (status == -ESHUTDOWN ) +return ;if (status == -EPIPE || status == -ECONNRESET )kernel_sock_shutdown (queue->sock,SHUT_RDWR );else @@ -922,11 +924,21 @@ static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue) iov.iov_len =sizeof (*icresp); ret =kernel_sendmsg (queue->sock, &msg, &iov,1 , iov.iov_len);if (ret <0 ) { - queue->state =NVMET_TCP_Q_FAILED ; +spin_lock_bh (&queue->state_lock); +if (queue->state !=NVMET_TCP_Q_DISCONNECTING ) + queue->state =NVMET_TCP_Q_FAILED ; +spin_unlock_bh (&queue->state_lock);return ret;/* queue removal will cleanup */ } +spin_lock_bh (&queue->state_lock); +if (queue->state ==NVMET_TCP_Q_DISCONNECTING ) { +spin_unlock_bh (&queue->state_lock); +/* Tell nvmet_tcp_socket_error() teardown is already in progress. */ +return -ESHUTDOWN ; + } queue->state =NVMET_TCP_Q_LIVE ; +spin_unlock_bh (&queue->state_lock);nvmet_prepare_receive_pdu (queue);return 0 ; } --2.39.5
Quoted 3 lines
-ck
Hi Chaitanya
I tested both the patches; these patches fix both the crash paths.
Patch 1 is the same fix I sent earlier, which has Christoph's Reviewed-by.
Thanks,
Shivam Kumar
Re: [Bug Report] nvmet-tcp: unbalanced percpu_ref_put on data digest error after nvmet_req_init failure causes refcount underflow, use-after-free, and permanent workqueue deadlock
Sagi,
Quoted 67 lines · Shivam Kumar
Quoted 62 lines
Can the following patch fix the ref count underflow issue ?
drivers/nvme/target/tcp.c +2
-2@@ -1310,7 +1310,8 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) queue->idx, cmd->req.cmd->common.command_id, queue->pdu.cmd.hdr.type,le32_to_cpu (cmd->recv_ddgst), le32_to_cpu(cmd->exp_ddgst)); -nvmet_req_uninit (&cmd->req); +if (!(cmd->flags &NVMET_TCP_F_INIT_FAILED )) +nvmet_req_uninit (&cmd->req);nvmet_tcp_free_cmd_buffers (cmd);nvmet_tcp_fatal_error (queue); ret = -EPROTO ; --2.39.5
and something like following can fix the race between ICReq handling and queue teardown ?
drivers/nvme/target/tcp.c +13
-2@@ -408,6 +408,8 @@ static void nvmet_tcp_fatal_error(struct nvmet_tcp_queue *queue)static void nvmet_tcp_socket_error (struct nvmet_tcp_queue *queue,int status) { queue->rcv_state =NVMET_TCP_RECV_ERR ; +if (status == -ESHUTDOWN ) +return ;if (status == -EPIPE || status == -ECONNRESET )kernel_sock_shutdown (queue->sock,SHUT_RDWR );else @@ -922,11 +924,21 @@ static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue) iov.iov_len =sizeof (*icresp); ret =kernel_sendmsg (queue->sock, &msg, &iov,1 , iov.iov_len);if (ret <0 ) { - queue->state =NVMET_TCP_Q_FAILED ; +spin_lock_bh (&queue->state_lock); +if (queue->state !=NVMET_TCP_Q_DISCONNECTING ) + queue->state =NVMET_TCP_Q_FAILED ; +spin_unlock_bh (&queue->state_lock);return ret;/* queue removal will cleanup */ } +spin_lock_bh (&queue->state_lock); +if (queue->state ==NVMET_TCP_Q_DISCONNECTING ) { +spin_unlock_bh (&queue->state_lock); +/* Tell nvmet_tcp_socket_error() teardown is already in progress. */ +return -ESHUTDOWN ; + } queue->state =NVMET_TCP_Q_LIVE ; +spin_unlock_bh (&queue->state_lock);nvmet_prepare_receive_pdu (queue);return 0 ; } --2.39.5
Quoted 3 lines
-ck
Hi Chaitanya
Quoted 8 lines
I tested both the patches; these patches fix both the crash paths.
Patch 1 is the same fix I sent earlier, which has Christoph's Reviewed-by.
Tested-by Shivam Kumar<[email protected]>Thanks,
Shivam Kumar
Can you please take a look before I send a series ?
-ck