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