1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * SMB/CIFS session setup handling routines 5 * 6 * Copyright (c) International Business Machines Corp., 2006, 2009 7 * Author(s): Steve French (sfrench@us.ibm.com) 8 * 9 */ 10 11 #include "cifspdu.h" 12 #include "cifsglob.h" 13 #include "cifsproto.h" 14 #include "cifs_unicode.h" 15 #include "cifs_debug.h" 16 #include "ntlmssp.h" 17 #include "nterr.h" 18 #include <linux/utsname.h> 19 #include <linux/slab.h> 20 #include <linux/version.h> 21 #include "cifsfs.h" 22 #include "cifs_spnego.h" 23 #include "smb2proto.h" 24 #include "fs_context.h" 25 26 static int 27 cifs_ses_add_channel(struct cifs_ses *ses, 28 struct cifs_server_iface *iface); 29 30 bool 31 is_server_using_iface(struct TCP_Server_Info *server, 32 struct cifs_server_iface *iface) 33 { 34 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr; 35 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr; 36 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr; 37 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr; 38 39 if (server->dstaddr.ss_family != iface->sockaddr.ss_family) 40 return false; 41 if (server->dstaddr.ss_family == AF_INET) { 42 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr) 43 return false; 44 } else if (server->dstaddr.ss_family == AF_INET6) { 45 if (memcmp(&s6->sin6_addr, &i6->sin6_addr, 46 sizeof(i6->sin6_addr)) != 0) 47 return false; 48 } else { 49 /* unknown family.. */ 50 return false; 51 } 52 return true; 53 } 54 55 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface) 56 { 57 int i; 58 59 spin_lock(&ses->chan_lock); 60 for (i = 0; i < ses->chan_count; i++) { 61 if (ses->chans[i].iface == iface) { 62 spin_unlock(&ses->chan_lock); 63 return true; 64 } 65 } 66 spin_unlock(&ses->chan_lock); 67 return false; 68 } 69 70 /* channel helper functions. assumed that chan_lock is held by caller. */ 71 72 int 73 cifs_ses_get_chan_index(struct cifs_ses *ses, 74 struct TCP_Server_Info *server) 75 { 76 unsigned int i; 77 78 /* if the channel is waiting for termination */ 79 if (server && server->terminate) 80 return CIFS_INVAL_CHAN_INDEX; 81 82 for (i = 0; i < ses->chan_count; i++) { 83 if (ses->chans[i].server == server) 84 return i; 85 } 86 87 /* If we didn't find the channel, it is likely a bug */ 88 if (server) 89 cifs_dbg(VFS, "unable to get chan index for server: 0x%llx", 90 server->conn_id); 91 return CIFS_INVAL_CHAN_INDEX; 92 } 93 94 void 95 cifs_chan_set_in_reconnect(struct cifs_ses *ses, 96 struct TCP_Server_Info *server) 97 { 98 int chan_index = cifs_ses_get_chan_index(ses, server); 99 100 if (chan_index == CIFS_INVAL_CHAN_INDEX) 101 return; 102 103 ses->chans[chan_index].in_reconnect = true; 104 } 105 106 void 107 cifs_chan_clear_in_reconnect(struct cifs_ses *ses, 108 struct TCP_Server_Info *server) 109 { 110 unsigned int chan_index = cifs_ses_get_chan_index(ses, server); 111 112 if (chan_index == CIFS_INVAL_CHAN_INDEX) 113 return; 114 115 ses->chans[chan_index].in_reconnect = false; 116 } 117 118 void 119 cifs_chan_set_need_reconnect(struct cifs_ses *ses, 120 struct TCP_Server_Info *server) 121 { 122 unsigned int chan_index = cifs_ses_get_chan_index(ses, server); 123 124 if (chan_index == CIFS_INVAL_CHAN_INDEX) 125 return; 126 127 set_bit(chan_index, &ses->chans_need_reconnect); 128 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n", 129 chan_index, ses->chans_need_reconnect); 130 } 131 132 void 133 cifs_chan_clear_need_reconnect(struct cifs_ses *ses, 134 struct TCP_Server_Info *server) 135 { 136 unsigned int chan_index = cifs_ses_get_chan_index(ses, server); 137 138 if (chan_index == CIFS_INVAL_CHAN_INDEX) 139 return; 140 141 clear_bit(chan_index, &ses->chans_need_reconnect); 142 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n", 143 chan_index, ses->chans_need_reconnect); 144 } 145 146 bool 147 cifs_chan_needs_reconnect(struct cifs_ses *ses, 148 struct TCP_Server_Info *server) 149 { 150 unsigned int chan_index = cifs_ses_get_chan_index(ses, server); 151 152 if (chan_index == CIFS_INVAL_CHAN_INDEX) 153 return true; /* err on the safer side */ 154 155 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index); 156 } 157 158 bool 159 cifs_chan_is_iface_active(struct cifs_ses *ses, 160 struct TCP_Server_Info *server) 161 { 162 unsigned int chan_index = cifs_ses_get_chan_index(ses, server); 163 164 if (chan_index == CIFS_INVAL_CHAN_INDEX) 165 return true; /* err on the safer side */ 166 167 return ses->chans[chan_index].iface && 168 ses->chans[chan_index].iface->is_active; 169 } 170 171 /* returns number of channels added */ 172 int cifs_try_adding_channels(struct cifs_ses *ses) 173 { 174 struct TCP_Server_Info *server = ses->server; 175 int old_chan_count, new_chan_count; 176 int left; 177 int rc = 0; 178 int tries = 0; 179 size_t iface_weight = 0, iface_min_speed = 0; 180 struct cifs_server_iface *iface = NULL, *niface = NULL; 181 struct cifs_server_iface *last_iface = NULL; 182 183 spin_lock(&ses->chan_lock); 184 185 new_chan_count = old_chan_count = ses->chan_count; 186 left = ses->chan_max - ses->chan_count; 187 188 if (left <= 0) { 189 spin_unlock(&ses->chan_lock); 190 cifs_dbg(FYI, 191 "ses already at max_channels (%zu), nothing to open\n", 192 ses->chan_max); 193 return 0; 194 } 195 196 if (server->dialect < SMB30_PROT_ID) { 197 spin_unlock(&ses->chan_lock); 198 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n"); 199 return 0; 200 } 201 202 if (!(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) { 203 spin_unlock(&ses->chan_lock); 204 cifs_server_dbg(VFS, "no multichannel support\n"); 205 return 0; 206 } 207 spin_unlock(&ses->chan_lock); 208 209 while (left > 0) { 210 211 tries++; 212 if (tries > 3*ses->chan_max) { 213 cifs_dbg(VFS, "too many channel open attempts (%d channels left to open)\n", 214 left); 215 break; 216 } 217 218 spin_lock(&ses->iface_lock); 219 if (!ses->iface_count) { 220 spin_unlock(&ses->iface_lock); 221 cifs_dbg(ONCE, "server %s does not advertise interfaces\n", 222 ses->server->hostname); 223 break; 224 } 225 226 if (!iface) 227 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface, 228 iface_head); 229 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface, 230 iface_head); 231 iface_min_speed = last_iface->speed; 232 233 list_for_each_entry_safe_from(iface, niface, &ses->iface_list, 234 iface_head) { 235 /* do not mix rdma and non-rdma interfaces */ 236 if (iface->rdma_capable != ses->server->rdma) 237 continue; 238 239 /* skip ifaces that are unusable */ 240 if (!iface->is_active || 241 (is_ses_using_iface(ses, iface) && 242 !iface->rss_capable)) 243 continue; 244 245 /* check if we already allocated enough channels */ 246 iface_weight = iface->speed / iface_min_speed; 247 248 if (iface->weight_fulfilled >= iface_weight) 249 continue; 250 251 /* take ref before unlock */ 252 kref_get(&iface->refcount); 253 254 spin_unlock(&ses->iface_lock); 255 rc = cifs_ses_add_channel(ses, iface); 256 spin_lock(&ses->iface_lock); 257 258 if (rc) { 259 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n", 260 &iface->sockaddr, 261 rc); 262 kref_put(&iface->refcount, release_iface); 263 /* failure to add chan should increase weight */ 264 iface->weight_fulfilled++; 265 continue; 266 } 267 268 iface->num_channels++; 269 iface->weight_fulfilled++; 270 cifs_dbg(VFS, "successfully opened new channel on iface:%pIS\n", 271 &iface->sockaddr); 272 break; 273 } 274 275 /* reached end of list. reset weight_fulfilled and start over */ 276 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) { 277 list_for_each_entry(iface, &ses->iface_list, iface_head) 278 iface->weight_fulfilled = 0; 279 spin_unlock(&ses->iface_lock); 280 iface = NULL; 281 continue; 282 } 283 spin_unlock(&ses->iface_lock); 284 285 left--; 286 new_chan_count++; 287 } 288 289 return new_chan_count - old_chan_count; 290 } 291 292 /* 293 * called when multichannel is disabled by the server. 294 * this always gets called from smb2_reconnect 295 * and cannot get called in parallel threads. 296 */ 297 void 298 cifs_disable_secondary_channels(struct cifs_ses *ses) 299 { 300 int i, chan_count; 301 struct TCP_Server_Info *server; 302 struct cifs_server_iface *iface; 303 304 spin_lock(&ses->chan_lock); 305 chan_count = ses->chan_count; 306 if (chan_count == 1) 307 goto done; 308 309 ses->chan_count = 1; 310 311 /* for all secondary channels reset the need reconnect bit */ 312 ses->chans_need_reconnect &= 1; 313 314 for (i = 1; i < chan_count; i++) { 315 iface = ses->chans[i].iface; 316 server = ses->chans[i].server; 317 318 /* 319 * remove these references first, since we need to unlock 320 * the chan_lock here, since iface_lock is a higher lock 321 */ 322 ses->chans[i].iface = NULL; 323 ses->chans[i].server = NULL; 324 spin_unlock(&ses->chan_lock); 325 326 if (iface) { 327 spin_lock(&ses->iface_lock); 328 iface->num_channels--; 329 if (iface->weight_fulfilled) 330 iface->weight_fulfilled--; 331 kref_put(&iface->refcount, release_iface); 332 spin_unlock(&ses->iface_lock); 333 } 334 335 if (server) { 336 if (!server->terminate) { 337 server->terminate = true; 338 cifs_signal_cifsd_for_reconnect(server, false); 339 } 340 cifs_put_tcp_session(server, false); 341 } 342 343 spin_lock(&ses->chan_lock); 344 } 345 346 done: 347 spin_unlock(&ses->chan_lock); 348 } 349 350 /* update the iface for the channel if necessary. */ 351 void 352 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server) 353 { 354 unsigned int chan_index; 355 size_t iface_weight = 0, iface_min_speed = 0; 356 struct cifs_server_iface *iface = NULL; 357 struct cifs_server_iface *old_iface = NULL; 358 struct cifs_server_iface *last_iface = NULL; 359 struct sockaddr_storage ss; 360 361 spin_lock(&ses->chan_lock); 362 chan_index = cifs_ses_get_chan_index(ses, server); 363 if (chan_index == CIFS_INVAL_CHAN_INDEX) { 364 spin_unlock(&ses->chan_lock); 365 return; 366 } 367 368 if (ses->chans[chan_index].iface) { 369 old_iface = ses->chans[chan_index].iface; 370 if (old_iface->is_active) { 371 spin_unlock(&ses->chan_lock); 372 return; 373 } 374 } 375 spin_unlock(&ses->chan_lock); 376 377 spin_lock(&server->srv_lock); 378 ss = server->dstaddr; 379 spin_unlock(&server->srv_lock); 380 381 spin_lock(&ses->iface_lock); 382 if (!ses->iface_count) { 383 spin_unlock(&ses->iface_lock); 384 cifs_dbg(ONCE, "server %s does not advertise interfaces\n", ses->server->hostname); 385 return; 386 } 387 388 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface, 389 iface_head); 390 iface_min_speed = last_iface->speed; 391 392 /* then look for a new one */ 393 list_for_each_entry(iface, &ses->iface_list, iface_head) { 394 if (!chan_index) { 395 /* if we're trying to get the updated iface for primary channel */ 396 if (!cifs_match_ipaddr((struct sockaddr *) &ss, 397 (struct sockaddr *) &iface->sockaddr)) 398 continue; 399 400 kref_get(&iface->refcount); 401 break; 402 } 403 404 /* do not mix rdma and non-rdma interfaces */ 405 if (iface->rdma_capable != server->rdma) 406 continue; 407 408 if (!iface->is_active || 409 (is_ses_using_iface(ses, iface) && 410 !iface->rss_capable)) { 411 continue; 412 } 413 414 /* check if we already allocated enough channels */ 415 iface_weight = iface->speed / iface_min_speed; 416 417 if (iface->weight_fulfilled >= iface_weight) 418 continue; 419 420 kref_get(&iface->refcount); 421 break; 422 } 423 424 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) { 425 iface = NULL; 426 cifs_dbg(FYI, "unable to find a suitable iface\n"); 427 } 428 429 if (!iface) { 430 if (!chan_index) 431 cifs_dbg(FYI, "unable to get the interface matching: %pIS\n", 432 &ss); 433 else { 434 cifs_dbg(FYI, "unable to find another interface to replace: %pIS\n", 435 &old_iface->sockaddr); 436 } 437 438 spin_unlock(&ses->iface_lock); 439 return; 440 } 441 442 /* now drop the ref to the current iface */ 443 if (old_iface) { 444 cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n", 445 &old_iface->sockaddr, 446 &iface->sockaddr); 447 448 old_iface->num_channels--; 449 if (old_iface->weight_fulfilled) 450 old_iface->weight_fulfilled--; 451 iface->num_channels++; 452 iface->weight_fulfilled++; 453 454 kref_put(&old_iface->refcount, release_iface); 455 } else if (!chan_index) { 456 /* special case: update interface for primary channel */ 457 cifs_dbg(FYI, "referencing primary channel iface: %pIS\n", 458 &iface->sockaddr); 459 iface->num_channels++; 460 iface->weight_fulfilled++; 461 } 462 spin_unlock(&ses->iface_lock); 463 464 spin_lock(&ses->chan_lock); 465 chan_index = cifs_ses_get_chan_index(ses, server); 466 if (chan_index == CIFS_INVAL_CHAN_INDEX) { 467 spin_unlock(&ses->chan_lock); 468 return; 469 } 470 471 ses->chans[chan_index].iface = iface; 472 spin_unlock(&ses->chan_lock); 473 } 474 475 static int 476 cifs_ses_add_channel(struct cifs_ses *ses, 477 struct cifs_server_iface *iface) 478 { 479 struct TCP_Server_Info *chan_server; 480 struct cifs_chan *chan; 481 struct smb3_fs_context *ctx; 482 static const char unc_fmt[] = "\\%s\\foo"; 483 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr; 484 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr; 485 size_t len; 486 int rc; 487 unsigned int xid = get_xid(); 488 489 if (iface->sockaddr.ss_family == AF_INET) 490 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n", 491 ses, iface->speed, str_yes_no(iface->rdma_capable), 492 &ipv4->sin_addr); 493 else 494 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n", 495 ses, iface->speed, str_yes_no(iface->rdma_capable), 496 &ipv6->sin6_addr); 497 498 /* 499 * Setup a ctx with mostly the same info as the existing 500 * session and overwrite it with the requested iface data. 501 * 502 * We need to setup at least the fields used for negprot and 503 * sesssetup. 504 * 505 * We only need the ctx here, so we can reuse memory from 506 * the session and server without caring about memory 507 * management. 508 */ 509 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 510 if (!ctx) { 511 rc = -ENOMEM; 512 goto out_free_xid; 513 } 514 515 /* Always make new connection for now (TODO?) */ 516 ctx->nosharesock = true; 517 518 /* Auth */ 519 ctx->domainauto = ses->domainAuto; 520 ctx->domainname = ses->domainName; 521 522 /* no hostname for extra channels */ 523 ctx->server_hostname = ""; 524 525 ctx->username = ses->user_name; 526 ctx->password = ses->password; 527 ctx->sectype = ses->sectype; 528 ctx->sign = ses->sign; 529 530 /* UNC and paths */ 531 /* XXX: Use ses->server->hostname? */ 532 len = sizeof(unc_fmt) + SERVER_NAME_LEN_WITH_NULL; 533 ctx->UNC = kzalloc(len, GFP_KERNEL); 534 if (!ctx->UNC) { 535 rc = -ENOMEM; 536 goto out_free_ctx; 537 } 538 scnprintf(ctx->UNC, len, unc_fmt, ses->ip_addr); 539 ctx->prepath = ""; 540 541 /* Reuse same version as master connection */ 542 ctx->vals = ses->server->vals; 543 ctx->ops = ses->server->ops; 544 545 ctx->noblocksnd = ses->server->noblocksnd; 546 ctx->noautotune = ses->server->noautotune; 547 ctx->sockopt_tcp_nodelay = ses->server->tcp_nodelay; 548 ctx->echo_interval = ses->server->echo_interval / HZ; 549 ctx->max_credits = ses->server->max_credits; 550 551 /* 552 * This will be used for encoding/decoding user/domain/pw 553 * during sess setup auth. 554 */ 555 ctx->local_nls = ses->local_nls; 556 557 /* Use RDMA if possible */ 558 ctx->rdma = iface->rdma_capable; 559 memcpy(&ctx->dstaddr, &iface->sockaddr, sizeof(ctx->dstaddr)); 560 561 /* reuse master con client guid */ 562 memcpy(&ctx->client_guid, ses->server->client_guid, 563 sizeof(ctx->client_guid)); 564 ctx->use_client_guid = true; 565 566 chan_server = cifs_get_tcp_session(ctx, ses->server); 567 568 spin_lock(&ses->chan_lock); 569 chan = &ses->chans[ses->chan_count]; 570 chan->server = chan_server; 571 if (IS_ERR(chan->server)) { 572 rc = PTR_ERR(chan->server); 573 chan->server = NULL; 574 spin_unlock(&ses->chan_lock); 575 goto out; 576 } 577 chan->iface = iface; 578 ses->chan_count++; 579 atomic_set(&ses->chan_seq, 0); 580 581 /* Mark this channel as needing connect/setup */ 582 cifs_chan_set_need_reconnect(ses, chan->server); 583 584 spin_unlock(&ses->chan_lock); 585 586 mutex_lock(&ses->session_mutex); 587 /* 588 * We need to allocate the server crypto now as we will need 589 * to sign packets before we generate the channel signing key 590 * (we sign with the session key) 591 */ 592 rc = smb311_crypto_shash_allocate(chan->server); 593 if (rc) { 594 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__); 595 mutex_unlock(&ses->session_mutex); 596 goto out; 597 } 598 599 rc = cifs_negotiate_protocol(xid, ses, chan->server); 600 if (!rc) 601 rc = cifs_setup_session(xid, ses, chan->server, ses->local_nls); 602 603 mutex_unlock(&ses->session_mutex); 604 605 out: 606 if (rc && chan->server) { 607 cifs_put_tcp_session(chan->server, 0); 608 609 spin_lock(&ses->chan_lock); 610 611 /* we rely on all bits beyond chan_count to be clear */ 612 cifs_chan_clear_need_reconnect(ses, chan->server); 613 ses->chan_count--; 614 /* 615 * chan_count should never reach 0 as at least the primary 616 * channel is always allocated 617 */ 618 WARN_ON(ses->chan_count < 1); 619 spin_unlock(&ses->chan_lock); 620 } 621 622 kfree(ctx->UNC); 623 out_free_ctx: 624 kfree(ctx); 625 out_free_xid: 626 free_xid(xid); 627 return rc; 628 } 629 630 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 631 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, 632 struct TCP_Server_Info *server, 633 SESSION_SETUP_ANDX *pSMB) 634 { 635 __u32 capabilities = 0; 636 637 /* init fields common to all four types of SessSetup */ 638 /* Note that offsets for first seven fields in req struct are same */ 639 /* in CIFS Specs so does not matter which of 3 forms of struct */ 640 /* that we use in next few lines */ 641 /* Note that header is initialized to zero in header_assemble */ 642 pSMB->req.AndXCommand = 0xFF; 643 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32, 644 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4, 645 USHRT_MAX)); 646 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq); 647 pSMB->req.VcNumber = cpu_to_le16(1); 648 649 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */ 650 651 /* BB verify whether signing required on neg or just auth frame (and NTLM case) */ 652 653 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | 654 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X; 655 656 if (server->sign) 657 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; 658 659 if (ses->capabilities & CAP_UNICODE) { 660 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE; 661 capabilities |= CAP_UNICODE; 662 } 663 if (ses->capabilities & CAP_STATUS32) { 664 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS; 665 capabilities |= CAP_STATUS32; 666 } 667 if (ses->capabilities & CAP_DFS) { 668 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS; 669 capabilities |= CAP_DFS; 670 } 671 if (ses->capabilities & CAP_UNIX) 672 capabilities |= CAP_UNIX; 673 674 return capabilities; 675 } 676 677 static void 678 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp) 679 { 680 char *bcc_ptr = *pbcc_area; 681 int bytes_ret = 0; 682 683 /* Copy OS version */ 684 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32, 685 nls_cp); 686 bcc_ptr += 2 * bytes_ret; 687 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release, 688 32, nls_cp); 689 bcc_ptr += 2 * bytes_ret; 690 bcc_ptr += 2; /* trailing null */ 691 692 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS, 693 32, nls_cp); 694 bcc_ptr += 2 * bytes_ret; 695 bcc_ptr += 2; /* trailing null */ 696 697 *pbcc_area = bcc_ptr; 698 } 699 700 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses, 701 const struct nls_table *nls_cp) 702 { 703 char *bcc_ptr = *pbcc_area; 704 int bytes_ret = 0; 705 706 /* copy domain */ 707 if (ses->domainName == NULL) { 708 /* 709 * Sending null domain better than using a bogus domain name (as 710 * we did briefly in 2.6.18) since server will use its default 711 */ 712 *bcc_ptr = 0; 713 *(bcc_ptr+1) = 0; 714 bytes_ret = 0; 715 } else 716 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName, 717 CIFS_MAX_DOMAINNAME_LEN, nls_cp); 718 bcc_ptr += 2 * bytes_ret; 719 bcc_ptr += 2; /* account for null terminator */ 720 721 *pbcc_area = bcc_ptr; 722 } 723 724 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses, 725 const struct nls_table *nls_cp) 726 { 727 char *bcc_ptr = *pbcc_area; 728 int bytes_ret = 0; 729 730 /* BB FIXME add check that strings less than 335 or will need to send as arrays */ 731 732 /* copy user */ 733 if (ses->user_name == NULL) { 734 /* null user mount */ 735 *bcc_ptr = 0; 736 *(bcc_ptr+1) = 0; 737 } else { 738 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name, 739 CIFS_MAX_USERNAME_LEN, nls_cp); 740 } 741 bcc_ptr += 2 * bytes_ret; 742 bcc_ptr += 2; /* account for null termination */ 743 744 unicode_domain_string(&bcc_ptr, ses, nls_cp); 745 unicode_oslm_strings(&bcc_ptr, nls_cp); 746 747 *pbcc_area = bcc_ptr; 748 } 749 750 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses, 751 const struct nls_table *nls_cp) 752 { 753 char *bcc_ptr = *pbcc_area; 754 int len; 755 756 /* copy user */ 757 /* BB what about null user mounts - check that we do this BB */ 758 /* copy user */ 759 if (ses->user_name != NULL) { 760 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN); 761 if (WARN_ON_ONCE(len < 0)) 762 len = CIFS_MAX_USERNAME_LEN - 1; 763 bcc_ptr += len; 764 } 765 /* else null user mount */ 766 *bcc_ptr = 0; 767 bcc_ptr++; /* account for null termination */ 768 769 /* copy domain */ 770 if (ses->domainName != NULL) { 771 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN); 772 if (WARN_ON_ONCE(len < 0)) 773 len = CIFS_MAX_DOMAINNAME_LEN - 1; 774 bcc_ptr += len; 775 } /* else we send a null domain name so server will default to its own domain */ 776 *bcc_ptr = 0; 777 bcc_ptr++; 778 779 /* BB check for overflow here */ 780 781 strcpy(bcc_ptr, "Linux version "); 782 bcc_ptr += strlen("Linux version "); 783 strcpy(bcc_ptr, init_utsname()->release); 784 bcc_ptr += strlen(init_utsname()->release) + 1; 785 786 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS); 787 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1; 788 789 *pbcc_area = bcc_ptr; 790 } 791 792 static void 793 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses, 794 const struct nls_table *nls_cp) 795 { 796 int len; 797 char *data = *pbcc_area; 798 799 cifs_dbg(FYI, "bleft %d\n", bleft); 800 801 kfree(ses->serverOS); 802 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp); 803 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS); 804 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2; 805 data += len; 806 bleft -= len; 807 if (bleft <= 0) 808 return; 809 810 kfree(ses->serverNOS); 811 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp); 812 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS); 813 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2; 814 data += len; 815 bleft -= len; 816 if (bleft <= 0) 817 return; 818 819 kfree(ses->serverDomain); 820 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp); 821 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain); 822 823 return; 824 } 825 826 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft, 827 struct cifs_ses *ses, 828 const struct nls_table *nls_cp) 829 { 830 int len; 831 char *bcc_ptr = *pbcc_area; 832 833 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft); 834 835 len = strnlen(bcc_ptr, bleft); 836 if (len >= bleft) 837 return; 838 839 kfree(ses->serverOS); 840 841 ses->serverOS = kmalloc(len + 1, GFP_KERNEL); 842 if (ses->serverOS) { 843 memcpy(ses->serverOS, bcc_ptr, len); 844 ses->serverOS[len] = 0; 845 if (strncmp(ses->serverOS, "OS/2", 4) == 0) 846 cifs_dbg(FYI, "OS/2 server\n"); 847 } 848 849 bcc_ptr += len + 1; 850 bleft -= len + 1; 851 852 len = strnlen(bcc_ptr, bleft); 853 if (len >= bleft) 854 return; 855 856 kfree(ses->serverNOS); 857 858 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL); 859 if (ses->serverNOS) { 860 memcpy(ses->serverNOS, bcc_ptr, len); 861 ses->serverNOS[len] = 0; 862 } 863 864 bcc_ptr += len + 1; 865 bleft -= len + 1; 866 867 len = strnlen(bcc_ptr, bleft); 868 if (len > bleft) 869 return; 870 871 /* 872 * No domain field in LANMAN case. Domain is 873 * returned by old servers in the SMB negprot response 874 * 875 * BB For newer servers which do not support Unicode, 876 * but thus do return domain here, we could add parsing 877 * for it later, but it is not very important 878 */ 879 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft); 880 } 881 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 882 883 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len, 884 struct cifs_ses *ses) 885 { 886 unsigned int tioffset; /* challenge message target info area */ 887 unsigned int tilen; /* challenge message target info area length */ 888 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr; 889 __u32 server_flags; 890 891 if (blob_len < sizeof(CHALLENGE_MESSAGE)) { 892 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len); 893 return -EINVAL; 894 } 895 896 if (memcmp(pblob->Signature, "NTLMSSP", 8)) { 897 cifs_dbg(VFS, "blob signature incorrect %s\n", 898 pblob->Signature); 899 return -EINVAL; 900 } 901 if (pblob->MessageType != NtLmChallenge) { 902 cifs_dbg(VFS, "Incorrect message type %d\n", 903 pblob->MessageType); 904 return -EINVAL; 905 } 906 907 server_flags = le32_to_cpu(pblob->NegotiateFlags); 908 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__, 909 ses->ntlmssp->client_flags, server_flags); 910 911 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) && 912 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) { 913 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n", 914 __func__); 915 return -EINVAL; 916 } 917 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) { 918 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__); 919 return -EINVAL; 920 } 921 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) { 922 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n", 923 __func__); 924 return -EOPNOTSUPP; 925 } 926 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) && 927 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH)) 928 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n", 929 __func__); 930 931 ses->ntlmssp->server_flags = server_flags; 932 933 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE); 934 /* 935 * In particular we can examine sign flags 936 * 937 * BB spec says that if AvId field of MsvAvTimestamp is populated then 938 * we must set the MIC field of the AUTHENTICATE_MESSAGE 939 */ 940 941 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset); 942 tilen = le16_to_cpu(pblob->TargetInfoArray.Length); 943 if (tioffset > blob_len || tioffset + tilen > blob_len) { 944 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n", 945 tioffset, tilen); 946 return -EINVAL; 947 } 948 if (tilen) { 949 kfree_sensitive(ses->auth_key.response); 950 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen, 951 GFP_KERNEL); 952 if (!ses->auth_key.response) { 953 cifs_dbg(VFS, "Challenge target info alloc failure\n"); 954 return -ENOMEM; 955 } 956 ses->auth_key.len = tilen; 957 } 958 959 return 0; 960 } 961 962 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size) 963 { 964 int sz = base_size + ses->auth_key.len 965 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2; 966 967 if (ses->domainName) 968 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN); 969 else 970 sz += sizeof(__le16); 971 972 if (ses->user_name) 973 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN); 974 else 975 sz += sizeof(__le16); 976 977 if (ses->workstation_name[0]) 978 sz += sizeof(__le16) * strnlen(ses->workstation_name, 979 ntlmssp_workstation_name_size(ses)); 980 else 981 sz += sizeof(__le16); 982 983 return sz; 984 } 985 986 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf, 987 char *str_value, 988 int str_length, 989 unsigned char *pstart, 990 unsigned char **pcur, 991 const struct nls_table *nls_cp) 992 { 993 unsigned char *tmp = pstart; 994 int len; 995 996 if (!pbuf) 997 return; 998 999 if (!pcur) 1000 pcur = &tmp; 1001 1002 if (!str_value) { 1003 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart); 1004 pbuf->Length = 0; 1005 pbuf->MaximumLength = 0; 1006 *pcur += sizeof(__le16); 1007 } else { 1008 len = cifs_strtoUTF16((__le16 *)*pcur, 1009 str_value, 1010 str_length, 1011 nls_cp); 1012 len *= sizeof(__le16); 1013 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart); 1014 pbuf->Length = cpu_to_le16(len); 1015 pbuf->MaximumLength = cpu_to_le16(len); 1016 *pcur += len; 1017 } 1018 } 1019 1020 /* BB Move to ntlmssp.c eventually */ 1021 1022 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer, 1023 u16 *buflen, 1024 struct cifs_ses *ses, 1025 struct TCP_Server_Info *server, 1026 const struct nls_table *nls_cp) 1027 { 1028 int rc = 0; 1029 NEGOTIATE_MESSAGE *sec_blob; 1030 __u32 flags; 1031 unsigned char *tmp; 1032 int len; 1033 1034 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE)); 1035 *pbuffer = kmalloc(len, GFP_KERNEL); 1036 if (!*pbuffer) { 1037 rc = -ENOMEM; 1038 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc); 1039 *buflen = 0; 1040 goto setup_ntlm_neg_ret; 1041 } 1042 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer; 1043 1044 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE)); 1045 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8); 1046 sec_blob->MessageType = NtLmNegotiate; 1047 1048 /* BB is NTLMV2 session security format easier to use here? */ 1049 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET | 1050 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE | 1051 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC | 1052 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL | 1053 NTLMSSP_NEGOTIATE_SIGN; 1054 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess) 1055 flags |= NTLMSSP_NEGOTIATE_KEY_XCH; 1056 1057 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE); 1058 ses->ntlmssp->client_flags = flags; 1059 sec_blob->NegotiateFlags = cpu_to_le32(flags); 1060 1061 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */ 1062 cifs_security_buffer_from_str(&sec_blob->DomainName, 1063 NULL, 1064 CIFS_MAX_DOMAINNAME_LEN, 1065 *pbuffer, &tmp, 1066 nls_cp); 1067 1068 cifs_security_buffer_from_str(&sec_blob->WorkstationName, 1069 NULL, 1070 CIFS_MAX_WORKSTATION_LEN, 1071 *pbuffer, &tmp, 1072 nls_cp); 1073 1074 *buflen = tmp - *pbuffer; 1075 setup_ntlm_neg_ret: 1076 return rc; 1077 } 1078 1079 /* 1080 * Build ntlmssp blob with additional fields, such as version, 1081 * supported by modern servers. For safety limit to SMB3 or later 1082 * See notes in MS-NLMP Section 2.2.2.1 e.g. 1083 */ 1084 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer, 1085 u16 *buflen, 1086 struct cifs_ses *ses, 1087 struct TCP_Server_Info *server, 1088 const struct nls_table *nls_cp) 1089 { 1090 int rc = 0; 1091 struct negotiate_message *sec_blob; 1092 __u32 flags; 1093 unsigned char *tmp; 1094 int len; 1095 1096 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message)); 1097 *pbuffer = kmalloc(len, GFP_KERNEL); 1098 if (!*pbuffer) { 1099 rc = -ENOMEM; 1100 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc); 1101 *buflen = 0; 1102 goto setup_ntlm_smb3_neg_ret; 1103 } 1104 sec_blob = (struct negotiate_message *)*pbuffer; 1105 1106 memset(*pbuffer, 0, sizeof(struct negotiate_message)); 1107 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8); 1108 sec_blob->MessageType = NtLmNegotiate; 1109 1110 /* BB is NTLMV2 session security format easier to use here? */ 1111 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET | 1112 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE | 1113 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC | 1114 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL | 1115 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION; 1116 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess) 1117 flags |= NTLMSSP_NEGOTIATE_KEY_XCH; 1118 1119 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR; 1120 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL; 1121 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD); 1122 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3; 1123 1124 tmp = *pbuffer + sizeof(struct negotiate_message); 1125 ses->ntlmssp->client_flags = flags; 1126 sec_blob->NegotiateFlags = cpu_to_le32(flags); 1127 1128 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */ 1129 cifs_security_buffer_from_str(&sec_blob->DomainName, 1130 NULL, 1131 CIFS_MAX_DOMAINNAME_LEN, 1132 *pbuffer, &tmp, 1133 nls_cp); 1134 1135 cifs_security_buffer_from_str(&sec_blob->WorkstationName, 1136 NULL, 1137 CIFS_MAX_WORKSTATION_LEN, 1138 *pbuffer, &tmp, 1139 nls_cp); 1140 1141 *buflen = tmp - *pbuffer; 1142 setup_ntlm_smb3_neg_ret: 1143 return rc; 1144 } 1145 1146 1147 /* See MS-NLMP 2.2.1.3 */ 1148 int build_ntlmssp_auth_blob(unsigned char **pbuffer, 1149 u16 *buflen, 1150 struct cifs_ses *ses, 1151 struct TCP_Server_Info *server, 1152 const struct nls_table *nls_cp) 1153 { 1154 int rc; 1155 AUTHENTICATE_MESSAGE *sec_blob; 1156 __u32 flags; 1157 unsigned char *tmp; 1158 int len; 1159 1160 rc = setup_ntlmv2_rsp(ses, nls_cp); 1161 if (rc) { 1162 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc); 1163 *buflen = 0; 1164 goto setup_ntlmv2_ret; 1165 } 1166 1167 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE)); 1168 *pbuffer = kmalloc(len, GFP_KERNEL); 1169 if (!*pbuffer) { 1170 rc = -ENOMEM; 1171 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc); 1172 *buflen = 0; 1173 goto setup_ntlmv2_ret; 1174 } 1175 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer; 1176 1177 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8); 1178 sec_blob->MessageType = NtLmAuthenticate; 1179 1180 /* send version information in ntlmssp authenticate also */ 1181 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET | 1182 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_VERSION | 1183 NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED; 1184 1185 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR; 1186 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL; 1187 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD); 1188 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3; 1189 1190 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE); 1191 sec_blob->NegotiateFlags = cpu_to_le32(flags); 1192 1193 sec_blob->LmChallengeResponse.BufferOffset = 1194 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE)); 1195 sec_blob->LmChallengeResponse.Length = 0; 1196 sec_blob->LmChallengeResponse.MaximumLength = 0; 1197 1198 sec_blob->NtChallengeResponse.BufferOffset = 1199 cpu_to_le32(tmp - *pbuffer); 1200 if (ses->user_name != NULL) { 1201 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE, 1202 ses->auth_key.len - CIFS_SESS_KEY_SIZE); 1203 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE; 1204 1205 sec_blob->NtChallengeResponse.Length = 1206 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE); 1207 sec_blob->NtChallengeResponse.MaximumLength = 1208 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE); 1209 } else { 1210 /* 1211 * don't send an NT Response for anonymous access 1212 */ 1213 sec_blob->NtChallengeResponse.Length = 0; 1214 sec_blob->NtChallengeResponse.MaximumLength = 0; 1215 } 1216 1217 cifs_security_buffer_from_str(&sec_blob->DomainName, 1218 ses->domainName, 1219 CIFS_MAX_DOMAINNAME_LEN, 1220 *pbuffer, &tmp, 1221 nls_cp); 1222 1223 cifs_security_buffer_from_str(&sec_blob->UserName, 1224 ses->user_name, 1225 CIFS_MAX_USERNAME_LEN, 1226 *pbuffer, &tmp, 1227 nls_cp); 1228 1229 cifs_security_buffer_from_str(&sec_blob->WorkstationName, 1230 ses->workstation_name, 1231 ntlmssp_workstation_name_size(ses), 1232 *pbuffer, &tmp, 1233 nls_cp); 1234 1235 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) && 1236 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) && 1237 !calc_seckey(ses)) { 1238 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE); 1239 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer); 1240 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE); 1241 sec_blob->SessionKey.MaximumLength = 1242 cpu_to_le16(CIFS_CPHTXT_SIZE); 1243 tmp += CIFS_CPHTXT_SIZE; 1244 } else { 1245 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer); 1246 sec_blob->SessionKey.Length = 0; 1247 sec_blob->SessionKey.MaximumLength = 0; 1248 } 1249 1250 *buflen = tmp - *pbuffer; 1251 setup_ntlmv2_ret: 1252 return rc; 1253 } 1254 1255 enum securityEnum 1256 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) 1257 { 1258 switch (server->negflavor) { 1259 case CIFS_NEGFLAVOR_EXTENDED: 1260 switch (requested) { 1261 case Kerberos: 1262 case RawNTLMSSP: 1263 return requested; 1264 case Unspecified: 1265 if (server->sec_ntlmssp && 1266 (global_secflags & CIFSSEC_MAY_NTLMSSP)) 1267 return RawNTLMSSP; 1268 if ((server->sec_kerberos || server->sec_mskerberos) && 1269 (global_secflags & CIFSSEC_MAY_KRB5)) 1270 return Kerberos; 1271 fallthrough; 1272 default: 1273 return Unspecified; 1274 } 1275 case CIFS_NEGFLAVOR_UNENCAP: 1276 switch (requested) { 1277 case NTLMv2: 1278 return requested; 1279 case Unspecified: 1280 if (global_secflags & CIFSSEC_MAY_NTLMV2) 1281 return NTLMv2; 1282 break; 1283 default: 1284 break; 1285 } 1286 fallthrough; 1287 default: 1288 return Unspecified; 1289 } 1290 } 1291 1292 struct sess_data { 1293 unsigned int xid; 1294 struct cifs_ses *ses; 1295 struct TCP_Server_Info *server; 1296 struct nls_table *nls_cp; 1297 void (*func)(struct sess_data *); 1298 int result; 1299 1300 /* we will send the SMB in three pieces: 1301 * a fixed length beginning part, an optional 1302 * SPNEGO blob (which can be zero length), and a 1303 * last part which will include the strings 1304 * and rest of bcc area. This allows us to avoid 1305 * a large buffer 17K allocation 1306 */ 1307 int buf0_type; 1308 struct kvec iov[3]; 1309 }; 1310 1311 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1312 static int 1313 sess_alloc_buffer(struct sess_data *sess_data, int wct) 1314 { 1315 int rc; 1316 struct cifs_ses *ses = sess_data->ses; 1317 struct smb_hdr *smb_buf; 1318 1319 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses, 1320 (void **)&smb_buf); 1321 1322 if (rc) 1323 return rc; 1324 1325 sess_data->iov[0].iov_base = (char *)smb_buf; 1326 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4; 1327 /* 1328 * This variable will be used to clear the buffer 1329 * allocated above in case of any error in the calling function. 1330 */ 1331 sess_data->buf0_type = CIFS_SMALL_BUFFER; 1332 1333 /* 2000 big enough to fit max user, domain, NOS name etc. */ 1334 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL); 1335 if (!sess_data->iov[2].iov_base) { 1336 rc = -ENOMEM; 1337 goto out_free_smb_buf; 1338 } 1339 1340 return 0; 1341 1342 out_free_smb_buf: 1343 cifs_small_buf_release(smb_buf); 1344 sess_data->iov[0].iov_base = NULL; 1345 sess_data->iov[0].iov_len = 0; 1346 sess_data->buf0_type = CIFS_NO_BUFFER; 1347 return rc; 1348 } 1349 1350 static void 1351 sess_free_buffer(struct sess_data *sess_data) 1352 { 1353 struct kvec *iov = sess_data->iov; 1354 1355 /* 1356 * Zero the session data before freeing, as it might contain sensitive info (keys, etc). 1357 * Note that iov[1] is already freed by caller. 1358 */ 1359 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base) 1360 memzero_explicit(iov[0].iov_base, iov[0].iov_len); 1361 1362 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base); 1363 sess_data->buf0_type = CIFS_NO_BUFFER; 1364 kfree_sensitive(iov[2].iov_base); 1365 } 1366 1367 static int 1368 sess_establish_session(struct sess_data *sess_data) 1369 { 1370 struct cifs_ses *ses = sess_data->ses; 1371 struct TCP_Server_Info *server = sess_data->server; 1372 1373 cifs_server_lock(server); 1374 if (!server->session_estab) { 1375 if (server->sign) { 1376 server->session_key.response = 1377 kmemdup(ses->auth_key.response, 1378 ses->auth_key.len, GFP_KERNEL); 1379 if (!server->session_key.response) { 1380 cifs_server_unlock(server); 1381 return -ENOMEM; 1382 } 1383 server->session_key.len = 1384 ses->auth_key.len; 1385 } 1386 server->sequence_number = 0x2; 1387 server->session_estab = true; 1388 } 1389 cifs_server_unlock(server); 1390 1391 cifs_dbg(FYI, "CIFS session established successfully\n"); 1392 return 0; 1393 } 1394 1395 static int 1396 sess_sendreceive(struct sess_data *sess_data) 1397 { 1398 int rc; 1399 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base; 1400 __u16 count; 1401 struct kvec rsp_iov = { NULL, 0 }; 1402 1403 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len; 1404 be32_add_cpu(&smb_buf->smb_buf_length, count); 1405 put_bcc(count, smb_buf); 1406 1407 rc = SendReceive2(sess_data->xid, sess_data->ses, 1408 sess_data->iov, 3 /* num_iovecs */, 1409 &sess_data->buf0_type, 1410 CIFS_LOG_ERROR, &rsp_iov); 1411 cifs_small_buf_release(sess_data->iov[0].iov_base); 1412 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); 1413 1414 return rc; 1415 } 1416 1417 static void 1418 sess_auth_ntlmv2(struct sess_data *sess_data) 1419 { 1420 int rc = 0; 1421 struct smb_hdr *smb_buf; 1422 SESSION_SETUP_ANDX *pSMB; 1423 char *bcc_ptr; 1424 struct cifs_ses *ses = sess_data->ses; 1425 struct TCP_Server_Info *server = sess_data->server; 1426 __u32 capabilities; 1427 __u16 bytes_remaining; 1428 1429 /* old style NTLM sessionsetup */ 1430 /* wct = 13 */ 1431 rc = sess_alloc_buffer(sess_data, 13); 1432 if (rc) 1433 goto out; 1434 1435 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1436 bcc_ptr = sess_data->iov[2].iov_base; 1437 capabilities = cifs_ssetup_hdr(ses, server, pSMB); 1438 1439 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities); 1440 1441 /* LM2 password would be here if we supported it */ 1442 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0; 1443 1444 if (ses->user_name != NULL) { 1445 /* calculate nlmv2 response and session key */ 1446 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp); 1447 if (rc) { 1448 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc); 1449 goto out; 1450 } 1451 1452 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE, 1453 ses->auth_key.len - CIFS_SESS_KEY_SIZE); 1454 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE; 1455 1456 /* set case sensitive password length after tilen may get 1457 * assigned, tilen is 0 otherwise. 1458 */ 1459 pSMB->req_no_secext.CaseSensitivePasswordLength = 1460 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE); 1461 } else { 1462 pSMB->req_no_secext.CaseSensitivePasswordLength = 0; 1463 } 1464 1465 if (ses->capabilities & CAP_UNICODE) { 1466 if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) { 1467 *bcc_ptr = 0; 1468 bcc_ptr++; 1469 } 1470 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp); 1471 } else { 1472 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp); 1473 } 1474 1475 1476 sess_data->iov[2].iov_len = (long) bcc_ptr - 1477 (long) sess_data->iov[2].iov_base; 1478 1479 rc = sess_sendreceive(sess_data); 1480 if (rc) 1481 goto out; 1482 1483 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1484 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; 1485 1486 if (smb_buf->WordCount != 3) { 1487 rc = -EIO; 1488 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); 1489 goto out; 1490 } 1491 1492 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN) 1493 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */ 1494 1495 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ 1496 cifs_dbg(FYI, "UID = %llu\n", ses->Suid); 1497 1498 bytes_remaining = get_bcc(smb_buf); 1499 bcc_ptr = pByteArea(smb_buf); 1500 1501 /* BB check if Unicode and decode strings */ 1502 if (bytes_remaining == 0) { 1503 /* no string area to decode, do nothing */ 1504 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) { 1505 /* unicode string area must be word-aligned */ 1506 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) { 1507 ++bcc_ptr; 1508 --bytes_remaining; 1509 } 1510 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, 1511 sess_data->nls_cp); 1512 } else { 1513 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses, 1514 sess_data->nls_cp); 1515 } 1516 1517 rc = sess_establish_session(sess_data); 1518 out: 1519 sess_data->result = rc; 1520 sess_data->func = NULL; 1521 sess_free_buffer(sess_data); 1522 kfree_sensitive(ses->auth_key.response); 1523 ses->auth_key.response = NULL; 1524 } 1525 1526 #ifdef CONFIG_CIFS_UPCALL 1527 static void 1528 sess_auth_kerberos(struct sess_data *sess_data) 1529 { 1530 int rc = 0; 1531 struct smb_hdr *smb_buf; 1532 SESSION_SETUP_ANDX *pSMB; 1533 char *bcc_ptr; 1534 struct cifs_ses *ses = sess_data->ses; 1535 struct TCP_Server_Info *server = sess_data->server; 1536 __u32 capabilities; 1537 __u16 bytes_remaining; 1538 struct key *spnego_key = NULL; 1539 struct cifs_spnego_msg *msg; 1540 u16 blob_len; 1541 1542 /* extended security */ 1543 /* wct = 12 */ 1544 rc = sess_alloc_buffer(sess_data, 12); 1545 if (rc) 1546 goto out; 1547 1548 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1549 bcc_ptr = sess_data->iov[2].iov_base; 1550 capabilities = cifs_ssetup_hdr(ses, server, pSMB); 1551 1552 spnego_key = cifs_get_spnego_key(ses, server); 1553 if (IS_ERR(spnego_key)) { 1554 rc = PTR_ERR(spnego_key); 1555 spnego_key = NULL; 1556 goto out; 1557 } 1558 1559 msg = spnego_key->payload.data[0]; 1560 /* 1561 * check version field to make sure that cifs.upcall is 1562 * sending us a response in an expected form 1563 */ 1564 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { 1565 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n", 1566 CIFS_SPNEGO_UPCALL_VERSION, msg->version); 1567 rc = -EKEYREJECTED; 1568 goto out_put_spnego_key; 1569 } 1570 1571 kfree_sensitive(ses->auth_key.response); 1572 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, 1573 GFP_KERNEL); 1574 if (!ses->auth_key.response) { 1575 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n", 1576 msg->sesskey_len); 1577 rc = -ENOMEM; 1578 goto out_put_spnego_key; 1579 } 1580 ses->auth_key.len = msg->sesskey_len; 1581 1582 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC; 1583 capabilities |= CAP_EXTENDED_SECURITY; 1584 pSMB->req.Capabilities = cpu_to_le32(capabilities); 1585 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; 1586 sess_data->iov[1].iov_len = msg->secblob_len; 1587 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len); 1588 1589 if (ses->capabilities & CAP_UNICODE) { 1590 /* unicode strings must be word aligned */ 1591 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) { 1592 *bcc_ptr = 0; 1593 bcc_ptr++; 1594 } 1595 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp); 1596 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp); 1597 } else { 1598 /* BB: is this right? */ 1599 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp); 1600 } 1601 1602 sess_data->iov[2].iov_len = (long) bcc_ptr - 1603 (long) sess_data->iov[2].iov_base; 1604 1605 rc = sess_sendreceive(sess_data); 1606 if (rc) 1607 goto out_put_spnego_key; 1608 1609 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1610 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; 1611 1612 if (smb_buf->WordCount != 4) { 1613 rc = -EIO; 1614 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); 1615 goto out_put_spnego_key; 1616 } 1617 1618 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN) 1619 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */ 1620 1621 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ 1622 cifs_dbg(FYI, "UID = %llu\n", ses->Suid); 1623 1624 bytes_remaining = get_bcc(smb_buf); 1625 bcc_ptr = pByteArea(smb_buf); 1626 1627 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); 1628 if (blob_len > bytes_remaining) { 1629 cifs_dbg(VFS, "bad security blob length %d\n", 1630 blob_len); 1631 rc = -EINVAL; 1632 goto out_put_spnego_key; 1633 } 1634 bcc_ptr += blob_len; 1635 bytes_remaining -= blob_len; 1636 1637 /* BB check if Unicode and decode strings */ 1638 if (bytes_remaining == 0) { 1639 /* no string area to decode, do nothing */ 1640 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) { 1641 /* unicode string area must be word-aligned */ 1642 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) { 1643 ++bcc_ptr; 1644 --bytes_remaining; 1645 } 1646 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, 1647 sess_data->nls_cp); 1648 } else { 1649 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses, 1650 sess_data->nls_cp); 1651 } 1652 1653 rc = sess_establish_session(sess_data); 1654 out_put_spnego_key: 1655 key_invalidate(spnego_key); 1656 key_put(spnego_key); 1657 out: 1658 sess_data->result = rc; 1659 sess_data->func = NULL; 1660 sess_free_buffer(sess_data); 1661 kfree_sensitive(ses->auth_key.response); 1662 ses->auth_key.response = NULL; 1663 } 1664 1665 #endif /* ! CONFIG_CIFS_UPCALL */ 1666 1667 /* 1668 * The required kvec buffers have to be allocated before calling this 1669 * function. 1670 */ 1671 static int 1672 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data) 1673 { 1674 SESSION_SETUP_ANDX *pSMB; 1675 struct cifs_ses *ses = sess_data->ses; 1676 struct TCP_Server_Info *server = sess_data->server; 1677 __u32 capabilities; 1678 char *bcc_ptr; 1679 1680 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1681 1682 capabilities = cifs_ssetup_hdr(ses, server, pSMB); 1683 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) { 1684 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n"); 1685 return -ENOSYS; 1686 } 1687 1688 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC; 1689 capabilities |= CAP_EXTENDED_SECURITY; 1690 pSMB->req.Capabilities |= cpu_to_le32(capabilities); 1691 1692 bcc_ptr = sess_data->iov[2].iov_base; 1693 /* unicode strings must be word aligned */ 1694 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) { 1695 *bcc_ptr = 0; 1696 bcc_ptr++; 1697 } 1698 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp); 1699 1700 sess_data->iov[2].iov_len = (long) bcc_ptr - 1701 (long) sess_data->iov[2].iov_base; 1702 1703 return 0; 1704 } 1705 1706 static void 1707 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data); 1708 1709 static void 1710 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data) 1711 { 1712 int rc; 1713 struct smb_hdr *smb_buf; 1714 SESSION_SETUP_ANDX *pSMB; 1715 struct cifs_ses *ses = sess_data->ses; 1716 struct TCP_Server_Info *server = sess_data->server; 1717 __u16 bytes_remaining; 1718 char *bcc_ptr; 1719 unsigned char *ntlmsspblob = NULL; 1720 u16 blob_len; 1721 1722 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n"); 1723 1724 /* 1725 * if memory allocation is successful, caller of this function 1726 * frees it. 1727 */ 1728 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); 1729 if (!ses->ntlmssp) { 1730 rc = -ENOMEM; 1731 goto out; 1732 } 1733 ses->ntlmssp->sesskey_per_smbsess = false; 1734 1735 /* wct = 12 */ 1736 rc = sess_alloc_buffer(sess_data, 12); 1737 if (rc) 1738 goto out; 1739 1740 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1741 1742 /* Build security blob before we assemble the request */ 1743 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob, 1744 &blob_len, ses, server, 1745 sess_data->nls_cp); 1746 if (rc) 1747 goto out_free_ntlmsspblob; 1748 1749 sess_data->iov[1].iov_len = blob_len; 1750 sess_data->iov[1].iov_base = ntlmsspblob; 1751 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len); 1752 1753 rc = _sess_auth_rawntlmssp_assemble_req(sess_data); 1754 if (rc) 1755 goto out_free_ntlmsspblob; 1756 1757 rc = sess_sendreceive(sess_data); 1758 1759 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1760 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; 1761 1762 /* If true, rc here is expected and not an error */ 1763 if (sess_data->buf0_type != CIFS_NO_BUFFER && 1764 smb_buf->Status.CifsError == 1765 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED)) 1766 rc = 0; 1767 1768 if (rc) 1769 goto out_free_ntlmsspblob; 1770 1771 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); 1772 1773 if (smb_buf->WordCount != 4) { 1774 rc = -EIO; 1775 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); 1776 goto out_free_ntlmsspblob; 1777 } 1778 1779 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ 1780 cifs_dbg(FYI, "UID = %llu\n", ses->Suid); 1781 1782 bytes_remaining = get_bcc(smb_buf); 1783 bcc_ptr = pByteArea(smb_buf); 1784 1785 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); 1786 if (blob_len > bytes_remaining) { 1787 cifs_dbg(VFS, "bad security blob length %d\n", 1788 blob_len); 1789 rc = -EINVAL; 1790 goto out_free_ntlmsspblob; 1791 } 1792 1793 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses); 1794 1795 out_free_ntlmsspblob: 1796 kfree_sensitive(ntlmsspblob); 1797 out: 1798 sess_free_buffer(sess_data); 1799 1800 if (!rc) { 1801 sess_data->func = sess_auth_rawntlmssp_authenticate; 1802 return; 1803 } 1804 1805 /* Else error. Cleanup */ 1806 kfree_sensitive(ses->auth_key.response); 1807 ses->auth_key.response = NULL; 1808 kfree_sensitive(ses->ntlmssp); 1809 ses->ntlmssp = NULL; 1810 1811 sess_data->func = NULL; 1812 sess_data->result = rc; 1813 } 1814 1815 static void 1816 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data) 1817 { 1818 int rc; 1819 struct smb_hdr *smb_buf; 1820 SESSION_SETUP_ANDX *pSMB; 1821 struct cifs_ses *ses = sess_data->ses; 1822 struct TCP_Server_Info *server = sess_data->server; 1823 __u16 bytes_remaining; 1824 char *bcc_ptr; 1825 unsigned char *ntlmsspblob = NULL; 1826 u16 blob_len; 1827 1828 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n"); 1829 1830 /* wct = 12 */ 1831 rc = sess_alloc_buffer(sess_data, 12); 1832 if (rc) 1833 goto out; 1834 1835 /* Build security blob before we assemble the request */ 1836 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1837 smb_buf = (struct smb_hdr *)pSMB; 1838 rc = build_ntlmssp_auth_blob(&ntlmsspblob, 1839 &blob_len, ses, server, 1840 sess_data->nls_cp); 1841 if (rc) 1842 goto out_free_ntlmsspblob; 1843 sess_data->iov[1].iov_len = blob_len; 1844 sess_data->iov[1].iov_base = ntlmsspblob; 1845 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len); 1846 /* 1847 * Make sure that we tell the server that we are using 1848 * the uid that it just gave us back on the response 1849 * (challenge) 1850 */ 1851 smb_buf->Uid = ses->Suid; 1852 1853 rc = _sess_auth_rawntlmssp_assemble_req(sess_data); 1854 if (rc) 1855 goto out_free_ntlmsspblob; 1856 1857 rc = sess_sendreceive(sess_data); 1858 if (rc) 1859 goto out_free_ntlmsspblob; 1860 1861 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; 1862 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; 1863 if (smb_buf->WordCount != 4) { 1864 rc = -EIO; 1865 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); 1866 goto out_free_ntlmsspblob; 1867 } 1868 1869 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN) 1870 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */ 1871 1872 if (ses->Suid != smb_buf->Uid) { 1873 ses->Suid = smb_buf->Uid; 1874 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid); 1875 } 1876 1877 bytes_remaining = get_bcc(smb_buf); 1878 bcc_ptr = pByteArea(smb_buf); 1879 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); 1880 if (blob_len > bytes_remaining) { 1881 cifs_dbg(VFS, "bad security blob length %d\n", 1882 blob_len); 1883 rc = -EINVAL; 1884 goto out_free_ntlmsspblob; 1885 } 1886 bcc_ptr += blob_len; 1887 bytes_remaining -= blob_len; 1888 1889 1890 /* BB check if Unicode and decode strings */ 1891 if (bytes_remaining == 0) { 1892 /* no string area to decode, do nothing */ 1893 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) { 1894 /* unicode string area must be word-aligned */ 1895 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) { 1896 ++bcc_ptr; 1897 --bytes_remaining; 1898 } 1899 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, 1900 sess_data->nls_cp); 1901 } else { 1902 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses, 1903 sess_data->nls_cp); 1904 } 1905 1906 out_free_ntlmsspblob: 1907 kfree_sensitive(ntlmsspblob); 1908 out: 1909 sess_free_buffer(sess_data); 1910 1911 if (!rc) 1912 rc = sess_establish_session(sess_data); 1913 1914 /* Cleanup */ 1915 kfree_sensitive(ses->auth_key.response); 1916 ses->auth_key.response = NULL; 1917 kfree_sensitive(ses->ntlmssp); 1918 ses->ntlmssp = NULL; 1919 1920 sess_data->func = NULL; 1921 sess_data->result = rc; 1922 } 1923 1924 static int select_sec(struct sess_data *sess_data) 1925 { 1926 int type; 1927 struct cifs_ses *ses = sess_data->ses; 1928 struct TCP_Server_Info *server = sess_data->server; 1929 1930 type = cifs_select_sectype(server, ses->sectype); 1931 cifs_dbg(FYI, "sess setup type %d\n", type); 1932 if (type == Unspecified) { 1933 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n"); 1934 return -EINVAL; 1935 } 1936 1937 switch (type) { 1938 case NTLMv2: 1939 sess_data->func = sess_auth_ntlmv2; 1940 break; 1941 case Kerberos: 1942 #ifdef CONFIG_CIFS_UPCALL 1943 sess_data->func = sess_auth_kerberos; 1944 break; 1945 #else 1946 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); 1947 return -ENOSYS; 1948 #endif /* CONFIG_CIFS_UPCALL */ 1949 case RawNTLMSSP: 1950 sess_data->func = sess_auth_rawntlmssp_negotiate; 1951 break; 1952 default: 1953 cifs_dbg(VFS, "secType %d not supported!\n", type); 1954 return -ENOSYS; 1955 } 1956 1957 return 0; 1958 } 1959 1960 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, 1961 struct TCP_Server_Info *server, 1962 const struct nls_table *nls_cp) 1963 { 1964 int rc = 0; 1965 struct sess_data *sess_data; 1966 1967 if (ses == NULL) { 1968 WARN(1, "%s: ses == NULL!", __func__); 1969 return -EINVAL; 1970 } 1971 1972 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL); 1973 if (!sess_data) 1974 return -ENOMEM; 1975 1976 sess_data->xid = xid; 1977 sess_data->ses = ses; 1978 sess_data->server = server; 1979 sess_data->buf0_type = CIFS_NO_BUFFER; 1980 sess_data->nls_cp = (struct nls_table *) nls_cp; 1981 1982 rc = select_sec(sess_data); 1983 if (rc) 1984 goto out; 1985 1986 while (sess_data->func) 1987 sess_data->func(sess_data); 1988 1989 /* Store result before we free sess_data */ 1990 rc = sess_data->result; 1991 1992 out: 1993 kfree_sensitive(sess_data); 1994 return rc; 1995 } 1996 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1997