• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
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  */
11 
12 #include <linux/fs.h>
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/net.h>
16 #include <linux/delay.h>
17 #include <linux/uaccess.h>
18 #include <asm/processor.h>
19 #include <linux/mempool.h>
20 #include <linux/highmem.h>
21 #include <crypto/aead.h>
22 #include "smb2pdu.h"
23 #include "cifsglob.h"
24 #include "cifsproto.h"
25 #include "smb2proto.h"
26 #include "cifs_debug.h"
27 #include "smb2status.h"
28 #include "smb2glob.h"
29 
30 static int
smb3_crypto_shash_allocate(struct TCP_Server_Info * server)31 smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
32 {
33 	struct cifs_secmech *p = &server->secmech;
34 	int rc;
35 
36 	rc = cifs_alloc_hash("hmac(sha256)",
37 			     &p->hmacsha256,
38 			     &p->sdeschmacsha256);
39 	if (rc)
40 		goto err;
41 
42 	rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
43 	if (rc)
44 		goto err;
45 
46 	return 0;
47 err:
48 	cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
49 	return rc;
50 }
51 
52 int
smb311_crypto_shash_allocate(struct TCP_Server_Info * server)53 smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
54 {
55 	struct cifs_secmech *p = &server->secmech;
56 	int rc = 0;
57 
58 	rc = cifs_alloc_hash("hmac(sha256)",
59 			     &p->hmacsha256,
60 			     &p->sdeschmacsha256);
61 	if (rc)
62 		return rc;
63 
64 	rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
65 	if (rc)
66 		goto err;
67 
68 	rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512);
69 	if (rc)
70 		goto err;
71 
72 	return 0;
73 
74 err:
75 	cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
76 	cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
77 	return rc;
78 }
79 
80 
81 static
smb2_get_sign_key(__u64 ses_id,struct TCP_Server_Info * server,u8 * key)82 int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
83 {
84 	struct cifs_chan *chan;
85 	struct cifs_ses *ses = NULL;
86 	struct TCP_Server_Info *it = NULL;
87 	int i;
88 	int rc = 0;
89 
90 	spin_lock(&cifs_tcp_ses_lock);
91 
92 	list_for_each_entry(it, &cifs_tcp_ses_list, tcp_ses_list) {
93 		list_for_each_entry(ses, &it->smb_ses_list, smb_ses_list) {
94 			if (ses->Suid == ses_id)
95 				goto found;
96 		}
97 	}
98 	cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
99 			__func__, ses_id);
100 	rc = -ENOENT;
101 	goto out;
102 
103 found:
104 	if (ses->binding) {
105 		/*
106 		 * If we are in the process of binding a new channel
107 		 * to an existing session, use the master connection
108 		 * session key
109 		 */
110 		memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
111 		goto out;
112 	}
113 
114 	/*
115 	 * Otherwise, use the channel key.
116 	 */
117 
118 	for (i = 0; i < ses->chan_count; i++) {
119 		chan = ses->chans + i;
120 		if (chan->server == server) {
121 			memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
122 			goto out;
123 		}
124 	}
125 
126 	cifs_dbg(VFS,
127 		 "%s: Could not find channel signing key for session 0x%llx\n",
128 		 __func__, ses_id);
129 	rc = -ENOENT;
130 
131 out:
132 	spin_unlock(&cifs_tcp_ses_lock);
133 	return rc;
134 }
135 
136 static struct cifs_ses *
smb2_find_smb_ses_unlocked(struct TCP_Server_Info * server,__u64 ses_id)137 smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
138 {
139 	struct cifs_ses *ses;
140 
141 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
142 		if (ses->Suid != ses_id)
143 			continue;
144 		++ses->ses_count;
145 		return ses;
146 	}
147 
148 	return NULL;
149 }
150 
151 struct cifs_ses *
smb2_find_smb_ses(struct TCP_Server_Info * server,__u64 ses_id)152 smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
153 {
154 	struct cifs_ses *ses;
155 
156 	spin_lock(&cifs_tcp_ses_lock);
157 	ses = smb2_find_smb_ses_unlocked(server, ses_id);
158 	spin_unlock(&cifs_tcp_ses_lock);
159 
160 	return ses;
161 }
162 
163 static struct cifs_tcon *
smb2_find_smb_sess_tcon_unlocked(struct cifs_ses * ses,__u32 tid)164 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32  tid)
165 {
166 	struct cifs_tcon *tcon;
167 
168 	list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
169 		if (tcon->tid != tid)
170 			continue;
171 		++tcon->tc_count;
172 		return tcon;
173 	}
174 
175 	return NULL;
176 }
177 
178 /*
179  * Obtain tcon corresponding to the tid in the given
180  * cifs_ses
181  */
182 
183 struct cifs_tcon *
smb2_find_smb_tcon(struct TCP_Server_Info * server,__u64 ses_id,__u32 tid)184 smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32  tid)
185 {
186 	struct cifs_ses *ses;
187 	struct cifs_tcon *tcon;
188 
189 	spin_lock(&cifs_tcp_ses_lock);
190 	ses = smb2_find_smb_ses_unlocked(server, ses_id);
191 	if (!ses) {
192 		spin_unlock(&cifs_tcp_ses_lock);
193 		return NULL;
194 	}
195 	tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
196 	if (!tcon) {
197 		cifs_put_smb_ses(ses);
198 		spin_unlock(&cifs_tcp_ses_lock);
199 		return NULL;
200 	}
201 	spin_unlock(&cifs_tcp_ses_lock);
202 	/* tcon already has a ref to ses, so we don't need ses anymore */
203 	cifs_put_smb_ses(ses);
204 
205 	return tcon;
206 }
207 
208 int
smb2_calc_signature(struct smb_rqst * rqst,struct TCP_Server_Info * server,bool allocate_crypto)209 smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
210 			bool allocate_crypto)
211 {
212 	int rc;
213 	unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
214 	unsigned char *sigptr = smb2_signature;
215 	struct kvec *iov = rqst->rq_iov;
216 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
217 	struct cifs_ses *ses;
218 	struct shash_desc *shash;
219 	struct crypto_shash *hash;
220 	struct sdesc *sdesc = NULL;
221 	struct smb_rqst drqst;
222 
223 	ses = smb2_find_smb_ses(server, shdr->SessionId);
224 	if (unlikely(!ses)) {
225 		cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
226 		return -ENOENT;
227 	}
228 
229 	memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
230 	memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
231 
232 	if (allocate_crypto) {
233 		rc = cifs_alloc_hash("hmac(sha256)", &hash, &sdesc);
234 		if (rc) {
235 			cifs_server_dbg(VFS,
236 					"%s: sha256 alloc failed\n", __func__);
237 			goto out;
238 		}
239 		shash = &sdesc->shash;
240 	} else {
241 		hash = server->secmech.hmacsha256;
242 		shash = &server->secmech.sdeschmacsha256->shash;
243 	}
244 
245 	rc = crypto_shash_setkey(hash, ses->auth_key.response,
246 			SMB2_NTLMV2_SESSKEY_SIZE);
247 	if (rc) {
248 		cifs_server_dbg(VFS,
249 				"%s: Could not update with response\n",
250 				__func__);
251 		goto out;
252 	}
253 
254 	rc = crypto_shash_init(shash);
255 	if (rc) {
256 		cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
257 		goto out;
258 	}
259 
260 	/*
261 	 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
262 	 * data, that is, iov[0] should not contain a rfc1002 length.
263 	 *
264 	 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
265 	 * __cifs_calc_signature().
266 	 */
267 	drqst = *rqst;
268 	if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
269 		rc = crypto_shash_update(shash, iov[0].iov_base,
270 					 iov[0].iov_len);
271 		if (rc) {
272 			cifs_server_dbg(VFS,
273 					"%s: Could not update with payload\n",
274 					__func__);
275 			goto out;
276 		}
277 		drqst.rq_iov++;
278 		drqst.rq_nvec--;
279 	}
280 
281 	rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
282 	if (!rc)
283 		memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
284 
285 out:
286 	if (allocate_crypto)
287 		cifs_free_hash(&hash, &sdesc);
288 	if (ses)
289 		cifs_put_smb_ses(ses);
290 	return rc;
291 }
292 
generate_key(struct cifs_ses * ses,struct kvec label,struct kvec context,__u8 * key,unsigned int key_size)293 static int generate_key(struct cifs_ses *ses, struct kvec label,
294 			struct kvec context, __u8 *key, unsigned int key_size)
295 {
296 	unsigned char zero = 0x0;
297 	__u8 i[4] = {0, 0, 0, 1};
298 	__u8 L128[4] = {0, 0, 0, 128};
299 	__u8 L256[4] = {0, 0, 1, 0};
300 	int rc = 0;
301 	unsigned char prfhash[SMB2_HMACSHA256_SIZE];
302 	unsigned char *hashptr = prfhash;
303 	struct TCP_Server_Info *server = ses->server;
304 
305 	memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
306 	memset(key, 0x0, key_size);
307 
308 	rc = smb3_crypto_shash_allocate(server);
309 	if (rc) {
310 		cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
311 		goto smb3signkey_ret;
312 	}
313 
314 	rc = crypto_shash_setkey(server->secmech.hmacsha256,
315 		ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
316 	if (rc) {
317 		cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
318 		goto smb3signkey_ret;
319 	}
320 
321 	rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
322 	if (rc) {
323 		cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
324 		goto smb3signkey_ret;
325 	}
326 
327 	rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
328 				i, 4);
329 	if (rc) {
330 		cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
331 		goto smb3signkey_ret;
332 	}
333 
334 	rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
335 				label.iov_base, label.iov_len);
336 	if (rc) {
337 		cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
338 		goto smb3signkey_ret;
339 	}
340 
341 	rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
342 				&zero, 1);
343 	if (rc) {
344 		cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
345 		goto smb3signkey_ret;
346 	}
347 
348 	rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
349 				context.iov_base, context.iov_len);
350 	if (rc) {
351 		cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
352 		goto smb3signkey_ret;
353 	}
354 
355 	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
356 		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) {
357 		rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
358 				L256, 4);
359 	} else {
360 		rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
361 				L128, 4);
362 	}
363 	if (rc) {
364 		cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
365 		goto smb3signkey_ret;
366 	}
367 
368 	rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
369 				hashptr);
370 	if (rc) {
371 		cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
372 		goto smb3signkey_ret;
373 	}
374 
375 	memcpy(key, hashptr, key_size);
376 
377 smb3signkey_ret:
378 	return rc;
379 }
380 
381 struct derivation {
382 	struct kvec label;
383 	struct kvec context;
384 };
385 
386 struct derivation_triplet {
387 	struct derivation signing;
388 	struct derivation encryption;
389 	struct derivation decryption;
390 };
391 
392 static int
generate_smb3signingkey(struct cifs_ses * ses,const struct derivation_triplet * ptriplet)393 generate_smb3signingkey(struct cifs_ses *ses,
394 			const struct derivation_triplet *ptriplet)
395 {
396 	int rc;
397 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
398 	struct TCP_Server_Info *server = ses->server;
399 #endif
400 
401 	/*
402 	 * All channels use the same encryption/decryption keys but
403 	 * they have their own signing key.
404 	 *
405 	 * When we generate the keys, check if it is for a new channel
406 	 * (binding) in which case we only need to generate a signing
407 	 * key and store it in the channel as to not overwrite the
408 	 * master connection signing key stored in the session
409 	 */
410 
411 	if (ses->binding) {
412 		rc = generate_key(ses, ptriplet->signing.label,
413 				  ptriplet->signing.context,
414 				  cifs_ses_binding_channel(ses)->signkey,
415 				  SMB3_SIGN_KEY_SIZE);
416 		if (rc)
417 			return rc;
418 	} else {
419 		rc = generate_key(ses, ptriplet->signing.label,
420 				  ptriplet->signing.context,
421 				  ses->smb3signingkey,
422 				  SMB3_SIGN_KEY_SIZE);
423 		if (rc)
424 			return rc;
425 
426 		memcpy(ses->chans[0].signkey, ses->smb3signingkey,
427 		       SMB3_SIGN_KEY_SIZE);
428 
429 		rc = generate_key(ses, ptriplet->encryption.label,
430 				  ptriplet->encryption.context,
431 				  ses->smb3encryptionkey,
432 				  SMB3_ENC_DEC_KEY_SIZE);
433 		if (rc)
434 			return rc;
435 		rc = generate_key(ses, ptriplet->decryption.label,
436 				  ptriplet->decryption.context,
437 				  ses->smb3decryptionkey,
438 				  SMB3_ENC_DEC_KEY_SIZE);
439 		if (rc)
440 			return rc;
441 	}
442 
443 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
444 	cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
445 	/*
446 	 * The session id is opaque in terms of endianness, so we can't
447 	 * print it as a long long. we dump it as we got it on the wire
448 	 */
449 	cifs_dbg(VFS, "Session Id    %*ph\n", (int)sizeof(ses->Suid),
450 			&ses->Suid);
451 	cifs_dbg(VFS, "Cipher type   %d\n", server->cipher_type);
452 	cifs_dbg(VFS, "Session Key   %*ph\n",
453 		 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
454 	cifs_dbg(VFS, "Signing Key   %*ph\n",
455 		 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
456 	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
457 		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) {
458 		cifs_dbg(VFS, "ServerIn Key  %*ph\n",
459 				SMB3_GCM256_CRYPTKEY_SIZE, ses->smb3encryptionkey);
460 		cifs_dbg(VFS, "ServerOut Key %*ph\n",
461 				SMB3_GCM256_CRYPTKEY_SIZE, ses->smb3decryptionkey);
462 	} else {
463 		cifs_dbg(VFS, "ServerIn Key  %*ph\n",
464 				SMB3_GCM128_CRYPTKEY_SIZE, ses->smb3encryptionkey);
465 		cifs_dbg(VFS, "ServerOut Key %*ph\n",
466 				SMB3_GCM128_CRYPTKEY_SIZE, ses->smb3decryptionkey);
467 	}
468 #endif
469 	return rc;
470 }
471 
472 int
generate_smb30signingkey(struct cifs_ses * ses)473 generate_smb30signingkey(struct cifs_ses *ses)
474 
475 {
476 	struct derivation_triplet triplet;
477 	struct derivation *d;
478 
479 	d = &triplet.signing;
480 	d->label.iov_base = "SMB2AESCMAC";
481 	d->label.iov_len = 12;
482 	d->context.iov_base = "SmbSign";
483 	d->context.iov_len = 8;
484 
485 	d = &triplet.encryption;
486 	d->label.iov_base = "SMB2AESCCM";
487 	d->label.iov_len = 11;
488 	d->context.iov_base = "ServerIn ";
489 	d->context.iov_len = 10;
490 
491 	d = &triplet.decryption;
492 	d->label.iov_base = "SMB2AESCCM";
493 	d->label.iov_len = 11;
494 	d->context.iov_base = "ServerOut";
495 	d->context.iov_len = 10;
496 
497 	return generate_smb3signingkey(ses, &triplet);
498 }
499 
500 int
generate_smb311signingkey(struct cifs_ses * ses)501 generate_smb311signingkey(struct cifs_ses *ses)
502 
503 {
504 	struct derivation_triplet triplet;
505 	struct derivation *d;
506 
507 	d = &triplet.signing;
508 	d->label.iov_base = "SMBSigningKey";
509 	d->label.iov_len = 14;
510 	d->context.iov_base = ses->preauth_sha_hash;
511 	d->context.iov_len = 64;
512 
513 	d = &triplet.encryption;
514 	d->label.iov_base = "SMBC2SCipherKey";
515 	d->label.iov_len = 16;
516 	d->context.iov_base = ses->preauth_sha_hash;
517 	d->context.iov_len = 64;
518 
519 	d = &triplet.decryption;
520 	d->label.iov_base = "SMBS2CCipherKey";
521 	d->label.iov_len = 16;
522 	d->context.iov_base = ses->preauth_sha_hash;
523 	d->context.iov_len = 64;
524 
525 	return generate_smb3signingkey(ses, &triplet);
526 }
527 
528 int
smb3_calc_signature(struct smb_rqst * rqst,struct TCP_Server_Info * server,bool allocate_crypto)529 smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
530 			bool allocate_crypto)
531 {
532 	int rc;
533 	unsigned char smb3_signature[SMB2_CMACAES_SIZE];
534 	unsigned char *sigptr = smb3_signature;
535 	struct kvec *iov = rqst->rq_iov;
536 	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
537 	struct shash_desc *shash;
538 	struct crypto_shash *hash;
539 	struct sdesc *sdesc = NULL;
540 	struct smb_rqst drqst;
541 	u8 key[SMB3_SIGN_KEY_SIZE];
542 
543 	rc = smb2_get_sign_key(shdr->SessionId, server, key);
544 	if (unlikely(rc)) {
545 		cifs_server_dbg(VFS, "%s: Could not get signing key\n", __func__);
546 		return rc;
547 	}
548 
549 	if (allocate_crypto) {
550 		rc = cifs_alloc_hash("cmac(aes)", &hash, &sdesc);
551 		if (rc)
552 			return rc;
553 
554 		shash = &sdesc->shash;
555 	} else {
556 		hash = server->secmech.cmacaes;
557 		shash = &server->secmech.sdesccmacaes->shash;
558 	}
559 
560 	memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
561 	memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
562 
563 	rc = crypto_shash_setkey(hash, key, SMB2_CMACAES_SIZE);
564 	if (rc) {
565 		cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
566 		goto out;
567 	}
568 
569 	/*
570 	 * we already allocate sdesccmacaes when we init smb3 signing key,
571 	 * so unlike smb2 case we do not have to check here if secmech are
572 	 * initialized
573 	 */
574 	rc = crypto_shash_init(shash);
575 	if (rc) {
576 		cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
577 		goto out;
578 	}
579 
580 	/*
581 	 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
582 	 * data, that is, iov[0] should not contain a rfc1002 length.
583 	 *
584 	 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
585 	 * __cifs_calc_signature().
586 	 */
587 	drqst = *rqst;
588 	if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
589 		rc = crypto_shash_update(shash, iov[0].iov_base,
590 					 iov[0].iov_len);
591 		if (rc) {
592 			cifs_server_dbg(VFS, "%s: Could not update with payload\n",
593 				 __func__);
594 			goto out;
595 		}
596 		drqst.rq_iov++;
597 		drqst.rq_nvec--;
598 	}
599 
600 	rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
601 	if (!rc)
602 		memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
603 
604 out:
605 	if (allocate_crypto)
606 		cifs_free_hash(&hash, &sdesc);
607 	return rc;
608 }
609 
610 /* must be called with server->srv_mutex held */
611 static int
smb2_sign_rqst(struct smb_rqst * rqst,struct TCP_Server_Info * server)612 smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
613 {
614 	int rc = 0;
615 	struct smb2_sync_hdr *shdr;
616 	struct smb2_sess_setup_req *ssr;
617 	bool is_binding;
618 	bool is_signed;
619 
620 	shdr = (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
621 	ssr = (struct smb2_sess_setup_req *)shdr;
622 
623 	is_binding = shdr->Command == SMB2_SESSION_SETUP &&
624 		(ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
625 	is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
626 
627 	if (!is_signed)
628 		return 0;
629 	if (server->tcpStatus == CifsNeedNegotiate)
630 		return 0;
631 	if (!is_binding && !server->session_estab) {
632 		strncpy(shdr->Signature, "BSRSPYL", 8);
633 		return 0;
634 	}
635 
636 	rc = server->ops->calc_signature(rqst, server, false);
637 
638 	return rc;
639 }
640 
641 int
smb2_verify_signature(struct smb_rqst * rqst,struct TCP_Server_Info * server)642 smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
643 {
644 	unsigned int rc;
645 	char server_response_sig[SMB2_SIGNATURE_SIZE];
646 	struct smb2_sync_hdr *shdr =
647 			(struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
648 
649 	if ((shdr->Command == SMB2_NEGOTIATE) ||
650 	    (shdr->Command == SMB2_SESSION_SETUP) ||
651 	    (shdr->Command == SMB2_OPLOCK_BREAK) ||
652 	    server->ignore_signature ||
653 	    (!server->session_estab))
654 		return 0;
655 
656 	/*
657 	 * BB what if signatures are supposed to be on for session but
658 	 * server does not send one? BB
659 	 */
660 
661 	/* Do not need to verify session setups with signature "BSRSPYL " */
662 	if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
663 		cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
664 			 shdr->Command);
665 
666 	/*
667 	 * Save off the origiginal signature so we can modify the smb and check
668 	 * our calculated signature against what the server sent.
669 	 */
670 	memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
671 
672 	memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
673 
674 	rc = server->ops->calc_signature(rqst, server, true);
675 
676 	if (rc)
677 		return rc;
678 
679 	if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE)) {
680 		cifs_dbg(VFS, "sign fail cmd 0x%x message id 0x%llx\n",
681 			shdr->Command, shdr->MessageId);
682 		return -EACCES;
683 	} else
684 		return 0;
685 }
686 
687 /*
688  * Set message id for the request. Should be called after wait_for_free_request
689  * and when srv_mutex is held.
690  */
691 static inline void
smb2_seq_num_into_buf(struct TCP_Server_Info * server,struct smb2_sync_hdr * shdr)692 smb2_seq_num_into_buf(struct TCP_Server_Info *server,
693 		      struct smb2_sync_hdr *shdr)
694 {
695 	unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
696 
697 	shdr->MessageId = get_next_mid64(server);
698 	/* skip message numbers according to CreditCharge field */
699 	for (i = 1; i < num; i++)
700 		get_next_mid(server);
701 }
702 
703 static struct mid_q_entry *
smb2_mid_entry_alloc(const struct smb2_sync_hdr * shdr,struct TCP_Server_Info * server)704 smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
705 		     struct TCP_Server_Info *server)
706 {
707 	struct mid_q_entry *temp;
708 	unsigned int credits = le16_to_cpu(shdr->CreditCharge);
709 
710 	if (server == NULL) {
711 		cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
712 		return NULL;
713 	}
714 
715 	temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
716 	memset(temp, 0, sizeof(struct mid_q_entry));
717 	kref_init(&temp->refcount);
718 	temp->mid = le64_to_cpu(shdr->MessageId);
719 	temp->credits = credits > 0 ? credits : 1;
720 	temp->pid = current->pid;
721 	temp->command = shdr->Command; /* Always LE */
722 	temp->when_alloc = jiffies;
723 	temp->server = server;
724 
725 	/*
726 	 * The default is for the mid to be synchronous, so the
727 	 * default callback just wakes up the current task.
728 	 */
729 	get_task_struct(current);
730 	temp->creator = current;
731 	temp->callback = cifs_wake_up_task;
732 	temp->callback_data = current;
733 
734 	atomic_inc(&midCount);
735 	temp->mid_state = MID_REQUEST_ALLOCATED;
736 	trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
737 		le16_to_cpu(shdr->Command), temp->mid);
738 	return temp;
739 }
740 
741 static int
smb2_get_mid_entry(struct cifs_ses * ses,struct TCP_Server_Info * server,struct smb2_sync_hdr * shdr,struct mid_q_entry ** mid)742 smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
743 		   struct smb2_sync_hdr *shdr, struct mid_q_entry **mid)
744 {
745 	if (server->tcpStatus == CifsExiting)
746 		return -ENOENT;
747 
748 	if (server->tcpStatus == CifsNeedReconnect) {
749 		cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
750 		return -EAGAIN;
751 	}
752 
753 	if (server->tcpStatus == CifsNeedNegotiate &&
754 	   shdr->Command != SMB2_NEGOTIATE)
755 		return -EAGAIN;
756 
757 	if (ses->status == CifsNew) {
758 		if ((shdr->Command != SMB2_SESSION_SETUP) &&
759 		    (shdr->Command != SMB2_NEGOTIATE))
760 			return -EAGAIN;
761 		/* else ok - we are setting up session */
762 	}
763 
764 	if (ses->status == CifsExiting) {
765 		if (shdr->Command != SMB2_LOGOFF)
766 			return -EAGAIN;
767 		/* else ok - we are shutting down the session */
768 	}
769 
770 	*mid = smb2_mid_entry_alloc(shdr, server);
771 	if (*mid == NULL)
772 		return -ENOMEM;
773 	spin_lock(&GlobalMid_Lock);
774 	list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
775 	spin_unlock(&GlobalMid_Lock);
776 
777 	return 0;
778 }
779 
780 int
smb2_check_receive(struct mid_q_entry * mid,struct TCP_Server_Info * server,bool log_error)781 smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
782 		   bool log_error)
783 {
784 	unsigned int len = mid->resp_buf_size;
785 	struct kvec iov[1];
786 	struct smb_rqst rqst = { .rq_iov = iov,
787 				 .rq_nvec = 1 };
788 
789 	iov[0].iov_base = (char *)mid->resp_buf;
790 	iov[0].iov_len = len;
791 
792 	dump_smb(mid->resp_buf, min_t(u32, 80, len));
793 	/* convert the length into a more usable form */
794 	if (len > 24 && server->sign && !mid->decrypted) {
795 		int rc;
796 
797 		rc = smb2_verify_signature(&rqst, server);
798 		if (rc)
799 			cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
800 				 rc);
801 	}
802 
803 	return map_smb2_to_linux_error(mid->resp_buf, log_error);
804 }
805 
806 struct mid_q_entry *
smb2_setup_request(struct cifs_ses * ses,struct TCP_Server_Info * server,struct smb_rqst * rqst)807 smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
808 		   struct smb_rqst *rqst)
809 {
810 	int rc;
811 	struct smb2_sync_hdr *shdr =
812 			(struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
813 	struct mid_q_entry *mid;
814 
815 	smb2_seq_num_into_buf(server, shdr);
816 
817 	rc = smb2_get_mid_entry(ses, server, shdr, &mid);
818 	if (rc) {
819 		revert_current_mid_from_hdr(server, shdr);
820 		return ERR_PTR(rc);
821 	}
822 
823 	rc = smb2_sign_rqst(rqst, server);
824 	if (rc) {
825 		revert_current_mid_from_hdr(server, shdr);
826 		cifs_delete_mid(mid);
827 		return ERR_PTR(rc);
828 	}
829 
830 	return mid;
831 }
832 
833 struct mid_q_entry *
smb2_setup_async_request(struct TCP_Server_Info * server,struct smb_rqst * rqst)834 smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
835 {
836 	int rc;
837 	struct smb2_sync_hdr *shdr =
838 			(struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
839 	struct mid_q_entry *mid;
840 
841 	if (server->tcpStatus == CifsNeedNegotiate &&
842 	   shdr->Command != SMB2_NEGOTIATE)
843 		return ERR_PTR(-EAGAIN);
844 
845 	smb2_seq_num_into_buf(server, shdr);
846 
847 	mid = smb2_mid_entry_alloc(shdr, server);
848 	if (mid == NULL) {
849 		revert_current_mid_from_hdr(server, shdr);
850 		return ERR_PTR(-ENOMEM);
851 	}
852 
853 	rc = smb2_sign_rqst(rqst, server);
854 	if (rc) {
855 		revert_current_mid_from_hdr(server, shdr);
856 		DeleteMidQEntry(mid);
857 		return ERR_PTR(rc);
858 	}
859 
860 	return mid;
861 }
862 
863 int
smb3_crypto_aead_allocate(struct TCP_Server_Info * server)864 smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
865 {
866 	struct crypto_aead *tfm;
867 
868 	if (!server->secmech.ccmaesencrypt) {
869 		if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
870 		    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
871 			tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
872 		else
873 			tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
874 		if (IS_ERR(tfm)) {
875 			cifs_server_dbg(VFS, "%s: Failed alloc encrypt aead\n",
876 				 __func__);
877 			return PTR_ERR(tfm);
878 		}
879 		server->secmech.ccmaesencrypt = tfm;
880 	}
881 
882 	if (!server->secmech.ccmaesdecrypt) {
883 		if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
884 		    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
885 			tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
886 		else
887 			tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
888 		if (IS_ERR(tfm)) {
889 			crypto_free_aead(server->secmech.ccmaesencrypt);
890 			server->secmech.ccmaesencrypt = NULL;
891 			cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
892 				 __func__);
893 			return PTR_ERR(tfm);
894 		}
895 		server->secmech.ccmaesdecrypt = tfm;
896 	}
897 
898 	return 0;
899 }
900