ksmbd: bound the NTLMv2 key length by the login response hash size
Subsystems (2)
PATCH 1/1 ksmbd: bound the NTLMv2 key length by the login response hash size
ksmbd_alloc_user() takes passkey_sz from the daemon's login response, but calc_ntlmv2_hash() always feeds CIFS_ENCPWD_SIZE bytes to hmac_md5_init_usingrawkey(). A response with hash_sz between 1 and 15 therefore reads past the passkey allocation, and hash_sz 0 allocates ZERO_SIZE_PTR, so the copy reads from 0x10:
BUG: KASAN: slab-out-of-bounds in __hmac_md5_preparekey Read of size 16 at addr ffff888007cdf8c0 Workqueue: ksmbd-io handle_ksmbd_work __hmac_md5_preparekey hmac_md5_init_usingrawkey calc_ntlmv2_hash ksmbd_auth_ntlmv2 smb2_sess_setup
The daemon sends an empty hash for guest accounts, so hash_sz 0 has to keep working. Bound the key length where the fixed-size NTLM hash is assumed instead of rejecting the short hash in ksmbd_alloc_user(); a truncated key now yields a different HMAC and simply fails the authentication.
--- fs/smb/server/auth.c | 4 +++- fs/smb/server/mgmt/user_config.h | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-)
fs/smb/server/auth.c +3 -1
@@ -79,8 +79,10 @@ static int calc_ntlmv2_hash(struct ksmbd_conn *conn, struct ksmbd_session *sess,__le16 *uniname =NULL ;struct hmac_md5_ctx ctx; +/* the key comes from the daemon and may be shorter than the NTLM hash */ hmac_md5_init_usingrawkey (&ctx,user_passkey (sess->user), -CIFS_ENCPWD_SIZE ); +min_t (size_t,CIFS_ENCPWD_SIZE , +user_passkey_sz (sess->user)));/* convert user_name to unicode */ len =strlen (user_name (sess->user));
fs/smb/server/mgmt/user_config.h +5 -0
@@ -46,6 +46,11 @@ static inline char *user_passkey(struct ksmbd_user *user)return user->passkey; } +static inline size_t user_passkey_sz (struct ksmbd_user *user) +{ +return user->passkey_sz; +} +static inline char *user_name (struct ksmbd_user *user) {return user->name;
Signature · 1 line
2.56.1
fs/smb/server/auth.c static int calc_ntlmv2_hash(struct ksmbd_conn *conn, struct ksmbd_session *sess, · line 85
Quoted 4 lines
hmac_md5_init_usingrawkey(&ctx, user_passkey(sess->user),fs/smb/server/auth.c +2
-1- CIFS_ENCPWD_SIZE); +min_t (size_t,CIFS_ENCPWD_SIZE , + user_passkey_sz(sess->user)));
With a hash shorter than 16 bytes, the login does not always fail as the commit message says. HMAC-MD5 pads a short key with zeros, so a key of n bytes acts like a 16-byte NT hash made of those bytes and then zeros. A hash of 0 bytes is then the all-zero NT hash, which any client can use.
ksmbd-tools can send such a hash for a normal user. Its password database accepts an entry whose password field decodes to no bytes, such as "alice:A". I tested this on ksmbd-for-next 4543a3349127 with KASAN. With the patch, an NTLMv2 login with the all-zero NT hash succeeded for that user, and it was not a guest session. It also succeeded for a user whose field decodes to 3 zero bytes. Logins with any other hash failed.
Guest accounts never reach calc_ntlmv2_hash(). Could it fail with -EINVAL when the key is shorter than CIFS_ENCPWD_SIZE?
PATCH v2 1/1 ksmbd: reject a short NTLMv2 key instead of truncating it
ksmbd: reject a short NTLMv2 key instead of truncating it
calc_ntlmv2_hash() feeds CIFS_ENCPWD_SIZE bytes to hmac_md5_init_usingrawkey() whatever length the daemon sent in LOGIN_RESPONSE.hash_sz. A hash shorter than 16 bytes makes the ksmbd-io worker read past the passkey allocation, and hash_sz 0 allocates ZERO_SIZE_PTR, so the copy reads from 0x10:
BUG: KASAN: slab-out-of-bounds in __hmac_md5_preparekey Read of size 16 at addr ffff888007cdf8c0 Workqueue: ksmbd-io handle_ksmbd_work __hmac_md5_preparekey hmac_md5_init_usingrawkey calc_ntlmv2_hash ksmbd_auth_ntlmv2 smb2_sess_setup
Padding the key down to hash_sz is not a fix: HMAC-MD5 zero pads a key, so an empty key is the same key as the all-zero 16 byte NT hash, and any client can log in with it. Ian Bridges reproduced that with a ksmbd-tools entry whose password field decodes to no bytes ("alice:A") on ksmbd-for-next 4543a3349127: an NTLMv2 login using the all-zero NT hash succeeded for a normal, non-guest user.
Guest accounts never reach calc_ntlmv2_hash(), so there is nothing to preserve for them: refuse the short key and let the login fail.
---
v2: reject the short key instead of truncating it, after Ian's review.
Truncating the key to hash_sz does not make the login fail: HMAC-MD5 zero pads its key, so an empty key is the same key as the all-zero 16 byte NT hash and any client can log in with it. Guest sessions never reach calc_ntlmv2_hash(), so there is nothing to preserve for them.
Thanks to Ian Bridges for the report and for the ksmbd-tools reproducer.
fs/smb/server/auth.c | 4 ++++ fs/smb/server/mgmt/user_config.h | 5 +++++ 2 files changed, 9 insertions(+)
fs/smb/server/auth.c +4 -0
@@ -79,6 +79,10 @@ static int calc_ntlmv2_hash(struct ksmbd_conn *conn, struct ksmbd_session *sess,__le16 *uniname =NULL ;struct hmac_md5_ctx ctx; +/* a short key would be zero padded by HMAC into a guessable one */ +if (user_passkey_sz (sess->user) <CIFS_ENCPWD_SIZE ) +return -EINVAL ; +hmac_md5_init_usingrawkey (&ctx,user_passkey (sess->user),CIFS_ENCPWD_SIZE );
fs/smb/server/mgmt/user_config.h +5 -0
@@ -46,6 +46,11 @@ static inline char *user_passkey(struct ksmbd_user *user)return user->passkey; } +static inline size_t user_passkey_sz (struct ksmbd_user *user) +{ +return user->passkey_sz; +} +static inline char *user_name (struct ksmbd_user *user) {return user->name;
Signature · 1 line
2.56.1
Re: [PATCH v2] ksmbd: reject a short NTLMv2 key instead of truncating it
Quoted 4 lines
Ian Bridges reproduced that with a ksmbd-tools
entry whose password field decodes to no bytes ("alice:A") on ksmbd-for-next4543a3349127: an NTLMv2 login using the all-zero NT hash succeeded for a normal, non-guest user.
That login succeeded with v1 applied on 4543a3349127, not on 4543a3349127 alone.