nvme: optimise io_uring passthrough completion
[linux-block.git] / fs / ksmbd / smb2misc.c
CommitLineData
e2f34481
NJ
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
5 */
6
7#include "glob.h"
8#include "nterr.h"
e2f34481
NJ
9#include "smb_common.h"
10#include "smbstatus.h"
11#include "mgmt/user_session.h"
12#include "connection.h"
13
14static int check_smb2_hdr(struct smb2_hdr *hdr)
15{
16 /*
17 * Make sure that this really is an SMB, that it is a response.
18 */
19 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
20 return 1;
21 return 0;
22}
23
24/*
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.
27 *
28 * Note that commands are defined in smb2pdu.h in le16 but the array below is
29 * indexed by command in host byte order
30 */
31static 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)
52};
53
54/*
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.
58 */
59static 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,
68 /* SMB2_READ */ true,
69 /* SMB2_WRITE */ true,
70 /* SMB2_LOCK */ 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
79};
80
81/*
c2e99d47
NJ
82 * Set length of the data area and the offset to arguments.
83 * if they are invalid, return error.
e2f34481 84 */
c2e99d47
NJ
85static int smb2_get_data_area_len(unsigned int *off, unsigned int *len,
86 struct smb2_hdr *hdr)
e2f34481 87{
c2e99d47
NJ
88 int ret = 0;
89
e2f34481
NJ
90 *off = 0;
91 *len = 0;
92
e2f34481
NJ
93 /*
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
96 * command.
97 */
98 switch (hdr->Command) {
99 case SMB2_SESSION_SETUP:
64b39f4a
NJ
100 *off = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset);
101 *len = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength);
e2f34481
NJ
102 break;
103 case SMB2_TREE_CONNECT:
64b39f4a
NJ
104 *off = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathOffset);
105 *len = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathLength);
e2f34481
NJ
106 break;
107 case SMB2_CREATE:
108 {
109 if (((struct smb2_create_req *)hdr)->CreateContextsLength) {
110 *off = le32_to_cpu(((struct smb2_create_req *)
111 hdr)->CreateContextsOffset);
112 *len = le32_to_cpu(((struct smb2_create_req *)
113 hdr)->CreateContextsLength);
114 break;
115 }
116
64b39f4a
NJ
117 *off = le16_to_cpu(((struct smb2_create_req *)hdr)->NameOffset);
118 *len = le16_to_cpu(((struct smb2_create_req *)hdr)->NameLength);
e2f34481
NJ
119 break;
120 }
121 case SMB2_QUERY_INFO:
64b39f4a
NJ
122 *off = le16_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferOffset);
123 *len = le32_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferLength);
e2f34481
NJ
124 break;
125 case SMB2_SET_INFO:
64b39f4a
NJ
126 *off = le16_to_cpu(((struct smb2_set_info_req *)hdr)->BufferOffset);
127 *len = le32_to_cpu(((struct smb2_set_info_req *)hdr)->BufferLength);
e2f34481
NJ
128 break;
129 case SMB2_READ:
64b39f4a
NJ
130 *off = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoOffset);
131 *len = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoLength);
e2f34481
NJ
132 break;
133 case SMB2_WRITE:
ac60778b
HL
134 if (((struct smb2_write_req *)hdr)->DataOffset ||
135 ((struct smb2_write_req *)hdr)->Length) {
136 *off = max_t(unsigned int,
137 le16_to_cpu(((struct smb2_write_req *)hdr)->DataOffset),
138 offsetof(struct smb2_write_req, Buffer));
64b39f4a 139 *len = le32_to_cpu(((struct smb2_write_req *)hdr)->Length);
e2f34481
NJ
140 break;
141 }
142
64b39f4a
NJ
143 *off = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoOffset);
144 *len = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoLength);
e2f34481
NJ
145 break;
146 case SMB2_QUERY_DIRECTORY:
64b39f4a
NJ
147 *off = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameOffset);
148 *len = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameLength);
e2f34481
NJ
149 break;
150 case SMB2_LOCK:
151 {
8f8c43b1 152 unsigned short lock_count;
e2f34481 153
8f8c43b1 154 lock_count = le16_to_cpu(((struct smb2_lock_req *)hdr)->LockCount);
e2f34481 155 if (lock_count > 0) {
8f8c43b1 156 *off = offsetof(struct smb2_lock_req, locks);
e2f34481
NJ
157 *len = sizeof(struct smb2_lock_element) * lock_count;
158 }
159 break;
160 }
161 case SMB2_IOCTL:
64b39f4a 162 *off = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputOffset);
e2f34481 163 *len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount);
e2f34481
NJ
164 break;
165 default:
166 ksmbd_debug(SMB, "no length check for command\n");
167 break;
168 }
169
e2f34481 170 if (*off > 4096) {
c2e99d47
NJ
171 ksmbd_debug(SMB, "offset %d too large\n", *off);
172 ret = -EINVAL;
173 } else if ((u64)*off + *len > MAX_STREAM_PROT_LEN) {
174 ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n",
175 MAX_STREAM_PROT_LEN, (u64)*off + *len);
176 ret = -EINVAL;
e2f34481
NJ
177 }
178
c2e99d47 179 return ret;
e2f34481
NJ
180}
181
182/*
183 * Calculate the size of the SMB message based on the fixed header
184 * portion, the number of word parameters and the data portion of the message.
185 */
c2e99d47 186static int smb2_calc_size(void *buf, unsigned int *len)
e2f34481
NJ
187{
188 struct smb2_pdu *pdu = (struct smb2_pdu *)buf;
189 struct smb2_hdr *hdr = &pdu->hdr;
c2e99d47
NJ
190 unsigned int offset; /* the offset from the beginning of SMB to data area */
191 unsigned int data_length; /* the length of the variable length data area */
192 int ret;
193
e2f34481 194 /* Structure Size has already been checked to make sure it is 64 */
c2e99d47 195 *len = le16_to_cpu(hdr->StructureSize);
e2f34481
NJ
196
197 /*
198 * StructureSize2, ie length of fixed parameter area has already
199 * been checked to make sure it is the correct length.
200 */
c2e99d47
NJ
201 *len += le16_to_cpu(pdu->StructureSize2);
202 /*
203 * StructureSize2 of smb2_lock pdu is set to 48, indicating
204 * the size of smb2 lock request with single smb2_lock_element
205 * regardless of number of locks. Subtract single
206 * smb2_lock_element for correct buffer size check.
207 */
208 if (hdr->Command == SMB2_LOCK)
209 *len -= sizeof(struct smb2_lock_element);
e2f34481
NJ
210
211 if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false)
212 goto calc_size_exit;
213
c2e99d47
NJ
214 ret = smb2_get_data_area_len(&offset, &data_length, hdr);
215 if (ret)
216 return ret;
217 ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length,
070fb21e 218 offset);
e2f34481
NJ
219
220 if (data_length > 0) {
221 /*
222 * Check to make sure that data area begins after fixed area,
223 * Note that last byte of the fixed area is part of data area
224 * for some commands, typically those with odd StructureSize,
225 * so we must add one to the calculation.
226 */
c2e99d47 227 if (offset + 1 < *len) {
e2f34481 228 ksmbd_debug(SMB,
c2e99d47
NJ
229 "data area offset %d overlaps SMB2 header %u\n",
230 offset + 1, *len);
231 return -EINVAL;
232 }
233
234 *len = offset + data_length;
e2f34481 235 }
c2e99d47 236
e2f34481 237calc_size_exit:
c2e99d47
NJ
238 ksmbd_debug(SMB, "SMB2 len %u\n", *len);
239 return 0;
e2f34481
NJ
240}
241
242static inline int smb2_query_info_req_len(struct smb2_query_info_req *h)
243{
244 return le32_to_cpu(h->InputBufferLength) +
245 le32_to_cpu(h->OutputBufferLength);
246}
247
248static inline int smb2_set_info_req_len(struct smb2_set_info_req *h)
249{
250 return le32_to_cpu(h->BufferLength);
251}
252
253static inline int smb2_read_req_len(struct smb2_read_req *h)
254{
255 return le32_to_cpu(h->Length);
256}
257
258static inline int smb2_write_req_len(struct smb2_write_req *h)
259{
260 return le32_to_cpu(h->Length);
261}
262
263static inline int smb2_query_dir_req_len(struct smb2_query_directory_req *h)
264{
265 return le32_to_cpu(h->OutputBufferLength);
266}
267
268static inline int smb2_ioctl_req_len(struct smb2_ioctl_req *h)
269{
270 return le32_to_cpu(h->InputCount) +
271 le32_to_cpu(h->OutputCount);
272}
273
274static inline int smb2_ioctl_resp_len(struct smb2_ioctl_req *h)
275{
276 return le32_to_cpu(h->MaxInputResponse) +
277 le32_to_cpu(h->MaxOutputResponse);
278}
279
bf8acc9e
HL
280static int smb2_validate_credit_charge(struct ksmbd_conn *conn,
281 struct smb2_hdr *hdr)
e2f34481 282{
bf8acc9e
HL
283 unsigned int req_len = 0, expect_resp_len = 0, calc_credit_num, max_len;
284 unsigned short credit_charge = le16_to_cpu(hdr->CreditCharge);
e2f34481 285 void *__hdr = hdr;
914d7e57 286 int ret = 0;
e2f34481
NJ
287
288 switch (hdr->Command) {
289 case SMB2_QUERY_INFO:
290 req_len = smb2_query_info_req_len(__hdr);
291 break;
292 case SMB2_SET_INFO:
293 req_len = smb2_set_info_req_len(__hdr);
294 break;
295 case SMB2_READ:
296 req_len = smb2_read_req_len(__hdr);
297 break;
298 case SMB2_WRITE:
299 req_len = smb2_write_req_len(__hdr);
300 break;
301 case SMB2_QUERY_DIRECTORY:
302 req_len = smb2_query_dir_req_len(__hdr);
303 break;
304 case SMB2_IOCTL:
305 req_len = smb2_ioctl_req_len(__hdr);
306 expect_resp_len = smb2_ioctl_resp_len(__hdr);
307 break;
bf8acc9e 308 case SMB2_CANCEL:
e2f34481 309 return 0;
bf8acc9e
HL
310 default:
311 req_len = 1;
312 break;
e2f34481
NJ
313 }
314
bf8acc9e
HL
315 credit_charge = max_t(unsigned short, credit_charge, 1);
316 max_len = max_t(unsigned int, req_len, expect_resp_len);
e2f34481 317 calc_credit_num = DIV_ROUND_UP(max_len, SMB2_MAX_BUFFER_SIZE);
a5a25a11
MM
318
319 if (credit_charge < calc_credit_num) {
bf8acc9e
HL
320 ksmbd_debug(SMB, "Insufficient credit charge, given: %d, needed: %d\n",
321 credit_charge, calc_credit_num);
322 return 1;
004443b3 323 } else if (credit_charge > conn->vals->max_credits) {
bf8acc9e 324 ksmbd_debug(SMB, "Too large credit charge: %d\n", credit_charge);
e2f34481
NJ
325 return 1;
326 }
327
bf8acc9e 328 spin_lock(&conn->credits_lock);
914d7e57 329 if (credit_charge > conn->total_credits) {
bf8acc9e
HL
330 ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n",
331 credit_charge, conn->total_credits);
332 ret = 1;
333 }
b589f5db 334
376b9133 335 if ((u64)conn->outstanding_credits + credit_charge > conn->total_credits) {
b589f5db
NJ
336 ksmbd_debug(SMB, "Limits exceeding the maximum allowable outstanding requests, given : %u, pending : %u\n",
337 credit_charge, conn->outstanding_credits);
338 ret = 1;
339 } else
340 conn->outstanding_credits += credit_charge;
341
bf8acc9e 342 spin_unlock(&conn->credits_lock);
b589f5db 343
bf8acc9e 344 return ret;
e2f34481
NJ
345}
346
347int ksmbd_smb2_check_message(struct ksmbd_work *work)
348{
b83b2790 349 struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
e2f34481
NJ
350 struct smb2_hdr *hdr = &pdu->hdr;
351 int command;
352 __u32 clc_len; /* calculated length */
cb451720 353 __u32 len = get_rfc1002_len(work->request_buf);
e2f34481 354
b53ad810 355 if (le32_to_cpu(hdr->NextCommand) > 0)
e2f34481 356 len = le32_to_cpu(hdr->NextCommand);
b53ad810 357 else if (work->next_smb2_rcv_hdr_off)
e2f34481 358 len -= work->next_smb2_rcv_hdr_off;
e2f34481
NJ
359
360 if (check_smb2_hdr(hdr))
361 return 1;
362
363 if (hdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
364 ksmbd_debug(SMB, "Illegal structure size %u\n",
070fb21e 365 le16_to_cpu(hdr->StructureSize));
e2f34481
NJ
366 return 1;
367 }
368
369 command = le16_to_cpu(hdr->Command);
370 if (command >= NUMBER_OF_SMB2_COMMANDS) {
371 ksmbd_debug(SMB, "Illegal SMB2 command %d\n", command);
372 return 1;
373 }
374
375 if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {
64b39f4a
NJ
376 if (command != SMB2_OPLOCK_BREAK_HE &&
377 (hdr->Status == 0 || pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2_LE)) {
e2f34481
NJ
378 /* error packets have 9 byte structure size */
379 ksmbd_debug(SMB,
070fb21e
NJ
380 "Illegal request size %u for command %d\n",
381 le16_to_cpu(pdu->StructureSize2), command);
e2f34481 382 return 1;
64b39f4a
NJ
383 } else if (command == SMB2_OPLOCK_BREAK_HE &&
384 hdr->Status == 0 &&
385 le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_20 &&
386 le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_21) {
e2f34481
NJ
387 /* special case for SMB2.1 lease break message */
388 ksmbd_debug(SMB,
070fb21e
NJ
389 "Illegal request size %d for oplock break\n",
390 le16_to_cpu(pdu->StructureSize2));
e2f34481
NJ
391 return 1;
392 }
393 }
394
c2e99d47
NJ
395 if (smb2_calc_size(hdr, &clc_len))
396 return 1;
397
e2f34481 398 if (len != clc_len) {
c2e99d47 399 /* client can return one byte more due to implied bcc[0] */
e2f34481 400 if (clc_len == len + 1)
7a334887 401 goto validate_credit;
e2f34481
NJ
402
403 /*
404 * Some windows servers (win2016) will pad also the final
405 * PDU in a compound to 8 bytes.
406 */
407 if (ALIGN(clc_len, 8) == len)
7a334887 408 goto validate_credit;
e2f34481
NJ
409
410 /*
fb533473
NJ
411 * SMB2 NEGOTIATE request will be validated when message
412 * handling proceeds.
e2f34481 413 */
fb533473
NJ
414 if (command == SMB2_NEGOTIATE_HE)
415 goto validate_credit;
416
417 /*
418 * Allow a message that padded to 8byte boundary.
419 */
420 if (clc_len < len && (len - clc_len) < 8)
7a334887 421 goto validate_credit;
e2f34481 422
fb533473 423 pr_err_ratelimited(
070fb21e
NJ
424 "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
425 len, clc_len, command,
426 le64_to_cpu(hdr->MessageId));
e2f34481
NJ
427
428 return 1;
429 }
430
7a334887
RB
431validate_credit:
432 if ((work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) &&
433 smb2_validate_credit_charge(work->conn, hdr)) {
434 work->conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
435 return 1;
436 }
437
67307023 438 return 0;
e2f34481
NJ
439}
440
441int smb2_negotiate_request(struct ksmbd_work *work)
442{
443 return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE);
444}