blob: d1181572758b186c67fccaab572b502aef7497fc [file] [log] [blame]
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001/*
2 * fs/cifs/smb2transport.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002, 2011
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006
8 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 *
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/fs.h>
26#include <linux/list.h>
27#include <linux/wait.h>
28#include <linux/net.h>
29#include <linux/delay.h>
30#include <linux/uaccess.h>
31#include <asm/processor.h>
32#include <linux/mempool.h>
Jeff Laytonfb308a62012-09-18 16:20:35 -070033#include <linux/highmem.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070034#include <crypto/aead.h>
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040035#include "smb2pdu.h"
36#include "cifsglob.h"
37#include "cifsproto.h"
38#include "smb2proto.h"
39#include "cifs_debug.h"
40#include "smb2status.h"
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070041#include "smb2glob.h"
42
Steve French95dc8dd2013-07-04 10:35:21 -050043static int
44smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
45{
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010046 return cifs_alloc_hash("hmac(sha256)",
47 &server->secmech.hmacsha256,
48 &server->secmech.sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -050049}
50
51static int
52smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
53{
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010054 struct cifs_secmech *p = &server->secmech;
Steve French95dc8dd2013-07-04 10:35:21 -050055 int rc;
56
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010057 rc = cifs_alloc_hash("hmac(sha256)",
58 &p->hmacsha256,
59 &p->sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -050060 if (rc)
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010061 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050062
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010063 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
64 if (rc)
65 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050066
67 return 0;
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010068err:
69 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
70 return rc;
Steve French95dc8dd2013-07-04 10:35:21 -050071}
72
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +010073int
74smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
75{
76 struct cifs_secmech *p = &server->secmech;
77 int rc = 0;
78
79 rc = cifs_alloc_hash("hmac(sha256)",
80 &p->hmacsha256,
81 &p->sdeschmacsha256);
82 if (rc)
83 return rc;
84
85 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
86 if (rc)
87 goto err;
88
89 rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512);
90 if (rc)
91 goto err;
92
93 return 0;
94
95err:
96 cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
97 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
98 return rc;
99}
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +0100100
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800101static struct cifs_ses *
102smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
103{
104 struct cifs_ses *ses;
105
106 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
107 if (ses->Suid != ses_id)
108 continue;
109 return ses;
110 }
111
112 return NULL;
113}
114
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700115struct cifs_ses *
116smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500117{
118 struct cifs_ses *ses;
119
120 spin_lock(&cifs_tcp_ses_lock);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800121 ses = smb2_find_smb_ses_unlocked(server, ses_id);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500122 spin_unlock(&cifs_tcp_ses_lock);
123
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800124 return ses;
125}
126
127static struct cifs_tcon *
128smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
129{
130 struct cifs_tcon *tcon;
131
132 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
133 if (tcon->tid != tid)
134 continue;
135 ++tcon->tc_count;
136 return tcon;
137 }
138
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500139 return NULL;
140}
141
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800142/*
143 * Obtain tcon corresponding to the tid in the given
144 * cifs_ses
145 */
146
147struct cifs_tcon *
148smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
149{
150 struct cifs_ses *ses;
151 struct cifs_tcon *tcon;
152
153 spin_lock(&cifs_tcp_ses_lock);
154 ses = smb2_find_smb_ses_unlocked(server, ses_id);
155 if (!ses) {
156 spin_unlock(&cifs_tcp_ses_lock);
157 return NULL;
158 }
159 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
160 spin_unlock(&cifs_tcp_ses_lock);
161
162 return tcon;
163}
164
Steve French38107d42012-12-08 22:08:06 -0600165int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700166smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700167{
Al Viro16c568e2015-11-12 22:46:49 -0500168 int rc;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700169 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
170 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700171 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000172 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500173 struct cifs_ses *ses;
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200174 struct shash_desc *shash;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300175 struct smb_rqst drqst;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500176
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700177 ses = smb2_find_smb_ses(server, shdr->SessionId);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500178 if (!ses) {
179 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
180 return 0;
181 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700182
183 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700184 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700185
Steve French95dc8dd2013-07-04 10:35:21 -0500186 rc = smb2_crypto_shash_allocate(server);
187 if (rc) {
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200188 cifs_dbg(VFS, "%s: sha256 alloc failed\n", __func__);
Steve French95dc8dd2013-07-04 10:35:21 -0500189 return rc;
190 }
191
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700192 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300193 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700194 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500195 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700196 return rc;
197 }
198
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200199 shash = &server->secmech.sdeschmacsha256->shash;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300200 rc = crypto_shash_init(shash);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700201 if (rc) {
Steve French95dc8dd2013-07-04 10:35:21 -0500202 cifs_dbg(VFS, "%s: Could not init sha256", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700203 return rc;
204 }
205
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300206 /*
207 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
208 * data, that is, iov[0] should not contain a rfc1002 length.
209 *
210 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
211 * __cifs_calc_signature().
212 */
213 drqst = *rqst;
214 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
215 rc = crypto_shash_update(shash, iov[0].iov_base,
216 iov[0].iov_len);
217 if (rc) {
218 cifs_dbg(VFS, "%s: Could not update with payload\n",
219 __func__);
220 return rc;
221 }
222 drqst.rq_iov++;
223 drqst.rq_nvec--;
224 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700225
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300226 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500227 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700228 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700229
230 return rc;
231}
232
Steve French373512e2015-12-18 13:05:30 -0600233static int generate_key(struct cifs_ses *ses, struct kvec label,
234 struct kvec context, __u8 *key, unsigned int key_size)
Steve French429b46f2013-06-26 23:45:05 -0500235{
236 unsigned char zero = 0x0;
237 __u8 i[4] = {0, 0, 0, 1};
238 __u8 L[4] = {0, 0, 0, 128};
239 int rc = 0;
240 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
241 unsigned char *hashptr = prfhash;
242
243 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
Steve French373512e2015-12-18 13:05:30 -0600244 memset(key, 0x0, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500245
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500246 rc = smb3_crypto_shash_allocate(ses->server);
Steve French95dc8dd2013-07-04 10:35:21 -0500247 if (rc) {
248 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
249 goto smb3signkey_ret;
250 }
251
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500252 rc = crypto_shash_setkey(ses->server->secmech.hmacsha256,
253 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500254 if (rc) {
255 cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
256 goto smb3signkey_ret;
257 }
258
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500259 rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash);
Steve French429b46f2013-06-26 23:45:05 -0500260 if (rc) {
261 cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
262 goto smb3signkey_ret;
263 }
264
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500265 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500266 i, 4);
267 if (rc) {
268 cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
269 goto smb3signkey_ret;
270 }
271
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500272 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600273 label.iov_base, label.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500274 if (rc) {
275 cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
276 goto smb3signkey_ret;
277 }
278
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500279 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500280 &zero, 1);
281 if (rc) {
282 cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
283 goto smb3signkey_ret;
284 }
285
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500286 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600287 context.iov_base, context.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500288 if (rc) {
289 cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
290 goto smb3signkey_ret;
291 }
292
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500293 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500294 L, 4);
295 if (rc) {
296 cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
297 goto smb3signkey_ret;
298 }
299
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500300 rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500301 hashptr);
302 if (rc) {
303 cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
304 goto smb3signkey_ret;
305 }
306
Steve French373512e2015-12-18 13:05:30 -0600307 memcpy(key, hashptr, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500308
309smb3signkey_ret:
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500310 return rc;
Steve French429b46f2013-06-26 23:45:05 -0500311}
312
Steve French373512e2015-12-18 13:05:30 -0600313struct derivation {
314 struct kvec label;
315 struct kvec context;
316};
317
318struct derivation_triplet {
319 struct derivation signing;
320 struct derivation encryption;
321 struct derivation decryption;
322};
323
324static int
325generate_smb3signingkey(struct cifs_ses *ses,
326 const struct derivation_triplet *ptriplet)
327{
328 int rc;
329
330 rc = generate_key(ses, ptriplet->signing.label,
331 ptriplet->signing.context, ses->smb3signingkey,
332 SMB3_SIGN_KEY_SIZE);
333 if (rc)
334 return rc;
335
336 rc = generate_key(ses, ptriplet->encryption.label,
337 ptriplet->encryption.context, ses->smb3encryptionkey,
338 SMB3_SIGN_KEY_SIZE);
339 if (rc)
340 return rc;
341
Aurélien Apteld38de3c62017-05-24 16:13:25 +0200342 rc = generate_key(ses, ptriplet->decryption.label,
343 ptriplet->decryption.context,
344 ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
345
346 if (rc)
347 return rc;
348
349#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
350 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
351 /*
352 * The session id is opaque in terms of endianness, so we can't
353 * print it as a long long. we dump it as we got it on the wire
354 */
355 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
356 &ses->Suid);
357 cifs_dbg(VFS, "Session Key %*ph\n",
358 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
359 cifs_dbg(VFS, "Signing Key %*ph\n",
360 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
361 cifs_dbg(VFS, "ServerIn Key %*ph\n",
362 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
363 cifs_dbg(VFS, "ServerOut Key %*ph\n",
364 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
365#endif
366 return rc;
Steve French373512e2015-12-18 13:05:30 -0600367}
368
369int
370generate_smb30signingkey(struct cifs_ses *ses)
371
372{
373 struct derivation_triplet triplet;
374 struct derivation *d;
375
376 d = &triplet.signing;
377 d->label.iov_base = "SMB2AESCMAC";
378 d->label.iov_len = 12;
379 d->context.iov_base = "SmbSign";
380 d->context.iov_len = 8;
381
382 d = &triplet.encryption;
383 d->label.iov_base = "SMB2AESCCM";
384 d->label.iov_len = 11;
385 d->context.iov_base = "ServerIn ";
386 d->context.iov_len = 10;
387
388 d = &triplet.decryption;
389 d->label.iov_base = "SMB2AESCCM";
390 d->label.iov_len = 11;
391 d->context.iov_base = "ServerOut";
392 d->context.iov_len = 10;
393
394 return generate_smb3signingkey(ses, &triplet);
395}
396
397int
398generate_smb311signingkey(struct cifs_ses *ses)
399
400{
401 struct derivation_triplet triplet;
402 struct derivation *d;
403
404 d = &triplet.signing;
Steve French06e22902017-09-25 20:11:58 -0500405 d->label.iov_base = "SMBSigningKey";
406 d->label.iov_len = 14;
407 d->context.iov_base = ses->preauth_sha_hash;
408 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600409
410 d = &triplet.encryption;
Steve French06e22902017-09-25 20:11:58 -0500411 d->label.iov_base = "SMBC2SCipherKey";
412 d->label.iov_len = 16;
413 d->context.iov_base = ses->preauth_sha_hash;
414 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600415
416 d = &triplet.decryption;
Steve French06e22902017-09-25 20:11:58 -0500417 d->label.iov_base = "SMBS2CCipherKey";
418 d->label.iov_len = 16;
419 d->context.iov_base = ses->preauth_sha_hash;
420 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600421
422 return generate_smb3signingkey(ses, &triplet);
423}
424
Steve French429b46f2013-06-26 23:45:05 -0500425int
Steve French38107d42012-12-08 22:08:06 -0600426smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
427{
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300428 int rc;
Steve French429b46f2013-06-26 23:45:05 -0500429 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
430 unsigned char *sigptr = smb3_signature;
431 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000432 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500433 struct cifs_ses *ses;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300434 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
435 struct smb_rqst drqst;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500436
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700437 ses = smb2_find_smb_ses(server, shdr->SessionId);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500438 if (!ses) {
439 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
440 return 0;
441 }
Steve French429b46f2013-06-26 23:45:05 -0500442
443 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700444 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500445
446 rc = crypto_shash_setkey(server->secmech.cmacaes,
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300447 ses->smb3signingkey, SMB2_CMACAES_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500448 if (rc) {
449 cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
450 return rc;
451 }
452
Steve French95dc8dd2013-07-04 10:35:21 -0500453 /*
454 * we already allocate sdesccmacaes when we init smb3 signing key,
455 * so unlike smb2 case we do not have to check here if secmech are
456 * initialized
457 */
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300458 rc = crypto_shash_init(shash);
Steve French429b46f2013-06-26 23:45:05 -0500459 if (rc) {
460 cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
461 return rc;
462 }
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100463
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300464 /*
465 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
466 * data, that is, iov[0] should not contain a rfc1002 length.
467 *
468 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
469 * __cifs_calc_signature().
470 */
471 drqst = *rqst;
472 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
473 rc = crypto_shash_update(shash, iov[0].iov_base,
474 iov[0].iov_len);
475 if (rc) {
476 cifs_dbg(VFS, "%s: Could not update with payload\n",
477 __func__);
478 return rc;
479 }
480 drqst.rq_iov++;
481 drqst.rq_nvec--;
482 }
Steve French429b46f2013-06-26 23:45:05 -0500483
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300484 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500485 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700486 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500487
488 return rc;
Steve French38107d42012-12-08 22:08:06 -0600489}
490
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700491/* must be called with server->srv_mutex held */
492static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700493smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700494{
495 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800496 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000497 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700498
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700499 if (!(shdr->Flags & SMB2_FLAGS_SIGNED) ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700500 server->tcpStatus == CifsNeedNegotiate)
501 return rc;
502
503 if (!server->session_estab) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700504 strncpy(shdr->Signature, "BSRSPYL", 8);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700505 return rc;
506 }
507
Steve French38107d42012-12-08 22:08:06 -0600508 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700509
510 return rc;
511}
512
513int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700514smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700515{
516 unsigned int rc;
517 char server_response_sig[16];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800518 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000519 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700520
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700521 if ((shdr->Command == SMB2_NEGOTIATE) ||
522 (shdr->Command == SMB2_SESSION_SETUP) ||
523 (shdr->Command == SMB2_OPLOCK_BREAK) ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700524 (!server->session_estab))
525 return 0;
526
527 /*
528 * BB what if signatures are supposed to be on for session but
529 * server does not send one? BB
530 */
531
532 /* Do not need to verify session setups with signature "BSRSPYL " */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700533 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500534 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700535 shdr->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700536
537 /*
538 * Save off the origiginal signature so we can modify the smb and check
539 * our calculated signature against what the server sent.
540 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700541 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700542
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700543 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700544
545 mutex_lock(&server->srv_mutex);
Steve French38107d42012-12-08 22:08:06 -0600546 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700547 mutex_unlock(&server->srv_mutex);
548
549 if (rc)
550 return rc;
551
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700552 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700553 return -EACCES;
554 else
555 return 0;
556}
557
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400558/*
559 * Set message id for the request. Should be called after wait_for_free_request
560 * and when srv_mutex is held.
561 */
562static inline void
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700563smb2_seq_num_into_buf(struct TCP_Server_Info *server,
564 struct smb2_sync_hdr *shdr)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400565{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700566 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400567
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700568 shdr->MessageId = get_next_mid64(server);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400569 /* skip message numbers according to CreditCharge field */
570 for (i = 1; i < num; i++)
571 get_next_mid(server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400572}
573
574static struct mid_q_entry *
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700575smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400576 struct TCP_Server_Info *server)
577{
578 struct mid_q_entry *temp;
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800579 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400580
581 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500582 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400583 return NULL;
584 }
585
586 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +1000587 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +0200588 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +1000589 temp->mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800590 temp->credits = credits > 0 ? credits : 1;
NeilBrowna6f74e82017-04-10 12:08:53 +1000591 temp->pid = current->pid;
592 temp->command = shdr->Command; /* Always LE */
593 temp->when_alloc = jiffies;
594 temp->server = server;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400595
NeilBrowna6f74e82017-04-10 12:08:53 +1000596 /*
597 * The default is for the mid to be synchronous, so the
598 * default callback just wakes up the current task.
599 */
600 temp->callback = cifs_wake_up_task;
601 temp->callback_data = current;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400602
603 atomic_inc(&midCount);
604 temp->mid_state = MID_REQUEST_ALLOCATED;
Steve French53a3e0d2019-02-26 21:26:20 -0600605 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
606 le16_to_cpu(shdr->Command), temp->mid);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400607 return temp;
608}
609
610static int
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700611smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_sync_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400612 struct mid_q_entry **mid)
613{
614 if (ses->server->tcpStatus == CifsExiting)
615 return -ENOENT;
616
617 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500618 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400619 return -EAGAIN;
620 }
621
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800622 if (ses->server->tcpStatus == CifsNeedNegotiate &&
623 shdr->Command != SMB2_NEGOTIATE)
624 return -EAGAIN;
625
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500626 if (ses->status == CifsNew) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700627 if ((shdr->Command != SMB2_SESSION_SETUP) &&
628 (shdr->Command != SMB2_NEGOTIATE))
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400629 return -EAGAIN;
630 /* else ok - we are setting up session */
631 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500632
633 if (ses->status == CifsExiting) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700634 if (shdr->Command != SMB2_LOGOFF)
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500635 return -EAGAIN;
636 /* else ok - we are shutting down the session */
637 }
638
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700639 *mid = smb2_mid_entry_alloc(shdr, ses->server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400640 if (*mid == NULL)
641 return -ENOMEM;
642 spin_lock(&GlobalMid_Lock);
643 list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
644 spin_unlock(&GlobalMid_Lock);
Steve French53a3e0d2019-02-26 21:26:20 -0600645
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400646 return 0;
647}
648
649int
650smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
651 bool log_error)
652{
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000653 unsigned int len = mid->resp_buf_size;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000654 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800655 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000656 .rq_nvec = 1 };
Jeff Layton0b688cf2012-09-18 16:20:34 -0700657
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800658 iov[0].iov_base = (char *)mid->resp_buf;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000659 iov[0].iov_len = len;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400660
661 dump_smb(mid->resp_buf, min_t(u32, 80, len));
662 /* convert the length into a more usable form */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800663 if (len > 24 && server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700664 int rc;
665
Jeff Layton0b688cf2012-09-18 16:20:34 -0700666 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700667 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500668 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
669 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700670 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400671
672 return map_smb2_to_linux_error(mid->resp_buf, log_error);
673}
674
Jeff Laytonfec344e2012-09-18 16:20:35 -0700675struct mid_q_entry *
676smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400677{
678 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800679 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000680 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400681 struct mid_q_entry *mid;
682
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700683 smb2_seq_num_into_buf(ses->server, shdr);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400684
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700685 rc = smb2_get_mid_entry(ses, shdr, &mid);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800686 if (rc) {
687 revert_current_mid_from_hdr(ses->server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700688 return ERR_PTR(rc);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800689 }
690
Jeff Laytonfec344e2012-09-18 16:20:35 -0700691 rc = smb2_sign_rqst(rqst, ses->server);
692 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800693 revert_current_mid_from_hdr(ses->server, shdr);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700694 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700695 return ERR_PTR(rc);
696 }
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800697
Jeff Laytonfec344e2012-09-18 16:20:35 -0700698 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400699}
700
Jeff Laytonfec344e2012-09-18 16:20:35 -0700701struct mid_q_entry *
702smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400703{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700704 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800705 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000706 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400707 struct mid_q_entry *mid;
708
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800709 if (server->tcpStatus == CifsNeedNegotiate &&
710 shdr->Command != SMB2_NEGOTIATE)
711 return ERR_PTR(-EAGAIN);
712
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700713 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400714
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700715 mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800716 if (mid == NULL) {
717 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700718 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800719 }
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400720
Jeff Laytonfec344e2012-09-18 16:20:35 -0700721 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400722 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800723 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400724 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700725 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700726 }
727
Jeff Laytonfec344e2012-09-18 16:20:35 -0700728 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400729}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700730
731int
732smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
733{
734 struct crypto_aead *tfm;
735
736 if (!server->secmech.ccmaesencrypt) {
737 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
738 if (IS_ERR(tfm)) {
739 cifs_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
740 __func__);
741 return PTR_ERR(tfm);
742 }
743 server->secmech.ccmaesencrypt = tfm;
744 }
745
746 if (!server->secmech.ccmaesdecrypt) {
747 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
748 if (IS_ERR(tfm)) {
749 crypto_free_aead(server->secmech.ccmaesencrypt);
750 server->secmech.ccmaesencrypt = NULL;
751 cifs_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
752 __func__);
753 return PTR_ERR(tfm);
754 }
755 server->secmech.ccmaesdecrypt = tfm;
756 }
757
758 return 0;
759}