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