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