From: bob init_chained_smb2_rsp() calls smb2_get_file_ids() on the request that was just processed to find the FileId to inherit into the next request of a related compound. The function reads the CtlCode of an IOCTL, the StructureSize of an oplock break and the 16-byte FileId at a per-command offset up to 104 bytes from the start of the SMB2 header, without checking that this many bytes are part of the message. For a request that went through ksmbd_verify_smb_message() that is implied by the size validation. But __process_request() returns SERVER_HANDLER_CONTINUE when check_conn_state() fires, before the validation, and the compound is then still chained by is_chained_smb2_message(), which only checks that the next 64-byte header fits. An element that is, say, a QUERY_INFO with a small NextCommand is then only guaranteed to contain its 64-byte header, and the FileId is read up to 38 bytes past the end of the request buffer (a heap or vmalloc object of the PDU size plus one byte). The value is only stored in work->compound_fid and never returned to the client, so this is an out-of-bounds read, not an information leak, and it needs the connection state to change while the compound is being processed. Pass the number of bytes left in the request message to smb2_get_file_ids() and return false when the fields it needs are not inside it. The two callers pass the length from the start of the current compound element to the end of the message. Requests that passed validation are unaffected, since their fixed size covers the FileId. This issue was found by manual code review with the help of an AI coding assistant (Claude Opus 5.5), which also drafted this patch. It has been compile-tested with W=1 and smatch on x86_64 allmodconfig (no new warnings; smatch reports the same two pre-existing warnings in this file before and after). It has not been runtime-tested (no KASAN run) and no reproducer was built: the connection state has to flip inside a window of a few microseconds, which was not attempted. Fixes: 318cd7461a7f ("ksmbd: inherit FileIds in related compound requests") Fixes: 3f67e624e591 ("ksmbd: propagate failed command status in related compounds") Cc: stable@vger.kernel.org Signed-off-by: bob --- ksmbd: bound the FileId reads of an unvalidated compound request --- fs/smb/server/smb2pdu.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c index a5601d170d23..c04435f53485 100644 --- a/fs/smb/server/smb2pdu.c +++ b/fs/smb/server/smb2pdu.c @@ -455,8 +455,27 @@ int smb2_set_rsp_credits(struct ksmbd_work *work) return 0; } -static bool smb2_get_file_ids(struct smb2_hdr *hdr, u64 *volatile_id, - u64 *persistent_id) +/* + * Return the number of bytes of the request message that follow, and include, + * the SMB2 header of the current (possibly compounded) request. + */ +static unsigned int smb2_req_buf_next_len(struct ksmbd_work *work) +{ + unsigned int len = get_rfc1002_len(work->request_buf); + + if (work->next_smb2_rcv_hdr_off >= len) + return 0; + return len - work->next_smb2_rcv_hdr_off; +} + +/* + * @len is the number of valid bytes starting at @hdr. A compounded request + * that was not validated (check_conn_state() skips the validation) is only + * known to contain a full SMB2 header, so do not read the FileId, or the + * fields that decide where it is, beyond @len. + */ +static bool smb2_get_file_ids(struct smb2_hdr *hdr, unsigned int len, + u64 *volatile_id, u64 *persistent_id) { unsigned int offset; @@ -479,6 +498,10 @@ static bool smb2_get_file_ids(struct smb2_hdr *hdr, u64 *volatile_id, case SMB2_IOCTL: { struct smb2_ioctl_req *req = (struct smb2_ioctl_req *)hdr; + if (len < offsetof(struct smb2_ioctl_req, CtlCode) + + sizeof(req->CtlCode)) + return false; + switch (le32_to_cpu(req->CtlCode)) { case FSCTL_DFS_GET_REFERRALS: case FSCTL_DFS_GET_REFERRALS_EX: @@ -503,6 +526,8 @@ static bool smb2_get_file_ids(struct smb2_hdr *hdr, u64 *volatile_id, offset = offsetof(struct smb2_set_info_req, PersistentFileId); break; case SMB2_OPLOCK_BREAK: + if (len < offsetof(struct smb2_oplock_break, OplockLevel)) + return false; if (((struct smb2_oplock_break *)hdr)->StructureSize != cpu_to_le16(OP_BREAK_STRUCT_SIZE_20)) return false; @@ -512,6 +537,9 @@ static bool smb2_get_file_ids(struct smb2_hdr *hdr, u64 *volatile_id, return false; } + if (len < offset + sizeof(*persistent_id) + sizeof(*volatile_id)) + return false; + /* FileId contains consecutive persistent and volatile opaque values. */ memcpy(persistent_id, (char *)hdr + offset, sizeof(*persistent_id)); memcpy(volatile_id, (char *)hdr + offset + sizeof(*persistent_id), @@ -532,7 +560,8 @@ bool smb2_check_compound_fid(struct ksmbd_work *work) if (!work->compound_fid_missing && !work->compound_fid_present && - smb2_get_file_ids(req, &volatile_id, &persistent_id)) + smb2_get_file_ids(req, smb2_req_buf_next_len(work), + &volatile_id, &persistent_id)) work->compound_fid_missing = true; if (!work->compound_fid_missing) @@ -559,7 +588,8 @@ static void init_chained_smb2_rsp(struct ksmbd_work *work) u64 volatile_id = KSMBD_NO_FID, persistent_id = KSMBD_NO_FID; bool req_has_fid; - req_has_fid = smb2_get_file_ids(req, &volatile_id, &persistent_id); + req_has_fid = smb2_get_file_ids(req, smb2_req_buf_next_len(work), + &volatile_id, &persistent_id); /* A failed CREATE is handled through compound_status instead. */ work->compound_fid_present = req_has_fid || req->Command == SMB2_CREATE; if (!work->compound_fid_present) { --- base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 change-id: 20261203-fix-k2-ksmbd-get-file-ids-bound-9f5c631eb866 Best regards, -- bob