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