GNU Linux-libre 6.0.2-gnu
[releases.git] / fs / ksmbd / smb2misc.c
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 "smb_common.h"
10 #include "smbstatus.h"
11 #include "mgmt/user_session.h"
12 #include "connection.h"
13
14 static 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  */
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)
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  */
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,
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 /*
82  * Set length of the data area and the offset to arguments.
83  * if they are invalid, return error.
84  */
85 static int smb2_get_data_area_len(unsigned int *off, unsigned int *len,
86                                   struct smb2_hdr *hdr)
87 {
88         int ret = 0;
89
90         *off = 0;
91         *len = 0;
92
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:
100                 *off = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset);
101                 *len = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength);
102                 break;
103         case SMB2_TREE_CONNECT:
104                 *off = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathOffset);
105                 *len = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathLength);
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
117                 *off = le16_to_cpu(((struct smb2_create_req *)hdr)->NameOffset);
118                 *len = le16_to_cpu(((struct smb2_create_req *)hdr)->NameLength);
119                 break;
120         }
121         case SMB2_QUERY_INFO:
122                 *off = le16_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferOffset);
123                 *len = le32_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferLength);
124                 break;
125         case SMB2_SET_INFO:
126                 *off = le16_to_cpu(((struct smb2_set_info_req *)hdr)->BufferOffset);
127                 *len = le32_to_cpu(((struct smb2_set_info_req *)hdr)->BufferLength);
128                 break;
129         case SMB2_READ:
130                 *off = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoOffset);
131                 *len = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoLength);
132                 break;
133         case SMB2_WRITE:
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));
139                         *len = le32_to_cpu(((struct smb2_write_req *)hdr)->Length);
140                         break;
141                 }
142
143                 *off = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoOffset);
144                 *len = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoLength);
145                 break;
146         case SMB2_QUERY_DIRECTORY:
147                 *off = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameOffset);
148                 *len = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameLength);
149                 break;
150         case SMB2_LOCK:
151         {
152                 int lock_count;
153
154                 /*
155                  * smb2_lock request size is 48 included single
156                  * smb2_lock_element structure size.
157                  */
158                 lock_count = le16_to_cpu(((struct smb2_lock_req *)hdr)->LockCount) - 1;
159                 if (lock_count > 0) {
160                         *off = __SMB2_HEADER_STRUCTURE_SIZE + 48;
161                         *len = sizeof(struct smb2_lock_element) * lock_count;
162                 }
163                 break;
164         }
165         case SMB2_IOCTL:
166                 *off = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputOffset);
167                 *len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount);
168                 break;
169         default:
170                 ksmbd_debug(SMB, "no length check for command\n");
171                 break;
172         }
173
174         if (*off > 4096) {
175                 ksmbd_debug(SMB, "offset %d too large\n", *off);
176                 ret = -EINVAL;
177         } else if ((u64)*off + *len > MAX_STREAM_PROT_LEN) {
178                 ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n",
179                             MAX_STREAM_PROT_LEN, (u64)*off + *len);
180                 ret = -EINVAL;
181         }
182
183         return ret;
184 }
185
186 /*
187  * Calculate the size of the SMB message based on the fixed header
188  * portion, the number of word parameters and the data portion of the message.
189  */
190 static int smb2_calc_size(void *buf, unsigned int *len)
191 {
192         struct smb2_pdu *pdu = (struct smb2_pdu *)buf;
193         struct smb2_hdr *hdr = &pdu->hdr;
194         unsigned int offset; /* the offset from the beginning of SMB to data area */
195         unsigned int data_length; /* the length of the variable length data area */
196         int ret;
197
198         /* Structure Size has already been checked to make sure it is 64 */
199         *len = le16_to_cpu(hdr->StructureSize);
200
201         /*
202          * StructureSize2, ie length of fixed parameter area has already
203          * been checked to make sure it is the correct length.
204          */
205         *len += le16_to_cpu(pdu->StructureSize2);
206         /*
207          * StructureSize2 of smb2_lock pdu is set to 48, indicating
208          * the size of smb2 lock request with single smb2_lock_element
209          * regardless of number of locks. Subtract single
210          * smb2_lock_element for correct buffer size check.
211          */
212         if (hdr->Command == SMB2_LOCK)
213                 *len -= sizeof(struct smb2_lock_element);
214
215         if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false)
216                 goto calc_size_exit;
217
218         ret = smb2_get_data_area_len(&offset, &data_length, hdr);
219         if (ret)
220                 return ret;
221         ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length,
222                     offset);
223
224         if (data_length > 0) {
225                 /*
226                  * Check to make sure that data area begins after fixed area,
227                  * Note that last byte of the fixed area is part of data area
228                  * for some commands, typically those with odd StructureSize,
229                  * so we must add one to the calculation.
230                  */
231                 if (offset + 1 < *len) {
232                         ksmbd_debug(SMB,
233                                     "data area offset %d overlaps SMB2 header %u\n",
234                                     offset + 1, *len);
235                         return -EINVAL;
236                 }
237
238                 *len = offset + data_length;
239         }
240
241 calc_size_exit:
242         ksmbd_debug(SMB, "SMB2 len %u\n", *len);
243         return 0;
244 }
245
246 static inline int smb2_query_info_req_len(struct smb2_query_info_req *h)
247 {
248         return le32_to_cpu(h->InputBufferLength) +
249                 le32_to_cpu(h->OutputBufferLength);
250 }
251
252 static inline int smb2_set_info_req_len(struct smb2_set_info_req *h)
253 {
254         return le32_to_cpu(h->BufferLength);
255 }
256
257 static inline int smb2_read_req_len(struct smb2_read_req *h)
258 {
259         return le32_to_cpu(h->Length);
260 }
261
262 static inline int smb2_write_req_len(struct smb2_write_req *h)
263 {
264         return le32_to_cpu(h->Length);
265 }
266
267 static inline int smb2_query_dir_req_len(struct smb2_query_directory_req *h)
268 {
269         return le32_to_cpu(h->OutputBufferLength);
270 }
271
272 static inline int smb2_ioctl_req_len(struct smb2_ioctl_req *h)
273 {
274         return le32_to_cpu(h->InputCount) +
275                 le32_to_cpu(h->OutputCount);
276 }
277
278 static inline int smb2_ioctl_resp_len(struct smb2_ioctl_req *h)
279 {
280         return le32_to_cpu(h->MaxInputResponse) +
281                 le32_to_cpu(h->MaxOutputResponse);
282 }
283
284 static int smb2_validate_credit_charge(struct ksmbd_conn *conn,
285                                        struct smb2_hdr *hdr)
286 {
287         unsigned int req_len = 0, expect_resp_len = 0, calc_credit_num, max_len;
288         unsigned short credit_charge = le16_to_cpu(hdr->CreditCharge);
289         void *__hdr = hdr;
290         int ret = 0;
291
292         switch (hdr->Command) {
293         case SMB2_QUERY_INFO:
294                 req_len = smb2_query_info_req_len(__hdr);
295                 break;
296         case SMB2_SET_INFO:
297                 req_len = smb2_set_info_req_len(__hdr);
298                 break;
299         case SMB2_READ:
300                 req_len = smb2_read_req_len(__hdr);
301                 break;
302         case SMB2_WRITE:
303                 req_len = smb2_write_req_len(__hdr);
304                 break;
305         case SMB2_QUERY_DIRECTORY:
306                 req_len = smb2_query_dir_req_len(__hdr);
307                 break;
308         case SMB2_IOCTL:
309                 req_len = smb2_ioctl_req_len(__hdr);
310                 expect_resp_len = smb2_ioctl_resp_len(__hdr);
311                 break;
312         case SMB2_CANCEL:
313                 return 0;
314         default:
315                 req_len = 1;
316                 break;
317         }
318
319         credit_charge = max_t(unsigned short, credit_charge, 1);
320         max_len = max_t(unsigned int, req_len, expect_resp_len);
321         calc_credit_num = DIV_ROUND_UP(max_len, SMB2_MAX_BUFFER_SIZE);
322
323         if (credit_charge < calc_credit_num) {
324                 ksmbd_debug(SMB, "Insufficient credit charge, given: %d, needed: %d\n",
325                             credit_charge, calc_credit_num);
326                 return 1;
327         } else if (credit_charge > conn->vals->max_credits) {
328                 ksmbd_debug(SMB, "Too large credit charge: %d\n", credit_charge);
329                 return 1;
330         }
331
332         spin_lock(&conn->credits_lock);
333         if (credit_charge > conn->total_credits) {
334                 ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n",
335                             credit_charge, conn->total_credits);
336                 ret = 1;
337         }
338
339         if ((u64)conn->outstanding_credits + credit_charge > conn->total_credits) {
340                 ksmbd_debug(SMB, "Limits exceeding the maximum allowable outstanding requests, given : %u, pending : %u\n",
341                             credit_charge, conn->outstanding_credits);
342                 ret = 1;
343         } else
344                 conn->outstanding_credits += credit_charge;
345
346         spin_unlock(&conn->credits_lock);
347
348         return ret;
349 }
350
351 int ksmbd_smb2_check_message(struct ksmbd_work *work)
352 {
353         struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
354         struct smb2_hdr *hdr = &pdu->hdr;
355         int command;
356         __u32 clc_len;  /* calculated length */
357         __u32 len = get_rfc1002_len(work->request_buf);
358
359         if (le32_to_cpu(hdr->NextCommand) > 0)
360                 len = le32_to_cpu(hdr->NextCommand);
361         else if (work->next_smb2_rcv_hdr_off)
362                 len -= work->next_smb2_rcv_hdr_off;
363
364         if (check_smb2_hdr(hdr))
365                 return 1;
366
367         if (hdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
368                 ksmbd_debug(SMB, "Illegal structure size %u\n",
369                             le16_to_cpu(hdr->StructureSize));
370                 return 1;
371         }
372
373         command = le16_to_cpu(hdr->Command);
374         if (command >= NUMBER_OF_SMB2_COMMANDS) {
375                 ksmbd_debug(SMB, "Illegal SMB2 command %d\n", command);
376                 return 1;
377         }
378
379         if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {
380                 if (command != SMB2_OPLOCK_BREAK_HE &&
381                     (hdr->Status == 0 || pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2_LE)) {
382                         /* error packets have 9 byte structure size */
383                         ksmbd_debug(SMB,
384                                     "Illegal request size %u for command %d\n",
385                                     le16_to_cpu(pdu->StructureSize2), command);
386                         return 1;
387                 } else if (command == SMB2_OPLOCK_BREAK_HE &&
388                            hdr->Status == 0 &&
389                            le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_20 &&
390                            le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_21) {
391                         /* special case for SMB2.1 lease break message */
392                         ksmbd_debug(SMB,
393                                     "Illegal request size %d for oplock break\n",
394                                     le16_to_cpu(pdu->StructureSize2));
395                         return 1;
396                 }
397         }
398
399         if (smb2_calc_size(hdr, &clc_len))
400                 return 1;
401
402         if (len != clc_len) {
403                 /* client can return one byte more due to implied bcc[0] */
404                 if (clc_len == len + 1)
405                         goto validate_credit;
406
407                 /*
408                  * Some windows servers (win2016) will pad also the final
409                  * PDU in a compound to 8 bytes.
410                  */
411                 if (ALIGN(clc_len, 8) == len)
412                         goto validate_credit;
413
414                 /*
415                  * windows client also pad up to 8 bytes when compounding.
416                  * If pad is longer than eight bytes, log the server behavior
417                  * (once), since may indicate a problem but allow it and
418                  * continue since the frame is parseable.
419                  */
420                 if (clc_len < len) {
421                         ksmbd_debug(SMB,
422                                     "cli req padded more than expected. Length %d not %d for cmd:%d mid:%llu\n",
423                                     len, clc_len, command,
424                                     le64_to_cpu(hdr->MessageId));
425                         goto validate_credit;
426                 }
427
428                 ksmbd_debug(SMB,
429                             "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
430                             len, clc_len, command,
431                             le64_to_cpu(hdr->MessageId));
432
433                 return 1;
434         }
435
436 validate_credit:
437         if ((work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) &&
438             smb2_validate_credit_charge(work->conn, hdr)) {
439                 work->conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
440                 return 1;
441         }
442
443         return 0;
444 }
445
446 int smb2_negotiate_request(struct ksmbd_work *work)
447 {
448         return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE);
449 }