smb: client: Avoid leaking of sensitive data to the stack or heap
Subsystems (2)
cover smb: client: Avoid leaking of sensitive data to the stack or heap
Similar to the patch series that I sent for the server code recently, here's a clean-up series for the client part:
Sensitive data like keys that are stored in stack-local arrays could be leaked via the stack to the calling functions, or via the heap when using only normal kfree() functions.
It's good security practice to clear sensitive data on the stack first with memzero_explicit() before leaving the context, and to use kfree_sensitive() for data that is returned to the heap.
Thomas Huth (5):
smb: client: Clear sensitive stack data in smb2transport.c
smb: client: Clear sensitive stack and heap data in smb2ops.c
smb: client: Clear sensitive stack data in cifsencrypt.c
smb: client: Clear sensitive stack data in smb1encrypt.c
smb: client: Avoid leaking sensitive data to the heap in connect.c
fs/smb/client/cifsencrypt.c | 12 +++++++++--- fs/smb/client/connect.c | 2 +- fs/smb/client/smb1encrypt.c | 19 +++++++++---------- fs/smb/client/smb2ops.c | 4 ++-- fs/smb/client/smb2transport.c | 4 ++++ 5 files changed, 25 insertions(+), 16 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/5 smb: client: Clear sensitive stack data in smb2transport.c
From: Thomas Huth <[email protected]>
Sensitive data like keys that are stored in stack-local arrays could be leaked via the stack to the calling functions. There is no known vulnerability for this right now, but it's good security style to explicitly zeroize this sensitive material as soon as possible to avoid that it could be exploited together with other bugs later.
--- fs/smb/client/smb2transport.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/smb/client/smb2transport.c +4 -0
@@ -249,6 +249,8 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)if (!rc)memcpy (shdr->Signature, smb2_signature,SMB2_SIGNATURE_SIZE ); +memzero_explicit (key,sizeof (key)); +memzero_explicit (&hmac_ctx,sizeof (hmac_ctx));return rc; }
@@ -283,6 +285,7 @@ static void generate_key(struct cifs_ses *ses, struct kvec label,hmac_sha256_final (&hmac_ctx, prfhash);memcpy (key, prfhash, key_size); +memzero_explicit (prfhash,sizeof (prfhash)); }struct derivation {
@@ -482,6 +485,7 @@ smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)memset (shdr->Signature,0x0 ,SMB2_SIGNATURE_SIZE ); rc =aes_cmac_preparekey (&cmac_key, key,SMB2_CMACAES_SIZE ); +memzero_explicit (key,sizeof (key));if (rc) {cifs_server_dbg (VFS ,"%s: Could not set key for cmac aes\n" , __func__);return rc;
Signature · 1 line
2.55.0
Namjae Jeon
maintainerRe: [PATCH 1/5] smb: client: Clear sensitive stack data in smb2transport.c
fs/smb/client/smb2transport.c smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) · line 488
Quoted 6 lines
struct derivation {
fs/smb/client/smb2transport.c +1
-0@@ -482,6 +485,7 @@ smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)memset (shdr->Signature,0x0 ,SMB2_SIGNATURE_SIZE ); rc =aes_cmac_preparekey (&cmac_key, key,SMB2_CMACAES_SIZE ); +memzero_explicit (key,sizeof (key));
cmac_key is not zeroized, and cmac_ctx is not zeroized on error paths in smb3_calc_signature(). Please zeroize both on all exit paths, taking care not to clear an uninitialized context.
Re: [PATCH 1/5] smb: client: Clear sensitive stack data in smb2transport.c
Quoted 9 lines · Namjae Jeon
fs/smb/client/smb2transport.c smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) · line 488
Quoted 6 lines
struct derivation {fs/smb/client/smb2transport.c +1
-0@@ -482,6 +485,7 @@ smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)memset (shdr->Signature,0x0 ,SMB2_SIGNATURE_SIZE ); rc =aes_cmac_preparekey (&cmac_key, key,SMB2_CMACAES_SIZE ); +memzero_explicit (key,sizeof (key));
cmac_keyis not zeroized, andcmac_ctxis not zeroized on error paths insmb3_calc_signature(). Please zeroize both on all exit paths, taking care not to clear an uninitialized context.
Hi!
The cmac_key and cmac_ctx have just been handled via the new __cleanup() functions, that's why I did not add these to this patch here. The patch has just been merged:
Thomas
Namjae Jeon
maintainerRe: [PATCH 1/5] smb: client: Clear sensitive stack data in smb2transport.c
Quoted 11 lines · Thomas Huth
Quoted 9 lines · Namjae Jeon
fs/smb/client/smb2transport.c smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) · line 488
Quoted 6 lines
struct derivation {fs/smb/client/smb2transport.c +1
-0@@ -482,6 +485,7 @@ smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)memset (shdr->Signature,0x0 ,SMB2_SIGNATURE_SIZE ); rc =aes_cmac_preparekey (&cmac_key, key,SMB2_CMACAES_SIZE ); +memzero_explicit (key,sizeof (key));
cmac_keyis not zeroized, andcmac_ctxis not zeroized on error paths insmb3_calc_signature(). Please zeroize both on all exit paths, taking care not to clear an uninitialized context.
Quoted 7 lines
Hi!
The
cmac_keyandcmac_ctxhave just been handled via the new__cleanup()functions, that's why I did not add these to this patch here. The patch has just been merged:
Okay. I will apply this patch-set to #for-next.
Thanks!
PATCH 2/5 smb: client: Clear sensitive stack and heap data in smb2ops.c
In reply to Thomas HuthFrom: Thomas Huth <[email protected]>
Make sure to not leak key-related data via the heap or the stack by using kfree_sensitive() or memzero_explicit() here.
--- fs/smb/client/smb2ops.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/smb/client/smb2ops.c +2 -2
@@ -1569,7 +1569,7 @@ SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,memcpy (pcchunk->SourceKey, res_key->ResumeKey,COPY_CHUNK_RES_KEY_SIZE ); req_res_key_exit: -kfree (res_key); +kfree_sensitive (res_key);return rc; }
@@ -4636,7 +4636,7 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst, rc =crypto_aead_setkey (tfm, key,SMB3_GCM256_CRYPTKEY_SIZE ); else rc =crypto_aead_setkey (tfm, key,SMB3_GCM128_CRYPTKEY_SIZE ); - +memzero_explicit (key,sizeof (key));if (rc) {cifs_server_dbg (VFS ,"%s: Failed to set aead key %d\n" , __func__, rc);return rc;
Signature · 1 line
2.55.0
PATCH 3/5 smb: client: Clear sensitive stack data in cifsencrypt.c
In reply to Thomas HuthFrom: Thomas Huth <[email protected]>
Make sure to not leak hash data via the stack, clear it with memzero_explicit() before leaving the function.
--- fs/smb/client/cifsencrypt.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
fs/smb/client/cifsencrypt.c +9 -3
@@ -249,12 +249,13 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,E_md4hash (ses->password, nt_hash, nls_cp);hmac_md5_init_usingrawkey (&hmac_ctx, nt_hash,CIFS_NTHASH_SIZE ); +memzero_explicit (nt_hash,sizeof (nt_hash));/* 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 ) -return -ENOMEM ; + goto out_nomem;if (len) { len =cifs_strtoUTF16 (user, ses->user_name, len, nls_cp);
@@ -272,7 +273,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, domain =kmalloc (2 + (len *2 ),GFP_KERNEL );if (domain ==NULL ) -return -ENOMEM ; + goto out_nomem; len =cifs_strtoUTF16 ((__le16 *)domain, ses->domainName, len, nls_cp);
@@ -284,7 +285,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, server =kmalloc (2 + (len *2 ),GFP_KERNEL );if (server ==NULL ) -return -ENOMEM ; + goto out_nomem; len =cifs_strtoUTF16 ((__le16 *)server, ses->ip_addr, len, nls_cp);hmac_md5_update (&hmac_ctx, (const u8 *)server,2 * len);
@@ -293,6 +294,10 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,hmac_md5_final (&hmac_ctx, ntlmv2_hash);return 0 ; + +out_nomem: +memzero_explicit (&hmac_ctx,sizeof (hmac_ctx)); +return -ENOMEM ; }static void CalcNTLMv2_response (const struct cifs_ses *ses,char *ntlmv2_hash)
@@ -463,6 +468,7 @@ setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp) rc =0 ; unlock:cifs_server_unlock (ses->server); +memzero_explicit (ntlmv2_hash,sizeof (ntlmv2_hash)); setup_ntlmv2_rsp_ret:kfree_sensitive (tiblob);
Signature · 1 line
2.55.0
PATCH 4/5 smb: client: Clear sensitive stack data in smb1encrypt.c
In reply to Thomas HuthFrom: Thomas Huth <[email protected]>
Make sure to not leak signature data via the stack, clear it with memzero_explicit() before leaving the function.
To avoid that we have to introduce "goto"-cleanup here, we re-arrange the code a little bit (and drop the commented cifs_dump_mem debug code that looks like a leftover from very early days).
--- fs/smb/client/smb1encrypt.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
fs/smb/client/smb1encrypt.c +9 -10
@@ -81,6 +81,7 @@ int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,else memcpy (cifs_pdu->Signature.SecuritySignature, smb_signature,8 ); +memzero_explicit (smb_signature,sizeof (smb_signature));return rc; }
@@ -126,15 +127,13 @@ int cifs_verify_signature(struct smb_rqst *rqst, rc =cifs_calc_signature (rqst, server, what_we_think_sig_should_be);cifs_server_unlock (server); -if (rc) -return rc; - -/* cifs_dump_mem("what we think it should be: ", -what_we_think_sig_should_be, 16); */ - -if (crypto_memneq (server_response_sig, what_we_think_sig_should_be,8 )) -return -EACCES ; -else -return 0 ; +if (!rc) { +if (crypto_memneq (server_response_sig, + what_we_think_sig_should_be,8 )) + rc = -EACCES ; + } +memzero_explicit (what_we_think_sig_should_be, +sizeof (what_we_think_sig_should_be)); +return rc; }
Signature · 1 line
2.55.0
Namjae Jeon
maintainerRe: [PATCH 4/5] smb: client: Clear sensitive stack data in smb1encrypt.c
Quoted 28 lines · Thomas Huth
From: Thomas Huth <[email protected]>
Make sure to not leak signature data via the stack, clear it with
memzero_explicit()before leaving the function.To avoid that we have to introduce "goto"-cleanup here, we re-arrange the code a little bit (and drop the commented
cifs_dump_memdebug code that looks like a leftover from very early days).Signed-off-by Thomas Huth <[email protected]>--- fs/smb/client/smb1encrypt.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)fs/smb/client/smb1encrypt.c +1
-0@@ -81,6 +81,7 @@ int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,else memcpy (cifs_pdu->Signature.SecuritySignature, smb_signature,8 ); +memzero_explicit (smb_signature,sizeof (smb_signature));return rc; }
fs/smb/client/smb1encrypt.c int cifs_verify_signature(struct smb_rqst *rqst, · line 128
Quoted 2 lines
fs/smb/client/smb1encrypt.c +0
-0@@ -126,15 +127,13 @@ int cifs_verify_signature(struct smb_rqst *rqst, rc =cifs_calc_signature (rqst, server, what_we_think_sig_should_be);cifs_server_unlock (server);
The MD5 context in cifs_calc_signature() may remain uncleared when __cifs_calc_signature() fails.
rc = __cifs_calc_signature(...);
┃memzero_explicit(&ctx, sizeof(ctx));
Re: [PATCH 4/5] smb: client: Clear sensitive stack data in smb1encrypt.c
Quoted 34 lines · Namjae Jeon
Quoted 28 lines · Thomas Huth
From: Thomas Huth <[email protected]>
Make sure to not leak signature data via the stack, clear it with
memzero_explicit()before leaving the function.To avoid that we have to introduce "goto"-cleanup here, we re-arrange the code a little bit (and drop the commented
cifs_dump_memdebug code that looks like a leftover from very early days).Signed-off-by Thomas Huth <[email protected]>--- fs/smb/client/smb1encrypt.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)fs/smb/client/smb1encrypt.c +1
-0@@ -81,6 +81,7 @@ int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,else memcpy (cifs_pdu->Signature.SecuritySignature, smb_signature,8 ); +memzero_explicit (smb_signature,sizeof (smb_signature));return rc; }fs/smb/client/smb1encrypt.c int cifs_verify_signature(struct smb_rqst *rqst, · line 128
Quoted 2 lines
fs/smb/client/smb1encrypt.c +0
-0@@ -126,15 +127,13 @@ int cifs_verify_signature(struct smb_rqst *rqst, rc =cifs_calc_signature (rqst, server, what_we_think_sig_should_be);cifs_server_unlock (server);The MD5 context in
cifs_calc_signature()may remain uncleared when__cifs_calc_signature()fails.
Quoted 3 lines
rc =
__cifs_calc_signature(...);
┃memzero_explicit(&ctx, sizeof(ctx));
My plan was to handle that via a __cleanup() function, too, but I need to get that merged first:
https://lore.kernel.org/lkml/[email protected]/
Thomas
Namjae Jeon
maintainerRe: [PATCH 4/5] smb: client: Clear sensitive stack data in smb1encrypt.c
Quoted 36 lines · Thomas Huth
Quoted 34 lines · Namjae Jeon
Quoted 28 lines · Thomas Huth
From: Thomas Huth <[email protected]>
Make sure to not leak signature data via the stack, clear it with
memzero_explicit()before leaving the function.To avoid that we have to introduce "goto"-cleanup here, we re-arrange the code a little bit (and drop the commented
cifs_dump_memdebug code that looks like a leftover from very early days).Signed-off-by Thomas Huth <[email protected]>--- fs/smb/client/smb1encrypt.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)fs/smb/client/smb1encrypt.c +1
-0@@ -81,6 +81,7 @@ int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,else memcpy (cifs_pdu->Signature.SecuritySignature, smb_signature,8 ); +memzero_explicit (smb_signature,sizeof (smb_signature));return rc; }fs/smb/client/smb1encrypt.c int cifs_verify_signature(struct smb_rqst *rqst, · line 128
Quoted 2 lines
fs/smb/client/smb1encrypt.c +0
-0@@ -126,15 +127,13 @@ int cifs_verify_signature(struct smb_rqst *rqst, rc =cifs_calc_signature (rqst, server, what_we_think_sig_should_be);cifs_server_unlock (server);The MD5 context in
cifs_calc_signature()may remain uncleared when__cifs_calc_signature()fails.
Quoted 7 lines
Quoted 3 lines
rc =
__cifs_calc_signature(...);
┃memzero_explicit(&ctx, sizeof(ctx));My plan was to handle that via a
__cleanup()function, too, but I need to get that merged first:
Okay.
Thanks.
PATCH 5/5 smb: client: Avoid leaking sensitive data to the heap in connect.c
In reply to Thomas HuthFrom: Thomas Huth <[email protected]>
TCP_Server_Info contains a preauth_sha_hash[] and a cryptkey[] array that might contain sensitive data. Thus free its memory with kfree_sensitive() to avoid that we are leaking this information to the heap.
--- fs/smb/client/connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/connect.c +1 -1
@@ -1143,7 +1143,7 @@ clean_demultiplex_info(struct TCP_Server_Info *server)put_net (cifs_net_ns (server));kfree (server->leaf_fullpath);kfree (server->hostname); -kfree (server); +kfree_sensitive (server); length =atomic_dec_return (&tcpSesAllocCount);if (length >0 )
Signature · 1 line
2.55.0