1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
9 #include "smb_common.h"
10 #include "smbstatus.h"
11 #include "mgmt/user_session.h"
12 #include "connection.h"
14 static int check_smb2_hdr(struct smb2_hdr *hdr)
17 * Make sure that this really is an SMB, that it is a response.
19 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
25 * The following table defines the expected "StructureSize" of SMB2 requests
26 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
28 * Note that commands are defined in smb2pdu.h in le16 but the array below is
29 * indexed by command in host byte order
31 static const __le16 smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
32 /* SMB2_NEGOTIATE */ cpu_to_le16(36),
33 /* SMB2_SESSION_SETUP */ cpu_to_le16(25),
34 /* SMB2_LOGOFF */ cpu_to_le16(4),
35 /* SMB2_TREE_CONNECT */ cpu_to_le16(9),
36 /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4),
37 /* SMB2_CREATE */ cpu_to_le16(57),
38 /* SMB2_CLOSE */ cpu_to_le16(24),
39 /* SMB2_FLUSH */ cpu_to_le16(24),
40 /* SMB2_READ */ cpu_to_le16(49),
41 /* SMB2_WRITE */ cpu_to_le16(49),
42 /* SMB2_LOCK */ cpu_to_le16(48),
43 /* SMB2_IOCTL */ cpu_to_le16(57),
44 /* SMB2_CANCEL */ cpu_to_le16(4),
45 /* SMB2_ECHO */ cpu_to_le16(4),
46 /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(33),
47 /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(32),
48 /* SMB2_QUERY_INFO */ cpu_to_le16(41),
49 /* SMB2_SET_INFO */ cpu_to_le16(33),
50 /* use 44 for lease break */
51 /* SMB2_OPLOCK_BREAK */ cpu_to_le16(36)
55 * The size of the variable area depends on the offset and length fields
56 * located in different fields for various SMB2 requests. SMB2 requests
57 * with no variable length info, show an offset of zero for the offset field.
59 static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ true,
61 /* SMB2_SESSION_SETUP */ true,
62 /* SMB2_LOGOFF */ false,
63 /* SMB2_TREE_CONNECT */ true,
64 /* SMB2_TREE_DISCONNECT */ false,
65 /* SMB2_CREATE */ true,
66 /* SMB2_CLOSE */ false,
67 /* SMB2_FLUSH */ false,
69 /* SMB2_WRITE */ true,
71 /* SMB2_IOCTL */ true,
72 /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */
73 /* SMB2_ECHO */ false,
74 /* SMB2_QUERY_DIRECTORY */ true,
75 /* SMB2_CHANGE_NOTIFY */ false,
76 /* SMB2_QUERY_INFO */ true,
77 /* SMB2_SET_INFO */ true,
78 /* SMB2_OPLOCK_BREAK */ false
82 * Set length of the data area and the offset to arguments.
83 * if they are invalid, return error.
85 static int smb2_get_data_area_len(unsigned int *off, unsigned int *len,
94 * Following commands have data areas so we have to get the location
95 * of the data buffer offset and data buffer length for the particular
98 switch (hdr->Command) {
99 case SMB2_SESSION_SETUP:
100 *off = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset);
101 *len = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength);
103 case SMB2_TREE_CONNECT:
104 *off = max_t(unsigned short int,
105 le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathOffset),
106 offsetof(struct smb2_tree_connect_req, Buffer));
107 *len = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathLength);
111 unsigned short int name_off =
112 max_t(unsigned short int,
113 le16_to_cpu(((struct smb2_create_req *)hdr)->NameOffset),
114 offsetof(struct smb2_create_req, Buffer));
115 unsigned short int name_len =
116 le16_to_cpu(((struct smb2_create_req *)hdr)->NameLength);
118 if (((struct smb2_create_req *)hdr)->CreateContextsLength) {
119 *off = le32_to_cpu(((struct smb2_create_req *)
120 hdr)->CreateContextsOffset);
121 *len = le32_to_cpu(((struct smb2_create_req *)
122 hdr)->CreateContextsLength);
126 if (name_off + name_len < (u64)*off + *len)
134 case SMB2_QUERY_INFO:
135 *off = max_t(unsigned int,
136 le16_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferOffset),
137 offsetof(struct smb2_query_info_req, Buffer));
138 *len = le32_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferLength);
141 *off = max_t(unsigned int,
142 le16_to_cpu(((struct smb2_set_info_req *)hdr)->BufferOffset),
143 offsetof(struct smb2_set_info_req, Buffer));
144 *len = le32_to_cpu(((struct smb2_set_info_req *)hdr)->BufferLength);
147 *off = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoOffset);
148 *len = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoLength);
151 if (((struct smb2_write_req *)hdr)->DataOffset ||
152 ((struct smb2_write_req *)hdr)->Length) {
153 *off = max_t(unsigned short int,
154 le16_to_cpu(((struct smb2_write_req *)hdr)->DataOffset),
155 offsetof(struct smb2_write_req, Buffer));
156 *len = le32_to_cpu(((struct smb2_write_req *)hdr)->Length);
160 *off = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoOffset);
161 *len = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoLength);
163 case SMB2_QUERY_DIRECTORY:
164 *off = max_t(unsigned short int,
165 le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameOffset),
166 offsetof(struct smb2_query_directory_req, Buffer));
167 *len = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameLength);
171 unsigned short lock_count;
173 lock_count = le16_to_cpu(((struct smb2_lock_req *)hdr)->LockCount);
174 if (lock_count > 0) {
175 *off = offsetof(struct smb2_lock_req, locks);
176 *len = sizeof(struct smb2_lock_element) * lock_count;
181 *off = max_t(unsigned int,
182 le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputOffset),
183 offsetof(struct smb2_ioctl_req, Buffer));
184 *len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount);
187 ksmbd_debug(SMB, "no length check for command\n");
192 ksmbd_debug(SMB, "offset %d too large\n", *off);
194 } else if ((u64)*off + *len > MAX_STREAM_PROT_LEN) {
195 ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n",
196 MAX_STREAM_PROT_LEN, (u64)*off + *len);
204 * Calculate the size of the SMB message based on the fixed header
205 * portion, the number of word parameters and the data portion of the message.
207 static int smb2_calc_size(void *buf, unsigned int *len)
209 struct smb2_pdu *pdu = (struct smb2_pdu *)buf;
210 struct smb2_hdr *hdr = &pdu->hdr;
211 unsigned int offset; /* the offset from the beginning of SMB to data area */
212 unsigned int data_length; /* the length of the variable length data area */
215 /* Structure Size has already been checked to make sure it is 64 */
216 *len = le16_to_cpu(hdr->StructureSize);
219 * StructureSize2, ie length of fixed parameter area has already
220 * been checked to make sure it is the correct length.
222 *len += le16_to_cpu(pdu->StructureSize2);
224 * StructureSize2 of smb2_lock pdu is set to 48, indicating
225 * the size of smb2 lock request with single smb2_lock_element
226 * regardless of number of locks. Subtract single
227 * smb2_lock_element for correct buffer size check.
229 if (hdr->Command == SMB2_LOCK)
230 *len -= sizeof(struct smb2_lock_element);
232 if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false)
235 ret = smb2_get_data_area_len(&offset, &data_length, hdr);
238 ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length,
241 if (data_length > 0) {
243 * Check to make sure that data area begins after fixed area,
244 * Note that last byte of the fixed area is part of data area
245 * for some commands, typically those with odd StructureSize,
246 * so we must add one to the calculation.
248 if (offset + 1 < *len) {
250 "data area offset %d overlaps SMB2 header %u\n",
255 *len = offset + data_length;
259 ksmbd_debug(SMB, "SMB2 len %u\n", *len);
263 static inline int smb2_query_info_req_len(struct smb2_query_info_req *h)
265 return le32_to_cpu(h->InputBufferLength) +
266 le32_to_cpu(h->OutputBufferLength);
269 static inline int smb2_set_info_req_len(struct smb2_set_info_req *h)
271 return le32_to_cpu(h->BufferLength);
274 static inline int smb2_read_req_len(struct smb2_read_req *h)
276 return le32_to_cpu(h->Length);
279 static inline int smb2_write_req_len(struct smb2_write_req *h)
281 return le32_to_cpu(h->Length);
284 static inline int smb2_query_dir_req_len(struct smb2_query_directory_req *h)
286 return le32_to_cpu(h->OutputBufferLength);
289 static inline int smb2_ioctl_req_len(struct smb2_ioctl_req *h)
291 return le32_to_cpu(h->InputCount) +
292 le32_to_cpu(h->OutputCount);
295 static inline int smb2_ioctl_resp_len(struct smb2_ioctl_req *h)
297 return le32_to_cpu(h->MaxInputResponse) +
298 le32_to_cpu(h->MaxOutputResponse);
301 static int smb2_validate_credit_charge(struct ksmbd_conn *conn,
302 struct smb2_hdr *hdr)
304 unsigned int req_len = 0, expect_resp_len = 0, calc_credit_num, max_len;
305 unsigned short credit_charge = le16_to_cpu(hdr->CreditCharge);
309 switch (hdr->Command) {
310 case SMB2_QUERY_INFO:
311 req_len = smb2_query_info_req_len(__hdr);
314 req_len = smb2_set_info_req_len(__hdr);
317 req_len = smb2_read_req_len(__hdr);
320 req_len = smb2_write_req_len(__hdr);
322 case SMB2_QUERY_DIRECTORY:
323 req_len = smb2_query_dir_req_len(__hdr);
326 req_len = smb2_ioctl_req_len(__hdr);
327 expect_resp_len = smb2_ioctl_resp_len(__hdr);
336 credit_charge = max_t(unsigned short, credit_charge, 1);
337 max_len = max_t(unsigned int, req_len, expect_resp_len);
338 calc_credit_num = DIV_ROUND_UP(max_len, SMB2_MAX_BUFFER_SIZE);
340 if (credit_charge < calc_credit_num) {
341 ksmbd_debug(SMB, "Insufficient credit charge, given: %d, needed: %d\n",
342 credit_charge, calc_credit_num);
344 } else if (credit_charge > conn->vals->max_credits) {
345 ksmbd_debug(SMB, "Too large credit charge: %d\n", credit_charge);
349 spin_lock(&conn->credits_lock);
350 if (credit_charge > conn->total_credits) {
351 ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n",
352 credit_charge, conn->total_credits);
356 if ((u64)conn->outstanding_credits + credit_charge > conn->total_credits) {
357 ksmbd_debug(SMB, "Limits exceeding the maximum allowable outstanding requests, given : %u, pending : %u\n",
358 credit_charge, conn->outstanding_credits);
361 conn->outstanding_credits += credit_charge;
363 spin_unlock(&conn->credits_lock);
368 int ksmbd_smb2_check_message(struct ksmbd_work *work)
370 struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
371 struct smb2_hdr *hdr = &pdu->hdr;
373 __u32 clc_len; /* calculated length */
374 __u32 len = get_rfc1002_len(work->request_buf);
375 __u32 req_struct_size, next_cmd = le32_to_cpu(hdr->NextCommand);
377 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd > len) {
378 pr_err("next command(%u) offset exceeds smb msg size\n",
385 else if (work->next_smb2_rcv_hdr_off)
386 len -= work->next_smb2_rcv_hdr_off;
388 if (check_smb2_hdr(hdr))
391 if (hdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
392 ksmbd_debug(SMB, "Illegal structure size %u\n",
393 le16_to_cpu(hdr->StructureSize));
397 command = le16_to_cpu(hdr->Command);
398 if (command >= NUMBER_OF_SMB2_COMMANDS) {
399 ksmbd_debug(SMB, "Illegal SMB2 command %d\n", command);
403 if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {
404 if (!(command == SMB2_OPLOCK_BREAK_HE &&
405 (le16_to_cpu(pdu->StructureSize2) == OP_BREAK_STRUCT_SIZE_20 ||
406 le16_to_cpu(pdu->StructureSize2) == OP_BREAK_STRUCT_SIZE_21))) {
407 /* special case for SMB2.1 lease break message */
409 "Illegal request size %u for command %d\n",
410 le16_to_cpu(pdu->StructureSize2), command);
415 req_struct_size = le16_to_cpu(pdu->StructureSize2) +
416 __SMB2_HEADER_STRUCTURE_SIZE;
417 if (command == SMB2_LOCK_HE)
418 req_struct_size -= sizeof(struct smb2_lock_element);
420 if (req_struct_size > len + 1)
423 if (smb2_calc_size(hdr, &clc_len))
426 if (len != clc_len) {
427 /* client can return one byte more due to implied bcc[0] */
428 if (clc_len == len + 1)
429 goto validate_credit;
432 * Some windows servers (win2016) will pad also the final
433 * PDU in a compound to 8 bytes.
435 if (ALIGN(clc_len, 8) == len)
436 goto validate_credit;
439 * SMB2 NEGOTIATE request will be validated when message
442 if (command == SMB2_NEGOTIATE_HE)
443 goto validate_credit;
446 * Allow a message that padded to 8byte boundary.
447 * Linux 4.19.217 with smb 3.0.2 are sometimes
448 * sending messages where the cls_len is exactly
449 * 8 bytes less than len.
451 if (clc_len < len && (len - clc_len) <= 8)
452 goto validate_credit;
455 "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
456 len, clc_len, command,
457 le64_to_cpu(hdr->MessageId));
463 if ((work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) &&
464 smb2_validate_credit_charge(work->conn, hdr))
470 int smb2_negotiate_request(struct ksmbd_work *work)
472 return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE);