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