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