Debian Codegraph @

fs/smb/client/smb2transport.c

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