Re: [PATCH] smb: server: Clear sensitive stack and heap data in auth.c
Thomas Huth <[email protected]>
| Newsgroups | org.kernel.vger.linux-cifs,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
On 07/08/2026 19.10, Thomas Huth wrote: > 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 in > this code 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. > > 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. > > Signed-off-by: Thomas Huth <[email protected]> > --- > fs/smb/server/auth.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) > > diff --git a/fs/smb/server/auth.c b/fs/smb/server/auth.c > index 4e7b6f0e6b8cd..acdac40bee813 100644 > --- a/fs/smb/server/auth.c > +++ b/fs/smb/server/auth.c > @@ -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,7 @@ 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); D'oh, self-nack, I really should not send patches in a hurry on Friday afternoon, sorry! I will send a fixed version next week. Thomas