smb:client: fix possible use after free problems
Subsystems (3)
Stefan Metzmacher
reviewercover smb:client: fix possible use after free problems
While refactoring the client and server smbdirect code I noticed a few problems where we might hit use after free style problems.
In order to allow backports I decided to fix the problems before trying to move things to common code.
The server has similar problems, I'll send a separate patchset for the server as they are independed.
Stefan Metzmacher (5):
smb: client: let send_done() cleanup before calling
smbd_disconnect_rdma_connection()
smb: client: remove separate empty_packet_queue
smb: client: make sure we call ib_dma_unmap_single() only if we called
ib_dma_map_single already
smb: client: let recv_done() cleanup before notifying the callers.
smb: client: let recv_done() avoid touching data_transfer after
cleanup/move
fs/smb/client/cifs_debug.c | 6 +- fs/smb/client/smbdirect.c | 124 ++++++++++++------------------------- fs/smb/client/smbdirect.h | 4 -- 3 files changed, 42 insertions(+), 92 deletions(-)
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerPATCH 1/5 smb: client: let send_done() cleanup before calling smbd_disconnect_rdma_connection()
We should call ib_dma_unmap_single() and mempool_free() before calling smbd_disconnect_rdma_connection().
And smbd_disconnect_rdma_connection() needs to be the last function to call as all other state might already be gone after it returns.
--- fs/smb/client/smbdirect.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
fs/smb/client/smbdirect.c +8 -6
@@ -281,18 +281,20 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)log_rdma_send (INFO ,"smbd_request 0x%p completed wc->status=%d\n" , request, wc->status); -if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) { -log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , - wc->status, wc->opcode); -smbd_disconnect_rdma_connection (request->info); - } -for (i =0 ; i < request->num_sge; i++)ib_dma_unmap_single (sc->ib.dev, request->sge[i].addr, request->sge[i].length,DMA_TO_DEVICE ); +if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) { +log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , + wc->status, wc->opcode); +mempool_free (request, request->info->request_mempool); +smbd_disconnect_rdma_connection (request->info); +return ; + } +if (atomic_dec_and_test (&request->info->send_pending))wake_up (&request->info->wait_send_pending);
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerRe: [PATCH 1/5] smb: client: let send_done() cleanup before calling smbd_disconnect_rdma_connection()
Hi Steve,
can you please squash this into the commit? Otherwise it introduces as new use-after-free problem with request->info.
fs/smb/client/smbdirect.c +2 -2
@@ -286,8 +286,8 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) {log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , wc->status, wc->opcode); -mempool_free (request, request->info->request_mempool); -smbd_disconnect_rdma_connection (request->info); +mempool_free (request, info->request_mempool); +smbd_disconnect_rdma_connection (info);return ; }
Thanks!
metze
Am 04.08.25 um 14:10 schrieb Stefan Metzmacher:
Quoted 48 lines
We should call
ib_dma_unmap_single()andmempool_free()before callingsmbd_disconnect_rdma_connection().And
smbd_disconnect_rdma_connection()needs to be the last function to call as all other state might already be gone after it returns.Cc Steve French <[email protected]>Cc Tom Talpey <[email protected]>Cc Long Li <[email protected]>Fixes f198186aa9bb ("CIFS: SMBD: Establish SMB Direct connection")Signed-off-by Stefan Metzmacher <[email protected]>--- fs/smb/client/smbdirect.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/smb/client/smbdirect.c +8
-6@@ -281,18 +281,20 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)log_rdma_send (INFO ,"smbd_request 0x%p completed wc->status=%d\n" , request, wc->status); -if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) { -log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , - wc->status, wc->opcode); -smbd_disconnect_rdma_connection (request->info); - } -for (i =0 ; i < request->num_sge; i++)ib_dma_unmap_single (sc->ib.dev, request->sge[i].addr, request->sge[i].length,DMA_TO_DEVICE ); +if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) { +log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , + wc->status, wc->opcode); +mempool_free (request, request->info->request_mempool); +smbd_disconnect_rdma_connection (request->info); +return ; + } +if (atomic_dec_and_test (&request->info->send_pending))wake_up (&request->info->wait_send_pending);
Re: [PATCH 1/5] smb: client: let send_done() cleanup before calling smbd_disconnect_rdma_connection()
Done
Quoted 74 lines · Stefan Metzmacher
Hi Steve,
can you please squash this into the commit? Otherwise it introduces as new use-after-free problem with
request->info.fs/smb/client/smbdirect.c +2
-2@@ -286,8 +286,8 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) {log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , wc->status, wc->opcode); -mempool_free (request, request->info->request_mempool); -smbd_disconnect_rdma_connection (request->info); +mempool_free (request, info->request_mempool); +smbd_disconnect_rdma_connection (info);return ; }Thanks!
metzeAm 04.08.25 um 14:10 schrieb Stefan Metzmacher:
Quoted 48 lines
We should call
ib_dma_unmap_single()andmempool_free()before callingsmbd_disconnect_rdma_connection().And
smbd_disconnect_rdma_connection()needs to be the last function to call as all other state might already be gone after it returns.Cc Steve French <[email protected]>Cc Tom Talpey <[email protected]>Cc Long Li <[email protected]>Fixes f198186aa9bb ("CIFS: SMBD: Establish SMB Direct connection")Signed-off-by Stefan Metzmacher <[email protected]>--- fs/smb/client/smbdirect.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)fs/smb/client/smbdirect.c +8
-6@@ -281,18 +281,20 @@ static void send_done(struct ib_cq *cq, struct ib_wc *wc)log_rdma_send (INFO ,"smbd_request 0x%p completed wc->status=%d\n" , request, wc->status); -if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) { -log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , - wc->status, wc->opcode); -smbd_disconnect_rdma_connection (request->info); - } -for (i =0 ; i < request->num_sge; i++)ib_dma_unmap_single (sc->ib.dev, request->sge[i].addr, request->sge[i].length,DMA_TO_DEVICE ); +if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_SEND ) { +log_rdma_send (ERR ,"wc->status=%d wc->opcode=%d\n" , + wc->status, wc->opcode); +mempool_free (request, request->info->request_mempool); +smbd_disconnect_rdma_connection (request->info); +return ; + } +if (atomic_dec_and_test (&request->info->send_pending))wake_up (&request->info->wait_send_pending);
Signature · 3 lines
Thanks, Steve
Stefan Metzmacher
reviewerPATCH 2/5 smb: client: remove separate empty_packet_queue
In reply to Stefan MetzmacherThere's no need to maintain two lists, we can just have a single list of receive buffers, which are free to use.
It just added unneeded complexity and resulted in ib_dma_unmap_single() not being called from recv_done() for empty keepalive packets.
--- fs/smb/client/cifs_debug.c | 6 ++-- fs/smb/client/smbdirect.c | 62 +++----------------------------------- fs/smb/client/smbdirect.h | 4 --- 3 files changed, 7 insertions(+), 65 deletions(-)
fs/smb/client/cifs_debug.c +2 -4
@@ -481,10 +481,8 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v) server->smbd_conn->receive_credit_target);seq_printf (m,"\nPending send_pending: %x " ,atomic_read (&server->smbd_conn->send_pending)); -seq_printf (m,"\nReceive buffers count_receive_queue: %x " -"count_empty_packet_queue: %x" , - server->smbd_conn->count_receive_queue, - server->smbd_conn->count_empty_packet_queue); +seq_printf (m,"\nReceive buffers count_receive_queue: %x " , + server->smbd_conn->count_receive_queue); seq_printf(m, "\nMR responder_resources: %x " "max_frmr_depth: %x mr_type: %x", server->smbd_conn->responder_resources,
fs/smb/client/smbdirect.c +5 -57
122 lines not on this page · raw message
fs/smb/client/smbdirect.h +0 -4
@@ -110,10 +110,6 @@ struct smbd_connection {int count_receive_queue;spinlock_t receive_queue_lock; -struct list_head empty_packet_queue; -int count_empty_packet_queue; -spinlock_t empty_packet_queue_lock; -wait_queue_head_t wait_receive_queues;/* Reassembly queue */
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerPATCH 3/5 smb: client: make sure we call ib_dma_unmap_single() only if we called ib_dma_map_single already
In reply to Stefan MetzmacherIn case of failures either ib_dma_map_single() might not be called yet or ib_dma_unmap_single() was already called.
We should make sure put_receive_buffer() only calls ib_dma_unmap_single() if needed.
--- fs/smb/client/smbdirect.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
fs/smb/client/smbdirect.c +9 -2
@@ -1057,6 +1057,7 @@ static int smbd_post_recv(if (rc) {ib_dma_unmap_single (sc->ib.dev, response->sge.addr, response->sge.length,DMA_FROM_DEVICE ); + response->sge.length =0 ;smbd_disconnect_rdma_connection (info);log_rdma_recv (ERR ,"ib_post_recv failed rc=%d\n" , rc); }
@@ -1186,8 +1187,13 @@ static void put_receive_buffer(struct smbdirect_socket *sc = &info->socket;unsigned long flags; -ib_dma_unmap_single (sc->ib.dev, response->sge.addr, - response->sge.length,DMA_FROM_DEVICE ); +if (likely (response->sge.length !=0 )) { +ib_dma_unmap_single (sc->ib.dev, + response->sge.addr, + response->sge.length, +DMA_FROM_DEVICE ); + response->sge.length =0 ; + }spin_lock_irqsave (&info->receive_queue_lock, flags);list_add_tail (&response->list, &info->receive_queue);
@@ -1221,6 +1227,7 @@ static int allocate_receive_buffers(struct smbd_connection *info, int num_buf) goto allocate_failed; response->info = info; + response->sge.length =0 ;list_add_tail (&response->list, &info->receive_queue); info->count_receive_queue++; }
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerPATCH 4/5 smb: client: let recv_done() cleanup before notifying the callers.
In reply to Stefan MetzmacherWe should call put_receive_buffer() before waking up the callers.
For the internal error case of response->type being unexpected, we now also call smbd_disconnect_rdma_connection() instead of not waking up the callers at all.
Note that the SMBD_TRANSFER_DATA case still has problems, which will be addressed in the next commit in order to make it easier to review this one.
--- fs/smb/client/smbdirect.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
fs/smb/client/smbdirect.c +8 -6
@@ -454,7 +454,6 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc)if (wc->status !=IB_WC_SUCCESS || wc->opcode !=IB_WC_RECV ) {log_rdma_recv (INFO ,"wc->status=%d opcode=%d\n" , wc->status, wc->opcode); -smbd_disconnect_rdma_connection (info); goto error; }
@@ -471,8 +470,9 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc) info->full_packet_received = true; info->negotiate_done =process_negotiation_response (response, wc->byte_len); +put_receive_buffer (info, response);complete (&info->negotiate_completion); -break ; +return ;/* SMBD data transfer packet */ case SMBD_TRANSFER_DATA :
@@ -529,14 +529,16 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc) }return ; - -default : -log_rdma_recv (ERR , -"unexpected response type=%d\n" , response->type); } +/* +* This is an internal error! +*/ +log_rdma_recv (ERR ,"unexpected response type=%d\n" , response->type); +WARN_ON_ONCE (response->type !=SMBD_TRANSFER_DATA ); error:put_receive_buffer (info, response); +smbd_disconnect_rdma_connection (info); } static struct rdma_cm_id *smbd_create_id(
Signature · 1 line
2.43.0
Stefan Metzmacher
reviewerPATCH 5/5 smb: client: let recv_done() avoid touching data_transfer after cleanup/move
In reply to Stefan MetzmacherCalling enqueue_reassembly() and wake_up_interruptible(&info->wait_reassembly_queue) or put_receive_buffer() means the reponse/data_transfer pointer might get re-used by another thread, which means these should be
the last operations before calling return.
--- fs/smb/client/smbdirect.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
fs/smb/client/smbdirect.c +11 -14
@@ -479,10 +479,6 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc) data_transfer =smbd_response_payload (response); data_length =le32_to_cpu (data_transfer->data_length); -/* -* If this is a packet with data playload place the data in -* reassembly queue and wake up the reading thread -*/ if (data_length) {if (info->full_packet_received) response->first_segment = true;
@@ -491,16 +487,7 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc) info->full_packet_received = false; else info->full_packet_received = true; - -enqueue_reassembly ( - info, - response, - data_length); - }else -put_receive_buffer (info, response); - -if (data_length) -wake_up_interruptible (&info->wait_reassembly_queue); + }atomic_dec (&info->receive_credits); info->receive_credit_target =
@@ -528,6 +515,16 @@ static void recv_done(struct ib_cq *cq, struct ib_wc *wc) info->keep_alive_requested =KEEP_ALIVE_PENDING ; } +/* +* If this is a packet with data playload place the data in +* reassembly queue and wake up the reading thread +*/ +if (data_length) { +enqueue_reassembly (info, response, data_length); +wake_up_interruptible (&info->wait_reassembly_queue); + }else +put_receive_buffer (info, response); +return ; }
Signature · 1 line
2.43.0