blob: b02242eacb55d68c3109ff3fa5151677eb5529c7 [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) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000179 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500180 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) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000188 cifs_server_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) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000195 cifs_server_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) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000202 cifs_server_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) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000218 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300219 __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;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000242 struct TCP_Server_Info *server = ses->server;
Steve French429b46f2013-06-26 23:45:05 -0500243
244 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
Steve French373512e2015-12-18 13:05:30 -0600245 memset(key, 0x0, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500246
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000247 rc = smb3_crypto_shash_allocate(server);
Steve French95dc8dd2013-07-04 10:35:21 -0500248 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000249 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Steve French95dc8dd2013-07-04 10:35:21 -0500250 goto smb3signkey_ret;
251 }
252
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000253 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500254 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500255 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000256 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500257 goto smb3signkey_ret;
258 }
259
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000260 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
Steve French429b46f2013-06-26 23:45:05 -0500261 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000262 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500263 goto smb3signkey_ret;
264 }
265
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000266 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500267 i, 4);
268 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000269 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500270 goto smb3signkey_ret;
271 }
272
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000273 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600274 label.iov_base, label.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500275 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000276 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500277 goto smb3signkey_ret;
278 }
279
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000280 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500281 &zero, 1);
282 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000283 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500284 goto smb3signkey_ret;
285 }
286
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000287 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600288 context.iov_base, context.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500289 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000290 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500291 goto smb3signkey_ret;
292 }
293
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000294 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500295 L, 4);
296 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000297 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500298 goto smb3signkey_ret;
299 }
300
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000301 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500302 hashptr);
303 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000304 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500305 goto smb3signkey_ret;
306 }
307
Steve French373512e2015-12-18 13:05:30 -0600308 memcpy(key, hashptr, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500309
310smb3signkey_ret:
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500311 return rc;
Steve French429b46f2013-06-26 23:45:05 -0500312}
313
Steve French373512e2015-12-18 13:05:30 -0600314struct derivation {
315 struct kvec label;
316 struct kvec context;
317};
318
319struct derivation_triplet {
320 struct derivation signing;
321 struct derivation encryption;
322 struct derivation decryption;
323};
324
325static int
326generate_smb3signingkey(struct cifs_ses *ses,
327 const struct derivation_triplet *ptriplet)
328{
329 int rc;
330
331 rc = generate_key(ses, ptriplet->signing.label,
332 ptriplet->signing.context, ses->smb3signingkey,
333 SMB3_SIGN_KEY_SIZE);
334 if (rc)
335 return rc;
336
337 rc = generate_key(ses, ptriplet->encryption.label,
338 ptriplet->encryption.context, ses->smb3encryptionkey,
339 SMB3_SIGN_KEY_SIZE);
340 if (rc)
341 return rc;
342
Aurélien Apteld38de3c62017-05-24 16:13:25 +0200343 rc = generate_key(ses, ptriplet->decryption.label,
344 ptriplet->decryption.context,
345 ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
346
347 if (rc)
348 return rc;
349
350#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
351 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
352 /*
353 * The session id is opaque in terms of endianness, so we can't
354 * print it as a long long. we dump it as we got it on the wire
355 */
356 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
357 &ses->Suid);
358 cifs_dbg(VFS, "Session Key %*ph\n",
359 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
360 cifs_dbg(VFS, "Signing Key %*ph\n",
361 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
362 cifs_dbg(VFS, "ServerIn Key %*ph\n",
363 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
364 cifs_dbg(VFS, "ServerOut Key %*ph\n",
365 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
366#endif
367 return rc;
Steve French373512e2015-12-18 13:05:30 -0600368}
369
370int
371generate_smb30signingkey(struct cifs_ses *ses)
372
373{
374 struct derivation_triplet triplet;
375 struct derivation *d;
376
377 d = &triplet.signing;
378 d->label.iov_base = "SMB2AESCMAC";
379 d->label.iov_len = 12;
380 d->context.iov_base = "SmbSign";
381 d->context.iov_len = 8;
382
383 d = &triplet.encryption;
384 d->label.iov_base = "SMB2AESCCM";
385 d->label.iov_len = 11;
386 d->context.iov_base = "ServerIn ";
387 d->context.iov_len = 10;
388
389 d = &triplet.decryption;
390 d->label.iov_base = "SMB2AESCCM";
391 d->label.iov_len = 11;
392 d->context.iov_base = "ServerOut";
393 d->context.iov_len = 10;
394
395 return generate_smb3signingkey(ses, &triplet);
396}
397
398int
399generate_smb311signingkey(struct cifs_ses *ses)
400
401{
402 struct derivation_triplet triplet;
403 struct derivation *d;
404
405 d = &triplet.signing;
Steve French06e22902017-09-25 20:11:58 -0500406 d->label.iov_base = "SMBSigningKey";
407 d->label.iov_len = 14;
408 d->context.iov_base = ses->preauth_sha_hash;
409 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600410
411 d = &triplet.encryption;
Steve French06e22902017-09-25 20:11:58 -0500412 d->label.iov_base = "SMBC2SCipherKey";
413 d->label.iov_len = 16;
414 d->context.iov_base = ses->preauth_sha_hash;
415 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600416
417 d = &triplet.decryption;
Steve French06e22902017-09-25 20:11:58 -0500418 d->label.iov_base = "SMBS2CCipherKey";
419 d->label.iov_len = 16;
420 d->context.iov_base = ses->preauth_sha_hash;
421 d->context.iov_len = 64;
Steve French373512e2015-12-18 13:05:30 -0600422
423 return generate_smb3signingkey(ses, &triplet);
424}
425
Steve French429b46f2013-06-26 23:45:05 -0500426int
Steve French38107d42012-12-08 22:08:06 -0600427smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
428{
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300429 int rc;
Steve French429b46f2013-06-26 23:45:05 -0500430 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
431 unsigned char *sigptr = smb3_signature;
432 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000433 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500434 struct cifs_ses *ses;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300435 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
436 struct smb_rqst drqst;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500437
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700438 ses = smb2_find_smb_ses(server, shdr->SessionId);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500439 if (!ses) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000440 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500441 return 0;
442 }
Steve French429b46f2013-06-26 23:45:05 -0500443
444 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700445 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500446
447 rc = crypto_shash_setkey(server->secmech.cmacaes,
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300448 ses->smb3signingkey, SMB2_CMACAES_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500449 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000450 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500451 return rc;
452 }
453
Steve French95dc8dd2013-07-04 10:35:21 -0500454 /*
455 * we already allocate sdesccmacaes when we init smb3 signing key,
456 * so unlike smb2 case we do not have to check here if secmech are
457 * initialized
458 */
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300459 rc = crypto_shash_init(shash);
Steve French429b46f2013-06-26 23:45:05 -0500460 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000461 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500462 return rc;
463 }
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100464
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300465 /*
466 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
467 * data, that is, iov[0] should not contain a rfc1002 length.
468 *
469 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
470 * __cifs_calc_signature().
471 */
472 drqst = *rqst;
473 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
474 rc = crypto_shash_update(shash, iov[0].iov_base,
475 iov[0].iov_len);
476 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000477 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300478 __func__);
479 return rc;
480 }
481 drqst.rq_iov++;
482 drqst.rq_nvec--;
483 }
Steve French429b46f2013-06-26 23:45:05 -0500484
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300485 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500486 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700487 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500488
489 return rc;
Steve French38107d42012-12-08 22:08:06 -0600490}
491
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700492/* must be called with server->srv_mutex held */
493static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700494smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700495{
496 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800497 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000498 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700499
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700500 if (!(shdr->Flags & SMB2_FLAGS_SIGNED) ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700501 server->tcpStatus == CifsNeedNegotiate)
502 return rc;
503
504 if (!server->session_estab) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700505 strncpy(shdr->Signature, "BSRSPYL", 8);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700506 return rc;
507 }
508
Steve French38107d42012-12-08 22:08:06 -0600509 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700510
511 return rc;
512}
513
514int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700515smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700516{
517 unsigned int rc;
518 char server_response_sig[16];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800519 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000520 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700521
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700522 if ((shdr->Command == SMB2_NEGOTIATE) ||
523 (shdr->Command == SMB2_SESSION_SETUP) ||
524 (shdr->Command == SMB2_OPLOCK_BREAK) ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700525 (!server->session_estab))
526 return 0;
527
528 /*
529 * BB what if signatures are supposed to be on for session but
530 * server does not send one? BB
531 */
532
533 /* Do not need to verify session setups with signature "BSRSPYL " */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700534 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500535 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700536 shdr->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700537
538 /*
539 * Save off the origiginal signature so we can modify the smb and check
540 * our calculated signature against what the server sent.
541 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700542 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700543
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700544 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700545
546 mutex_lock(&server->srv_mutex);
Steve French38107d42012-12-08 22:08:06 -0600547 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700548 mutex_unlock(&server->srv_mutex);
549
550 if (rc)
551 return rc;
552
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700553 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700554 return -EACCES;
555 else
556 return 0;
557}
558
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400559/*
560 * Set message id for the request. Should be called after wait_for_free_request
561 * and when srv_mutex is held.
562 */
563static inline void
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700564smb2_seq_num_into_buf(struct TCP_Server_Info *server,
565 struct smb2_sync_hdr *shdr)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400566{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700567 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400568
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700569 shdr->MessageId = get_next_mid64(server);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400570 /* skip message numbers according to CreditCharge field */
571 for (i = 1; i < num; i++)
572 get_next_mid(server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400573}
574
575static struct mid_q_entry *
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700576smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400577 struct TCP_Server_Info *server)
578{
579 struct mid_q_entry *temp;
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800580 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400581
582 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500583 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400584 return NULL;
585 }
586
587 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +1000588 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +0200589 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +1000590 temp->mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800591 temp->credits = credits > 0 ? credits : 1;
NeilBrowna6f74e82017-04-10 12:08:53 +1000592 temp->pid = current->pid;
593 temp->command = shdr->Command; /* Always LE */
594 temp->when_alloc = jiffies;
595 temp->server = server;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400596
NeilBrowna6f74e82017-04-10 12:08:53 +1000597 /*
598 * The default is for the mid to be synchronous, so the
599 * default callback just wakes up the current task.
600 */
601 temp->callback = cifs_wake_up_task;
602 temp->callback_data = current;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400603
604 atomic_inc(&midCount);
605 temp->mid_state = MID_REQUEST_ALLOCATED;
Steve French53a3e0d2019-02-26 21:26:20 -0600606 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
607 le16_to_cpu(shdr->Command), temp->mid);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400608 return temp;
609}
610
611static int
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700612smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_sync_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400613 struct mid_q_entry **mid)
614{
615 if (ses->server->tcpStatus == CifsExiting)
616 return -ENOENT;
617
618 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500619 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400620 return -EAGAIN;
621 }
622
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800623 if (ses->server->tcpStatus == CifsNeedNegotiate &&
624 shdr->Command != SMB2_NEGOTIATE)
625 return -EAGAIN;
626
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500627 if (ses->status == CifsNew) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700628 if ((shdr->Command != SMB2_SESSION_SETUP) &&
629 (shdr->Command != SMB2_NEGOTIATE))
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400630 return -EAGAIN;
631 /* else ok - we are setting up session */
632 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500633
634 if (ses->status == CifsExiting) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700635 if (shdr->Command != SMB2_LOGOFF)
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500636 return -EAGAIN;
637 /* else ok - we are shutting down the session */
638 }
639
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700640 *mid = smb2_mid_entry_alloc(shdr, ses->server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400641 if (*mid == NULL)
642 return -ENOMEM;
643 spin_lock(&GlobalMid_Lock);
644 list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
645 spin_unlock(&GlobalMid_Lock);
Steve French53a3e0d2019-02-26 21:26:20 -0600646
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400647 return 0;
648}
649
650int
651smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
652 bool log_error)
653{
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000654 unsigned int len = mid->resp_buf_size;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000655 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800656 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000657 .rq_nvec = 1 };
Jeff Layton0b688cf2012-09-18 16:20:34 -0700658
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800659 iov[0].iov_base = (char *)mid->resp_buf;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000660 iov[0].iov_len = len;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400661
662 dump_smb(mid->resp_buf, min_t(u32, 80, len));
663 /* convert the length into a more usable form */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800664 if (len > 24 && server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700665 int rc;
666
Jeff Layton0b688cf2012-09-18 16:20:34 -0700667 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700668 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000669 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500670 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700671 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400672
673 return map_smb2_to_linux_error(mid->resp_buf, log_error);
674}
675
Jeff Laytonfec344e2012-09-18 16:20:35 -0700676struct mid_q_entry *
677smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400678{
679 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800680 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000681 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400682 struct mid_q_entry *mid;
683
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700684 smb2_seq_num_into_buf(ses->server, shdr);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400685
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700686 rc = smb2_get_mid_entry(ses, shdr, &mid);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800687 if (rc) {
688 revert_current_mid_from_hdr(ses->server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700689 return ERR_PTR(rc);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800690 }
691
Jeff Laytonfec344e2012-09-18 16:20:35 -0700692 rc = smb2_sign_rqst(rqst, ses->server);
693 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800694 revert_current_mid_from_hdr(ses->server, shdr);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700695 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700696 return ERR_PTR(rc);
697 }
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800698
Jeff Laytonfec344e2012-09-18 16:20:35 -0700699 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400700}
701
Jeff Laytonfec344e2012-09-18 16:20:35 -0700702struct mid_q_entry *
703smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400704{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700705 int rc;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800706 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +1000707 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400708 struct mid_q_entry *mid;
709
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800710 if (server->tcpStatus == CifsNeedNegotiate &&
711 shdr->Command != SMB2_NEGOTIATE)
712 return ERR_PTR(-EAGAIN);
713
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700714 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400715
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700716 mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800717 if (mid == NULL) {
718 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700719 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800720 }
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400721
Jeff Laytonfec344e2012-09-18 16:20:35 -0700722 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400723 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800724 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400725 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700726 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700727 }
728
Jeff Laytonfec344e2012-09-18 16:20:35 -0700729 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400730}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700731
732int
733smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
734{
735 struct crypto_aead *tfm;
736
737 if (!server->secmech.ccmaesencrypt) {
Steve French2b2f7542019-06-07 15:16:10 -0500738 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
739 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
740 else
741 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700742 if (IS_ERR(tfm)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000743 cifs_server_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700744 __func__);
745 return PTR_ERR(tfm);
746 }
747 server->secmech.ccmaesencrypt = tfm;
748 }
749
750 if (!server->secmech.ccmaesdecrypt) {
Steve French2b2f7542019-06-07 15:16:10 -0500751 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
752 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
753 else
754 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700755 if (IS_ERR(tfm)) {
756 crypto_free_aead(server->secmech.ccmaesencrypt);
757 server->secmech.ccmaesencrypt = NULL;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000758 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700759 __func__);
760 return PTR_ERR(tfm);
761 }
762 server->secmech.ccmaesdecrypt = tfm;
763 }
764
765 return 0;
766}