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