staticint check_smb2_hdr(struct smb2_hdr *hdr)
{ /* * Make sure that this really is an SMB, that it is a response.
*/ if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) return 1; return 0;
}
/* * The following table defines the expected "StructureSize" of SMB2 requests * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests. * * Note that commands are defined in smb2pdu.h in le16 but the array below is * indexed by command in host byte order
*/ staticconst __le16 smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { /* SMB2_NEGOTIATE */ cpu_to_le16(36), /* SMB2_SESSION_SETUP */ cpu_to_le16(25), /* SMB2_LOGOFF */ cpu_to_le16(4), /* SMB2_TREE_CONNECT */ cpu_to_le16(9), /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4), /* SMB2_CREATE */ cpu_to_le16(57), /* SMB2_CLOSE */ cpu_to_le16(24), /* SMB2_FLUSH */ cpu_to_le16(24), /* SMB2_READ */ cpu_to_le16(49), /* SMB2_WRITE */ cpu_to_le16(49), /* SMB2_LOCK */ cpu_to_le16(48), /* SMB2_IOCTL */ cpu_to_le16(57), /* SMB2_CANCEL */ cpu_to_le16(4), /* SMB2_ECHO */ cpu_to_le16(4), /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(33), /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(32), /* SMB2_QUERY_INFO */ cpu_to_le16(41), /* SMB2_SET_INFO */ cpu_to_le16(33), /* use 44 for lease break */ /* SMB2_OPLOCK_BREAK */ cpu_to_le16(36)
};
/* * The size of the variable area depends on the offset and length fields * located in different fields for various SMB2 requests. SMB2 requests * with no variable length info, show an offset of zero for the offset field.
*/ staticconstbool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = { /* SMB2_NEGOTIATE */ true, /* SMB2_SESSION_SETUP */ true, /* SMB2_LOGOFF */ false, /* SMB2_TREE_CONNECT */ true, /* SMB2_TREE_DISCONNECT */ false, /* SMB2_CREATE */ true, /* SMB2_CLOSE */ false, /* SMB2_FLUSH */ false, /* SMB2_READ */ true, /* SMB2_WRITE */ true, /* SMB2_LOCK */ true, /* SMB2_IOCTL */ true, /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */ /* SMB2_ECHO */ false, /* SMB2_QUERY_DIRECTORY */ true, /* SMB2_CHANGE_NOTIFY */ false, /* SMB2_QUERY_INFO */ true, /* SMB2_SET_INFO */ true, /* SMB2_OPLOCK_BREAK */ false
};
/* * Set length of the data area and the offset to arguments. * if they are invalid, return error.
*/ staticint smb2_get_data_area_len(unsignedint *off, unsignedint *len, struct smb2_hdr *hdr)
{ int ret = 0;
*off = 0;
*len = 0;
/* * Following commands have data areas so we have to get the location * of the data buffer offset and data buffer length for the particular * command.
*/ switch (hdr->Command) { case SMB2_SESSION_SETUP:
*off = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset);
*len = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength); break; case SMB2_TREE_CONNECT:
*off = max_t(unsignedshortint,
le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathOffset),
offsetof(struct smb2_tree_connect_req, Buffer));
*len = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathLength); break; case SMB2_CREATE:
{ unsignedshortint name_off =
max_t(unsignedshortint,
le16_to_cpu(((struct smb2_create_req *)hdr)->NameOffset),
offsetof(struct smb2_create_req, Buffer)); unsignedshortint name_len =
le16_to_cpu(((struct smb2_create_req *)hdr)->NameLength);
if (((struct smb2_create_req *)hdr)->CreateContextsLength) {
*off = le32_to_cpu(((struct smb2_create_req *)
hdr)->CreateContextsOffset);
*len = le32_to_cpu(((struct smb2_create_req *)
hdr)->CreateContextsLength); if (!name_len) break;
if (name_off + name_len < (u64)*off + *len) break;
}
if (*off > 4096) {
ksmbd_debug(SMB, "offset %d too large\n", *off);
ret = -EINVAL;
} elseif ((u64)*off + *len > MAX_STREAM_PROT_LEN) {
ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n",
MAX_STREAM_PROT_LEN, (u64)*off + *len);
ret = -EINVAL;
}
return ret;
}
/* * Calculate the size of the SMB message based on the fixed header * portion, the number of word parameters and the data portion of the message.
*/ staticint smb2_calc_size(void *buf, unsignedint *len)
{ struct smb2_pdu *pdu = (struct smb2_pdu *)buf; struct smb2_hdr *hdr = &pdu->hdr; unsignedint offset; /* the offset from the beginning of SMB to data area */ unsignedint data_length; /* the length of the variable length data area */ int ret;
/* Structure Size has already been checked to make sure it is 64 */
*len = le16_to_cpu(hdr->StructureSize);
/* * StructureSize2, ie length of fixed parameter area has already * been checked to make sure it is the correct length.
*/
*len += le16_to_cpu(pdu->StructureSize2); /* * StructureSize2 of smb2_lock pdu is set to 48, indicating * the size of smb2 lock request with single smb2_lock_element * regardless of number of locks. Subtract single * smb2_lock_element for correct buffer size check.
*/ if (hdr->Command == SMB2_LOCK)
*len -= sizeof(struct smb2_lock_element);
if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false) goto calc_size_exit;
ret = smb2_get_data_area_len(&offset, &data_length, hdr); if (ret) return ret;
ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length,
offset);
if (data_length > 0) { /* * Check to make sure that data area begins after fixed area, * Note that last byte of the fixed area is part of data area * for some commands, typically those with odd StructureSize, * so we must add one to the calculation.
*/ if (offset + 1 < *len) {
ksmbd_debug(SMB, "data area offset %d overlaps SMB2 header %u\n",
offset + 1, *len); return -EINVAL;
}
*len = offset + data_length;
}
calc_size_exit:
ksmbd_debug(SMB, "SMB2 len %u\n", *len); return 0;
}
if (len != clc_len) { /* client can return one byte more due to implied bcc[0] */ if (clc_len == len + 1) goto validate_credit;
/* * Some windows servers (win2016) will pad also the final * PDU in a compound to 8 bytes.
*/ if (ALIGN(clc_len, 8) == len) goto validate_credit;
/* * SMB2 NEGOTIATE request will be validated when message * handling proceeds.
*/ if (command == SMB2_NEGOTIATE_HE) goto validate_credit;
/* * Allow a message that padded to 8byte boundary. * Linux 4.19.217 with smb 3.0.2 are sometimes * sending messages where the cls_len is exactly * 8 bytes less than len.
*/ if (clc_len < len && (len - clc_len) <= 8) goto validate_credit;
pr_err_ratelimited( "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
len, clc_len, command,
le64_to_cpu(hdr->MessageId));
return 1;
}
validate_credit: if ((work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) &&
smb2_validate_credit_charge(work->conn, hdr)) return 1;
return 0;
}
int smb2_negotiate_request(struct ksmbd_work *work)
{ return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.