smb/server: fix some bugs in preparation for change notify support
Subsystems (2)
ChenXiaoSong
reviewerv2 cover smb/server: fix some bugs in preparation for change notify support
From: ChenXiaoSong <[email protected]>
v1->v2:
- Update some coding style.
- Patch #03: use pr_err_ratelimited().
v1: https://lore.kernel.org/linux-cifs/[email protected]/
I have implemented the ksmbd change notify feature, please see the following GitHub link (some minor changes are also needed):
https://github.com/chenxiaosonggithub/linux/commits/ksmbd-change-notify/
However, to make this feature work properly, some pre-existing bugs need to be fixed first.
ChenXiaoSong (3):
smb/server: fix signing when a response uses more than one iov smb/server: cancel async requests when closing connection smb/server: stop new async work when closing connection
fs/smb/server/connection.c | 20 ++++++++++ fs/smb/server/smb2pdu.c | 76 ++++++++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 23 deletions(-)
Signature · 1 line
2.54.0
ChenXiaoSong
reviewerPATCH v2 1/3 smb/server: fix signing when a response uses more than one iov
From: ChenXiaoSong <[email protected]>
Some SMB responses keep their data in another buffer. The SMB header and the data are then in different iovs.
The old code only handled this for SMB2 READ. For other commands, it signed only the last iov. QUERY_INFO and CHANGE_NOTIFY can also use another iov for their data. Their SMB header was not signed, so Windows will client rejected the response.
Find the iov that starts with the current SMB header. Sign this iov and all iovs after it.
--- fs/smb/server/smb2pdu.c | 52 ++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 14 deletions(-)
fs/smb/server/smb2pdu.c +38 -14
@@ -10624,6 +10624,40 @@ int smb2_check_sign_req(struct ksmbd_work *work)return 1 ; } +/** +* smb2_get_sign_rsp_iov() - get the iovecs used to sign a response +* @work: work that has the response iovecs +* @hdr: SMB2 header of the response +* @n_vec: set to the number of iovecs to sign +* +* Response data may be in another buffer. In this case, the response uses +* more than one iovec. Find the iovec that starts with @hdr. Sign this +* iovec and all iovecs after it. +* +* Return: The first iovec to sign. +*/ +static struct kvec *smb2_get_sign_rsp_iov (struct ksmbd_work *work, +struct smb2_hdr *hdr,int *n_vec) +{ +int i; + +/* +* iov[0] has the RFC1002 message length. It is not part of the SMB2 +* message, so do not sign it. +*/ +for (i =1 ; i <= work->iov_idx; i++) { +if (work->iov[i].iov_base == hdr) { + *n_vec = work->iov_idx - i +1 ; +return &work->iov[i]; + } + } + +WARN_ON_ONCE (work->iov_idx <1 || + work->iov[work->iov_idx].iov_base != hdr); + *n_vec =1 ; +return &work->iov[work->iov_idx]; +} +/** * smb2_set_sign_rsp() - handler for rsp packet sign processing * @work: smb work containing notify command buffer
@@ -10634,18 +10668,13 @@ void smb2_set_sign_rsp(struct ksmbd_work *work)struct smb2_hdr *hdr;char signature[SMB2_HMACSHA256_SIZE ];struct kvec *iov; -int n_vec =1 ; +int n_vec; hdr =ksmbd_resp_buf_curr (work); hdr->Flags |=SMB2_FLAGS_SIGNED ;memset (hdr->Signature,0 ,SMB2_SIGNATURE_SIZE ); -if (hdr->Command ==SMB2_READ ) { - iov = &work->iov[work->iov_idx -1 ]; - n_vec++; - }else { - iov = &work->iov[work->iov_idx]; - } + iov =smb2_get_sign_rsp_iov (work, hdr, &n_vec);ksmbd_sign_smb2_pdu (work->conn, work->sess->sess_key, iov, n_vec, signature);
@@ -10728,7 +10757,7 @@ void smb3_set_sign_rsp(struct ksmbd_work *work)char signature[SMB2_CMACAES_SIZE ];struct kvec *iov;u16 command = conn->ops->get_cmd_val (work); -int n_vec =1 ; +int n_vec;char *signing_key; hdr =ksmbd_resp_buf_curr (work);
@@ -10750,12 +10779,7 @@ void smb3_set_sign_rsp(struct ksmbd_work *work) hdr->Flags |=SMB2_FLAGS_SIGNED ;memset (hdr->Signature,0 ,SMB2_SIGNATURE_SIZE ); -if (hdr->Command ==SMB2_READ ) { - iov = &work->iov[work->iov_idx -1 ]; - n_vec++; - }else { - iov = &work->iov[work->iov_idx]; - } + iov =smb2_get_sign_rsp_iov (work, hdr, &n_vec);ksmbd_sign_smb3_pdu (conn, signing_key, iov, n_vec, signature);memcpy (hdr->Signature, signature,SMB2_SIGNATURE_SIZE );
Signature · 1 line
2.54.0
Re: [PATCH v2 1/3] smb/server: fix signing when a response uses more than one iov
Quoted 10 lines · ChenXiaoSong
Some SMB responses keep their data in another buffer. The SMB header and the data are then in different iovs.
The old code only handled this for SMB2 READ. For other commands, it signed only the last iov.
QUERY_INFOandCHANGE_NOTIFYcan also use another iov for their data. Their SMB header was not signed, so Windows will client rejected the response.Find the iov that starts with the current SMB header. Sign this iov and all iovs after it.
...
fs/smb/server/smb2pdu.c int smb2_check_sign_req(struct ksmbd_work *work) · line 10642
Quoted 5 lines
fs/smb/server/smb2pdu.c +4
-0+static struct kvec *smb2_get_sign_rsp_iov (struct ksmbd_work *work, +struct smb2_hdr *hdr,int *n_vec) +{ +int i;
This is not addressed, why?
fs/smb/server/smb2pdu.c int smb2_check_sign_req(struct ksmbd_work *work) · line 10656
Quoted 13 lines
fs/smb/server/smb2pdu.c +13
-0+/* +* iov[0] has the RFC1002 message length. It is not part of the SMB2 +* message, so do not sign it. +*/ +for (i =1 ; i <= work->iov_idx; i++) { +if (work->iov[i].iov_base == hdr) { + *n_vec = work->iov_idx - i +1 ; +return &work->iov[i]; + } + } + +WARN_ON_ONCE (work->iov_idx <1 || + work->iov[work->iov_idx].iov_base != hdr);
The second part of this check is basically always true. What's the point?
Quoted 3 lines
fs/smb/server/smb2pdu.c +3
-0+ *n_vec =1 ; +return &work->iov[work->iov_idx]; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
ChenXiaoSong
reviewerRe: [PATCH v2 1/3] smb/server: fix signing when a response uses more than one iov
The warning is triggered only when invalid arguments are passed.
Quoted 3 lines · Andy Shevchenko
fs/smb/server/smb2pdu.c int smb2_check_sign_req(struct ksmbd_work *work) · line 10656
Quoted 2 lines
fs/smb/server/smb2pdu.c +2
-0+WARN_ON_ONCE (work->iov_idx <1 || + work->iov[work->iov_idx].iov_base != hdr);The second part of this check is basically always true. What's the point?
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Re: [PATCH v2 1/3] smb/server: fix signing when a response uses more than one iov
Quoted 1 line · ChenXiaoSong
The warning is triggered only when invalid arguments are passed.
Can you elaborate? The second part is the same check as it's done in the for-loop body. The first part checks if we ever go into that loop, so the logic here is either we have an invalid input (we skip the loop) or always true due to repetition of the check inside, meaning that the loop passed without finding the match.
So, what's the point?
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines
fs/smb/server/smb2pdu.c int smb2_check_sign_req(struct ksmbd_work *work) · line 10656
Quoted 2 lines
fs/smb/server/smb2pdu.c +2
-0+WARN_ON_ONCE (work->iov_idx <1 || + work->iov[work->iov_idx].iov_base != hdr);The second part of this check is basically always true. What's the point?
Signature · 2 lines
With Best Regards, Andy Shevchenko
ChenXiaoSong
reviewerRe: [PATCH v2 1/3] smb/server: fix signing when a response uses more than one iov
The condition is always true. The explicit checks were intended to document the invalid state, but WARN_ON_ONCE(1) is clearer. I will update it. Thanks!
Quoted 7 lines · Andy Shevchenko
Can you elaborate? The second part is the same check as it's done in the for-loop body. The first part checks if we ever go into that loop, so the logic here is either we have an invalid input (we skip the loop) or always true due to repetition of the check inside, meaning that the loop passed without finding the match.
So, what's the point?
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerPATCH v2 2/3 smb/server: cancel async requests when closing connection
In reply to ChenXiaoSongFrom: ChenXiaoSong <[email protected]>
An async request may still be waiting when a connection is closed. This can stop the connection from closing.
Cancel active async requests before waiting for them to finish.
--- fs/smb/server/connection.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
fs/smb/server/connection.c +20 -0
@@ -291,6 +291,25 @@ void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work)wake_up_all (&conn->req_running_q); } +static void ksmbd_conn_cancel_async_requests (struct ksmbd_conn *conn) +{ +struct ksmbd_work *work; + +ksmbd_debug (CONN ,"Cancel pending async requests on releasing connection\n" ); +spin_lock (&conn->request_lock); +list_for_each_entry (work, &conn->async_requests, async_request_entry) { +if (work->state !=KSMBD_WORK_ACTIVE ) +continue ; + +ksmbd_debug (CONN ,"Cancel async request id %d\n" , + work->async_id); + work->state =KSMBD_WORK_CANCELLED ; +if (work->cancel_fn) + work->cancel_fn (work->cancel_argv); + } +spin_unlock (&conn->request_lock); +} +void ksmbd_conn_lock (struct ksmbd_conn *conn) {mutex_lock (&conn->srv_mutex);
@@ -589,6 +608,7 @@ int ksmbd_conn_handler_loop(void *p) }ksmbd_conn_set_releasing (conn); +ksmbd_conn_cancel_async_requests (conn);/* Wait till all reference dropped to the Server object*/ ksmbd_debug (CONN ,"Wait for all pending requests(%d)\n" ,atomic_read (&conn->r_count));wait_event (conn->r_count_q,atomic_read (&conn->r_count) ==0 );
Signature · 1 line
2.54.0
Namjae Jeon
maintainerRe: [PATCH v2 2/3] smb/server: cancel async requests when closing connection
fs/smb/server/connection.c void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work) · line 300
Quoted 7 lines
fs/smb/server/connection.c +7
-0+static void ksmbd_conn_cancel_async_requests (struct ksmbd_conn *conn) +{ +struct ksmbd_work *work; + +ksmbd_debug (CONN ,"Cancel pending async requests on releasing connection\n" ); +spin_lock (&conn->request_lock); +list_for_each_entry (work, &conn->async_requests, async_request_entry) {
smb2_notify_cancel_fn() calls list_del_init() on async_request_entry. With list_for_each_entry(), the loop increment then follows the self-linked entry again, causing an infinite loop while holding request_lock ? So, should we use list_for_each_entry_safe() ?
Quoted 15 lines
fs/smb/server/connection.c +12
-0+if (work->state !=KSMBD_WORK_ACTIVE ) +continue ; + +ksmbd_debug (CONN ,"Cancel async request id %d\n" , + work->async_id); + work->state =KSMBD_WORK_CANCELLED ; +if (work->cancel_fn) + work->cancel_fn (work->cancel_argv); + } +spin_unlock (&conn->request_lock); +} +void ksmbd_conn_lock (struct ksmbd_conn *conn) {mutex_lock (&conn->srv_mutex);
ChenXiaoSong
reviewerRe: [PATCH v2 2/3] smb/server: cancel async requests when closing connection
Ok, list_for_each_entry_safe() would indeed be safer. I will use list_for_each_entry_safe() in the next version.
Quoted 5 lines · Namjae Jeon
Quoted 1 line
+
list_for_each_entry(work, &conn->async_requests, async_request_entry){
smb2_notify_cancel_fn()callslist_del_init()onasync_request_entry. Withlist_for_each_entry(), the loop increment then follows the self-linked entry again, causing an infinite loop while holdingrequest_lock? So, should we uselist_for_each_entry_safe()?
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerPATCH v2 3/3 smb/server: stop new async work when closing connection
In reply to ChenXiaoSongFrom: ChenXiaoSong <[email protected]>
A new async request may be added while a connection is closing. The close code may miss this request and wait forever.
Check the connection state before adding the request. Return an error and release the async ID if the connection is closing.
--- fs/smb/server/smb2pdu.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
fs/smb/server/smb2pdu.c +15 -9
@@ -802,21 +802,27 @@ int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)pr_err ("Failed to alloc async message id\n" );return id; } - work->asynchronous = true; - work->async_id = id; -ksmbd_debug (SMB , -"Send interim Response to inform async request id : %d\n" , - work->async_id); +spin_lock (&conn->request_lock); +if (ksmbd_conn_exiting (conn) ||ksmbd_conn_releasing (conn)) { +spin_unlock (&conn->request_lock); +pr_err_ratelimited ("Failed to setup async work: connection is exiting\n" ); +ksmbd_release_id (&conn->async_ida, id); +return -ESHUTDOWN ; + } + work->asynchronous = true; + work->async_id = id; work->cancel_fn = fn; work->cancel_argv = arg; -if (list_empty (&work->async_request_entry)) { -spin_lock (&conn->request_lock); +if (list_empty (&work->async_request_entry))list_add_tail (&work->async_request_entry, &conn->async_requests); -spin_unlock (&conn->request_lock); - } +spin_unlock (&conn->request_lock); + +ksmbd_debug (SMB , +"Send interim Response to inform async request id : %d\n" , + work->async_id);return 0 ; }
Signature · 1 line
2.54.0
Namjae Jeon
maintainerRe: [PATCH v2 3/3] smb/server: stop new async work when closing connection
Quoted 8 lines · ChenXiaoSong
From: ChenXiaoSong <[email protected]>
A new async request may be added while a connection is closing. The close code may miss this request and wait forever.
Check the connection state before adding the request. Return an error and release the async ID if the connection is closing.
setup_async_work() protects only work items registered through that helper. However, smb2_notify() later creates a synthetic in_work and adds it directly to conn->async_requests. Therefore, the connection-close path can finish scanning and cancelling the existing async requests before in_work is added. in_work is then registered after the connection enters the releasing state and is not cancelled. So, we need to protect this direct registration with the same connection-state check, or use a common helper for all async request registrations.
Thanks!
ChenXiaoSong
reviewerRe: [PATCH v2 3/3] smb/server: stop new async work when closing connection
Gaël will update his patch "ksmbd: defer CHANGE_NOTIFY completion instead of STATUS_NOT_IMPLEMENTED". Maybe the next version he won't use synthetic in_work.
Quoted 10 lines · Namjae Jeon
setup_async_work()protects only work items registered through that helper. However,smb2_notify()later creates a syntheticin_workand adds it directly toconn->async_requests. Therefore, the connection-close path can finish scanning and cancelling the existing async requests beforein_workis added.in_workis then registered after the connection enters the releasing state and is not cancelled. So, we need to protect this direct registration with the same connection-state check, or use a common helper for all async request registrations.
Thanks!
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en