1 // SPDX-License-Identifier: LGPL-2.1
4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2009
13 #include "cifsproto.h"
14 #include "cifs_unicode.h"
15 #include "cifs_debug.h"
18 #include <linux/utsname.h>
19 #include <linux/slab.h>
20 #include <linux/version.h>
22 #include "cifs_spnego.h"
23 #include "smb2proto.h"
24 #include "fs_context.h"
27 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
28 struct cifs_server_iface *iface);
31 is_server_using_iface(struct TCP_Server_Info *server,
32 struct cifs_server_iface *iface)
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;
39 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
41 if (server->dstaddr.ss_family == AF_INET) {
42 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
44 } else if (server->dstaddr.ss_family == AF_INET6) {
45 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
46 sizeof(i6->sin6_addr)) != 0)
49 /* unknown family.. */
55 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
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);
66 spin_unlock(&ses->chan_lock);
70 /* channel helper functions. assumed that chan_lock is held by caller. */
73 cifs_ses_get_chan_index(struct cifs_ses *ses,
74 struct TCP_Server_Info *server)
78 for (i = 0; i < ses->chan_count; i++) {
79 if (ses->chans[i].server == server)
83 /* If we didn't find the channel, it is likely a bug */
85 cifs_dbg(VFS, "unable to get chan index for server: 0x%llx",
92 cifs_chan_set_in_reconnect(struct cifs_ses *ses,
93 struct TCP_Server_Info *server)
95 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
97 ses->chans[chan_index].in_reconnect = true;
101 cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
102 struct TCP_Server_Info *server)
104 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
106 ses->chans[chan_index].in_reconnect = false;
110 cifs_chan_in_reconnect(struct cifs_ses *ses,
111 struct TCP_Server_Info *server)
113 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
115 return CIFS_CHAN_IN_RECONNECT(ses, chan_index);
119 cifs_chan_set_need_reconnect(struct cifs_ses *ses,
120 struct TCP_Server_Info *server)
122 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
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);
130 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
131 struct TCP_Server_Info *server)
133 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
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);
141 cifs_chan_needs_reconnect(struct cifs_ses *ses,
142 struct TCP_Server_Info *server)
144 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
146 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
150 cifs_chan_is_iface_active(struct cifs_ses *ses,
151 struct TCP_Server_Info *server)
153 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
155 return ses->chans[chan_index].iface &&
156 ses->chans[chan_index].iface->is_active;
159 /* returns number of channels added */
160 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
162 int old_chan_count, new_chan_count;
166 struct cifs_server_iface *iface = NULL, *niface = NULL;
168 spin_lock(&ses->chan_lock);
170 new_chan_count = old_chan_count = ses->chan_count;
171 left = ses->chan_max - ses->chan_count;
174 spin_unlock(&ses->chan_lock);
176 "ses already at max_channels (%zu), nothing to open\n",
181 if (ses->server->dialect < SMB30_PROT_ID) {
182 spin_unlock(&ses->chan_lock);
183 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
187 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
189 spin_unlock(&ses->chan_lock);
190 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
193 spin_unlock(&ses->chan_lock);
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
200 spin_lock(&ses->iface_lock);
201 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
203 spin_unlock(&ses->iface_lock);
208 if (tries > 3*ses->chan_max) {
209 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
214 spin_lock(&ses->iface_lock);
215 if (!ses->iface_count) {
216 spin_unlock(&ses->iface_lock);
220 list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
222 /* skip ifaces that are unusable */
223 if (!iface->is_active ||
224 (is_ses_using_iface(ses, iface) &&
225 !iface->rss_capable)) {
229 /* take ref before unlock */
230 kref_get(&iface->refcount);
232 spin_unlock(&ses->iface_lock);
233 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
234 spin_lock(&ses->iface_lock);
237 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
240 kref_put(&iface->refcount, release_iface);
244 cifs_dbg(FYI, "successfully opened new channel on iface:%pIS\n",
248 spin_unlock(&ses->iface_lock);
254 return new_chan_count - old_chan_count;
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.
263 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
265 unsigned int chan_index;
266 struct cifs_server_iface *iface = NULL;
267 struct cifs_server_iface *old_iface = NULL;
270 spin_lock(&ses->chan_lock);
271 chan_index = cifs_ses_get_chan_index(ses, server);
273 spin_unlock(&ses->chan_lock);
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);
284 spin_unlock(&ses->chan_lock);
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)) {
294 kref_get(&iface->refcount);
298 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
301 cifs_dbg(FYI, "unable to find a suitable iface\n");
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,
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);
316 cifs_dbg(FYI, "adding new iface: %pIS\n", &iface->sockaddr);
318 spin_unlock(&ses->iface_lock);
320 spin_lock(&ses->chan_lock);
321 chan_index = cifs_ses_get_chan_index(ses, server);
322 ses->chans[chan_index].iface = iface;
324 /* No iface is found. if secondary chan, drop connection */
325 if (!iface && CIFS_SERVER_IS_CHAN(server))
326 ses->chans[chan_index].server = NULL;
328 spin_unlock(&ses->chan_lock);
330 if (!iface && CIFS_SERVER_IS_CHAN(server))
331 cifs_put_tcp_session(server, false);
337 * If server is a channel of ses, return the corresponding enclosing
338 * cifs_chan otherwise return NULL.
341 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
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];
352 spin_unlock(&ses->chan_lock);
357 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
358 struct cifs_server_iface *iface)
360 struct TCP_Server_Info *chan_server;
361 struct cifs_chan *chan;
362 struct smb3_fs_context ctx = {NULL};
363 static const char unc_fmt[] = "\\%s\\foo";
364 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
365 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
366 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
368 unsigned int xid = get_xid();
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",
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",
380 * Setup a ctx with mostly the same info as the existing
381 * session and overwrite it with the requested iface data.
383 * We need to setup at least the fields used for negprot and
386 * We only need the ctx here, so we can reuse memory from
387 * the session and server without caring about memory
391 /* Always make new connection for now (TODO?) */
392 ctx.nosharesock = true;
395 ctx.domainauto = ses->domainAuto;
396 ctx.domainname = ses->domainName;
398 /* no hostname for extra channels */
399 ctx.server_hostname = "";
401 ctx.username = ses->user_name;
402 ctx.password = ses->password;
403 ctx.sectype = ses->sectype;
404 ctx.sign = ses->sign;
407 /* XXX: Use ses->server->hostname? */
408 sprintf(unc, unc_fmt, ses->ip_addr);
412 /* Reuse same version as master connection */
413 ctx.vals = ses->server->vals;
414 ctx.ops = ses->server->ops;
416 ctx.noblocksnd = ses->server->noblocksnd;
417 ctx.noautotune = ses->server->noautotune;
418 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
419 ctx.echo_interval = ses->server->echo_interval / HZ;
420 ctx.max_credits = ses->server->max_credits;
423 * This will be used for encoding/decoding user/domain/pw
424 * during sess setup auth.
426 ctx.local_nls = cifs_sb->local_nls;
428 /* Use RDMA if possible */
429 ctx.rdma = iface->rdma_capable;
430 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
432 /* reuse master con client guid */
433 memcpy(&ctx.client_guid, ses->server->client_guid,
434 SMB2_CLIENT_GUID_SIZE);
435 ctx.use_client_guid = true;
437 chan_server = cifs_get_tcp_session(&ctx, ses->server);
439 spin_lock(&ses->chan_lock);
440 chan = &ses->chans[ses->chan_count];
441 chan->server = chan_server;
442 if (IS_ERR(chan->server)) {
443 rc = PTR_ERR(chan->server);
445 spin_unlock(&ses->chan_lock);
450 atomic_set(&ses->chan_seq, 0);
452 /* Mark this channel as needing connect/setup */
453 cifs_chan_set_need_reconnect(ses, chan->server);
455 spin_unlock(&ses->chan_lock);
457 mutex_lock(&ses->session_mutex);
459 * We need to allocate the server crypto now as we will need
460 * to sign packets before we generate the channel signing key
461 * (we sign with the session key)
463 rc = smb311_crypto_shash_allocate(chan->server);
465 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
466 mutex_unlock(&ses->session_mutex);
470 rc = cifs_negotiate_protocol(xid, ses, chan->server);
472 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
474 mutex_unlock(&ses->session_mutex);
477 if (rc && chan->server) {
479 * we should avoid race with these delayed works before we
480 * remove this channel
482 cancel_delayed_work_sync(&chan->server->echo);
483 cancel_delayed_work_sync(&chan->server->resolve);
484 cancel_delayed_work_sync(&chan->server->reconnect);
486 spin_lock(&ses->chan_lock);
487 /* we rely on all bits beyond chan_count to be clear */
488 cifs_chan_clear_need_reconnect(ses, chan->server);
491 * chan_count should never reach 0 as at least the primary
492 * channel is always allocated
494 WARN_ON(ses->chan_count < 1);
495 spin_unlock(&ses->chan_lock);
497 cifs_put_tcp_session(chan->server, 0);
504 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
505 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
506 struct TCP_Server_Info *server,
507 SESSION_SETUP_ANDX *pSMB)
509 __u32 capabilities = 0;
511 /* init fields common to all four types of SessSetup */
512 /* Note that offsets for first seven fields in req struct are same */
513 /* in CIFS Specs so does not matter which of 3 forms of struct */
514 /* that we use in next few lines */
515 /* Note that header is initialized to zero in header_assemble */
516 pSMB->req.AndXCommand = 0xFF;
517 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
518 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
520 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
521 pSMB->req.VcNumber = cpu_to_le16(1);
523 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
525 /* BB verify whether signing required on neg or just on auth frame
528 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
529 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
532 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
534 if (ses->capabilities & CAP_UNICODE) {
535 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
536 capabilities |= CAP_UNICODE;
538 if (ses->capabilities & CAP_STATUS32) {
539 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
540 capabilities |= CAP_STATUS32;
542 if (ses->capabilities & CAP_DFS) {
543 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
544 capabilities |= CAP_DFS;
546 if (ses->capabilities & CAP_UNIX)
547 capabilities |= CAP_UNIX;
553 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
555 char *bcc_ptr = *pbcc_area;
558 /* Copy OS version */
559 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
561 bcc_ptr += 2 * bytes_ret;
562 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
564 bcc_ptr += 2 * bytes_ret;
565 bcc_ptr += 2; /* trailing null */
567 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
569 bcc_ptr += 2 * bytes_ret;
570 bcc_ptr += 2; /* trailing null */
572 *pbcc_area = bcc_ptr;
575 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
576 const struct nls_table *nls_cp)
578 char *bcc_ptr = *pbcc_area;
582 if (ses->domainName == NULL) {
583 /* Sending null domain better than using a bogus domain name (as
584 we did briefly in 2.6.18) since server will use its default */
589 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
590 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
591 bcc_ptr += 2 * bytes_ret;
592 bcc_ptr += 2; /* account for null terminator */
594 *pbcc_area = bcc_ptr;
597 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
598 const struct nls_table *nls_cp)
600 char *bcc_ptr = *pbcc_area;
603 /* BB FIXME add check that strings total less
604 than 335 or will need to send them as arrays */
607 if (ses->user_name == NULL) {
608 /* null user mount */
612 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
613 CIFS_MAX_USERNAME_LEN, nls_cp);
615 bcc_ptr += 2 * bytes_ret;
616 bcc_ptr += 2; /* account for null termination */
618 unicode_domain_string(&bcc_ptr, ses, nls_cp);
619 unicode_oslm_strings(&bcc_ptr, nls_cp);
621 *pbcc_area = bcc_ptr;
624 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
625 const struct nls_table *nls_cp)
627 char *bcc_ptr = *pbcc_area;
631 /* BB what about null user mounts - check that we do this BB */
633 if (ses->user_name != NULL) {
634 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
635 if (WARN_ON_ONCE(len < 0))
636 len = CIFS_MAX_USERNAME_LEN - 1;
639 /* else null user mount */
641 bcc_ptr++; /* account for null termination */
644 if (ses->domainName != NULL) {
645 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
646 if (WARN_ON_ONCE(len < 0))
647 len = CIFS_MAX_DOMAINNAME_LEN - 1;
649 } /* else we will send a null domain name
650 so the server will default to its own domain */
654 /* BB check for overflow here */
656 strcpy(bcc_ptr, "Linux version ");
657 bcc_ptr += strlen("Linux version ");
658 strcpy(bcc_ptr, init_utsname()->release);
659 bcc_ptr += strlen(init_utsname()->release) + 1;
661 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
662 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
664 *pbcc_area = bcc_ptr;
668 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
669 const struct nls_table *nls_cp)
672 char *data = *pbcc_area;
674 cifs_dbg(FYI, "bleft %d\n", bleft);
676 kfree(ses->serverOS);
677 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
678 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
679 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
685 kfree(ses->serverNOS);
686 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
687 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
688 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
694 kfree(ses->serverDomain);
695 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
696 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
701 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
702 struct cifs_ses *ses,
703 const struct nls_table *nls_cp)
706 char *bcc_ptr = *pbcc_area;
708 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
710 len = strnlen(bcc_ptr, bleft);
714 kfree(ses->serverOS);
716 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
718 memcpy(ses->serverOS, bcc_ptr, len);
719 ses->serverOS[len] = 0;
720 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
721 cifs_dbg(FYI, "OS/2 server\n");
727 len = strnlen(bcc_ptr, bleft);
731 kfree(ses->serverNOS);
733 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
734 if (ses->serverNOS) {
735 memcpy(ses->serverNOS, bcc_ptr, len);
736 ses->serverNOS[len] = 0;
742 len = strnlen(bcc_ptr, bleft);
746 /* No domain field in LANMAN case. Domain is
747 returned by old servers in the SMB negprot response */
748 /* BB For newer servers which do not support Unicode,
749 but thus do return domain here we could add parsing
750 for it later, but it is not very important */
751 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
753 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
755 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
756 struct cifs_ses *ses)
758 unsigned int tioffset; /* challenge message target info area */
759 unsigned int tilen; /* challenge message target info area length */
760 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
763 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
764 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
768 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
769 cifs_dbg(VFS, "blob signature incorrect %s\n",
773 if (pblob->MessageType != NtLmChallenge) {
774 cifs_dbg(VFS, "Incorrect message type %d\n",
779 server_flags = le32_to_cpu(pblob->NegotiateFlags);
780 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
781 ses->ntlmssp->client_flags, server_flags);
783 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
784 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
785 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
789 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
790 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
793 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
794 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
798 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
799 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
800 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
803 ses->ntlmssp->server_flags = server_flags;
805 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
806 /* In particular we can examine sign flags */
807 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
808 we must set the MIC field of the AUTHENTICATE_MESSAGE */
810 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
811 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
812 if (tioffset > blob_len || tioffset + tilen > blob_len) {
813 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
818 kfree_sensitive(ses->auth_key.response);
819 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
821 if (!ses->auth_key.response) {
822 cifs_dbg(VFS, "Challenge target info alloc failure\n");
825 ses->auth_key.len = tilen;
831 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
833 int sz = base_size + ses->auth_key.len
834 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
837 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
839 sz += sizeof(__le16);
842 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
844 sz += sizeof(__le16);
846 if (ses->workstation_name[0])
847 sz += sizeof(__le16) * strnlen(ses->workstation_name,
848 ntlmssp_workstation_name_size(ses));
850 sz += sizeof(__le16);
855 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
858 unsigned char *pstart,
859 unsigned char **pcur,
860 const struct nls_table *nls_cp)
862 unsigned char *tmp = pstart;
872 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
874 pbuf->MaximumLength = 0;
875 *pcur += sizeof(__le16);
877 len = cifs_strtoUTF16((__le16 *)*pcur,
881 len *= sizeof(__le16);
882 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
883 pbuf->Length = cpu_to_le16(len);
884 pbuf->MaximumLength = cpu_to_le16(len);
889 /* BB Move to ntlmssp.c eventually */
891 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
893 struct cifs_ses *ses,
894 struct TCP_Server_Info *server,
895 const struct nls_table *nls_cp)
898 NEGOTIATE_MESSAGE *sec_blob;
903 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
904 *pbuffer = kmalloc(len, GFP_KERNEL);
907 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
909 goto setup_ntlm_neg_ret;
911 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
913 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
914 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
915 sec_blob->MessageType = NtLmNegotiate;
917 /* BB is NTLMV2 session security format easier to use here? */
918 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
919 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
920 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
921 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
922 NTLMSSP_NEGOTIATE_SIGN;
923 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
924 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
926 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
927 ses->ntlmssp->client_flags = flags;
928 sec_blob->NegotiateFlags = cpu_to_le32(flags);
930 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
931 cifs_security_buffer_from_str(&sec_blob->DomainName,
933 CIFS_MAX_DOMAINNAME_LEN,
937 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
939 CIFS_MAX_WORKSTATION_LEN,
943 *buflen = tmp - *pbuffer;
949 * Build ntlmssp blob with additional fields, such as version,
950 * supported by modern servers. For safety limit to SMB3 or later
951 * See notes in MS-NLMP Section 2.2.2.1 e.g.
953 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
955 struct cifs_ses *ses,
956 struct TCP_Server_Info *server,
957 const struct nls_table *nls_cp)
960 struct negotiate_message *sec_blob;
965 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
966 *pbuffer = kmalloc(len, GFP_KERNEL);
969 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
971 goto setup_ntlm_smb3_neg_ret;
973 sec_blob = (struct negotiate_message *)*pbuffer;
975 memset(*pbuffer, 0, sizeof(struct negotiate_message));
976 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
977 sec_blob->MessageType = NtLmNegotiate;
979 /* BB is NTLMV2 session security format easier to use here? */
980 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
981 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
982 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
983 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
984 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
985 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
986 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
988 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
989 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
990 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
991 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
993 tmp = *pbuffer + sizeof(struct negotiate_message);
994 ses->ntlmssp->client_flags = flags;
995 sec_blob->NegotiateFlags = cpu_to_le32(flags);
997 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
998 cifs_security_buffer_from_str(&sec_blob->DomainName,
1000 CIFS_MAX_DOMAINNAME_LEN,
1004 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1006 CIFS_MAX_WORKSTATION_LEN,
1010 *buflen = tmp - *pbuffer;
1011 setup_ntlm_smb3_neg_ret:
1016 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1018 struct cifs_ses *ses,
1019 struct TCP_Server_Info *server,
1020 const struct nls_table *nls_cp)
1023 AUTHENTICATE_MESSAGE *sec_blob;
1028 rc = setup_ntlmv2_rsp(ses, nls_cp);
1030 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1032 goto setup_ntlmv2_ret;
1035 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1036 *pbuffer = kmalloc(len, GFP_KERNEL);
1039 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1041 goto setup_ntlmv2_ret;
1043 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1045 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1046 sec_blob->MessageType = NtLmAuthenticate;
1048 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1049 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1051 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1052 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1054 sec_blob->LmChallengeResponse.BufferOffset =
1055 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1056 sec_blob->LmChallengeResponse.Length = 0;
1057 sec_blob->LmChallengeResponse.MaximumLength = 0;
1059 sec_blob->NtChallengeResponse.BufferOffset =
1060 cpu_to_le32(tmp - *pbuffer);
1061 if (ses->user_name != NULL) {
1062 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1063 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1064 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1066 sec_blob->NtChallengeResponse.Length =
1067 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1068 sec_blob->NtChallengeResponse.MaximumLength =
1069 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1072 * don't send an NT Response for anonymous access
1074 sec_blob->NtChallengeResponse.Length = 0;
1075 sec_blob->NtChallengeResponse.MaximumLength = 0;
1078 cifs_security_buffer_from_str(&sec_blob->DomainName,
1080 CIFS_MAX_DOMAINNAME_LEN,
1084 cifs_security_buffer_from_str(&sec_blob->UserName,
1086 CIFS_MAX_USERNAME_LEN,
1090 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1091 ses->workstation_name,
1092 ntlmssp_workstation_name_size(ses),
1096 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1097 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1098 !calc_seckey(ses)) {
1099 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1100 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1101 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1102 sec_blob->SessionKey.MaximumLength =
1103 cpu_to_le16(CIFS_CPHTXT_SIZE);
1104 tmp += CIFS_CPHTXT_SIZE;
1106 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1107 sec_blob->SessionKey.Length = 0;
1108 sec_blob->SessionKey.MaximumLength = 0;
1111 *buflen = tmp - *pbuffer;
1117 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1119 switch (server->negflavor) {
1120 case CIFS_NEGFLAVOR_EXTENDED:
1121 switch (requested) {
1126 if (server->sec_ntlmssp &&
1127 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1129 if ((server->sec_kerberos || server->sec_mskerberos) &&
1130 (global_secflags & CIFSSEC_MAY_KRB5))
1136 case CIFS_NEGFLAVOR_UNENCAP:
1137 switch (requested) {
1141 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1155 struct cifs_ses *ses;
1156 struct TCP_Server_Info *server;
1157 struct nls_table *nls_cp;
1158 void (*func)(struct sess_data *);
1161 /* we will send the SMB in three pieces:
1162 * a fixed length beginning part, an optional
1163 * SPNEGO blob (which can be zero length), and a
1164 * last part which will include the strings
1165 * and rest of bcc area. This allows us to avoid
1166 * a large buffer 17K allocation
1172 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1174 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1177 struct cifs_ses *ses = sess_data->ses;
1178 struct smb_hdr *smb_buf;
1180 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1186 sess_data->iov[0].iov_base = (char *)smb_buf;
1187 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1189 * This variable will be used to clear the buffer
1190 * allocated above in case of any error in the calling function.
1192 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1194 /* 2000 big enough to fit max user, domain, NOS name etc. */
1195 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1196 if (!sess_data->iov[2].iov_base) {
1198 goto out_free_smb_buf;
1204 cifs_small_buf_release(smb_buf);
1205 sess_data->iov[0].iov_base = NULL;
1206 sess_data->iov[0].iov_len = 0;
1207 sess_data->buf0_type = CIFS_NO_BUFFER;
1212 sess_free_buffer(struct sess_data *sess_data)
1214 struct kvec *iov = sess_data->iov;
1217 * Zero the session data before freeing, as it might contain sensitive info (keys, etc).
1218 * Note that iov[1] is already freed by caller.
1220 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1221 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1223 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1224 sess_data->buf0_type = CIFS_NO_BUFFER;
1225 kfree_sensitive(iov[2].iov_base);
1229 sess_establish_session(struct sess_data *sess_data)
1231 struct cifs_ses *ses = sess_data->ses;
1232 struct TCP_Server_Info *server = sess_data->server;
1234 cifs_server_lock(server);
1235 if (!server->session_estab) {
1237 server->session_key.response =
1238 kmemdup(ses->auth_key.response,
1239 ses->auth_key.len, GFP_KERNEL);
1240 if (!server->session_key.response) {
1241 cifs_server_unlock(server);
1244 server->session_key.len =
1247 server->sequence_number = 0x2;
1248 server->session_estab = true;
1250 cifs_server_unlock(server);
1252 cifs_dbg(FYI, "CIFS session established successfully\n");
1257 sess_sendreceive(struct sess_data *sess_data)
1260 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1262 struct kvec rsp_iov = { NULL, 0 };
1264 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1265 be32_add_cpu(&smb_buf->smb_buf_length, count);
1266 put_bcc(count, smb_buf);
1268 rc = SendReceive2(sess_data->xid, sess_data->ses,
1269 sess_data->iov, 3 /* num_iovecs */,
1270 &sess_data->buf0_type,
1271 CIFS_LOG_ERROR, &rsp_iov);
1272 cifs_small_buf_release(sess_data->iov[0].iov_base);
1273 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1279 sess_auth_ntlmv2(struct sess_data *sess_data)
1282 struct smb_hdr *smb_buf;
1283 SESSION_SETUP_ANDX *pSMB;
1285 struct cifs_ses *ses = sess_data->ses;
1286 struct TCP_Server_Info *server = sess_data->server;
1288 __u16 bytes_remaining;
1290 /* old style NTLM sessionsetup */
1292 rc = sess_alloc_buffer(sess_data, 13);
1296 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1297 bcc_ptr = sess_data->iov[2].iov_base;
1298 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1300 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1302 /* LM2 password would be here if we supported it */
1303 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1305 if (ses->user_name != NULL) {
1306 /* calculate nlmv2 response and session key */
1307 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1309 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1313 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1314 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1315 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1317 /* set case sensitive password length after tilen may get
1318 * assigned, tilen is 0 otherwise.
1320 pSMB->req_no_secext.CaseSensitivePasswordLength =
1321 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1323 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1326 if (ses->capabilities & CAP_UNICODE) {
1327 if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) {
1331 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1333 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1337 sess_data->iov[2].iov_len = (long) bcc_ptr -
1338 (long) sess_data->iov[2].iov_base;
1340 rc = sess_sendreceive(sess_data);
1344 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1345 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1347 if (smb_buf->WordCount != 3) {
1349 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1353 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1354 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1356 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1357 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1359 bytes_remaining = get_bcc(smb_buf);
1360 bcc_ptr = pByteArea(smb_buf);
1362 /* BB check if Unicode and decode strings */
1363 if (bytes_remaining == 0) {
1364 /* no string area to decode, do nothing */
1365 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1366 /* unicode string area must be word-aligned */
1367 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1371 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1374 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1378 rc = sess_establish_session(sess_data);
1380 sess_data->result = rc;
1381 sess_data->func = NULL;
1382 sess_free_buffer(sess_data);
1383 kfree_sensitive(ses->auth_key.response);
1384 ses->auth_key.response = NULL;
1387 #ifdef CONFIG_CIFS_UPCALL
1389 sess_auth_kerberos(struct sess_data *sess_data)
1392 struct smb_hdr *smb_buf;
1393 SESSION_SETUP_ANDX *pSMB;
1395 struct cifs_ses *ses = sess_data->ses;
1396 struct TCP_Server_Info *server = sess_data->server;
1398 __u16 bytes_remaining;
1399 struct key *spnego_key = NULL;
1400 struct cifs_spnego_msg *msg;
1403 /* extended security */
1405 rc = sess_alloc_buffer(sess_data, 12);
1409 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1410 bcc_ptr = sess_data->iov[2].iov_base;
1411 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1413 spnego_key = cifs_get_spnego_key(ses, server);
1414 if (IS_ERR(spnego_key)) {
1415 rc = PTR_ERR(spnego_key);
1420 msg = spnego_key->payload.data[0];
1422 * check version field to make sure that cifs.upcall is
1423 * sending us a response in an expected form
1425 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1426 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1427 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1429 goto out_put_spnego_key;
1432 kfree_sensitive(ses->auth_key.response);
1433 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1435 if (!ses->auth_key.response) {
1436 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1439 goto out_put_spnego_key;
1441 ses->auth_key.len = msg->sesskey_len;
1443 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1444 capabilities |= CAP_EXTENDED_SECURITY;
1445 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1446 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1447 sess_data->iov[1].iov_len = msg->secblob_len;
1448 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1450 if (ses->capabilities & CAP_UNICODE) {
1451 /* unicode strings must be word aligned */
1452 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1456 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1457 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1459 /* BB: is this right? */
1460 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1463 sess_data->iov[2].iov_len = (long) bcc_ptr -
1464 (long) sess_data->iov[2].iov_base;
1466 rc = sess_sendreceive(sess_data);
1468 goto out_put_spnego_key;
1470 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1471 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1473 if (smb_buf->WordCount != 4) {
1475 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1476 goto out_put_spnego_key;
1479 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1480 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1482 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1483 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1485 bytes_remaining = get_bcc(smb_buf);
1486 bcc_ptr = pByteArea(smb_buf);
1488 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1489 if (blob_len > bytes_remaining) {
1490 cifs_dbg(VFS, "bad security blob length %d\n",
1493 goto out_put_spnego_key;
1495 bcc_ptr += blob_len;
1496 bytes_remaining -= blob_len;
1498 /* BB check if Unicode and decode strings */
1499 if (bytes_remaining == 0) {
1500 /* no string area to decode, do nothing */
1501 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1502 /* unicode string area must be word-aligned */
1503 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1507 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1510 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1514 rc = sess_establish_session(sess_data);
1516 key_invalidate(spnego_key);
1517 key_put(spnego_key);
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;
1526 #endif /* ! CONFIG_CIFS_UPCALL */
1529 * The required kvec buffers have to be allocated before calling this
1533 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1535 SESSION_SETUP_ANDX *pSMB;
1536 struct cifs_ses *ses = sess_data->ses;
1537 struct TCP_Server_Info *server = sess_data->server;
1541 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1543 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1544 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1545 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1549 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1550 capabilities |= CAP_EXTENDED_SECURITY;
1551 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1553 bcc_ptr = sess_data->iov[2].iov_base;
1554 /* unicode strings must be word aligned */
1555 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1559 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1561 sess_data->iov[2].iov_len = (long) bcc_ptr -
1562 (long) sess_data->iov[2].iov_base;
1568 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1571 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1574 struct smb_hdr *smb_buf;
1575 SESSION_SETUP_ANDX *pSMB;
1576 struct cifs_ses *ses = sess_data->ses;
1577 struct TCP_Server_Info *server = sess_data->server;
1578 __u16 bytes_remaining;
1580 unsigned char *ntlmsspblob = NULL;
1583 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1586 * if memory allocation is successful, caller of this function
1589 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1590 if (!ses->ntlmssp) {
1594 ses->ntlmssp->sesskey_per_smbsess = false;
1597 rc = sess_alloc_buffer(sess_data, 12);
1601 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1603 /* Build security blob before we assemble the request */
1604 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1605 &blob_len, ses, server,
1608 goto out_free_ntlmsspblob;
1610 sess_data->iov[1].iov_len = blob_len;
1611 sess_data->iov[1].iov_base = ntlmsspblob;
1612 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1614 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1616 goto out_free_ntlmsspblob;
1618 rc = sess_sendreceive(sess_data);
1620 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1621 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1623 /* If true, rc here is expected and not an error */
1624 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1625 smb_buf->Status.CifsError ==
1626 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1630 goto out_free_ntlmsspblob;
1632 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1634 if (smb_buf->WordCount != 4) {
1636 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1637 goto out_free_ntlmsspblob;
1640 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1641 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1643 bytes_remaining = get_bcc(smb_buf);
1644 bcc_ptr = pByteArea(smb_buf);
1646 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1647 if (blob_len > bytes_remaining) {
1648 cifs_dbg(VFS, "bad security blob length %d\n",
1651 goto out_free_ntlmsspblob;
1654 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1656 out_free_ntlmsspblob:
1657 kfree_sensitive(ntlmsspblob);
1659 sess_free_buffer(sess_data);
1662 sess_data->func = sess_auth_rawntlmssp_authenticate;
1666 /* Else error. Cleanup */
1667 kfree_sensitive(ses->auth_key.response);
1668 ses->auth_key.response = NULL;
1669 kfree_sensitive(ses->ntlmssp);
1670 ses->ntlmssp = NULL;
1672 sess_data->func = NULL;
1673 sess_data->result = rc;
1677 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1680 struct smb_hdr *smb_buf;
1681 SESSION_SETUP_ANDX *pSMB;
1682 struct cifs_ses *ses = sess_data->ses;
1683 struct TCP_Server_Info *server = sess_data->server;
1684 __u16 bytes_remaining;
1686 unsigned char *ntlmsspblob = NULL;
1689 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1692 rc = sess_alloc_buffer(sess_data, 12);
1696 /* Build security blob before we assemble the request */
1697 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1698 smb_buf = (struct smb_hdr *)pSMB;
1699 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1700 &blob_len, ses, server,
1703 goto out_free_ntlmsspblob;
1704 sess_data->iov[1].iov_len = blob_len;
1705 sess_data->iov[1].iov_base = ntlmsspblob;
1706 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1708 * Make sure that we tell the server that we are using
1709 * the uid that it just gave us back on the response
1712 smb_buf->Uid = ses->Suid;
1714 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1716 goto out_free_ntlmsspblob;
1718 rc = sess_sendreceive(sess_data);
1720 goto out_free_ntlmsspblob;
1722 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1723 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1724 if (smb_buf->WordCount != 4) {
1726 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1727 goto out_free_ntlmsspblob;
1730 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1731 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1733 if (ses->Suid != smb_buf->Uid) {
1734 ses->Suid = smb_buf->Uid;
1735 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1738 bytes_remaining = get_bcc(smb_buf);
1739 bcc_ptr = pByteArea(smb_buf);
1740 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1741 if (blob_len > bytes_remaining) {
1742 cifs_dbg(VFS, "bad security blob length %d\n",
1745 goto out_free_ntlmsspblob;
1747 bcc_ptr += blob_len;
1748 bytes_remaining -= blob_len;
1751 /* BB check if Unicode and decode strings */
1752 if (bytes_remaining == 0) {
1753 /* no string area to decode, do nothing */
1754 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1755 /* unicode string area must be word-aligned */
1756 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1760 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1763 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1767 out_free_ntlmsspblob:
1768 kfree_sensitive(ntlmsspblob);
1770 sess_free_buffer(sess_data);
1773 rc = sess_establish_session(sess_data);
1776 kfree_sensitive(ses->auth_key.response);
1777 ses->auth_key.response = NULL;
1778 kfree_sensitive(ses->ntlmssp);
1779 ses->ntlmssp = NULL;
1781 sess_data->func = NULL;
1782 sess_data->result = rc;
1785 static int select_sec(struct sess_data *sess_data)
1788 struct cifs_ses *ses = sess_data->ses;
1789 struct TCP_Server_Info *server = sess_data->server;
1791 type = cifs_select_sectype(server, ses->sectype);
1792 cifs_dbg(FYI, "sess setup type %d\n", type);
1793 if (type == Unspecified) {
1794 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1800 sess_data->func = sess_auth_ntlmv2;
1803 #ifdef CONFIG_CIFS_UPCALL
1804 sess_data->func = sess_auth_kerberos;
1807 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1809 #endif /* CONFIG_CIFS_UPCALL */
1811 sess_data->func = sess_auth_rawntlmssp_negotiate;
1814 cifs_dbg(VFS, "secType %d not supported!\n", type);
1821 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1822 struct TCP_Server_Info *server,
1823 const struct nls_table *nls_cp)
1826 struct sess_data *sess_data;
1829 WARN(1, "%s: ses == NULL!", __func__);
1833 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1837 sess_data->xid = xid;
1838 sess_data->ses = ses;
1839 sess_data->server = server;
1840 sess_data->buf0_type = CIFS_NO_BUFFER;
1841 sess_data->nls_cp = (struct nls_table *) nls_cp;
1843 rc = select_sec(sess_data);
1847 while (sess_data->func)
1848 sess_data->func(sess_data);
1850 /* Store result before we free sess_data */
1851 rc = sess_data->result;
1854 kfree_sensitive(sess_data);
1857 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */