ksmbd: some potential bugfixes
Subsystems (2)
PATCH 1/3 ksmbd: validate EaNameLength in smb2_get_ea()
In reply to Greg Kroah-Hartmansmb2_get_ea() reads ea_req->EaNameLength from the client request and passes it directly to strncmp() as the comparison length without verifying that the length of the name really is the size of the input buffer recieved.
Fix this up by properly checking the size of the name based on the value recieved and the overall size of the request, to prevent a later strncmp() call to use the length as a "trusted" size of the buffer. Without this check, uninitialized heap values might be slowly leaked to the client.
--- fs/smb/server/smb2pdu.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/smb/server/smb2pdu.c +5 -0
@@ -4716,6 +4716,11 @@ static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp, ea_req = (struct smb2_ea_info_req *)((char *)req +le16_to_cpu (req->InputBufferOffset)); + +if (le32_to_cpu (req->InputBufferLength) < + offsetof(struct smb2_ea_info_req , name) + + ea_req->EaNameLength) +return -EINVAL ; }else {/* need to send all EAs, if no specific EA is requested*/ if (le32_to_cpu (req->Flags) &SL_RETURN_SINGLE_ENTRY )
Signature · 1 line
2.53.0
cover ksmbd: some potential bugfixes
I spent the time exercising some new fuzzing tools on the ksmbd and smb code purely because it's something that is simple to set up and test locally with virtual machines, and in doing so, potentially found some minor problems for when you have an "untrusted" client.
Here's some fixes for what I happened to notice. They pass my very limited testing here, but please don't trust them at all and verify that I'm not just making this all up before accepting them.
thanks!
greg k-h
Greg Kroah-Hartman (3):
ksmbd: validate EaNameLength in smb2_get_ea()
ksmbd: require 3 sub-authorities before reading sub_auth[2]
ksmbd: fix mechToken leak when SPNEGO decode fails after token alloc
fs/smb/server/connection.c | 1 + fs/smb/server/smb2pdu.c | 7 ++++++- fs/smb/server/smbacl.c | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0
PATCH 2/3 ksmbd: require 3 sub-authorities before reading sub_auth[2]
parse_dacl() compares each ACE SID against sid_unix_NFS_mode and on match reads sid.sub_auth[2] as the file mode. If sid_unix_NFS_mode is the prefix S-1-5-88-3 with num_subauth = 2 then compare_sids() compares only min(num_subauth, 2) sub-authorities so a client SID with num_subauth = 2 and sub_auth = {88, 3} will match.
If num_subauth = 2 and the ACE is placed at the very end of the security descriptor, sub_auth[2] will be 4 bytes past end_of_acl. The out-of-band bytes will then be masked to the low 9 bits and applied as the file's POSIX mode, probably not something that is good to have happen.
Fix this up by forcing the SID to actually carry a third sub-authority before reading it at all.
--- fs/smb/server/smbacl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/server/smbacl.c +2 -1
@@ -451,7 +451,8 @@ static void parse_dacl(struct mnt_idmap *idmap, ppace[i]->access_req =smb_map_generic_desired_access (ppace[i]->access_req); -if (!(compare_sids (&ppace[i]->sid, &sid_unix_NFS_mode))) { +if (ppace[i]->sid.num_subauth >=3 && + !(compare_sids (&ppace[i]->sid, &sid_unix_NFS_mode))) { fattr->cf_mode =le32_to_cpu (ppace[i]->sid.sub_auth[2 ]);break ;
Signature · 1 line
2.53.0
PATCH 3/3 ksmbd: fix mechToken leak when SPNEGO decode fails after token alloc
In reply to Greg Kroah-HartmanThe kernel ASN.1 BER decoder calls action callbacks incrementally as it walks the input. When ksmbd_decode_negTokenInit() reaches the mechToken
[2] OCTET STRING element, ksmbd_neg_token_alloc() allocates conn->mechToken immediately via kmemdup_nul(). If a later element in the same blob is malformed, then the decoder will return nonzero after the allocation is already live. This could happen if mechListMIC [3] overrunse the enclosing SEQUENCE.
decode_negotiation_token() then sets conn->use_spnego = false because both the negTokenInit and negTokenTarg grammars failed. The cleanup at the bottom of smb2_sess_setup() is gated on use_spnego:
if (conn->use_spnego && conn->mechToken) {
kfree(conn->mechToken);
conn->mechToken = NULL;
}so the kfree is skipped, causing the mechToken to never be freed.
This codepath is reachable pre-authentication, so untrusted clients can cause slow memory leaks on a server without even being properly authenticated.
Fix this up by not checking check for use_spnego, as it's not required, so the memory will always be properly freed. At the same time, always free the memory in ksmbd_conn_free() incase some other failure path forgot to free it.
--- fs/smb/server/connection.c | 1 + fs/smb/server/smb2pdu.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
fs/smb/server/connection.c +1 -0
@@ -96,6 +96,7 @@ void ksmbd_conn_free(struct ksmbd_conn *conn)xa_destroy (&conn->sessions);kvfree (conn->request_buf);kfree (conn->preauth_info); +kfree (conn->mechToken);if (atomic_dec_and_test (&conn->refcnt)) { conn->transport->ops->free_transport (conn->transport);kfree (conn);
fs/smb/server/smb2pdu.c +1 -1
@@ -1915,7 +1915,7 @@ int smb2_sess_setup(struct ksmbd_work *work)else if (rc) rsp->hdr.Status =STATUS_LOGON_FAILURE ; -if (conn->use_spnego && conn->mechToken) { +if (conn->mechToken) {kfree (conn->mechToken); conn->mechToken =NULL ; }
Signature · 1 line
2.53.0
Quoted 10 lines · Greg Kroah-Hartman
I spent the time exercising some new fuzzing tools on the ksmbd and smb code purely because it's something that is simple to set up and test locally with virtual machines, and in doing so, potentially found some minor problems for when you have an "untrusted" client.
Here's some fixes for what I happened to notice. They pass my very limited testing here, but please don't trust them at all and verify that I'm not just making this all up before accepting them.
Quoted 8 lines
thanks!
greg k-h
Greg Kroah-Hartman (3):
ksmbd: validate EaNameLength insmb2_get_ea()
ksmbd: require 3 sub-authorities before reading sub_auth[2]
ksmbd: fix mechToken leak when SPNEGO decode fails after token alloc
Applied them to #ksmbd-for-next-next.
Thanks for the patches!
Quoted 3 lines · Steve French
Potentially some useful comments from Sashiko's AI review of this series:
https://sashiko.dev/#/patchset/2026040644-brussels-dab-6f99%40gregkh
Looks like it is pointing out other issues to potentially work on in the future, but the current series seems to not have any review problems?
thanks,
greg k-h