]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/cifsd/smb2misc.c
cifsd: fix error return code in ksmbd_vfs_remove_file()
[mirror_ubuntu-jammy-kernel.git] / fs / cifsd / 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"
9#include "smb2pdu.h"
10#include "smb_common.h"
11#include "smbstatus.h"
12#include "mgmt/user_session.h"
13#include "connection.h"
14
15static int check_smb2_hdr(struct smb2_hdr *hdr)
16{
17 /*
18 * Make sure that this really is an SMB, that it is a response.
19 */
20 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
21 return 1;
22 return 0;
23}
24
25/*
26 * The following table defines the expected "StructureSize" of SMB2 requests
27 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
28 *
29 * Note that commands are defined in smb2pdu.h in le16 but the array below is
30 * indexed by command in host byte order
31 */
32static const __le16 smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
33 /* SMB2_NEGOTIATE */ cpu_to_le16(36),
34 /* SMB2_SESSION_SETUP */ cpu_to_le16(25),
35 /* SMB2_LOGOFF */ cpu_to_le16(4),
36 /* SMB2_TREE_CONNECT */ cpu_to_le16(9),
37 /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4),
38 /* SMB2_CREATE */ cpu_to_le16(57),
39 /* SMB2_CLOSE */ cpu_to_le16(24),
40 /* SMB2_FLUSH */ cpu_to_le16(24),
41 /* SMB2_READ */ cpu_to_le16(49),
42 /* SMB2_WRITE */ cpu_to_le16(49),
43 /* SMB2_LOCK */ cpu_to_le16(48),
44 /* SMB2_IOCTL */ cpu_to_le16(57),
45 /* SMB2_CANCEL */ cpu_to_le16(4),
46 /* SMB2_ECHO */ cpu_to_le16(4),
47 /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(33),
48 /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(32),
49 /* SMB2_QUERY_INFO */ cpu_to_le16(41),
50 /* SMB2_SET_INFO */ cpu_to_le16(33),
51 /* use 44 for lease break */
52 /* SMB2_OPLOCK_BREAK */ cpu_to_le16(36)
53};
54
55/*
56 * The size of the variable area depends on the offset and length fields
57 * located in different fields for various SMB2 requests. SMB2 requests
58 * with no variable length info, show an offset of zero for the offset field.
59 */
60static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
61 /* SMB2_NEGOTIATE */ true,
62 /* SMB2_SESSION_SETUP */ true,
63 /* SMB2_LOGOFF */ false,
64 /* SMB2_TREE_CONNECT */ true,
65 /* SMB2_TREE_DISCONNECT */ false,
66 /* SMB2_CREATE */ true,
67 /* SMB2_CLOSE */ false,
68 /* SMB2_FLUSH */ false,
69 /* SMB2_READ */ true,
70 /* SMB2_WRITE */ true,
71 /* SMB2_LOCK */ true,
72 /* SMB2_IOCTL */ true,
73 /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */
74 /* SMB2_ECHO */ false,
75 /* SMB2_QUERY_DIRECTORY */ true,
76 /* SMB2_CHANGE_NOTIFY */ false,
77 /* SMB2_QUERY_INFO */ true,
78 /* SMB2_SET_INFO */ true,
79 /* SMB2_OPLOCK_BREAK */ false
80};
81
82/*
83 * Returns the pointer to the beginning of the data area. Length of the data
84 * area and the offset to it (from the beginning of the smb are also returned.
85 */
86static char *smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *hdr)
87{
88 *off = 0;
89 *len = 0;
90
91 /* error reqeusts do not have data area */
92 if (hdr->Status && hdr->Status != STATUS_MORE_PROCESSING_REQUIRED &&
93 (((struct smb2_err_rsp *)hdr)->StructureSize) ==
94 SMB2_ERROR_STRUCTURE_SIZE2_LE)
95 return NULL;
96
97 /*
98 * Following commands have data areas so we have to get the location
99 * of the data buffer offset and data buffer length for the particular
100 * command.
101 */
102 switch (hdr->Command) {
103 case SMB2_SESSION_SETUP:
104 *off = le16_to_cpu(
105 ((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset);
106 *len = le16_to_cpu(
107 ((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength);
108 break;
109 case SMB2_TREE_CONNECT:
110 *off = le16_to_cpu(
111 ((struct smb2_tree_connect_req *)hdr)->PathOffset);
112 *len = le16_to_cpu(
113 ((struct smb2_tree_connect_req *)hdr)->PathLength);
114 break;
115 case SMB2_CREATE:
116 {
117 if (((struct smb2_create_req *)hdr)->CreateContextsLength) {
118 *off = le32_to_cpu(((struct smb2_create_req *)
119 hdr)->CreateContextsOffset);
120 *len = le32_to_cpu(((struct smb2_create_req *)
121 hdr)->CreateContextsLength);
122 break;
123 }
124
125 *off = le16_to_cpu(
126 ((struct smb2_create_req *)hdr)->NameOffset);
127 *len = le16_to_cpu(
128 ((struct smb2_create_req *)hdr)->NameLength);
129 break;
130 }
131 case SMB2_QUERY_INFO:
132 *off = le16_to_cpu(
133 ((struct smb2_query_info_req *)hdr)->InputBufferOffset);
134 *len = le32_to_cpu(
135 ((struct smb2_query_info_req *)hdr)->InputBufferLength);
136 break;
137 case SMB2_SET_INFO:
138 *off = le16_to_cpu(
139 ((struct smb2_set_info_req *)hdr)->BufferOffset);
140 *len = le32_to_cpu(
141 ((struct smb2_set_info_req *)hdr)->BufferLength);
142 break;
143 case SMB2_READ:
144 *off = le16_to_cpu(
145 ((struct smb2_read_req *)hdr)->ReadChannelInfoOffset);
146 *len = le16_to_cpu(
147 ((struct smb2_read_req *)hdr)->ReadChannelInfoLength);
148 break;
149 case SMB2_WRITE:
150 if (((struct smb2_write_req *)hdr)->DataOffset) {
151 *off = le16_to_cpu(
152 ((struct smb2_write_req *)hdr)->DataOffset);
153 *len = le32_to_cpu(
154 ((struct smb2_write_req *)hdr)->Length);
155 break;
156 }
157
158 *off = le16_to_cpu(
159 ((struct smb2_write_req *)hdr)->WriteChannelInfoOffset);
160 *len = le16_to_cpu(
161 ((struct smb2_write_req *)hdr)->WriteChannelInfoLength);
162 break;
163 case SMB2_QUERY_DIRECTORY:
164 *off = le16_to_cpu(
165 ((struct smb2_query_directory_req *)hdr)->FileNameOffset);
166 *len = le16_to_cpu(
167 ((struct smb2_query_directory_req *)hdr)->FileNameLength);
168 break;
169 case SMB2_LOCK:
170 {
171 int lock_count;
172
173 /*
174 * smb2_lock request size is 48 included single
175 * smb2_lock_element structure size.
176 */
177 lock_count = le16_to_cpu(
178 ((struct smb2_lock_req *)hdr)->LockCount) - 1;
179 if (lock_count > 0) {
180 *off = __SMB2_HEADER_STRUCTURE_SIZE + 48;
181 *len = sizeof(struct smb2_lock_element) * lock_count;
182 }
183 break;
184 }
185 case SMB2_IOCTL:
186 *off = le32_to_cpu(
187 ((struct smb2_ioctl_req *)hdr)->InputOffset);
188 *len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount);
189
190 break;
191 default:
192 ksmbd_debug(SMB, "no length check for command\n");
193 break;
194 }
195
196 /*
197 * Invalid length or offset probably means data area is invalid, but
198 * we have little choice but to ignore the data area in this case.
199 */
200 if (*off > 4096) {
201 ksmbd_debug(SMB, "offset %d too large, data area ignored\n",
202 *off);
203 *len = 0;
204 *off = 0;
205 } else if (*off < 0) {
206 ksmbd_debug(SMB,
207 "negative offset %d to data invalid ignore data area\n",
208 *off);
209 *off = 0;
210 *len = 0;
211 } else if (*len < 0) {
212 ksmbd_debug(SMB,
213 "negative data length %d invalid, data area ignored\n",
214 *len);
215 *len = 0;
216 } else if (*len > 128 * 1024) {
217 ksmbd_debug(SMB, "data area larger than 128K: %d\n", *len);
218 *len = 0;
219 }
220
221 /* return pointer to beginning of data area, ie offset from SMB start */
222 if ((*off != 0) && (*len != 0))
223 return (char *)hdr + *off;
224 else
225 return NULL;
226}
227
228/*
229 * Calculate the size of the SMB message based on the fixed header
230 * portion, the number of word parameters and the data portion of the message.
231 */
232static unsigned int smb2_calc_size(void *buf)
233{
234 struct smb2_pdu *pdu = (struct smb2_pdu *)buf;
235 struct smb2_hdr *hdr = &pdu->hdr;
236 int offset; /* the offset from the beginning of SMB to data area */
237 int data_length; /* the length of the variable length data area */
238 /* Structure Size has already been checked to make sure it is 64 */
239 int len = le16_to_cpu(hdr->StructureSize);
240
241 /*
242 * StructureSize2, ie length of fixed parameter area has already
243 * been checked to make sure it is the correct length.
244 */
245 len += le16_to_cpu(pdu->StructureSize2);
246
247 if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false)
248 goto calc_size_exit;
249
250 smb2_get_data_area_len(&offset, &data_length, hdr);
251 ksmbd_debug(SMB, "SMB2 data length %d offset %d\n", data_length,
252 offset);
253
254 if (data_length > 0) {
255 /*
256 * Check to make sure that data area begins after fixed area,
257 * Note that last byte of the fixed area is part of data area
258 * for some commands, typically those with odd StructureSize,
259 * so we must add one to the calculation.
260 */
261 if (offset + 1 < len)
262 ksmbd_debug(SMB,
263 "data area offset %d overlaps SMB2 header %d\n",
264 offset + 1, len);
265 else
266 len = offset + data_length;
267 }
268calc_size_exit:
269 ksmbd_debug(SMB, "SMB2 len %d\n", len);
270 return len;
271}
272
273static inline int smb2_query_info_req_len(struct smb2_query_info_req *h)
274{
275 return le32_to_cpu(h->InputBufferLength) +
276 le32_to_cpu(h->OutputBufferLength);
277}
278
279static inline int smb2_set_info_req_len(struct smb2_set_info_req *h)
280{
281 return le32_to_cpu(h->BufferLength);
282}
283
284static inline int smb2_read_req_len(struct smb2_read_req *h)
285{
286 return le32_to_cpu(h->Length);
287}
288
289static inline int smb2_write_req_len(struct smb2_write_req *h)
290{
291 return le32_to_cpu(h->Length);
292}
293
294static inline int smb2_query_dir_req_len(struct smb2_query_directory_req *h)
295{
296 return le32_to_cpu(h->OutputBufferLength);
297}
298
299static inline int smb2_ioctl_req_len(struct smb2_ioctl_req *h)
300{
301 return le32_to_cpu(h->InputCount) +
302 le32_to_cpu(h->OutputCount);
303}
304
305static inline int smb2_ioctl_resp_len(struct smb2_ioctl_req *h)
306{
307 return le32_to_cpu(h->MaxInputResponse) +
308 le32_to_cpu(h->MaxOutputResponse);
309}
310
311static int smb2_validate_credit_charge(struct smb2_hdr *hdr)
312{
313 int req_len = 0, expect_resp_len = 0, calc_credit_num, max_len;
314 int credit_charge = le16_to_cpu(hdr->CreditCharge);
315 void *__hdr = hdr;
316
317 switch (hdr->Command) {
318 case SMB2_QUERY_INFO:
319 req_len = smb2_query_info_req_len(__hdr);
320 break;
321 case SMB2_SET_INFO:
322 req_len = smb2_set_info_req_len(__hdr);
323 break;
324 case SMB2_READ:
325 req_len = smb2_read_req_len(__hdr);
326 break;
327 case SMB2_WRITE:
328 req_len = smb2_write_req_len(__hdr);
329 break;
330 case SMB2_QUERY_DIRECTORY:
331 req_len = smb2_query_dir_req_len(__hdr);
332 break;
333 case SMB2_IOCTL:
334 req_len = smb2_ioctl_req_len(__hdr);
335 expect_resp_len = smb2_ioctl_resp_len(__hdr);
336 break;
337 default:
338 return 0;
339 }
340
341 max_len = max(req_len, expect_resp_len);
342 calc_credit_num = DIV_ROUND_UP(max_len, SMB2_MAX_BUFFER_SIZE);
343 if (!credit_charge && max_len > SMB2_MAX_BUFFER_SIZE) {
344 ksmbd_err("credit charge is zero and payload size(%d) is bigger than 64K\n",
345 max_len);
346 return 1;
347 } else if (credit_charge < calc_credit_num) {
348 ksmbd_err("credit charge : %d, calc_credit_num : %d\n",
349 credit_charge, calc_credit_num);
350 return 1;
351 }
352
353 return 0;
354}
355
356int ksmbd_smb2_check_message(struct ksmbd_work *work)
357{
e5066499 358 struct smb2_pdu *pdu = work->request_buf;
e2f34481
NJ
359 struct smb2_hdr *hdr = &pdu->hdr;
360 int command;
361 __u32 clc_len; /* calculated length */
362 __u32 len = get_rfc1002_len(pdu);
363
364 if (work->next_smb2_rcv_hdr_off) {
365 pdu = REQUEST_BUF_NEXT(work);
366 hdr = &pdu->hdr;
367 }
368
369 if (le32_to_cpu(hdr->NextCommand) > 0)
370 len = le32_to_cpu(hdr->NextCommand);
371 else if (work->next_smb2_rcv_hdr_off) {
372 len -= work->next_smb2_rcv_hdr_off;
373 len = round_up(len, 8);
374 }
375
376 if (check_smb2_hdr(hdr))
377 return 1;
378
379 if (hdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
380 ksmbd_debug(SMB, "Illegal structure size %u\n",
381 le16_to_cpu(hdr->StructureSize));
382 return 1;
383 }
384
385 command = le16_to_cpu(hdr->Command);
386 if (command >= NUMBER_OF_SMB2_COMMANDS) {
387 ksmbd_debug(SMB, "Illegal SMB2 command %d\n", command);
388 return 1;
389 }
390
391 if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {
392 if (command != SMB2_OPLOCK_BREAK_HE && (hdr->Status == 0 ||
393 pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2_LE)) {
394 /* error packets have 9 byte structure size */
395 ksmbd_debug(SMB,
396 "Illegal request size %u for command %d\n",
397 le16_to_cpu(pdu->StructureSize2), command);
398 return 1;
399 } else if (command == SMB2_OPLOCK_BREAK_HE
400 && (hdr->Status == 0)
401 && (le16_to_cpu(pdu->StructureSize2) !=
402 OP_BREAK_STRUCT_SIZE_20)
403 && (le16_to_cpu(pdu->StructureSize2) !=
404 OP_BREAK_STRUCT_SIZE_21)) {
405 /* special case for SMB2.1 lease break message */
406 ksmbd_debug(SMB,
407 "Illegal request size %d for oplock break\n",
408 le16_to_cpu(pdu->StructureSize2));
409 return 1;
410 }
411 }
412
413 clc_len = smb2_calc_size(hdr);
414 if (len != clc_len) {
415 /* server can return one byte more due to implied bcc[0] */
416 if (clc_len == len + 1)
417 return 0;
418
419 /*
420 * Some windows servers (win2016) will pad also the final
421 * PDU in a compound to 8 bytes.
422 */
423 if (ALIGN(clc_len, 8) == len)
424 return 0;
425
426 /*
427 * windows client also pad up to 8 bytes when compounding.
428 * If pad is longer than eight bytes, log the server behavior
429 * (once), since may indicate a problem but allow it and
430 * continue since the frame is parseable.
431 */
432 if (clc_len < len) {
433 ksmbd_debug(SMB,
434 "cli req padded more than expected. Length %d not %d for cmd:%d mid:%llu\n",
435 len, clc_len, command,
436 le64_to_cpu(hdr->MessageId));
437 return 0;
438 }
439
440 if (command == SMB2_LOCK_HE && len == 88)
441 return 0;
442
443 ksmbd_debug(SMB,
444 "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
445 len, clc_len, command,
446 le64_to_cpu(hdr->MessageId));
447
448 return 1;
449 }
450
451 return work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU ?
452 smb2_validate_credit_charge(hdr) : 0;
453}
454
455int smb2_negotiate_request(struct ksmbd_work *work)
456{
457 return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE);
458}