blob: c5785fd3f52e841a8ef046c43b0d4226f5c3fafb [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Steve French39798772006-05-31 22:40:51 +00002/*
3 * fs/cifs/sess.c
4 *
5 * SMB/CIFS session setup handling routines
6 *
Steve Frenchd185cda2009-04-30 17:45:10 +00007 * Copyright (c) International Business Machines Corp., 2006, 2009
Steve French39798772006-05-31 22:40:51 +00008 * Author(s): Steve French (sfrench@us.ibm.com)
9 *
Steve French39798772006-05-31 22:40:51 +000010 */
11
12#include "cifspdu.h"
13#include "cifsglob.h"
14#include "cifsproto.h"
15#include "cifs_unicode.h"
16#include "cifs_debug.h"
17#include "ntlmssp.h"
18#include "nterr.h"
Steve French9c535882006-06-01 05:09:10 +000019#include <linux/utsname.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Steve French24424212007-11-16 23:37:35 +000021#include "cifs_spnego.h"
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020022#include "smb2proto.h"
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060023#include "fs_context.h"
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020024
Ronnie Sahlberg387ec582020-12-14 16:40:20 +100025static int
26cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
27 struct cifs_server_iface *iface);
28
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020029bool
30is_server_using_iface(struct TCP_Server_Info *server,
31 struct cifs_server_iface *iface)
32{
33 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
34 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
35 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
36 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
37
38 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
39 return false;
40 if (server->dstaddr.ss_family == AF_INET) {
41 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
42 return false;
43 } else if (server->dstaddr.ss_family == AF_INET6) {
44 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
45 sizeof(i6->sin6_addr)) != 0)
46 return false;
47 } else {
48 /* unknown family.. */
49 return false;
50 }
51 return true;
52}
53
54bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
55{
56 int i;
57
58 for (i = 0; i < ses->chan_count; i++) {
59 if (is_server_using_iface(ses->chans[i].server, iface))
60 return true;
61 }
62 return false;
63}
64
65/* returns number of channels added */
Ronnie Sahlberg387ec582020-12-14 16:40:20 +100066int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020067{
68 int old_chan_count = ses->chan_count;
69 int left = ses->chan_max - ses->chan_count;
70 int i = 0;
71 int rc = 0;
Aurelien Aptel65a37a32019-11-20 17:15:59 +010072 int tries = 0;
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +010073 struct cifs_server_iface *ifaces = NULL;
74 size_t iface_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020075
76 if (left <= 0) {
77 cifs_dbg(FYI,
78 "ses already at max_channels (%zu), nothing to open\n",
79 ses->chan_max);
80 return 0;
81 }
82
83 if (ses->server->dialect < SMB30_PROT_ID) {
84 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
85 return 0;
86 }
87
Steve French9c2dc112021-05-07 20:00:41 -050088 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
89 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
90 ses->chan_max = 1;
91 return 0;
92 }
93
Aurelien Aptel65a37a32019-11-20 17:15:59 +010094 /*
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +010095 * Make a copy of the iface list at the time and use that
96 * instead so as to not hold the iface spinlock for opening
97 * channels
98 */
99 spin_lock(&ses->iface_lock);
100 iface_count = ses->iface_count;
101 if (iface_count <= 0) {
102 spin_unlock(&ses->iface_lock);
Aurelien Aptel343a1b72020-02-06 10:19:11 +0100103 cifs_dbg(VFS, "no iface list available to open channels\n");
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100104 return 0;
105 }
106 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
107 GFP_ATOMIC);
108 if (!ifaces) {
109 spin_unlock(&ses->iface_lock);
110 return 0;
111 }
112 spin_unlock(&ses->iface_lock);
113
114 /*
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100115 * Keep connecting to same, fastest, iface for all channels as
116 * long as its RSS. Try next fastest one if not RSS or channel
117 * creation fails.
118 */
119 while (left > 0) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200120 struct cifs_server_iface *iface;
121
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100122 tries++;
123 if (tries > 3*ses->chan_max) {
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500124 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100125 left);
126 break;
127 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200128
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100129 iface = &ifaces[i];
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100130 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100131 i = (i+1) % iface_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200132 continue;
133 }
134
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000135 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100136 if (rc) {
137 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
138 i, rc);
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100139 i = (i+1) % iface_count;
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100140 continue;
141 }
142
143 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
144 i);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200145 left--;
146 }
147
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100148 kfree(ifaces);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200149 return ses->chan_count - old_chan_count;
150}
151
Aurelien Aptel2f589672020-04-24 16:55:31 +0200152/*
153 * If server is a channel of ses, return the corresponding enclosing
154 * cifs_chan otherwise return NULL.
155 */
156struct cifs_chan *
157cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
158{
159 int i;
160
161 for (i = 0; i < ses->chan_count; i++) {
162 if (ses->chans[i].server == server)
163 return &ses->chans[i];
164 }
165 return NULL;
166}
167
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000168static int
169cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
170 struct cifs_server_iface *iface)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200171{
172 struct cifs_chan *chan;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600173 struct smb3_fs_context ctx = {NULL};
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200174 static const char unc_fmt[] = "\\%s\\foo";
175 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
176 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
177 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
178 int rc;
179 unsigned int xid = get_xid();
180
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200181 if (iface->sockaddr.ss_family == AF_INET)
Joe Perchesa0a30362020-04-14 22:42:53 -0700182 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
183 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
184 &ipv4->sin_addr);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200185 else
Aurelien Aptel5e538952021-05-28 16:32:48 +0200186 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
Joe Perchesa0a30362020-04-14 22:42:53 -0700187 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
188 &ipv6->sin6_addr);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200189
190 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600191 * Setup a ctx with mostly the same info as the existing
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200192 * session and overwrite it with the requested iface data.
193 *
194 * We need to setup at least the fields used for negprot and
195 * sesssetup.
196 *
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600197 * We only need the ctx here, so we can reuse memory from
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200198 * the session and server without caring about memory
199 * management.
200 */
201
202 /* Always make new connection for now (TODO?) */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600203 ctx.nosharesock = true;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200204
205 /* Auth */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600206 ctx.domainauto = ses->domainAuto;
207 ctx.domainname = ses->domainName;
208 ctx.username = ses->user_name;
209 ctx.password = ses->password;
210 ctx.sectype = ses->sectype;
211 ctx.sign = ses->sign;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200212
213 /* UNC and paths */
214 /* XXX: Use ses->server->hostname? */
Steve Frenchb438fcf2021-02-20 19:24:11 -0600215 sprintf(unc, unc_fmt, ses->ip_addr);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600216 ctx.UNC = unc;
217 ctx.prepath = "";
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200218
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500219 /* Reuse same version as master connection */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600220 ctx.vals = ses->server->vals;
221 ctx.ops = ses->server->ops;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200222
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600223 ctx.noblocksnd = ses->server->noblocksnd;
224 ctx.noautotune = ses->server->noautotune;
225 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
226 ctx.echo_interval = ses->server->echo_interval / HZ;
Aurelien Aptela249cc82021-03-04 17:42:21 +0000227 ctx.max_credits = ses->server->max_credits;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200228
229 /*
230 * This will be used for encoding/decoding user/domain/pw
231 * during sess setup auth.
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200232 */
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000233 ctx.local_nls = cifs_sb->local_nls;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200234
235 /* Use RDMA if possible */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600236 ctx.rdma = iface->rdma_capable;
237 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200238
239 /* reuse master con client guid */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600240 memcpy(&ctx.client_guid, ses->server->client_guid,
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200241 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600242 ctx.use_client_guid = true;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200243
244 mutex_lock(&ses->session_mutex);
245
Aurelien Aptel8eec7952020-04-24 15:24:05 +0200246 chan = ses->binding_chan = &ses->chans[ses->chan_count];
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600247 chan->server = cifs_get_tcp_session(&ctx);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200248 if (IS_ERR(chan->server)) {
249 rc = PTR_ERR(chan->server);
250 chan->server = NULL;
251 goto out;
252 }
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -0300253 spin_lock(&cifs_tcp_ses_lock);
254 chan->server->is_channel = true;
255 spin_unlock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200256
257 /*
258 * We need to allocate the server crypto now as we will need
259 * to sign packets before we generate the channel signing key
260 * (we sign with the session key)
261 */
262 rc = smb311_crypto_shash_allocate(chan->server);
263 if (rc) {
264 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
265 goto out;
266 }
267
268 ses->binding = true;
269 rc = cifs_negotiate_protocol(xid, ses);
270 if (rc)
271 goto out;
272
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000273 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200274 if (rc)
275 goto out;
276
277 /* success, put it on the list
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500278 * XXX: sharing ses between 2 tcp servers is not possible, the
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200279 * way "internal" linked lists works in linux makes element
280 * only able to belong to one list
281 *
282 * the binding session is already established so the rest of
283 * the code should be able to look it up, no need to add the
284 * ses to the new server.
285 */
286
287 ses->chan_count++;
288 atomic_set(&ses->chan_seq, 0);
289out:
290 ses->binding = false;
Aurelien Aptel8eec7952020-04-24 15:24:05 +0200291 ses->binding_chan = NULL;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200292 mutex_unlock(&ses->session_mutex);
293
294 if (rc && chan->server)
295 cifs_put_tcp_session(chan->server, 0);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200296
297 return rc;
298}
Steve French39798772006-05-31 22:40:51 +0000299
Steve French96daf2b2011-05-27 04:34:02 +0000300static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
Steve French39798772006-05-31 22:40:51 +0000301{
302 __u32 capabilities = 0;
303
304 /* init fields common to all four types of SessSetup */
Steve Frencheca6acf2009-02-20 05:43:09 +0000305 /* Note that offsets for first seven fields in req struct are same */
306 /* in CIFS Specs so does not matter which of 3 forms of struct */
307 /* that we use in next few lines */
308 /* Note that header is initialized to zero in header_assemble */
Steve French39798772006-05-31 22:40:51 +0000309 pSMB->req.AndXCommand = 0xFF;
Jeff Laytonc974bef2011-10-11 06:41:32 -0400310 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
311 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
312 USHRT_MAX));
Steve French39798772006-05-31 22:40:51 +0000313 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
Fabian Frederickbc09d142014-12-10 15:41:15 -0800314 pSMB->req.VcNumber = cpu_to_le16(1);
Steve French39798772006-05-31 22:40:51 +0000315
316 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
317
Steve French790fe572007-07-07 19:25:05 +0000318 /* BB verify whether signing required on neg or just on auth frame
Steve French39798772006-05-31 22:40:51 +0000319 (and NTLM case) */
320
321 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
322 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
323
Jeff Layton38d77c52013-05-26 07:01:00 -0400324 if (ses->server->sign)
Steve French39798772006-05-31 22:40:51 +0000325 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
326
327 if (ses->capabilities & CAP_UNICODE) {
328 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
329 capabilities |= CAP_UNICODE;
330 }
331 if (ses->capabilities & CAP_STATUS32) {
332 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
333 capabilities |= CAP_STATUS32;
334 }
335 if (ses->capabilities & CAP_DFS) {
336 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
337 capabilities |= CAP_DFS;
338 }
Steve French26f57362007-08-30 22:09:15 +0000339 if (ses->capabilities & CAP_UNIX)
Steve French39798772006-05-31 22:40:51 +0000340 capabilities |= CAP_UNIX;
Steve French39798772006-05-31 22:40:51 +0000341
Steve French39798772006-05-31 22:40:51 +0000342 return capabilities;
343}
344
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000345static void
346unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
347{
348 char *bcc_ptr = *pbcc_area;
349 int bytes_ret = 0;
350
351 /* Copy OS version */
Steve Frenchacbbb762012-01-18 22:32:33 -0600352 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
353 nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000354 bcc_ptr += 2 * bytes_ret;
Steve Frenchacbbb762012-01-18 22:32:33 -0600355 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
356 32, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000357 bcc_ptr += 2 * bytes_ret;
358 bcc_ptr += 2; /* trailing null */
359
Steve Frenchacbbb762012-01-18 22:32:33 -0600360 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
361 32, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000362 bcc_ptr += 2 * bytes_ret;
363 bcc_ptr += 2; /* trailing null */
364
365 *pbcc_area = bcc_ptr;
366}
367
Steve French96daf2b2011-05-27 04:34:02 +0000368static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000369 const struct nls_table *nls_cp)
370{
371 char *bcc_ptr = *pbcc_area;
372 int bytes_ret = 0;
373
374 /* copy domain */
375 if (ses->domainName == NULL) {
376 /* Sending null domain better than using a bogus domain name (as
377 we did briefly in 2.6.18) since server will use its default */
378 *bcc_ptr = 0;
379 *(bcc_ptr+1) = 0;
380 bytes_ret = 0;
381 } else
Steve Frenchacbbb762012-01-18 22:32:33 -0600382 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
Chen Gang057d6332013-07-19 09:01:36 +0800383 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000384 bcc_ptr += 2 * bytes_ret;
385 bcc_ptr += 2; /* account for null terminator */
386
387 *pbcc_area = bcc_ptr;
388}
389
390
Steve French96daf2b2011-05-27 04:34:02 +0000391static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
Steve French790fe572007-07-07 19:25:05 +0000392 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000393{
Steve French790fe572007-07-07 19:25:05 +0000394 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000395 int bytes_ret = 0;
396
397 /* BB FIXME add check that strings total less
398 than 335 or will need to send them as arrays */
399
Steve French0223cf02006-06-27 19:50:57 +0000400 /* unicode strings, must be word aligned before the call */
401/* if ((long) bcc_ptr % 2) {
Steve French39798772006-05-31 22:40:51 +0000402 *bcc_ptr = 0;
403 bcc_ptr++;
Steve French0223cf02006-06-27 19:50:57 +0000404 } */
Steve French39798772006-05-31 22:40:51 +0000405 /* copy user */
Steve French8727c8a2011-02-25 01:11:56 -0600406 if (ses->user_name == NULL) {
Steve French6e659c62006-11-08 23:10:46 +0000407 /* null user mount */
408 *bcc_ptr = 0;
409 *(bcc_ptr+1) = 0;
Steve French301a6a32010-02-06 07:08:53 +0000410 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -0600411 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -0400412 CIFS_MAX_USERNAME_LEN, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000413 }
414 bcc_ptr += 2 * bytes_ret;
415 bcc_ptr += 2; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000416
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000417 unicode_domain_string(&bcc_ptr, ses, nls_cp);
418 unicode_oslm_strings(&bcc_ptr, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000419
420 *pbcc_area = bcc_ptr;
421}
422
Steve French96daf2b2011-05-27 04:34:02 +0000423static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
Steve French790fe572007-07-07 19:25:05 +0000424 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000425{
Steve French790fe572007-07-07 19:25:05 +0000426 char *bcc_ptr = *pbcc_area;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000427 int len;
Steve French39798772006-05-31 22:40:51 +0000428
429 /* copy user */
430 /* BB what about null user mounts - check that we do this BB */
Steve French790fe572007-07-07 19:25:05 +0000431 /* copy user */
Shirish Pargaonkarde47a412012-02-02 15:28:28 -0600432 if (ses->user_name != NULL) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000433 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
434 if (WARN_ON_ONCE(len < 0))
435 len = CIFS_MAX_USERNAME_LEN - 1;
436 bcc_ptr += len;
Shirish Pargaonkarde47a412012-02-02 15:28:28 -0600437 }
Steve French8727c8a2011-02-25 01:11:56 -0600438 /* else null user mount */
Steve French39798772006-05-31 22:40:51 +0000439 *bcc_ptr = 0;
Steve French790fe572007-07-07 19:25:05 +0000440 bcc_ptr++; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000441
Steve French790fe572007-07-07 19:25:05 +0000442 /* copy domain */
Steve French790fe572007-07-07 19:25:05 +0000443 if (ses->domainName != NULL) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000444 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
445 if (WARN_ON_ONCE(len < 0))
446 len = CIFS_MAX_DOMAINNAME_LEN - 1;
447 bcc_ptr += len;
Steve French790fe572007-07-07 19:25:05 +0000448 } /* else we will send a null domain name
Steve French6e659c62006-11-08 23:10:46 +0000449 so the server will default to its own domain */
Steve French39798772006-05-31 22:40:51 +0000450 *bcc_ptr = 0;
451 bcc_ptr++;
452
453 /* BB check for overflow here */
454
455 strcpy(bcc_ptr, "Linux version ");
456 bcc_ptr += strlen("Linux version ");
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700457 strcpy(bcc_ptr, init_utsname()->release);
458 bcc_ptr += strlen(init_utsname()->release) + 1;
Steve French39798772006-05-31 22:40:51 +0000459
460 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
461 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
462
Steve French790fe572007-07-07 19:25:05 +0000463 *pbcc_area = bcc_ptr;
Steve French39798772006-05-31 22:40:51 +0000464}
465
Jeff Layton59140792009-04-30 07:16:21 -0400466static void
Steve French96daf2b2011-05-27 04:34:02 +0000467decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
Jeff Layton59140792009-04-30 07:16:21 -0400468 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000469{
Jeff Layton59140792009-04-30 07:16:21 -0400470 int len;
Steve French790fe572007-07-07 19:25:05 +0000471 char *data = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000472
Joe Perchesf96637b2013-05-04 22:12:25 -0500473 cifs_dbg(FYI, "bleft %d\n", bleft);
Steve French39798772006-05-31 22:40:51 +0000474
Steve French26f57362007-08-30 22:09:15 +0000475 kfree(ses->serverOS);
Steve Frenchacbbb762012-01-18 22:32:33 -0600476 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500477 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
Jeff Layton59140792009-04-30 07:16:21 -0400478 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
479 data += len;
480 bleft -= len;
481 if (bleft <= 0)
482 return;
Steve French39798772006-05-31 22:40:51 +0000483
Steve French26f57362007-08-30 22:09:15 +0000484 kfree(ses->serverNOS);
Steve Frenchacbbb762012-01-18 22:32:33 -0600485 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500486 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
Jeff Layton59140792009-04-30 07:16:21 -0400487 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
488 data += len;
489 bleft -= len;
490 if (bleft <= 0)
491 return;
Steve French39798772006-05-31 22:40:51 +0000492
Steve French26f57362007-08-30 22:09:15 +0000493 kfree(ses->serverDomain);
Steve Frenchacbbb762012-01-18 22:32:33 -0600494 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500495 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
Steve French790fe572007-07-07 19:25:05 +0000496
Jeff Layton59140792009-04-30 07:16:21 -0400497 return;
Steve French39798772006-05-31 22:40:51 +0000498}
499
Jeff Layton7d066452013-05-24 07:41:00 -0400500static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
501 struct cifs_ses *ses,
502 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000503{
Steve French39798772006-05-31 22:40:51 +0000504 int len;
Steve French790fe572007-07-07 19:25:05 +0000505 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000506
Joe Perchesf96637b2013-05-04 22:12:25 -0500507 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
Steve French50c2f752007-07-13 00:33:32 +0000508
Steve French39798772006-05-31 22:40:51 +0000509 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000510 if (len >= bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400511 return;
Steve French50c2f752007-07-13 00:33:32 +0000512
Steve French26f57362007-08-30 22:09:15 +0000513 kfree(ses->serverOS);
Steve French39798772006-05-31 22:40:51 +0000514
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000515 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
Namjae Jeon27b7edc2014-08-20 19:39:28 +0900516 if (ses->serverOS) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000517 memcpy(ses->serverOS, bcc_ptr, len);
518 ses->serverOS[len] = 0;
Namjae Jeon27b7edc2014-08-20 19:39:28 +0900519 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
520 cifs_dbg(FYI, "OS/2 server\n");
521 }
Steve French39798772006-05-31 22:40:51 +0000522
523 bcc_ptr += len + 1;
524 bleft -= len + 1;
525
526 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000527 if (len >= bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400528 return;
Steve French39798772006-05-31 22:40:51 +0000529
Steve French26f57362007-08-30 22:09:15 +0000530 kfree(ses->serverNOS);
Steve French39798772006-05-31 22:40:51 +0000531
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000532 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
533 if (ses->serverNOS) {
534 memcpy(ses->serverNOS, bcc_ptr, len);
535 ses->serverNOS[len] = 0;
536 }
Steve French39798772006-05-31 22:40:51 +0000537
538 bcc_ptr += len + 1;
539 bleft -= len + 1;
540
Steve French790fe572007-07-07 19:25:05 +0000541 len = strnlen(bcc_ptr, bleft);
542 if (len > bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400543 return;
Steve French39798772006-05-31 22:40:51 +0000544
Steve French9ac00b72006-09-30 04:13:17 +0000545 /* No domain field in LANMAN case. Domain is
546 returned by old servers in the SMB negprot response */
547 /* BB For newer servers which do not support Unicode,
548 but thus do return domain here we could add parsing
549 for it later, but it is not very important */
Joe Perchesf96637b2013-05-04 22:12:25 -0500550 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
Steve French39798772006-05-31 22:40:51 +0000551}
552
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400553int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
Steve French96daf2b2011-05-27 04:34:02 +0000554 struct cifs_ses *ses)
Steve French0b3cc8582009-05-04 08:37:12 +0000555{
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500556 unsigned int tioffset; /* challenge message target info area */
557 unsigned int tilen; /* challenge message target info area length */
558
Steve French0b3cc8582009-05-04 08:37:12 +0000559 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
560
561 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500562 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
Steve French0b3cc8582009-05-04 08:37:12 +0000563 return -EINVAL;
564 }
565
566 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500567 cifs_dbg(VFS, "blob signature incorrect %s\n",
568 pblob->Signature);
Steve French0b3cc8582009-05-04 08:37:12 +0000569 return -EINVAL;
570 }
571 if (pblob->MessageType != NtLmChallenge) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500572 cifs_dbg(VFS, "Incorrect message type %d\n",
573 pblob->MessageType);
Steve French0b3cc8582009-05-04 08:37:12 +0000574 return -EINVAL;
575 }
576
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500577 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
Steve French0b3cc8582009-05-04 08:37:12 +0000578 /* BB we could decode pblob->NegotiateFlags; some may be useful */
579 /* In particular we can examine sign flags */
580 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
581 we must set the MIC field of the AUTHENTICATE_MESSAGE */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500582 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
Steve French5443d132011-03-13 05:08:25 +0000583 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
584 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
Dan Carpenter4991a5f2012-01-31 11:52:01 +0300585 if (tioffset > blob_len || tioffset + tilen > blob_len) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700586 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
587 tioffset, tilen);
Dan Carpenter4991a5f2012-01-31 11:52:01 +0300588 return -EINVAL;
589 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500590 if (tilen) {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +0200591 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
592 GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500593 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700594 cifs_dbg(VFS, "Challenge target info alloc failure\n");
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500595 return -ENOMEM;
596 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500597 ses->auth_key.len = tilen;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500598 }
599
Steve French0b3cc8582009-05-04 08:37:12 +0000600 return 0;
601}
602
Steve French0b3cc8582009-05-04 08:37:12 +0000603/* BB Move to ntlmssp.c eventually */
604
605/* We do not malloc the blob, it is passed in pbuffer, because
606 it is fixed size, and small, making this approach cleaner */
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400607void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
Steve French96daf2b2011-05-27 04:34:02 +0000608 struct cifs_ses *ses)
Steve French0b3cc8582009-05-04 08:37:12 +0000609{
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200610 struct TCP_Server_Info *server = cifs_ses_server(ses);
Steve French0b3cc8582009-05-04 08:37:12 +0000611 NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
612 __u32 flags;
613
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600614 memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
Steve French0b3cc8582009-05-04 08:37:12 +0000615 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
616 sec_blob->MessageType = NtLmNegotiate;
617
618 /* BB is NTLMV2 session security format easier to use here? */
619 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
620 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800621 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
622 NTLMSSP_NEGOTIATE_SEAL;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200623 if (server->sign)
Steve French745e5072010-09-08 21:09:27 +0000624 flags |= NTLMSSP_NEGOTIATE_SIGN;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200625 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800626 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
Steve French0b3cc8582009-05-04 08:37:12 +0000627
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600628 sec_blob->NegotiateFlags = cpu_to_le32(flags);
Steve French0b3cc8582009-05-04 08:37:12 +0000629
630 sec_blob->WorkstationName.BufferOffset = 0;
631 sec_blob->WorkstationName.Length = 0;
632 sec_blob->WorkstationName.MaximumLength = 0;
633
634 /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
635 sec_blob->DomainName.BufferOffset = 0;
636 sec_blob->DomainName.Length = 0;
637 sec_blob->DomainName.MaximumLength = 0;
638}
639
Jerome Marchandb8da3442016-05-26 11:52:25 +0200640static int size_of_ntlmssp_blob(struct cifs_ses *ses)
641{
642 int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
643 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
644
645 if (ses->domainName)
646 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
647 else
648 sz += 2;
649
650 if (ses->user_name)
651 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
652 else
653 sz += 2;
654
655 return sz;
656}
657
658int build_ntlmssp_auth_blob(unsigned char **pbuffer,
Shirish Pargaonkar89f150f2010-10-19 11:47:52 -0500659 u16 *buflen,
Steve French96daf2b2011-05-27 04:34:02 +0000660 struct cifs_ses *ses,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500661 const struct nls_table *nls_cp)
Steve French0b3cc8582009-05-04 08:37:12 +0000662{
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500663 int rc;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200664 AUTHENTICATE_MESSAGE *sec_blob;
Steve French0b3cc8582009-05-04 08:37:12 +0000665 __u32 flags;
666 unsigned char *tmp;
Steve French0b3cc8582009-05-04 08:37:12 +0000667
Jerome Marchandb8da3442016-05-26 11:52:25 +0200668 rc = setup_ntlmv2_rsp(ses, nls_cp);
669 if (rc) {
670 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
671 *buflen = 0;
672 goto setup_ntlmv2_ret;
673 }
674 *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
Nicholas Mc Guire126c97f2018-08-23 12:24:02 +0200675 if (!*pbuffer) {
676 rc = -ENOMEM;
677 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
678 *buflen = 0;
679 goto setup_ntlmv2_ret;
680 }
Jerome Marchandb8da3442016-05-26 11:52:25 +0200681 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
682
Steve French0b3cc8582009-05-04 08:37:12 +0000683 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
684 sec_blob->MessageType = NtLmAuthenticate;
685
686 flags = NTLMSSP_NEGOTIATE_56 |
687 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
688 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800689 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
690 NTLMSSP_NEGOTIATE_SEAL;
691 if (ses->server->sign)
Steve French0b3cc8582009-05-04 08:37:12 +0000692 flags |= NTLMSSP_NEGOTIATE_SIGN;
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800693 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
694 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
Steve French0b3cc8582009-05-04 08:37:12 +0000695
Jerome Marchandb8da3442016-05-26 11:52:25 +0200696 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600697 sec_blob->NegotiateFlags = cpu_to_le32(flags);
Steve French0b3cc8582009-05-04 08:37:12 +0000698
699 sec_blob->LmChallengeResponse.BufferOffset =
700 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
701 sec_blob->LmChallengeResponse.Length = 0;
702 sec_blob->LmChallengeResponse.MaximumLength = 0;
703
Jerome Marchandb8da3442016-05-26 11:52:25 +0200704 sec_blob->NtChallengeResponse.BufferOffset =
705 cpu_to_le32(tmp - *pbuffer);
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200706 if (ses->user_name != NULL) {
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200707 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
708 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
709 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000710
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200711 sec_blob->NtChallengeResponse.Length =
712 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
713 sec_blob->NtChallengeResponse.MaximumLength =
714 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
715 } else {
716 /*
717 * don't send an NT Response for anonymous access
718 */
719 sec_blob->NtChallengeResponse.Length = 0;
720 sec_blob->NtChallengeResponse.MaximumLength = 0;
721 }
Steve French0b3cc8582009-05-04 08:37:12 +0000722
723 if (ses->domainName == NULL) {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200724 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000725 sec_blob->DomainName.Length = 0;
726 sec_blob->DomainName.MaximumLength = 0;
727 tmp += 2;
728 } else {
729 int len;
Steve Frenchacbbb762012-01-18 22:32:33 -0600730 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
Jerome Marchand202d7722016-05-26 11:52:24 +0200731 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000732 len *= 2; /* unicode is 2 bytes each */
Jerome Marchandb8da3442016-05-26 11:52:25 +0200733 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000734 sec_blob->DomainName.Length = cpu_to_le16(len);
735 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
736 tmp += len;
737 }
738
Steve French8727c8a2011-02-25 01:11:56 -0600739 if (ses->user_name == NULL) {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200740 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000741 sec_blob->UserName.Length = 0;
742 sec_blob->UserName.MaximumLength = 0;
743 tmp += 2;
744 } else {
745 int len;
Steve Frenchacbbb762012-01-18 22:32:33 -0600746 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -0400747 CIFS_MAX_USERNAME_LEN, nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000748 len *= 2; /* unicode is 2 bytes each */
Jerome Marchandb8da3442016-05-26 11:52:25 +0200749 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000750 sec_blob->UserName.Length = cpu_to_le16(len);
751 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
752 tmp += len;
753 }
754
Jerome Marchandb8da3442016-05-26 11:52:25 +0200755 sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Steve French0b3cc8582009-05-04 08:37:12 +0000756 sec_blob->WorkstationName.Length = 0;
757 sec_blob->WorkstationName.MaximumLength = 0;
758 tmp += 2;
759
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600760 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
761 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
762 && !calc_seckey(ses)) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500763 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Jerome Marchandb8da3442016-05-26 11:52:25 +0200764 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500765 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
766 sec_blob->SessionKey.MaximumLength =
767 cpu_to_le16(CIFS_CPHTXT_SIZE);
768 tmp += CIFS_CPHTXT_SIZE;
769 } else {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200770 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500771 sec_blob->SessionKey.Length = 0;
772 sec_blob->SessionKey.MaximumLength = 0;
773 }
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500774
Jerome Marchandb8da3442016-05-26 11:52:25 +0200775 *buflen = tmp - *pbuffer;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500776setup_ntlmv2_ret:
Shirish Pargaonkar89f150f2010-10-19 11:47:52 -0500777 return rc;
Steve French0b3cc8582009-05-04 08:37:12 +0000778}
Steve French0b3cc8582009-05-04 08:37:12 +0000779
Jeff Layton3f618222013-06-12 19:52:14 -0500780enum securityEnum
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530781cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
Jeff Layton3f618222013-06-12 19:52:14 -0500782{
783 switch (server->negflavor) {
784 case CIFS_NEGFLAVOR_EXTENDED:
785 switch (requested) {
786 case Kerberos:
787 case RawNTLMSSP:
788 return requested;
789 case Unspecified:
790 if (server->sec_ntlmssp &&
791 (global_secflags & CIFSSEC_MAY_NTLMSSP))
792 return RawNTLMSSP;
793 if ((server->sec_kerberos || server->sec_mskerberos) &&
794 (global_secflags & CIFSSEC_MAY_KRB5))
795 return Kerberos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500796 fallthrough;
Jeff Layton3f618222013-06-12 19:52:14 -0500797 default:
798 return Unspecified;
799 }
800 case CIFS_NEGFLAVOR_UNENCAP:
801 switch (requested) {
802 case NTLM:
803 case NTLMv2:
804 return requested;
805 case Unspecified:
806 if (global_secflags & CIFSSEC_MAY_NTLMV2)
807 return NTLMv2;
808 if (global_secflags & CIFSSEC_MAY_NTLM)
809 return NTLM;
Gustavo A. R. Silva21ac58f2020-11-20 12:24:14 -0600810 break;
Jeff Layton3f618222013-06-12 19:52:14 -0500811 default:
Sachin Prabhudde23562013-09-27 18:35:42 +0100812 break;
Jeff Layton3f618222013-06-12 19:52:14 -0500813 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500814 fallthrough; /* to attempt LANMAN authentication next */
Jeff Layton3f618222013-06-12 19:52:14 -0500815 case CIFS_NEGFLAVOR_LANMAN:
816 switch (requested) {
817 case LANMAN:
818 return requested;
819 case Unspecified:
820 if (global_secflags & CIFSSEC_MAY_LANMAN)
821 return LANMAN;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500822 fallthrough;
Jeff Layton3f618222013-06-12 19:52:14 -0500823 default:
824 return Unspecified;
825 }
826 default:
827 return Unspecified;
828 }
829}
830
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100831struct sess_data {
832 unsigned int xid;
833 struct cifs_ses *ses;
834 struct nls_table *nls_cp;
835 void (*func)(struct sess_data *);
836 int result;
837
838 /* we will send the SMB in three pieces:
839 * a fixed length beginning part, an optional
840 * SPNEGO blob (which can be zero length), and a
841 * last part which will include the strings
842 * and rest of bcc area. This allows us to avoid
843 * a large buffer 17K allocation
844 */
845 int buf0_type;
846 struct kvec iov[3];
847};
848
849static int
850sess_alloc_buffer(struct sess_data *sess_data, int wct)
851{
852 int rc;
853 struct cifs_ses *ses = sess_data->ses;
854 struct smb_hdr *smb_buf;
855
856 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
857 (void **)&smb_buf);
858
859 if (rc)
860 return rc;
861
862 sess_data->iov[0].iov_base = (char *)smb_buf;
863 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
864 /*
865 * This variable will be used to clear the buffer
866 * allocated above in case of any error in the calling function.
867 */
868 sess_data->buf0_type = CIFS_SMALL_BUFFER;
869
870 /* 2000 big enough to fit max user, domain, NOS name etc. */
871 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
872 if (!sess_data->iov[2].iov_base) {
873 rc = -ENOMEM;
874 goto out_free_smb_buf;
875 }
876
877 return 0;
878
879out_free_smb_buf:
880 kfree(smb_buf);
881 sess_data->iov[0].iov_base = NULL;
882 sess_data->iov[0].iov_len = 0;
883 sess_data->buf0_type = CIFS_NO_BUFFER;
884 return rc;
885}
886
887static void
888sess_free_buffer(struct sess_data *sess_data)
889{
890
891 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
892 sess_data->buf0_type = CIFS_NO_BUFFER;
893 kfree(sess_data->iov[2].iov_base);
894}
895
896static int
897sess_establish_session(struct sess_data *sess_data)
898{
899 struct cifs_ses *ses = sess_data->ses;
900
901 mutex_lock(&ses->server->srv_mutex);
902 if (!ses->server->session_estab) {
903 if (ses->server->sign) {
904 ses->server->session_key.response =
905 kmemdup(ses->auth_key.response,
906 ses->auth_key.len, GFP_KERNEL);
907 if (!ses->server->session_key.response) {
908 mutex_unlock(&ses->server->srv_mutex);
909 return -ENOMEM;
910 }
911 ses->server->session_key.len =
912 ses->auth_key.len;
913 }
914 ses->server->sequence_number = 0x2;
915 ses->server->session_estab = true;
916 }
917 mutex_unlock(&ses->server->srv_mutex);
918
919 cifs_dbg(FYI, "CIFS session established successfully\n");
920 spin_lock(&GlobalMid_Lock);
921 ses->status = CifsGood;
922 ses->need_reconnect = false;
923 spin_unlock(&GlobalMid_Lock);
924
925 return 0;
926}
927
928static int
929sess_sendreceive(struct sess_data *sess_data)
930{
931 int rc;
932 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
933 __u16 count;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700934 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100935
936 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
Qinglang Miao1a0e7f72020-07-25 16:56:01 +0800937 be32_add_cpu(&smb_buf->smb_buf_length, count);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100938 put_bcc(count, smb_buf);
939
940 rc = SendReceive2(sess_data->xid, sess_data->ses,
941 sess_data->iov, 3 /* num_iovecs */,
942 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700943 CIFS_LOG_ERROR, &rsp_iov);
944 cifs_small_buf_release(sess_data->iov[0].iov_base);
945 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100946
947 return rc;
948}
949
950/*
951 * LANMAN and plaintext are less secure and off by default.
952 * So we make this explicitly be turned on in kconfig (in the
953 * build) and turned on at runtime (changed from the default)
954 * in proc/fs/cifs or via mount parm. Unfortunately this is
955 * needed for old Win (e.g. Win95), some obscure NAS and OS/2
956 */
957#ifdef CONFIG_CIFS_WEAK_PW_HASH
958static void
959sess_auth_lanman(struct sess_data *sess_data)
960{
961 int rc = 0;
962 struct smb_hdr *smb_buf;
963 SESSION_SETUP_ANDX *pSMB;
964 char *bcc_ptr;
965 struct cifs_ses *ses = sess_data->ses;
966 char lnm_session_key[CIFS_AUTH_RESP_SIZE];
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100967 __u16 bytes_remaining;
968
969 /* lanman 2 style sessionsetup */
970 /* wct = 10 */
971 rc = sess_alloc_buffer(sess_data, 10);
972 if (rc)
973 goto out;
974
975 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
976 bcc_ptr = sess_data->iov[2].iov_base;
zhengbin8559ad82019-09-24 10:13:47 +0800977 (void)cifs_ssetup_hdr(ses, pSMB);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100978
979 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
980
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200981 if (ses->user_name != NULL) {
982 /* no capabilities flags in old lanman negotiation */
983 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100984
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200985 /* Calculate hash with password and copy into bcc_ptr.
986 * Encryption Key (stored as in cryptkey) gets used if the
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500987 * security mode bit in Negotiate Protocol response states
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200988 * to use challenge/response method (i.e. Password bit is 1).
989 */
990 rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
991 ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
992 true : false, lnm_session_key);
Luis de Bethencourta6b6bef2016-06-08 17:02:32 +0100993 if (rc)
994 goto out;
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100995
Stefan Metzmacherfa8f3a32016-05-03 10:52:30 +0200996 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
997 bcc_ptr += CIFS_AUTH_RESP_SIZE;
998 } else {
999 pSMB->old_req.PasswordLength = 0;
1000 }
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001001
1002 /*
1003 * can not sign if LANMAN negotiated so no need
1004 * to calculate signing key? but what if server
1005 * changed to do higher than lanman dialect and
1006 * we reconnected would we ever calc signing_key?
1007 */
1008
1009 cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
1010 /* Unicode not allowed for LANMAN dialects */
1011 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1012
1013 sess_data->iov[2].iov_len = (long) bcc_ptr -
1014 (long) sess_data->iov[2].iov_base;
1015
1016 rc = sess_sendreceive(sess_data);
1017 if (rc)
1018 goto out;
1019
1020 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1021 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1022
1023 /* lanman response has a word count of 3 */
1024 if (smb_buf->WordCount != 3) {
1025 rc = -EIO;
1026 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1027 goto out;
1028 }
1029
1030 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1031 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1032
1033 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1034 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1035
1036 bytes_remaining = get_bcc(smb_buf);
1037 bcc_ptr = pByteArea(smb_buf);
1038
1039 /* BB check if Unicode and decode strings */
1040 if (bytes_remaining == 0) {
1041 /* no string area to decode, do nothing */
1042 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1043 /* unicode string area must be word-aligned */
1044 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1045 ++bcc_ptr;
1046 --bytes_remaining;
1047 }
1048 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1049 sess_data->nls_cp);
1050 } else {
1051 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1052 sess_data->nls_cp);
1053 }
1054
1055 rc = sess_establish_session(sess_data);
1056out:
1057 sess_data->result = rc;
1058 sess_data->func = NULL;
1059 sess_free_buffer(sess_data);
1060}
1061
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001062#endif
1063
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001064static void
1065sess_auth_ntlm(struct sess_data *sess_data)
1066{
1067 int rc = 0;
1068 struct smb_hdr *smb_buf;
1069 SESSION_SETUP_ANDX *pSMB;
1070 char *bcc_ptr;
1071 struct cifs_ses *ses = sess_data->ses;
1072 __u32 capabilities;
1073 __u16 bytes_remaining;
1074
1075 /* old style NTLM sessionsetup */
1076 /* wct = 13 */
1077 rc = sess_alloc_buffer(sess_data, 13);
1078 if (rc)
1079 goto out;
1080
1081 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1082 bcc_ptr = sess_data->iov[2].iov_base;
1083 capabilities = cifs_ssetup_hdr(ses, pSMB);
1084
1085 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
Stefan Metzmacher777f69b2016-05-03 10:52:30 +02001086 if (ses->user_name != NULL) {
1087 pSMB->req_no_secext.CaseInsensitivePasswordLength =
1088 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1089 pSMB->req_no_secext.CaseSensitivePasswordLength =
1090 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001091
Stefan Metzmacher777f69b2016-05-03 10:52:30 +02001092 /* calculate ntlm response and session key */
1093 rc = setup_ntlm_response(ses, sess_data->nls_cp);
1094 if (rc) {
1095 cifs_dbg(VFS, "Error %d during NTLM authentication\n",
1096 rc);
1097 goto out;
1098 }
1099
1100 /* copy ntlm response */
1101 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1102 CIFS_AUTH_RESP_SIZE);
1103 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1104 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1105 CIFS_AUTH_RESP_SIZE);
1106 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1107 } else {
1108 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1109 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001110 }
1111
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001112 if (ses->capabilities & CAP_UNICODE) {
1113 /* unicode strings must be word aligned */
1114 if (sess_data->iov[0].iov_len % 2) {
1115 *bcc_ptr = 0;
1116 bcc_ptr++;
1117 }
1118 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1119 } else {
1120 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1121 }
1122
1123
1124 sess_data->iov[2].iov_len = (long) bcc_ptr -
1125 (long) sess_data->iov[2].iov_base;
1126
1127 rc = sess_sendreceive(sess_data);
1128 if (rc)
1129 goto out;
1130
1131 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1132 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1133
1134 if (smb_buf->WordCount != 3) {
1135 rc = -EIO;
1136 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1137 goto out;
1138 }
1139
1140 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1141 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1142
1143 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1144 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1145
1146 bytes_remaining = get_bcc(smb_buf);
1147 bcc_ptr = pByteArea(smb_buf);
1148
1149 /* BB check if Unicode and decode strings */
1150 if (bytes_remaining == 0) {
1151 /* no string area to decode, do nothing */
1152 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1153 /* unicode string area must be word-aligned */
1154 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1155 ++bcc_ptr;
1156 --bytes_remaining;
1157 }
1158 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1159 sess_data->nls_cp);
1160 } else {
1161 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1162 sess_data->nls_cp);
1163 }
1164
1165 rc = sess_establish_session(sess_data);
1166out:
1167 sess_data->result = rc;
1168 sess_data->func = NULL;
1169 sess_free_buffer(sess_data);
1170 kfree(ses->auth_key.response);
1171 ses->auth_key.response = NULL;
1172}
1173
1174static void
1175sess_auth_ntlmv2(struct sess_data *sess_data)
1176{
1177 int rc = 0;
1178 struct smb_hdr *smb_buf;
1179 SESSION_SETUP_ANDX *pSMB;
1180 char *bcc_ptr;
1181 struct cifs_ses *ses = sess_data->ses;
1182 __u32 capabilities;
1183 __u16 bytes_remaining;
1184
1185 /* old style NTLM sessionsetup */
1186 /* wct = 13 */
1187 rc = sess_alloc_buffer(sess_data, 13);
1188 if (rc)
1189 goto out;
1190
1191 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1192 bcc_ptr = sess_data->iov[2].iov_base;
1193 capabilities = cifs_ssetup_hdr(ses, pSMB);
1194
1195 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1196
1197 /* LM2 password would be here if we supported it */
1198 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1199
Stefan Metzmacher1a967d62016-05-03 10:52:30 +02001200 if (ses->user_name != NULL) {
1201 /* calculate nlmv2 response and session key */
1202 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1203 if (rc) {
1204 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1205 goto out;
1206 }
1207
1208 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1209 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1210 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1211
1212 /* set case sensitive password length after tilen may get
1213 * assigned, tilen is 0 otherwise.
1214 */
1215 pSMB->req_no_secext.CaseSensitivePasswordLength =
1216 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1217 } else {
1218 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001219 }
1220
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001221 if (ses->capabilities & CAP_UNICODE) {
1222 if (sess_data->iov[0].iov_len % 2) {
1223 *bcc_ptr = 0;
1224 bcc_ptr++;
1225 }
1226 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1227 } else {
1228 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1229 }
1230
1231
1232 sess_data->iov[2].iov_len = (long) bcc_ptr -
1233 (long) sess_data->iov[2].iov_base;
1234
1235 rc = sess_sendreceive(sess_data);
1236 if (rc)
1237 goto out;
1238
1239 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1240 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1241
1242 if (smb_buf->WordCount != 3) {
1243 rc = -EIO;
1244 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1245 goto out;
1246 }
1247
1248 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1249 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1250
1251 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1252 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1253
1254 bytes_remaining = get_bcc(smb_buf);
1255 bcc_ptr = pByteArea(smb_buf);
1256
1257 /* BB check if Unicode and decode strings */
1258 if (bytes_remaining == 0) {
1259 /* no string area to decode, do nothing */
1260 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1261 /* unicode string area must be word-aligned */
1262 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1263 ++bcc_ptr;
1264 --bytes_remaining;
1265 }
1266 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1267 sess_data->nls_cp);
1268 } else {
1269 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1270 sess_data->nls_cp);
1271 }
1272
1273 rc = sess_establish_session(sess_data);
1274out:
1275 sess_data->result = rc;
1276 sess_data->func = NULL;
1277 sess_free_buffer(sess_data);
1278 kfree(ses->auth_key.response);
1279 ses->auth_key.response = NULL;
1280}
1281
Sachin Prabhuee03c642014-06-16 15:35:27 +01001282#ifdef CONFIG_CIFS_UPCALL
1283static void
1284sess_auth_kerberos(struct sess_data *sess_data)
1285{
1286 int rc = 0;
1287 struct smb_hdr *smb_buf;
1288 SESSION_SETUP_ANDX *pSMB;
1289 char *bcc_ptr;
1290 struct cifs_ses *ses = sess_data->ses;
1291 __u32 capabilities;
1292 __u16 bytes_remaining;
1293 struct key *spnego_key = NULL;
1294 struct cifs_spnego_msg *msg;
1295 u16 blob_len;
1296
1297 /* extended security */
1298 /* wct = 12 */
1299 rc = sess_alloc_buffer(sess_data, 12);
1300 if (rc)
1301 goto out;
1302
1303 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1304 bcc_ptr = sess_data->iov[2].iov_base;
1305 capabilities = cifs_ssetup_hdr(ses, pSMB);
1306
1307 spnego_key = cifs_get_spnego_key(ses);
1308 if (IS_ERR(spnego_key)) {
1309 rc = PTR_ERR(spnego_key);
1310 spnego_key = NULL;
1311 goto out;
1312 }
1313
David Howells146aa8b2015-10-21 14:04:48 +01001314 msg = spnego_key->payload.data[0];
Sachin Prabhuee03c642014-06-16 15:35:27 +01001315 /*
1316 * check version field to make sure that cifs.upcall is
1317 * sending us a response in an expected form
1318 */
1319 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001320 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1321 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhuee03c642014-06-16 15:35:27 +01001322 rc = -EKEYREJECTED;
1323 goto out_put_spnego_key;
1324 }
1325
1326 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1327 GFP_KERNEL);
1328 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001329 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1330 msg->sesskey_len);
Sachin Prabhuee03c642014-06-16 15:35:27 +01001331 rc = -ENOMEM;
1332 goto out_put_spnego_key;
1333 }
1334 ses->auth_key.len = msg->sesskey_len;
1335
1336 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1337 capabilities |= CAP_EXTENDED_SECURITY;
1338 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1339 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1340 sess_data->iov[1].iov_len = msg->secblob_len;
1341 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1342
1343 if (ses->capabilities & CAP_UNICODE) {
1344 /* unicode strings must be word aligned */
1345 if ((sess_data->iov[0].iov_len
1346 + sess_data->iov[1].iov_len) % 2) {
1347 *bcc_ptr = 0;
1348 bcc_ptr++;
1349 }
1350 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1351 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1352 } else {
1353 /* BB: is this right? */
1354 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1355 }
1356
1357 sess_data->iov[2].iov_len = (long) bcc_ptr -
1358 (long) sess_data->iov[2].iov_base;
1359
1360 rc = sess_sendreceive(sess_data);
1361 if (rc)
1362 goto out_put_spnego_key;
1363
1364 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1365 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1366
1367 if (smb_buf->WordCount != 4) {
1368 rc = -EIO;
1369 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1370 goto out_put_spnego_key;
1371 }
1372
1373 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1374 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1375
1376 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1377 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1378
1379 bytes_remaining = get_bcc(smb_buf);
1380 bcc_ptr = pByteArea(smb_buf);
1381
1382 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1383 if (blob_len > bytes_remaining) {
1384 cifs_dbg(VFS, "bad security blob length %d\n",
1385 blob_len);
1386 rc = -EINVAL;
1387 goto out_put_spnego_key;
1388 }
1389 bcc_ptr += blob_len;
1390 bytes_remaining -= blob_len;
1391
1392 /* BB check if Unicode and decode strings */
1393 if (bytes_remaining == 0) {
1394 /* no string area to decode, do nothing */
1395 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1396 /* unicode string area must be word-aligned */
1397 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1398 ++bcc_ptr;
1399 --bytes_remaining;
1400 }
1401 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1402 sess_data->nls_cp);
1403 } else {
1404 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1405 sess_data->nls_cp);
1406 }
1407
1408 rc = sess_establish_session(sess_data);
1409out_put_spnego_key:
1410 key_invalidate(spnego_key);
1411 key_put(spnego_key);
1412out:
1413 sess_data->result = rc;
1414 sess_data->func = NULL;
1415 sess_free_buffer(sess_data);
1416 kfree(ses->auth_key.response);
1417 ses->auth_key.response = NULL;
1418}
1419
Sachin Prabhuee03c642014-06-16 15:35:27 +01001420#endif /* ! CONFIG_CIFS_UPCALL */
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001421
Sachin Prabhucc87c472014-06-16 15:35:28 +01001422/*
1423 * The required kvec buffers have to be allocated before calling this
1424 * function.
1425 */
1426static int
1427_sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
Steve French39798772006-05-31 22:40:51 +00001428{
Steve French39798772006-05-31 22:40:51 +00001429 SESSION_SETUP_ANDX *pSMB;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001430 struct cifs_ses *ses = sess_data->ses;
Steve French39798772006-05-31 22:40:51 +00001431 __u32 capabilities;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001432 char *bcc_ptr;
Steve French254e55e2006-06-04 05:53:15 +00001433
Sachin Prabhucc87c472014-06-16 15:35:28 +01001434 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001435
1436 capabilities = cifs_ssetup_hdr(ses, pSMB);
1437 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1438 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1439 return -ENOSYS;
Jeff Layton3534b852013-05-24 07:41:01 -04001440 }
Steve French39798772006-05-31 22:40:51 +00001441
Sachin Prabhucc87c472014-06-16 15:35:28 +01001442 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1443 capabilities |= CAP_EXTENDED_SECURITY;
1444 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1445
1446 bcc_ptr = sess_data->iov[2].iov_base;
1447 /* unicode strings must be word aligned */
1448 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1449 *bcc_ptr = 0;
1450 bcc_ptr++;
1451 }
1452 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1453
1454 sess_data->iov[2].iov_len = (long) bcc_ptr -
1455 (long) sess_data->iov[2].iov_base;
1456
1457 return 0;
1458}
1459
1460static void
1461sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1462
1463static void
1464sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1465{
1466 int rc;
1467 struct smb_hdr *smb_buf;
1468 SESSION_SETUP_ANDX *pSMB;
1469 struct cifs_ses *ses = sess_data->ses;
1470 __u16 bytes_remaining;
1471 char *bcc_ptr;
1472 u16 blob_len;
1473
1474 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1475
1476 /*
1477 * if memory allocation is successful, caller of this function
1478 * frees it.
1479 */
1480 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1481 if (!ses->ntlmssp) {
1482 rc = -ENOMEM;
1483 goto out;
1484 }
1485 ses->ntlmssp->sesskey_per_smbsess = false;
1486
1487 /* wct = 12 */
1488 rc = sess_alloc_buffer(sess_data, 12);
1489 if (rc)
1490 goto out;
1491
1492 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1493
1494 /* Build security blob before we assemble the request */
1495 build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1496 sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1497 sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1498 pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1499
1500 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1501 if (rc)
1502 goto out;
1503
1504 rc = sess_sendreceive(sess_data);
1505
1506 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1507 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1508
1509 /* If true, rc here is expected and not an error */
1510 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1511 smb_buf->Status.CifsError ==
1512 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1513 rc = 0;
1514
1515 if (rc)
1516 goto out;
1517
1518 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1519
1520 if (smb_buf->WordCount != 4) {
1521 rc = -EIO;
1522 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1523 goto out;
1524 }
1525
1526 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1527 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1528
1529 bytes_remaining = get_bcc(smb_buf);
1530 bcc_ptr = pByteArea(smb_buf);
1531
1532 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1533 if (blob_len > bytes_remaining) {
1534 cifs_dbg(VFS, "bad security blob length %d\n",
1535 blob_len);
1536 rc = -EINVAL;
1537 goto out;
1538 }
1539
1540 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1541out:
1542 sess_free_buffer(sess_data);
1543
1544 if (!rc) {
1545 sess_data->func = sess_auth_rawntlmssp_authenticate;
1546 return;
1547 }
1548
1549 /* Else error. Cleanup */
1550 kfree(ses->auth_key.response);
1551 ses->auth_key.response = NULL;
1552 kfree(ses->ntlmssp);
1553 ses->ntlmssp = NULL;
1554
1555 sess_data->func = NULL;
1556 sess_data->result = rc;
1557}
1558
1559static void
1560sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1561{
1562 int rc;
1563 struct smb_hdr *smb_buf;
1564 SESSION_SETUP_ANDX *pSMB;
1565 struct cifs_ses *ses = sess_data->ses;
1566 __u16 bytes_remaining;
1567 char *bcc_ptr;
Jerome Marchandb8da3442016-05-26 11:52:25 +02001568 unsigned char *ntlmsspblob = NULL;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001569 u16 blob_len;
1570
1571 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1572
1573 /* wct = 12 */
1574 rc = sess_alloc_buffer(sess_data, 12);
1575 if (rc)
1576 goto out;
1577
1578 /* Build security blob before we assemble the request */
1579 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1580 smb_buf = (struct smb_hdr *)pSMB;
Jerome Marchandb8da3442016-05-26 11:52:25 +02001581 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
Sachin Prabhucc87c472014-06-16 15:35:28 +01001582 &blob_len, ses, sess_data->nls_cp);
1583 if (rc)
1584 goto out_free_ntlmsspblob;
1585 sess_data->iov[1].iov_len = blob_len;
1586 sess_data->iov[1].iov_base = ntlmsspblob;
1587 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1588 /*
1589 * Make sure that we tell the server that we are using
1590 * the uid that it just gave us back on the response
1591 * (challenge)
1592 */
1593 smb_buf->Uid = ses->Suid;
1594
1595 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1596 if (rc)
1597 goto out_free_ntlmsspblob;
1598
1599 rc = sess_sendreceive(sess_data);
1600 if (rc)
1601 goto out_free_ntlmsspblob;
1602
1603 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1604 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1605 if (smb_buf->WordCount != 4) {
1606 rc = -EIO;
1607 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1608 goto out_free_ntlmsspblob;
1609 }
1610
1611 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1612 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1613
Sachin Prabhuee9bbf42014-12-03 12:26:36 +00001614 if (ses->Suid != smb_buf->Uid) {
1615 ses->Suid = smb_buf->Uid;
1616 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1617 }
1618
Sachin Prabhucc87c472014-06-16 15:35:28 +01001619 bytes_remaining = get_bcc(smb_buf);
1620 bcc_ptr = pByteArea(smb_buf);
1621 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1622 if (blob_len > bytes_remaining) {
1623 cifs_dbg(VFS, "bad security blob length %d\n",
1624 blob_len);
1625 rc = -EINVAL;
1626 goto out_free_ntlmsspblob;
1627 }
1628 bcc_ptr += blob_len;
1629 bytes_remaining -= blob_len;
1630
1631
1632 /* BB check if Unicode and decode strings */
1633 if (bytes_remaining == 0) {
1634 /* no string area to decode, do nothing */
1635 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1636 /* unicode string area must be word-aligned */
1637 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1638 ++bcc_ptr;
1639 --bytes_remaining;
1640 }
1641 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1642 sess_data->nls_cp);
1643 } else {
1644 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1645 sess_data->nls_cp);
1646 }
1647
1648out_free_ntlmsspblob:
1649 kfree(ntlmsspblob);
1650out:
1651 sess_free_buffer(sess_data);
1652
1653 if (!rc)
1654 rc = sess_establish_session(sess_data);
1655
1656 /* Cleanup */
1657 kfree(ses->auth_key.response);
1658 ses->auth_key.response = NULL;
1659 kfree(ses->ntlmssp);
1660 ses->ntlmssp = NULL;
1661
1662 sess_data->func = NULL;
1663 sess_data->result = rc;
1664}
1665
Steve French27924072014-07-11 07:47:01 -05001666static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
Sachin Prabhucc87c472014-06-16 15:35:28 +01001667{
1668 int type;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001669
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301670 type = cifs_select_sectype(ses->server, ses->sectype);
Joe Perchesf96637b2013-05-04 22:12:25 -05001671 cifs_dbg(FYI, "sess setup type %d\n", type);
Jeff Layton3f618222013-06-12 19:52:14 -05001672 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001673 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Jeff Layton3f618222013-06-12 19:52:14 -05001674 return -EINVAL;
1675 }
1676
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001677 switch (type) {
1678 case LANMAN:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001679 /* LANMAN and plaintext are less secure and off by default.
1680 * So we make this explicitly be turned on in kconfig (in the
1681 * build) and turned on at runtime (changed from the default)
1682 * in proc/fs/cifs or via mount parm. Unfortunately this is
1683 * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
1684#ifdef CONFIG_CIFS_WEAK_PW_HASH
1685 sess_data->func = sess_auth_lanman;
1686 break;
1687#else
1688 return -EOPNOTSUPP;
1689#endif
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001690 case NTLM:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001691 sess_data->func = sess_auth_ntlm;
1692 break;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001693 case NTLMv2:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001694 sess_data->func = sess_auth_ntlmv2;
1695 break;
Sachin Prabhuee03c642014-06-16 15:35:27 +01001696 case Kerberos:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001697#ifdef CONFIG_CIFS_UPCALL
1698 sess_data->func = sess_auth_kerberos;
1699 break;
1700#else
1701 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1702 return -ENOSYS;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001703#endif /* CONFIG_CIFS_UPCALL */
1704 case RawNTLMSSP:
1705 sess_data->func = sess_auth_rawntlmssp_negotiate;
1706 break;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001707 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001708 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhucc87c472014-06-16 15:35:28 +01001709 return -ENOSYS;
Steve French39798772006-05-31 22:40:51 +00001710 }
1711
Sachin Prabhucc87c472014-06-16 15:35:28 +01001712 return 0;
1713}
Steve French24424212007-11-16 23:37:35 +00001714
Sachin Prabhucc87c472014-06-16 15:35:28 +01001715int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1716 const struct nls_table *nls_cp)
1717{
1718 int rc = 0;
1719 struct sess_data *sess_data;
Steve French39798772006-05-31 22:40:51 +00001720
Sachin Prabhucc87c472014-06-16 15:35:28 +01001721 if (ses == NULL) {
1722 WARN(1, "%s: ses == NULL!", __func__);
1723 return -EINVAL;
Steve French0b3cc8582009-05-04 08:37:12 +00001724 }
Sachin Prabhucc87c472014-06-16 15:35:28 +01001725
1726 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1727 if (!sess_data)
1728 return -ENOMEM;
1729
1730 rc = select_sec(ses, sess_data);
Steve French0b3cc8582009-05-04 08:37:12 +00001731 if (rc)
Sachin Prabhucc87c472014-06-16 15:35:28 +01001732 goto out;
Steve French0b3cc8582009-05-04 08:37:12 +00001733
Sachin Prabhucc87c472014-06-16 15:35:28 +01001734 sess_data->xid = xid;
1735 sess_data->ses = ses;
1736 sess_data->buf0_type = CIFS_NO_BUFFER;
1737 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve French39798772006-05-31 22:40:51 +00001738
Sachin Prabhucc87c472014-06-16 15:35:28 +01001739 while (sess_data->func)
1740 sess_data->func(sess_data);
Steve French39798772006-05-31 22:40:51 +00001741
Sachin Prabhucc87c472014-06-16 15:35:28 +01001742 /* Store result before we free sess_data */
1743 rc = sess_data->result;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001744
1745out:
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001746 kfree(sess_data);
1747 return rc;
Steve French39798772006-05-31 22:40:51 +00001748}