Skip to contentSkip to conversation

smb: server: Clear sensitive data before freeing it

PATCH Applied Thomas Huth 2 participants 7 messages Aug 11 – Aug 12 Kernel SMB3 server (ksmbd)

lore
Share & tools
lore
#0 lore raw

v2 cover smb: server: Clear sensitive data before freeing it

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.

Disclaimer: The spots that need clearing have been identified with AI, but the patches have been created manually (for double-checking whether the findings really make sense). Anyway, I'm not very familiar with the smb code, so please review carefully. Thanks!

v2:
- Calculate proper size for the kvfree_sensitive() in the first patch
- Add "kfree_sensitive(target)" hunk to the connection.c patch
- Add patch to clear Preauth_HashValue in smb2pdu.c

Thomas Huth (5):
smb: server: Clear sensitive stack and heap data in auth.c
smb: server: Make sure that passkey is not leaked on the heap in
user_config.c
smb: server: Free session data in user_session.c with
kfree_sensitive()
smb: server: Free sensitive data in connection.c with
kfree_sensitive()
smb: server: Clear Preauth_HashValue in smb2pdu.c with
kfree_sensitive()

 fs/smb/server/auth.c              | 12 ++++++++++--
 fs/smb/server/connection.c        |  6 +++---
 fs/smb/server/mgmt/user_config.c  |  6 +++---
 fs/smb/server/mgmt/user_session.c |  4 ++--
 fs/smb/server/smb2pdu.c           |  4 ++--
 5 files changed, 20 insertions(+), 12 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/5 smb: server: Clear sensitive stack and heap data in auth.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, or via the heap when using only normal kfree() functions. There is no known vulnaribility for this right now, but it's good security style to explicitly zeroize this sensitive matieral as soon as possible to avoid that it could be exploited together with other bugs later.

In calc_ntlmv2_hash(), the struct hmac_md5_ctx is normally cleared during hmac_md5_final() already, but in case of errors, this function is skipped and ctx is never zeroized, so add a memzero_explicit(&ctx, sizeof(ctx)) there to fix the problem.

In ksmbd_krb5_authenticate(), the ksmbd_spnego_authen_response contains the session key in the payload. It's currently freed with plain kvfree(). Let's better use kvfree_sensitive() instead.

In generate_key(), the prfhash[] array is used to calculate the key, but it's never cleared, so it leaks on the stack. Thus clear this with a memzero_explicit(), too.

In ksmbd_crypt_message(), the sign[] and key[] arrays are leaked via the stack, too. Make sure to clear them via memzero_explicit() at the end.

---
 fs/smb/server/auth.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/smb/server/auth.c +10 -2
index 4e7b6f0e6b8cd..573c5c6492c97 100644
@@ -122,6 +122,8 @@ static int calc_ntlmv2_hash(struct ksmbd_conn *conn, struct ksmbd_session *sess,
 out:
 	kfree(uniname);
 	kfree(domain);
+	if (ret)	/* Done by hmac_md5_final() already if ret == 0 */
+		memzero_explicit(&ctx, sizeof(ctx));
 	return ret;
 }
 
@@ -464,7 +466,8 @@ int ksmbd_krb5_authenticate(struct ksmbd_session *sess, char *in_blob,
 	*out_len = resp->spnego_blob_len;
 	retval = 0;
 out:
-	kvfree(resp);
+	kvfree_sensitive(resp, sizeof(*resp) + resp->session_key_len +
+				resp->spnego_blob_len);
 	return retval;
 }
 #else
@@ -556,6 +559,7 @@ static void generate_key(struct ksmbd_conn *conn, const char *sess_key,
 
 	hmac_sha256_final(&ctx, prfhash);
 	memcpy(key, prfhash, key_size);
+	memzero_explicit(prfhash, sizeof(prfhash));
 }
 
 static int generate_smb3signingkey(struct ksmbd_session *sess,
@@ -848,7 +852,8 @@ int ksmbd_crypt_message(struct ksmbd_work *work, struct kvec *iov,
 		ctx = ksmbd_crypto_ctx_find_ccm();
 	if (!ctx) {
 		pr_err("crypto alloc failed\n");
-		return -ENOMEM;
+		rc = -ENOMEM;
+		goto zeroize_key;
 	}
 
 	if (conn->cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
@@ -928,5 +933,8 @@ int ksmbd_crypt_message(struct ksmbd_work *work, struct kvec *iov,
 	aead_request_free(req);
 free_ctx:
 	ksmbd_release_crypto_ctx(ctx);
+zeroize_key:
+	memzero_explicit(key, sizeof(key));
+	memzero_explicit(sign, sizeof(sign));
 	return rc;
 }
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/5 smb: server: Make sure that passkey is not leaked on the heap in user_config.c

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

Use kfree_sensitive() to free the user->passkey (and the struct ksmbd_login_response in ksmbd_login_user() that contains the same information) to avoid that this information could leak somewhere else via the heap.

---
 fs/smb/server/mgmt/user_config.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/smb/server/mgmt/user_config.c +3 -3
index cf45841d9d1b9..76b74d68369d8 100644
@@ -27,7 +27,7 @@ struct ksmbd_user *ksmbd_login_user(const char *account)
 
 	user = ksmbd_alloc_user(resp, resp_ext);
 out:
-	kvfree(resp);
+	kvfree_sensitive(resp, sizeof(*resp));
 	return user;
 }
 
@@ -70,7 +70,7 @@ struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp,
 
 err_free:
 	kfree(user->name);
-	kfree(user->passkey);
+	kfree_sensitive(user->passkey);
 	kfree(user);
 	return NULL;
 }
@@ -80,7 +80,7 @@ void ksmbd_free_user(struct ksmbd_user *user)
 	ksmbd_ipc_logout_request(user->name, user->flags);
 	kfree(user->sgid);
 	kfree(user->name);
-	kfree(user->passkey);
+	kfree_sensitive(user->passkey);
 	kfree(user);
 }
 
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 3/5 smb: server: Free session data in user_session.c with kfree_sensitive()

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

struct ksmbd_session contains some arrays with sensitive information, like sess_key, smb3encryptionkey, smb3decryptionkey and smb3signingkey. Thus let's make sure that this information cannot leak via the heap and use kfree_sensitive() to free it.

---
 fs/smb/server/mgmt/user_session.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/smb/server/mgmt/user_session.c +2 -2
index f99c86284ba3d..9cb990a22fd56 100644
@@ -389,10 +389,10 @@ void ksmbd_session_destroy(struct ksmbd_session *sess)
 	ksmbd_launch_ksmbd_durable_scavenger();
 	ksmbd_session_rpc_clear_list(sess);
 	free_channel_list(sess);
-	kfree(sess->Preauth_HashValue);
+	kfree_sensitive(sess->Preauth_HashValue);
 	ksmbd_release_id(&session_ida, sess->id);
 	ida_destroy(&sess->tree_conn_ida);
-	kfree(sess);
+	kfree_sensitive(sess);
 }
 
 struct ksmbd_session *__session_lookup(unsigned long long id)
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 4/5 smb: server: Free sensitive data in connection.c with kfree_sensitive()

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

struct ksmbd_conn contains an embedded struct ntlmssp_auth with the ciphertext[] and cryptkey[] arrays, so to avoid leaking this information via the heap, it should be freed with kfree_sensitive().

While we're at it, also use kfree_sensitive() for freeing preauth_info in ksmbd_conn_free() to avoid that the Preauth_HashValue[] could leak via the heap here, too.

---
 fs/smb/server/connection.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/smb/server/connection.c +3 -3
index ef6f202f4024c..68b9e698b1cb6 100644
@@ -117,7 +117,7 @@ static void __ksmbd_conn_release_work(struct work_struct *work)
 
 	ida_destroy(&conn->async_ida);
 	conn->transport->ops->free_transport(conn->transport);
-	kfree(conn);
+	kfree_sensitive(conn);
 }
 
 /**
@@ -183,7 +183,7 @@ void ksmbd_conn_free(struct ksmbd_conn *conn)
 	 */
 	xa_destroy(&conn->sessions);
 	kvfree(conn->request_buf);
-	kfree(conn->preauth_info);
+	kfree_sensitive(conn->preauth_info);
 	kfree(conn->mechToken);
 	ksmbd_conn_put(conn);
 }
@@ -671,7 +671,7 @@ static void stop_sessions(void)
 		if (atomic_dec_and_test(&target->refcnt)) {
 			ida_destroy(&target->async_ida);
 			t->ops->free_transport(t);
-			kfree(target);
+			kfree_sensitive(target);
 		}
 		goto again;
 	}
Signature · 1 line
2.55.0
#5 lore raw

PATCH v2 5/5 smb: server: Clear Preauth_HashValue in smb2pdu.c with kfree_sensitive()

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

struct preauth_session contains the Preauth_HashValue[] array that might contain sensitive data. Use kfree_sensitive() to clear it before returning the memory to the heap.

---
 fs/smb/server/smb2pdu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +2 -2
index 184501e08b29a..fc44ac6b56427 100644
@@ -2136,7 +2136,7 @@ int smb2_sess_setup(struct ksmbd_work *work)
 						ksmbd_preauth_session_lookup(conn, sess->id);
 					if (preauth_sess) {
 						list_del(&preauth_sess->preauth_entry);
-						kfree(preauth_sess);
+						kfree_sensitive(preauth_sess);
 					}
 				}
 			} else {
@@ -2190,7 +2190,7 @@ int smb2_sess_setup(struct ksmbd_work *work)
 			preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
 			if (preauth_sess) {
 				list_del(&preauth_sess->preauth_entry);
-				kfree(preauth_sess);
+				kfree_sensitive(preauth_sess);
 			}
 		}
 
Signature · 1 line
2.55.0
maintainer Applied
#6 lore raw
In reply to Thomas Huth
Quoted 14 lines · Thomas Huth

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.

Disclaimer: The spots that need clearing have been identified with AI, but the patches have been created manually (for double-checking whether the findings really make sense). Anyway, I'm not very familiar with the smb code, so please review carefully. Thanks!

Quoted 4 lines

v2:
- Calculate proper size for the kvfree_sensitive() in the first patch
- Add "kfree_sensitive(target)" hunk to the connection.c patch
- Add patch to clear Preauth_HashValue in smb2pdu.c

Applied them to #ksmbd-for-next-next.
Thanks!