Change session key to per smb session for smb2 and smb3
Subsystems (2)
v3 cover Change session key to per smb session for smb2 and smb3
This patchset makes changes to make session key used for smb signature generation, per smb session from per smb connection, for smb2 and smb3. For smb1, the scheme remains the same.
Patch 1 separates common post session setup processing to dialet specific processing. smb3 also generates signing key (based on session key) which is used in signature generation instead of session key.
Patch 2 enables that the keys are exchanged during session setup with NTLMSSP authentication mechanism for smb2 and smb3
Patch 3 starts using the per session session keys for signature generation for smb2 and smb3 after finding for a matching session in the linked list off of smb connection structure.
Shirish Pargaonkar (3):
cifs: Process post session setup code in respective dialect functions.
cifs: Add a variable specific to NTLMSSP for key exchange.
cifs: Start using per session key for smb2/3 for signature generation
fs/cifs/cifsglob.h | 6 ++--- fs/cifs/cifsproto.h | 2 +- fs/cifs/connect.c | 27 +------------------ fs/cifs/misc.c | 1 + fs/cifs/sess.c | 48 +++++++++++++++++++++++++++++---- fs/cifs/smb2pdu.c | 42 +++++++++++++++++++++++++++++ fs/cifs/smb2transport.c | 70 ++++++++++++++++++++++++++++++++++++------------- 7 files changed, 143 insertions(+), 53 deletions(-)
Signature · 1 line
1.8.1.2
A message that is not in the archive (sent off-list or not yet received): 1377783311-3924-1-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
PATCH v3 1/3 cifs: Process post session setup code in respective dialect functions.
Move the post (successful) session setup code to respective dialect routines.
For smb1, session key is per smb connection.
For smb2/smb3, session key is per smb session.
If client and server do not require signing, free session key for smb1/2/3.
If client and server require signing
smb1 - Copy (kmemdup) session key for the first session to connection.
Free session key of that and subsequent sessions on this connection.
smb2 - For every session, keep the session key and free it when the
session is being shutdown.
smb3 - For every session, generate the smb3 signing key using the session key
and then free the session key.
There are two unrelated line formatting changes as well.
--- fs/cifs/connect.c | 27 +-------------------------- fs/cifs/misc.c | 1 + fs/cifs/sess.c | 40 +++++++++++++++++++++++++++++++++++++--- fs/cifs/smb2pdu.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 29 deletions(-)
fs/cifs/connect.c +1 -26
@@ -3826,33 +3826,8 @@ cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,if (server->ops->sess_setup) rc = server->ops->sess_setup (xid, ses, nls_info); -if (rc) { +if (rc)cifs_dbg (VFS ,"Send error in SessSetup = %d\n" , rc); - }else { -mutex_lock (&server->srv_mutex); -if (!server->session_estab) { - server->session_key.response = ses->auth_key.response; - server->session_key.len = ses->auth_key.len; - server->sequence_number =0x2 ; - server->session_estab = true; - ses->auth_key.response =NULL ; -if (server->ops->generate_signingkey) - server->ops->generate_signingkey (server); - } -mutex_unlock (&server->srv_mutex); - -cifs_dbg (FYI ,"CIFS Session Established successfully\n" ); -spin_lock (&GlobalMid_Lock); - ses->status = CifsGood; - ses->need_reconnect = false; -spin_unlock (&GlobalMid_Lock); - } - -kfree (ses->auth_key.response); - ses->auth_key.response =NULL ; - ses->auth_key.len =0 ; -kfree (ses->ntlmssp); - ses->ntlmssp =NULL ;return rc; }
fs/cifs/misc.c +1 -0
@@ -105,6 +105,7 @@ sesInfoFree(struct cifs_ses *buf_to_free) }kfree (buf_to_free->user_name);kfree (buf_to_free->domainName); +kfree (buf_to_free->auth_key.response);kfree (buf_to_free); }
fs/cifs/sess.c +37 -3
@@ -629,7 +629,8 @@ CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, type =select_sectype (ses->server, ses->sectype);cifs_dbg (FYI ,"sess setup type %d\n" , type);if (type == Unspecified) { -cifs_dbg (VFS ,"Unable to select appropriate authentication method!" ); +cifs_dbg (VFS , +"Unable to select appropriate authentication method!" );return -EINVAL ; }
@@ -815,8 +816,9 @@ ssetup_ntlmssp_authenticate: ses->auth_key.response =kmemdup (msg->data, msg->sesskey_len,GFP_KERNEL );if (!ses->auth_key.response) { -cifs_dbg (VFS ,"Kerberos can't allocate (%u bytes) memory" , - msg->sesskey_len); +cifs_dbg (VFS , +"Kerberos can't allocate (%u bytes) memory" , + msg->sesskey_len); rc = -ENOMEM ; goto ssetup_exit; }
@@ -1005,5 +1007,37 @@ ssetup_exit:if ((phase == NtLmChallenge) && (rc ==0 )) goto ssetup_ntlmssp_authenticate; +if (!rc) { +mutex_lock (&ses->server->srv_mutex); +if (!ses->server->session_estab) { +if (ses->server->sign) { + ses->server->session_key.response = +kmemdup (ses->auth_key.response, + ses->auth_key.len,GFP_KERNEL ); +if (!ses->server->session_key.response) { + rc = -ENOMEM ; +mutex_unlock (&ses->server->srv_mutex); + goto keycp_exit; + } + ses->server->session_key.len = + ses->auth_key.len; + } + ses->server->sequence_number =0x2 ; + ses->server->session_estab = true; + } +mutex_unlock (&ses->server->srv_mutex); + +cifs_dbg (FYI ,"CIFS session established successfully\n" ); +spin_lock (&GlobalMid_Lock); + ses->status = CifsGood; + ses->need_reconnect = false; +spin_unlock (&GlobalMid_Lock); + } + +keycp_exit: +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; +kfree (ses->ntlmssp); +return rc; }
fs/cifs/smb2pdu.c +31 -0
@@ -478,6 +478,13 @@ SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, }/* +* If we are here due to reconnect, free per-smb session key +* in case signing was required. +*/ +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + +/* * If memory allocation is successful, caller of this function * frees it. */
@@ -628,6 +635,30 @@ ssetup_exit:/* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */ if ((phase == NtLmChallenge) && (rc ==0 )) goto ssetup_ntlmssp_authenticate; + +if (!rc) { +mutex_lock (&server->srv_mutex); +if (!server->session_estab) { + server->sequence_number =0x2 ; + server->session_estab = true; +if (server->ops->generate_signingkey) + server->ops->generate_signingkey (server); + } +mutex_unlock (&server->srv_mutex); + +cifs_dbg (FYI ,"SMB2/3 session established successfully\n" ); +spin_lock (&GlobalMid_Lock); + ses->status = CifsGood; + ses->need_reconnect = false; +spin_unlock (&GlobalMid_Lock); + } + +if (!server->sign) { +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + } +kfree (ses->ntlmssp); +return rc; }
Signature · 1 line
1.8.1.2
A message that is not in the archive (sent off-list or not yet received): 1377783311-3924-2-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Re: [PATCH 1/3] cifs: Process post session setup code in respective dialect functions.
Quoted 177 lines · On Thu, 29 Aug 2013 08:35:09 -0500 Shirish Pargaonkar…
Move the post (successful) session setup code to respective dialect routines.
For smb1, session key is per smb connection.
For smb2/smb3, session key is per smb session.If client and server do not require signing, free session key for smb1/2/3.
If client and server require signing
smb1 - Copy (kmemdup) session key for the first session to connection.
Free session key of that and subsequent sessions on this connection.
smb2 - For every session, keep the session key and free it when the
session is being shutdown.
smb3 - For every session, generate the smb3 signing key using the session key
and then free the session key.There are two unrelated line formatting changes as well.
--- fs/cifs/connect.c | 27 +-------------------------- fs/cifs/misc.c | 1 + fs/cifs/sess.c | 40 +++++++++++++++++++++++++++++++++++++--- fs/cifs/smb2pdu.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 29 deletions(-)fs/cifs/connect.c +1
-26@@ -3826,33 +3826,8 @@ cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,if (server->ops->sess_setup) rc = server->ops->sess_setup (xid, ses, nls_info); -if (rc) { +if (rc)cifs_dbg (VFS ,"Send error in SessSetup = %d\n" , rc); - }else { -mutex_lock (&server->srv_mutex); -if (!server->session_estab) { - server->session_key.response = ses->auth_key.response; - server->session_key.len = ses->auth_key.len; - server->sequence_number =0x2 ; - server->session_estab = true; - ses->auth_key.response =NULL ; -if (server->ops->generate_signingkey) - server->ops->generate_signingkey (server); - } -mutex_unlock (&server->srv_mutex); - -cifs_dbg (FYI ,"CIFS Session Established successfully\n" ); -spin_lock (&GlobalMid_Lock); - ses->status = CifsGood; - ses->need_reconnect = false; -spin_unlock (&GlobalMid_Lock); - } - -kfree (ses->auth_key.response); - ses->auth_key.response =NULL ; - ses->auth_key.len =0 ; -kfree (ses->ntlmssp); - ses->ntlmssp =NULL ;return rc; }fs/cifs/misc.c +1
-0@@ -105,6 +105,7 @@ sesInfoFree(struct cifs_ses *buf_to_free) }kfree (buf_to_free->user_name);kfree (buf_to_free->domainName); +kfree (buf_to_free->auth_key.response);kfree (buf_to_free); }fs/cifs/sess.c +37
-3@@ -629,7 +629,8 @@ CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, type =select_sectype (ses->server, ses->sectype);cifs_dbg (FYI ,"sess setup type %d\n" , type);if (type == Unspecified) { -cifs_dbg (VFS ,"Unable to select appropriate authentication method!" ); +cifs_dbg (VFS , +"Unable to select appropriate authentication method!" );return -EINVAL ; }@@ -815,8 +816,9 @@ ssetup_ntlmssp_authenticate: ses->auth_key.response =kmemdup (msg->data, msg->sesskey_len,GFP_KERNEL );if (!ses->auth_key.response) { -cifs_dbg (VFS ,"Kerberos can't allocate (%u bytes) memory" , - msg->sesskey_len); +cifs_dbg (VFS , +"Kerberos can't allocate (%u bytes) memory" , + msg->sesskey_len); rc = -ENOMEM ; goto ssetup_exit; }@@ -1005,5 +1007,37 @@ ssetup_exit:if ((phase == NtLmChallenge) && (rc ==0 )) goto ssetup_ntlmssp_authenticate; +if (!rc) { +mutex_lock (&ses->server->srv_mutex); +if (!ses->server->session_estab) { +if (ses->server->sign) { + ses->server->session_key.response = +kmemdup (ses->auth_key.response, + ses->auth_key.len,GFP_KERNEL ); +if (!ses->server->session_key.response) { + rc = -ENOMEM ; +mutex_unlock (&ses->server->srv_mutex); + goto keycp_exit; + } + ses->server->session_key.len = + ses->auth_key.len; + } + ses->server->sequence_number =0x2 ; + ses->server->session_estab = true; + } +mutex_unlock (&ses->server->srv_mutex); + +cifs_dbg (FYI ,"CIFS session established successfully\n" ); +spin_lock (&GlobalMid_Lock); + ses->status = CifsGood; + ses->need_reconnect = false; +spin_unlock (&GlobalMid_Lock); + } + +keycp_exit: +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; +kfree (ses->ntlmssp); +return rc; }fs/cifs/smb2pdu.c +24
-0@@ -478,6 +478,13 @@ SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, }/* +* If we are here due to reconnect, free per-smb session key +* in case signing was required. +*/ +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + +/* * If memory allocation is successful, caller of this function * frees it. */ @@ -628,6 +635,30 @@ ssetup_exit:/* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */ if ((phase == NtLmChallenge) && (rc ==0 )) goto ssetup_ntlmssp_authenticate; + +if (!rc) { +mutex_lock (&server->srv_mutex); +if (!server->session_estab) { + server->sequence_number =0x2 ; + server->session_estab = true; +if (server->ops->generate_signingkey) + server->ops->generate_signingkey (server); + } +mutex_unlock (&server->srv_mutex); + +cifs_dbg (FYI ,"SMB2/3 session established successfully\n" ); +spin_lock (&GlobalMid_Lock); + ses->status = CifsGood; + ses->need_reconnect = false; +spin_unlock (&GlobalMid_Lock); + }
fs/cifs/smb2pdu.c ssetup_exit: · line 664
Quoted 10 lines
fs/cifs/smb2pdu.c +7
-0+ +if (!server->sign) { +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + } +kfree (ses->ntlmssp); +return rc; }
Nice cleanup. I think this looks reasonable.
Another possible cleanup in the future might be to get rid of the ses->ntlmssp field. Since it's only used during session setup, it would probably be better to pass that in as a separate parm, but that can and should be done in a later patch.
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20130906091117.5fc36bd4-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org
Re: [PATCH 1/3] cifs: Process post session setup code in respective dialect functions.
Thanks Jeff. Sure, noted.
Regards,
Shirish
Quoted 200 lines · Jeff Layton
Quoted 177 lines · On Thu, 29 Aug 2013 08:35:09 -0500 Shirish Pargaonkar…
Move the post (successful) session setup code to respective dialect routines.
For smb1, session key is per smb connection.
For smb2/smb3, session key is per smb session.If client and server do not require signing, free session key for smb1/2/3.
If client and server require signing
smb1 - Copy (kmemdup) session key for the first session to connection.
Free session key of that and subsequent sessions on this connection.
smb2 - For every session, keep the session key and free it when the
session is being shutdown.
smb3 - For every session, generate the smb3 signing key using the session key
and then free the session key.There are two unrelated line formatting changes as well.
--- fs/cifs/connect.c | 27 +-------------------------- fs/cifs/misc.c | 1 + fs/cifs/sess.c | 40 +++++++++++++++++++++++++++++++++++++--- fs/cifs/smb2pdu.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 29 deletions(-)fs/cifs/connect.c +1
-26@@ -3826,33 +3826,8 @@ cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,if (server->ops->sess_setup) rc = server->ops->sess_setup (xid, ses, nls_info); -if (rc) { +if (rc)cifs_dbg (VFS ,"Send error in SessSetup = %d\n" , rc); - }else { -mutex_lock (&server->srv_mutex); -if (!server->session_estab) { - server->session_key.response = ses->auth_key.response; - server->session_key.len = ses->auth_key.len; - server->sequence_number =0x2 ; - server->session_estab = true; - ses->auth_key.response =NULL ; -if (server->ops->generate_signingkey) - server->ops->generate_signingkey (server); - } -mutex_unlock (&server->srv_mutex); - -cifs_dbg (FYI ,"CIFS Session Established successfully\n" ); -spin_lock (&GlobalMid_Lock); - ses->status = CifsGood; - ses->need_reconnect = false; -spin_unlock (&GlobalMid_Lock); - } - -kfree (ses->auth_key.response); - ses->auth_key.response =NULL ; - ses->auth_key.len =0 ; -kfree (ses->ntlmssp); - ses->ntlmssp =NULL ;return rc; }fs/cifs/misc.c +1
-0@@ -105,6 +105,7 @@ sesInfoFree(struct cifs_ses *buf_to_free) }kfree (buf_to_free->user_name);kfree (buf_to_free->domainName); +kfree (buf_to_free->auth_key.response);kfree (buf_to_free); }fs/cifs/sess.c +37
-3@@ -629,7 +629,8 @@ CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, type =select_sectype (ses->server, ses->sectype);cifs_dbg (FYI ,"sess setup type %d\n" , type);if (type == Unspecified) { -cifs_dbg (VFS ,"Unable to select appropriate authentication method!" ); +cifs_dbg (VFS , +"Unable to select appropriate authentication method!" );return -EINVAL ; }@@ -815,8 +816,9 @@ ssetup_ntlmssp_authenticate: ses->auth_key.response =kmemdup (msg->data, msg->sesskey_len,GFP_KERNEL );if (!ses->auth_key.response) { -cifs_dbg (VFS ,"Kerberos can't allocate (%u bytes) memory" , - msg->sesskey_len); +cifs_dbg (VFS , +"Kerberos can't allocate (%u bytes) memory" , + msg->sesskey_len); rc = -ENOMEM ; goto ssetup_exit; }@@ -1005,5 +1007,37 @@ ssetup_exit:if ((phase == NtLmChallenge) && (rc ==0 )) goto ssetup_ntlmssp_authenticate; +if (!rc) { +mutex_lock (&ses->server->srv_mutex); +if (!ses->server->session_estab) { +if (ses->server->sign) { + ses->server->session_key.response = +kmemdup (ses->auth_key.response, + ses->auth_key.len,GFP_KERNEL ); +if (!ses->server->session_key.response) { + rc = -ENOMEM ; +mutex_unlock (&ses->server->srv_mutex); + goto keycp_exit; + } + ses->server->session_key.len = + ses->auth_key.len; + } + ses->server->sequence_number =0x2 ; + ses->server->session_estab = true; + } +mutex_unlock (&ses->server->srv_mutex); + +cifs_dbg (FYI ,"CIFS session established successfully\n" ); +spin_lock (&GlobalMid_Lock); + ses->status = CifsGood; + ses->need_reconnect = false; +spin_unlock (&GlobalMid_Lock); + } + +keycp_exit: +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; +kfree (ses->ntlmssp); +return rc; }fs/cifs/smb2pdu.c +24
-0@@ -478,6 +478,13 @@ SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, }/* +* If we are here due to reconnect, free per-smb session key +* in case signing was required. +*/ +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + +/* * If memory allocation is successful, caller of this function * frees it. */ @@ -628,6 +635,30 @@ ssetup_exit:/* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */ if ((phase == NtLmChallenge) && (rc ==0 )) goto ssetup_ntlmssp_authenticate; + +if (!rc) { +mutex_lock (&server->srv_mutex); +if (!server->session_estab) { + server->sequence_number =0x2 ; + server->session_estab = true; +if (server->ops->generate_signingkey) + server->ops->generate_signingkey (server); + } +mutex_unlock (&server->srv_mutex); + +cifs_dbg (FYI ,"SMB2/3 session established successfully\n" ); +spin_lock (&GlobalMid_Lock); + ses->status = CifsGood; + ses->need_reconnect = false; +spin_unlock (&GlobalMid_Lock); + }fs/cifs/smb2pdu.c ssetup_exit: · line 664
Quoted 10 lines
fs/cifs/smb2pdu.c +7
-0+ +if (!server->sign) { +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; + } +kfree (ses->ntlmssp); +return rc; }Nice cleanup. I think this looks reasonable.
Another possible cleanup in the future might be to get rid of the
ses->ntlmsspfield. Since it's only used during session setup, it would probably be better to pass that in as a separate parm, but that can and should be done in a later patch.--
Jeff Layton <[email protected]>
PATCH v3 2/3 cifs: Add a variable specific to NTLMSSP for key exchange.
In reply to a message not in the archiveAdd a variable specific to NTLMSSP authentication to determine whether to exchange keys during negotiation and authentication phases.
Since session key for smb1 is per smb connection, once a very first sesion is established, there is no need for key exchange during subsequent session setups. As a result, smb1 session setup code sets this variable as false.
Since session key for smb2 and smb3 is per smb connection, we need to exchange keys to generate session key for every sesion being established. As a result, smb2/3 session setup code sets this variable as true.
--- fs/cifs/cifsglob.h | 1 + fs/cifs/sess.c | 8 ++++++-- fs/cifs/smb2pdu.c | 1 + 3 files changed, 8 insertions(+), 2 deletions(-)
fs/cifs/cifsglob.h +1 -0
@@ -135,6 +135,7 @@ struct cifs_secmech {/* per smb session structure/fields */ struct ntlmssp_auth { +bool sesskey_per_smbsess;/* whether session key is per smb session */ __u32 client_flags;/* sent by client in type 1 ntlmsssp exchange */ __u32 server_flags;/* sent by server in type 2 ntlmssp exchange */ unsigned char ciphertext[CIFS_CPHTXT_SIZE ];/* sent to server */
fs/cifs/sess.c +6 -2
@@ -428,7 +428,8 @@ void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,NTLMSSP_NEGOTIATE_NTLM |NTLMSSP_NEGOTIATE_EXTENDED_SEC ;if (ses->server->sign) { flags |=NTLMSSP_NEGOTIATE_SIGN ; -if (!ses->server->session_estab) +if (!ses->server->session_estab || + ses->ntlmssp->sesskey_per_smbsess) flags |=NTLMSSP_NEGOTIATE_KEY_XCH ; }
@@ -466,7 +467,8 @@ int build_ntlmssp_auth_blob(unsigned char *pbuffer,NTLMSSP_NEGOTIATE_NTLM |NTLMSSP_NEGOTIATE_EXTENDED_SEC ;if (ses->server->sign) { flags |=NTLMSSP_NEGOTIATE_SIGN ; -if (!ses->server->session_estab) +if (!ses->server->session_estab || + ses->ntlmssp->sesskey_per_smbsess) flags |=NTLMSSP_NEGOTIATE_KEY_XCH ; }
@@ -641,6 +643,8 @@ CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, ses->ntlmssp =kmalloc (sizeof (struct ntlmssp_auth ),GFP_KERNEL );if (!ses->ntlmssp)return -ENOMEM ; + ses->ntlmssp->sesskey_per_smbsess = false; + } ssetup_ntlmssp_authenticate:
fs/cifs/smb2pdu.c +1 -0
@@ -491,6 +491,7 @@ SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, ses->ntlmssp =kmalloc (sizeof (struct ntlmssp_auth ),GFP_KERNEL );if (!ses->ntlmssp)return -ENOMEM ; + ses->ntlmssp->sesskey_per_smbsess = true;/* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */ ses->sectype = RawNTLMSSP;
Signature · 1 line
1.8.1.2
A message that is not in the archive (sent off-list or not yet received): 1377783311-3924-3-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Jeff Layton
Acked-byRe: [PATCH 2/3] cifs: Add a variable specific to NTLMSSP for key exchange.
Quoted 67 lines · On Thu, 29 Aug 2013 08:35:10 -0500 Shirish Pargaonkar…
Add a variable specific to NTLMSSP authentication to determine whether to exchange keys during negotiation and authentication phases.
Since session key for smb1 is per smb connection, once a very first sesion is established, there is no need for key exchange during subsequent session setups. As a result, smb1 session setup code sets this variable as false.
Since session key for smb2 and smb3 is per smb connection, we need to exchange keys to generate session key for every sesion being established. As a result, smb2/3 session setup code sets this variable as true.
--- fs/cifs/cifsglob.h | 1 + fs/cifs/sess.c | 8 ++++++-- fs/cifs/smb2pdu.c | 1 + 3 files changed, 8 insertions(+), 2 deletions(-)fs/cifs/cifsglob.h +1
-0@@ -135,6 +135,7 @@ struct cifs_secmech {/* per smb session structure/fields */ struct ntlmssp_auth { +bool sesskey_per_smbsess;/* whether session key is per smb session */ __u32 client_flags;/* sent by client in type 1 ntlmsssp exchange */ __u32 server_flags;/* sent by server in type 2 ntlmssp exchange */ unsigned char ciphertext[CIFS_CPHTXT_SIZE ];/* sent to server */ fs/cifs/sess.c +6
-2@@ -428,7 +428,8 @@ void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,NTLMSSP_NEGOTIATE_NTLM |NTLMSSP_NEGOTIATE_EXTENDED_SEC ;if (ses->server->sign) { flags |=NTLMSSP_NEGOTIATE_SIGN ; -if (!ses->server->session_estab) +if (!ses->server->session_estab || + ses->ntlmssp->sesskey_per_smbsess) flags |=NTLMSSP_NEGOTIATE_KEY_XCH ; }@@ -466,7 +467,8 @@ int build_ntlmssp_auth_blob(unsigned char *pbuffer,NTLMSSP_NEGOTIATE_NTLM |NTLMSSP_NEGOTIATE_EXTENDED_SEC ;if (ses->server->sign) { flags |=NTLMSSP_NEGOTIATE_SIGN ; -if (!ses->server->session_estab) +if (!ses->server->session_estab || + ses->ntlmssp->sesskey_per_smbsess) flags |=NTLMSSP_NEGOTIATE_KEY_XCH ; }@@ -641,6 +643,8 @@ CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, ses->ntlmssp =kmalloc (sizeof (struct ntlmssp_auth ),GFP_KERNEL );if (!ses->ntlmssp)return -ENOMEM ; + ses->ntlmssp->sesskey_per_smbsess = false; + } ssetup_ntlmssp_authenticate:fs/cifs/smb2pdu.c +0
-0
fs/cifs/smb2pdu.c SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, · line 497
Quoted 7 lines
fs/cifs/smb2pdu.c +1
-0@@ -491,6 +491,7 @@ SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, ses->ntlmssp =kmalloc (sizeof (struct ntlmssp_auth ),GFP_KERNEL );if (!ses->ntlmssp)return -ENOMEM ; + ses->ntlmssp->sesskey_per_smbsess = true;/* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */ ses->sectype = RawNTLMSSP;
PATCH v3 3/3 cifs: Start using per session key for smb2/3 for signature generation
In reply to a message not in the archiveSwitch smb2 code to use per session session key and smb3 code to use per session signing key instead of per connection key to generate signatures.
For that, we need to find a session to fetch the session key to generate signature to match for every request and response packet.
We also forgo checking signature for a session setup response from the server.
--- fs/cifs/cifsglob.h | 5 ++-- fs/cifs/cifsproto.h | 2 +- fs/cifs/smb2pdu.c | 14 ++++++++-- fs/cifs/smb2transport.c | 70 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 67 insertions(+), 24 deletions(-)
fs/cifs/cifsglob.h +2 -3
@@ -367,8 +367,7 @@ struct smb_version_operations {void (*set_lease_key)(struct inode *,struct cifs_fid *fid);/* generate new lease key */ void (*new_lease_key)(struct cifs_fid *fid); -/* The next two functions will need to be changed to per smb session */ -void (*generate_signingkey)(struct TCP_Server_Info *server); +int (*generate_signingkey)(struct cifs_ses *);int (*calc_signature)(struct smb_rqst *rqst,struct TCP_Server_Info *server); int (*query_mf_symlink)(const unsigned char *path, char *pbuf,
@@ -549,7 +548,6 @@ struct TCP_Server_Info {int timeAdj;/* Adjust for difference in server time zone in sec */ __u64 CurrentMid;/* multiplex id - rotating counter */ char cryptkey[CIFS_CRYPTO_KEY_SIZE ];/* used by ntlm, ntlmv2 etc */ -char smb3signingkey[SMB3_SIGN_KEY_SIZE ];/* for signing smb3 packets */ /* 16th byte of RFC1001 workstation name is always null */ char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL ];__u32 sequence_number;/* for signing, protected by srv_mutex */
@@ -732,6 +730,7 @@ struct cifs_ses { bool need_reconnect:1; /* connection reset, uid now invalid */#ifdef CONFIG_CIFS_SMB2 __u16 session_flags; +char smb3signingkey[SMB3_SIGN_KEY_SIZE ];/* for signing smb3 packets */ #endif /* CONFIG_CIFS_SMB2 */ };
fs/cifs/cifsproto.h +1 -1
@@ -435,7 +435,7 @@ extern int setup_ntlm_response(struct cifs_ses *, const struct nls_table *);extern int setup_ntlmv2_rsp (struct cifs_ses *,const struct nls_table *);extern void cifs_crypto_shash_release (struct TCP_Server_Info *);extern int calc_seckey (struct cifs_ses *); -extern void generate_smb3signingkey (struct TCP_Server_Info *); +extern int generate_smb3signingkey (struct cifs_ses *);#ifdef CONFIG_CIFS_WEAK_PW_HASH extern int calc_lanman_hash(const char *password, const char *cryptkey,
fs/cifs/smb2pdu.c +12 -2
@@ -639,11 +639,20 @@ ssetup_exit:if (!rc) {mutex_lock (&server->srv_mutex); +if (server->sign && server->ops->generate_signingkey) { + rc = server->ops->generate_signingkey (ses); +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; +if (rc) { +cifs_dbg (FYI , +"SMB3 session key generation failed\n" ); +mutex_unlock (&server->srv_mutex); + goto keygen_exit; + } + }if (!server->session_estab) { server->sequence_number =0x2 ; server->session_estab = true; -if (server->ops->generate_signingkey) - server->ops->generate_signingkey (server); }mutex_unlock (&server->srv_mutex);
@@ -654,6 +663,7 @@ ssetup_exit:spin_unlock (&GlobalMid_Lock); } +keygen_exit:if (!server->sign) {kfree (ses->auth_key.response); ses->auth_key.response =NULL ;
fs/cifs/smb2transport.c +52 -18
175 lines not on this page · raw message
Signature · 1 line
1.8.1.2
A message that is not in the archive (sent off-list or not yet received): 1377783311-3924-4-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Jeff Layton
Acked-byRe: [PATCH 3/3] cifs: Start using per session key for smb2/3 for signature generation
Quoted 272 lines · On Thu, 29 Aug 2013 08:35:11 -0500 Shirish Pargaonkar…
Switch smb2 code to use per session session key and smb3 code to use per session signing key instead of per connection key to generate signatures.
For that, we need to find a session to fetch the session key to generate signature to match for every request and response packet.
We also forgo checking signature for a session setup response from the server.
--- fs/cifs/cifsglob.h | 5 ++-- fs/cifs/cifsproto.h | 2 +- fs/cifs/smb2pdu.c | 14 ++++++++-- fs/cifs/smb2transport.c | 70 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 67 insertions(+), 24 deletions(-)fs/cifs/cifsglob.h +2
-3@@ -367,8 +367,7 @@ struct smb_version_operations {void (*set_lease_key)(struct inode *,struct cifs_fid *fid);/* generate new lease key */ void (*new_lease_key)(struct cifs_fid *fid); -/* The next two functions will need to be changed to per smb session */ -void (*generate_signingkey)(struct TCP_Server_Info *server); +int (*generate_signingkey)(struct cifs_ses *);int (*calc_signature)(struct smb_rqst *rqst,struct TCP_Server_Info *server); int (*query_mf_symlink)(const unsigned char *path, char *pbuf,@@ -549,7 +548,6 @@ struct TCP_Server_Info {int timeAdj;/* Adjust for difference in server time zone in sec */ __u64 CurrentMid;/* multiplex id - rotating counter */ char cryptkey[CIFS_CRYPTO_KEY_SIZE ];/* used by ntlm, ntlmv2 etc */ -char smb3signingkey[SMB3_SIGN_KEY_SIZE ];/* for signing smb3 packets */ /* 16th byte of RFC1001 workstation name is always null */ char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL ];__u32 sequence_number;/* for signing, protected by srv_mutex */ @@ -732,6 +730,7 @@ struct cifs_ses { bool need_reconnect:1; /* connection reset, uid now invalid */#ifdef CONFIG_CIFS_SMB2 __u16 session_flags; +char smb3signingkey[SMB3_SIGN_KEY_SIZE ];/* for signing smb3 packets */ #endif /* CONFIG_CIFS_SMB2 */ };fs/cifs/cifsproto.h +1
-1@@ -435,7 +435,7 @@ extern int setup_ntlm_response(struct cifs_ses *, const struct nls_table *);extern int setup_ntlmv2_rsp (struct cifs_ses *,const struct nls_table *);extern void cifs_crypto_shash_release (struct TCP_Server_Info *);extern int calc_seckey (struct cifs_ses *); -extern void generate_smb3signingkey (struct TCP_Server_Info *); +extern int generate_smb3signingkey (struct cifs_ses *);#ifdef CONFIG_CIFS_WEAK_PW_HASH extern int calc_lanman_hash(const char *password, const char *cryptkey,fs/cifs/smb2pdu.c +12
-2@@ -639,11 +639,20 @@ ssetup_exit:if (!rc) {mutex_lock (&server->srv_mutex); +if (server->sign && server->ops->generate_signingkey) { + rc = server->ops->generate_signingkey (ses); +kfree (ses->auth_key.response); + ses->auth_key.response =NULL ; +if (rc) { +cifs_dbg (FYI , +"SMB3 session key generation failed\n" ); +mutex_unlock (&server->srv_mutex); + goto keygen_exit; + } + }if (!server->session_estab) { server->sequence_number =0x2 ; server->session_estab = true; -if (server->ops->generate_signingkey) - server->ops->generate_signingkey (server); }mutex_unlock (&server->srv_mutex);@@ -654,6 +663,7 @@ ssetup_exit:spin_unlock (&GlobalMid_Lock); } +keygen_exit:if (!server->sign) {kfree (ses->auth_key.response); ses->auth_key.response =NULL ;fs/cifs/smb2transport.c +51
-18@@ -114,6 +114,23 @@ smb3_crypto_shash_allocate(struct TCP_Server_Info *server)return 0 ; } +static struct cifs_ses * +smb2_find_smb_ses (struct smb2_hdr *smb2hdr,struct TCP_Server_Info *server) +{ +struct cifs_ses *ses; + +spin_lock (&cifs_tcp_ses_lock); +list_for_each_entry (ses, &server->smb_ses_list, smb_ses_list) { +if (ses->Suid != smb2hdr->SessionId) +continue ; +spin_unlock (&cifs_tcp_ses_lock); +return ses; + } +spin_unlock (&cifs_tcp_ses_lock); + +return NULL ; +} +int smb2_calc_signature (struct smb_rqst *rqst,struct TCP_Server_Info *server)@@ -124,6 +141,13 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)struct kvec *iov = rqst->rq_iov;int n_vec = rqst->rq_nvec;struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0 ].iov_base; +struct cifs_ses *ses; + + ses =smb2_find_smb_ses (smb2_pdu, server); +if (!ses) { +cifs_dbg (VFS ,"%s: Could not find session\n" , __func__); +return 0 ; + }memset (smb2_signature,0x0 ,SMB2_HMACSHA256_SIZE );memset (smb2_pdu->Signature,0x0 ,SMB2_SIGNATURE_SIZE );@@ -135,7 +159,7 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) } rc =crypto_shash_setkey (server->secmech.hmacsha256, - server->session_key.response,SMB2_NTLMV2_SESSKEY_SIZE ); + ses->auth_key.response,SMB2_NTLMV2_SESSKEY_SIZE );if (rc) {cifs_dbg (VFS ,"%s: Could not update with response\n" , __func__);return rc;@@ -198,8 +222,8 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)return rc; } -void -generate_smb3signingkey (struct TCP_Server_Info *server) +int +generate_smb3signingkey (struct cifs_ses *ses) {unsigned char zero =0x0 ;__u8 i[4 ] = {0 ,0 ,0 ,1 };@@ -209,90 +233,99 @@ generate_smb3signingkey(struct TCP_Server_Info *server)unsigned char *hashptr = prfhash;memset (prfhash,0x0 ,SMB2_HMACSHA256_SIZE ); -memset (server->smb3signingkey,0x0 ,SMB3_SIGNKEY_SIZE ); +memset (ses->smb3signingkey,0x0 ,SMB3_SIGNKEY_SIZE ); - rc =smb3_crypto_shash_allocate (server); + rc =smb3_crypto_shash_allocate (ses->server);if (rc) {cifs_dbg (VFS ,"%s: crypto alloc failed\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_setkey (server->secmech.hmacsha256, - server->session_key.response,SMB2_NTLMV2_SESSKEY_SIZE ); + rc =crypto_shash_setkey (ses->server->secmech.hmacsha256, + ses->auth_key.response,SMB2_NTLMV2_SESSKEY_SIZE );if (rc) {cifs_dbg (VFS ,"%s: Could not set with session key\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_init (&server->secmech.sdeschmacsha256->shash); + rc =crypto_shash_init (&ses->server->secmech.sdeschmacsha256->shash);if (rc) {cifs_dbg (VFS ,"%s: Could not init sign hmac\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_update (&server->secmech.sdeschmacsha256->shash, + rc =crypto_shash_update (&ses->server->secmech.sdeschmacsha256->shash, i,4 );if (rc) {cifs_dbg (VFS ,"%s: Could not update with n\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_update (&server->secmech.sdeschmacsha256->shash, + rc =crypto_shash_update (&ses->server->secmech.sdeschmacsha256->shash,"SMB2AESCMAC" ,12 );if (rc) {cifs_dbg (VFS ,"%s: Could not update with label\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_update (&server->secmech.sdeschmacsha256->shash, + rc =crypto_shash_update (&ses->server->secmech.sdeschmacsha256->shash, &zero,1 );if (rc) {cifs_dbg (VFS ,"%s: Could not update with zero\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_update (&server->secmech.sdeschmacsha256->shash, + rc =crypto_shash_update (&ses->server->secmech.sdeschmacsha256->shash,"SmbSign" ,8 );if (rc) {cifs_dbg (VFS ,"%s: Could not update with context\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_update (&server->secmech.sdeschmacsha256->shash, + rc =crypto_shash_update (&ses->server->secmech.sdeschmacsha256->shash,L ,4 );if (rc) {cifs_dbg (VFS ,"%s: Could not update with L\n" , __func__); goto smb3signkey_ret; } - rc =crypto_shash_final (&server->secmech.sdeschmacsha256->shash, + rc =crypto_shash_final (&ses->server->secmech.sdeschmacsha256->shash, hashptr);if (rc) {cifs_dbg (VFS ,"%s: Could not generate sha256 hash\n" , __func__); goto smb3signkey_ret; } -memcpy (server->smb3signingkey, hashptr,SMB3_SIGNKEY_SIZE ); +memcpy (ses->smb3signingkey, hashptr,SMB3_SIGNKEY_SIZE ); smb3signkey_ret: -return ; +return rc; }int smb3_calc_signature (struct smb_rqst *rqst,struct TCP_Server_Info *server) { -int i, rc; +int i; +int rc =0 ;unsigned char smb3_signature[SMB2_CMACAES_SIZE ];unsigned char *sigptr = smb3_signature;struct kvec *iov = rqst->rq_iov;int n_vec = rqst->rq_nvec;struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0 ].iov_base; +struct cifs_ses *ses; + + ses =smb2_find_smb_ses (smb2_pdu, server); +if (!ses) { +cifs_dbg (VFS ,"%s: Could not find session\n" , __func__); +return 0 ; + }memset (smb3_signature,0x0 ,SMB2_CMACAES_SIZE );memset (smb2_pdu->Signature,0x0 ,SMB2_SIGNATURE_SIZE ); rc =crypto_shash_setkey (server->secmech.cmacaes, - server->smb3signingkey,SMB2_CMACAES_SIZE ); + ses->smb3signingkey,SMB2_CMACAES_SIZE ); +if (rc) {cifs_dbg (VFS ,"%s: Could not set key for cmac aes\n" , __func__);return rc;
fs/cifs/smb2transport.c smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) · line 428
Quoted 7 lines
fs/cifs/smb2transport.c +1
-0@@ -389,6 +422,7 @@ smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0 ].iov_base;if ((smb2_pdu->Command ==SMB2_NEGOTIATE ) || + (smb2_pdu->Command ==SMB2_SESSION_SETUP ) || (smb2_pdu->Command ==SMB2_OPLOCK_BREAK ) || (!server->session_estab))return 0 ;
Nice work. I think this looks clean and reasonable.