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