1
2
3
4
5
6
7
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 "cifs_spnego.h"
21#include "smb2proto.h"
22#include "fs_context.h"
23
24static int
25cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
26 struct cifs_server_iface *iface);
27
28bool
29is_server_using_iface(struct TCP_Server_Info *server,
30 struct cifs_server_iface *iface)
31{
32 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
33 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
34 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
35 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
36
37 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
38 return false;
39 if (server->dstaddr.ss_family == AF_INET) {
40 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
41 return false;
42 } else if (server->dstaddr.ss_family == AF_INET6) {
43 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
44 sizeof(i6->sin6_addr)) != 0)
45 return false;
46 } else {
47
48 return false;
49 }
50 return true;
51}
52
53bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
54{
55 int i;
56
57 spin_lock(&ses->chan_lock);
58 for (i = 0; i < ses->chan_count; i++) {
59 if (is_server_using_iface(ses->chans[i].server, iface)) {
60 spin_unlock(&ses->chan_lock);
61 return true;
62 }
63 }
64 spin_unlock(&ses->chan_lock);
65 return false;
66}
67
68
69int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
70{
71 int old_chan_count, new_chan_count;
72 int left;
73 int i = 0;
74 int rc = 0;
75 int tries = 0;
76 struct cifs_server_iface *ifaces = NULL;
77 size_t iface_count;
78
79 if (ses->server->dialect < SMB30_PROT_ID) {
80 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
81 return 0;
82 }
83
84 spin_lock(&ses->chan_lock);
85
86 new_chan_count = old_chan_count = ses->chan_count;
87 left = ses->chan_max - ses->chan_count;
88
89 if (left <= 0) {
90 cifs_dbg(FYI,
91 "ses already at max_channels (%zu), nothing to open\n",
92 ses->chan_max);
93 spin_unlock(&ses->chan_lock);
94 return 0;
95 }
96
97 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
98 ses->chan_max = 1;
99 spin_unlock(&ses->chan_lock);
100 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
101 return 0;
102 }
103 spin_unlock(&ses->chan_lock);
104
105
106
107
108
109
110 spin_lock(&ses->iface_lock);
111 iface_count = ses->iface_count;
112 if (iface_count <= 0) {
113 spin_unlock(&ses->iface_lock);
114 cifs_dbg(VFS, "no iface list available to open channels\n");
115 return 0;
116 }
117 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
118 GFP_ATOMIC);
119 if (!ifaces) {
120 spin_unlock(&ses->iface_lock);
121 return 0;
122 }
123 spin_unlock(&ses->iface_lock);
124
125
126
127
128
129
130 while (left > 0) {
131 struct cifs_server_iface *iface;
132
133 tries++;
134 if (tries > 3*ses->chan_max) {
135 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
136 left);
137 break;
138 }
139
140 iface = &ifaces[i];
141 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
142 i = (i+1) % iface_count;
143 continue;
144 }
145
146 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
147 if (rc) {
148 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
149 i, rc);
150 i = (i+1) % iface_count;
151 continue;
152 }
153
154 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
155 i);
156 left--;
157 new_chan_count++;
158 }
159
160 kfree(ifaces);
161 return new_chan_count - old_chan_count;
162}
163
164
165
166
167
168struct cifs_chan *
169cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
170{
171 int i;
172
173 spin_lock(&ses->chan_lock);
174 for (i = 0; i < ses->chan_count; i++) {
175 if (ses->chans[i].server == server) {
176 spin_unlock(&ses->chan_lock);
177 return &ses->chans[i];
178 }
179 }
180 spin_unlock(&ses->chan_lock);
181 return NULL;
182}
183
184static int
185cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
186 struct cifs_server_iface *iface)
187{
188 struct TCP_Server_Info *chan_server;
189 struct cifs_chan *chan;
190 struct smb3_fs_context ctx = {NULL};
191 static const char unc_fmt[] = "\\%s\\foo";
192 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
193 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
194 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
195 int rc;
196 unsigned int xid = get_xid();
197
198 if (iface->sockaddr.ss_family == AF_INET)
199 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
200 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
201 &ipv4->sin_addr);
202 else
203 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
204 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
205 &ipv6->sin6_addr);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 ctx.nosharesock = true;
221
222
223 ctx.domainauto = ses->domainAuto;
224 ctx.domainname = ses->domainName;
225 ctx.server_hostname = ses->server->hostname;
226 ctx.username = ses->user_name;
227 ctx.password = ses->password;
228 ctx.sectype = ses->sectype;
229 ctx.sign = ses->sign;
230
231
232
233 sprintf(unc, unc_fmt, ses->ip_addr);
234 ctx.UNC = unc;
235 ctx.prepath = "";
236
237
238 ctx.vals = ses->server->vals;
239 ctx.ops = ses->server->ops;
240
241 ctx.noblocksnd = ses->server->noblocksnd;
242 ctx.noautotune = ses->server->noautotune;
243 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
244 ctx.echo_interval = ses->server->echo_interval / HZ;
245 ctx.max_credits = ses->server->max_credits;
246
247
248
249
250
251 ctx.local_nls = cifs_sb->local_nls;
252
253
254 ctx.rdma = iface->rdma_capable;
255 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
256
257
258 memcpy(&ctx.client_guid, ses->server->client_guid,
259 SMB2_CLIENT_GUID_SIZE);
260 ctx.use_client_guid = true;
261
262 chan_server = cifs_get_tcp_session(&ctx, ses->server);
263
264 mutex_lock(&ses->session_mutex);
265 spin_lock(&ses->chan_lock);
266 chan = ses->binding_chan = &ses->chans[ses->chan_count];
267 chan->server = chan_server;
268 if (IS_ERR(chan->server)) {
269 rc = PTR_ERR(chan->server);
270 chan->server = NULL;
271 spin_unlock(&ses->chan_lock);
272 goto out;
273 }
274 spin_unlock(&ses->chan_lock);
275
276
277
278
279
280
281 rc = smb311_crypto_shash_allocate(chan->server);
282 if (rc) {
283 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
284 goto out;
285 }
286
287 ses->binding = true;
288 rc = cifs_negotiate_protocol(xid, ses);
289 if (rc)
290 goto out;
291
292 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
293 if (rc)
294 goto out;
295
296
297
298
299
300
301
302
303
304
305
306 spin_lock(&ses->chan_lock);
307 ses->chan_count++;
308 atomic_set(&ses->chan_seq, 0);
309 spin_unlock(&ses->chan_lock);
310
311out:
312 ses->binding = false;
313 ses->binding_chan = NULL;
314 mutex_unlock(&ses->session_mutex);
315
316 if (rc && chan->server)
317 cifs_put_tcp_session(chan->server, 0);
318
319 return rc;
320}
321
322
323void cifs_ses_mark_for_reconnect(struct cifs_ses *ses)
324{
325 int i;
326
327 for (i = 0; i < ses->chan_count; i++) {
328 spin_lock(&GlobalMid_Lock);
329 if (ses->chans[i].server->tcpStatus != CifsExiting)
330 ses->chans[i].server->tcpStatus = CifsNeedReconnect;
331 spin_unlock(&GlobalMid_Lock);
332 }
333}
334
335static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
336{
337 __u32 capabilities = 0;
338
339
340
341
342
343
344 pSMB->req.AndXCommand = 0xFF;
345 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
346 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
347 USHRT_MAX));
348 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
349 pSMB->req.VcNumber = cpu_to_le16(1);
350
351
352
353
354
355
356 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
357 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
358
359 if (ses->server->sign)
360 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
361
362 if (ses->capabilities & CAP_UNICODE) {
363 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
364 capabilities |= CAP_UNICODE;
365 }
366 if (ses->capabilities & CAP_STATUS32) {
367 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
368 capabilities |= CAP_STATUS32;
369 }
370 if (ses->capabilities & CAP_DFS) {
371 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
372 capabilities |= CAP_DFS;
373 }
374 if (ses->capabilities & CAP_UNIX)
375 capabilities |= CAP_UNIX;
376
377 return capabilities;
378}
379
380static void
381unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
382{
383 char *bcc_ptr = *pbcc_area;
384 int bytes_ret = 0;
385
386
387 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
388 nls_cp);
389 bcc_ptr += 2 * bytes_ret;
390 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
391 32, nls_cp);
392 bcc_ptr += 2 * bytes_ret;
393 bcc_ptr += 2;
394
395 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
396 32, nls_cp);
397 bcc_ptr += 2 * bytes_ret;
398 bcc_ptr += 2;
399
400 *pbcc_area = bcc_ptr;
401}
402
403static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
404 const struct nls_table *nls_cp)
405{
406 char *bcc_ptr = *pbcc_area;
407 int bytes_ret = 0;
408
409
410 if (ses->domainName == NULL) {
411
412
413 *bcc_ptr = 0;
414 *(bcc_ptr+1) = 0;
415 bytes_ret = 0;
416 } else
417 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
418 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
419 bcc_ptr += 2 * bytes_ret;
420 bcc_ptr += 2;
421
422 *pbcc_area = bcc_ptr;
423}
424
425
426static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
427 const struct nls_table *nls_cp)
428{
429 char *bcc_ptr = *pbcc_area;
430 int bytes_ret = 0;
431
432
433
434
435
436
437
438
439
440
441 if (ses->user_name == NULL) {
442
443 *bcc_ptr = 0;
444 *(bcc_ptr+1) = 0;
445 } else {
446 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
447 CIFS_MAX_USERNAME_LEN, nls_cp);
448 }
449 bcc_ptr += 2 * bytes_ret;
450 bcc_ptr += 2;
451
452 unicode_domain_string(&bcc_ptr, ses, nls_cp);
453 unicode_oslm_strings(&bcc_ptr, nls_cp);
454
455 *pbcc_area = bcc_ptr;
456}
457
458static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
459 const struct nls_table *nls_cp)
460{
461 char *bcc_ptr = *pbcc_area;
462 int len;
463
464
465
466
467 if (ses->user_name != NULL) {
468 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
469 if (WARN_ON_ONCE(len < 0))
470 len = CIFS_MAX_USERNAME_LEN - 1;
471 bcc_ptr += len;
472 }
473
474 *bcc_ptr = 0;
475 bcc_ptr++;
476
477
478 if (ses->domainName != NULL) {
479 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
480 if (WARN_ON_ONCE(len < 0))
481 len = CIFS_MAX_DOMAINNAME_LEN - 1;
482 bcc_ptr += len;
483 }
484
485 *bcc_ptr = 0;
486 bcc_ptr++;
487
488
489
490 strcpy(bcc_ptr, "Linux version ");
491 bcc_ptr += strlen("Linux version ");
492 strcpy(bcc_ptr, init_utsname()->release);
493 bcc_ptr += strlen(init_utsname()->release) + 1;
494
495 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
496 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
497
498 *pbcc_area = bcc_ptr;
499}
500
501static void
502decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
503 const struct nls_table *nls_cp)
504{
505 int len;
506 char *data = *pbcc_area;
507
508 cifs_dbg(FYI, "bleft %d\n", bleft);
509
510 kfree(ses->serverOS);
511 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
512 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
513 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
514 data += len;
515 bleft -= len;
516 if (bleft <= 0)
517 return;
518
519 kfree(ses->serverNOS);
520 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
521 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
522 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
523 data += len;
524 bleft -= len;
525 if (bleft <= 0)
526 return;
527
528 kfree(ses->serverDomain);
529 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
530 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
531
532 return;
533}
534
535static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
536 struct cifs_ses *ses,
537 const struct nls_table *nls_cp)
538{
539 int len;
540 char *bcc_ptr = *pbcc_area;
541
542 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
543
544 len = strnlen(bcc_ptr, bleft);
545 if (len >= bleft)
546 return;
547
548 kfree(ses->serverOS);
549
550 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
551 if (ses->serverOS) {
552 memcpy(ses->serverOS, bcc_ptr, len);
553 ses->serverOS[len] = 0;
554 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
555 cifs_dbg(FYI, "OS/2 server\n");
556 }
557
558 bcc_ptr += len + 1;
559 bleft -= len + 1;
560
561 len = strnlen(bcc_ptr, bleft);
562 if (len >= bleft)
563 return;
564
565 kfree(ses->serverNOS);
566
567 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
568 if (ses->serverNOS) {
569 memcpy(ses->serverNOS, bcc_ptr, len);
570 ses->serverNOS[len] = 0;
571 }
572
573 bcc_ptr += len + 1;
574 bleft -= len + 1;
575
576 len = strnlen(bcc_ptr, bleft);
577 if (len > bleft)
578 return;
579
580
581
582
583
584
585 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
586}
587
588int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
589 struct cifs_ses *ses)
590{
591 unsigned int tioffset;
592 unsigned int tilen;
593 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
594 __u32 server_flags;
595
596 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
597 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
598 return -EINVAL;
599 }
600
601 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
602 cifs_dbg(VFS, "blob signature incorrect %s\n",
603 pblob->Signature);
604 return -EINVAL;
605 }
606 if (pblob->MessageType != NtLmChallenge) {
607 cifs_dbg(VFS, "Incorrect message type %d\n",
608 pblob->MessageType);
609 return -EINVAL;
610 }
611
612 server_flags = le32_to_cpu(pblob->NegotiateFlags);
613 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
614 ses->ntlmssp->client_flags, server_flags);
615
616 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
617 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
618 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
619 __func__);
620 return -EINVAL;
621 }
622 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
623 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
624 return -EINVAL;
625 }
626 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
627 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
628 __func__);
629 return -EOPNOTSUPP;
630 }
631 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
632 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
633 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
634 __func__);
635
636 ses->ntlmssp->server_flags = server_flags;
637
638 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
639
640
641
642
643 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
644 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
645 if (tioffset > blob_len || tioffset + tilen > blob_len) {
646 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
647 tioffset, tilen);
648 return -EINVAL;
649 }
650 if (tilen) {
651 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
652 GFP_KERNEL);
653 if (!ses->auth_key.response) {
654 cifs_dbg(VFS, "Challenge target info alloc failure\n");
655 return -ENOMEM;
656 }
657 ses->auth_key.len = tilen;
658 }
659
660 return 0;
661}
662
663static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
664{
665 int sz = base_size + ses->auth_key.len
666 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
667
668 if (ses->domainName)
669 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
670 else
671 sz += sizeof(__le16);
672
673 if (ses->user_name)
674 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
675 else
676 sz += sizeof(__le16);
677
678 sz += sizeof(__le16) * strnlen(ses->workstation_name, CIFS_MAX_WORKSTATION_LEN);
679
680 return sz;
681}
682
683static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
684 char *str_value,
685 int str_length,
686 unsigned char *pstart,
687 unsigned char **pcur,
688 const struct nls_table *nls_cp)
689{
690 unsigned char *tmp = pstart;
691 int len;
692
693 if (!pbuf)
694 return;
695
696 if (!pcur)
697 pcur = &tmp;
698
699 if (!str_value) {
700 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
701 pbuf->Length = 0;
702 pbuf->MaximumLength = 0;
703 *pcur += sizeof(__le16);
704 } else {
705 len = cifs_strtoUTF16((__le16 *)*pcur,
706 str_value,
707 str_length,
708 nls_cp);
709 len *= sizeof(__le16);
710 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
711 pbuf->Length = cpu_to_le16(len);
712 pbuf->MaximumLength = cpu_to_le16(len);
713 *pcur += len;
714 }
715}
716
717
718
719int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
720 u16 *buflen,
721 struct cifs_ses *ses,
722 const struct nls_table *nls_cp)
723{
724 int rc = 0;
725 struct TCP_Server_Info *server = cifs_ses_server(ses);
726 NEGOTIATE_MESSAGE *sec_blob;
727 __u32 flags;
728 unsigned char *tmp;
729 int len;
730
731 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
732 *pbuffer = kmalloc(len, GFP_KERNEL);
733 if (!*pbuffer) {
734 rc = -ENOMEM;
735 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
736 *buflen = 0;
737 goto setup_ntlm_neg_ret;
738 }
739 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
740
741 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
742 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
743 sec_blob->MessageType = NtLmNegotiate;
744
745
746 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
747 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
748 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
749 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
750 NTLMSSP_NEGOTIATE_SIGN;
751 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
752 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
753
754 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
755 ses->ntlmssp->client_flags = flags;
756 sec_blob->NegotiateFlags = cpu_to_le32(flags);
757
758
759 cifs_security_buffer_from_str(&sec_blob->DomainName,
760 NULL,
761 CIFS_MAX_DOMAINNAME_LEN,
762 *pbuffer, &tmp,
763 nls_cp);
764
765 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
766 NULL,
767 CIFS_MAX_WORKSTATION_LEN,
768 *pbuffer, &tmp,
769 nls_cp);
770
771 *buflen = tmp - *pbuffer;
772setup_ntlm_neg_ret:
773 return rc;
774}
775
776int build_ntlmssp_auth_blob(unsigned char **pbuffer,
777 u16 *buflen,
778 struct cifs_ses *ses,
779 const struct nls_table *nls_cp)
780{
781 int rc;
782 AUTHENTICATE_MESSAGE *sec_blob;
783 __u32 flags;
784 unsigned char *tmp;
785 int len;
786
787 rc = setup_ntlmv2_rsp(ses, nls_cp);
788 if (rc) {
789 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
790 *buflen = 0;
791 goto setup_ntlmv2_ret;
792 }
793
794 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
795 *pbuffer = kmalloc(len, GFP_KERNEL);
796 if (!*pbuffer) {
797 rc = -ENOMEM;
798 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
799 *buflen = 0;
800 goto setup_ntlmv2_ret;
801 }
802 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
803
804 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
805 sec_blob->MessageType = NtLmAuthenticate;
806
807 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
808 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
809
810 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
811 sec_blob->NegotiateFlags = cpu_to_le32(flags);
812
813 sec_blob->LmChallengeResponse.BufferOffset =
814 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
815 sec_blob->LmChallengeResponse.Length = 0;
816 sec_blob->LmChallengeResponse.MaximumLength = 0;
817
818 sec_blob->NtChallengeResponse.BufferOffset =
819 cpu_to_le32(tmp - *pbuffer);
820 if (ses->user_name != NULL) {
821 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
822 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
823 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
824
825 sec_blob->NtChallengeResponse.Length =
826 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
827 sec_blob->NtChallengeResponse.MaximumLength =
828 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
829 } else {
830
831
832
833 sec_blob->NtChallengeResponse.Length = 0;
834 sec_blob->NtChallengeResponse.MaximumLength = 0;
835 }
836
837 cifs_security_buffer_from_str(&sec_blob->DomainName,
838 ses->domainName,
839 CIFS_MAX_DOMAINNAME_LEN,
840 *pbuffer, &tmp,
841 nls_cp);
842
843 cifs_security_buffer_from_str(&sec_blob->UserName,
844 ses->user_name,
845 CIFS_MAX_USERNAME_LEN,
846 *pbuffer, &tmp,
847 nls_cp);
848
849 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
850 ses->workstation_name,
851 CIFS_MAX_WORKSTATION_LEN,
852 *pbuffer, &tmp,
853 nls_cp);
854
855 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
856 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
857 !calc_seckey(ses)) {
858 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
859 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
860 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
861 sec_blob->SessionKey.MaximumLength =
862 cpu_to_le16(CIFS_CPHTXT_SIZE);
863 tmp += CIFS_CPHTXT_SIZE;
864 } else {
865 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
866 sec_blob->SessionKey.Length = 0;
867 sec_blob->SessionKey.MaximumLength = 0;
868 }
869
870 *buflen = tmp - *pbuffer;
871setup_ntlmv2_ret:
872 return rc;
873}
874
875enum securityEnum
876cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
877{
878 switch (server->negflavor) {
879 case CIFS_NEGFLAVOR_EXTENDED:
880 switch (requested) {
881 case Kerberos:
882 case RawNTLMSSP:
883 return requested;
884 case Unspecified:
885 if (server->sec_ntlmssp &&
886 (global_secflags & CIFSSEC_MAY_NTLMSSP))
887 return RawNTLMSSP;
888 if ((server->sec_kerberos || server->sec_mskerberos) &&
889 (global_secflags & CIFSSEC_MAY_KRB5))
890 return Kerberos;
891 fallthrough;
892 default:
893 return Unspecified;
894 }
895 case CIFS_NEGFLAVOR_UNENCAP:
896 switch (requested) {
897 case NTLMv2:
898 return requested;
899 case Unspecified:
900 if (global_secflags & CIFSSEC_MAY_NTLMV2)
901 return NTLMv2;
902 break;
903 default:
904 break;
905 }
906 fallthrough;
907 default:
908 return Unspecified;
909 }
910}
911
912struct sess_data {
913 unsigned int xid;
914 struct cifs_ses *ses;
915 struct nls_table *nls_cp;
916 void (*func)(struct sess_data *);
917 int result;
918
919
920
921
922
923
924
925
926 int buf0_type;
927 struct kvec iov[3];
928};
929
930static int
931sess_alloc_buffer(struct sess_data *sess_data, int wct)
932{
933 int rc;
934 struct cifs_ses *ses = sess_data->ses;
935 struct smb_hdr *smb_buf;
936
937 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
938 (void **)&smb_buf);
939
940 if (rc)
941 return rc;
942
943 sess_data->iov[0].iov_base = (char *)smb_buf;
944 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
945
946
947
948
949 sess_data->buf0_type = CIFS_SMALL_BUFFER;
950
951
952 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
953 if (!sess_data->iov[2].iov_base) {
954 rc = -ENOMEM;
955 goto out_free_smb_buf;
956 }
957
958 return 0;
959
960out_free_smb_buf:
961 cifs_small_buf_release(smb_buf);
962 sess_data->iov[0].iov_base = NULL;
963 sess_data->iov[0].iov_len = 0;
964 sess_data->buf0_type = CIFS_NO_BUFFER;
965 return rc;
966}
967
968static void
969sess_free_buffer(struct sess_data *sess_data)
970{
971
972 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
973 sess_data->buf0_type = CIFS_NO_BUFFER;
974 kfree(sess_data->iov[2].iov_base);
975}
976
977static int
978sess_establish_session(struct sess_data *sess_data)
979{
980 struct cifs_ses *ses = sess_data->ses;
981
982 mutex_lock(&ses->server->srv_mutex);
983 if (!ses->server->session_estab) {
984 if (ses->server->sign) {
985 ses->server->session_key.response =
986 kmemdup(ses->auth_key.response,
987 ses->auth_key.len, GFP_KERNEL);
988 if (!ses->server->session_key.response) {
989 mutex_unlock(&ses->server->srv_mutex);
990 return -ENOMEM;
991 }
992 ses->server->session_key.len =
993 ses->auth_key.len;
994 }
995 ses->server->sequence_number = 0x2;
996 ses->server->session_estab = true;
997 }
998 mutex_unlock(&ses->server->srv_mutex);
999
1000 cifs_dbg(FYI, "CIFS session established successfully\n");
1001 spin_lock(&GlobalMid_Lock);
1002 ses->status = CifsGood;
1003 ses->need_reconnect = false;
1004 spin_unlock(&GlobalMid_Lock);
1005
1006 return 0;
1007}
1008
1009static int
1010sess_sendreceive(struct sess_data *sess_data)
1011{
1012 int rc;
1013 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1014 __u16 count;
1015 struct kvec rsp_iov = { NULL, 0 };
1016
1017 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1018 be32_add_cpu(&smb_buf->smb_buf_length, count);
1019 put_bcc(count, smb_buf);
1020
1021 rc = SendReceive2(sess_data->xid, sess_data->ses,
1022 sess_data->iov, 3 ,
1023 &sess_data->buf0_type,
1024 CIFS_LOG_ERROR, &rsp_iov);
1025 cifs_small_buf_release(sess_data->iov[0].iov_base);
1026 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1027
1028 return rc;
1029}
1030
1031static void
1032sess_auth_ntlmv2(struct sess_data *sess_data)
1033{
1034 int rc = 0;
1035 struct smb_hdr *smb_buf;
1036 SESSION_SETUP_ANDX *pSMB;
1037 char *bcc_ptr;
1038 struct cifs_ses *ses = sess_data->ses;
1039 __u32 capabilities;
1040 __u16 bytes_remaining;
1041
1042
1043
1044 rc = sess_alloc_buffer(sess_data, 13);
1045 if (rc)
1046 goto out;
1047
1048 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1049 bcc_ptr = sess_data->iov[2].iov_base;
1050 capabilities = cifs_ssetup_hdr(ses, pSMB);
1051
1052 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1053
1054
1055 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1056
1057 if (ses->user_name != NULL) {
1058
1059 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1060 if (rc) {
1061 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1062 goto out;
1063 }
1064
1065 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1066 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1067 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1068
1069
1070
1071
1072 pSMB->req_no_secext.CaseSensitivePasswordLength =
1073 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1074 } else {
1075 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1076 }
1077
1078 if (ses->capabilities & CAP_UNICODE) {
1079 if (sess_data->iov[0].iov_len % 2) {
1080 *bcc_ptr = 0;
1081 bcc_ptr++;
1082 }
1083 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1084 } else {
1085 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1086 }
1087
1088
1089 sess_data->iov[2].iov_len = (long) bcc_ptr -
1090 (long) sess_data->iov[2].iov_base;
1091
1092 rc = sess_sendreceive(sess_data);
1093 if (rc)
1094 goto out;
1095
1096 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1097 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1098
1099 if (smb_buf->WordCount != 3) {
1100 rc = -EIO;
1101 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1102 goto out;
1103 }
1104
1105 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1106 cifs_dbg(FYI, "Guest login\n");
1107
1108 ses->Suid = smb_buf->Uid;
1109 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1110
1111 bytes_remaining = get_bcc(smb_buf);
1112 bcc_ptr = pByteArea(smb_buf);
1113
1114
1115 if (bytes_remaining == 0) {
1116
1117 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1118
1119 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1120 ++bcc_ptr;
1121 --bytes_remaining;
1122 }
1123 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1124 sess_data->nls_cp);
1125 } else {
1126 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1127 sess_data->nls_cp);
1128 }
1129
1130 rc = sess_establish_session(sess_data);
1131out:
1132 sess_data->result = rc;
1133 sess_data->func = NULL;
1134 sess_free_buffer(sess_data);
1135 kfree(ses->auth_key.response);
1136 ses->auth_key.response = NULL;
1137}
1138
1139#ifdef CONFIG_CIFS_UPCALL
1140static void
1141sess_auth_kerberos(struct sess_data *sess_data)
1142{
1143 int rc = 0;
1144 struct smb_hdr *smb_buf;
1145 SESSION_SETUP_ANDX *pSMB;
1146 char *bcc_ptr;
1147 struct cifs_ses *ses = sess_data->ses;
1148 __u32 capabilities;
1149 __u16 bytes_remaining;
1150 struct key *spnego_key = NULL;
1151 struct cifs_spnego_msg *msg;
1152 u16 blob_len;
1153
1154
1155
1156 rc = sess_alloc_buffer(sess_data, 12);
1157 if (rc)
1158 goto out;
1159
1160 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1161 bcc_ptr = sess_data->iov[2].iov_base;
1162 capabilities = cifs_ssetup_hdr(ses, pSMB);
1163
1164 spnego_key = cifs_get_spnego_key(ses);
1165 if (IS_ERR(spnego_key)) {
1166 rc = PTR_ERR(spnego_key);
1167 spnego_key = NULL;
1168 goto out;
1169 }
1170
1171 msg = spnego_key->payload.data[0];
1172
1173
1174
1175
1176 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1177 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1178 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1179 rc = -EKEYREJECTED;
1180 goto out_put_spnego_key;
1181 }
1182
1183 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1184 GFP_KERNEL);
1185 if (!ses->auth_key.response) {
1186 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1187 msg->sesskey_len);
1188 rc = -ENOMEM;
1189 goto out_put_spnego_key;
1190 }
1191 ses->auth_key.len = msg->sesskey_len;
1192
1193 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1194 capabilities |= CAP_EXTENDED_SECURITY;
1195 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1196 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1197 sess_data->iov[1].iov_len = msg->secblob_len;
1198 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1199
1200 if (ses->capabilities & CAP_UNICODE) {
1201
1202 if ((sess_data->iov[0].iov_len
1203 + sess_data->iov[1].iov_len) % 2) {
1204 *bcc_ptr = 0;
1205 bcc_ptr++;
1206 }
1207 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1208 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1209 } else {
1210
1211 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1212 }
1213
1214 sess_data->iov[2].iov_len = (long) bcc_ptr -
1215 (long) sess_data->iov[2].iov_base;
1216
1217 rc = sess_sendreceive(sess_data);
1218 if (rc)
1219 goto out_put_spnego_key;
1220
1221 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1222 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1223
1224 if (smb_buf->WordCount != 4) {
1225 rc = -EIO;
1226 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1227 goto out_put_spnego_key;
1228 }
1229
1230 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1231 cifs_dbg(FYI, "Guest login\n");
1232
1233 ses->Suid = smb_buf->Uid;
1234 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1235
1236 bytes_remaining = get_bcc(smb_buf);
1237 bcc_ptr = pByteArea(smb_buf);
1238
1239 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1240 if (blob_len > bytes_remaining) {
1241 cifs_dbg(VFS, "bad security blob length %d\n",
1242 blob_len);
1243 rc = -EINVAL;
1244 goto out_put_spnego_key;
1245 }
1246 bcc_ptr += blob_len;
1247 bytes_remaining -= blob_len;
1248
1249
1250 if (bytes_remaining == 0) {
1251
1252 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1253
1254 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1255 ++bcc_ptr;
1256 --bytes_remaining;
1257 }
1258 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1259 sess_data->nls_cp);
1260 } else {
1261 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1262 sess_data->nls_cp);
1263 }
1264
1265 rc = sess_establish_session(sess_data);
1266out_put_spnego_key:
1267 key_invalidate(spnego_key);
1268 key_put(spnego_key);
1269out:
1270 sess_data->result = rc;
1271 sess_data->func = NULL;
1272 sess_free_buffer(sess_data);
1273 kfree(ses->auth_key.response);
1274 ses->auth_key.response = NULL;
1275}
1276
1277#endif
1278
1279
1280
1281
1282
1283static int
1284_sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1285{
1286 SESSION_SETUP_ANDX *pSMB;
1287 struct cifs_ses *ses = sess_data->ses;
1288 __u32 capabilities;
1289 char *bcc_ptr;
1290
1291 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1292
1293 capabilities = cifs_ssetup_hdr(ses, pSMB);
1294 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1295 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1296 return -ENOSYS;
1297 }
1298
1299 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1300 capabilities |= CAP_EXTENDED_SECURITY;
1301 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1302
1303 bcc_ptr = sess_data->iov[2].iov_base;
1304
1305 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1306 *bcc_ptr = 0;
1307 bcc_ptr++;
1308 }
1309 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1310
1311 sess_data->iov[2].iov_len = (long) bcc_ptr -
1312 (long) sess_data->iov[2].iov_base;
1313
1314 return 0;
1315}
1316
1317static void
1318sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1319
1320static void
1321sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1322{
1323 int rc;
1324 struct smb_hdr *smb_buf;
1325 SESSION_SETUP_ANDX *pSMB;
1326 struct cifs_ses *ses = sess_data->ses;
1327 __u16 bytes_remaining;
1328 char *bcc_ptr;
1329 unsigned char *ntlmsspblob = NULL;
1330 u16 blob_len;
1331
1332 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1333
1334
1335
1336
1337
1338 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1339 if (!ses->ntlmssp) {
1340 rc = -ENOMEM;
1341 goto out;
1342 }
1343 ses->ntlmssp->sesskey_per_smbsess = false;
1344
1345
1346 rc = sess_alloc_buffer(sess_data, 12);
1347 if (rc)
1348 goto out;
1349
1350 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1351
1352
1353 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1354 &blob_len, ses,
1355 sess_data->nls_cp);
1356 if (rc)
1357 goto out;
1358
1359 sess_data->iov[1].iov_len = blob_len;
1360 sess_data->iov[1].iov_base = ntlmsspblob;
1361 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1362
1363 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1364 if (rc)
1365 goto out;
1366
1367 rc = sess_sendreceive(sess_data);
1368
1369 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1370 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1371
1372
1373 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1374 smb_buf->Status.CifsError ==
1375 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1376 rc = 0;
1377
1378 if (rc)
1379 goto out;
1380
1381 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1382
1383 if (smb_buf->WordCount != 4) {
1384 rc = -EIO;
1385 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1386 goto out;
1387 }
1388
1389 ses->Suid = smb_buf->Uid;
1390 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1391
1392 bytes_remaining = get_bcc(smb_buf);
1393 bcc_ptr = pByteArea(smb_buf);
1394
1395 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1396 if (blob_len > bytes_remaining) {
1397 cifs_dbg(VFS, "bad security blob length %d\n",
1398 blob_len);
1399 rc = -EINVAL;
1400 goto out;
1401 }
1402
1403 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1404out:
1405 sess_free_buffer(sess_data);
1406
1407 if (!rc) {
1408 sess_data->func = sess_auth_rawntlmssp_authenticate;
1409 return;
1410 }
1411
1412
1413 kfree(ses->auth_key.response);
1414 ses->auth_key.response = NULL;
1415 kfree(ses->ntlmssp);
1416 ses->ntlmssp = NULL;
1417
1418 sess_data->func = NULL;
1419 sess_data->result = rc;
1420}
1421
1422static void
1423sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1424{
1425 int rc;
1426 struct smb_hdr *smb_buf;
1427 SESSION_SETUP_ANDX *pSMB;
1428 struct cifs_ses *ses = sess_data->ses;
1429 __u16 bytes_remaining;
1430 char *bcc_ptr;
1431 unsigned char *ntlmsspblob = NULL;
1432 u16 blob_len;
1433
1434 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1435
1436
1437 rc = sess_alloc_buffer(sess_data, 12);
1438 if (rc)
1439 goto out;
1440
1441
1442 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1443 smb_buf = (struct smb_hdr *)pSMB;
1444 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1445 &blob_len, ses, sess_data->nls_cp);
1446 if (rc)
1447 goto out_free_ntlmsspblob;
1448 sess_data->iov[1].iov_len = blob_len;
1449 sess_data->iov[1].iov_base = ntlmsspblob;
1450 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1451
1452
1453
1454
1455
1456 smb_buf->Uid = ses->Suid;
1457
1458 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1459 if (rc)
1460 goto out_free_ntlmsspblob;
1461
1462 rc = sess_sendreceive(sess_data);
1463 if (rc)
1464 goto out_free_ntlmsspblob;
1465
1466 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1467 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1468 if (smb_buf->WordCount != 4) {
1469 rc = -EIO;
1470 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1471 goto out_free_ntlmsspblob;
1472 }
1473
1474 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1475 cifs_dbg(FYI, "Guest login\n");
1476
1477 if (ses->Suid != smb_buf->Uid) {
1478 ses->Suid = smb_buf->Uid;
1479 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1480 }
1481
1482 bytes_remaining = get_bcc(smb_buf);
1483 bcc_ptr = pByteArea(smb_buf);
1484 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1485 if (blob_len > bytes_remaining) {
1486 cifs_dbg(VFS, "bad security blob length %d\n",
1487 blob_len);
1488 rc = -EINVAL;
1489 goto out_free_ntlmsspblob;
1490 }
1491 bcc_ptr += blob_len;
1492 bytes_remaining -= blob_len;
1493
1494
1495
1496 if (bytes_remaining == 0) {
1497
1498 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1499
1500 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1501 ++bcc_ptr;
1502 --bytes_remaining;
1503 }
1504 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1505 sess_data->nls_cp);
1506 } else {
1507 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1508 sess_data->nls_cp);
1509 }
1510
1511out_free_ntlmsspblob:
1512 kfree(ntlmsspblob);
1513out:
1514 sess_free_buffer(sess_data);
1515
1516 if (!rc)
1517 rc = sess_establish_session(sess_data);
1518
1519
1520 kfree(ses->auth_key.response);
1521 ses->auth_key.response = NULL;
1522 kfree(ses->ntlmssp);
1523 ses->ntlmssp = NULL;
1524
1525 sess_data->func = NULL;
1526 sess_data->result = rc;
1527}
1528
1529static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
1530{
1531 int type;
1532
1533 type = cifs_select_sectype(ses->server, ses->sectype);
1534 cifs_dbg(FYI, "sess setup type %d\n", type);
1535 if (type == Unspecified) {
1536 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1537 return -EINVAL;
1538 }
1539
1540 switch (type) {
1541 case NTLMv2:
1542 sess_data->func = sess_auth_ntlmv2;
1543 break;
1544 case Kerberos:
1545#ifdef CONFIG_CIFS_UPCALL
1546 sess_data->func = sess_auth_kerberos;
1547 break;
1548#else
1549 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1550 return -ENOSYS;
1551#endif
1552 case RawNTLMSSP:
1553 sess_data->func = sess_auth_rawntlmssp_negotiate;
1554 break;
1555 default:
1556 cifs_dbg(VFS, "secType %d not supported!\n", type);
1557 return -ENOSYS;
1558 }
1559
1560 return 0;
1561}
1562
1563int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1564 const struct nls_table *nls_cp)
1565{
1566 int rc = 0;
1567 struct sess_data *sess_data;
1568
1569 if (ses == NULL) {
1570 WARN(1, "%s: ses == NULL!", __func__);
1571 return -EINVAL;
1572 }
1573
1574 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1575 if (!sess_data)
1576 return -ENOMEM;
1577
1578 rc = select_sec(ses, sess_data);
1579 if (rc)
1580 goto out;
1581
1582 sess_data->xid = xid;
1583 sess_data->ses = ses;
1584 sess_data->buf0_type = CIFS_NO_BUFFER;
1585 sess_data->nls_cp = (struct nls_table *) nls_cp;
1586
1587 while (sess_data->func)
1588 sess_data->func(sess_data);
1589
1590
1591 rc = sess_data->result;
1592
1593out:
1594 kfree(sess_data);
1595 return rc;
1596}
1597