Skip to contentSkip to conversation

NTLM auth and sign - Use appropriate server challenge

PATCH In review [email protected] 2 participants 7 messages Oct 27, 2010 – Oct 28, 2010 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 NTLM auth and sign - Use appropriate server challenge

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---
 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)
fs/cifs/cifsencrypt.c +7 -3
index 17d603a..ef95a27 100644
@@ -249,7 +249,7 @@ int setup_ntlm_response(struct cifsSesInfo *ses)
 	}
 	ses->auth_key.len = temp_len;
 
-	SMBNTencrypt(ses->password, ses->cryptKey,
+	SMBNTencrypt(ses->password, ses->server->cryptkey,
 			ses->auth_key.response + CIFS_SESS_KEY_SIZE);
 
 	E_md4hash(ses->password, temp_key);
@@ -537,8 +537,12 @@ CalcNTLMv2_response(const struct cifsSesInfo *ses)
 		return rc;
 	}
 
-	memcpy(ses->auth_key.response + offset,
-		ses->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
+	if (ses->server->secType == RawNTLMSSP)
+		memcpy(ses->auth_key.response + offset,
+			ses->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
+	else
+		memcpy(ses->auth_key.response + offset,
+			ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
 	crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
 		ses->auth_key.response + offset, ses->auth_key.len - offset);
 
fs/cifs/cifsglob.h +2 -1
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {
 	int capabilities; /* allow selective disabling of caps by smb sess */
 	int timeAdj;  /* Adjust for difference in server time zone in sec */
 	__u16 CurrentMid;         /* multiplex id - rotating counter */
+	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */
 	/* 16th byte of RFC1001 workstation name is always null */
 	char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
 	__u32 sequence_number; /* needed for CIFS PDU signature */
@@ -240,7 +241,7 @@ struct cifsSesInfo {
 	char userName[MAX_USERNAME_SIZE + 1];
 	char *domainName;
 	char *password;
-	char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */
 	struct session_key auth_key;
 	char ntlmv2_hash[16];
 	unsigned int tilen; /* length of the target info blob */
fs/cifs/cifssmb.c +2 -2
index e98f1f3..2f2632b 100644
@@ -503,7 +503,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 
 		if (rsp->EncryptionKeyLength ==
 				cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
-			memcpy(ses->cryptKey, rsp->EncryptionKey,
+			memcpy(ses->server->cryptkey, rsp->EncryptionKey,
 				CIFS_CRYPTO_KEY_SIZE);
 		} else if (server->secMode & SECMODE_PW_ENCRYPT) {
 			rc = -EIO; /* need cryptkey unless plain text */
@@ -574,7 +574,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 	server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
 	server->timeAdj *= 60;
 	if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
-		memcpy(ses->cryptKey, pSMBr->u.EncryptionKey,
+		memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
 		       CIFS_CRYPTO_KEY_SIZE);
 	} else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC)
 			&& (pSMBr->EncryptionKeyLength == 0)) {
fs/cifs/connect.c +2 -2
index 469c3dd..4d8004c 100644
@@ -3002,13 +3002,13 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
 #ifdef CONFIG_CIFS_WEAK_PW_HASH
 		if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
 		    (ses->server->secType == LANMAN))
-			calc_lanman_hash(tcon->password, ses->cryptKey,
+			calc_lanman_hash(tcon->password, ses->server->cryptkey,
 					 ses->server->secMode &
 					    SECMODE_PW_ENCRYPT ? true : false,
 					 bcc_ptr);
 		else
 #endif /* CIFS_WEAK_PW_HASH */
-		SMBNTencrypt(tcon->password, ses->cryptKey, bcc_ptr);
+		SMBNTencrypt(tcon->password, ses->server->cryptkey, bcc_ptr);
 
 		bcc_ptr += CIFS_SESS_KEY_SIZE;
 		if (ses->capabilities & CAP_UNICODE) {
fs/cifs/sess.c +8 -4
index e0515a6..f74c5a8 100644
@@ -399,7 +399,7 @@ static int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
 		return -EINVAL;
 	}
 
-	memcpy(ses->cryptKey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
+	memcpy(ses->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
 	/* BB we could decode pblob->NegotiateFlags; some may be useful */
 	/* In particular we can examine sign flags */
 	/* BB spec says that if AvId field of MsvAvTimestamp is populated then
@@ -667,10 +667,14 @@ ssetup_ntlmssp_authenticate:
 		/* no capabilities flags in old lanman negotiation */
 
 		pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
-		/* BB calculate hash with password */
-		/* and copy into bcc */
 
-		calc_lanman_hash(ses->password, ses->cryptKey,
+		/* Calculate hash with password and copy into bcc_ptr.
+		 * Encryption Key (stored as in cryptkey) gets used if the
+		 * security mode bit in Negottiate Protocol response states
+		 * to use challenge/response method (i.e. Password bit is 1).
+		 */
+
+		calc_lanman_hash(ses->password, ses->server->cryptkey,
 				 ses->server->secMode & SECMODE_PW_ENCRYPT ?
 					true : false, lnm_session_key);
 
Signature · 1 line
1.6.0.2

A message that is not in the archive (sent off-list or not yet received): 1288210836-12380-1-git-send-email-shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw
Quoted 26 lines · On Wed, 27 Oct 2010 15:20:36 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---
 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)

[...]

Quoted 14 lines
fs/cifs/cifsglob.h +1 -0
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {
 	int capabilities; /* allow selective disabling of caps by smb sess */
 	int timeAdj;  /* Adjust for difference in server time zone in sec */
 	__u16 CurrentMid;         /* multiplex id - rotating counter */
+	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */
 	/* 16th byte of RFC1001 workstation name is always null */
 	char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
 	__u32 sequence_number; /* needed for CIFS PDU signature */

fs/cifs/cifsglob.h struct cifsSesInfo { · line 244

Quoted 5 lines
fs/cifs/cifsglob.h +1 -1
@@ -240,7 +241,7 @@ struct cifsSesInfo {
 	char userName[MAX_USERNAME_SIZE + 1];
 	char *domainName;
 	char *password;
-	char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */

I'm trying to understand why we need ses->cryptkey at all. It holds the cryptkey provided by the server only if we're using ntlmssp. It's only used once, and essentially it just holds the key until we have a auth_key.response field that we can stuff this in. Is that correct? If so, wouldn't it make more sense to just allocate the auth_key.response field earlier and put this directly in there?

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20101027165619.512b50e8-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org

#4 lore raw
Quoted 59 lines · Jeff Layton
Quoted 26 lines · On Wed, 27 Oct 2010 15:20:36 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---

 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)

[...]

Quoted 14 lines
fs/cifs/cifsglob.h +0 -0
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {

      int capabilities; /* allow selective disabling of caps by smb sess */ int timeAdj;  /* Adjust for difference in server time zone in sec */ __u16 CurrentMid;         /* multiplex id - rotating counter */
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */ /* 16th byte of RFC1001 workstation name is always null */ char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; __u32 sequence_number; /* needed for CIFS PDU signature */

fs/cifs/cifsglob.h struct cifsSesInfo { · line 244

Quoted 5 lines
fs/cifs/cifsglob.h +1 -1
@@ -240,7 +241,7 @@ struct cifsSesInfo {
       char userName[MAX_USERNAME_SIZE + 1];
       char *domainName;
       char *password;
-     char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */

I'm trying to understand why we need ses->cryptkey at all. It holds the cryptkey provided by the server only if we're using ntlmssp. It's only used once, and essentially it just holds the key until we have a auth_key.response field that we can stuff this in. Is that correct? If so, wouldn't it make more sense to just allocate the auth_key.response field earlier and put this directly in there?

Quoted 3 lines

--
Jeff Layton <[email protected]>

Jeff, we could do that. We then have to allocate .response in function decode_ntlmssp_challenge(). We have to worry about freeing it if there are any errors between decode_ntlmssp_challenge() and setup_ntlmv2_response(). In function setup_ntlmv2_rsp(), we have to allocate .response only if called in case of ntlmv2 without extended security, so an extra if statement in there.

I think current code is simpler for the price of 16 bytes of cryptkey storage?

A message that is not in the archive (sent off-list or not yet received): AANLkTikx7cPwtbHP=pe4nYqS2EGz-CMa2wNPjZoqgLFg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#6 lore raw
Quoted 64 lines · Shirish Pargaonkar
Quoted 59 lines · Jeff Layton
Quoted 26 lines · On Wed, 27 Oct 2010 15:20:36 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---

 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)

[...]

Quoted 14 lines
fs/cifs/cifsglob.h +0 -0
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {

      int capabilities; /* allow selective disabling of caps by smb sess */ int timeAdj;  /* Adjust for difference in server time zone in sec */ __u16 CurrentMid;         /* multiplex id - rotating counter */
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */ /* 16th byte of RFC1001 workstation name is always null */ char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; __u32 sequence_number; /* needed for CIFS PDU signature */

fs/cifs/cifsglob.h struct cifsSesInfo { · line 244

Quoted 5 lines
fs/cifs/cifsglob.h +1 -1
@@ -240,7 +241,7 @@ struct cifsSesInfo {
       char userName[MAX_USERNAME_SIZE + 1];
       char *domainName;
       char *password;
-     char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */

I'm trying to understand why we need ses->cryptkey at all. It holds the cryptkey provided by the server only if we're using ntlmssp. It's only used once, and essentially it just holds the key until we have a auth_key.response field that we can stuff this in. Is that correct? If so, wouldn't it make more sense to just allocate the auth_key.response field earlier and put this directly in there?

Quoted 3 lines

--
Jeff Layton <[email protected]>

Quoted 8 lines

Jeff, we could do that.  We then have to allocate .response in function decode_ntlmssp_challenge().  We have to worry about freeing it if there are any errors between decode_ntlmssp_challenge() and setup_ntlmv2_response(). In function setup_ntlmv2_rsp(), we have to allocate .response only if called in case of ntlmv2 without extended security, so an extra if statement in there.

I think current code is simpler for the price of 16 bytes of cryptkey storage?

Not 16, 8 bytes. CIFS_CRYPTO_KEY_SIZE is 8 bytes, I thought it was 16 bytes.

A message that is not in the archive (sent off-list or not yet received): AANLkTikseELkqD2_ztH0KF5qQL_95W+T+xq+Pe5Sbj-3-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#8 lore raw
Quoted 72 lines · On Wed, 27 Oct 2010 16:20:48 -0500 Shirish Pargaonkar…
Quoted 70 lines · Shirish Pargaonkar
Quoted 59 lines · Jeff Layton
Quoted 26 lines · On Wed, 27 Oct 2010 15:20:36 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---

 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)

[...]

Quoted 14 lines
fs/cifs/cifsglob.h +0 -0
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {

      int capabilities; /* allow selective disabling of caps by smb sess */ int timeAdj;  /* Adjust for difference in server time zone in sec */ __u16 CurrentMid;         /* multiplex id - rotating counter */
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */ /* 16th byte of RFC1001 workstation name is always null */ char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; __u32 sequence_number; /* needed for CIFS PDU signature */

fs/cifs/cifsglob.h struct cifsSesInfo { · line 244

Quoted 5 lines
fs/cifs/cifsglob.h +1 -1
@@ -240,7 +241,7 @@ struct cifsSesInfo {
       char userName[MAX_USERNAME_SIZE + 1];
       char *domainName;
       char *password;
-     char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */

I'm trying to understand why we need ses->cryptkey at all. It holds the cryptkey provided by the server only if we're using ntlmssp. It's only used once, and essentially it just holds the key until we have a auth_key.response field that we can stuff this in. Is that correct? If so, wouldn't it make more sense to just allocate the auth_key.response field earlier and put this directly in there?

Quoted 3 lines

--
Jeff Layton <[email protected]>

Jeff, we could do that.  We then have to allocate .response in function decode_ntlmssp_challenge().  We have to worry about freeing it if there are any errors between decode_ntlmssp_challenge() and setup_ntlmv2_response(). In function setup_ntlmv2_rsp(), we have to allocate .response only if called in case of ntlmv2 without extended security, so an extra if statement in there.

Quoted 4 lines
Quoted 2 lines

I think current code is simpler for the price of 16 bytes of cryptkey storage?

Not 16, 8 bytes. CIFS_CRYPTO_KEY_SIZE is 8 bytes, I thought it was 16 bytes.

I don't think that's simpler. I'm becoming concerned about the proliferation of fields in cifsSesInfo. All of these are recent additions by your patches to fix NTLMSSP support.

char cryptKey[CIFS_CRYPTO_KEY_SIZE];
struct session_key auth_key;
char ntlmv2_hash[16];
unsigned int tilen; /* length of the target info blob */ unsigned char *tiblob; /* target info blob in challenge response */ struct ntlmssp_auth ntlmssp; /* ciphertext, flags */

...it's not so much the space that they take up, even though that's not exactly trivial. A lot of these fields have no use at all after authentication is done. There's a lot of memcpy'ing going on here too which is a red flag.

It's not clear to me why you need so many fields for this. It would seem to me that it would be a good idea to think about each of these fields and determine which ones need to stick around after authentication is done (i.e. for signing), and store the rest of them in a structure that can be freed after authentication is done.

My main concern here is debugging this later. One of the main techniques I use when tracking down bugs is to follow the lifecycle of the data. Whenever you add a new field, it makes it a lot more difficult for myself and others to follow the code.

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20101027193854.78c1b500-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org

#10 lore raw
Quoted 102 lines · Jeff Layton
Quoted 72 lines · On Wed, 27 Oct 2010 16:20:48 -0500 Shirish Pargaonkar…
Quoted 70 lines · Shirish Pargaonkar
Quoted 59 lines · Jeff Layton
Quoted 26 lines · On Wed, 27 Oct 2010 15:20:36 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---

 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)

[...]

Quoted 14 lines
fs/cifs/cifsglob.h +0 -0
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {

      int capabilities; /* allow selective disabling of caps by smb sess */ int timeAdj;  /* Adjust for difference in server time zone in sec */ __u16 CurrentMid;         /* multiplex id - rotating counter */
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */ /* 16th byte of RFC1001 workstation name is always null */ char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; __u32 sequence_number; /* needed for CIFS PDU signature */

fs/cifs/cifsglob.h struct cifsSesInfo { · line 244

Quoted 5 lines
fs/cifs/cifsglob.h +1 -1
@@ -240,7 +241,7 @@ struct cifsSesInfo {
       char userName[MAX_USERNAME_SIZE + 1];
       char *domainName;
       char *password;
-     char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+     char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */

I'm trying to understand why we need ses->cryptkey at all. It holds the cryptkey provided by the server only if we're using ntlmssp. It's only used once, and essentially it just holds the key until we have a auth_key.response field that we can stuff this in. Is that correct? If so, wouldn't it make more sense to just allocate the auth_key.response field earlier and put this directly in there?

Quoted 3 lines

--
Jeff Layton <[email protected]>

Jeff, we could do that.  We then have to allocate .response in function decode_ntlmssp_challenge().  We have to worry about freeing it if there are any errors between decode_ntlmssp_challenge() and setup_ntlmv2_response(). In function setup_ntlmv2_rsp(), we have to allocate .response only if called in case of ntlmv2 without extended security, so an extra if statement in there.

Quoted 4 lines
Quoted 2 lines

I think current code is simpler for the price of 16 bytes of cryptkey storage?

Not 16, 8 bytes.   CIFS_CRYPTO_KEY_SIZE is 8 bytes, I thought it was 16 bytes.

I don't think that's simpler. I'm becoming concerned about the proliferation of fields in cifsSesInfo. All of these are recent additions by your patches to fix NTLMSSP support.

       char cryptKey[CIFS_CRYPTO_KEY_SIZE];
       struct session_key auth_key;
       char ntlmv2_hash[16];
       unsigned int tilen; /* length of the target info blob */ unsigned char *tiblob; /* target info blob in challenge response */ struct ntlmssp_auth ntlmssp; /* ciphertext, flags */

...it's not so much the space that they take up, even though that's not exactly trivial. A lot of these fields have no use at all after authentication is done. There's a lot of memcpy'ing going on here too which is a red flag.

It's not clear to me why you need so many fields for this. It would seem to me that it would be a good idea to think about each of these fields and determine which ones need to stick around after authentication is done (i.e. for signing), and store the rest of them in a structure that can be freed after authentication is done.

Quoted 8 lines

My main concern here is debugging this later. One of the main techniques I use when tracking down bugs is to follow the lifecycle of the data. Whenever you add a new field, it makes it a lot more difficult for myself and others to follow the code.

--
Jeff Layton <[email protected]>

Posted a follow up patch that addresses the concerns. If that patch is agreeable in
some or no additional variations, it should follow this patch if this patch is good enough
because the functionality in this patch is needed for multiple smb sessions per smb connection.

Acked-by
#11 lore raw
In reply to a message not in the archive
Quoted 138 lines · On Wed, 27 Oct 2010 15:20:36 -0500 shirishpargaonkar-Re5JQEeQqe8AvxtiuMwx3w@pub…

From: Shirish Pargaonkar <[email protected]>

Need to have cryptkey or server challenge in smb connection (struct TCP_Server_Info) for ntlm and ntlmv2 auth types for which cryptkey (Encryption Key) is supplied just once in Negotiate Protocol response during an smb connection setup for all the smb sessions over that smb connection.

For ntlmssp, cryptkey or server challenge is provided for every smb session in type 2 packet of ntlmssp negotiation, the cryptkey provided during Negotiation Protocol response before smb connection does not count.

Rename cryptKey to cryptkey and related changes.

---
 fs/cifs/cifsencrypt.c |   10 +++++++---
 fs/cifs/cifsglob.h    |    3 ++-
 fs/cifs/cifssmb.c     |    4 ++--
 fs/cifs/connect.c     |    4 ++--
 fs/cifs/sess.c        |   12 ++++++++----
 5 files changed, 21 insertions(+), 12 deletions(-)
fs/cifs/cifsencrypt.c +7 -3
index 17d603a..ef95a27 100644
@@ -249,7 +249,7 @@ int setup_ntlm_response(struct cifsSesInfo *ses)
 	}
 	ses->auth_key.len = temp_len;
 
-	SMBNTencrypt(ses->password, ses->cryptKey,
+	SMBNTencrypt(ses->password, ses->server->cryptkey,
 			ses->auth_key.response + CIFS_SESS_KEY_SIZE);
 
 	E_md4hash(ses->password, temp_key);
@@ -537,8 +537,12 @@ CalcNTLMv2_response(const struct cifsSesInfo *ses)
 		return rc;
 	}
 
-	memcpy(ses->auth_key.response + offset,
-		ses->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
+	if (ses->server->secType == RawNTLMSSP)
+		memcpy(ses->auth_key.response + offset,
+			ses->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
+	else
+		memcpy(ses->auth_key.response + offset,
+			ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
 	crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
 		ses->auth_key.response + offset, ses->auth_key.len - offset);
 
fs/cifs/cifsglob.h +2 -1
index 67d6a22..b736951 100644
@@ -196,6 +196,7 @@ struct TCP_Server_Info {
 	int capabilities; /* allow selective disabling of caps by smb sess */
 	int timeAdj;  /* Adjust for difference in server time zone in sec */
 	__u16 CurrentMid;         /* multiplex id - rotating counter */
+	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlm, ntlmv2 etc */
 	/* 16th byte of RFC1001 workstation name is always null */
 	char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
 	__u32 sequence_number; /* needed for CIFS PDU signature */
@@ -240,7 +241,7 @@ struct cifsSesInfo {
 	char userName[MAX_USERNAME_SIZE + 1];
 	char *domainName;
 	char *password;
-	char cryptKey[CIFS_CRYPTO_KEY_SIZE];
+	char cryptkey[CIFS_CRYPTO_KEY_SIZE]; /* used by ntlmssp */
 	struct session_key auth_key;
 	char ntlmv2_hash[16];
 	unsigned int tilen; /* length of the target info blob */
fs/cifs/cifssmb.c +2 -2
index e98f1f3..2f2632b 100644
@@ -503,7 +503,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 
 		if (rsp->EncryptionKeyLength ==
 				cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
-			memcpy(ses->cryptKey, rsp->EncryptionKey,
+			memcpy(ses->server->cryptkey, rsp->EncryptionKey,
 				CIFS_CRYPTO_KEY_SIZE);
 		} else if (server->secMode & SECMODE_PW_ENCRYPT) {
 			rc = -EIO; /* need cryptkey unless plain text */
@@ -574,7 +574,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 	server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
 	server->timeAdj *= 60;
 	if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
-		memcpy(ses->cryptKey, pSMBr->u.EncryptionKey,
+		memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
 		       CIFS_CRYPTO_KEY_SIZE);
 	} else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC)
 			&& (pSMBr->EncryptionKeyLength == 0)) {
fs/cifs/connect.c +2 -2
index 469c3dd..4d8004c 100644
@@ -3002,13 +3002,13 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
 #ifdef CONFIG_CIFS_WEAK_PW_HASH
 		if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
 		    (ses->server->secType == LANMAN))
-			calc_lanman_hash(tcon->password, ses->cryptKey,
+			calc_lanman_hash(tcon->password, ses->server->cryptkey,
 					 ses->server->secMode &
 					    SECMODE_PW_ENCRYPT ? true : false,
 					 bcc_ptr);
 		else
 #endif /* CIFS_WEAK_PW_HASH */
-		SMBNTencrypt(tcon->password, ses->cryptKey, bcc_ptr);
+		SMBNTencrypt(tcon->password, ses->server->cryptkey, bcc_ptr);
 
 		bcc_ptr += CIFS_SESS_KEY_SIZE;
 		if (ses->capabilities & CAP_UNICODE) {
fs/cifs/sess.c +1 -4
index e0515a6..f74c5a8 100644
@@ -399,7 +399,7 @@ static int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
 		return -EINVAL;
 	}
 
-	memcpy(ses->cryptKey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
+	memcpy(ses->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
 	/* BB we could decode pblob->NegotiateFlags; some may be useful */
 	/* In particular we can examine sign flags */
 	/* BB spec says that if AvId field of MsvAvTimestamp is populated then
@@ -667,10 +667,14 @@ ssetup_ntlmssp_authenticate:
 		/* no capabilities flags in old lanman negotiation */
 
 		pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
-		/* BB calculate hash with password */
-		/* and copy into bcc */
 
-		calc_lanman_hash(ses->password, ses->cryptKey,

fs/cifs/sess.c ssetup_ntlmssp_authenticate: · line 680

Quoted 10 lines
fs/cifs/sess.c +7 -0
+		/* Calculate hash with password and copy into bcc_ptr.
+		 * Encryption Key (stored as in cryptkey) gets used if the
+		 * security mode bit in Negottiate Protocol response states
+		 * to use challenge/response method (i.e. Password bit is 1).
+		 */
+
+		calc_lanman_hash(ses->password, ses->server->cryptkey,
 				 ses->server->secMode & SECMODE_PW_ENCRYPT ?
 					true : false, lnm_session_key);
 

Ack on this patch, assuming that it's a prerequisite for the later cleanup patch.