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