blob: 183a3a868d7b90946a9cc9f9b1a2fcac05ae3d45 [file] [log] [blame]
Steve French39798772006-05-31 22:40:51 +00001/*
2 * fs/cifs/sess.c
3 *
4 * SMB/CIFS session setup handling routines
5 *
Steve Frenchd185cda2009-04-30 17:45:10 +00006 * Copyright (c) International Business Machines Corp., 2006, 2009
Steve French39798772006-05-31 22:40:51 +00007 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include "cifspdu.h"
25#include "cifsglob.h"
26#include "cifsproto.h"
27#include "cifs_unicode.h"
28#include "cifs_debug.h"
29#include "ntlmssp.h"
30#include "nterr.h"
Steve French9c535882006-06-01 05:09:10 +000031#include <linux/utsname.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Steve French24424212007-11-16 23:37:35 +000033#include "cifs_spnego.h"
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020034#include "smb2proto.h"
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060035#include "fs_context.h"
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020036
Ronnie Sahlberg387ec582020-12-14 16:40:20 +100037static int
38cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
39 struct cifs_server_iface *iface);
40
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020041bool
42is_server_using_iface(struct TCP_Server_Info *server,
43 struct cifs_server_iface *iface)
44{
45 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
46 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
47 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
48 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
49
50 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
51 return false;
52 if (server->dstaddr.ss_family == AF_INET) {
53 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
54 return false;
55 } else if (server->dstaddr.ss_family == AF_INET6) {
56 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
57 sizeof(i6->sin6_addr)) != 0)
58 return false;
59 } else {
60 /* unknown family.. */
61 return false;
62 }
63 return true;
64}
65
66bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
67{
68 int i;
69
70 for (i = 0; i < ses->chan_count; i++) {
71 if (is_server_using_iface(ses->chans[i].server, iface))
72 return true;
73 }
74 return false;
75}
76
77/* returns number of channels added */
Ronnie Sahlberg387ec582020-12-14 16:40:20 +100078int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020079{
80 int old_chan_count = ses->chan_count;
81 int left = ses->chan_max - ses->chan_count;
82 int i = 0;
83 int rc = 0;
Aurelien Aptel65a37a32019-11-20 17:15:59 +010084 int tries = 0;
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +010085 struct cifs_server_iface *ifaces = NULL;
86 size_t iface_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020087
88 if (left <= 0) {
89 cifs_dbg(FYI,
90 "ses already at max_channels (%zu), nothing to open\n",
91 ses->chan_max);
92 return 0;
93 }
94
95 if (ses->server->dialect < SMB30_PROT_ID) {
96 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
97 return 0;
98 }
99
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100100 /*
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100101 * Make a copy of the iface list at the time and use that
102 * instead so as to not hold the iface spinlock for opening
103 * channels
104 */
105 spin_lock(&ses->iface_lock);
106 iface_count = ses->iface_count;
107 if (iface_count <= 0) {
108 spin_unlock(&ses->iface_lock);
Aurelien Aptel343a1b72020-02-06 10:19:11 +0100109 cifs_dbg(VFS, "no iface list available to open channels\n");
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100110 return 0;
111 }
112 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
113 GFP_ATOMIC);
114 if (!ifaces) {
115 spin_unlock(&ses->iface_lock);
116 return 0;
117 }
118 spin_unlock(&ses->iface_lock);
119
120 /*
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100121 * Keep connecting to same, fastest, iface for all channels as
122 * long as its RSS. Try next fastest one if not RSS or channel
123 * creation fails.
124 */
125 while (left > 0) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200126 struct cifs_server_iface *iface;
127
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100128 tries++;
129 if (tries > 3*ses->chan_max) {
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500130 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100131 left);
132 break;
133 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200134
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100135 iface = &ifaces[i];
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100136 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100137 i = (i+1) % iface_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200138 continue;
139 }
140
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000141 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100142 if (rc) {
143 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
144 i, rc);
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100145 i = (i+1) % iface_count;
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100146 continue;
147 }
148
149 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
150 i);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200151 left--;
152 }
153
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100154 kfree(ifaces);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200155 return ses->chan_count - old_chan_count;
156}
157
Aurelien Aptel2f589672020-04-24 16:55:31 +0200158/*
159 * If server is a channel of ses, return the corresponding enclosing
160 * cifs_chan otherwise return NULL.
161 */
162struct cifs_chan *
163cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
164{
165 int i;
166
167 for (i = 0; i < ses->chan_count; i++) {
168 if (ses->chans[i].server == server)
169 return &ses->chans[i];
170 }
171 return NULL;
172}
173
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000174static int
175cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
176 struct cifs_server_iface *iface)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200177{
178 struct cifs_chan *chan;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600179 struct smb3_fs_context ctx = {NULL};
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200180 static const char unc_fmt[] = "\\%s\\foo";
181 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
182 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
183 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
184 int rc;
185 unsigned int xid = get_xid();
186
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200187 if (iface->sockaddr.ss_family == AF_INET)
Joe Perchesa0a30362020-04-14 22:42:53 -0700188 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
189 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
190 &ipv4->sin_addr);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200191 else
Joe Perchesa0a30362020-04-14 22:42:53 -0700192 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
193 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
194 &ipv6->sin6_addr);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200195
196 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600197 * Setup a ctx with mostly the same info as the existing
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200198 * session and overwrite it with the requested iface data.
199 *
200 * We need to setup at least the fields used for negprot and
201 * sesssetup.
202 *
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600203 * We only need the ctx here, so we can reuse memory from
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200204 * the session and server without caring about memory
205 * management.
206 */
207
208 /* Always make new connection for now (TODO?) */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600209 ctx.nosharesock = true;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200210
211 /* Auth */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600212 ctx.domainauto = ses->domainAuto;
213 ctx.domainname = ses->domainName;
214 ctx.username = ses->user_name;
215 ctx.password = ses->password;
216 ctx.sectype = ses->sectype;
217 ctx.sign = ses->sign;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200218
219 /* UNC and paths */
220 /* XXX: Use ses->server->hostname? */
Steve Frenchb438fcf2021-02-20 19:24:11 -0600221 sprintf(unc, unc_fmt, ses->ip_addr);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600222 ctx.UNC = unc;
223 ctx.prepath = "";
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200224
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500225 /* Reuse same version as master connection */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600226 ctx.vals = ses->server->vals;
227 ctx.ops = ses->server->ops;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200228
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600229 ctx.noblocksnd = ses->server->noblocksnd;
230 ctx.noautotune = ses->server->noautotune;
231 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
232 ctx.echo_interval = ses->server->echo_interval / HZ;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200233
234 /*
235 * This will be used for encoding/decoding user/domain/pw
236 * during sess setup auth.
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200237 */
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000238 ctx.local_nls = cifs_sb->local_nls;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200239
240 /* Use RDMA if possible */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600241 ctx.rdma = iface->rdma_capable;
242 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200243
244 /* reuse master con client guid */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600245 memcpy(&ctx.client_guid, ses->server->client_guid,
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200246 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600247 ctx.use_client_guid = true;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200248
249 mutex_lock(&ses->session_mutex);
250
Aurelien Aptel8eec7952020-04-24 15:24:05 +0200251 chan = ses->binding_chan = &ses->chans[ses->chan_count];
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600252 chan->server = cifs_get_tcp_session(&ctx);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200253 if (IS_ERR(chan->server)) {
254 rc = PTR_ERR(chan->server);
255 chan->server = NULL;
256 goto out;
257 }
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -0300258 spin_lock(&cifs_tcp_ses_lock);
259 chan->server->is_channel = true;
260 spin_unlock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200261
262 /*
263 * We need to allocate the server crypto now as we will need
264 * to sign packets before we generate the channel signing key
265 * (we sign with the session key)
266 */
267 rc = smb311_crypto_shash_allocate(chan->server);
268 if (rc) {
269 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
270 goto out;
271 }
272
273 ses->binding = true;
274 rc = cifs_negotiate_protocol(xid, ses);
275 if (rc)
276 goto out;
277
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000278 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200279 if (rc)
280 goto out;
281
282 /* success, put it on the list
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500283 * XXX: sharing ses between 2 tcp servers is not possible, the
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200284 * way "internal" linked lists works in linux makes element
285 * only able to belong to one list
286 *
287 * the binding session is already established so the rest of
288 * the code should be able to look it up, no need to add the
289 * ses to the new server.
290 */
291
292 ses->chan_count++;
293 atomic_set(&ses->chan_seq, 0);
294out:
295 ses->binding = false;
Aurelien Aptel8eec7952020-04-24 15:24:05 +0200296 ses->binding_chan = NULL;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200297 mutex_unlock(&ses->session_mutex);
298
299 if (rc && chan->server)
300 cifs_put_tcp_session(chan->server, 0);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200301
302 return rc;
303}
Steve French39798772006-05-31 22:40:51 +0000304
Steve French96daf2b2011-05-27 04:34:02 +0000305static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
Steve French39798772006-05-31 22:40:51 +0000306{
307 __u32 capabilities = 0;
308
309 /* init fields common to all four types of SessSetup */
Steve Frencheca6acf2009-02-20 05:43:09 +0000310 /* Note that offsets for first seven fields in req struct are same */
311 /* in CIFS Specs so does not matter which of 3 forms of struct */
312 /* that we use in next few lines */
313 /* Note that header is initialized to zero in header_assemble */
Steve French39798772006-05-31 22:40:51 +0000314 pSMB->req.AndXCommand = 0xFF;
Jeff Laytonc974bef2011-10-11 06:41:32 -0400315 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
316 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
317 USHRT_MAX));
Steve French39798772006-05-31 22:40:51 +0000318 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
Fabian Frederickbc09d142014-12-10 15:41:15 -0800319 pSMB->req.VcNumber = cpu_to_le16(1);
Steve French39798772006-05-31 22:40:51 +0000320
321 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
322
Steve French790fe572007-07-07 19:25:05 +0000323 /* BB verify whether signing required on neg or just on auth frame
Steve French39798772006-05-31 22:40:51 +0000324 (and NTLM case) */
325
326 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
327 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
328
Jeff Layton38d77c52013-05-26 07:01:00 -0400329 if (ses->server->sign)
Steve French39798772006-05-31 22:40:51 +0000330 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
331
332 if (ses->capabilities & CAP_UNICODE) {
333 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
334 capabilities |= CAP_UNICODE;
335 }
336 if (ses->capabilities & CAP_STATUS32) {
337 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
338 capabilities |= CAP_STATUS32;
339 }
340 if (ses->capabilities & CAP_DFS) {
341 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
342 capabilities |= CAP_DFS;
343 }
Steve French26f57362007-08-30 22:09:15 +0000344 if (ses->capabilities & CAP_UNIX)
Steve French39798772006-05-31 22:40:51 +0000345 capabilities |= CAP_UNIX;
Steve French39798772006-05-31 22:40:51 +0000346
Steve French39798772006-05-31 22:40:51 +0000347 return capabilities;
348}
349
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000350static void
351unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
352{
353 char *bcc_ptr = *pbcc_area;
354 int bytes_ret = 0;
355
356 /* Copy OS version */
Steve Frenchacbbb762012-01-18 22:32:33 -0600357 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
358 nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000359 bcc_ptr += 2 * bytes_ret;
Steve Frenchacbbb762012-01-18 22:32:33 -0600360 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
361 32, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000362 bcc_ptr += 2 * bytes_ret;
363 bcc_ptr += 2; /* trailing null */
364
Steve Frenchacbbb762012-01-18 22:32:33 -0600365 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
366 32, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000367 bcc_ptr += 2 * bytes_ret;
368 bcc_ptr += 2; /* trailing null */
369
370 *pbcc_area = bcc_ptr;
371}
372
Steve French96daf2b2011-05-27 04:34:02 +0000373static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000374 const struct nls_table *nls_cp)
375{
376 char *bcc_ptr = *pbcc_area;
377 int bytes_ret = 0;
378
379 /* copy domain */
380 if (ses->domainName == NULL) {
381 /* Sending null domain better than using a bogus domain name (as
382 we did briefly in 2.6.18) since server will use its default */
383 *bcc_ptr = 0;
384 *(bcc_ptr+1) = 0;
385 bytes_ret = 0;
386 } else
Steve Frenchacbbb762012-01-18 22:32:33 -0600387 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
Chen Gang057d6332013-07-19 09:01:36 +0800388 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000389 bcc_ptr += 2 * bytes_ret;
390 bcc_ptr += 2; /* account for null terminator */
391
392 *pbcc_area = bcc_ptr;
393}
394
395
Steve French96daf2b2011-05-27 04:34:02 +0000396static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
Steve French790fe572007-07-07 19:25:05 +0000397 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000398{
Steve French790fe572007-07-07 19:25:05 +0000399 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000400 int bytes_ret = 0;
401
402 /* BB FIXME add check that strings total less
403 than 335 or will need to send them as arrays */
404
Steve French0223cf02006-06-27 19:50:57 +0000405 /* unicode strings, must be word aligned before the call */
406/* if ((long) bcc_ptr % 2) {
Steve French39798772006-05-31 22:40:51 +0000407 *bcc_ptr = 0;
408 bcc_ptr++;
Steve French0223cf02006-06-27 19:50:57 +0000409 } */
Steve French39798772006-05-31 22:40:51 +0000410 /* copy user */
Steve French8727c8a2011-02-25 01:11:56 -0600411 if (ses->user_name == NULL) {
Steve French6e659c62006-11-08 23:10:46 +0000412 /* null user mount */
413 *bcc_ptr = 0;
414 *(bcc_ptr+1) = 0;
Steve French301a6a32010-02-06 07:08:53 +0000415 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -0600416 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -0400417 CIFS_MAX_USERNAME_LEN, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000418 }
419 bcc_ptr += 2 * bytes_ret;
420 bcc_ptr += 2; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000421
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000422 unicode_domain_string(&bcc_ptr, ses, nls_cp);
423 unicode_oslm_strings(&bcc_ptr, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000424
425 *pbcc_area = bcc_ptr;
426}
427
Steve French96daf2b2011-05-27 04:34:02 +0000428static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
Steve French790fe572007-07-07 19:25:05 +0000429 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000430{
Steve French790fe572007-07-07 19:25:05 +0000431 char *bcc_ptr = *pbcc_area;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000432 int len;
Steve French39798772006-05-31 22:40:51 +0000433
434 /* copy user */
435 /* BB what about null user mounts - check that we do this BB */
Steve French790fe572007-07-07 19:25:05 +0000436 /* copy user */
Shirish Pargaonkarde47a412012-02-02 15:28:28 -0600437 if (ses->user_name != NULL) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000438 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
439 if (WARN_ON_ONCE(len < 0))
440 len = CIFS_MAX_USERNAME_LEN - 1;
441 bcc_ptr += len;
Shirish Pargaonkarde47a412012-02-02 15:28:28 -0600442 }
Steve French8727c8a2011-02-25 01:11:56 -0600443 /* else null user mount */
Steve French39798772006-05-31 22:40:51 +0000444 *bcc_ptr = 0;
Steve French790fe572007-07-07 19:25:05 +0000445 bcc_ptr++; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000446
Steve French790fe572007-07-07 19:25:05 +0000447 /* copy domain */
Steve French790fe572007-07-07 19:25:05 +0000448 if (ses->domainName != NULL) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000449 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
450 if (WARN_ON_ONCE(len < 0))
451 len = CIFS_MAX_DOMAINNAME_LEN - 1;
452 bcc_ptr += len;
Steve French790fe572007-07-07 19:25:05 +0000453 } /* else we will send a null domain name
Steve French6e659c62006-11-08 23:10:46 +0000454 so the server will default to its own domain */
Steve French39798772006-05-31 22:40:51 +0000455 *bcc_ptr = 0;
456 bcc_ptr++;
457
458 /* BB check for overflow here */
459
460 strcpy(bcc_ptr, "Linux version ");
461 bcc_ptr += strlen("Linux version ");
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700462 strcpy(bcc_ptr, init_utsname()->release);
463 bcc_ptr += strlen(init_utsname()->release) + 1;
Steve French39798772006-05-31 22:40:51 +0000464
465 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
466 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
467
Steve French790fe572007-07-07 19:25:05 +0000468 *pbcc_area = bcc_ptr;
Steve French39798772006-05-31 22:40:51 +0000469}
470
Jeff Layton59140792009-04-30 07:16:21 -0400471static void
Steve French96daf2b2011-05-27 04:34:02 +0000472decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
Jeff Layton59140792009-04-30 07:16:21 -0400473 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000474{
Jeff Layton59140792009-04-30 07:16:21 -0400475 int len;
Steve French790fe572007-07-07 19:25:05 +0000476 char *data = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000477
Joe Perchesf96637b2013-05-04 22:12:25 -0500478 cifs_dbg(FYI, "bleft %d\n", bleft);
Steve French39798772006-05-31 22:40:51 +0000479
Steve French26f57362007-08-30 22:09:15 +0000480 kfree(ses->serverOS);
Steve Frenchacbbb762012-01-18 22:32:33 -0600481 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500482 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
Jeff Layton59140792009-04-30 07:16:21 -0400483 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
484 data += len;
485 bleft -= len;
486 if (bleft <= 0)
487 return;
Steve French39798772006-05-31 22:40:51 +0000488
Steve French26f57362007-08-30 22:09:15 +0000489 kfree(ses->serverNOS);
Steve Frenchacbbb762012-01-18 22:32:33 -0600490 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500491 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
Jeff Layton59140792009-04-30 07:16:21 -0400492 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
493 data += len;
494 bleft -= len;
495 if (bleft <= 0)
496 return;
Steve French39798772006-05-31 22:40:51 +0000497
Steve French26f57362007-08-30 22:09:15 +0000498 kfree(ses->serverDomain);
Steve Frenchacbbb762012-01-18 22:32:33 -0600499 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500500 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
Steve French790fe572007-07-07 19:25:05 +0000501
Jeff Layton59140792009-04-30 07:16:21 -0400502 return;
Steve French39798772006-05-31 22:40:51 +0000503}
504
Jeff Layton7d066452013-05-24 07:41:00 -0400505static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
506 struct cifs_ses *ses,
507 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000508{
Steve French39798772006-05-31 22:40:51 +0000509 int len;
Steve French790fe572007-07-07 19:25:05 +0000510 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000511
Joe Perchesf96637b2013-05-04 22:12:25 -0500512 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
Steve French50c2f752007-07-13 00:33:32 +0000513
Steve French39798772006-05-31 22:40:51 +0000514 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000515 if (len >= bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400516 return;
Steve French50c2f752007-07-13 00:33:32 +0000517
Steve French26f57362007-08-30 22:09:15 +0000518 kfree(ses->serverOS);
Steve French39798772006-05-31 22:40:51 +0000519
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000520 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
Namjae Jeon27b7edc2014-08-20 19:39:28 +0900521 if (ses->serverOS) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000522 memcpy(ses->serverOS, bcc_ptr, len);
523 ses->serverOS[len] = 0;
Namjae Jeon27b7edc2014-08-20 19:39:28 +0900524 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
525 cifs_dbg(FYI, "OS/2 server\n");
526 }
Steve French39798772006-05-31 22:40:51 +0000527
528 bcc_ptr += len + 1;
529 bleft -= len + 1;
530
531 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000532 if (len >= bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400533 return;
Steve French39798772006-05-31 22:40:51 +0000534
Steve French26f57362007-08-30 22:09:15 +0000535 kfree(ses->serverNOS);
Steve French39798772006-05-31 22:40:51 +0000536
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000537 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
538 if (ses->serverNOS) {
539 memcpy(ses->serverNOS, bcc_ptr, len);
540 ses->serverNOS[len] = 0;
541 }
Steve French39798772006-05-31 22:40:51 +0000542
543 bcc_ptr += len + 1;
544 bleft -= len + 1;
545
Steve French790fe572007-07-07 19:25:05 +0000546 len = strnlen(bcc_ptr, bleft);
547 if (len > bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400548 return;
Steve French39798772006-05-31 22:40:51 +0000549
Steve French9ac00b72006-09-30 04:13:17 +0000550 /* No domain field in LANMAN case. Domain is
551 returned by old servers in the SMB negprot response */
552 /* BB For newer servers which do not support Unicode,
553 but thus do return domain here we could add parsing
554 for it later, but it is not very important */
Joe Perchesf96637b2013-05-04 22:12:25 -0500555 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
Steve French39798772006-05-31 22:40:51 +0000556}
557
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400558int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
Steve French96daf2b2011-05-27 04:34:02 +0000559 struct cifs_ses *ses)
Steve French0b3cc8582009-05-04 08:37:12 +0000560{
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500561 unsigned int tioffset; /* challenge message target info area */
562 unsigned int tilen; /* challenge message target info area length */
563
Steve French0b3cc8582009-05-04 08:37:12 +0000564 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
565
566 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500567 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
Steve French0b3cc8582009-05-04 08:37:12 +0000568 return -EINVAL;
569 }
570
571 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500572 cifs_dbg(VFS, "blob signature incorrect %s\n",
573 pblob->Signature);
Steve French0b3cc8582009-05-04 08:37:12 +0000574 return -EINVAL;
575 }
576 if (pblob->MessageType != NtLmChallenge) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500577 cifs_dbg(VFS, "Incorrect message type %d\n",
578 pblob->MessageType);
Steve French0b3cc8582009-05-04 08:37:12 +0000579 return -EINVAL;
580 }
581
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500582 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
Steve French0b3cc8582009-05-04 08:37:12 +0000583 /* BB we could decode pblob->NegotiateFlags; some may be useful */
584 /* In particular we can examine sign flags */
585 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
586 we must set the MIC field of the AUTHENTICATE_MESSAGE */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500587 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
Steve French5443d132011-03-13 05:08:25 +0000588 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
589 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
Dan Carpenter4991a5f2012-01-31 11:52:01 +0300590 if (tioffset > blob_len || tioffset + tilen > blob_len) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700591 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
592 tioffset, tilen);
Dan Carpenter4991a5f2012-01-31 11:52:01 +0300593 return -EINVAL;
594 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500595 if (tilen) {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +0200596 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
597 GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500598 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700599 cifs_dbg(VFS, "Challenge target info alloc failure\n");
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500600 return -ENOMEM;
601 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500602 ses->auth_key.len = tilen;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500603 }
604
Steve French0b3cc8582009-05-04 08:37:12 +0000605 return 0;
606}
607
Steve French0b3cc8582009-05-04 08:37:12 +0000608/* BB Move to ntlmssp.c eventually */
609
610/* We do not malloc the blob, it is passed in pbuffer, because
611 it is fixed size, and small, making this approach cleaner */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400612void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
Steve French96daf2b2011-05-27 04:34:02 +0000613 struct cifs_ses *ses)
Steve French0b3cc8582009-05-04 08:37:12 +0000614{
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200615 struct TCP_Server_Info *server = cifs_ses_server(ses);
Steve French0b3cc8582009-05-04 08:37:12 +0000616 NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
617 __u32 flags;
618
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600619 memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
Steve French0b3cc8582009-05-04 08:37:12 +0000620 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
621 sec_blob->MessageType = NtLmNegotiate;
622
623 /* BB is NTLMV2 session security format easier to use here? */
624 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
625 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800626 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
627 NTLMSSP_NEGOTIATE_SEAL;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200628 if (server->sign)
Steve French745e5072010-09-08 21:09:27 +0000629 flags |= NTLMSSP_NEGOTIATE_SIGN;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200630 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800631 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
Steve French0b3cc8582009-05-04 08:37:12 +0000632
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600633 sec_blob->NegotiateFlags = cpu_to_le32(flags);
Steve French0b3cc8582009-05-04 08:37:12 +0000634
635 sec_blob->WorkstationName.BufferOffset = 0;
636 sec_blob->WorkstationName.Length = 0;
637 sec_blob->WorkstationName.MaximumLength = 0;
638
639 /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
640 sec_blob->DomainName.BufferOffset = 0;
641 sec_blob->DomainName.Length = 0;
642 sec_blob->DomainName.MaximumLength = 0;
643}
644
Jerome Marchandb8da3442016-05-26 11:52:25 +0200645static int size_of_ntlmssp_blob(struct cifs_ses *ses)
646{
647 int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
648 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
649
650 if (ses->domainName)
651 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
652 else
653 sz += 2;
654
655 if (ses->user_name)
656 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
657 else
658 sz += 2;
659
660 return sz;
661}
662
663int build_ntlmssp_auth_blob(unsigned char **pbuffer,
Shirish Pargaonkar89f150f2010-10-19 11:47:52 -0500664 u16 *buflen,
Steve French96daf2b2011-05-27 04:34:02 +0000665 struct cifs_ses *ses,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500666 const struct nls_table *nls_cp)
Steve French0b3cc8582009-05-04 08:37:12 +0000667{
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500668 int rc;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200669 AUTHENTICATE_MESSAGE *sec_blob;
Steve French0b3cc8582009-05-04 08:37:12 +0000670 __u32 flags;
671 unsigned char *tmp;
Steve French0b3cc8582009-05-04 08:37:12 +0000672
Jerome Marchandb8da3442016-05-26 11:52:25 +0200673 rc = setup_ntlmv2_rsp(ses, nls_cp);
674 if (rc) {
675 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
676 *buflen = 0;
677 goto setup_ntlmv2_ret;
678 }
679 *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
Nicholas Mc Guire126c97f2018-08-23 12:24:02 +0200680 if (!*pbuffer) {
681 rc = -ENOMEM;
682 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
683 *buflen = 0;
684 goto setup_ntlmv2_ret;
685 }
Jerome Marchandb8da3442016-05-26 11:52:25 +0200686 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
687
Steve French0b3cc8582009-05-04 08:37:12 +0000688 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
689 sec_blob->MessageType = NtLmAuthenticate;
690
691 flags = NTLMSSP_NEGOTIATE_56 |
692 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
693 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800694 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
695 NTLMSSP_NEGOTIATE_SEAL;
696 if (ses->server->sign)
Steve French0b3cc8582009-05-04 08:37:12 +0000697 flags |= NTLMSSP_NEGOTIATE_SIGN;
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800698 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
699 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
Steve French0b3cc8582009-05-04 08:37:12 +0000700
Jerome Marchandb8da3442016-05-26 11:52:25 +0200701 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600702 sec_blob->NegotiateFlags = cpu_to_le32(flags);
Steve French0b3cc8582009-05-04 08:37:12 +0000703
704 sec_blob->LmChallengeResponse.BufferOffset =
705 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
706 sec_blob->LmChallengeResponse.Length = 0;
707 sec_blob->LmChallengeResponse.MaximumLength = 0;
708
Jerome Marchandb8da3442016-05-26 11:52:25 +0200709 sec_blob->NtChallengeResponse.BufferOffset =
710 cpu_to_le32(tmp - *pbuffer);
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200711 if (ses->user_name != NULL) {
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200712 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
713 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
714 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000715
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200716 sec_blob->NtChallengeResponse.Length =
717 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
718 sec_blob->NtChallengeResponse.MaximumLength =
719 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
720 } else {
721 /*
722 * don't send an NT Response for anonymous access
723 */
724 sec_blob->NtChallengeResponse.Length = 0;
725 sec_blob->NtChallengeResponse.MaximumLength = 0;
726 }
Steve French0b3cc8582009-05-04 08:37:12 +0000727
728 if (ses->domainName == NULL) {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200729 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000730 sec_blob->DomainName.Length = 0;
731 sec_blob->DomainName.MaximumLength = 0;
732 tmp += 2;
733 } else {
734 int len;
Steve Frenchacbbb762012-01-18 22:32:33 -0600735 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
Jerome Marchand202d7722016-05-26 11:52:24 +0200736 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000737 len *= 2; /* unicode is 2 bytes each */
Jerome Marchandb8da3442016-05-26 11:52:25 +0200738 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000739 sec_blob->DomainName.Length = cpu_to_le16(len);
740 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
741 tmp += len;
742 }
743
Steve French8727c8a2011-02-25 01:11:56 -0600744 if (ses->user_name == NULL) {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200745 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000746 sec_blob->UserName.Length = 0;
747 sec_blob->UserName.MaximumLength = 0;
748 tmp += 2;
749 } else {
750 int len;
Steve Frenchacbbb762012-01-18 22:32:33 -0600751 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -0400752 CIFS_MAX_USERNAME_LEN, nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000753 len *= 2; /* unicode is 2 bytes each */
Jerome Marchandb8da3442016-05-26 11:52:25 +0200754 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000755 sec_blob->UserName.Length = cpu_to_le16(len);
756 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
757 tmp += len;
758 }
759
Jerome Marchandb8da3442016-05-26 11:52:25 +0200760 sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000761 sec_blob->WorkstationName.Length = 0;
762 sec_blob->WorkstationName.MaximumLength = 0;
763 tmp += 2;
764
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600765 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
766 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
767 && !calc_seckey(ses)) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500768 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Jerome Marchandb8da3442016-05-26 11:52:25 +0200769 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500770 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
771 sec_blob->SessionKey.MaximumLength =
772 cpu_to_le16(CIFS_CPHTXT_SIZE);
773 tmp += CIFS_CPHTXT_SIZE;
774 } else {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200775 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500776 sec_blob->SessionKey.Length = 0;
777 sec_blob->SessionKey.MaximumLength = 0;
778 }
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500779
Jerome Marchandb8da3442016-05-26 11:52:25 +0200780 *buflen = tmp - *pbuffer;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500781setup_ntlmv2_ret:
Shirish Pargaonkar89f150f2010-10-19 11:47:52 -0500782 return rc;
Steve French0b3cc8582009-05-04 08:37:12 +0000783}
Steve French0b3cc8582009-05-04 08:37:12 +0000784
Jeff Layton3f618222013-06-12 19:52:14 -0500785enum securityEnum
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530786cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
Jeff Layton3f618222013-06-12 19:52:14 -0500787{
788 switch (server->negflavor) {
789 case CIFS_NEGFLAVOR_EXTENDED:
790 switch (requested) {
791 case Kerberos:
792 case RawNTLMSSP:
793 return requested;
794 case Unspecified:
795 if (server->sec_ntlmssp &&
796 (global_secflags & CIFSSEC_MAY_NTLMSSP))
797 return RawNTLMSSP;
798 if ((server->sec_kerberos || server->sec_mskerberos) &&
799 (global_secflags & CIFSSEC_MAY_KRB5))
800 return Kerberos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500801 fallthrough;
Jeff Layton3f618222013-06-12 19:52:14 -0500802 default:
803 return Unspecified;
804 }
805 case CIFS_NEGFLAVOR_UNENCAP:
806 switch (requested) {
807 case NTLM:
808 case NTLMv2:
809 return requested;
810 case Unspecified:
811 if (global_secflags & CIFSSEC_MAY_NTLMV2)
812 return NTLMv2;
813 if (global_secflags & CIFSSEC_MAY_NTLM)
814 return NTLM;
Gustavo A. R. Silva21ac58f2020-11-20 12:24:14 -0600815 break;
Jeff Layton3f618222013-06-12 19:52:14 -0500816 default:
Sachin Prabhudde23562013-09-27 18:35:42 +0100817 break;
Jeff Layton3f618222013-06-12 19:52:14 -0500818 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500819 fallthrough; /* to attempt LANMAN authentication next */
Jeff Layton3f618222013-06-12 19:52:14 -0500820 case CIFS_NEGFLAVOR_LANMAN:
821 switch (requested) {
822 case LANMAN:
823 return requested;
824 case Unspecified:
825 if (global_secflags & CIFSSEC_MAY_LANMAN)
826 return LANMAN;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500827 fallthrough;
Jeff Layton3f618222013-06-12 19:52:14 -0500828 default:
829 return Unspecified;
830 }
831 default:
832 return Unspecified;
833 }
834}
835
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100836struct sess_data {
837 unsigned int xid;
838 struct cifs_ses *ses;
839 struct nls_table *nls_cp;
840 void (*func)(struct sess_data *);
841 int result;
842
843 /* we will send the SMB in three pieces:
844 * a fixed length beginning part, an optional
845 * SPNEGO blob (which can be zero length), and a
846 * last part which will include the strings
847 * and rest of bcc area. This allows us to avoid
848 * a large buffer 17K allocation
849 */
850 int buf0_type;
851 struct kvec iov[3];
852};
853
854static int
855sess_alloc_buffer(struct sess_data *sess_data, int wct)
856{
857 int rc;
858 struct cifs_ses *ses = sess_data->ses;
859 struct smb_hdr *smb_buf;
860
861 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
862 (void **)&smb_buf);
863
864 if (rc)
865 return rc;
866
867 sess_data->iov[0].iov_base = (char *)smb_buf;
868 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
869 /*
870 * This variable will be used to clear the buffer
871 * allocated above in case of any error in the calling function.
872 */
873 sess_data->buf0_type = CIFS_SMALL_BUFFER;
874
875 /* 2000 big enough to fit max user, domain, NOS name etc. */
876 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
877 if (!sess_data->iov[2].iov_base) {
878 rc = -ENOMEM;
879 goto out_free_smb_buf;
880 }
881
882 return 0;
883
884out_free_smb_buf:
885 kfree(smb_buf);
886 sess_data->iov[0].iov_base = NULL;
887 sess_data->iov[0].iov_len = 0;
888 sess_data->buf0_type = CIFS_NO_BUFFER;
889 return rc;
890}
891
892static void
893sess_free_buffer(struct sess_data *sess_data)
894{
895
896 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
897 sess_data->buf0_type = CIFS_NO_BUFFER;
898 kfree(sess_data->iov[2].iov_base);
899}
900
901static int
902sess_establish_session(struct sess_data *sess_data)
903{
904 struct cifs_ses *ses = sess_data->ses;
905
906 mutex_lock(&ses->server->srv_mutex);
907 if (!ses->server->session_estab) {
908 if (ses->server->sign) {
909 ses->server->session_key.response =
910 kmemdup(ses->auth_key.response,
911 ses->auth_key.len, GFP_KERNEL);
912 if (!ses->server->session_key.response) {
913 mutex_unlock(&ses->server->srv_mutex);
914 return -ENOMEM;
915 }
916 ses->server->session_key.len =
917 ses->auth_key.len;
918 }
919 ses->server->sequence_number = 0x2;
920 ses->server->session_estab = true;
921 }
922 mutex_unlock(&ses->server->srv_mutex);
923
924 cifs_dbg(FYI, "CIFS session established successfully\n");
925 spin_lock(&GlobalMid_Lock);
926 ses->status = CifsGood;
927 ses->need_reconnect = false;
928 spin_unlock(&GlobalMid_Lock);
929
930 return 0;
931}
932
933static int
934sess_sendreceive(struct sess_data *sess_data)
935{
936 int rc;
937 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
938 __u16 count;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700939 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100940
941 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
Qinglang Miao1a0e7f72020-07-25 16:56:01 +0800942 be32_add_cpu(&smb_buf->smb_buf_length, count);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100943 put_bcc(count, smb_buf);
944
945 rc = SendReceive2(sess_data->xid, sess_data->ses,
946 sess_data->iov, 3 /* num_iovecs */,
947 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700948 CIFS_LOG_ERROR, &rsp_iov);
949 cifs_small_buf_release(sess_data->iov[0].iov_base);
950 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100951
952 return rc;
953}
954
955/*
956 * LANMAN and plaintext are less secure and off by default.
957 * So we make this explicitly be turned on in kconfig (in the
958 * build) and turned on at runtime (changed from the default)
959 * in proc/fs/cifs or via mount parm. Unfortunately this is
960 * needed for old Win (e.g. Win95), some obscure NAS and OS/2
961 */
962#ifdef CONFIG_CIFS_WEAK_PW_HASH
963static void
964sess_auth_lanman(struct sess_data *sess_data)
965{
966 int rc = 0;
967 struct smb_hdr *smb_buf;
968 SESSION_SETUP_ANDX *pSMB;
969 char *bcc_ptr;
970 struct cifs_ses *ses = sess_data->ses;
971 char lnm_session_key[CIFS_AUTH_RESP_SIZE];
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100972 __u16 bytes_remaining;
973
974 /* lanman 2 style sessionsetup */
975 /* wct = 10 */
976 rc = sess_alloc_buffer(sess_data, 10);
977 if (rc)
978 goto out;
979
980 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
981 bcc_ptr = sess_data->iov[2].iov_base;
zhengbin8559ad82019-09-24 10:13:47 +0800982 (void)cifs_ssetup_hdr(ses, pSMB);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100983
984 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
985
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200986 if (ses->user_name != NULL) {
987 /* no capabilities flags in old lanman negotiation */
988 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100989
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200990 /* Calculate hash with password and copy into bcc_ptr.
991 * Encryption Key (stored as in cryptkey) gets used if the
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500992 * security mode bit in Negotiate Protocol response states
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200993 * to use challenge/response method (i.e. Password bit is 1).
994 */
995 rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
996 ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
997 true : false, lnm_session_key);
Luis de Bethencourta6b6bef2016-06-08 17:02:32 +0100998 if (rc)
999 goto out;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001000
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +02001001 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
1002 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1003 } else {
1004 pSMB->old_req.PasswordLength = 0;
1005 }
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001006
1007 /*
1008 * can not sign if LANMAN negotiated so no need
1009 * to calculate signing key? but what if server
1010 * changed to do higher than lanman dialect and
1011 * we reconnected would we ever calc signing_key?
1012 */
1013
1014 cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
1015 /* Unicode not allowed for LANMAN dialects */
1016 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1017
1018 sess_data->iov[2].iov_len = (long) bcc_ptr -
1019 (long) sess_data->iov[2].iov_base;
1020
1021 rc = sess_sendreceive(sess_data);
1022 if (rc)
1023 goto out;
1024
1025 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1026 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1027
1028 /* lanman response has a word count of 3 */
1029 if (smb_buf->WordCount != 3) {
1030 rc = -EIO;
1031 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1032 goto out;
1033 }
1034
1035 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1036 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1037
1038 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1039 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1040
1041 bytes_remaining = get_bcc(smb_buf);
1042 bcc_ptr = pByteArea(smb_buf);
1043
1044 /* BB check if Unicode and decode strings */
1045 if (bytes_remaining == 0) {
1046 /* no string area to decode, do nothing */
1047 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1048 /* unicode string area must be word-aligned */
1049 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1050 ++bcc_ptr;
1051 --bytes_remaining;
1052 }
1053 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1054 sess_data->nls_cp);
1055 } else {
1056 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1057 sess_data->nls_cp);
1058 }
1059
1060 rc = sess_establish_session(sess_data);
1061out:
1062 sess_data->result = rc;
1063 sess_data->func = NULL;
1064 sess_free_buffer(sess_data);
1065}
1066
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001067#endif
1068
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001069static void
1070sess_auth_ntlm(struct sess_data *sess_data)
1071{
1072 int rc = 0;
1073 struct smb_hdr *smb_buf;
1074 SESSION_SETUP_ANDX *pSMB;
1075 char *bcc_ptr;
1076 struct cifs_ses *ses = sess_data->ses;
1077 __u32 capabilities;
1078 __u16 bytes_remaining;
1079
1080 /* old style NTLM sessionsetup */
1081 /* wct = 13 */
1082 rc = sess_alloc_buffer(sess_data, 13);
1083 if (rc)
1084 goto out;
1085
1086 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1087 bcc_ptr = sess_data->iov[2].iov_base;
1088 capabilities = cifs_ssetup_hdr(ses, pSMB);
1089
1090 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
Stefan Metzmacher777f69b2016-05-03 10:52:30 +02001091 if (ses->user_name != NULL) {
1092 pSMB->req_no_secext.CaseInsensitivePasswordLength =
1093 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1094 pSMB->req_no_secext.CaseSensitivePasswordLength =
1095 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001096
Stefan Metzmacher777f69b2016-05-03 10:52:30 +02001097 /* calculate ntlm response and session key */
1098 rc = setup_ntlm_response(ses, sess_data->nls_cp);
1099 if (rc) {
1100 cifs_dbg(VFS, "Error %d during NTLM authentication\n",
1101 rc);
1102 goto out;
1103 }
1104
1105 /* copy ntlm response */
1106 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1107 CIFS_AUTH_RESP_SIZE);
1108 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1109 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1110 CIFS_AUTH_RESP_SIZE);
1111 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1112 } else {
1113 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1114 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001115 }
1116
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001117 if (ses->capabilities & CAP_UNICODE) {
1118 /* unicode strings must be word aligned */
1119 if (sess_data->iov[0].iov_len % 2) {
1120 *bcc_ptr = 0;
1121 bcc_ptr++;
1122 }
1123 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1124 } else {
1125 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1126 }
1127
1128
1129 sess_data->iov[2].iov_len = (long) bcc_ptr -
1130 (long) sess_data->iov[2].iov_base;
1131
1132 rc = sess_sendreceive(sess_data);
1133 if (rc)
1134 goto out;
1135
1136 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1137 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1138
1139 if (smb_buf->WordCount != 3) {
1140 rc = -EIO;
1141 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1142 goto out;
1143 }
1144
1145 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1146 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1147
1148 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1149 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1150
1151 bytes_remaining = get_bcc(smb_buf);
1152 bcc_ptr = pByteArea(smb_buf);
1153
1154 /* BB check if Unicode and decode strings */
1155 if (bytes_remaining == 0) {
1156 /* no string area to decode, do nothing */
1157 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1158 /* unicode string area must be word-aligned */
1159 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1160 ++bcc_ptr;
1161 --bytes_remaining;
1162 }
1163 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1164 sess_data->nls_cp);
1165 } else {
1166 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1167 sess_data->nls_cp);
1168 }
1169
1170 rc = sess_establish_session(sess_data);
1171out:
1172 sess_data->result = rc;
1173 sess_data->func = NULL;
1174 sess_free_buffer(sess_data);
1175 kfree(ses->auth_key.response);
1176 ses->auth_key.response = NULL;
1177}
1178
1179static void
1180sess_auth_ntlmv2(struct sess_data *sess_data)
1181{
1182 int rc = 0;
1183 struct smb_hdr *smb_buf;
1184 SESSION_SETUP_ANDX *pSMB;
1185 char *bcc_ptr;
1186 struct cifs_ses *ses = sess_data->ses;
1187 __u32 capabilities;
1188 __u16 bytes_remaining;
1189
1190 /* old style NTLM sessionsetup */
1191 /* wct = 13 */
1192 rc = sess_alloc_buffer(sess_data, 13);
1193 if (rc)
1194 goto out;
1195
1196 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1197 bcc_ptr = sess_data->iov[2].iov_base;
1198 capabilities = cifs_ssetup_hdr(ses, pSMB);
1199
1200 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1201
1202 /* LM2 password would be here if we supported it */
1203 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1204
Stefan Metzmacher1a967d62016-05-03 10:52:30 +02001205 if (ses->user_name != NULL) {
1206 /* calculate nlmv2 response and session key */
1207 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1208 if (rc) {
1209 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1210 goto out;
1211 }
1212
1213 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1214 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1215 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1216
1217 /* set case sensitive password length after tilen may get
1218 * assigned, tilen is 0 otherwise.
1219 */
1220 pSMB->req_no_secext.CaseSensitivePasswordLength =
1221 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1222 } else {
1223 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001224 }
1225
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001226 if (ses->capabilities & CAP_UNICODE) {
1227 if (sess_data->iov[0].iov_len % 2) {
1228 *bcc_ptr = 0;
1229 bcc_ptr++;
1230 }
1231 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1232 } else {
1233 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1234 }
1235
1236
1237 sess_data->iov[2].iov_len = (long) bcc_ptr -
1238 (long) sess_data->iov[2].iov_base;
1239
1240 rc = sess_sendreceive(sess_data);
1241 if (rc)
1242 goto out;
1243
1244 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1245 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1246
1247 if (smb_buf->WordCount != 3) {
1248 rc = -EIO;
1249 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1250 goto out;
1251 }
1252
1253 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1254 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1255
1256 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1257 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1258
1259 bytes_remaining = get_bcc(smb_buf);
1260 bcc_ptr = pByteArea(smb_buf);
1261
1262 /* BB check if Unicode and decode strings */
1263 if (bytes_remaining == 0) {
1264 /* no string area to decode, do nothing */
1265 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1266 /* unicode string area must be word-aligned */
1267 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1268 ++bcc_ptr;
1269 --bytes_remaining;
1270 }
1271 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1272 sess_data->nls_cp);
1273 } else {
1274 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1275 sess_data->nls_cp);
1276 }
1277
1278 rc = sess_establish_session(sess_data);
1279out:
1280 sess_data->result = rc;
1281 sess_data->func = NULL;
1282 sess_free_buffer(sess_data);
1283 kfree(ses->auth_key.response);
1284 ses->auth_key.response = NULL;
1285}
1286
Sachin Prabhuee03c642014-06-16 15:35:27 +01001287#ifdef CONFIG_CIFS_UPCALL
1288static void
1289sess_auth_kerberos(struct sess_data *sess_data)
1290{
1291 int rc = 0;
1292 struct smb_hdr *smb_buf;
1293 SESSION_SETUP_ANDX *pSMB;
1294 char *bcc_ptr;
1295 struct cifs_ses *ses = sess_data->ses;
1296 __u32 capabilities;
1297 __u16 bytes_remaining;
1298 struct key *spnego_key = NULL;
1299 struct cifs_spnego_msg *msg;
1300 u16 blob_len;
1301
1302 /* extended security */
1303 /* wct = 12 */
1304 rc = sess_alloc_buffer(sess_data, 12);
1305 if (rc)
1306 goto out;
1307
1308 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1309 bcc_ptr = sess_data->iov[2].iov_base;
1310 capabilities = cifs_ssetup_hdr(ses, pSMB);
1311
1312 spnego_key = cifs_get_spnego_key(ses);
1313 if (IS_ERR(spnego_key)) {
1314 rc = PTR_ERR(spnego_key);
1315 spnego_key = NULL;
1316 goto out;
1317 }
1318
David Howells146aa8b2015-10-21 14:04:48 +01001319 msg = spnego_key->payload.data[0];
Sachin Prabhuee03c642014-06-16 15:35:27 +01001320 /*
1321 * check version field to make sure that cifs.upcall is
1322 * sending us a response in an expected form
1323 */
1324 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001325 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1326 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhuee03c642014-06-16 15:35:27 +01001327 rc = -EKEYREJECTED;
1328 goto out_put_spnego_key;
1329 }
1330
1331 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1332 GFP_KERNEL);
1333 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001334 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1335 msg->sesskey_len);
Sachin Prabhuee03c642014-06-16 15:35:27 +01001336 rc = -ENOMEM;
1337 goto out_put_spnego_key;
1338 }
1339 ses->auth_key.len = msg->sesskey_len;
1340
1341 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1342 capabilities |= CAP_EXTENDED_SECURITY;
1343 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1344 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1345 sess_data->iov[1].iov_len = msg->secblob_len;
1346 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1347
1348 if (ses->capabilities & CAP_UNICODE) {
1349 /* unicode strings must be word aligned */
1350 if ((sess_data->iov[0].iov_len
1351 + sess_data->iov[1].iov_len) % 2) {
1352 *bcc_ptr = 0;
1353 bcc_ptr++;
1354 }
1355 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1356 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1357 } else {
1358 /* BB: is this right? */
1359 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1360 }
1361
1362 sess_data->iov[2].iov_len = (long) bcc_ptr -
1363 (long) sess_data->iov[2].iov_base;
1364
1365 rc = sess_sendreceive(sess_data);
1366 if (rc)
1367 goto out_put_spnego_key;
1368
1369 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1370 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1371
1372 if (smb_buf->WordCount != 4) {
1373 rc = -EIO;
1374 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1375 goto out_put_spnego_key;
1376 }
1377
1378 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1379 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1380
1381 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1382 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1383
1384 bytes_remaining = get_bcc(smb_buf);
1385 bcc_ptr = pByteArea(smb_buf);
1386
1387 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1388 if (blob_len > bytes_remaining) {
1389 cifs_dbg(VFS, "bad security blob length %d\n",
1390 blob_len);
1391 rc = -EINVAL;
1392 goto out_put_spnego_key;
1393 }
1394 bcc_ptr += blob_len;
1395 bytes_remaining -= blob_len;
1396
1397 /* BB check if Unicode and decode strings */
1398 if (bytes_remaining == 0) {
1399 /* no string area to decode, do nothing */
1400 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1401 /* unicode string area must be word-aligned */
1402 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1403 ++bcc_ptr;
1404 --bytes_remaining;
1405 }
1406 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1407 sess_data->nls_cp);
1408 } else {
1409 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1410 sess_data->nls_cp);
1411 }
1412
1413 rc = sess_establish_session(sess_data);
1414out_put_spnego_key:
1415 key_invalidate(spnego_key);
1416 key_put(spnego_key);
1417out:
1418 sess_data->result = rc;
1419 sess_data->func = NULL;
1420 sess_free_buffer(sess_data);
1421 kfree(ses->auth_key.response);
1422 ses->auth_key.response = NULL;
1423}
1424
Sachin Prabhuee03c642014-06-16 15:35:27 +01001425#endif /* ! CONFIG_CIFS_UPCALL */
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001426
Sachin Prabhucc87c472014-06-16 15:35:28 +01001427/*
1428 * The required kvec buffers have to be allocated before calling this
1429 * function.
1430 */
1431static int
1432_sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
Steve French39798772006-05-31 22:40:51 +00001433{
Steve French39798772006-05-31 22:40:51 +00001434 SESSION_SETUP_ANDX *pSMB;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001435 struct cifs_ses *ses = sess_data->ses;
Steve French39798772006-05-31 22:40:51 +00001436 __u32 capabilities;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001437 char *bcc_ptr;
Steve French254e55e2006-06-04 05:53:15 +00001438
Sachin Prabhucc87c472014-06-16 15:35:28 +01001439 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001440
1441 capabilities = cifs_ssetup_hdr(ses, pSMB);
1442 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1443 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1444 return -ENOSYS;
Jeff Layton3534b852013-05-24 07:41:01 -04001445 }
Steve French39798772006-05-31 22:40:51 +00001446
Sachin Prabhucc87c472014-06-16 15:35:28 +01001447 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1448 capabilities |= CAP_EXTENDED_SECURITY;
1449 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1450
1451 bcc_ptr = sess_data->iov[2].iov_base;
1452 /* unicode strings must be word aligned */
1453 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1454 *bcc_ptr = 0;
1455 bcc_ptr++;
1456 }
1457 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1458
1459 sess_data->iov[2].iov_len = (long) bcc_ptr -
1460 (long) sess_data->iov[2].iov_base;
1461
1462 return 0;
1463}
1464
1465static void
1466sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1467
1468static void
1469sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1470{
1471 int rc;
1472 struct smb_hdr *smb_buf;
1473 SESSION_SETUP_ANDX *pSMB;
1474 struct cifs_ses *ses = sess_data->ses;
1475 __u16 bytes_remaining;
1476 char *bcc_ptr;
1477 u16 blob_len;
1478
1479 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1480
1481 /*
1482 * if memory allocation is successful, caller of this function
1483 * frees it.
1484 */
1485 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1486 if (!ses->ntlmssp) {
1487 rc = -ENOMEM;
1488 goto out;
1489 }
1490 ses->ntlmssp->sesskey_per_smbsess = false;
1491
1492 /* wct = 12 */
1493 rc = sess_alloc_buffer(sess_data, 12);
1494 if (rc)
1495 goto out;
1496
1497 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1498
1499 /* Build security blob before we assemble the request */
1500 build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1501 sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1502 sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1503 pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1504
1505 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1506 if (rc)
1507 goto out;
1508
1509 rc = sess_sendreceive(sess_data);
1510
1511 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1512 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1513
1514 /* If true, rc here is expected and not an error */
1515 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1516 smb_buf->Status.CifsError ==
1517 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1518 rc = 0;
1519
1520 if (rc)
1521 goto out;
1522
1523 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1524
1525 if (smb_buf->WordCount != 4) {
1526 rc = -EIO;
1527 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1528 goto out;
1529 }
1530
1531 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1532 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1533
1534 bytes_remaining = get_bcc(smb_buf);
1535 bcc_ptr = pByteArea(smb_buf);
1536
1537 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1538 if (blob_len > bytes_remaining) {
1539 cifs_dbg(VFS, "bad security blob length %d\n",
1540 blob_len);
1541 rc = -EINVAL;
1542 goto out;
1543 }
1544
1545 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1546out:
1547 sess_free_buffer(sess_data);
1548
1549 if (!rc) {
1550 sess_data->func = sess_auth_rawntlmssp_authenticate;
1551 return;
1552 }
1553
1554 /* Else error. Cleanup */
1555 kfree(ses->auth_key.response);
1556 ses->auth_key.response = NULL;
1557 kfree(ses->ntlmssp);
1558 ses->ntlmssp = NULL;
1559
1560 sess_data->func = NULL;
1561 sess_data->result = rc;
1562}
1563
1564static void
1565sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1566{
1567 int rc;
1568 struct smb_hdr *smb_buf;
1569 SESSION_SETUP_ANDX *pSMB;
1570 struct cifs_ses *ses = sess_data->ses;
1571 __u16 bytes_remaining;
1572 char *bcc_ptr;
Jerome Marchandb8da3442016-05-26 11:52:25 +02001573 unsigned char *ntlmsspblob = NULL;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001574 u16 blob_len;
1575
1576 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1577
1578 /* wct = 12 */
1579 rc = sess_alloc_buffer(sess_data, 12);
1580 if (rc)
1581 goto out;
1582
1583 /* Build security blob before we assemble the request */
1584 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1585 smb_buf = (struct smb_hdr *)pSMB;
Jerome Marchandb8da3442016-05-26 11:52:25 +02001586 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
Sachin Prabhucc87c472014-06-16 15:35:28 +01001587 &blob_len, ses, sess_data->nls_cp);
1588 if (rc)
1589 goto out_free_ntlmsspblob;
1590 sess_data->iov[1].iov_len = blob_len;
1591 sess_data->iov[1].iov_base = ntlmsspblob;
1592 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1593 /*
1594 * Make sure that we tell the server that we are using
1595 * the uid that it just gave us back on the response
1596 * (challenge)
1597 */
1598 smb_buf->Uid = ses->Suid;
1599
1600 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1601 if (rc)
1602 goto out_free_ntlmsspblob;
1603
1604 rc = sess_sendreceive(sess_data);
1605 if (rc)
1606 goto out_free_ntlmsspblob;
1607
1608 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1609 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1610 if (smb_buf->WordCount != 4) {
1611 rc = -EIO;
1612 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1613 goto out_free_ntlmsspblob;
1614 }
1615
1616 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1617 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1618
Sachin Prabhuee9bbf42014-12-03 12:26:36 +00001619 if (ses->Suid != smb_buf->Uid) {
1620 ses->Suid = smb_buf->Uid;
1621 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1622 }
1623
Sachin Prabhucc87c472014-06-16 15:35:28 +01001624 bytes_remaining = get_bcc(smb_buf);
1625 bcc_ptr = pByteArea(smb_buf);
1626 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1627 if (blob_len > bytes_remaining) {
1628 cifs_dbg(VFS, "bad security blob length %d\n",
1629 blob_len);
1630 rc = -EINVAL;
1631 goto out_free_ntlmsspblob;
1632 }
1633 bcc_ptr += blob_len;
1634 bytes_remaining -= blob_len;
1635
1636
1637 /* BB check if Unicode and decode strings */
1638 if (bytes_remaining == 0) {
1639 /* no string area to decode, do nothing */
1640 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1641 /* unicode string area must be word-aligned */
1642 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1643 ++bcc_ptr;
1644 --bytes_remaining;
1645 }
1646 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1647 sess_data->nls_cp);
1648 } else {
1649 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1650 sess_data->nls_cp);
1651 }
1652
1653out_free_ntlmsspblob:
1654 kfree(ntlmsspblob);
1655out:
1656 sess_free_buffer(sess_data);
1657
1658 if (!rc)
1659 rc = sess_establish_session(sess_data);
1660
1661 /* Cleanup */
1662 kfree(ses->auth_key.response);
1663 ses->auth_key.response = NULL;
1664 kfree(ses->ntlmssp);
1665 ses->ntlmssp = NULL;
1666
1667 sess_data->func = NULL;
1668 sess_data->result = rc;
1669}
1670
Steve French27924072014-07-11 07:47:01 -05001671static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
Sachin Prabhucc87c472014-06-16 15:35:28 +01001672{
1673 int type;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001674
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301675 type = cifs_select_sectype(ses->server, ses->sectype);
Joe Perchesf96637b2013-05-04 22:12:25 -05001676 cifs_dbg(FYI, "sess setup type %d\n", type);
Jeff Layton3f618222013-06-12 19:52:14 -05001677 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001678 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Jeff Layton3f618222013-06-12 19:52:14 -05001679 return -EINVAL;
1680 }
1681
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001682 switch (type) {
1683 case LANMAN:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001684 /* LANMAN and plaintext are less secure and off by default.
1685 * So we make this explicitly be turned on in kconfig (in the
1686 * build) and turned on at runtime (changed from the default)
1687 * in proc/fs/cifs or via mount parm. Unfortunately this is
1688 * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
1689#ifdef CONFIG_CIFS_WEAK_PW_HASH
1690 sess_data->func = sess_auth_lanman;
1691 break;
1692#else
1693 return -EOPNOTSUPP;
1694#endif
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001695 case NTLM:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001696 sess_data->func = sess_auth_ntlm;
1697 break;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001698 case NTLMv2:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001699 sess_data->func = sess_auth_ntlmv2;
1700 break;
Sachin Prabhuee03c642014-06-16 15:35:27 +01001701 case Kerberos:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001702#ifdef CONFIG_CIFS_UPCALL
1703 sess_data->func = sess_auth_kerberos;
1704 break;
1705#else
1706 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1707 return -ENOSYS;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001708#endif /* CONFIG_CIFS_UPCALL */
1709 case RawNTLMSSP:
1710 sess_data->func = sess_auth_rawntlmssp_negotiate;
1711 break;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001712 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001713 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhucc87c472014-06-16 15:35:28 +01001714 return -ENOSYS;
Steve French39798772006-05-31 22:40:51 +00001715 }
1716
Sachin Prabhucc87c472014-06-16 15:35:28 +01001717 return 0;
1718}
Steve French24424212007-11-16 23:37:35 +00001719
Sachin Prabhucc87c472014-06-16 15:35:28 +01001720int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1721 const struct nls_table *nls_cp)
1722{
1723 int rc = 0;
1724 struct sess_data *sess_data;
Steve French39798772006-05-31 22:40:51 +00001725
Sachin Prabhucc87c472014-06-16 15:35:28 +01001726 if (ses == NULL) {
1727 WARN(1, "%s: ses == NULL!", __func__);
1728 return -EINVAL;
Steve French0b3cc8582009-05-04 08:37:12 +00001729 }
Sachin Prabhucc87c472014-06-16 15:35:28 +01001730
1731 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1732 if (!sess_data)
1733 return -ENOMEM;
1734
1735 rc = select_sec(ses, sess_data);
Steve French0b3cc8582009-05-04 08:37:12 +00001736 if (rc)
Sachin Prabhucc87c472014-06-16 15:35:28 +01001737 goto out;
Steve French0b3cc8582009-05-04 08:37:12 +00001738
Sachin Prabhucc87c472014-06-16 15:35:28 +01001739 sess_data->xid = xid;
1740 sess_data->ses = ses;
1741 sess_data->buf0_type = CIFS_NO_BUFFER;
1742 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve French39798772006-05-31 22:40:51 +00001743
Sachin Prabhucc87c472014-06-16 15:35:28 +01001744 while (sess_data->func)
1745 sess_data->func(sess_data);
Steve French39798772006-05-31 22:40:51 +00001746
Sachin Prabhucc87c472014-06-16 15:35:28 +01001747 /* Store result before we free sess_data */
1748 rc = sess_data->result;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001749
1750out:
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001751 kfree(sess_data);
1752 return rc;
Steve French39798772006-05-31 22:40:51 +00001753}