NTLM auth and sign - Allocate session key/client response dynamically
Subsystems (2)
PATCH 1/3 NTLM auth and sign - Allocate session key/client response dynamically
From: Shirish Pargaonkar <[email protected]>
Start calculating auth response within a session. Move/Add pertinet data structures like session key, server challenge and ntlmv2_hash in a session structure. We should do the calculations within a session before copying session key and response over to server data structures because a session setup can fail.
Only after a very first smb session succeeds, it copy/make its session key, session key of smb connection. This key stays with the smb connection throughout its life. sequence_number within server is set to 0x2.
The authentication Message Authentication Key (mak) which consists of session key followed by client response within structure session_key is now dynamic. Every authentication type allocates the key + response sized memory within its session structure and later either assigns or frees it once the client response is sent and if session's session key becomes connetion's session key.
ntlm/ntlmi authentication functions are rearranged. A function named setup_ntlm_resp(), similar to setup_ntlmv2_resp(), replaces function cifs_calculate_session_key().
size of CIFS_SESS_KEY_SIZE is changed to 16, to reflect the byte size of the key it holds.
--- fs/cifs/cifsencrypt.c | 113 ++++++++++++++++++++++++++++--------------------- fs/cifs/cifsglob.h | 9 +---- fs/cifs/cifspdu.h | 7 +++- fs/cifs/cifsproto.h | 11 ++--- fs/cifs/connect.c | 18 +++++++- fs/cifs/sess.c | 107 +++++++++++++++++----------------------------- fs/cifs/transport.c | 6 +- 7 files changed, 134 insertions(+), 137 deletions(-)
fs/cifs/cifsencrypt.c +65 -48
@@ -43,15 +43,16 @@ extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24);static int cifs_calculate_signature (const struct smb_hdr *cifs_pdu, -const struct session_key *key,char *signature) +struct TCP_Server_Info *server,char *signature) {struct MD5Context context; -if ((cifs_pdu ==NULL ) || (signature ==NULL ) || (key ==NULL )) +if (cifs_pdu ==NULL || signature ==NULL || server ==NULL )return -EINVAL ;cifs_MD5_init (&context); -cifs_MD5_update (&context, (char *)&key->data, key->len); +cifs_MD5_update (&context, server->session_key.response, + server->session_key.len);cifs_MD5_update (&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length);cifs_MD5_final (signature, &context);
@@ -79,8 +80,7 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, server->sequence_number++;spin_unlock (&GlobalMid_Lock); - rc =cifs_calculate_signature (cifs_pdu, &server->session_key, - smb_signature); + rc =cifs_calculate_signature (cifs_pdu, server, smb_signature);if (rc)memset (cifs_pdu->Signature.SecuritySignature,0 ,8 );else
@@ -90,16 +90,17 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, }static int cifs_calc_signature2 (const struct kvec *iov,int n_vec, -const struct session_key *key,char *signature) +struct TCP_Server_Info *server,char *signature) {struct MD5Context context;int i; -if ((iov ==NULL ) || (signature ==NULL ) || (key ==NULL )) +if (iov ==NULL || signature ==NULL || server ==NULL )return -EINVAL ;cifs_MD5_init (&context); -cifs_MD5_update (&context, (char *)&key->data, key->len); +cifs_MD5_update (&context, server->session_key.response, + server->session_key.len);for (i =0 ; i < n_vec; i++) {if (iov[i].iov_len ==0 )continue ;
@@ -146,8 +147,7 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, server->sequence_number++;spin_unlock (&GlobalMid_Lock); - rc =cifs_calc_signature2 (iov, n_vec, &server->session_key, - smb_signature); + rc =cifs_calc_signature2 (iov, n_vec, server, smb_signature);if (rc)memset (cifs_pdu->Signature.SecuritySignature,0 ,8 );else
@@ -157,14 +157,14 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, }int cifs_verify_signature (struct smb_hdr *cifs_pdu, -const struct session_key *session_key, +struct TCP_Server_Info *server,__u32 expected_sequence_number) {unsigned int rc;char server_response_sig[8 ];char what_we_think_sig_should_be[20 ]; -if (cifs_pdu ==NULL || session_key ==NULL ) +if (cifs_pdu ==NULL || server ==NULL )return -EINVAL ;if (cifs_pdu->Command ==SMB_COM_NEGOTIATE )
@@ -193,7 +193,7 @@ int cifs_verify_signature(struct smb_hdr *cifs_pdu,cpu_to_le32 (expected_sequence_number); cifs_pdu->Signature.Sequence.Reserved =0 ; - rc =cifs_calculate_signature (cifs_pdu, session_key, + rc =cifs_calculate_signature (cifs_pdu, server, what_we_think_sig_should_be);if (rc)
@@ -209,18 +209,28 @@ int cifs_verify_signature(struct smb_hdr *cifs_pdu, } -/* We fill in key by putting in 40 byte array which was allocated by caller */ -int cifs_calculate_session_key (struct session_key *key,const char *rn, -const char *password) +/* first calculate 24 bytes ntlm response and then 16 byte session key */ +int setup_ntlm_response (struct cifsSesInfo *ses) { -char temp_key[16 ]; -if ((key ==NULL ) || (rn ==NULL )) +unsigned int temp_len =CIFS_SESS_KEY_SIZE +CIFS_AUTH_RESP_SIZE ; +char temp_key[CIFS_SESS_KEY_SIZE ]; + +if (!ses)return -EINVAL ; -E_md4hash (password, temp_key); -mdfour (key->data.ntlm, temp_key,16 ); -memcpy (key->data.ntlm+16 , rn,CIFS_SESS_KEY_SIZE ); - key->len =40 ; + ses->auth_key.response =kmalloc (temp_len,GFP_KERNEL ); +if (!ses->auth_key.response) { +cERROR (1 ,"NTLM can't allocate (%u bytes) memory" , temp_len); +return -ENOMEM ; + } + ses->auth_key.len = temp_len; + +SMBNTencrypt (ses->password, ses->cryptKey, + ses->auth_key.response +CIFS_SESS_KEY_SIZE ); + +E_md4hash (ses->password, temp_key); +mdfour (ses->auth_key.response, temp_key,CIFS_SESS_KEY_SIZE ); +return 0 ; }
@@ -465,19 +475,13 @@ calc_exit_2: }int -setup_ntlmv2_rsp (struct cifsSesInfo *ses,char *resp_buf, -const struct nls_table *nls_cp) +setup_ntlmv2_rsp (struct cifsSesInfo *ses,const struct nls_table *nls_cp) {int rc; -struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf; +int baselen; +struct ntlmv2_resp *buf;struct HMACMD5Context context; - buf->blob_signature =cpu_to_le32 (0x00000101 ); - buf->reserved =0 ; - buf->time =cpu_to_le64 (cifs_UnixTimeToNT (CURRENT_TIME )); -get_random_bytes (&buf->client_chal,sizeof (buf->client_chal)); - buf->reserved2 =0 ; -if (ses->server->secType == RawNTLMSSP) {if (!ses->domainName) { rc =find_domain_name (ses);
@@ -494,22 +498,38 @@ setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf, } } + baselen =CIFS_SESS_KEY_SIZE +sizeof (struct ntlmv2_resp ); + ses->auth_key.len = baselen + ses->tilen; + ses->auth_key.response =kmalloc (ses->auth_key.len,GFP_KERNEL ); +if (!ses->auth_key.response) { + rc =ENOMEM ; +cERROR (1 ,"%s: Can't allocate auth blob" , __func__); + goto setup_ntlmv2_rsp_ret; + } + + buf = (struct ntlmv2_resp *) + (ses->auth_key.response +CIFS_SESS_KEY_SIZE ); + buf->blob_signature =cpu_to_le32 (0x00000101 ); + buf->reserved =0 ; + buf->time =cpu_to_le64 (cifs_UnixTimeToNT (CURRENT_TIME )); +get_random_bytes (&buf->client_chal,sizeof (buf->client_chal)); + buf->reserved2 =0 ; + +memcpy (ses->auth_key.response + baselen, ses->tiblob, ses->tilen); +/* calculate buf->ntlmv2_hash */ rc =calc_ntlmv2_hash (ses, nls_cp);if (rc) {cERROR (1 ,"could not get v2 hash rc %d" , rc); goto setup_ntlmv2_rsp_ret; } -CalcNTLMv2_response (ses, resp_buf); +CalcNTLMv2_response (ses);/* now calculate the session key for NTLMv2 */ hmac_md5_init_limK_to_64 (ses->ntlmv2_hash,16 , &context); -hmac_md5_update (resp_buf,16 , &context); -hmac_md5_final (ses->auth_key.data.ntlmv2.key, &context); - -memcpy (&ses->auth_key.data.ntlmv2.resp, resp_buf, -sizeof (struct ntlmv2_resp )); - ses->auth_key.len =16 +sizeof (struct ntlmv2_resp ); +hmac_md5_update (ses->auth_key.response +CIFS_SESS_KEY_SIZE , +16 , &context); +hmac_md5_final (ses->auth_key.response, &context);return 0 ;
@@ -521,20 +541,17 @@ setup_ntlmv2_rsp_ret:return rc; } -void CalcNTLMv2_response (const struct cifsSesInfo *ses, -char *v2_session_response) +void CalcNTLMv2_response (const struct cifsSesInfo *ses) { +unsigned int offset =CIFS_SESS_KEY_SIZE +8 ;struct HMACMD5Context context; +/* rest of v2 struct already generated */ -memcpy (v2_session_response +8 , ses->cryptKey,8 ); +memcpy (ses->auth_key.response + offset, ses->cryptKey,8 );hmac_md5_init_limK_to_64 (ses->ntlmv2_hash,16 , &context); -hmac_md5_update (v2_session_response+8 , -sizeof (struct ntlmv2_resp ) -8 , &context); - -if (ses->tilen) -hmac_md5_update (ses->tiblob, ses->tilen, &context); +hmac_md5_update (ses->auth_key.response + offset, + ses->auth_key.len - offset, &context); -hmac_md5_final (v2_session_response, &context); -/* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */ +hmac_md5_final (ses->auth_key.response +CIFS_SESS_KEY_SIZE , &context); }
fs/cifs/cifsglob.h +1 -8
@@ -99,14 +99,7 @@ enum protocolEnum {struct session_key {unsigned int len; -union { -char ntlm[CIFS_SESS_KEY_SIZE +16 ]; -char krb5[CIFS_SESS_KEY_SIZE +16 ];/* BB: length correct? */ -struct { -char key[16 ]; -struct ntlmv2_resp resp; - } ntlmv2; - } data; +char *response; };struct cifs_cred {
fs/cifs/cifspdu.h +6 -1
@@ -131,9 +131,14 @@#define CIFS_CRYPTO_KEY_SIZE (8)/* +* Size of the ntlm client response +*/ +#define CIFS_AUTH_RESP_SIZE (24) + +/* * Size of the session key (crypto key encrypted with the password */ -#define CIFS_SESS_KEY_SIZE (24) +#define CIFS_SESS_KEY_SIZE (16)/* * Maximum user name length
fs/cifs/cifsproto.h +5 -6
@@ -362,13 +362,12 @@ extern int cifs_sign_smb(struct smb_hdr *, struct TCP_Server_Info *, __u32 *);extern int cifs_sign_smb2 (struct kvec *iov,int n_vec,struct TCP_Server_Info *,__u32 *);extern int cifs_verify_signature (struct smb_hdr *, -const struct session_key *session_key, +struct TCP_Server_Info *server,__u32 expected_sequence_number); -extern int cifs_calculate_session_key (struct session_key *key,const char *rn, -const char *pass); -extern void CalcNTLMv2_response (const struct cifsSesInfo *,char *); -extern int setup_ntlmv2_rsp (struct cifsSesInfo *,char *, -const struct nls_table *); +extern void SMBNTencrypt (unsigned char *,unsigned char *,unsigned char *); +extern int setup_ntlm_response (struct cifsSesInfo *); +extern void CalcNTLMv2_response (const struct cifsSesInfo *); +extern int setup_ntlmv2_rsp (struct cifsSesInfo *,const struct nls_table *);#ifdef CONFIG_CIFS_WEAK_PW_HASH extern void calc_lanman_hash (const char *password,const char *cryptkey,bool encrypt,char *lnm_session_key);
fs/cifs/connect.c +15 -3
@@ -175,6 +175,9 @@ cifs_reconnect(struct TCP_Server_Info *server) } server->sequence_number =0 ; server->session_estab = false; +kfree (server->session_key.response); + server->session_key.response =NULL ; + server->session_key.len =0 ;spin_lock (&GlobalMid_Lock);list_for_each (tmp, &server->pending_mid_q) {
@@ -1562,6 +1565,10 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)cifs_fscache_release_client_cookie (server); +kfree (server->session_key.response); + server->session_key.response =NULL ; + server->session_key.len =0 ; + task =xchg (&server->tsk,NULL );if (task)force_sig (SIGKILL , task);
@@ -3178,10 +3185,11 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, }else {mutex_lock (&ses->server->srv_mutex);if (!server->session_estab) { -memcpy (&server->session_key.data, - &ses->auth_key.data, ses->auth_key.len); + server->session_key.response = ses->auth_key.response; server->session_key.len = ses->auth_key.len; - ses->server->session_estab = true; + server->sequence_number =0x2 ; + server->session_estab = true; + ses->auth_key.response =NULL ; }mutex_unlock (&server->srv_mutex);
@@ -3192,6 +3200,10 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses,spin_unlock (&GlobalMid_Lock); } +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + ses->auth_key.len =0 ; +return rc; }
fs/cifs/sess.c +39 -68
185 lines not on this page · raw message
fs/cifs/transport.c +3 -3
@@ -543,7 +543,7 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses, (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))) { rc =cifs_verify_signature (midQ->resp_buf, - &ses->server->session_key, + ses->server, midQ->sequence_number+1 );if (rc) {cERROR (1 ,"Unexpected SMB signature" );
@@ -731,7 +731,7 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses, (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))) { rc =cifs_verify_signature (out_buf, - &ses->server->session_key, + ses->server, midQ->sequence_number+1 );if (rc) {cERROR (1 ,"Unexpected SMB signature" );
@@ -981,7 +981,7 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon, (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))) { rc =cifs_verify_signature (out_buf, - &ses->server->session_key, + ses->server, midQ->sequence_number+1 );if (rc) {cERROR (1 ,"Unexpected SMB signature" );
Signature · 1 line
1.6.0.2
A message that is not in the archive (sent off-list or not yet received): 1287661375-6954-1-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Quoted 40 lines · On Thu, 21 Oct 2010 06:42:55 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…
From: Shirish Pargaonkar <[email protected]>
Start calculating auth response within a session. Move/Add pertinet data structures like session key, server challenge and
ntlmv2_hashin a session structure. We should do the calculations within a session before copying session key and response over to server data structures because a session setup can fail.Only after a very first smb session succeeds, it copy/make its session key, session key of smb connection. This key stays with the smb connection throughout its life.
sequence_numberwithin server is set to0x2.The authentication Message Authentication Key (mak) which consists of session key followed by client response within structure
session_keyis now dynamic. Every authentication type allocates the key + response sized memory within its session structure and later either assigns or frees it once the client response is sent and if session's session key becomes connetion's session key.ntlm/ntlmi authentication functions are rearranged. A function named
setup_ntlm_resp(), similar tosetup_ntlmv2_resp(), replaces functioncifs_calculate_session_key().size of
CIFS_SESS_KEY_SIZEis changed to 16, to reflect the byte size of the key it holds.Signed-off-by Shirish Pargaonkar <[email protected]>--- fs/cifs/cifsencrypt.c | 113 ++++++++++++++++++++++++++++--------------------- fs/cifs/cifsglob.h | 9 +---- fs/cifs/cifspdu.h | 7 +++- fs/cifs/cifsproto.h | 11 ++--- fs/cifs/connect.c | 18 +++++++- fs/cifs/sess.c | 107 +++++++++++++++++----------------------------- fs/cifs/transport.c | 6 +- 7 files changed, 134 insertions(+), 137 deletions(-)
[...]
fs/cifs/connect.c int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, · line 3190
Quoted 10 lines
fs/cifs/connect.c +2
-3@@ -3178,10 +3185,11 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, }else {mutex_lock (&ses->server->srv_mutex);if (!server->session_estab) { -memcpy (&server->session_key.data, - &ses->auth_key.data, ses->auth_key.len); + server->session_key.response = ses->auth_key.response; server->session_key.len = ses->auth_key.len; - ses->server->session_estab = true; + server->sequence_number =0x2 ;
It looks like the sequence_number is protected by the Global_MidLock. It's not held here. Are there potential races with setting this value here?
On a related note, given that the sequence_number is a simple counter, is there a reason to protect it with that lock? Maybe it's a candidate for converstion to atomic_t?
If so, I'd advise doing that conversion in a separate follow-on patch.
Quoted 5 lines
fs/cifs/connect.c +2
-0+ server->session_estab = true; + ses->auth_key.response =NULL ; }mutex_unlock (&server->srv_mutex);
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101025121410.6a0aef5f-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org
Quoted 46 lines · Jeff Layton
Quoted 40 lines · On Thu, 21 Oct 2010 06:42:55 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…
From: Shirish Pargaonkar <[email protected]>
Start calculating auth response within a session. Move/Add pertinet data structures like session key, server challenge and
ntlmv2_hashin a session structure. We should do the calculations within a session before copying session key and response over to server data structures because a session setup can fail.Only after a very first smb session succeeds, it copy/make its session key, session key of smb connection. This key stays with the smb connection throughout its life.
sequence_numberwithin server is set to0x2.The authentication Message Authentication Key (mak) which consists of session key followed by client response within structure
session_keyis now dynamic. Every authentication type allocates the key + response sized memory within its session structure and later either assigns or frees it once the client response is sent and if session's session key becomes connetion's session key.ntlm/ntlmi authentication functions are rearranged. A function named
setup_ntlm_resp(), similar tosetup_ntlmv2_resp(), replaces functioncifs_calculate_session_key().size of
CIFS_SESS_KEY_SIZEis changed to 16, to reflect the byte size of the key it holds.Signed-off-by Shirish Pargaonkar <[email protected]>---
fs/cifs/cifsencrypt.c | 113 ++++++++++++++++++++++++++++---------------------fs/cifs/cifsglob.h| 9 +----
fs/cifs/cifspdu.h | 7 +++-
fs/cifs/cifsproto.h | 11 ++---
fs/cifs/connect.c | 18 +++++++-
fs/cifs/sess.c | 107 +++++++++++++++++-----------------------------fs/cifs/transport.c| 6 +-7 files changed, 134 insertions(+), 137 deletions(-)[...]
Quoted 15 lines
fs/cifs/connect.c int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, · line 3190
Quoted 10 lines
fs/cifs/connect.c +2
-3@@ -3178,10 +3185,11 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, } else { mutex_lock(&ses->server->srv_mutex); if (!server->session_estab) { - memcpy(&server->session_key.data, - &ses->auth_key.data, ses->auth_key.len); + server->session_key.response = ses->auth_key.response; server->session_key.len = ses->auth_key.len; - ses->server->session_estab = true; + server->sequence_number = 0x2;It looks like the
sequence_numberis protected by theGlobal_MidLock. It's not held here. Are there potential races with setting this value here?
Jeff, I do not think so. This setting is happening only during the very first smb session setup for that smb connection. Rest of the smb sessions and subsequent smb traffic going on on that smb connection for all the smb sessions ensues only after this step i.e. for signature calculations. So there is no resetting sequence_number by doing this.
If signing is not enabled, this is not even an issue at all.
Quoted 15 lines
On a related note, given that the
sequence_numberis a simple counter, is there a reason to protect it with that lock? Maybe it's a candidate for converstion toatomic_t?If so, I'd advise doing that conversion in a separate follow-on patch.
fs/cifs/connect.c int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, · line 3195
Quoted 5 lines
fs/cifs/connect.c +2
-0+ server->session_estab = true; + ses->auth_key.response = NULL; } mutex_unlock(&server->srv_mutex);--
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): AANLkTik0aRH9NcBjR0AGVgc5V-TwEsV3uMYJ_PhcqvD8-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 63 lines · On Mon, 25 Oct 2010 11:26:53 -0500 Shirish Pargaonkar…
Quoted 46 lines · Jeff Layton
Quoted 40 lines · On Thu, 21 Oct 2010 06:42:55 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…
From: Shirish Pargaonkar <[email protected]>
Start calculating auth response within a session. Move/Add pertinet data structures like session key, server challenge and
ntlmv2_hashin a session structure. We should do the calculations within a session before copying session key and response over to server data structures because a session setup can fail.Only after a very first smb session succeeds, it copy/make its session key, session key of smb connection. This key stays with the smb connection throughout its life.
sequence_numberwithin server is set to0x2.The authentication Message Authentication Key (mak) which consists of session key followed by client response within structure
session_keyis now dynamic. Every authentication type allocates the key + response sized memory within its session structure and later either assigns or frees it once the client response is sent and if session's session key becomes connetion's session key.ntlm/ntlmi authentication functions are rearranged. A function named
setup_ntlm_resp(), similar tosetup_ntlmv2_resp(), replaces functioncifs_calculate_session_key().size of
CIFS_SESS_KEY_SIZEis changed to 16, to reflect the byte size of the key it holds.Signed-off-by Shirish Pargaonkar <[email protected]>---
fs/cifs/cifsencrypt.c | 113 ++++++++++++++++++++++++++++---------------------fs/cifs/cifsglob.h| 9 +----
fs/cifs/cifspdu.h | 7 +++-
fs/cifs/cifsproto.h | 11 ++---
fs/cifs/connect.c | 18 +++++++-
fs/cifs/sess.c | 107 +++++++++++++++++-----------------------------fs/cifs/transport.c| 6 +-7 files changed, 134 insertions(+), 137 deletions(-)[...]
Quoted 15 lines
fs/cifs/connect.c int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, · line 3190
Quoted 10 lines
fs/cifs/connect.c +2
-3@@ -3178,10 +3185,11 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, } else { mutex_lock(&ses->server->srv_mutex); if (!server->session_estab) { - memcpy(&server->session_key.data, - &ses->auth_key.data, ses->auth_key.len); + server->session_key.response = ses->auth_key.response; server->session_key.len = ses->auth_key.len; - ses->server->session_estab = true; + server->sequence_number = 0x2;It looks like the
sequence_numberis protected by theGlobal_MidLock. It's not held here. Are there potential races with setting this value here?
Quoted 7 lines
Jeff, I do not think so. This setting is happening only during the very first smb session setup for that smb connection. Rest of the smb sessions and subsequent smb traffic going on on that smb connection for all the smb sessions ensues only after this step i.e. for signature calculations. So there is no resetting
sequence_numberby doing this.
If signing is not enabled, this is not even an issue at all.
I'm not sure I follow...
The Global_MidLock is taken in the other places where this counter is incremented. Why is it OK to set it here outside of that lock?
Isn't it possible for two session setups to proceed in parallel? If not, what serializes them?
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101025131344.65779724-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org
Quoted 76 lines · Jeff Layton
Quoted 63 lines · On Mon, 25 Oct 2010 11:26:53 -0500 Shirish Pargaonkar…
Quoted 46 lines · Jeff Layton
Quoted 40 lines · On Thu, 21 Oct 2010 06:42:55 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…
From: Shirish Pargaonkar <[email protected]>
Start calculating auth response within a session. Move/Add pertinet data structures like session key, server challenge and
ntlmv2_hashin a session structure. We should do the calculations within a session before copying session key and response over to server data structures because a session setup can fail.Only after a very first smb session succeeds, it copy/make its session key, session key of smb connection. This key stays with the smb connection throughout its life.
sequence_numberwithin server is set to0x2.The authentication Message Authentication Key (mak) which consists of session key followed by client response within structure
session_keyis now dynamic. Every authentication type allocates the key + response sized memory within its session structure and later either assigns or frees it once the client response is sent and if session's session key becomes connetion's session key.ntlm/ntlmi authentication functions are rearranged. A function named
setup_ntlm_resp(), similar tosetup_ntlmv2_resp(), replaces functioncifs_calculate_session_key().size of
CIFS_SESS_KEY_SIZEis changed to 16, to reflect the byte size of the key it holds.Signed-off-by Shirish Pargaonkar <[email protected]>---
fs/cifs/cifsencrypt.c | 113 ++++++++++++++++++++++++++++---------------------fs/cifs/cifsglob.h| 9 +----
fs/cifs/cifspdu.h | 7 +++-
fs/cifs/cifsproto.h | 11 ++---
fs/cifs/connect.c | 18 +++++++-
fs/cifs/sess.c | 107 +++++++++++++++++-----------------------------fs/cifs/transport.c| 6 +-7 files changed, 134 insertions(+), 137 deletions(-)[...]
Quoted 15 lines
Review int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, · line 3190
Quoted 10 lines
(quoted diff) +2
-3@@ -3178,10 +3185,11 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, } else { mutex_lock(&ses->server->srv_mutex); if (!server->session_estab) { - memcpy(&server->session_key.data, - &ses->auth_key.data, ses->auth_key.len); + server->session_key.response = ses->auth_key.response; server->session_key.len = ses->auth_key.len; - ses->server->session_estab = true; + server->sequence_number = 0x2;It looks like the
sequence_numberis protected by theGlobal_MidLock. It's not held here. Are there potential races with setting this value here?Quoted 7 lines
Jeff, I do not think so. This setting is happening only during the very first smb session setup for that smb connection. Rest of the smb sessions and subsequent smb traffic going on on that smb connection for all the smb sessions ensues only after this step i.e. for signature calculations. So there is no resetting
sequence_numberby doing this.
If signing is not enabled, this is not even an issue at all.I'm not sure I follow...
Quoted 9 lines
The
Global_MidLockis taken in the other places where this counter is incremented. Why is it OK to set it here outside of that lock?Isn't it possible for two session setups to proceed in parallel? If not, what serializes them?
--
Jeff Layton <[email protected]>
Jeff, for a brand new smb connection, if the session connections race, they are serialized on srv_mutex cifs_setup_session. Only one session setup grabs that lock does change server's sequene number, none of the ones that follow can reset sequence_number because session_estab is true now.
A message that is not in the archive (sent off-list or not yet received): AANLkTikwVH2L+oXRT8+jp=Xg4H_DMoaezCvcp04kxffx-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 83 lines · On Mon, 25 Oct 2010 12:39:54 -0500 Shirish Pargaonkar…
Quoted 82 lines · Jeff Layton
Quoted 63 lines · On Mon, 25 Oct 2010 11:26:53 -0500 Shirish Pargaonkar…
Quoted 46 lines · Jeff Layton
Quoted 40 lines · On Thu, 21 Oct 2010 06:42:55 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…
From: Shirish Pargaonkar <[email protected]>
Start calculating auth response within a session. Move/Add pertinet data structures like session key, server challenge and
ntlmv2_hashin a session structure. We should do the calculations within a session before copying session key and response over to server data structures because a session setup can fail.Only after a very first smb session succeeds, it copy/make its session key, session key of smb connection. This key stays with the smb connection throughout its life.
sequence_numberwithin server is set to0x2.The authentication Message Authentication Key (mak) which consists of session key followed by client response within structure
session_keyis now dynamic. Every authentication type allocates the key + response sized memory within its session structure and later either assigns or frees it once the client response is sent and if session's session key becomes connetion's session key.ntlm/ntlmi authentication functions are rearranged. A function named
setup_ntlm_resp(), similar tosetup_ntlmv2_resp(), replaces functioncifs_calculate_session_key().size of
CIFS_SESS_KEY_SIZEis changed to 16, to reflect the byte size of the key it holds.Signed-off-by Shirish Pargaonkar <[email protected]>---
fs/cifs/cifsencrypt.c | 113 ++++++++++++++++++++++++++++---------------------fs/cifs/cifsglob.h| 9 +----
fs/cifs/cifspdu.h | 7 +++-
fs/cifs/cifsproto.h | 11 ++---
fs/cifs/connect.c | 18 +++++++-
fs/cifs/sess.c | 107 +++++++++++++++++-----------------------------fs/cifs/transport.c| 6 +-7 files changed, 134 insertions(+), 137 deletions(-)[...]
Quoted 15 lines
Review int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, · line 3190
Quoted 10 lines
(quoted diff) +2
-3@@ -3178,10 +3185,11 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, } else { mutex_lock(&ses->server->srv_mutex); if (!server->session_estab) { - memcpy(&server->session_key.data, - &ses->auth_key.data, ses->auth_key.len); + server->session_key.response = ses->auth_key.response; server->session_key.len = ses->auth_key.len; - ses->server->session_estab = true; + server->sequence_number = 0x2;It looks like the
sequence_numberis protected by theGlobal_MidLock. It's not held here. Are there potential races with setting this value here?Quoted 7 lines
Jeff, I do not think so. This setting is happening only during the very first smb session setup for that smb connection. Rest of the smb sessions and subsequent smb traffic going on on that smb connection for all the smb sessions ensues only after this step i.e. for signature calculations. So there is no resetting
sequence_numberby doing this.
If signing is not enabled, this is not even an issue at all.I'm not sure I follow...
The
Global_MidLockis taken in the other places where this counter is incremented. Why is it OK to set it here outside of that lock?Isn't it possible for two session setups to proceed in parallel? If not, what serializes them?
Quoted 8 lines
Quoted 3 lines
--
Jeff Layton <[email protected]>Jeff, for a brand new smb connection, if the session connections race, they are serialized on
srv_mutexcifs_setup_session. Only one session setup grabs that lock does change server's sequene number, none of the ones that follow can resetsequence_numberbecausesession_estabis true now.
Ok. I think the locking around the sequence number could stand some improvement. It seems a bit complex -- maybe a simple atomic counter would be more appropriate? In any case, it's probably better to leave that for a later patch.
You can add my reviewed-by on this one. I don't see any obvious problems now.
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101025134751.068706f0-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org
Quoted 11 lines
Ok. I think the locking around the sequence number could stand some improvement. It seems a bit complex -- maybe a simple atomic counter would be more appropriate? In any case, it's probably better to leave that for a later patch.
You can add my reviewed-by on this one. I don't see any obvious problems now.
--
Jeff Layton <[email protected]>
Jeff, Thanks. I see three issues that need to be addressed once these NTLM patches are through.
1. purging code obsolet'ed by kernel crypto apis.
2. revisiting logic in cifs_reconnect()
3. data type and/or locking around sequence_number