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