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