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