Skip to contentSkip to conversation

fix async decryption + some secmech cleanups

PATCH In review Enzo Matsumiya 4 participants 11 messages Sep 26, 2024 – Jul 5, 2025

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

cover fix async decryption + some secmech cleanups

Hi,

Patch 1/4:
Fix a use-after-free in the crypto API when using the same TFM in parallel to decrypt SMB2 messages. 'parallel' meaning only when 'min_enc_offload/esize' is > 0, so smb2_decrypt_offload() is called.

Patch 2/4:
We store crypto keys in cifs_ses and we find them on every crypto operation by the Session ID. Using a crypto TFM with the crypto keys set right after session setup would be ideal, but would require a non-trivial redesign of the module. So, instead, allocate a TFM on the primary server only, and keep finding/setting the keys on a per-usage basis.

Patches 3 and 4/4:
HMAC-MD5 and SHA-512 TFMs are only used on Session Setup and each contained in a single function. The allocated memory size doesn't hurt, but makes no sense to keep them around when they're not going to be used again so soon (or at all).

Reviews appreciated.

Cheers,

Enzo Matsumiya (4):
smb: client: fix UAF in async decryption
smb: client: allocate crypto only for primary server
smb: client: make HMAC-MD5 TFM ephemeral
smb: client: make SHA-512 TFM ephemeral

 fs/smb/client/cifsencrypt.c   | 151 ++++++++++++++--------------------
 fs/smb/client/cifsglob.h      |   2 -
 fs/smb/client/sess.c          |   2 +-
 fs/smb/client/smb2misc.c      |  28 +++----
 fs/smb/client/smb2ops.c       |  47 ++++++-----
 fs/smb/client/smb2pdu.c       |  10 +++
 fs/smb/client/smb2proto.h     |   2 +-
 fs/smb/client/smb2transport.c |  30 +------
 8 files changed, 116 insertions(+), 156 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/4 smb: client: fix UAF in async decryption

Doing an async decryption (large read) crashes with a slab-use-after-free way down in the crypto API.

Reproducer:

    # mount.cifs -o ...,seal,esize=1 //srv/share /mnt
    # dd if=/mnt/largefile of=/dev/null
    ...
    [  194.196391] ==================================================================
    [  194.196844] BUG: KASAN: slab-use-after-free in gf128mul_4k_lle+0xc1/0x110
    [  194.197269] Read of size 8 at addr ffff888112bd0448 by task kworker/u77:2/899
    [  194.197707]
    [  194.197818] CPU: 12 UID: 0 PID: 899 Comm: kworker/u77:2 Not tainted 6.11.0-lku-00028-gfca3ca14a17a-dirty #43
    [  194.198400] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.2-3-gd478f380-prebuilt.qemu.org 04/01/2014
    [  194.199046] Workqueue: smb3decryptd smb2_decrypt_offload [cifs]
    [  194.200032] Call Trace:
    [  194.200191]  <TASK>
    [  194.200327]  dump_stack_lvl+0x4e/0x70
    [  194.200558]  ? gf128mul_4k_lle+0xc1/0x110
    [  194.200809]  print_report+0x174/0x505
    [  194.201040]  ? __pfx__raw_spin_lock_irqsave+0x10/0x10
    [  194.201352]  ? srso_return_thunk+0x5/0x5f
    [  194.201604]  ? __virt_addr_valid+0xdf/0x1c0
    [  194.201868]  ? gf128mul_4k_lle+0xc1/0x110
    [  194.202128]  kasan_report+0xc8/0x150
    [  194.202361]  ? gf128mul_4k_lle+0xc1/0x110
    [  194.202616]  gf128mul_4k_lle+0xc1/0x110
    [  194.202863]  ghash_update+0x184/0x210
    [  194.203103]  shash_ahash_update+0x184/0x2a0
    [  194.203377]  ? __pfx_shash_ahash_update+0x10/0x10
    [  194.203651]  ? srso_return_thunk+0x5/0x5f
    [  194.203877]  ? crypto_gcm_init_common+0x1ba/0x340
    [  194.204142]  gcm_hash_assoc_remain_continue+0x10a/0x140
    [  194.204434]  crypt_message+0xec1/0x10a0 [cifs]
    [  194.206489]  ? __pfx_crypt_message+0x10/0x10 [cifs]
    [  194.208507]  ? srso_return_thunk+0x5/0x5f
    [  194.209205]  ? srso_return_thunk+0x5/0x5f
    [  194.209925]  ? srso_return_thunk+0x5/0x5f
    [  194.210443]  ? srso_return_thunk+0x5/0x5f
    [  194.211037]  decrypt_raw_data+0x15f/0x250 [cifs]
    [  194.212906]  ? __pfx_decrypt_raw_data+0x10/0x10 [cifs]
    [  194.214670]  ? srso_return_thunk+0x5/0x5f
    [  194.215193]  smb2_decrypt_offload+0x12a/0x6c0 [cifs]

This is because TFM is being used in parallel.

Fix this by allocating a new AEAD TFM for async decryption, but keep the existing one for synchronous READ cases (similar to what is done in smb3_calc_signature()).

Also remove the calls to aead_request_set_callback() and crypto_wait_req() since it's always going to be a synchronous operation.

---
 fs/smb/client/smb2ops.c | 47 ++++++++++++++++++++++++-----------------
 fs/smb/client/smb2pdu.c |  6 ++++++
 2 files changed, 34 insertions(+), 19 deletions(-)
fs/smb/client/smb2ops.c +28 -19
index 1ee2dd4a1cae..177173072bfa 100644
@@ -4309,7 +4309,7 @@ smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
  */
 static int
 crypt_message(struct TCP_Server_Info *server, int num_rqst,
-	      struct smb_rqst *rqst, int enc)
+	      struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
 {
 	struct smb2_transform_hdr *tr_hdr =
 		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
@@ -4320,8 +4320,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	u8 key[SMB3_ENC_DEC_KEY_SIZE];
 	struct aead_request *req;
 	u8 *iv;
-	DECLARE_CRYPTO_WAIT(wait);
-	struct crypto_aead *tfm;
 	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
 	void *creq;
 	size_t sensitive_size;
@@ -4333,14 +4331,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 		return rc;
 	}
 
-	rc = smb3_crypto_aead_allocate(server);
-	if (rc) {
-		cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
-		return rc;
-	}
-
-	tfm = enc ? server->secmech.enc : server->secmech.dec;
-
 	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
 		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
 		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
@@ -4380,11 +4370,7 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
 	aead_request_set_ad(req, assoc_data_len);
 
-	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
-				  crypto_req_done, &wait);
-
-	rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
-				: crypto_aead_decrypt(req), &wait);
+	rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
 
 	if (!rc && enc)
 		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
@@ -4526,7 +4512,7 @@ smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
 	/* fill the 1st iov with a transform header */
 	fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
 
-	rc = crypt_message(server, num_rqst, new_rq, 1);
+	rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
 	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
 	if (rc)
 		goto err_free;
@@ -4551,8 +4537,9 @@ decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
 		 unsigned int buf_data_size, struct iov_iter *iter,
 		 bool is_offloaded)
 {
-	struct kvec iov[2];
+	struct crypto_aead *tfm;
 	struct smb_rqst rqst = {NULL};
+	struct kvec iov[2];
 	size_t iter_size = 0;
 	int rc;
 
@@ -4568,9 +4555,31 @@ decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
 		iter_size = iov_iter_count(iter);
 	}
 
-	rc = crypt_message(server, 1, &rqst, 0);
+	if (is_offloaded) {
+		if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
+		    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+			tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
+		else
+			tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
+		if (IS_ERR(tfm)) {
+			rc = PTR_ERR(tfm);
+			cifs_server_dbg(VFS, "%s: Failed alloc decrypt TFM, rc=%d\n", __func__, rc);
+
+			return rc;
+		}
+	} else {
+		if (unlikely(!server->secmech.dec))
+			return -EIO;
+
+		tfm = server->secmech.dec;
+	}
+
+	rc = crypt_message(server, 1, &rqst, 0, tfm);
 	cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
 
+	if (is_offloaded)
+		crypto_free_aead(tfm);
+
 	if (rc)
 		return rc;
 
fs/smb/client/smb2pdu.c +6 -0
index a328b9dd145d..a07327b704b5 100644
@@ -1266,6 +1266,12 @@ SMB2_negotiate(const unsigned int xid,
 		else
 			cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
 	}
+
+	if (server->cipher_type && !rc) {
+		rc = smb3_crypto_aead_allocate(server);
+		if (rc)
+			cifs_server_dbg(VFS, "%s: crypto alloc failed, rc=%d\n", __func__, rc);
+	}
 neg_exit:
 	free_rsp_buf(resp_buftype, rsp);
 	return rc;
Signature · 1 line
2.46.0
#2 lore raw

Re: [PATCH 1/4] smb: client: fix UAF in async decryption

Hi Enzo,

I've been testing the CVE-2024-50047 fix (this patch) and encountered a NULL pointer dereference when using hardware crypto acceleration. I'd like to discuss a potential issue with the patch and propose a solution.

When mounting CIFS with encryption enabled and using hardware crypto modules, I triggered a NULL pointer dereference in the hardware encryption driver.

The patch removed the asynchronous callback mechanism from crypt_message():

Quoted 39 lines

index 1ee2dd4a1cae..177173072bfa 100644

fs/smb/client/smb2ops.c +1 -11
@@ -4309,7 +4309,7 @@ smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
   */
  static int
  crypt_message(struct TCP_Server_Info *server, int num_rqst,
-	      struct smb_rqst *rqst, int enc)
+	      struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
  {
  	struct smb2_transform_hdr *tr_hdr =
  		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
@@ -4320,8 +4320,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
  	u8 key[SMB3_ENC_DEC_KEY_SIZE];
  	struct aead_request *req;
  	u8 *iv;
-	DECLARE_CRYPTO_WAIT(wait);
-	struct crypto_aead *tfm;
  	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
  	void *creq;
  	size_t sensitive_size;
@@ -4333,14 +4331,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
  		return rc;
  	}
 
-	rc = smb3_crypto_aead_allocate(server);
-	if (rc) {
-		cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
-		return rc;
-	}
-
-	tfm = enc ? server->secmech.enc : server->secmech.dec;
-
  	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
  		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
  		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
@@ -4380,11 +4370,7 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
  	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
  	aead_request_set_ad(req, assoc_data_len);

fs/smb/client/smb2ops.c crypt_message(struct TCP_Server_Info *server, int num_rqst, · line 4376

Quoted 10 lines
fs/smb/client/smb2ops.c +1 -5
 
-	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
-				  crypto_req_done, &wait);
-
-	rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
-				: crypto_aead_decrypt(req), &wait);
+	rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
 
  	if (!rc && enc)
  		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);

The patch removed the asynchronous callback mechanism from crypt_message(), This will cause the driver to immediately return -115(-EINPROGRESS) when calling crypto_aead_encrypt(). And when the function exits, it will call kvfree_sensitive(creq, sensitive_size) to free the creq buffer, which still contains the req request. This leads to a crash when the driver processes the req later.

The commit message states "it's always going to be a synchronous operation", but this assumption doesn't hold for hardware crypto implementations. According to the kernel's AEAD API documentation[1], crypto_aead_encrypt/decrypt operations can be asynchronous:

These functions may return -EINPROGRESS or -EBUSY for async operations The API provides aead_request_set_callback() specifically for async handling Hardware accelerators commonly implement async operations for performance Without crypto_wait_req(), the code immediately frees the request buffer with kvfree_sensitive(), leading to use-after-free when the async crypto worker accesses it.

Questions:

- What was the specific rationale for assuming synchronous-only operations?
- Were there concerns about the async mechanism interfering with the CVE fix?

I believe we can restore the async handling without affecting the CVE fix:

Best regards,
Wang Zhaolong

[1] https://docs.kernel.org/crypto/api-aead.html

#3 lore raw

Re: [PATCH 1/4] smb: client: fix UAF in async decryption

Hi Wang,

Quoted 73 lines · Wang Zhaolong

Hi Enzo,

I've been testing the CVE-2024-50047 fix (this patch) and encountered a NULL pointer dereference when using hardware crypto acceleration. I'd like to discuss a potential issue with the patch and propose a solution.

When mounting CIFS with encryption enabled and using hardware crypto modules, I triggered a NULL pointer dereference in the hardware encryption driver.

The patch removed the asynchronous callback mechanism from crypt_message():

Quoted 36 lines

index 1ee2dd4a1cae..177173072bfa 100644

fs/smb/client/smb2ops.c +1 -11
@@ -4309,7 +4309,7 @@ smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
  */
 static int
 crypt_message(struct TCP_Server_Info *server, int num_rqst,
-	      struct smb_rqst *rqst, int enc)
+	      struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
 {
 	struct smb2_transform_hdr *tr_hdr =
 		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
@@ -4320,8 +4320,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	u8 key[SMB3_ENC_DEC_KEY_SIZE];
 	struct aead_request *req;
 	u8 *iv;
-	DECLARE_CRYPTO_WAIT(wait);
-	struct crypto_aead *tfm;
 	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
 	void *creq;
 	size_t sensitive_size;
@@ -4333,14 +4331,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 		return rc;
 	}
-	rc = smb3_crypto_aead_allocate(server);
-	if (rc) {
-		cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
-		return rc;
-	}
-
-	tfm = enc ? server->secmech.enc : server->secmech.dec;
-
 	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
 		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
 		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);

fs/smb/client/smb2ops.c crypt_message(struct TCP_Server_Info *server, int num_rqst, · line 4374

Quoted 10 lines
fs/smb/client/smb2ops.c +1 -5
@@ -4380,11 +4370,7 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
 	aead_request_set_ad(req, assoc_data_len);
-	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
-				  crypto_req_done, &wait);
-
-	rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
-				: crypto_aead_decrypt(req), &wait);
+	rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
 	if (!rc && enc)
 		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);

The patch removed the asynchronous callback mechanism from crypt_message(), This will cause the driver to immediately return -115(-EINPROGRESS) when calling crypto_aead_encrypt(). And when the function exits, it will call kvfree_sensitive(creq, sensitive_size) to free the creq buffer, which still contains the req request. This leads to a crash when the driver processes the req later.

The commit message states "it's always going to be a synchronous operation", but this assumption doesn't hold for hardware crypto implementations. According to the kernel's AEAD API documentation[1], crypto_aead_encrypt/decrypt operations can be asynchronous:

Quoted 6 lines

These functions may return -EINPROGRESS or -EBUSY for async operations The API provides aead_request_set_callback() specifically for async handling Hardware accelerators commonly implement async operations for performance Without crypto_wait_req(), the code immediately frees the request buffer with kvfree_sensitive(), leading to use-after-free when the async crypto worker accesses it.

The way I see this is all on the HW crypto driver implementation not respecting the mask in crypto_alloc_aead() -- IIRC, for API users, it must be either 0 or CRYPTO_ALG_ASYNC.

Even though cifs called crypto_wait_req() with crypto_req_done() callback, we never allocated AEAD TFM with CRYPTO_ALG_ASYNC, so this has been always effectivelly a synchronous op.

To fix this correctly, it'd need to be done on the HW crypto driver itself -- not in crypto API, not in cifs.

If you want to circumvent this particular issue on cifs side, you'd need to implement the whole mechanism to hold the unencrypted request until your callback is called, while taking into consideration reconnects, which can get quite tricky for writes especially (this is not fun, believe me, I tried).

Quoted 3 lines

Questions:

- What was the specific rationale for assuming synchronous-only operations?

We never allocated AEAD TFMs with CRYPTO_ALG_ASYNC (see above), so it was not a mere assumption, it was (is) an expectation.

Quoted 1 line

- Were there concerns about the async mechanism interfering with the CVE fix?

Not really, no -- again, cifs never had async AEAD nor the mechanisms to handle it.

FWIW the CVE came after my fix, until then it was "just" a bug fix.

Quoted 1 line

I believe we can restore the async handling without affecting the CVE fix:

Restoring the crypto_wait_req/callback code should fix the particular crash you get, but the calling thread would still block, completely invalidating any async benefit that you might be otherwise expecting.

Cheers,

Enzo

#4 lore raw

Re: [PATCH 1/4] smb: client: fix UAF in async decryption

Hi Enzo,

Thank you for your detailed response. I appreciate your explanation, but I believe there may be a misunderstanding about how the crypto API's mask parameter works.

Quoted 4 lines

Even though cifs called crypto_wait_req() with crypto_req_done() callback, we never allocated AEAD TFM with CRYPTO_ALG_ASYNC, so this has been always effectivelly a synchronous op.

Quoted 8 lines

To fix this correctly, it'd need to be done on the HW crypto driver itself -- not in crypto API, not in cifs.

If you want to circumvent this particular issue on cifs side, you'd need to implement the whole mechanism to hold the unencrypted request until your callback is called, while taking into consideration reconnects, which can get quite tricky for writes especially (this is not fun, believe me, I tried).

I've examined the kernel crypto API implementation in crypto/api.c, specifically the __crypto_alg_lookup() function:

~~~c
// crypto/api.c
static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,

					      u32 mask)
{
	struct crypto_alg *q, *alg = NULL;
	int best = -2;

	list_for_each_entry(q, &crypto_alg_list, cra_list) {
		int exact, fuzzy;

		if (crypto_is_moribund(q))
			continue;

		if ((q->cra_flags ^ type) & mask)
			continue;

		exact = !strcmp(q->cra_driver_name, name);
		fuzzy = !strcmp(q->cra_name, name);
		if (!exact && !(fuzzy && q->cra_priority > best))
			continue;

		if (unlikely(!crypto_mod_get(q)))
			continue;

		best = q->cra_priority;
		if (alg)
			crypto_mod_put(alg);
		alg = q;

		if (exact)
			break;
	}

	return alg;
}

~~~
If the mask is 0, it does not distinguish between synchronous and asynchronous. It only looks up the encryption module based on the encryption algorithm name.

https://docs.kernel.org/crypto/architecture.html#cipher-allocation-type-and-masks

The kernel crypto architecture documentation states:

Quoted 3 lines

When the caller provides a mask and type specification, the caller limits the search the kernel crypto API can perform for
a suitable cipher implementation

The documentation does not state that if mask=0, the returned cipher is always synchronous. Instead, the documentation implies:

- API supports both synchronous and asynchronous operations
- The mask flag is used to limit the search, not to guarantee a specific type of return

Many hardware crypto drivers:

- Register with high priority for performance
- May implement asynchronous operations for efficiency
- Get selected by the priority-based mechanism even with mask=0

The original code's use of crypto_wait_req() was defensive programming - it handled both sync and async cases correctly.

Quoted 3 lines

Restoring the crypto_wait_req/callback code should fix the particular crash you get, but the calling thread would still block, completely invalidating any async benefit that you might be otherwise expecting.

While I understand your concern about "losing async benefits," the primary goal should be kernel stability. The current patch introduces a use-after-free vulnerability when async implementations are selected.

I suggest we restore the async handling as a short-term fix.This ensures correctness regardless of the underlying implementation, while maintaining the fix (separate TFM allocation for offloaded operations).

Best regards,
Wang Zhaolong

#5 lore raw

PATCH 2/4 smb: client: allocate crypto only for primary server

In reply to Enzo Matsumiya

For extra channels, point ->secmech.{enc,dec} to the primary server ones.

---
 fs/smb/client/cifsencrypt.c | 17 +++++++++++------
 fs/smb/client/smb2pdu.c     | 10 +++++++---
 2 files changed, 18 insertions(+), 9 deletions(-)
fs/smb/client/cifsencrypt.c +11 -6
index 7481b21a0489..15aa75e7f1c3 100644
@@ -735,13 +735,18 @@ cifs_crypto_secmech_release(struct TCP_Server_Info *server)
 	cifs_free_hash(&server->secmech.sha512);
 	cifs_free_hash(&server->secmech.hmacmd5);
 
-	if (server->secmech.enc) {
-		crypto_free_aead(server->secmech.enc);
-		server->secmech.enc = NULL;
-	}
+	if (!SERVER_IS_CHAN(server)) {
+		if (server->secmech.enc) {
+			crypto_free_aead(server->secmech.enc);
+			server->secmech.enc = NULL;
+		}
 
-	if (server->secmech.dec) {
-		crypto_free_aead(server->secmech.dec);
+		if (server->secmech.dec) {
+			crypto_free_aead(server->secmech.dec);
+			server->secmech.dec = NULL;
+		}
+	} else {
+		server->secmech.enc = NULL;
 		server->secmech.dec = NULL;
 	}
 }
fs/smb/client/smb2pdu.c +7 -3
index a07327b704b5..12c7acae2905 100644
@@ -1268,9 +1268,13 @@ SMB2_negotiate(const unsigned int xid,
 	}
 
 	if (server->cipher_type && !rc) {
-		rc = smb3_crypto_aead_allocate(server);
-		if (rc)
-			cifs_server_dbg(VFS, "%s: crypto alloc failed, rc=%d\n", __func__, rc);
+		if (!SERVER_IS_CHAN(server)) {
+			rc = smb3_crypto_aead_allocate(server);
+		} else {
+			/* For channels, just reuse the primary server crypto secmech. */
+			server->secmech.enc = server->primary_server->secmech.enc;
+			server->secmech.dec = server->primary_server->secmech.dec;
+		}
 	}
 neg_exit:
 	free_rsp_buf(resp_buftype, rsp);
Signature · 1 line
2.46.0
#6 lore raw

PATCH 3/4 smb: client: make HMAC-MD5 TFM ephemeral

In reply to Enzo Matsumiya

The HMAC-MD5 shash TFM is used only briefly during Session Setup stage, when computing NTLMv2 hashes.

There's no need to keep it allocated in servers' secmech the whole time, so keep its lifetime inside setup_ntlmv2_rsp().

---
 fs/smb/client/cifsencrypt.c | 133 ++++++++++++++----------------------
 fs/smb/client/cifsglob.h    |   1 -
 2 files changed, 50 insertions(+), 84 deletions(-)
fs/smb/client/cifsencrypt.c +50 -83
index 15aa75e7f1c3..464e6ccdfa5f 100644
@@ -416,7 +416,7 @@ find_timestamp(struct cifs_ses *ses)
 }
 
 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
-			    const struct nls_table *nls_cp)
+			    const struct nls_table *nls_cp, struct shash_desc *hmacmd5)
 {
 	int rc = 0;
 	int len;
@@ -425,34 +425,26 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
 	wchar_t *domain;
 	wchar_t *server;
 
-	if (!ses->server->secmech.hmacmd5) {
-		cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
-		return -1;
-	}
-
 	/* calculate md4 hash of password */
 	E_md4hash(ses->password, nt_hash, nls_cp);
 
-	rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm, nt_hash,
-				CIFS_NTHASH_SIZE);
+	rc = crypto_shash_setkey(hmacmd5->tfm, nt_hash, CIFS_NTHASH_SIZE);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
+		cifs_dbg(VFS, "%s: Could not set NT hash as a key, rc=%d\n", __func__, rc);
 		return rc;
 	}
 
-	rc = crypto_shash_init(ses->server->secmech.hmacmd5);
+	rc = crypto_shash_init(hmacmd5);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
+		cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc);
 		return rc;
 	}
 
 	/* convert ses->user_name to unicode */
 	len = ses->user_name ? strlen(ses->user_name) : 0;
 	user = kmalloc(2 + (len * 2), GFP_KERNEL);
-	if (user == NULL) {
-		rc = -ENOMEM;
-		return rc;
-	}
+	if (user == NULL)
+		return -ENOMEM;
 
 	if (len) {
 		len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
@@ -461,11 +453,10 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
 		*(u16 *)user = 0;
 	}
 
-	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
-				(char *)user, 2 * len);
+	rc = crypto_shash_update(hmacmd5, (char *)user, 2 * len);
 	kfree(user);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
+		cifs_dbg(VFS, "%s: Could not update with user, rc=%d\n", __func__, rc);
 		return rc;
 	}
 
@@ -474,19 +465,15 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
 		len = strlen(ses->domainName);
 
 		domain = kmalloc(2 + (len * 2), GFP_KERNEL);
-		if (domain == NULL) {
-			rc = -ENOMEM;
-			return rc;
-		}
+		if (domain == NULL)
+			return -ENOMEM;
+
 		len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
 				      nls_cp);
-		rc =
-		crypto_shash_update(ses->server->secmech.hmacmd5,
-					(char *)domain, 2 * len);
+		rc = crypto_shash_update(hmacmd5, (char *)domain, 2 * len);
 		kfree(domain);
 		if (rc) {
-			cifs_dbg(VFS, "%s: Could not update with domain\n",
-				 __func__);
+			cifs_dbg(VFS, "%s: Could not update with domain, rc=%d\n", __func__, rc);
 			return rc;
 		}
 	} else {
@@ -494,33 +481,27 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
 		len = strlen(ses->ip_addr);
 
 		server = kmalloc(2 + (len * 2), GFP_KERNEL);
-		if (server == NULL) {
-			rc = -ENOMEM;
-			return rc;
-		}
-		len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len,
-					nls_cp);
-		rc =
-		crypto_shash_update(ses->server->secmech.hmacmd5,
-					(char *)server, 2 * len);
+		if (server == NULL)
+			return -ENOMEM;
+
+		len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, nls_cp);
+		rc = crypto_shash_update(hmacmd5, (char *)server, 2 * len);
 		kfree(server);
 		if (rc) {
-			cifs_dbg(VFS, "%s: Could not update with server\n",
-				 __func__);
+			cifs_dbg(VFS, "%s: Could not update with server, rc=%d\n", __func__, rc);
 			return rc;
 		}
 	}
 
-	rc = crypto_shash_final(ses->server->secmech.hmacmd5,
-					ntlmv2_hash);
+	rc = crypto_shash_final(hmacmd5, ntlmv2_hash);
 	if (rc)
-		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
+		cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc);
 
 	return rc;
 }
 
 static int
-CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
+CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash, struct shash_desc *hmacmd5)
 {
 	int rc;
 	struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
@@ -531,43 +512,33 @@ CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
 	hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
 		offsetof(struct ntlmv2_resp, challenge.key[0]));
 
-	if (!ses->server->secmech.hmacmd5) {
-		cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
-		return -1;
-	}
-
-	rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm,
-				 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
+	rc = crypto_shash_setkey(hmacmd5->tfm, ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
-			 __func__);
+		cifs_dbg(VFS, "%s: Could not set NTLMv2 hash as a key, rc=%d\n", __func__, rc);
 		return rc;
 	}
 
-	rc = crypto_shash_init(ses->server->secmech.hmacmd5);
+	rc = crypto_shash_init(hmacmd5);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
+		cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc);
 		return rc;
 	}
 
 	if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
-		memcpy(ntlmv2->challenge.key,
-		       ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
+		memcpy(ntlmv2->challenge.key, ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
 	else
-		memcpy(ntlmv2->challenge.key,
-		       ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
-	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
-				 ntlmv2->challenge.key, hash_len);
+		memcpy(ntlmv2->challenge.key, ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
+
+	rc = crypto_shash_update(hmacmd5, ntlmv2->challenge.key, hash_len);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
+		cifs_dbg(VFS, "%s: Could not update with response, rc=%d\n", __func__, rc);
 		return rc;
 	}
 
 	/* Note that the MD5 digest over writes anon.challenge_key.key */
-	rc = crypto_shash_final(ses->server->secmech.hmacmd5,
-				ntlmv2->ntlmv2_hash);
+	rc = crypto_shash_final(hmacmd5, ntlmv2->ntlmv2_hash);
 	if (rc)
-		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
+		cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc);
 
 	return rc;
 }
@@ -575,6 +546,7 @@ CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
 int
 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
 {
+	struct shash_desc *hmacmd5 = NULL;
 	int rc;
 	int baselen;
 	unsigned int tilen;
@@ -640,55 +612,51 @@ setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
 
 	cifs_server_lock(ses->server);
 
-	rc = cifs_alloc_hash("hmac(md5)", &ses->server->secmech.hmacmd5);
+	rc = cifs_alloc_hash("hmac(md5)", &hmacmd5);
 	if (rc) {
+		cifs_dbg(VFS, "Could not allocate HMAC-MD5, rc=%d\n", rc);
 		goto unlock;
 	}
 
 	/* calculate ntlmv2_hash */
-	rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
+	rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp, hmacmd5);
 	if (rc) {
-		cifs_dbg(VFS, "Could not get v2 hash rc %d\n", rc);
+		cifs_dbg(VFS, "Could not get NTLMv2 hash, rc=%d\n", rc);
 		goto unlock;
 	}
 
 	/* calculate first part of the client response (CR1) */
-	rc = CalcNTLMv2_response(ses, ntlmv2_hash);
+	rc = CalcNTLMv2_response(ses, ntlmv2_hash, hmacmd5);
 	if (rc) {
-		cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
+		cifs_dbg(VFS, "Could not calculate CR1, rc=%d\n", rc);
 		goto unlock;
 	}
 
 	/* now calculate the session key for NTLMv2 */
-	rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm,
-		ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
+	rc = crypto_shash_setkey(hmacmd5->tfm, ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
-			 __func__);
+		cifs_dbg(VFS, "%s: Could not set NTLMv2 hash as a key, rc=%d\n", __func__, rc);
 		goto unlock;
 	}
 
-	rc = crypto_shash_init(ses->server->secmech.hmacmd5);
+	rc = crypto_shash_init(hmacmd5);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
+		cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc);
 		goto unlock;
 	}
 
-	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
-		ntlmv2->ntlmv2_hash,
-		CIFS_HMAC_MD5_HASH_SIZE);
+	rc = crypto_shash_update(hmacmd5, ntlmv2->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
+		cifs_dbg(VFS, "%s: Could not update with response, rc=%d\n", __func__, rc);
 		goto unlock;
 	}
 
-	rc = crypto_shash_final(ses->server->secmech.hmacmd5,
-		ses->auth_key.response);
+	rc = crypto_shash_final(hmacmd5, ses->auth_key.response);
 	if (rc)
-		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
-
+		cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc);
 unlock:
 	cifs_server_unlock(ses->server);
+	cifs_free_hash(&hmacmd5);
 setup_ntlmv2_rsp_ret:
 	kfree_sensitive(tiblob);
 
@@ -733,7 +701,6 @@ cifs_crypto_secmech_release(struct TCP_Server_Info *server)
 	cifs_free_hash(&server->secmech.hmacsha256);
 	cifs_free_hash(&server->secmech.md5);
 	cifs_free_hash(&server->secmech.sha512);
-	cifs_free_hash(&server->secmech.hmacmd5);
 
 	if (!SERVER_IS_CHAN(server)) {
 		if (server->secmech.enc) {
fs/smb/client/cifsglob.h +0 -1
index 15571cf0ba63..da35c160e7dd 100644
@@ -178,7 +178,6 @@ struct session_key {
 
 /* crypto hashing related structure/fields, not specific to a sec mech */
 struct cifs_secmech {
-	struct shash_desc *hmacmd5; /* hmacmd5 hash function, for NTLMv2/CR1 hashes */
 	struct shash_desc *md5; /* md5 hash function, for CIFS/SMB1 signatures */
 	struct shash_desc *hmacsha256; /* hmac-sha256 hash function, for SMB2 signatures */
 	struct shash_desc *sha512; /* sha512 hash function, for SMB3.1.1 preauth hash */
Signature · 1 line
2.46.0
#7 lore raw

PATCH 4/4 smb: client: make SHA-512 TFM ephemeral

In reply to Enzo Matsumiya

The SHA-512 shash TFM is used only briefly during Session Setup stage, when computing SMB 3.1.1 preauth hash.

There's no need to keep it allocated in servers' secmech the whole time, so keep its lifetime inside smb311_update_preauth_hash().

This also makes smb311_crypto_shash_allocate() redundant, so expose smb3_crypto_shash_allocate() and use that.

---
 fs/smb/client/cifsencrypt.c   |  1 -
 fs/smb/client/cifsglob.h      |  1 -
 fs/smb/client/sess.c          |  2 +-
 fs/smb/client/smb2misc.c      | 28 ++++++++++++++--------------
 fs/smb/client/smb2proto.h     |  2 +-
 fs/smb/client/smb2transport.c | 30 +-----------------------------
 6 files changed, 17 insertions(+), 47 deletions(-)
fs/smb/client/cifsencrypt.c +0 -1
index 464e6ccdfa5f..2d851f596a72 100644
@@ -700,7 +700,6 @@ cifs_crypto_secmech_release(struct TCP_Server_Info *server)
 	cifs_free_hash(&server->secmech.aes_cmac);
 	cifs_free_hash(&server->secmech.hmacsha256);
 	cifs_free_hash(&server->secmech.md5);
-	cifs_free_hash(&server->secmech.sha512);
 
 	if (!SERVER_IS_CHAN(server)) {
 		if (server->secmech.enc) {
fs/smb/client/cifsglob.h +0 -1
index da35c160e7dd..315aac5dec05 100644
@@ -180,7 +180,6 @@ struct session_key {
 struct cifs_secmech {
 	struct shash_desc *md5; /* md5 hash function, for CIFS/SMB1 signatures */
 	struct shash_desc *hmacsha256; /* hmac-sha256 hash function, for SMB2 signatures */
-	struct shash_desc *sha512; /* sha512 hash function, for SMB3.1.1 preauth hash */
 	struct shash_desc *aes_cmac; /* block-cipher based MAC function, for SMB3 signatures */
 
 	struct crypto_aead *enc; /* smb3 encryption AEAD TFM (AES-CCM and AES-GCM) */
fs/smb/client/sess.c +1 -1
index 3216f786908f..03c0b484a4b5 100644
@@ -624,7 +624,7 @@ cifs_ses_add_channel(struct cifs_ses *ses,
 	 * to sign packets before we generate the channel signing key
 	 * (we sign with the session key)
 	 */
-	rc = smb311_crypto_shash_allocate(chan->server);
+	rc = smb3_crypto_shash_allocate(chan->server);
 	if (rc) {
 		cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
 		mutex_unlock(&ses->session_mutex);
fs/smb/client/smb2misc.c +14 -14
index f3c4b70b77b9..bdeb12ff53e3 100644
@@ -906,41 +906,41 @@ smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,
 		|| (hdr->Status !=
 		    cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))))
 		return 0;
-
 ok:
-	rc = smb311_crypto_shash_allocate(server);
-	if (rc)
+	rc = cifs_alloc_hash("sha512", &sha512);
+	if (rc) {
+		cifs_dbg(VFS, "%s: Could not allocate SHA512 shash, rc=%d\n", __func__, rc);
 		return rc;
+	}
 
-	sha512 = server->secmech.sha512;
 	rc = crypto_shash_init(sha512);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not init sha512 shash\n", __func__);
-		return rc;
+		cifs_dbg(VFS, "%s: Could not init SHA512 shash, rc=%d\n", __func__, rc);
+		goto err_free;
 	}
 
 	rc = crypto_shash_update(sha512, ses->preauth_sha_hash,
 				 SMB2_PREAUTH_HASH_SIZE);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not update sha512 shash\n", __func__);
-		return rc;
+		cifs_dbg(VFS, "%s: Could not update SHA512 shash, rc=%d\n", __func__, rc);
+		goto err_free;
 	}
 
 	for (i = 0; i < nvec; i++) {
 		rc = crypto_shash_update(sha512, iov[i].iov_base, iov[i].iov_len);
 		if (rc) {
-			cifs_dbg(VFS, "%s: Could not update sha512 shash\n",
-				 __func__);
-			return rc;
+			cifs_dbg(VFS, "%s: Could not update SHA512 shash, rc=%d\n", __func__, rc);
+			goto err_free;
 		}
 	}
 
 	rc = crypto_shash_final(sha512, ses->preauth_sha_hash);
 	if (rc) {
-		cifs_dbg(VFS, "%s: Could not finalize sha512 shash\n",
-			 __func__);
-		return rc;
+		cifs_dbg(VFS, "%s: Could not finalize SHA12 shash, rc=%d\n", __func__, rc);
+		goto err_free;
 	}
+err_free:
+	cifs_free_hash(&sha512);
 
 	return 0;
 }
fs/smb/client/smb2proto.h +1 -1
index c7e1b149877a..56a896ff7cd9 100644
@@ -291,7 +291,7 @@ extern int smb2_validate_and_copy_iov(unsigned int offset,
 extern void smb2_copy_fs_info_to_kstatfs(
 	 struct smb2_fs_full_size_info *pfs_inf,
 	 struct kstatfs *kst);
-extern int smb311_crypto_shash_allocate(struct TCP_Server_Info *server);
+extern int smb3_crypto_shash_allocate(struct TCP_Server_Info *server);
 extern int smb311_update_preauth_hash(struct cifs_ses *ses,
 				      struct TCP_Server_Info *server,
 				      struct kvec *iov, int nvec);
fs/smb/client/smb2transport.c +1 -29
index c8bf0000f73b..f7e04c40d22e 100644
@@ -26,8 +26,7 @@
 #include "../common/smb2status.h"
 #include "smb2glob.h"
 
-static int
-smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
+int smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
 {
 	struct cifs_secmech *p = &server->secmech;
 	int rc;
@@ -46,33 +45,6 @@ smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
 	return rc;
 }
 
-int
-smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
-{
-	struct cifs_secmech *p = &server->secmech;
-	int rc = 0;
-
-	rc = cifs_alloc_hash("hmac(sha256)", &p->hmacsha256);
-	if (rc)
-		return rc;
-
-	rc = cifs_alloc_hash("cmac(aes)", &p->aes_cmac);
-	if (rc)
-		goto err;
-
-	rc = cifs_alloc_hash("sha512", &p->sha512);
-	if (rc)
-		goto err;
-
-	return 0;
-
-err:
-	cifs_free_hash(&p->aes_cmac);
-	cifs_free_hash(&p->hmacsha256);
-	return rc;
-}
-
-
 static
 int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
 {
Signature · 1 line
2.46.0
#8 lore raw

Re: [PATCH 4/4] smb: client: make SHA-512 TFM ephemeral

Quoted 10 lines · Enzo Matsumiya

The SHA-512 shash TFM is used only briefly during Session Setup stage, when computing SMB 3.1.1 preauth hash.

There's no need to keep it allocated in servers' secmech the whole time, so keep its lifetime inside smb311_update_preauth_hash().

This also makes smb311_crypto_shash_allocate() redundant, so expose smb3_crypto_shash_allocate() and use that.

Quoted 8 lines
---
 fs/smb/client/cifsencrypt.c   |  1 -
 fs/smb/client/cifsglob.h      |  1 -
 fs/smb/client/sess.c          |  2 +-
 fs/smb/client/smb2misc.c      | 28 ++++++++++++++--------------
 fs/smb/client/smb2proto.h     |  2 +-
 fs/smb/client/smb2transport.c | 30 +-----------------------------
 6 files changed, 17 insertions(+), 47 deletions(-)

This commit leads to the following NULL ptr deref when mounting a share with 'vers=2.1'. Reproduced it against Windows Server 2022 and samba 4.21.

  $ mount.cifs //srv/share /mnt -o ...,vers=2.1
  BUG: KASAN: null-ptr-deref in smb2_calc_signature+0x195/0x4f0 [cifs]
  Read of size 8 at addr 0000000000000000 by task mount.cifs/693
  
  CPU: 2 UID: 0 PID: 693 Comm: mount.cifs Not tainted 6.11.0 #3
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-2.fc40
  04/01/2014
  Call Trace:
   <TASK>
   dump_stack_lvl+0x5d/0x80
   ? smb2_calc_signature+0x195/0x4f0 [cifs]
   kasan_report+0xda/0x110
   ? smb2_calc_signature+0x195/0x4f0 [cifs]
   smb2_calc_signature+0x195/0x4f0 [cifs]
   ? __pfx_smb2_calc_signature+0x10/0x10 [cifs]
   ? do_raw_spin_trylock+0xc6/0x120
   ? do_raw_spin_unlock+0x9a/0xf0
   ? _raw_spin_unlock+0x23/0x40
   ? smb2_sign_rqst+0x10c/0x160 [cifs]
   smb2_setup_request+0x225/0x3a0 [cifs]
   compound_send_recv+0x417/0x1140 [cifs]
   ? __pfx_compound_send_recv+0x10/0x10 [cifs]
   ? __create_object+0x5e/0x90
   ? hlock_class+0x32/0xb0
   ? do_raw_spin_unlock+0x9a/0xf0
   cifs_send_recv+0x23/0x30 [cifs]
   SMB2_tcon+0x3ec/0xb30 [cifs]
   ? __pfx_SMB2_tcon+0x10/0x10 [cifs]
   ? ___ratelimit+0x133/0x1f0
   ? __pfx____ratelimit+0x10/0x10
   ? __pfx_SMB2_tcon+0x10/0x10 [cifs]
   ? cifs_get_smb_ses+0xcaf/0x1070 [cifs]
   cifs_get_smb_ses+0xcaf/0x1070 [cifs]
   ? __pfx_cifs_get_smb_ses+0x10/0x10 [cifs]
   ? cifs_get_tcp_session+0xaa0/0xca0 [cifs]
   cifs_mount_get_session+0x8a/0x210 [cifs]
   dfs_mount_share+0x1b0/0x11d0 [cifs]
   ? __pfx___lock_acquire+0x10/0x10
   ? __pfx_dfs_mount_share+0x10/0x10 [cifs]
   ? lock_acquire+0x14b/0x3e0
   ? find_held_lock+0x8a/0xa0
   ? hlock_class+0x32/0xb0
   ? lock_release+0x203/0x5d0
   cifs_mount+0xb3/0x3d0 [cifs]
   ? do_raw_spin_trylock+0xc6/0x120
   ? __pfx_cifs_mount+0x10/0x10 [cifs]
   ? ___ratelimit+0x133/0x1f0
   ? smb3_update_mnt_flags+0x372/0x3b0 [cifs]
   cifs_smb3_do_mount+0x1e2/0xc80 [cifs]
   ? __pfx___mutex_lock+0x10/0x10
   ? __pfx_cifs_smb3_do_mount+0x10/0x10 [cifs]
   smb3_get_tree+0x1bf/0x330 [cifs]
   vfs_get_tree+0x4a/0x160
   path_mount+0x3c1/0xfb0
   ? kasan_quarantine_put+0xc7/0x1d0
   ? __pfx_path_mount+0x10/0x10
   ? kmem_cache_free+0x118/0x3e0
   ? user_path_at+0x74/0xa0
   __x64_sys_mount+0x1a6/0x1e0
   ? __pfx___x64_sys_mount+0x10/0x10
   ? mark_held_locks+0x1a/0x90
   do_syscall_64+0xbb/0x1d0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
  RIP: 0033:0x7fd23a9a88fe
  Code: 48 8b 0d 1d a5 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84
  00 00 00 00 00 90 f3 0f 1e fa 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01
  f0 ff ff 73 01 c3 48 8b 0d ea a4 0c 00 f7 d8 64 89 01 48
  RSP: 002b:00007ffdeef79388 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
  RAX: ffffffffffffffda RBX: 0000562a96057eb0 RCX: 00007fd23a9a88fe
  RDX: 0000562a6d33347e RSI: 0000562a6d3334e4 RDI: 00007ffdeef7a4a2
  RBP: 00007ffdeef79440 R08: 0000562a96057eb0 R09: 0000000000000000
  R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000a
  R13: 00007ffdeef7a4a2 R14: 0000562a96058f00 R15: 00007fd23aa96000
   </TASK>
  $ ./scripts/faddr2line --list fs/smb/client/cifs.o smb2_calc_signature+0x195
  smb2_calc_signature+0x195/0x4f0:
  smb2_calc_signature at /home/pc/g/linux/fs/smb/client/smb2transport.c:235
   230 			}
   231 		} else {
   232 			shash = server->secmech.hmacsha256;
   233 		}
   234 	
  >235<		rc = crypto_shash_setkey(shash->tfm, ses->auth_key.response,
   236 				SMB2_NTLMV2_SESSKEY_SIZE);
   237 		if (rc) {
   238 			cifs_server_dbg(VFS,
   239 					"%s: Could not update with response\n",
   240 					__func__);

What's the problem with having it allocated the whole time? Reconnect path will need it to re-establish SMB sessions, so this means we'll now have to allocate it every time we attempt to reconnect a session? This could be worse when smb2_reconnect_server() is periodically attemping to reconnect a session.

#9 lore raw

Re: [PATCH 4/4] smb: client: make SHA-512 TFM ephemeral

I also see a problem with smb2.1 signed mounts (will need to make sure our buildbot is testing with this config) so have reverted this patch in for-next

Quoted 123 lines · Paulo Alcantara
Quoted 10 lines · Enzo Matsumiya

The SHA-512 shash TFM is used only briefly during Session Setup stage, when computing SMB 3.1.1 preauth hash.

There's no need to keep it allocated in servers' secmech the whole time, so keep its lifetime inside smb311_update_preauth_hash().

This also makes smb311_crypto_shash_allocate() redundant, so expose smb3_crypto_shash_allocate() and use that.

Quoted 8 lines
---
 fs/smb/client/cifsencrypt.c   |  1 -
 fs/smb/client/cifsglob.h      |  1 -
 fs/smb/client/sess.c          |  2 +-
 fs/smb/client/smb2misc.c      | 28 ++++++++++++++--------------
 fs/smb/client/smb2proto.h     |  2 +-
 fs/smb/client/smb2transport.c | 30 +-----------------------------
 6 files changed, 17 insertions(+), 47 deletions(-)

This commit leads to the following NULL ptr deref when mounting a share with 'vers=2.1'. Reproduced it against Windows Server 2022 and samba 4.21.

  $ mount.cifs //srv/share /mnt -o ...,vers=2.1
  BUG: KASAN: null-ptr-deref in smb2_calc_signature+0x195/0x4f0 [cifs]
  Read of size 8 at addr 0000000000000000 by task mount.cifs/693

  CPU: 2 UID: 0 PID: 693 Comm: mount.cifs Not tainted 6.11.0 #3
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-2.fc40
  04/01/2014
  Call Trace:
   <TASK>
   dump_stack_lvl+0x5d/0x80
   ? smb2_calc_signature+0x195/0x4f0 [cifs]
   kasan_report+0xda/0x110
   ? smb2_calc_signature+0x195/0x4f0 [cifs]
   smb2_calc_signature+0x195/0x4f0 [cifs]
   ? __pfx_smb2_calc_signature+0x10/0x10 [cifs]
   ? do_raw_spin_trylock+0xc6/0x120
   ? do_raw_spin_unlock+0x9a/0xf0
   ? _raw_spin_unlock+0x23/0x40
   ? smb2_sign_rqst+0x10c/0x160 [cifs]
   smb2_setup_request+0x225/0x3a0 [cifs]
   compound_send_recv+0x417/0x1140 [cifs]
   ? __pfx_compound_send_recv+0x10/0x10 [cifs]
   ? __create_object+0x5e/0x90
   ? hlock_class+0x32/0xb0
   ? do_raw_spin_unlock+0x9a/0xf0
   cifs_send_recv+0x23/0x30 [cifs]
   SMB2_tcon+0x3ec/0xb30 [cifs]
   ? __pfx_SMB2_tcon+0x10/0x10 [cifs]
   ? ___ratelimit+0x133/0x1f0
   ? __pfx____ratelimit+0x10/0x10
   ? __pfx_SMB2_tcon+0x10/0x10 [cifs]
   ? cifs_get_smb_ses+0xcaf/0x1070 [cifs]
   cifs_get_smb_ses+0xcaf/0x1070 [cifs]
   ? __pfx_cifs_get_smb_ses+0x10/0x10 [cifs]
   ? cifs_get_tcp_session+0xaa0/0xca0 [cifs]
   cifs_mount_get_session+0x8a/0x210 [cifs]
   dfs_mount_share+0x1b0/0x11d0 [cifs]
   ? __pfx___lock_acquire+0x10/0x10
   ? __pfx_dfs_mount_share+0x10/0x10 [cifs]
   ? lock_acquire+0x14b/0x3e0
   ? find_held_lock+0x8a/0xa0
   ? hlock_class+0x32/0xb0
   ? lock_release+0x203/0x5d0
   cifs_mount+0xb3/0x3d0 [cifs]
   ? do_raw_spin_trylock+0xc6/0x120
   ? __pfx_cifs_mount+0x10/0x10 [cifs]
   ? ___ratelimit+0x133/0x1f0
   ? smb3_update_mnt_flags+0x372/0x3b0 [cifs]
   cifs_smb3_do_mount+0x1e2/0xc80 [cifs]
   ? __pfx___mutex_lock+0x10/0x10
   ? __pfx_cifs_smb3_do_mount+0x10/0x10 [cifs]
   smb3_get_tree+0x1bf/0x330 [cifs]
   vfs_get_tree+0x4a/0x160
   path_mount+0x3c1/0xfb0
   ? kasan_quarantine_put+0xc7/0x1d0
   ? __pfx_path_mount+0x10/0x10
   ? kmem_cache_free+0x118/0x3e0
   ? user_path_at+0x74/0xa0
   __x64_sys_mount+0x1a6/0x1e0
   ? __pfx___x64_sys_mount+0x10/0x10
   ? mark_held_locks+0x1a/0x90
   do_syscall_64+0xbb/0x1d0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
  RIP: 0033:0x7fd23a9a88fe
  Code: 48 8b 0d 1d a5 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84
  00 00 00 00 00 90 f3 0f 1e fa 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01
  f0 ff ff 73 01 c3 48 8b 0d ea a4 0c 00 f7 d8 64 89 01 48
  RSP: 002b:00007ffdeef79388 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
  RAX: ffffffffffffffda RBX: 0000562a96057eb0 RCX: 00007fd23a9a88fe
  RDX: 0000562a6d33347e RSI: 0000562a6d3334e4 RDI: 00007ffdeef7a4a2
  RBP: 00007ffdeef79440 R08: 0000562a96057eb0 R09: 0000000000000000
  R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000a
  R13: 00007ffdeef7a4a2 R14: 0000562a96058f00 R15: 00007fd23aa96000
   </TASK>
  $ ./scripts/faddr2line --list fs/smb/client/cifs.o smb2_calc_signature+0x195
  smb2_calc_signature+0x195/0x4f0:
  smb2_calc_signature at /home/pc/g/linux/fs/smb/client/smb2transport.c:235
   230                  }
   231          } else {
   232                  shash = server->secmech.hmacsha256;
   233          }
   234
  >235<         rc = crypto_shash_setkey(shash->tfm, ses->auth_key.response,
   236                          SMB2_NTLMV2_SESSKEY_SIZE);
   237          if (rc) {
   238                  cifs_server_dbg(VFS,
   239                                  "%s: Could not update with response\n",
   240                                  __func__);

What's the problem with having it allocated the whole time? Reconnect path will need it to re-establish SMB sessions, so this means we'll now have to allocate it every time we attempt to reconnect a session? This could be worse when smb2_reconnect_server() is periodically attemping to reconnect a session.

Signature · 3 lines
Thanks,

Steve
#10 lore raw

Re: [PATCH 4/4] smb: client: make SHA-512 TFM ephemeral

I have also added a few tests to the 'buildbot' that will do signed SMB2.1 mounts so this won't be missed in the future.

Quoted 136 lines · Steve French

I also see a problem with smb2.1 signed mounts (will need to make sure our buildbot is testing with this config) so have reverted this patch in for-next

Quoted 118 lines · Paulo Alcantara
Quoted 10 lines · Enzo Matsumiya

The SHA-512 shash TFM is used only briefly during Session Setup stage, when computing SMB 3.1.1 preauth hash.

There's no need to keep it allocated in servers' secmech the whole time, so keep its lifetime inside smb311_update_preauth_hash().

This also makes smb311_crypto_shash_allocate() redundant, so expose smb3_crypto_shash_allocate() and use that.

Quoted 8 lines
---
 fs/smb/client/cifsencrypt.c   |  1 -
 fs/smb/client/cifsglob.h      |  1 -
 fs/smb/client/sess.c          |  2 +-
 fs/smb/client/smb2misc.c      | 28 ++++++++++++++--------------
 fs/smb/client/smb2proto.h     |  2 +-
 fs/smb/client/smb2transport.c | 30 +-----------------------------
 6 files changed, 17 insertions(+), 47 deletions(-)

This commit leads to the following NULL ptr deref when mounting a share with 'vers=2.1'. Reproduced it against Windows Server 2022 and samba 4.21.

  $ mount.cifs //srv/share /mnt -o ...,vers=2.1
  BUG: KASAN: null-ptr-deref in smb2_calc_signature+0x195/0x4f0 [cifs]
  Read of size 8 at addr 0000000000000000 by task mount.cifs/693

  CPU: 2 UID: 0 PID: 693 Comm: mount.cifs Not tainted 6.11.0 #3
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-2.fc40
  04/01/2014
  Call Trace:
   <TASK>
   dump_stack_lvl+0x5d/0x80
   ? smb2_calc_signature+0x195/0x4f0 [cifs]
   kasan_report+0xda/0x110
   ? smb2_calc_signature+0x195/0x4f0 [cifs]
   smb2_calc_signature+0x195/0x4f0 [cifs]
   ? __pfx_smb2_calc_signature+0x10/0x10 [cifs]
   ? do_raw_spin_trylock+0xc6/0x120
   ? do_raw_spin_unlock+0x9a/0xf0
   ? _raw_spin_unlock+0x23/0x40
   ? smb2_sign_rqst+0x10c/0x160 [cifs]
   smb2_setup_request+0x225/0x3a0 [cifs]
   compound_send_recv+0x417/0x1140 [cifs]
   ? __pfx_compound_send_recv+0x10/0x10 [cifs]
   ? __create_object+0x5e/0x90
   ? hlock_class+0x32/0xb0
   ? do_raw_spin_unlock+0x9a/0xf0
   cifs_send_recv+0x23/0x30 [cifs]
   SMB2_tcon+0x3ec/0xb30 [cifs]
   ? __pfx_SMB2_tcon+0x10/0x10 [cifs]
   ? ___ratelimit+0x133/0x1f0
   ? __pfx____ratelimit+0x10/0x10
   ? __pfx_SMB2_tcon+0x10/0x10 [cifs]
   ? cifs_get_smb_ses+0xcaf/0x1070 [cifs]
   cifs_get_smb_ses+0xcaf/0x1070 [cifs]
   ? __pfx_cifs_get_smb_ses+0x10/0x10 [cifs]
   ? cifs_get_tcp_session+0xaa0/0xca0 [cifs]
   cifs_mount_get_session+0x8a/0x210 [cifs]
   dfs_mount_share+0x1b0/0x11d0 [cifs]
   ? __pfx___lock_acquire+0x10/0x10
   ? __pfx_dfs_mount_share+0x10/0x10 [cifs]
   ? lock_acquire+0x14b/0x3e0
   ? find_held_lock+0x8a/0xa0
   ? hlock_class+0x32/0xb0
   ? lock_release+0x203/0x5d0
   cifs_mount+0xb3/0x3d0 [cifs]
   ? do_raw_spin_trylock+0xc6/0x120
   ? __pfx_cifs_mount+0x10/0x10 [cifs]
   ? ___ratelimit+0x133/0x1f0
   ? smb3_update_mnt_flags+0x372/0x3b0 [cifs]
   cifs_smb3_do_mount+0x1e2/0xc80 [cifs]
   ? __pfx___mutex_lock+0x10/0x10
   ? __pfx_cifs_smb3_do_mount+0x10/0x10 [cifs]
   smb3_get_tree+0x1bf/0x330 [cifs]
   vfs_get_tree+0x4a/0x160
   path_mount+0x3c1/0xfb0
   ? kasan_quarantine_put+0xc7/0x1d0
   ? __pfx_path_mount+0x10/0x10
   ? kmem_cache_free+0x118/0x3e0
   ? user_path_at+0x74/0xa0
   __x64_sys_mount+0x1a6/0x1e0
   ? __pfx___x64_sys_mount+0x10/0x10
   ? mark_held_locks+0x1a/0x90
   do_syscall_64+0xbb/0x1d0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
  RIP: 0033:0x7fd23a9a88fe
  Code: 48 8b 0d 1d a5 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84
  00 00 00 00 00 90 f3 0f 1e fa 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01
  f0 ff ff 73 01 c3 48 8b 0d ea a4 0c 00 f7 d8 64 89 01 48
  RSP: 002b:00007ffdeef79388 EFLAGS: 00000246 ORIG_RAX: 00000000000000a5
  RAX: ffffffffffffffda RBX: 0000562a96057eb0 RCX: 00007fd23a9a88fe
  RDX: 0000562a6d33347e RSI: 0000562a6d3334e4 RDI: 00007ffdeef7a4a2
  RBP: 00007ffdeef79440 R08: 0000562a96057eb0 R09: 0000000000000000
  R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000a
  R13: 00007ffdeef7a4a2 R14: 0000562a96058f00 R15: 00007fd23aa96000
   </TASK>
  $ ./scripts/faddr2line --list fs/smb/client/cifs.o smb2_calc_signature+0x195
  smb2_calc_signature+0x195/0x4f0:
  smb2_calc_signature at /home/pc/g/linux/fs/smb/client/smb2transport.c:235
   230                  }
   231          } else {
   232                  shash = server->secmech.hmacsha256;
   233          }
   234
  >235<         rc = crypto_shash_setkey(shash->tfm, ses->auth_key.response,
   236                          SMB2_NTLMV2_SESSKEY_SIZE);
   237          if (rc) {
   238                  cifs_server_dbg(VFS,
   239                                  "%s: Could not update with response\n",
   240                                  __func__);
Quoted 5 lines

What's the problem with having it allocated the whole time? Reconnect path will need it to re-establish SMB sessions, so this means we'll now have to allocate it every time we attempt to reconnect a session? This could be worse when smb2_reconnect_server() is periodically attemping to reconnect a session.

--
Thanks,

Steve

Signature · 3 lines
Thanks,

Steve