1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * Copyright (C) International Business Machines Corp., 2002,2011 5 * Etersoft, 2012 6 * Author(s): Steve French (sfrench@us.ibm.com) 7 * Pavel Shilovsky (pshilovsky@samba.org) 2012 8 * 9 */ 10 #include <crypto/sha2.h> 11 #include <linux/ctype.h> 12 #include "cifsglob.h" 13 #include "cifsproto.h" 14 #include "smb2proto.h" 15 #include "cifs_debug.h" 16 #include "cifs_unicode.h" 17 #include "../common/smb2status.h" 18 #include "smb2glob.h" 19 #include "nterr.h" 20 #include "cached_dir.h" 21 22 static unsigned int __smb2_calc_size(void *buf, bool *have_data, 23 bool *data_area_overlap); 24 25 static int 26 check_smb2_hdr(struct smb2_hdr *shdr, __u64 mid) 27 { 28 __u64 wire_mid = le64_to_cpu(shdr->MessageId); 29 30 /* 31 * Make sure that this really is an SMB, that it is a response, 32 * and that the message ids match. 33 */ 34 if ((shdr->ProtocolId == SMB2_PROTO_NUMBER) && 35 (mid == wire_mid)) { 36 if (shdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) 37 return 0; 38 else { 39 /* only one valid case where server sends us request */ 40 if (shdr->Command == SMB2_OPLOCK_BREAK) 41 return 0; 42 else 43 cifs_dbg(VFS, "Received Request not response\n"); 44 } 45 } else { /* bad signature or mid */ 46 if (shdr->ProtocolId != SMB2_PROTO_NUMBER) 47 cifs_dbg(VFS, "Bad protocol string signature header %x\n", 48 le32_to_cpu(shdr->ProtocolId)); 49 if (mid != wire_mid) 50 cifs_dbg(VFS, "Mids do not match: %llu and %llu\n", 51 mid, wire_mid); 52 } 53 cifs_dbg(VFS, "Bad SMB detected. The Mid=%llu\n", wire_mid); 54 return 1; 55 } 56 57 /* 58 * The following table defines the expected "StructureSize" of SMB2 responses 59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS responses. 60 * 61 * Note that commands are defined in smb2pdu.h in le16 but the array below is 62 * indexed by command in host byte order 63 */ 64 static const __le16 smb2_rsp_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { 65 /* SMB2_NEGOTIATE */ cpu_to_le16(65), 66 /* SMB2_SESSION_SETUP */ cpu_to_le16(9), 67 /* SMB2_LOGOFF */ cpu_to_le16(4), 68 /* SMB2_TREE_CONNECT */ cpu_to_le16(16), 69 /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4), 70 /* SMB2_CREATE */ cpu_to_le16(89), 71 /* SMB2_CLOSE */ cpu_to_le16(60), 72 /* SMB2_FLUSH */ cpu_to_le16(4), 73 /* SMB2_READ */ cpu_to_le16(17), 74 /* SMB2_WRITE */ cpu_to_le16(17), 75 /* SMB2_LOCK */ cpu_to_le16(4), 76 /* SMB2_IOCTL */ cpu_to_le16(49), 77 /* BB CHECK this ... not listed in documentation */ 78 /* SMB2_CANCEL */ cpu_to_le16(0), 79 /* SMB2_ECHO */ cpu_to_le16(4), 80 /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(9), 81 /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(9), 82 /* SMB2_QUERY_INFO */ cpu_to_le16(9), 83 /* SMB2_SET_INFO */ cpu_to_le16(2), 84 /* BB FIXME can also be 44 for lease break */ 85 /* SMB2_OPLOCK_BREAK */ cpu_to_le16(24) 86 }; 87 88 #define SMB311_NEGPROT_BASE_SIZE (sizeof(struct smb2_hdr) + sizeof(struct smb2_negotiate_rsp)) 89 90 static __u32 get_neg_ctxt_len(struct smb2_hdr *hdr, __u32 len, 91 __u32 non_ctxlen) 92 { 93 __u16 neg_count; 94 __u32 nc_offset, size_of_pad_before_neg_ctxts; 95 struct smb2_negotiate_rsp *pneg_rsp = (struct smb2_negotiate_rsp *)hdr; 96 97 /* Negotiate contexts are only valid for latest dialect SMB3.11 */ 98 neg_count = le16_to_cpu(pneg_rsp->NegotiateContextCount); 99 if ((neg_count == 0) || 100 (pneg_rsp->DialectRevision != cpu_to_le16(SMB311_PROT_ID))) 101 return 0; 102 103 /* 104 * if SPNEGO blob present (ie the RFC2478 GSS info which indicates 105 * which security mechanisms the server supports) make sure that 106 * the negotiate contexts start after it 107 */ 108 nc_offset = le32_to_cpu(pneg_rsp->NegotiateContextOffset); 109 /* 110 * non_ctxlen is at least shdr->StructureSize + pdu->StructureSize2 111 * and the latter is 1 byte bigger than the fix-sized area of the 112 * NEGOTIATE response 113 */ 114 if (nc_offset + 1 < non_ctxlen) { 115 pr_warn_once("Invalid negotiate context offset %d\n", nc_offset); 116 return 0; 117 } else if (nc_offset + 1 == non_ctxlen) { 118 cifs_dbg(FYI, "no SPNEGO security blob in negprot rsp\n"); 119 size_of_pad_before_neg_ctxts = 0; 120 } else if (non_ctxlen == SMB311_NEGPROT_BASE_SIZE + 1) 121 /* has padding, but no SPNEGO blob */ 122 size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen + 1; 123 else 124 size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen; 125 126 /* Verify that at least minimal negotiate contexts fit within frame */ 127 if (len < nc_offset + (neg_count * sizeof(struct smb2_neg_context))) { 128 pr_warn_once("negotiate context goes beyond end\n"); 129 return 0; 130 } 131 132 cifs_dbg(FYI, "length of negcontexts %d pad %d\n", 133 len - nc_offset, size_of_pad_before_neg_ctxts); 134 135 /* length of negcontexts including pad from end of sec blob to them */ 136 return (len - nc_offset) + size_of_pad_before_neg_ctxts; 137 } 138 139 int 140 smb2_check_message(char *buf, unsigned int pdu_len, unsigned int len, 141 struct TCP_Server_Info *server) 142 { 143 struct TCP_Server_Info *pserver; 144 struct smb2_hdr *shdr = (struct smb2_hdr *)buf; 145 struct smb2_pdu *pdu = (struct smb2_pdu *)shdr; 146 int hdr_size = sizeof(struct smb2_hdr); 147 int pdu_size = sizeof(struct smb2_pdu); 148 int command; 149 __u32 calc_len; /* calculated length */ 150 __u64 mid; 151 bool have_data; 152 bool data_area_overlap; 153 154 /* If server is a channel, select the primary channel */ 155 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 156 157 /* 158 * Add function to do table lookup of StructureSize by command 159 * ie Validate the wct via smb2_struct_sizes table above 160 */ 161 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) { 162 struct smb2_transform_hdr *thdr = 163 (struct smb2_transform_hdr *)buf; 164 struct cifs_ses *ses = NULL; 165 struct cifs_ses *iter; 166 167 /* decrypt frame now that it is completely read in */ 168 spin_lock(&cifs_tcp_ses_lock); 169 list_for_each_entry(iter, &pserver->smb_ses_list, smb_ses_list) { 170 if (iter->Suid == le64_to_cpu(thdr->SessionId)) { 171 ses = iter; 172 break; 173 } 174 } 175 spin_unlock(&cifs_tcp_ses_lock); 176 if (!ses) { 177 cifs_dbg(VFS, "no decryption - session id not found\n"); 178 return 1; 179 } 180 } 181 182 mid = le64_to_cpu(shdr->MessageId); 183 if (check_smb2_hdr(shdr, mid)) 184 return 1; 185 186 if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) { 187 cifs_dbg(VFS, "Invalid structure size %u\n", 188 le16_to_cpu(shdr->StructureSize)); 189 return 1; 190 } 191 192 command = le16_to_cpu(shdr->Command); 193 if (command >= NUMBER_OF_SMB2_COMMANDS) { 194 cifs_dbg(VFS, "Invalid SMB2 command %d\n", command); 195 return 1; 196 } 197 198 if (len < pdu_size) { 199 if ((len >= hdr_size) 200 && (shdr->Status != 0)) { 201 pdu->StructureSize2 = 0; 202 /* 203 * As with SMB/CIFS, on some error cases servers may 204 * not return wct properly 205 */ 206 return 0; 207 } else { 208 cifs_dbg(VFS, "Length less than SMB header size\n"); 209 } 210 return 1; 211 } 212 if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE) { 213 cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n", 214 mid); 215 return 1; 216 } 217 218 if (smb2_rsp_struct_sizes[command] != pdu->StructureSize2) { 219 if (command != SMB2_OPLOCK_BREAK_HE && (shdr->Status == 0 || 220 pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2_LE)) { 221 /* error packets have 9 byte structure size */ 222 cifs_dbg(VFS, "Invalid response size %u for command %d\n", 223 le16_to_cpu(pdu->StructureSize2), command); 224 return 1; 225 } else if (command == SMB2_OPLOCK_BREAK_HE 226 && (shdr->Status == 0) 227 && (le16_to_cpu(pdu->StructureSize2) != 44) 228 && (le16_to_cpu(pdu->StructureSize2) != 36)) { 229 /* special case for SMB2.1 lease break message */ 230 cifs_dbg(VFS, "Invalid response size %d for oplock break\n", 231 le16_to_cpu(pdu->StructureSize2)); 232 return 1; 233 } 234 } 235 236 have_data = false; 237 data_area_overlap = false; 238 calc_len = __smb2_calc_size(buf, &have_data, &data_area_overlap); 239 240 /* Reject responses whose data area overlaps the fixed area. */ 241 if (data_area_overlap) 242 return 1; 243 244 /* For SMB2_IOCTL, OutputOffset and OutputLength are optional, so might 245 * be 0, and not a real miscalculation */ 246 if (command == SMB2_IOCTL_HE && calc_len == 0) 247 return 0; 248 249 if (command == SMB2_NEGOTIATE_HE) 250 calc_len += get_neg_ctxt_len(shdr, len, calc_len); 251 252 if (len != calc_len) { 253 /* create failed on symlink */ 254 if (command == SMB2_CREATE_HE && 255 shdr->Status == STATUS_STOPPED_ON_SYMLINK && 256 len > calc_len) 257 return 0; 258 /* Windows 7 server returns 24 bytes more */ 259 if (calc_len + 24 == len && command == SMB2_OPLOCK_BREAK_HE) 260 return 0; 261 /* 262 * Server can return one byte more due to implied bcc[0]. 263 * Allow it only when there is no data area; if data_length > 0 264 * the +1 gap indicates an overreported data length rather than 265 * the bcc[0] omission. 266 */ 267 if (calc_len == len + 1 && !have_data) 268 return 0; 269 270 /* 271 * Some windows servers (win2016) will pad also the final 272 * PDU in a compound to 8 bytes. 273 */ 274 if (ALIGN(calc_len, 8) == len) 275 return 0; 276 277 /* 278 * MacOS server pads after SMB2.1 write response with 3 bytes 279 * of junk. Other servers match RFC1001 len to actual 280 * SMB2/SMB3 frame length (header + smb2 response specific data) 281 * Some windows servers also pad up to 8 bytes when compounding. 282 */ 283 if (calc_len < len) 284 return 0; 285 286 /* Only log a message if len was really miscalculated */ 287 if (unlikely(cifsFYI)) 288 cifs_dbg(FYI, "Server response too short: calculated " 289 "length %u doesn't match read length %u (cmd=%d, mid=%llu)\n", 290 calc_len, len, command, mid); 291 else 292 pr_warn("Server response too short: calculated length " 293 "%u doesn't match read length %u (cmd=%d, mid=%llu)\n", 294 calc_len, len, command, mid); 295 296 return 1; 297 } 298 return 0; 299 } 300 301 /* 302 * The size of the variable area depends on the offset and length fields 303 * located in different fields for various SMB2 responses. SMB2 responses 304 * with no variable length info, show an offset of zero for the offset field. 305 */ 306 static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = { 307 /* SMB2_NEGOTIATE */ true, 308 /* SMB2_SESSION_SETUP */ true, 309 /* SMB2_LOGOFF */ false, 310 /* SMB2_TREE_CONNECT */ false, 311 /* SMB2_TREE_DISCONNECT */ false, 312 /* SMB2_CREATE */ true, 313 /* SMB2_CLOSE */ false, 314 /* SMB2_FLUSH */ false, 315 /* SMB2_READ */ true, 316 /* SMB2_WRITE */ false, 317 /* SMB2_LOCK */ false, 318 /* SMB2_IOCTL */ true, 319 /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */ 320 /* SMB2_ECHO */ false, 321 /* SMB2_QUERY_DIRECTORY */ true, 322 /* SMB2_CHANGE_NOTIFY */ true, 323 /* SMB2_QUERY_INFO */ true, 324 /* SMB2_SET_INFO */ false, 325 /* SMB2_OPLOCK_BREAK */ false 326 }; 327 328 /* 329 * Returns the pointer to the beginning of the data area. Length of the data 330 * area and the offset to it (from the beginning of the smb are also returned. 331 */ 332 char * 333 smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *shdr) 334 { 335 const int max_off = 4096; 336 const int max_len = 128 * 1024; 337 338 *off = 0; 339 *len = 0; 340 341 /* error responses do not have data area */ 342 if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED && 343 (((struct smb2_err_rsp *)shdr)->StructureSize) == 344 SMB2_ERROR_STRUCTURE_SIZE2_LE) 345 return NULL; 346 347 /* 348 * Following commands have data areas so we have to get the location 349 * of the data buffer offset and data buffer length for the particular 350 * command. 351 */ 352 switch (shdr->Command) { 353 case SMB2_NEGOTIATE: 354 *off = le16_to_cpu( 355 ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferOffset); 356 *len = le16_to_cpu( 357 ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferLength); 358 break; 359 case SMB2_SESSION_SETUP: 360 *off = le16_to_cpu( 361 ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferOffset); 362 *len = le16_to_cpu( 363 ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferLength); 364 break; 365 case SMB2_CREATE: 366 *off = le32_to_cpu( 367 ((struct smb2_create_rsp *)shdr)->CreateContextsOffset); 368 *len = le32_to_cpu( 369 ((struct smb2_create_rsp *)shdr)->CreateContextsLength); 370 break; 371 case SMB2_QUERY_INFO: 372 *off = le16_to_cpu( 373 ((struct smb2_query_info_rsp *)shdr)->OutputBufferOffset); 374 *len = le32_to_cpu( 375 ((struct smb2_query_info_rsp *)shdr)->OutputBufferLength); 376 break; 377 case SMB2_READ: 378 /* TODO: is this a bug ? */ 379 *off = ((struct smb2_read_rsp *)shdr)->DataOffset; 380 *len = le32_to_cpu(((struct smb2_read_rsp *)shdr)->DataLength); 381 break; 382 case SMB2_QUERY_DIRECTORY: 383 *off = le16_to_cpu( 384 ((struct smb2_query_directory_rsp *)shdr)->OutputBufferOffset); 385 *len = le32_to_cpu( 386 ((struct smb2_query_directory_rsp *)shdr)->OutputBufferLength); 387 break; 388 case SMB2_IOCTL: 389 *off = le32_to_cpu( 390 ((struct smb2_ioctl_rsp *)shdr)->OutputOffset); 391 *len = le32_to_cpu( 392 ((struct smb2_ioctl_rsp *)shdr)->OutputCount); 393 break; 394 case SMB2_CHANGE_NOTIFY: 395 *off = le16_to_cpu( 396 ((struct smb2_change_notify_rsp *)shdr)->OutputBufferOffset); 397 *len = le32_to_cpu( 398 ((struct smb2_change_notify_rsp *)shdr)->OutputBufferLength); 399 break; 400 default: 401 cifs_dbg(VFS, "no length check for command %d\n", le16_to_cpu(shdr->Command)); 402 break; 403 } 404 405 /* 406 * Invalid length or offset probably means data area is invalid, but 407 * we have little choice but to ignore the data area in this case. 408 */ 409 if (unlikely(*off < 0 || *off > max_off || 410 *len < 0 || *len > max_len)) { 411 cifs_dbg(VFS, "%s: invalid data area (off=%d len=%d)\n", 412 __func__, *off, *len); 413 *off = 0; 414 *len = 0; 415 } else if (*off == 0) { 416 *len = 0; 417 } 418 419 /* return pointer to beginning of data area, ie offset from SMB start */ 420 if (*off > 0 && *len > 0) 421 return (char *)shdr + *off; 422 return NULL; 423 } 424 425 /* 426 * Calculate the size of the SMB message based on the fixed header, fixed 427 * parameter area, and variable data area. 428 * 429 * If have_data is not NULL, it is set when a non-empty data area is found. 430 * If data_area_overlap is not NULL, it is set when the data area overlaps 431 * the fixed area. 432 */ 433 static unsigned int 434 __smb2_calc_size(void *buf, bool *have_data, bool *data_area_overlap) 435 { 436 struct smb2_pdu *pdu = buf; 437 struct smb2_hdr *shdr = &pdu->hdr; 438 int offset; /* the offset from the beginning of SMB to data area */ 439 int data_length = 0; /* the length of the variable length data area */ 440 /* Structure Size has already been checked to make sure it is 64 */ 441 int len = le16_to_cpu(shdr->StructureSize); 442 443 if (have_data) 444 *have_data = false; 445 if (data_area_overlap) 446 *data_area_overlap = false; 447 448 /* 449 * StructureSize2, ie length of fixed parameter area has already 450 * been checked to make sure it is the correct length. 451 */ 452 len += le16_to_cpu(pdu->StructureSize2); 453 454 if (has_smb2_data_area[le16_to_cpu(shdr->Command)] == false) 455 goto calc_size_exit; 456 457 smb2_get_data_area_len(&offset, &data_length, shdr); 458 cifs_dbg(FYI, "SMB2 data length %d offset %d\n", data_length, offset); 459 460 if (data_length > 0) { 461 /* 462 * Check to make sure that data area begins after fixed area, 463 * Note that last byte of the fixed area is part of data area 464 * for some commands, typically those with odd StructureSize, 465 * so we must add one to the calculation. 466 */ 467 if (offset + 1 < len) { 468 cifs_dbg(VFS, "data area offset %d overlaps SMB2 header %d\n", 469 offset + 1, len); 470 if (data_area_overlap) 471 *data_area_overlap = true; 472 data_length = 0; 473 goto calc_size_exit; 474 } else { 475 len = offset + data_length; 476 } 477 } 478 calc_size_exit: 479 cifs_dbg(FYI, "SMB2 len %d\n", len); 480 if (have_data) 481 *have_data = (data_length > 0); 482 return len; 483 } 484 485 unsigned int 486 smb2_calc_size(void *buf) 487 { 488 return __smb2_calc_size(buf, NULL, NULL); 489 } 490 491 /* Note: caller must free return buffer */ 492 __le16 * 493 cifs_convert_path_to_utf16(const char *from, struct cifs_sb_info *cifs_sb) 494 { 495 const char *start_of_path; 496 int len; 497 498 /* Windows doesn't allow paths beginning with \ */ 499 if (from[0] == '\\') 500 start_of_path = from + 1; 501 502 /* SMB311 POSIX extensions paths do not include leading slash */ 503 else if (cifs_sb_master_tlink(cifs_sb) && 504 cifs_sb_master_tcon(cifs_sb)->posix_extensions && 505 (from[0] == '/')) { 506 start_of_path = from + 1; 507 } else 508 start_of_path = from; 509 510 return cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len, 511 cifs_sb->local_nls, cifs_remap(cifs_sb)); 512 } 513 514 __le32 smb2_get_lease_state(struct cifsInodeInfo *cinode, unsigned int oplock) 515 { 516 unsigned int sbflags = cifs_sb_flags(CIFS_SB(cinode)); 517 __le32 lease = 0; 518 519 if ((oplock & CIFS_CACHE_WRITE_FLG) || (sbflags & CIFS_MOUNT_RW_CACHE)) 520 lease |= SMB2_LEASE_WRITE_CACHING_LE; 521 if (oplock & CIFS_CACHE_HANDLE_FLG) 522 lease |= SMB2_LEASE_HANDLE_CACHING_LE; 523 if ((oplock & CIFS_CACHE_READ_FLG) || (sbflags & CIFS_MOUNT_RO_CACHE)) 524 lease |= SMB2_LEASE_READ_CACHING_LE; 525 return lease; 526 } 527 528 struct smb2_lease_break_work { 529 struct work_struct lease_break; 530 struct tcon_link *tlink; 531 __u8 lease_key[16]; 532 __le32 lease_state; 533 }; 534 535 static void 536 cifs_ses_oplock_break(struct work_struct *work) 537 { 538 struct smb2_lease_break_work *lw = container_of(work, 539 struct smb2_lease_break_work, lease_break); 540 int rc = 0; 541 542 rc = SMB2_lease_break(0, tlink_tcon(lw->tlink), lw->lease_key, 543 lw->lease_state); 544 545 cifs_dbg(FYI, "Lease release rc %d\n", rc); 546 cifs_put_tlink(lw->tlink); 547 kfree(lw); 548 } 549 550 static void 551 smb2_queue_pending_open_break(struct tcon_link *tlink, __u8 *lease_key, 552 __le32 new_lease_state) 553 { 554 struct smb2_lease_break_work *lw; 555 556 lw = kmalloc_obj(struct smb2_lease_break_work); 557 if (!lw) { 558 cifs_put_tlink(tlink); 559 return; 560 } 561 562 INIT_WORK(&lw->lease_break, cifs_ses_oplock_break); 563 lw->tlink = tlink; 564 lw->lease_state = new_lease_state; 565 memcpy(lw->lease_key, lease_key, SMB2_LEASE_KEY_SIZE); 566 queue_work(cifsiod_wq, &lw->lease_break); 567 } 568 569 static bool 570 smb2_tcon_has_lease(struct cifs_tcon *tcon, struct smb2_lease_break *rsp) 571 { 572 __u8 lease_state; 573 struct cifsFileInfo *cfile; 574 struct cifsInodeInfo *cinode; 575 int ack_req = le32_to_cpu(rsp->Flags & 576 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED); 577 578 lease_state = le32_to_cpu(rsp->NewLeaseState); 579 580 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 581 cinode = CIFS_I(d_inode(cfile->dentry)); 582 583 if (memcmp(cinode->lease_key, rsp->LeaseKey, 584 SMB2_LEASE_KEY_SIZE)) 585 continue; 586 587 cifs_dbg(FYI, "found in the open list\n"); 588 cifs_dbg(FYI, "lease key match, lease break 0x%x\n", 589 lease_state); 590 591 if (ack_req) 592 cfile->oplock_break_cancelled = false; 593 else 594 cfile->oplock_break_cancelled = true; 595 596 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags); 597 598 cfile->oplock_epoch = le16_to_cpu(rsp->Epoch); 599 cfile->oplock_level = lease_state; 600 601 cifs_queue_oplock_break(cfile); 602 return true; 603 } 604 605 return false; 606 } 607 608 static struct cifs_pending_open * 609 smb2_tcon_find_pending_open_lease(struct cifs_tcon *tcon, 610 struct smb2_lease_break *rsp) 611 { 612 __u8 lease_state = le32_to_cpu(rsp->NewLeaseState); 613 int ack_req = le32_to_cpu(rsp->Flags & 614 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED); 615 struct cifs_pending_open *open; 616 struct cifs_pending_open *found = NULL; 617 618 list_for_each_entry(open, &tcon->pending_opens, olist) { 619 if (memcmp(open->lease_key, rsp->LeaseKey, 620 SMB2_LEASE_KEY_SIZE)) 621 continue; 622 623 if (!found && ack_req) { 624 found = open; 625 } 626 627 cifs_dbg(FYI, "found in the pending open list\n"); 628 cifs_dbg(FYI, "lease key match, lease break 0x%x\n", 629 lease_state); 630 631 open->oplock = lease_state; 632 } 633 634 return found; 635 } 636 637 static bool 638 smb2_is_valid_lease_break(char *buffer, struct TCP_Server_Info *server) 639 { 640 struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer; 641 struct TCP_Server_Info *pserver; 642 struct cifs_ses *ses; 643 struct cifs_tcon *tcon; 644 struct cifs_pending_open *open; 645 646 /* Trace receipt of lease break request from server */ 647 trace_smb3_lease_break_enter(le32_to_cpu(rsp->CurrentLeaseState), 648 le32_to_cpu(rsp->Flags), 649 le16_to_cpu(rsp->Epoch), 650 le32_to_cpu(rsp->hdr.Id.SyncId.TreeId), 651 le64_to_cpu(rsp->hdr.SessionId), 652 *((u64 *)rsp->LeaseKey), 653 *((u64 *)&rsp->LeaseKey[8])); 654 655 cifs_dbg(FYI, "Checking for lease break\n"); 656 657 /* If server is a channel, select the primary channel */ 658 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 659 660 /* look up tcon based on tid & uid */ 661 spin_lock(&cifs_tcp_ses_lock); 662 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 663 if (cifs_ses_exiting(ses)) 664 continue; 665 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 666 spin_lock(&tcon->open_file_lock); 667 cifs_stats_inc( 668 &tcon->stats.cifs_stats.num_oplock_brks); 669 if (smb2_tcon_has_lease(tcon, rsp)) { 670 spin_unlock(&tcon->open_file_lock); 671 spin_unlock(&cifs_tcp_ses_lock); 672 return true; 673 } 674 open = smb2_tcon_find_pending_open_lease(tcon, 675 rsp); 676 if (open) { 677 __u8 lease_key[SMB2_LEASE_KEY_SIZE]; 678 struct tcon_link *tlink; 679 680 tlink = cifs_get_tlink(open->tlink); 681 memcpy(lease_key, open->lease_key, 682 SMB2_LEASE_KEY_SIZE); 683 spin_unlock(&tcon->open_file_lock); 684 spin_unlock(&cifs_tcp_ses_lock); 685 smb2_queue_pending_open_break(tlink, 686 lease_key, 687 rsp->NewLeaseState); 688 return true; 689 } 690 spin_unlock(&tcon->open_file_lock); 691 692 if (cached_dir_lease_break(tcon, rsp->LeaseKey)) { 693 spin_unlock(&cifs_tcp_ses_lock); 694 return true; 695 } 696 } 697 } 698 spin_unlock(&cifs_tcp_ses_lock); 699 cifs_dbg(FYI, "Can not process lease break - no lease matched\n"); 700 trace_smb3_lease_not_found(le32_to_cpu(rsp->CurrentLeaseState), 701 le32_to_cpu(rsp->Flags), 702 le16_to_cpu(rsp->Epoch), 703 le32_to_cpu(rsp->hdr.Id.SyncId.TreeId), 704 le64_to_cpu(rsp->hdr.SessionId), 705 *((u64 *)rsp->LeaseKey), 706 *((u64 *)&rsp->LeaseKey[8])); 707 708 return false; 709 } 710 711 bool 712 smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server) 713 { 714 struct smb2_oplock_break *rsp = (struct smb2_oplock_break *)buffer; 715 struct TCP_Server_Info *pserver; 716 struct cifs_ses *ses; 717 struct cifs_tcon *tcon; 718 struct cifsInodeInfo *cinode; 719 struct cifsFileInfo *cfile; 720 721 cifs_dbg(FYI, "Checking for oplock break\n"); 722 723 if (rsp->hdr.Command != SMB2_OPLOCK_BREAK) 724 return false; 725 726 if (rsp->StructureSize != 727 smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) { 728 if (le16_to_cpu(rsp->StructureSize) == 44) 729 return smb2_is_valid_lease_break(buffer, server); 730 else 731 return false; 732 } 733 734 cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel); 735 736 /* If server is a channel, select the primary channel */ 737 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 738 739 /* look up tcon based on tid & uid */ 740 spin_lock(&cifs_tcp_ses_lock); 741 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 742 if (cifs_ses_exiting(ses)) 743 continue; 744 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 745 746 spin_lock(&tcon->open_file_lock); 747 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 748 if (rsp->PersistentFid != 749 cfile->fid.persistent_fid || 750 rsp->VolatileFid != 751 cfile->fid.volatile_fid) 752 continue; 753 754 cifs_dbg(FYI, "file id match, oplock break\n"); 755 cifs_stats_inc( 756 &tcon->stats.cifs_stats.num_oplock_brks); 757 cinode = CIFS_I(d_inode(cfile->dentry)); 758 spin_lock(&cfile->file_info_lock); 759 if (!CIFS_CACHE_WRITE(cinode) && 760 rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE) 761 cfile->oplock_break_cancelled = true; 762 else 763 cfile->oplock_break_cancelled = false; 764 765 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, 766 &cinode->flags); 767 768 cfile->oplock_epoch = 0; 769 cfile->oplock_level = rsp->OplockLevel; 770 771 spin_unlock(&cfile->file_info_lock); 772 773 cifs_queue_oplock_break(cfile); 774 775 spin_unlock(&tcon->open_file_lock); 776 spin_unlock(&cifs_tcp_ses_lock); 777 return true; 778 } 779 spin_unlock(&tcon->open_file_lock); 780 } 781 } 782 spin_unlock(&cifs_tcp_ses_lock); 783 cifs_dbg(FYI, "No file id matched, oplock break ignored\n"); 784 trace_smb3_oplock_not_found(0 /* no xid */, rsp->PersistentFid, 785 le32_to_cpu(rsp->hdr.Id.SyncId.TreeId), 786 le64_to_cpu(rsp->hdr.SessionId)); 787 788 return true; 789 } 790 791 void 792 smb2_cancelled_close_fid(struct work_struct *work) 793 { 794 struct close_cancelled_open *cancelled = container_of(work, 795 struct close_cancelled_open, work); 796 struct cifs_tcon *tcon = cancelled->tcon; 797 int rc; 798 799 if (cancelled->mid) 800 cifs_tcon_dbg(VFS, "Close unmatched open for MID:%llu\n", 801 cancelled->mid); 802 else 803 cifs_tcon_dbg(VFS, "Close interrupted close\n"); 804 805 rc = SMB2_close(0, tcon, cancelled->fid.persistent_fid, 806 cancelled->fid.volatile_fid); 807 if (rc) 808 cifs_tcon_dbg(VFS, "Close cancelled mid failed rc:%d\n", rc); 809 810 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cancelled_close_fid); 811 kfree(cancelled); 812 } 813 814 /* 815 * Caller should already has an extra reference to @tcon 816 * This function is used to queue work to close a handle to prevent leaks 817 * on the server. 818 * We handle two cases. If an open was interrupted after we sent the 819 * SMB2_CREATE to the server but before we processed the reply, and second 820 * if a close was interrupted before we sent the SMB2_CLOSE to the server. 821 */ 822 static int 823 __smb2_handle_cancelled_cmd(struct cifs_tcon *tcon, __u16 cmd, __u64 mid, 824 __u64 persistent_fid, __u64 volatile_fid) 825 { 826 struct close_cancelled_open *cancelled; 827 828 cancelled = kzalloc_obj(*cancelled); 829 if (!cancelled) 830 return -ENOMEM; 831 832 cancelled->fid.persistent_fid = persistent_fid; 833 cancelled->fid.volatile_fid = volatile_fid; 834 cancelled->tcon = tcon; 835 cancelled->cmd = cmd; 836 cancelled->mid = mid; 837 INIT_WORK(&cancelled->work, smb2_cancelled_close_fid); 838 WARN_ON(queue_work(cifsiod_wq, &cancelled->work) == false); 839 840 return 0; 841 } 842 843 int 844 smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid, 845 __u64 volatile_fid) 846 { 847 int rc; 848 849 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); 850 spin_lock(&tcon->tc_lock); 851 if (tcon->tc_count <= 0) { 852 struct TCP_Server_Info *server = NULL; 853 854 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 855 netfs_trace_tcon_ref_see_cancelled_close); 856 WARN_ONCE(tcon->tc_count < 0, "tcon refcount is negative"); 857 spin_unlock(&tcon->tc_lock); 858 859 if (tcon->ses) { 860 server = tcon->ses->server; 861 cifs_server_dbg(FYI, 862 "tid=0x%x: tcon is closing, skipping async close retry of fid %llu %llu\n", 863 tcon->tid, persistent_fid, volatile_fid); 864 } 865 866 return 0; 867 } 868 tcon->tc_count++; 869 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 870 netfs_trace_tcon_ref_get_cancelled_close); 871 spin_unlock(&tcon->tc_lock); 872 873 rc = __smb2_handle_cancelled_cmd(tcon, SMB2_CLOSE_HE, 0, 874 persistent_fid, volatile_fid); 875 if (rc) 876 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cancelled_close); 877 878 return rc; 879 } 880 881 int 882 smb2_handle_cancelled_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server) 883 { 884 struct smb2_hdr *hdr = mid->resp_buf; 885 struct smb2_create_rsp *rsp = mid->resp_buf; 886 struct cifs_tcon *tcon; 887 int rc; 888 889 if ((mid->optype & CIFS_CP_CREATE_CLOSE_OP) || hdr->Command != SMB2_CREATE || 890 hdr->Status != STATUS_SUCCESS) 891 return 0; 892 893 tcon = smb2_find_smb_tcon(server, le64_to_cpu(hdr->SessionId), 894 le32_to_cpu(hdr->Id.SyncId.TreeId)); 895 if (!tcon) 896 return -ENOENT; 897 898 rc = __smb2_handle_cancelled_cmd(tcon, 899 le16_to_cpu(hdr->Command), 900 le64_to_cpu(hdr->MessageId), 901 rsp->PersistentFileId, 902 rsp->VolatileFileId); 903 if (rc) 904 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cancelled_mid); 905 906 return rc; 907 } 908 909 /** 910 * smb311_update_preauth_hash - update @ses hash with the packet data in @iov 911 * 912 * Assumes @iov does not contain the rfc1002 length and iov[0] has the 913 * SMB2 header. 914 * 915 * @ses: server session structure 916 * @server: pointer to server info 917 * @iov: array containing the SMB request we will send to the server 918 * @nvec: number of array entries for the iov 919 */ 920 void 921 smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server, 922 struct kvec *iov, int nvec) 923 { 924 int i; 925 struct smb2_hdr *hdr; 926 struct sha512_ctx sha_ctx; 927 928 hdr = (struct smb2_hdr *)iov[0].iov_base; 929 /* neg prot are always taken */ 930 if (hdr->Command == SMB2_NEGOTIATE) 931 goto ok; 932 933 /* 934 * If we process a command which wasn't a negprot it means the 935 * neg prot was already done, so the server dialect was set 936 * and we can test it. Preauth requires 3.1.1 for now. 937 */ 938 if (server->dialect != SMB311_PROT_ID) 939 return; 940 941 if (hdr->Command != SMB2_SESSION_SETUP) 942 return; 943 944 /* skip last sess setup response */ 945 if ((hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) 946 && (hdr->Status == NT_STATUS_OK 947 || (hdr->Status != 948 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED)))) 949 return; 950 951 ok: 952 sha512_init(&sha_ctx); 953 sha512_update(&sha_ctx, ses->preauth_sha_hash, SMB2_PREAUTH_HASH_SIZE); 954 for (i = 0; i < nvec; i++) 955 sha512_update(&sha_ctx, iov[i].iov_base, iov[i].iov_len); 956 sha512_final(&sha_ctx, ses->preauth_sha_hash); 957 } 958