1 // SPDX-License-Identifier: GPL-2.0-or-later 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 2 /* 3 * Copyright (C) 2016 Namjae Jeon <linkinjeo 3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> 4 * Copyright (C) 2018 Samsung Electronics Co 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 5 */ 6 6 7 #include "glob.h" 7 #include "glob.h" 8 #include "nterr.h" 8 #include "nterr.h" 9 #include "smb_common.h" 9 #include "smb_common.h" 10 #include "smbstatus.h" 10 #include "smbstatus.h" 11 #include "mgmt/user_session.h" 11 #include "mgmt/user_session.h" 12 #include "connection.h" 12 #include "connection.h" 13 13 14 static int check_smb2_hdr(struct smb2_hdr *hdr 14 static int check_smb2_hdr(struct smb2_hdr *hdr) 15 { 15 { 16 /* 16 /* 17 * Make sure that this really is an SM 17 * Make sure that this really is an SMB, that it is a response. 18 */ 18 */ 19 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_ 19 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) 20 return 1; 20 return 1; 21 return 0; 21 return 0; 22 } 22 } 23 23 24 /* 24 /* 25 * The following table defines the expected " 25 * The following table defines the expected "StructureSize" of SMB2 requests 26 * in order by SMB2 command. This is similar 26 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests. 27 * 27 * 28 * Note that commands are defined in smb2pdu. 28 * Note that commands are defined in smb2pdu.h in le16 but the array below is 29 * indexed by command in host byte order 29 * indexed by command in host byte order 30 */ 30 */ 31 static const __le16 smb2_req_struct_sizes[NUMB 31 static const __le16 smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { 32 /* SMB2_NEGOTIATE */ cpu_to_le16(36), 32 /* SMB2_NEGOTIATE */ cpu_to_le16(36), 33 /* SMB2_SESSION_SETUP */ cpu_to_le16(2 33 /* SMB2_SESSION_SETUP */ cpu_to_le16(25), 34 /* SMB2_LOGOFF */ cpu_to_le16(4), 34 /* SMB2_LOGOFF */ cpu_to_le16(4), 35 /* SMB2_TREE_CONNECT */ cpu_to_le16(9) 35 /* SMB2_TREE_CONNECT */ cpu_to_le16(9), 36 /* SMB2_TREE_DISCONNECT */ cpu_to_le16 36 /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4), 37 /* SMB2_CREATE */ cpu_to_le16(57), 37 /* SMB2_CREATE */ cpu_to_le16(57), 38 /* SMB2_CLOSE */ cpu_to_le16(24), 38 /* SMB2_CLOSE */ cpu_to_le16(24), 39 /* SMB2_FLUSH */ cpu_to_le16(24), 39 /* SMB2_FLUSH */ cpu_to_le16(24), 40 /* SMB2_READ */ cpu_to_le16(49), 40 /* SMB2_READ */ cpu_to_le16(49), 41 /* SMB2_WRITE */ cpu_to_le16(49), 41 /* SMB2_WRITE */ cpu_to_le16(49), 42 /* SMB2_LOCK */ cpu_to_le16(48), 42 /* SMB2_LOCK */ cpu_to_le16(48), 43 /* SMB2_IOCTL */ cpu_to_le16(57), 43 /* SMB2_IOCTL */ cpu_to_le16(57), 44 /* SMB2_CANCEL */ cpu_to_le16(4), 44 /* SMB2_CANCEL */ cpu_to_le16(4), 45 /* SMB2_ECHO */ cpu_to_le16(4), 45 /* SMB2_ECHO */ cpu_to_le16(4), 46 /* SMB2_QUERY_DIRECTORY */ cpu_to_le16 46 /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(33), 47 /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(3 47 /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(32), 48 /* SMB2_QUERY_INFO */ cpu_to_le16(41), 48 /* SMB2_QUERY_INFO */ cpu_to_le16(41), 49 /* SMB2_SET_INFO */ cpu_to_le16(33), 49 /* SMB2_SET_INFO */ cpu_to_le16(33), 50 /* use 44 for lease break */ 50 /* use 44 for lease break */ 51 /* SMB2_OPLOCK_BREAK */ cpu_to_le16(36 51 /* SMB2_OPLOCK_BREAK */ cpu_to_le16(36) 52 }; 52 }; 53 53 54 /* 54 /* 55 * The size of the variable area depends on th 55 * The size of the variable area depends on the offset and length fields 56 * located in different fields for various SMB 56 * located in different fields for various SMB2 requests. SMB2 requests 57 * with no variable length info, show an offse 57 * with no variable length info, show an offset of zero for the offset field. 58 */ 58 */ 59 static const bool has_smb2_data_area[NUMBER_OF 59 static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = { 60 /* SMB2_NEGOTIATE */ true, 60 /* SMB2_NEGOTIATE */ true, 61 /* SMB2_SESSION_SETUP */ true, 61 /* SMB2_SESSION_SETUP */ true, 62 /* SMB2_LOGOFF */ false, 62 /* SMB2_LOGOFF */ false, 63 /* SMB2_TREE_CONNECT */ true, 63 /* SMB2_TREE_CONNECT */ true, 64 /* SMB2_TREE_DISCONNECT */ false, 64 /* SMB2_TREE_DISCONNECT */ false, 65 /* SMB2_CREATE */ true, 65 /* SMB2_CREATE */ true, 66 /* SMB2_CLOSE */ false, 66 /* SMB2_CLOSE */ false, 67 /* SMB2_FLUSH */ false, 67 /* SMB2_FLUSH */ false, 68 /* SMB2_READ */ true, 68 /* SMB2_READ */ true, 69 /* SMB2_WRITE */ true, 69 /* SMB2_WRITE */ true, 70 /* SMB2_LOCK */ true, 70 /* SMB2_LOCK */ true, 71 /* SMB2_IOCTL */ true, 71 /* SMB2_IOCTL */ true, 72 /* SMB2_CANCEL */ false, /* BB CHECK t 72 /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */ 73 /* SMB2_ECHO */ false, 73 /* SMB2_ECHO */ false, 74 /* SMB2_QUERY_DIRECTORY */ true, 74 /* SMB2_QUERY_DIRECTORY */ true, 75 /* SMB2_CHANGE_NOTIFY */ false, 75 /* SMB2_CHANGE_NOTIFY */ false, 76 /* SMB2_QUERY_INFO */ true, 76 /* SMB2_QUERY_INFO */ true, 77 /* SMB2_SET_INFO */ true, 77 /* SMB2_SET_INFO */ true, 78 /* SMB2_OPLOCK_BREAK */ false 78 /* SMB2_OPLOCK_BREAK */ false 79 }; 79 }; 80 80 81 /* 81 /* 82 * Set length of the data area and the offset 82 * Set length of the data area and the offset to arguments. 83 * if they are invalid, return error. 83 * if they are invalid, return error. 84 */ 84 */ 85 static int smb2_get_data_area_len(unsigned int 85 static int smb2_get_data_area_len(unsigned int *off, unsigned int *len, 86 struct smb2_ 86 struct smb2_hdr *hdr) 87 { 87 { 88 int ret = 0; 88 int ret = 0; 89 89 90 *off = 0; 90 *off = 0; 91 *len = 0; 91 *len = 0; 92 92 93 /* 93 /* 94 * Following commands have data areas 94 * Following commands have data areas so we have to get the location 95 * of the data buffer offset and data 95 * of the data buffer offset and data buffer length for the particular 96 * command. 96 * command. 97 */ 97 */ 98 switch (hdr->Command) { 98 switch (hdr->Command) { 99 case SMB2_SESSION_SETUP: 99 case SMB2_SESSION_SETUP: 100 *off = le16_to_cpu(((struct sm 100 *off = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset); 101 *len = le16_to_cpu(((struct sm 101 *len = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength); 102 break; 102 break; 103 case SMB2_TREE_CONNECT: 103 case SMB2_TREE_CONNECT: 104 *off = max_t(unsigned short in !! 104 *off = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathOffset); 105 le16_to_cpu(((str << 106 offsetof(struct s << 107 *len = le16_to_cpu(((struct sm 105 *len = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathLength); 108 break; 106 break; 109 case SMB2_CREATE: 107 case SMB2_CREATE: 110 { 108 { 111 unsigned short int name_off = << 112 max_t(unsigned short i << 113 le16_to_cpu(((st << 114 offsetof(struct << 115 unsigned short int name_len = << 116 le16_to_cpu(((struct s << 117 << 118 if (((struct smb2_create_req * 109 if (((struct smb2_create_req *)hdr)->CreateContextsLength) { 119 *off = le32_to_cpu(((s 110 *off = le32_to_cpu(((struct smb2_create_req *) 120 hdr)->CreateCo 111 hdr)->CreateContextsOffset); 121 *len = le32_to_cpu(((s 112 *len = le32_to_cpu(((struct smb2_create_req *) 122 hdr)->CreateCo 113 hdr)->CreateContextsLength); 123 if (!name_len) !! 114 break; 124 break; << 125 << 126 if (name_off + name_le << 127 break; << 128 } 115 } 129 116 130 *off = name_off; !! 117 *off = le16_to_cpu(((struct smb2_create_req *)hdr)->NameOffset); 131 *len = name_len; !! 118 *len = le16_to_cpu(((struct smb2_create_req *)hdr)->NameLength); 132 break; 119 break; 133 } 120 } 134 case SMB2_QUERY_INFO: 121 case SMB2_QUERY_INFO: 135 *off = max_t(unsigned int, !! 122 *off = le16_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferOffset); 136 le16_to_cpu(((str << 137 offsetof(struct s << 138 *len = le32_to_cpu(((struct sm 123 *len = le32_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferLength); 139 break; 124 break; 140 case SMB2_SET_INFO: 125 case SMB2_SET_INFO: 141 *off = max_t(unsigned int, !! 126 *off = le16_to_cpu(((struct smb2_set_info_req *)hdr)->BufferOffset); 142 le16_to_cpu(((str << 143 offsetof(struct s << 144 *len = le32_to_cpu(((struct sm 127 *len = le32_to_cpu(((struct smb2_set_info_req *)hdr)->BufferLength); 145 break; 128 break; 146 case SMB2_READ: 129 case SMB2_READ: 147 *off = le16_to_cpu(((struct sm 130 *off = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoOffset); 148 *len = le16_to_cpu(((struct sm 131 *len = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoLength); 149 break; 132 break; 150 case SMB2_WRITE: 133 case SMB2_WRITE: 151 if (((struct smb2_write_req *) 134 if (((struct smb2_write_req *)hdr)->DataOffset || 152 ((struct smb2_write_req *) 135 ((struct smb2_write_req *)hdr)->Length) { 153 *off = max_t(unsigned !! 136 *off = max_t(unsigned int, 154 le16_to_c 137 le16_to_cpu(((struct smb2_write_req *)hdr)->DataOffset), 155 offsetof( 138 offsetof(struct smb2_write_req, Buffer)); 156 *len = le32_to_cpu(((s 139 *len = le32_to_cpu(((struct smb2_write_req *)hdr)->Length); 157 break; 140 break; 158 } 141 } 159 142 160 *off = le16_to_cpu(((struct sm 143 *off = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoOffset); 161 *len = le16_to_cpu(((struct sm 144 *len = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoLength); 162 break; 145 break; 163 case SMB2_QUERY_DIRECTORY: 146 case SMB2_QUERY_DIRECTORY: 164 *off = max_t(unsigned short in !! 147 *off = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameOffset); 165 le16_to_cpu(((str << 166 offsetof(struct s << 167 *len = le16_to_cpu(((struct sm 148 *len = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameLength); 168 break; 149 break; 169 case SMB2_LOCK: 150 case SMB2_LOCK: 170 { 151 { 171 unsigned short lock_count; 152 unsigned short lock_count; 172 153 173 lock_count = le16_to_cpu(((str 154 lock_count = le16_to_cpu(((struct smb2_lock_req *)hdr)->LockCount); 174 if (lock_count > 0) { 155 if (lock_count > 0) { 175 *off = offsetof(struct 156 *off = offsetof(struct smb2_lock_req, locks); 176 *len = sizeof(struct s 157 *len = sizeof(struct smb2_lock_element) * lock_count; 177 } 158 } 178 break; 159 break; 179 } 160 } 180 case SMB2_IOCTL: 161 case SMB2_IOCTL: 181 *off = max_t(unsigned int, !! 162 *off = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputOffset); 182 le32_to_cpu(((str << 183 offsetof(struct s << 184 *len = le32_to_cpu(((struct sm 163 *len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount); 185 break; 164 break; 186 default: 165 default: 187 ksmbd_debug(SMB, "no length ch 166 ksmbd_debug(SMB, "no length check for command\n"); 188 break; 167 break; 189 } 168 } 190 169 191 if (*off > 4096) { 170 if (*off > 4096) { 192 ksmbd_debug(SMB, "offset %d to 171 ksmbd_debug(SMB, "offset %d too large\n", *off); 193 ret = -EINVAL; 172 ret = -EINVAL; 194 } else if ((u64)*off + *len > MAX_STRE 173 } else if ((u64)*off + *len > MAX_STREAM_PROT_LEN) { 195 ksmbd_debug(SMB, "Request is l 174 ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n", 196 MAX_STREAM_PROT_LE 175 MAX_STREAM_PROT_LEN, (u64)*off + *len); 197 ret = -EINVAL; 176 ret = -EINVAL; 198 } 177 } 199 178 200 return ret; 179 return ret; 201 } 180 } 202 181 203 /* 182 /* 204 * Calculate the size of the SMB message based 183 * Calculate the size of the SMB message based on the fixed header 205 * portion, the number of word parameters and 184 * portion, the number of word parameters and the data portion of the message. 206 */ 185 */ 207 static int smb2_calc_size(void *buf, unsigned 186 static int smb2_calc_size(void *buf, unsigned int *len) 208 { 187 { 209 struct smb2_pdu *pdu = (struct smb2_pd 188 struct smb2_pdu *pdu = (struct smb2_pdu *)buf; 210 struct smb2_hdr *hdr = &pdu->hdr; 189 struct smb2_hdr *hdr = &pdu->hdr; 211 unsigned int offset; /* the offset fro 190 unsigned int offset; /* the offset from the beginning of SMB to data area */ 212 unsigned int data_length; /* the lengt 191 unsigned int data_length; /* the length of the variable length data area */ 213 int ret; 192 int ret; 214 193 215 /* Structure Size has already been che 194 /* Structure Size has already been checked to make sure it is 64 */ 216 *len = le16_to_cpu(hdr->StructureSize) 195 *len = le16_to_cpu(hdr->StructureSize); 217 196 218 /* 197 /* 219 * StructureSize2, ie length of fixed 198 * StructureSize2, ie length of fixed parameter area has already 220 * been checked to make sure it is the 199 * been checked to make sure it is the correct length. 221 */ 200 */ 222 *len += le16_to_cpu(pdu->StructureSize 201 *len += le16_to_cpu(pdu->StructureSize2); 223 /* 202 /* 224 * StructureSize2 of smb2_lock pdu is 203 * StructureSize2 of smb2_lock pdu is set to 48, indicating 225 * the size of smb2 lock request with 204 * the size of smb2 lock request with single smb2_lock_element 226 * regardless of number of locks. Subt 205 * regardless of number of locks. Subtract single 227 * smb2_lock_element for correct buffe 206 * smb2_lock_element for correct buffer size check. 228 */ 207 */ 229 if (hdr->Command == SMB2_LOCK) 208 if (hdr->Command == SMB2_LOCK) 230 *len -= sizeof(struct smb2_loc 209 *len -= sizeof(struct smb2_lock_element); 231 210 232 if (has_smb2_data_area[le16_to_cpu(hdr 211 if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false) 233 goto calc_size_exit; 212 goto calc_size_exit; 234 213 235 ret = smb2_get_data_area_len(&offset, 214 ret = smb2_get_data_area_len(&offset, &data_length, hdr); 236 if (ret) 215 if (ret) 237 return ret; 216 return ret; 238 ksmbd_debug(SMB, "SMB2 data length %u 217 ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length, 239 offset); 218 offset); 240 219 241 if (data_length > 0) { 220 if (data_length > 0) { 242 /* 221 /* 243 * Check to make sure that dat 222 * Check to make sure that data area begins after fixed area, 244 * Note that last byte of the 223 * Note that last byte of the fixed area is part of data area 245 * for some commands, typicall 224 * for some commands, typically those with odd StructureSize, 246 * so we must add one to the c 225 * so we must add one to the calculation. 247 */ 226 */ 248 if (offset + 1 < *len) { 227 if (offset + 1 < *len) { 249 ksmbd_debug(SMB, 228 ksmbd_debug(SMB, 250 "data area 229 "data area offset %d overlaps SMB2 header %u\n", 251 offset + 1 230 offset + 1, *len); 252 return -EINVAL; 231 return -EINVAL; 253 } 232 } 254 233 255 *len = offset + data_length; 234 *len = offset + data_length; 256 } 235 } 257 236 258 calc_size_exit: 237 calc_size_exit: 259 ksmbd_debug(SMB, "SMB2 len %u\n", *len 238 ksmbd_debug(SMB, "SMB2 len %u\n", *len); 260 return 0; 239 return 0; 261 } 240 } 262 241 263 static inline int smb2_query_info_req_len(stru 242 static inline int smb2_query_info_req_len(struct smb2_query_info_req *h) 264 { 243 { 265 return le32_to_cpu(h->InputBufferLengt 244 return le32_to_cpu(h->InputBufferLength) + 266 le32_to_cpu(h->OutputBufferLen 245 le32_to_cpu(h->OutputBufferLength); 267 } 246 } 268 247 269 static inline int smb2_set_info_req_len(struct 248 static inline int smb2_set_info_req_len(struct smb2_set_info_req *h) 270 { 249 { 271 return le32_to_cpu(h->BufferLength); 250 return le32_to_cpu(h->BufferLength); 272 } 251 } 273 252 274 static inline int smb2_read_req_len(struct smb 253 static inline int smb2_read_req_len(struct smb2_read_req *h) 275 { 254 { 276 return le32_to_cpu(h->Length); 255 return le32_to_cpu(h->Length); 277 } 256 } 278 257 279 static inline int smb2_write_req_len(struct sm 258 static inline int smb2_write_req_len(struct smb2_write_req *h) 280 { 259 { 281 return le32_to_cpu(h->Length); 260 return le32_to_cpu(h->Length); 282 } 261 } 283 262 284 static inline int smb2_query_dir_req_len(struc 263 static inline int smb2_query_dir_req_len(struct smb2_query_directory_req *h) 285 { 264 { 286 return le32_to_cpu(h->OutputBufferLeng 265 return le32_to_cpu(h->OutputBufferLength); 287 } 266 } 288 267 289 static inline int smb2_ioctl_req_len(struct sm 268 static inline int smb2_ioctl_req_len(struct smb2_ioctl_req *h) 290 { 269 { 291 return le32_to_cpu(h->InputCount) + 270 return le32_to_cpu(h->InputCount) + 292 le32_to_cpu(h->OutputCount); 271 le32_to_cpu(h->OutputCount); 293 } 272 } 294 273 295 static inline int smb2_ioctl_resp_len(struct s 274 static inline int smb2_ioctl_resp_len(struct smb2_ioctl_req *h) 296 { 275 { 297 return le32_to_cpu(h->MaxInputResponse 276 return le32_to_cpu(h->MaxInputResponse) + 298 le32_to_cpu(h->MaxOutputRespon 277 le32_to_cpu(h->MaxOutputResponse); 299 } 278 } 300 279 301 static int smb2_validate_credit_charge(struct 280 static int smb2_validate_credit_charge(struct ksmbd_conn *conn, 302 struct 281 struct smb2_hdr *hdr) 303 { 282 { 304 unsigned int req_len = 0, expect_resp_ 283 unsigned int req_len = 0, expect_resp_len = 0, calc_credit_num, max_len; 305 unsigned short credit_charge = le16_to 284 unsigned short credit_charge = le16_to_cpu(hdr->CreditCharge); 306 void *__hdr = hdr; 285 void *__hdr = hdr; 307 int ret = 0; 286 int ret = 0; 308 287 309 switch (hdr->Command) { 288 switch (hdr->Command) { 310 case SMB2_QUERY_INFO: 289 case SMB2_QUERY_INFO: 311 req_len = smb2_query_info_req_ 290 req_len = smb2_query_info_req_len(__hdr); 312 break; 291 break; 313 case SMB2_SET_INFO: 292 case SMB2_SET_INFO: 314 req_len = smb2_set_info_req_le 293 req_len = smb2_set_info_req_len(__hdr); 315 break; 294 break; 316 case SMB2_READ: 295 case SMB2_READ: 317 req_len = smb2_read_req_len(__ 296 req_len = smb2_read_req_len(__hdr); 318 break; 297 break; 319 case SMB2_WRITE: 298 case SMB2_WRITE: 320 req_len = smb2_write_req_len(_ 299 req_len = smb2_write_req_len(__hdr); 321 break; 300 break; 322 case SMB2_QUERY_DIRECTORY: 301 case SMB2_QUERY_DIRECTORY: 323 req_len = smb2_query_dir_req_l 302 req_len = smb2_query_dir_req_len(__hdr); 324 break; 303 break; 325 case SMB2_IOCTL: 304 case SMB2_IOCTL: 326 req_len = smb2_ioctl_req_len(_ 305 req_len = smb2_ioctl_req_len(__hdr); 327 expect_resp_len = smb2_ioctl_r 306 expect_resp_len = smb2_ioctl_resp_len(__hdr); 328 break; 307 break; 329 case SMB2_CANCEL: 308 case SMB2_CANCEL: 330 return 0; 309 return 0; 331 default: 310 default: 332 req_len = 1; 311 req_len = 1; 333 break; 312 break; 334 } 313 } 335 314 336 credit_charge = max_t(unsigned short, 315 credit_charge = max_t(unsigned short, credit_charge, 1); 337 max_len = max_t(unsigned int, req_len, 316 max_len = max_t(unsigned int, req_len, expect_resp_len); 338 calc_credit_num = DIV_ROUND_UP(max_len 317 calc_credit_num = DIV_ROUND_UP(max_len, SMB2_MAX_BUFFER_SIZE); 339 318 340 if (credit_charge < calc_credit_num) { 319 if (credit_charge < calc_credit_num) { 341 ksmbd_debug(SMB, "Insufficient 320 ksmbd_debug(SMB, "Insufficient credit charge, given: %d, needed: %d\n", 342 credit_charge, cal 321 credit_charge, calc_credit_num); 343 return 1; 322 return 1; 344 } else if (credit_charge > conn->vals- 323 } else if (credit_charge > conn->vals->max_credits) { 345 ksmbd_debug(SMB, "Too large cr 324 ksmbd_debug(SMB, "Too large credit charge: %d\n", credit_charge); 346 return 1; 325 return 1; 347 } 326 } 348 327 349 spin_lock(&conn->credits_lock); 328 spin_lock(&conn->credits_lock); 350 if (credit_charge > conn->total_credit 329 if (credit_charge > conn->total_credits) { 351 ksmbd_debug(SMB, "Insufficient 330 ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n", 352 credit_charge, con 331 credit_charge, conn->total_credits); 353 ret = 1; 332 ret = 1; 354 } 333 } 355 334 356 if ((u64)conn->outstanding_credits + c 335 if ((u64)conn->outstanding_credits + credit_charge > conn->total_credits) { 357 ksmbd_debug(SMB, "Limits excee 336 ksmbd_debug(SMB, "Limits exceeding the maximum allowable outstanding requests, given : %u, pending : %u\n", 358 credit_charge, con 337 credit_charge, conn->outstanding_credits); 359 ret = 1; 338 ret = 1; 360 } else 339 } else 361 conn->outstanding_credits += c 340 conn->outstanding_credits += credit_charge; 362 341 363 spin_unlock(&conn->credits_lock); 342 spin_unlock(&conn->credits_lock); 364 343 365 return ret; 344 return ret; 366 } 345 } 367 346 368 int ksmbd_smb2_check_message(struct ksmbd_work 347 int ksmbd_smb2_check_message(struct ksmbd_work *work) 369 { 348 { 370 struct smb2_pdu *pdu = ksmbd_req_buf_n 349 struct smb2_pdu *pdu = ksmbd_req_buf_next(work); 371 struct smb2_hdr *hdr = &pdu->hdr; 350 struct smb2_hdr *hdr = &pdu->hdr; 372 int command; 351 int command; 373 __u32 clc_len; /* calculated length * 352 __u32 clc_len; /* calculated length */ 374 __u32 len = get_rfc1002_len(work->requ 353 __u32 len = get_rfc1002_len(work->request_buf); 375 __u32 req_struct_size, next_cmd = le32 354 __u32 req_struct_size, next_cmd = le32_to_cpu(hdr->NextCommand); 376 355 377 if ((u64)work->next_smb2_rcv_hdr_off + 356 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd > len) { 378 pr_err("next command(%u) offse 357 pr_err("next command(%u) offset exceeds smb msg size\n", 379 next_cmd); 358 next_cmd); 380 return 1; 359 return 1; 381 } 360 } 382 361 383 if (next_cmd > 0) 362 if (next_cmd > 0) 384 len = next_cmd; 363 len = next_cmd; 385 else if (work->next_smb2_rcv_hdr_off) 364 else if (work->next_smb2_rcv_hdr_off) 386 len -= work->next_smb2_rcv_hdr 365 len -= work->next_smb2_rcv_hdr_off; 387 366 388 if (check_smb2_hdr(hdr)) 367 if (check_smb2_hdr(hdr)) 389 return 1; 368 return 1; 390 369 391 if (hdr->StructureSize != SMB2_HEADER_ 370 if (hdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) { 392 ksmbd_debug(SMB, "Illegal stru 371 ksmbd_debug(SMB, "Illegal structure size %u\n", 393 le16_to_cpu(hdr->S 372 le16_to_cpu(hdr->StructureSize)); 394 return 1; 373 return 1; 395 } 374 } 396 375 397 command = le16_to_cpu(hdr->Command); 376 command = le16_to_cpu(hdr->Command); 398 if (command >= NUMBER_OF_SMB2_COMMANDS 377 if (command >= NUMBER_OF_SMB2_COMMANDS) { 399 ksmbd_debug(SMB, "Illegal SMB2 378 ksmbd_debug(SMB, "Illegal SMB2 command %d\n", command); 400 return 1; 379 return 1; 401 } 380 } 402 381 403 if (smb2_req_struct_sizes[command] != 382 if (smb2_req_struct_sizes[command] != pdu->StructureSize2) { 404 if (!(command == SMB2_OPLOCK_B 383 if (!(command == SMB2_OPLOCK_BREAK_HE && 405 (le16_to_cpu(pdu->Structur 384 (le16_to_cpu(pdu->StructureSize2) == OP_BREAK_STRUCT_SIZE_20 || 406 le16_to_cpu(pdu->Structure 385 le16_to_cpu(pdu->StructureSize2) == OP_BREAK_STRUCT_SIZE_21))) { 407 /* special case for SM 386 /* special case for SMB2.1 lease break message */ 408 ksmbd_debug(SMB, 387 ksmbd_debug(SMB, 409 "Illegal reque 388 "Illegal request size %u for command %d\n", 410 le16_to_cpu(pd 389 le16_to_cpu(pdu->StructureSize2), command); 411 return 1; 390 return 1; 412 } 391 } 413 } 392 } 414 393 415 req_struct_size = le16_to_cpu(pdu->Str 394 req_struct_size = le16_to_cpu(pdu->StructureSize2) + 416 __SMB2_HEADER_STRUCTURE_SIZE; 395 __SMB2_HEADER_STRUCTURE_SIZE; 417 if (command == SMB2_LOCK_HE) 396 if (command == SMB2_LOCK_HE) 418 req_struct_size -= sizeof(stru 397 req_struct_size -= sizeof(struct smb2_lock_element); 419 398 420 if (req_struct_size > len + 1) 399 if (req_struct_size > len + 1) 421 return 1; 400 return 1; 422 401 423 if (smb2_calc_size(hdr, &clc_len)) 402 if (smb2_calc_size(hdr, &clc_len)) 424 return 1; 403 return 1; 425 404 426 if (len != clc_len) { 405 if (len != clc_len) { 427 /* client can return one byte 406 /* client can return one byte more due to implied bcc[0] */ 428 if (clc_len == len + 1) 407 if (clc_len == len + 1) 429 goto validate_credit; 408 goto validate_credit; 430 409 431 /* 410 /* 432 * Some windows servers (win20 411 * Some windows servers (win2016) will pad also the final 433 * PDU in a compound to 8 byte 412 * PDU in a compound to 8 bytes. 434 */ 413 */ 435 if (ALIGN(clc_len, 8) == len) 414 if (ALIGN(clc_len, 8) == len) 436 goto validate_credit; 415 goto validate_credit; 437 416 438 /* 417 /* 439 * SMB2 NEGOTIATE request will 418 * SMB2 NEGOTIATE request will be validated when message 440 * handling proceeds. 419 * handling proceeds. 441 */ 420 */ 442 if (command == SMB2_NEGOTIATE_ 421 if (command == SMB2_NEGOTIATE_HE) 443 goto validate_credit; 422 goto validate_credit; 444 423 445 /* 424 /* 446 * Allow a message that padded 425 * Allow a message that padded to 8byte boundary. 447 * Linux 4.19.217 with smb 3.0 426 * Linux 4.19.217 with smb 3.0.2 are sometimes 448 * sending messages where the 427 * sending messages where the cls_len is exactly 449 * 8 bytes less than len. 428 * 8 bytes less than len. 450 */ 429 */ 451 if (clc_len < len && (len - cl 430 if (clc_len < len && (len - clc_len) <= 8) 452 goto validate_credit; 431 goto validate_credit; 453 432 454 pr_err_ratelimited( 433 pr_err_ratelimited( 455 "cli req too short 434 "cli req too short, len %d not %d. cmd:%d mid:%llu\n", 456 len, clc_len, comm 435 len, clc_len, command, 457 le64_to_cpu(hdr->M 436 le64_to_cpu(hdr->MessageId)); 458 437 459 return 1; 438 return 1; 460 } 439 } 461 440 462 validate_credit: 441 validate_credit: 463 if ((work->conn->vals->capabilities & 442 if ((work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) && 464 smb2_validate_credit_charge(work-> !! 443 smb2_validate_credit_charge(work->conn, hdr)) { >> 444 work->conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER); 465 return 1; 445 return 1; >> 446 } 466 447 467 return 0; 448 return 0; 468 } 449 } 469 450 470 int smb2_negotiate_request(struct ksmbd_work * 451 int smb2_negotiate_request(struct ksmbd_work *work) 471 { 452 { 472 return ksmbd_smb_negotiate_common(work 453 return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE); 473 } 454 } 474 455
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.