blob: 27660bffb91884005343b4451d4ea3421a62d29d [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/*
Steve French39798772006-05-31 22:40:51 +00003 *
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 *
Steve French39798772006-05-31 22:40:51 +00009 */
10
11#include "cifspdu.h"
12#include "cifsglob.h"
13#include "cifsproto.h"
14#include "cifs_unicode.h"
15#include "cifs_debug.h"
16#include "ntlmssp.h"
17#include "nterr.h"
Steve French9c535882006-06-01 05:09:10 +000018#include <linux/utsname.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Steve French24424212007-11-16 23:37:35 +000020#include "cifs_spnego.h"
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020021#include "smb2proto.h"
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060022#include "fs_context.h"
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020023
Ronnie Sahlberg387ec582020-12-14 16:40:20 +100024static int
25cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
26 struct cifs_server_iface *iface);
27
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020028bool
29is_server_using_iface(struct TCP_Server_Info *server,
30 struct cifs_server_iface *iface)
31{
32 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
33 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
34 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
35 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
36
37 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
38 return false;
39 if (server->dstaddr.ss_family == AF_INET) {
40 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
41 return false;
42 } else if (server->dstaddr.ss_family == AF_INET6) {
43 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
44 sizeof(i6->sin6_addr)) != 0)
45 return false;
46 } else {
47 /* unknown family.. */
48 return false;
49 }
50 return true;
51}
52
53bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
54{
55 int i;
56
Shyam Prasad N724244c2021-07-19 10:54:46 +000057 spin_lock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020058 for (i = 0; i < ses->chan_count; i++) {
Shyam Prasad N724244c2021-07-19 10:54:46 +000059 if (is_server_using_iface(ses->chans[i].server, iface)) {
60 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020061 return true;
Shyam Prasad N724244c2021-07-19 10:54:46 +000062 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020063 }
Shyam Prasad N724244c2021-07-19 10:54:46 +000064 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020065 return false;
66}
67
68/* returns number of channels added */
Ronnie Sahlberg387ec582020-12-14 16:40:20 +100069int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020070{
Shyam Prasad N724244c2021-07-19 10:54:46 +000071 int old_chan_count, new_chan_count;
72 int left;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020073 int i = 0;
74 int rc = 0;
Aurelien Aptel65a37a32019-11-20 17:15:59 +010075 int tries = 0;
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +010076 struct cifs_server_iface *ifaces = NULL;
77 size_t iface_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020078
Shyam Prasad N724244c2021-07-19 10:54:46 +000079 if (ses->server->dialect < SMB30_PROT_ID) {
80 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
81 return 0;
82 }
83
84 spin_lock(&ses->chan_lock);
85
86 new_chan_count = old_chan_count = ses->chan_count;
87 left = ses->chan_max - ses->chan_count;
88
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020089 if (left <= 0) {
90 cifs_dbg(FYI,
91 "ses already at max_channels (%zu), nothing to open\n",
92 ses->chan_max);
Shyam Prasad N724244c2021-07-19 10:54:46 +000093 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020094 return 0;
95 }
96
Steve French9c2dc112021-05-07 20:00:41 -050097 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
98 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
99 ses->chan_max = 1;
Shyam Prasad N724244c2021-07-19 10:54:46 +0000100 spin_unlock(&ses->chan_lock);
Steve French9c2dc112021-05-07 20:00:41 -0500101 return 0;
102 }
Shyam Prasad N724244c2021-07-19 10:54:46 +0000103 spin_unlock(&ses->chan_lock);
Steve French9c2dc112021-05-07 20:00:41 -0500104
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100105 /*
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100106 * Make a copy of the iface list at the time and use that
107 * instead so as to not hold the iface spinlock for opening
108 * channels
109 */
110 spin_lock(&ses->iface_lock);
111 iface_count = ses->iface_count;
112 if (iface_count <= 0) {
113 spin_unlock(&ses->iface_lock);
Aurelien Aptel343a1b72020-02-06 10:19:11 +0100114 cifs_dbg(VFS, "no iface list available to open channels\n");
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100115 return 0;
116 }
117 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
118 GFP_ATOMIC);
119 if (!ifaces) {
120 spin_unlock(&ses->iface_lock);
121 return 0;
122 }
123 spin_unlock(&ses->iface_lock);
124
125 /*
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100126 * Keep connecting to same, fastest, iface for all channels as
127 * long as its RSS. Try next fastest one if not RSS or channel
128 * creation fails.
129 */
130 while (left > 0) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200131 struct cifs_server_iface *iface;
132
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100133 tries++;
134 if (tries > 3*ses->chan_max) {
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500135 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100136 left);
137 break;
138 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200139
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100140 iface = &ifaces[i];
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100141 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100142 i = (i+1) % iface_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200143 continue;
144 }
145
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000146 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100147 if (rc) {
148 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
149 i, rc);
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100150 i = (i+1) % iface_count;
Aurelien Aptel65a37a32019-11-20 17:15:59 +0100151 continue;
152 }
153
154 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
155 i);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200156 left--;
Shyam Prasad N724244c2021-07-19 10:54:46 +0000157 new_chan_count++;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200158 }
159
Aurelien Aptel9a7d5a92019-12-04 16:14:54 +0100160 kfree(ifaces);
Shyam Prasad N724244c2021-07-19 10:54:46 +0000161 return new_chan_count - old_chan_count;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200162}
163
Aurelien Aptel2f589672020-04-24 16:55:31 +0200164/*
165 * If server is a channel of ses, return the corresponding enclosing
166 * cifs_chan otherwise return NULL.
167 */
168struct cifs_chan *
169cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
170{
171 int i;
172
Shyam Prasad N724244c2021-07-19 10:54:46 +0000173 spin_lock(&ses->chan_lock);
Aurelien Aptel2f589672020-04-24 16:55:31 +0200174 for (i = 0; i < ses->chan_count; i++) {
Shyam Prasad N724244c2021-07-19 10:54:46 +0000175 if (ses->chans[i].server == server) {
176 spin_unlock(&ses->chan_lock);
Aurelien Aptel2f589672020-04-24 16:55:31 +0200177 return &ses->chans[i];
Shyam Prasad N724244c2021-07-19 10:54:46 +0000178 }
Aurelien Aptel2f589672020-04-24 16:55:31 +0200179 }
Shyam Prasad N724244c2021-07-19 10:54:46 +0000180 spin_unlock(&ses->chan_lock);
Aurelien Aptel2f589672020-04-24 16:55:31 +0200181 return NULL;
182}
183
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000184static int
185cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
186 struct cifs_server_iface *iface)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200187{
Shyam Prasad N724244c2021-07-19 10:54:46 +0000188 struct TCP_Server_Info *chan_server;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200189 struct cifs_chan *chan;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600190 struct smb3_fs_context ctx = {NULL};
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200191 static const char unc_fmt[] = "\\%s\\foo";
192 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
193 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
194 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
195 int rc;
196 unsigned int xid = get_xid();
197
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200198 if (iface->sockaddr.ss_family == AF_INET)
Joe Perchesa0a30362020-04-14 22:42:53 -0700199 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
200 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
201 &ipv4->sin_addr);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200202 else
Aurelien Aptel5e538952021-05-28 16:32:48 +0200203 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
Joe Perchesa0a30362020-04-14 22:42:53 -0700204 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
205 &ipv6->sin6_addr);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200206
207 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600208 * Setup a ctx with mostly the same info as the existing
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200209 * session and overwrite it with the requested iface data.
210 *
211 * We need to setup at least the fields used for negprot and
212 * sesssetup.
213 *
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600214 * We only need the ctx here, so we can reuse memory from
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200215 * the session and server without caring about memory
216 * management.
217 */
218
219 /* Always make new connection for now (TODO?) */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600220 ctx.nosharesock = true;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200221
222 /* Auth */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600223 ctx.domainauto = ses->domainAuto;
224 ctx.domainname = ses->domainName;
225 ctx.username = ses->user_name;
226 ctx.password = ses->password;
227 ctx.sectype = ses->sectype;
228 ctx.sign = ses->sign;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200229
230 /* UNC and paths */
231 /* XXX: Use ses->server->hostname? */
Steve Frenchb438fcf2021-02-20 19:24:11 -0600232 sprintf(unc, unc_fmt, ses->ip_addr);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600233 ctx.UNC = unc;
234 ctx.prepath = "";
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200235
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500236 /* Reuse same version as master connection */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600237 ctx.vals = ses->server->vals;
238 ctx.ops = ses->server->ops;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200239
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600240 ctx.noblocksnd = ses->server->noblocksnd;
241 ctx.noautotune = ses->server->noautotune;
242 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
243 ctx.echo_interval = ses->server->echo_interval / HZ;
Aurelien Aptela249cc82021-03-04 17:42:21 +0000244 ctx.max_credits = ses->server->max_credits;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200245
246 /*
247 * This will be used for encoding/decoding user/domain/pw
248 * during sess setup auth.
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200249 */
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000250 ctx.local_nls = cifs_sb->local_nls;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200251
252 /* Use RDMA if possible */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600253 ctx.rdma = iface->rdma_capable;
254 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200255
256 /* reuse master con client guid */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600257 memcpy(&ctx.client_guid, ses->server->client_guid,
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200258 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600259 ctx.use_client_guid = true;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200260
Shyam Prasad N724244c2021-07-19 10:54:46 +0000261 chan_server = cifs_get_tcp_session(&ctx);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200262
Shyam Prasad N724244c2021-07-19 10:54:46 +0000263 mutex_lock(&ses->session_mutex);
264 spin_lock(&ses->chan_lock);
Aurelien Aptel8eec7952020-04-24 15:24:05 +0200265 chan = ses->binding_chan = &ses->chans[ses->chan_count];
Shyam Prasad N724244c2021-07-19 10:54:46 +0000266 chan->server = chan_server;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200267 if (IS_ERR(chan->server)) {
268 rc = PTR_ERR(chan->server);
269 chan->server = NULL;
Shyam Prasad N724244c2021-07-19 10:54:46 +0000270 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200271 goto out;
272 }
Shyam Prasad N724244c2021-07-19 10:54:46 +0000273 spin_unlock(&ses->chan_lock);
274
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -0300275 spin_lock(&cifs_tcp_ses_lock);
276 chan->server->is_channel = true;
277 spin_unlock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200278
279 /*
280 * We need to allocate the server crypto now as we will need
281 * to sign packets before we generate the channel signing key
282 * (we sign with the session key)
283 */
284 rc = smb311_crypto_shash_allocate(chan->server);
285 if (rc) {
286 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
287 goto out;
288 }
289
290 ses->binding = true;
291 rc = cifs_negotiate_protocol(xid, ses);
292 if (rc)
293 goto out;
294
Ronnie Sahlberg387ec582020-12-14 16:40:20 +1000295 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200296 if (rc)
297 goto out;
298
299 /* success, put it on the list
Steve Frenchbbbf9ea2020-05-30 17:29:50 -0500300 * XXX: sharing ses between 2 tcp servers is not possible, the
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200301 * way "internal" linked lists works in linux makes element
302 * only able to belong to one list
303 *
304 * the binding session is already established so the rest of
305 * the code should be able to look it up, no need to add the
306 * ses to the new server.
307 */
308
Shyam Prasad N724244c2021-07-19 10:54:46 +0000309 spin_lock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200310 ses->chan_count++;
311 atomic_set(&ses->chan_seq, 0);
Shyam Prasad N724244c2021-07-19 10:54:46 +0000312 spin_unlock(&ses->chan_lock);
313
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200314out:
315 ses->binding = false;
Aurelien Aptel8eec7952020-04-24 15:24:05 +0200316 ses->binding_chan = NULL;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200317 mutex_unlock(&ses->session_mutex);
318
319 if (rc && chan->server)
320 cifs_put_tcp_session(chan->server, 0);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200321
322 return rc;
323}
Steve French39798772006-05-31 22:40:51 +0000324
Steve French96daf2b2011-05-27 04:34:02 +0000325static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
Steve French39798772006-05-31 22:40:51 +0000326{
327 __u32 capabilities = 0;
328
329 /* init fields common to all four types of SessSetup */
Steve Frencheca6acf2009-02-20 05:43:09 +0000330 /* Note that offsets for first seven fields in req struct are same */
331 /* in CIFS Specs so does not matter which of 3 forms of struct */
332 /* that we use in next few lines */
333 /* Note that header is initialized to zero in header_assemble */
Steve French39798772006-05-31 22:40:51 +0000334 pSMB->req.AndXCommand = 0xFF;
Jeff Laytonc974bef2011-10-11 06:41:32 -0400335 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
336 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
337 USHRT_MAX));
Steve French39798772006-05-31 22:40:51 +0000338 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
Fabian Frederickbc09d142014-12-10 15:41:15 -0800339 pSMB->req.VcNumber = cpu_to_le16(1);
Steve French39798772006-05-31 22:40:51 +0000340
341 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
342
Steve French790fe572007-07-07 19:25:05 +0000343 /* BB verify whether signing required on neg or just on auth frame
Steve French39798772006-05-31 22:40:51 +0000344 (and NTLM case) */
345
346 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
347 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
348
Jeff Layton38d77c52013-05-26 07:01:00 -0400349 if (ses->server->sign)
Steve French39798772006-05-31 22:40:51 +0000350 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
351
352 if (ses->capabilities & CAP_UNICODE) {
353 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
354 capabilities |= CAP_UNICODE;
355 }
356 if (ses->capabilities & CAP_STATUS32) {
357 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
358 capabilities |= CAP_STATUS32;
359 }
360 if (ses->capabilities & CAP_DFS) {
361 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
362 capabilities |= CAP_DFS;
363 }
Steve French26f57362007-08-30 22:09:15 +0000364 if (ses->capabilities & CAP_UNIX)
Steve French39798772006-05-31 22:40:51 +0000365 capabilities |= CAP_UNIX;
Steve French39798772006-05-31 22:40:51 +0000366
Steve French39798772006-05-31 22:40:51 +0000367 return capabilities;
368}
369
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000370static void
371unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
372{
373 char *bcc_ptr = *pbcc_area;
374 int bytes_ret = 0;
375
376 /* Copy OS version */
Steve Frenchacbbb762012-01-18 22:32:33 -0600377 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
378 nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000379 bcc_ptr += 2 * bytes_ret;
Steve Frenchacbbb762012-01-18 22:32:33 -0600380 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
381 32, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000382 bcc_ptr += 2 * bytes_ret;
383 bcc_ptr += 2; /* trailing null */
384
Steve Frenchacbbb762012-01-18 22:32:33 -0600385 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
386 32, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000387 bcc_ptr += 2 * bytes_ret;
388 bcc_ptr += 2; /* trailing null */
389
390 *pbcc_area = bcc_ptr;
391}
392
Steve French96daf2b2011-05-27 04:34:02 +0000393static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000394 const struct nls_table *nls_cp)
395{
396 char *bcc_ptr = *pbcc_area;
397 int bytes_ret = 0;
398
399 /* copy domain */
400 if (ses->domainName == NULL) {
401 /* Sending null domain better than using a bogus domain name (as
402 we did briefly in 2.6.18) since server will use its default */
403 *bcc_ptr = 0;
404 *(bcc_ptr+1) = 0;
405 bytes_ret = 0;
406 } else
Steve Frenchacbbb762012-01-18 22:32:33 -0600407 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
Chen Gang057d6332013-07-19 09:01:36 +0800408 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000409 bcc_ptr += 2 * bytes_ret;
410 bcc_ptr += 2; /* account for null terminator */
411
412 *pbcc_area = bcc_ptr;
413}
414
415
Steve French96daf2b2011-05-27 04:34:02 +0000416static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
Steve French790fe572007-07-07 19:25:05 +0000417 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000418{
Steve French790fe572007-07-07 19:25:05 +0000419 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000420 int bytes_ret = 0;
421
422 /* BB FIXME add check that strings total less
423 than 335 or will need to send them as arrays */
424
Steve French0223cf02006-06-27 19:50:57 +0000425 /* unicode strings, must be word aligned before the call */
426/* if ((long) bcc_ptr % 2) {
Steve French39798772006-05-31 22:40:51 +0000427 *bcc_ptr = 0;
428 bcc_ptr++;
Steve French0223cf02006-06-27 19:50:57 +0000429 } */
Steve French39798772006-05-31 22:40:51 +0000430 /* copy user */
Steve French8727c8a2011-02-25 01:11:56 -0600431 if (ses->user_name == NULL) {
Steve French6e659c62006-11-08 23:10:46 +0000432 /* null user mount */
433 *bcc_ptr = 0;
434 *(bcc_ptr+1) = 0;
Steve French301a6a32010-02-06 07:08:53 +0000435 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -0600436 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -0400437 CIFS_MAX_USERNAME_LEN, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000438 }
439 bcc_ptr += 2 * bytes_ret;
440 bcc_ptr += 2; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000441
Jeff Layton0d3a01f2007-10-16 17:32:19 +0000442 unicode_domain_string(&bcc_ptr, ses, nls_cp);
443 unicode_oslm_strings(&bcc_ptr, nls_cp);
Steve French39798772006-05-31 22:40:51 +0000444
445 *pbcc_area = bcc_ptr;
446}
447
Steve French96daf2b2011-05-27 04:34:02 +0000448static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
Steve French790fe572007-07-07 19:25:05 +0000449 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000450{
Steve French790fe572007-07-07 19:25:05 +0000451 char *bcc_ptr = *pbcc_area;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000452 int len;
Steve French39798772006-05-31 22:40:51 +0000453
454 /* copy user */
455 /* BB what about null user mounts - check that we do this BB */
Steve French790fe572007-07-07 19:25:05 +0000456 /* copy user */
Shirish Pargaonkarde47a412012-02-02 15:28:28 -0600457 if (ses->user_name != NULL) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000458 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
459 if (WARN_ON_ONCE(len < 0))
460 len = CIFS_MAX_USERNAME_LEN - 1;
461 bcc_ptr += len;
Shirish Pargaonkarde47a412012-02-02 15:28:28 -0600462 }
Steve French8727c8a2011-02-25 01:11:56 -0600463 /* else null user mount */
Steve French39798772006-05-31 22:40:51 +0000464 *bcc_ptr = 0;
Steve French790fe572007-07-07 19:25:05 +0000465 bcc_ptr++; /* account for null termination */
Steve French39798772006-05-31 22:40:51 +0000466
Steve French790fe572007-07-07 19:25:05 +0000467 /* copy domain */
Steve French790fe572007-07-07 19:25:05 +0000468 if (ses->domainName != NULL) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000469 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
470 if (WARN_ON_ONCE(len < 0))
471 len = CIFS_MAX_DOMAINNAME_LEN - 1;
472 bcc_ptr += len;
Steve French790fe572007-07-07 19:25:05 +0000473 } /* else we will send a null domain name
Steve French6e659c62006-11-08 23:10:46 +0000474 so the server will default to its own domain */
Steve French39798772006-05-31 22:40:51 +0000475 *bcc_ptr = 0;
476 bcc_ptr++;
477
478 /* BB check for overflow here */
479
480 strcpy(bcc_ptr, "Linux version ");
481 bcc_ptr += strlen("Linux version ");
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700482 strcpy(bcc_ptr, init_utsname()->release);
483 bcc_ptr += strlen(init_utsname()->release) + 1;
Steve French39798772006-05-31 22:40:51 +0000484
485 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
486 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
487
Steve French790fe572007-07-07 19:25:05 +0000488 *pbcc_area = bcc_ptr;
Steve French39798772006-05-31 22:40:51 +0000489}
490
Jeff Layton59140792009-04-30 07:16:21 -0400491static void
Steve French96daf2b2011-05-27 04:34:02 +0000492decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
Jeff Layton59140792009-04-30 07:16:21 -0400493 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000494{
Jeff Layton59140792009-04-30 07:16:21 -0400495 int len;
Steve French790fe572007-07-07 19:25:05 +0000496 char *data = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000497
Joe Perchesf96637b2013-05-04 22:12:25 -0500498 cifs_dbg(FYI, "bleft %d\n", bleft);
Steve French39798772006-05-31 22:40:51 +0000499
Steve French26f57362007-08-30 22:09:15 +0000500 kfree(ses->serverOS);
Steve Frenchacbbb762012-01-18 22:32:33 -0600501 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500502 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
Jeff Layton59140792009-04-30 07:16:21 -0400503 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
504 data += len;
505 bleft -= len;
506 if (bleft <= 0)
507 return;
Steve French39798772006-05-31 22:40:51 +0000508
Steve French26f57362007-08-30 22:09:15 +0000509 kfree(ses->serverNOS);
Steve Frenchacbbb762012-01-18 22:32:33 -0600510 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500511 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
Jeff Layton59140792009-04-30 07:16:21 -0400512 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
513 data += len;
514 bleft -= len;
515 if (bleft <= 0)
516 return;
Steve French39798772006-05-31 22:40:51 +0000517
Steve French26f57362007-08-30 22:09:15 +0000518 kfree(ses->serverDomain);
Steve Frenchacbbb762012-01-18 22:32:33 -0600519 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
Joe Perchesf96637b2013-05-04 22:12:25 -0500520 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
Steve French790fe572007-07-07 19:25:05 +0000521
Jeff Layton59140792009-04-30 07:16:21 -0400522 return;
Steve French39798772006-05-31 22:40:51 +0000523}
524
Jeff Layton7d066452013-05-24 07:41:00 -0400525static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
526 struct cifs_ses *ses,
527 const struct nls_table *nls_cp)
Steve French39798772006-05-31 22:40:51 +0000528{
Steve French39798772006-05-31 22:40:51 +0000529 int len;
Steve French790fe572007-07-07 19:25:05 +0000530 char *bcc_ptr = *pbcc_area;
Steve French39798772006-05-31 22:40:51 +0000531
Joe Perchesf96637b2013-05-04 22:12:25 -0500532 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
Steve French50c2f752007-07-13 00:33:32 +0000533
Steve French39798772006-05-31 22:40:51 +0000534 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000535 if (len >= bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400536 return;
Steve French50c2f752007-07-13 00:33:32 +0000537
Steve French26f57362007-08-30 22:09:15 +0000538 kfree(ses->serverOS);
Steve French39798772006-05-31 22:40:51 +0000539
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000540 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
Namjae Jeon27b7edc2014-08-20 19:39:28 +0900541 if (ses->serverOS) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000542 memcpy(ses->serverOS, bcc_ptr, len);
543 ses->serverOS[len] = 0;
Namjae Jeon27b7edc2014-08-20 19:39:28 +0900544 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
545 cifs_dbg(FYI, "OS/2 server\n");
546 }
Steve French39798772006-05-31 22:40:51 +0000547
548 bcc_ptr += len + 1;
549 bleft -= len + 1;
550
551 len = strnlen(bcc_ptr, bleft);
Steve French790fe572007-07-07 19:25:05 +0000552 if (len >= bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400553 return;
Steve French39798772006-05-31 22:40:51 +0000554
Steve French26f57362007-08-30 22:09:15 +0000555 kfree(ses->serverNOS);
Steve French39798772006-05-31 22:40:51 +0000556
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000557 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
558 if (ses->serverNOS) {
559 memcpy(ses->serverNOS, bcc_ptr, len);
560 ses->serverNOS[len] = 0;
561 }
Steve French39798772006-05-31 22:40:51 +0000562
563 bcc_ptr += len + 1;
564 bleft -= len + 1;
565
Steve French790fe572007-07-07 19:25:05 +0000566 len = strnlen(bcc_ptr, bleft);
567 if (len > bleft)
Jeff Layton7d066452013-05-24 07:41:00 -0400568 return;
Steve French39798772006-05-31 22:40:51 +0000569
Steve French9ac00b72006-09-30 04:13:17 +0000570 /* No domain field in LANMAN case. Domain is
571 returned by old servers in the SMB negprot response */
572 /* BB For newer servers which do not support Unicode,
573 but thus do return domain here we could add parsing
574 for it later, but it is not very important */
Joe Perchesf96637b2013-05-04 22:12:25 -0500575 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
Steve French39798772006-05-31 22:40:51 +0000576}
577
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400578int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
Steve French96daf2b2011-05-27 04:34:02 +0000579 struct cifs_ses *ses)
Steve French0b3cc8582009-05-04 08:37:12 +0000580{
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500581 unsigned int tioffset; /* challenge message target info area */
582 unsigned int tilen; /* challenge message target info area length */
583
Steve French0b3cc8582009-05-04 08:37:12 +0000584 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
585
586 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500587 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
Steve French0b3cc8582009-05-04 08:37:12 +0000588 return -EINVAL;
589 }
590
591 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500592 cifs_dbg(VFS, "blob signature incorrect %s\n",
593 pblob->Signature);
Steve French0b3cc8582009-05-04 08:37:12 +0000594 return -EINVAL;
595 }
596 if (pblob->MessageType != NtLmChallenge) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500597 cifs_dbg(VFS, "Incorrect message type %d\n",
598 pblob->MessageType);
Steve French0b3cc8582009-05-04 08:37:12 +0000599 return -EINVAL;
600 }
601
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500602 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
Steve French0b3cc8582009-05-04 08:37:12 +0000603 /* BB we could decode pblob->NegotiateFlags; some may be useful */
604 /* In particular we can examine sign flags */
605 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
606 we must set the MIC field of the AUTHENTICATE_MESSAGE */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500607 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
Steve French5443d132011-03-13 05:08:25 +0000608 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
609 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
Dan Carpenter4991a5f2012-01-31 11:52:01 +0300610 if (tioffset > blob_len || tioffset + tilen > blob_len) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700611 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
612 tioffset, tilen);
Dan Carpenter4991a5f2012-01-31 11:52:01 +0300613 return -EINVAL;
614 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500615 if (tilen) {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +0200616 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
617 GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500618 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700619 cifs_dbg(VFS, "Challenge target info alloc failure\n");
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500620 return -ENOMEM;
621 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500622 ses->auth_key.len = tilen;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500623 }
624
Steve French0b3cc8582009-05-04 08:37:12 +0000625 return 0;
626}
627
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000628static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
629{
630 int sz = base_size + ses->auth_key.len
631 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
632
633 if (ses->domainName)
634 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
635 else
636 sz += sizeof(__le16);
637
638 if (ses->user_name)
639 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
640 else
641 sz += sizeof(__le16);
642
643 sz += sizeof(__le16) * strnlen(ses->workstation_name, CIFS_MAX_WORKSTATION_LEN);
644
645 return sz;
646}
647
648static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
649 char *str_value,
650 int str_length,
651 unsigned char *pstart,
652 unsigned char **pcur,
653 const struct nls_table *nls_cp)
654{
655 unsigned char *tmp = pstart;
656 int len;
657
658 if (!pbuf)
659 return;
660
661 if (!pcur)
662 pcur = &tmp;
663
664 if (!str_value) {
665 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
666 pbuf->Length = 0;
667 pbuf->MaximumLength = 0;
668 *pcur += sizeof(__le16);
669 } else {
670 len = cifs_strtoUTF16((__le16 *)*pcur,
671 str_value,
672 str_length,
673 nls_cp);
674 len *= sizeof(__le16);
675 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
676 pbuf->Length = cpu_to_le16(len);
677 pbuf->MaximumLength = cpu_to_le16(len);
678 *pcur += len;
679 }
680}
681
Steve French0b3cc8582009-05-04 08:37:12 +0000682/* BB Move to ntlmssp.c eventually */
683
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000684int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
685 u16 *buflen,
686 struct cifs_ses *ses,
687 const struct nls_table *nls_cp)
Steve French0b3cc8582009-05-04 08:37:12 +0000688{
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000689 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200690 struct TCP_Server_Info *server = cifs_ses_server(ses);
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000691 NEGOTIATE_MESSAGE *sec_blob;
Steve French0b3cc8582009-05-04 08:37:12 +0000692 __u32 flags;
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000693 unsigned char *tmp;
694 int len;
Steve French0b3cc8582009-05-04 08:37:12 +0000695
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000696 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
697 *pbuffer = kmalloc(len, GFP_KERNEL);
698 if (!*pbuffer) {
699 rc = -ENOMEM;
700 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
701 *buflen = 0;
702 goto setup_ntlm_neg_ret;
703 }
704 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
705
706 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
Steve French0b3cc8582009-05-04 08:37:12 +0000707 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
708 sec_blob->MessageType = NtLmNegotiate;
709
710 /* BB is NTLMV2 session security format easier to use here? */
711 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
712 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800713 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
714 NTLMSSP_NEGOTIATE_SEAL;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200715 if (server->sign)
Steve French745e5072010-09-08 21:09:27 +0000716 flags |= NTLMSSP_NEGOTIATE_SIGN;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200717 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800718 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
Steve French0b3cc8582009-05-04 08:37:12 +0000719
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000720 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600721 sec_blob->NegotiateFlags = cpu_to_le32(flags);
Steve French0b3cc8582009-05-04 08:37:12 +0000722
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000723 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
724 cifs_security_buffer_from_str(&sec_blob->DomainName,
725 NULL,
726 CIFS_MAX_DOMAINNAME_LEN,
727 *pbuffer, &tmp,
728 nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000729
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000730 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
731 NULL,
732 CIFS_MAX_WORKSTATION_LEN,
733 *pbuffer, &tmp,
734 nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000735
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000736 *buflen = tmp - *pbuffer;
737setup_ntlm_neg_ret:
738 return rc;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200739}
740
741int build_ntlmssp_auth_blob(unsigned char **pbuffer,
Shirish Pargaonkar89f150f2010-10-19 11:47:52 -0500742 u16 *buflen,
Steve French96daf2b2011-05-27 04:34:02 +0000743 struct cifs_ses *ses,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500744 const struct nls_table *nls_cp)
Steve French0b3cc8582009-05-04 08:37:12 +0000745{
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500746 int rc;
Jerome Marchandb8da3442016-05-26 11:52:25 +0200747 AUTHENTICATE_MESSAGE *sec_blob;
Steve French0b3cc8582009-05-04 08:37:12 +0000748 __u32 flags;
749 unsigned char *tmp;
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000750 int len;
Steve French0b3cc8582009-05-04 08:37:12 +0000751
Jerome Marchandb8da3442016-05-26 11:52:25 +0200752 rc = setup_ntlmv2_rsp(ses, nls_cp);
753 if (rc) {
754 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
755 *buflen = 0;
756 goto setup_ntlmv2_ret;
757 }
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000758
759 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
760 *pbuffer = kmalloc(len, GFP_KERNEL);
Nicholas Mc Guire126c97f2018-08-23 12:24:02 +0200761 if (!*pbuffer) {
762 rc = -ENOMEM;
763 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
764 *buflen = 0;
765 goto setup_ntlmv2_ret;
766 }
Jerome Marchandb8da3442016-05-26 11:52:25 +0200767 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
768
Steve French0b3cc8582009-05-04 08:37:12 +0000769 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
770 sec_blob->MessageType = NtLmAuthenticate;
771
772 flags = NTLMSSP_NEGOTIATE_56 |
773 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
774 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800775 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000776 NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800777 if (ses->server->sign)
Steve French0b3cc8582009-05-04 08:37:12 +0000778 flags |= NTLMSSP_NEGOTIATE_SIGN;
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800779 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
780 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
Steve French0b3cc8582009-05-04 08:37:12 +0000781
Jerome Marchandb8da3442016-05-26 11:52:25 +0200782 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600783 sec_blob->NegotiateFlags = cpu_to_le32(flags);
Steve French0b3cc8582009-05-04 08:37:12 +0000784
785 sec_blob->LmChallengeResponse.BufferOffset =
786 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
787 sec_blob->LmChallengeResponse.Length = 0;
788 sec_blob->LmChallengeResponse.MaximumLength = 0;
789
Jerome Marchandb8da3442016-05-26 11:52:25 +0200790 sec_blob->NtChallengeResponse.BufferOffset =
791 cpu_to_le32(tmp - *pbuffer);
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200792 if (ses->user_name != NULL) {
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200793 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
794 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
795 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000796
Stefan Metzmachercfda35d2016-05-03 10:52:30 +0200797 sec_blob->NtChallengeResponse.Length =
798 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
799 sec_blob->NtChallengeResponse.MaximumLength =
800 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
801 } else {
802 /*
803 * don't send an NT Response for anonymous access
804 */
805 sec_blob->NtChallengeResponse.Length = 0;
806 sec_blob->NtChallengeResponse.MaximumLength = 0;
807 }
Steve French0b3cc8582009-05-04 08:37:12 +0000808
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000809 cifs_security_buffer_from_str(&sec_blob->DomainName,
810 ses->domainName,
811 CIFS_MAX_DOMAINNAME_LEN,
812 *pbuffer, &tmp,
813 nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000814
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000815 cifs_security_buffer_from_str(&sec_blob->UserName,
816 ses->user_name,
817 CIFS_MAX_USERNAME_LEN,
818 *pbuffer, &tmp,
819 nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000820
Shyam Prasad N49bd49f2021-11-05 19:03:57 +0000821 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
822 ses->workstation_name,
823 CIFS_MAX_WORKSTATION_LEN,
824 *pbuffer, &tmp,
825 nls_cp);
Steve French0b3cc8582009-05-04 08:37:12 +0000826
Shirish Pargaonkardf8fbc242010-12-11 14:19:22 -0600827 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
828 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
829 && !calc_seckey(ses)) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500830 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Jerome Marchandb8da3442016-05-26 11:52:25 +0200831 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500832 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
833 sec_blob->SessionKey.MaximumLength =
834 cpu_to_le16(CIFS_CPHTXT_SIZE);
835 tmp += CIFS_CPHTXT_SIZE;
836 } else {
Jerome Marchandb8da3442016-05-26 11:52:25 +0200837 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500838 sec_blob->SessionKey.Length = 0;
839 sec_blob->SessionKey.MaximumLength = 0;
840 }
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500841
Jerome Marchandb8da3442016-05-26 11:52:25 +0200842 *buflen = tmp - *pbuffer;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500843setup_ntlmv2_ret:
Shirish Pargaonkar89f150f2010-10-19 11:47:52 -0500844 return rc;
Steve French0b3cc8582009-05-04 08:37:12 +0000845}
Steve French0b3cc8582009-05-04 08:37:12 +0000846
Jeff Layton3f618222013-06-12 19:52:14 -0500847enum securityEnum
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530848cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
Jeff Layton3f618222013-06-12 19:52:14 -0500849{
850 switch (server->negflavor) {
851 case CIFS_NEGFLAVOR_EXTENDED:
852 switch (requested) {
853 case Kerberos:
854 case RawNTLMSSP:
855 return requested;
856 case Unspecified:
857 if (server->sec_ntlmssp &&
858 (global_secflags & CIFSSEC_MAY_NTLMSSP))
859 return RawNTLMSSP;
860 if ((server->sec_kerberos || server->sec_mskerberos) &&
861 (global_secflags & CIFSSEC_MAY_KRB5))
862 return Kerberos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500863 fallthrough;
Jeff Layton3f618222013-06-12 19:52:14 -0500864 default:
865 return Unspecified;
866 }
867 case CIFS_NEGFLAVOR_UNENCAP:
868 switch (requested) {
Jeff Layton3f618222013-06-12 19:52:14 -0500869 case NTLMv2:
870 return requested;
871 case Unspecified:
872 if (global_secflags & CIFSSEC_MAY_NTLMV2)
873 return NTLMv2;
Gustavo A. R. Silva21ac58f2020-11-20 12:24:14 -0600874 break;
Jeff Layton3f618222013-06-12 19:52:14 -0500875 default:
Sachin Prabhudde23562013-09-27 18:35:42 +0100876 break;
Jeff Layton3f618222013-06-12 19:52:14 -0500877 }
Ronnie Sahlberg76a3c922021-08-19 20:34:58 +1000878 fallthrough;
Jeff Layton3f618222013-06-12 19:52:14 -0500879 default:
880 return Unspecified;
881 }
882}
883
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100884struct sess_data {
885 unsigned int xid;
886 struct cifs_ses *ses;
887 struct nls_table *nls_cp;
888 void (*func)(struct sess_data *);
889 int result;
890
891 /* we will send the SMB in three pieces:
892 * a fixed length beginning part, an optional
893 * SPNEGO blob (which can be zero length), and a
894 * last part which will include the strings
895 * and rest of bcc area. This allows us to avoid
896 * a large buffer 17K allocation
897 */
898 int buf0_type;
899 struct kvec iov[3];
900};
901
902static int
903sess_alloc_buffer(struct sess_data *sess_data, int wct)
904{
905 int rc;
906 struct cifs_ses *ses = sess_data->ses;
907 struct smb_hdr *smb_buf;
908
909 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
910 (void **)&smb_buf);
911
912 if (rc)
913 return rc;
914
915 sess_data->iov[0].iov_base = (char *)smb_buf;
916 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
917 /*
918 * This variable will be used to clear the buffer
919 * allocated above in case of any error in the calling function.
920 */
921 sess_data->buf0_type = CIFS_SMALL_BUFFER;
922
923 /* 2000 big enough to fit max user, domain, NOS name etc. */
924 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
925 if (!sess_data->iov[2].iov_base) {
926 rc = -ENOMEM;
927 goto out_free_smb_buf;
928 }
929
930 return 0;
931
932out_free_smb_buf:
Ding Huid72c7412021-08-17 22:55:10 +0800933 cifs_small_buf_release(smb_buf);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100934 sess_data->iov[0].iov_base = NULL;
935 sess_data->iov[0].iov_len = 0;
936 sess_data->buf0_type = CIFS_NO_BUFFER;
937 return rc;
938}
939
940static void
941sess_free_buffer(struct sess_data *sess_data)
942{
943
944 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
945 sess_data->buf0_type = CIFS_NO_BUFFER;
946 kfree(sess_data->iov[2].iov_base);
947}
948
949static int
950sess_establish_session(struct sess_data *sess_data)
951{
952 struct cifs_ses *ses = sess_data->ses;
953
954 mutex_lock(&ses->server->srv_mutex);
955 if (!ses->server->session_estab) {
956 if (ses->server->sign) {
957 ses->server->session_key.response =
958 kmemdup(ses->auth_key.response,
959 ses->auth_key.len, GFP_KERNEL);
960 if (!ses->server->session_key.response) {
961 mutex_unlock(&ses->server->srv_mutex);
962 return -ENOMEM;
963 }
964 ses->server->session_key.len =
965 ses->auth_key.len;
966 }
967 ses->server->sequence_number = 0x2;
968 ses->server->session_estab = true;
969 }
970 mutex_unlock(&ses->server->srv_mutex);
971
972 cifs_dbg(FYI, "CIFS session established successfully\n");
973 spin_lock(&GlobalMid_Lock);
974 ses->status = CifsGood;
975 ses->need_reconnect = false;
976 spin_unlock(&GlobalMid_Lock);
977
978 return 0;
979}
980
981static int
982sess_sendreceive(struct sess_data *sess_data)
983{
984 int rc;
985 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
986 __u16 count;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700987 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100988
989 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
Qinglang Miao1a0e7f72020-07-25 16:56:01 +0800990 be32_add_cpu(&smb_buf->smb_buf_length, count);
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100991 put_bcc(count, smb_buf);
992
993 rc = SendReceive2(sess_data->xid, sess_data->ses,
994 sess_data->iov, 3 /* num_iovecs */,
995 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700996 CIFS_LOG_ERROR, &rsp_iov);
997 cifs_small_buf_release(sess_data->iov[0].iov_base);
998 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu80a0e632014-06-16 15:35:25 +0100999
1000 return rc;
1001}
1002
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001003static void
1004sess_auth_ntlmv2(struct sess_data *sess_data)
1005{
1006 int rc = 0;
1007 struct smb_hdr *smb_buf;
1008 SESSION_SETUP_ANDX *pSMB;
1009 char *bcc_ptr;
1010 struct cifs_ses *ses = sess_data->ses;
1011 __u32 capabilities;
1012 __u16 bytes_remaining;
1013
1014 /* old style NTLM sessionsetup */
1015 /* wct = 13 */
1016 rc = sess_alloc_buffer(sess_data, 13);
1017 if (rc)
1018 goto out;
1019
1020 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1021 bcc_ptr = sess_data->iov[2].iov_base;
1022 capabilities = cifs_ssetup_hdr(ses, pSMB);
1023
1024 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1025
1026 /* LM2 password would be here if we supported it */
1027 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1028
Stefan Metzmacher1a967d62016-05-03 10:52:30 +02001029 if (ses->user_name != NULL) {
1030 /* calculate nlmv2 response and session key */
1031 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1032 if (rc) {
1033 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1034 goto out;
1035 }
1036
1037 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1038 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1039 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1040
1041 /* set case sensitive password length after tilen may get
1042 * assigned, tilen is 0 otherwise.
1043 */
1044 pSMB->req_no_secext.CaseSensitivePasswordLength =
1045 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1046 } else {
1047 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001048 }
1049
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001050 if (ses->capabilities & CAP_UNICODE) {
1051 if (sess_data->iov[0].iov_len % 2) {
1052 *bcc_ptr = 0;
1053 bcc_ptr++;
1054 }
1055 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1056 } else {
1057 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1058 }
1059
1060
1061 sess_data->iov[2].iov_len = (long) bcc_ptr -
1062 (long) sess_data->iov[2].iov_base;
1063
1064 rc = sess_sendreceive(sess_data);
1065 if (rc)
1066 goto out;
1067
1068 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1069 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1070
1071 if (smb_buf->WordCount != 3) {
1072 rc = -EIO;
1073 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1074 goto out;
1075 }
1076
1077 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1078 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1079
1080 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1081 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1082
1083 bytes_remaining = get_bcc(smb_buf);
1084 bcc_ptr = pByteArea(smb_buf);
1085
1086 /* BB check if Unicode and decode strings */
1087 if (bytes_remaining == 0) {
1088 /* no string area to decode, do nothing */
1089 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1090 /* unicode string area must be word-aligned */
1091 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1092 ++bcc_ptr;
1093 --bytes_remaining;
1094 }
1095 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1096 sess_data->nls_cp);
1097 } else {
1098 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1099 sess_data->nls_cp);
1100 }
1101
1102 rc = sess_establish_session(sess_data);
1103out:
1104 sess_data->result = rc;
1105 sess_data->func = NULL;
1106 sess_free_buffer(sess_data);
1107 kfree(ses->auth_key.response);
1108 ses->auth_key.response = NULL;
1109}
1110
Sachin Prabhuee03c642014-06-16 15:35:27 +01001111#ifdef CONFIG_CIFS_UPCALL
1112static void
1113sess_auth_kerberos(struct sess_data *sess_data)
1114{
1115 int rc = 0;
1116 struct smb_hdr *smb_buf;
1117 SESSION_SETUP_ANDX *pSMB;
1118 char *bcc_ptr;
1119 struct cifs_ses *ses = sess_data->ses;
1120 __u32 capabilities;
1121 __u16 bytes_remaining;
1122 struct key *spnego_key = NULL;
1123 struct cifs_spnego_msg *msg;
1124 u16 blob_len;
1125
1126 /* extended security */
1127 /* wct = 12 */
1128 rc = sess_alloc_buffer(sess_data, 12);
1129 if (rc)
1130 goto out;
1131
1132 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1133 bcc_ptr = sess_data->iov[2].iov_base;
1134 capabilities = cifs_ssetup_hdr(ses, pSMB);
1135
1136 spnego_key = cifs_get_spnego_key(ses);
1137 if (IS_ERR(spnego_key)) {
1138 rc = PTR_ERR(spnego_key);
1139 spnego_key = NULL;
1140 goto out;
1141 }
1142
David Howells146aa8b2015-10-21 14:04:48 +01001143 msg = spnego_key->payload.data[0];
Sachin Prabhuee03c642014-06-16 15:35:27 +01001144 /*
1145 * check version field to make sure that cifs.upcall is
1146 * sending us a response in an expected form
1147 */
1148 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001149 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1150 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhuee03c642014-06-16 15:35:27 +01001151 rc = -EKEYREJECTED;
1152 goto out_put_spnego_key;
1153 }
1154
1155 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1156 GFP_KERNEL);
1157 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001158 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1159 msg->sesskey_len);
Sachin Prabhuee03c642014-06-16 15:35:27 +01001160 rc = -ENOMEM;
1161 goto out_put_spnego_key;
1162 }
1163 ses->auth_key.len = msg->sesskey_len;
1164
1165 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1166 capabilities |= CAP_EXTENDED_SECURITY;
1167 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1168 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1169 sess_data->iov[1].iov_len = msg->secblob_len;
1170 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1171
1172 if (ses->capabilities & CAP_UNICODE) {
1173 /* unicode strings must be word aligned */
1174 if ((sess_data->iov[0].iov_len
1175 + sess_data->iov[1].iov_len) % 2) {
1176 *bcc_ptr = 0;
1177 bcc_ptr++;
1178 }
1179 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1180 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1181 } else {
1182 /* BB: is this right? */
1183 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1184 }
1185
1186 sess_data->iov[2].iov_len = (long) bcc_ptr -
1187 (long) sess_data->iov[2].iov_base;
1188
1189 rc = sess_sendreceive(sess_data);
1190 if (rc)
1191 goto out_put_spnego_key;
1192
1193 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1194 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1195
1196 if (smb_buf->WordCount != 4) {
1197 rc = -EIO;
1198 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1199 goto out_put_spnego_key;
1200 }
1201
1202 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1203 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1204
1205 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1206 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1207
1208 bytes_remaining = get_bcc(smb_buf);
1209 bcc_ptr = pByteArea(smb_buf);
1210
1211 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1212 if (blob_len > bytes_remaining) {
1213 cifs_dbg(VFS, "bad security blob length %d\n",
1214 blob_len);
1215 rc = -EINVAL;
1216 goto out_put_spnego_key;
1217 }
1218 bcc_ptr += blob_len;
1219 bytes_remaining -= blob_len;
1220
1221 /* BB check if Unicode and decode strings */
1222 if (bytes_remaining == 0) {
1223 /* no string area to decode, do nothing */
1224 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1225 /* unicode string area must be word-aligned */
1226 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1227 ++bcc_ptr;
1228 --bytes_remaining;
1229 }
1230 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1231 sess_data->nls_cp);
1232 } else {
1233 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1234 sess_data->nls_cp);
1235 }
1236
1237 rc = sess_establish_session(sess_data);
1238out_put_spnego_key:
1239 key_invalidate(spnego_key);
1240 key_put(spnego_key);
1241out:
1242 sess_data->result = rc;
1243 sess_data->func = NULL;
1244 sess_free_buffer(sess_data);
1245 kfree(ses->auth_key.response);
1246 ses->auth_key.response = NULL;
1247}
1248
Sachin Prabhuee03c642014-06-16 15:35:27 +01001249#endif /* ! CONFIG_CIFS_UPCALL */
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001250
Sachin Prabhucc87c472014-06-16 15:35:28 +01001251/*
1252 * The required kvec buffers have to be allocated before calling this
1253 * function.
1254 */
1255static int
1256_sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
Steve French39798772006-05-31 22:40:51 +00001257{
Steve French39798772006-05-31 22:40:51 +00001258 SESSION_SETUP_ANDX *pSMB;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001259 struct cifs_ses *ses = sess_data->ses;
Steve French39798772006-05-31 22:40:51 +00001260 __u32 capabilities;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001261 char *bcc_ptr;
Steve French254e55e2006-06-04 05:53:15 +00001262
Sachin Prabhucc87c472014-06-16 15:35:28 +01001263 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001264
1265 capabilities = cifs_ssetup_hdr(ses, pSMB);
1266 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1267 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1268 return -ENOSYS;
Jeff Layton3534b852013-05-24 07:41:01 -04001269 }
Steve French39798772006-05-31 22:40:51 +00001270
Sachin Prabhucc87c472014-06-16 15:35:28 +01001271 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1272 capabilities |= CAP_EXTENDED_SECURITY;
1273 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1274
1275 bcc_ptr = sess_data->iov[2].iov_base;
1276 /* unicode strings must be word aligned */
1277 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1278 *bcc_ptr = 0;
1279 bcc_ptr++;
1280 }
1281 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1282
1283 sess_data->iov[2].iov_len = (long) bcc_ptr -
1284 (long) sess_data->iov[2].iov_base;
1285
1286 return 0;
1287}
1288
1289static void
1290sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1291
1292static void
1293sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1294{
1295 int rc;
1296 struct smb_hdr *smb_buf;
1297 SESSION_SETUP_ANDX *pSMB;
1298 struct cifs_ses *ses = sess_data->ses;
1299 __u16 bytes_remaining;
1300 char *bcc_ptr;
Shyam Prasad N49bd49f2021-11-05 19:03:57 +00001301 unsigned char *ntlmsspblob = NULL;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001302 u16 blob_len;
1303
1304 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1305
1306 /*
1307 * if memory allocation is successful, caller of this function
1308 * frees it.
1309 */
1310 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1311 if (!ses->ntlmssp) {
1312 rc = -ENOMEM;
1313 goto out;
1314 }
1315 ses->ntlmssp->sesskey_per_smbsess = false;
1316
1317 /* wct = 12 */
1318 rc = sess_alloc_buffer(sess_data, 12);
1319 if (rc)
1320 goto out;
1321
1322 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1323
1324 /* Build security blob before we assemble the request */
Shyam Prasad N49bd49f2021-11-05 19:03:57 +00001325 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1326 &blob_len, ses,
1327 sess_data->nls_cp);
1328 if (rc)
1329 goto out;
1330
1331 sess_data->iov[1].iov_len = blob_len;
1332 sess_data->iov[1].iov_base = ntlmsspblob;
1333 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
Sachin Prabhucc87c472014-06-16 15:35:28 +01001334
1335 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1336 if (rc)
1337 goto out;
1338
1339 rc = sess_sendreceive(sess_data);
1340
1341 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1342 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1343
1344 /* If true, rc here is expected and not an error */
1345 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1346 smb_buf->Status.CifsError ==
1347 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1348 rc = 0;
1349
1350 if (rc)
1351 goto out;
1352
1353 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1354
1355 if (smb_buf->WordCount != 4) {
1356 rc = -EIO;
1357 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1358 goto out;
1359 }
1360
1361 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1362 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1363
1364 bytes_remaining = get_bcc(smb_buf);
1365 bcc_ptr = pByteArea(smb_buf);
1366
1367 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1368 if (blob_len > bytes_remaining) {
1369 cifs_dbg(VFS, "bad security blob length %d\n",
1370 blob_len);
1371 rc = -EINVAL;
1372 goto out;
1373 }
1374
1375 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1376out:
1377 sess_free_buffer(sess_data);
1378
1379 if (!rc) {
1380 sess_data->func = sess_auth_rawntlmssp_authenticate;
1381 return;
1382 }
1383
1384 /* Else error. Cleanup */
1385 kfree(ses->auth_key.response);
1386 ses->auth_key.response = NULL;
1387 kfree(ses->ntlmssp);
1388 ses->ntlmssp = NULL;
1389
1390 sess_data->func = NULL;
1391 sess_data->result = rc;
1392}
1393
1394static void
1395sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1396{
1397 int rc;
1398 struct smb_hdr *smb_buf;
1399 SESSION_SETUP_ANDX *pSMB;
1400 struct cifs_ses *ses = sess_data->ses;
1401 __u16 bytes_remaining;
1402 char *bcc_ptr;
Jerome Marchandb8da3442016-05-26 11:52:25 +02001403 unsigned char *ntlmsspblob = NULL;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001404 u16 blob_len;
1405
1406 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1407
1408 /* wct = 12 */
1409 rc = sess_alloc_buffer(sess_data, 12);
1410 if (rc)
1411 goto out;
1412
1413 /* Build security blob before we assemble the request */
1414 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1415 smb_buf = (struct smb_hdr *)pSMB;
Jerome Marchandb8da3442016-05-26 11:52:25 +02001416 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
Sachin Prabhucc87c472014-06-16 15:35:28 +01001417 &blob_len, ses, sess_data->nls_cp);
1418 if (rc)
1419 goto out_free_ntlmsspblob;
1420 sess_data->iov[1].iov_len = blob_len;
1421 sess_data->iov[1].iov_base = ntlmsspblob;
1422 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1423 /*
1424 * Make sure that we tell the server that we are using
1425 * the uid that it just gave us back on the response
1426 * (challenge)
1427 */
1428 smb_buf->Uid = ses->Suid;
1429
1430 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1431 if (rc)
1432 goto out_free_ntlmsspblob;
1433
1434 rc = sess_sendreceive(sess_data);
1435 if (rc)
1436 goto out_free_ntlmsspblob;
1437
1438 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1439 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1440 if (smb_buf->WordCount != 4) {
1441 rc = -EIO;
1442 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1443 goto out_free_ntlmsspblob;
1444 }
1445
1446 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1447 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1448
Sachin Prabhuee9bbf42014-12-03 12:26:36 +00001449 if (ses->Suid != smb_buf->Uid) {
1450 ses->Suid = smb_buf->Uid;
1451 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1452 }
1453
Sachin Prabhucc87c472014-06-16 15:35:28 +01001454 bytes_remaining = get_bcc(smb_buf);
1455 bcc_ptr = pByteArea(smb_buf);
1456 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1457 if (blob_len > bytes_remaining) {
1458 cifs_dbg(VFS, "bad security blob length %d\n",
1459 blob_len);
1460 rc = -EINVAL;
1461 goto out_free_ntlmsspblob;
1462 }
1463 bcc_ptr += blob_len;
1464 bytes_remaining -= blob_len;
1465
1466
1467 /* BB check if Unicode and decode strings */
1468 if (bytes_remaining == 0) {
1469 /* no string area to decode, do nothing */
1470 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1471 /* unicode string area must be word-aligned */
1472 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1473 ++bcc_ptr;
1474 --bytes_remaining;
1475 }
1476 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1477 sess_data->nls_cp);
1478 } else {
1479 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1480 sess_data->nls_cp);
1481 }
1482
1483out_free_ntlmsspblob:
1484 kfree(ntlmsspblob);
1485out:
1486 sess_free_buffer(sess_data);
1487
1488 if (!rc)
1489 rc = sess_establish_session(sess_data);
1490
1491 /* Cleanup */
1492 kfree(ses->auth_key.response);
1493 ses->auth_key.response = NULL;
1494 kfree(ses->ntlmssp);
1495 ses->ntlmssp = NULL;
1496
1497 sess_data->func = NULL;
1498 sess_data->result = rc;
1499}
1500
Steve French27924072014-07-11 07:47:01 -05001501static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
Sachin Prabhucc87c472014-06-16 15:35:28 +01001502{
1503 int type;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001504
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301505 type = cifs_select_sectype(ses->server, ses->sectype);
Joe Perchesf96637b2013-05-04 22:12:25 -05001506 cifs_dbg(FYI, "sess setup type %d\n", type);
Jeff Layton3f618222013-06-12 19:52:14 -05001507 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001508 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Jeff Layton3f618222013-06-12 19:52:14 -05001509 return -EINVAL;
1510 }
1511
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001512 switch (type) {
Sachin Prabhu583cf7a2014-06-16 15:35:26 +01001513 case NTLMv2:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001514 sess_data->func = sess_auth_ntlmv2;
1515 break;
Sachin Prabhuee03c642014-06-16 15:35:27 +01001516 case Kerberos:
Sachin Prabhucc87c472014-06-16 15:35:28 +01001517#ifdef CONFIG_CIFS_UPCALL
1518 sess_data->func = sess_auth_kerberos;
1519 break;
1520#else
1521 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1522 return -ENOSYS;
Sachin Prabhucc87c472014-06-16 15:35:28 +01001523#endif /* CONFIG_CIFS_UPCALL */
1524 case RawNTLMSSP:
1525 sess_data->func = sess_auth_rawntlmssp_negotiate;
1526 break;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001527 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001528 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhucc87c472014-06-16 15:35:28 +01001529 return -ENOSYS;
Steve French39798772006-05-31 22:40:51 +00001530 }
1531
Sachin Prabhucc87c472014-06-16 15:35:28 +01001532 return 0;
1533}
Steve French24424212007-11-16 23:37:35 +00001534
Sachin Prabhucc87c472014-06-16 15:35:28 +01001535int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1536 const struct nls_table *nls_cp)
1537{
1538 int rc = 0;
1539 struct sess_data *sess_data;
Steve French39798772006-05-31 22:40:51 +00001540
Sachin Prabhucc87c472014-06-16 15:35:28 +01001541 if (ses == NULL) {
1542 WARN(1, "%s: ses == NULL!", __func__);
1543 return -EINVAL;
Steve French0b3cc8582009-05-04 08:37:12 +00001544 }
Sachin Prabhucc87c472014-06-16 15:35:28 +01001545
1546 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1547 if (!sess_data)
1548 return -ENOMEM;
1549
1550 rc = select_sec(ses, sess_data);
Steve French0b3cc8582009-05-04 08:37:12 +00001551 if (rc)
Sachin Prabhucc87c472014-06-16 15:35:28 +01001552 goto out;
Steve French0b3cc8582009-05-04 08:37:12 +00001553
Sachin Prabhucc87c472014-06-16 15:35:28 +01001554 sess_data->xid = xid;
1555 sess_data->ses = ses;
1556 sess_data->buf0_type = CIFS_NO_BUFFER;
1557 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve French39798772006-05-31 22:40:51 +00001558
Sachin Prabhucc87c472014-06-16 15:35:28 +01001559 while (sess_data->func)
1560 sess_data->func(sess_data);
Steve French39798772006-05-31 22:40:51 +00001561
Sachin Prabhucc87c472014-06-16 15:35:28 +01001562 /* Store result before we free sess_data */
1563 rc = sess_data->result;
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001564
1565out:
Sachin Prabhu80a0e632014-06-16 15:35:25 +01001566 kfree(sess_data);
1567 return rc;
Steve French39798772006-05-31 22:40:51 +00001568}