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