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