2 * fs/cifs/smb2transport.c
4 * Copyright (C) International Business Machines Corp., 2002, 2011
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006
8 * Pavel Shilovsky (pshilovsky@samba.org) 2012
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/list.h>
27 #include <linux/wait.h>
28 #include <linux/net.h>
29 #include <linux/delay.h>
30 #include <linux/uaccess.h>
31 #include <asm/processor.h>
32 #include <linux/mempool.h>
33 #include <linux/highmem.h>
34 #include <crypto/aead.h>
37 #include "cifsproto.h"
38 #include "smb2proto.h"
39 #include "cifs_debug.h"
40 #include "smb2status.h"
44 smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
46 return cifs_alloc_hash("hmac(sha256)",
47 &server->secmech.hmacsha256,
48 &server->secmech.sdeschmacsha256);
52 smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
54 struct cifs_secmech *p = &server->secmech;
57 rc = cifs_alloc_hash("hmac(sha256)",
63 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
69 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
74 smb311_crypto_shash_allocate(struct TCP_Server_Info *server)
76 struct cifs_secmech *p = &server->secmech;
79 rc = cifs_alloc_hash("hmac(sha256)",
85 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
89 rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512);
96 cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
97 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
101 static struct cifs_ses *
102 smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
104 struct cifs_ses *ses;
106 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
107 if (ses->Suid != ses_id)
116 smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
118 struct cifs_ses *ses;
120 spin_lock(&cifs_tcp_ses_lock);
121 ses = smb2_find_smb_ses_unlocked(server, ses_id);
122 spin_unlock(&cifs_tcp_ses_lock);
127 static struct cifs_tcon *
128 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
130 struct cifs_tcon *tcon;
132 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
133 if (tcon->tid != tid)
143 * Obtain tcon corresponding to the tid in the given
148 smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
150 struct cifs_ses *ses;
151 struct cifs_tcon *tcon;
153 spin_lock(&cifs_tcp_ses_lock);
154 ses = smb2_find_smb_ses_unlocked(server, ses_id);
156 spin_unlock(&cifs_tcp_ses_lock);
159 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
160 spin_unlock(&cifs_tcp_ses_lock);
166 smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
169 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
170 unsigned char *sigptr = smb2_signature;
171 struct kvec *iov = rqst->rq_iov;
172 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
173 struct cifs_ses *ses;
174 struct shash_desc *shash;
175 struct smb_rqst drqst;
177 ses = smb2_find_smb_ses(server, shdr->SessionId);
179 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
183 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
184 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
186 rc = smb2_crypto_shash_allocate(server);
188 cifs_dbg(VFS, "%s: sha256 alloc failed\n", __func__);
192 rc = crypto_shash_setkey(server->secmech.hmacsha256,
193 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
195 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
199 shash = &server->secmech.sdeschmacsha256->shash;
200 rc = crypto_shash_init(shash);
202 cifs_dbg(VFS, "%s: Could not init sha256", __func__);
207 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
208 * data, that is, iov[0] should not contain a rfc1002 length.
210 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
211 * __cifs_calc_signature().
214 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
215 rc = crypto_shash_update(shash, iov[0].iov_base,
218 cifs_dbg(VFS, "%s: Could not update with payload\n",
226 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
228 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
233 static int generate_key(struct cifs_ses *ses, struct kvec label,
234 struct kvec context, __u8 *key, unsigned int key_size)
236 unsigned char zero = 0x0;
237 __u8 i[4] = {0, 0, 0, 1};
238 __u8 L[4] = {0, 0, 0, 128};
240 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
241 unsigned char *hashptr = prfhash;
243 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
244 memset(key, 0x0, key_size);
246 rc = smb3_crypto_shash_allocate(ses->server);
248 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
249 goto smb3signkey_ret;
252 rc = crypto_shash_setkey(ses->server->secmech.hmacsha256,
253 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
255 cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
256 goto smb3signkey_ret;
259 rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash);
261 cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
262 goto smb3signkey_ret;
265 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
268 cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
269 goto smb3signkey_ret;
272 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
273 label.iov_base, label.iov_len);
275 cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
276 goto smb3signkey_ret;
279 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
282 cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
283 goto smb3signkey_ret;
286 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
287 context.iov_base, context.iov_len);
289 cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
290 goto smb3signkey_ret;
293 rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
296 cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
297 goto smb3signkey_ret;
300 rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash,
303 cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
304 goto smb3signkey_ret;
307 memcpy(key, hashptr, key_size);
318 struct derivation_triplet {
319 struct derivation signing;
320 struct derivation encryption;
321 struct derivation decryption;
325 generate_smb3signingkey(struct cifs_ses *ses,
326 const struct derivation_triplet *ptriplet)
330 rc = generate_key(ses, ptriplet->signing.label,
331 ptriplet->signing.context, ses->smb3signingkey,
336 rc = generate_key(ses, ptriplet->encryption.label,
337 ptriplet->encryption.context, ses->smb3encryptionkey,
342 rc = generate_key(ses, ptriplet->decryption.label,
343 ptriplet->decryption.context,
344 ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
349 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
350 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
352 * The session id is opaque in terms of endianness, so we can't
353 * print it as a long long. we dump it as we got it on the wire
355 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
357 cifs_dbg(VFS, "Session Key %*ph\n",
358 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
359 cifs_dbg(VFS, "Signing Key %*ph\n",
360 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
361 cifs_dbg(VFS, "ServerIn Key %*ph\n",
362 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
363 cifs_dbg(VFS, "ServerOut Key %*ph\n",
364 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
370 generate_smb30signingkey(struct cifs_ses *ses)
373 struct derivation_triplet triplet;
374 struct derivation *d;
376 d = &triplet.signing;
377 d->label.iov_base = "SMB2AESCMAC";
378 d->label.iov_len = 12;
379 d->context.iov_base = "SmbSign";
380 d->context.iov_len = 8;
382 d = &triplet.encryption;
383 d->label.iov_base = "SMB2AESCCM";
384 d->label.iov_len = 11;
385 d->context.iov_base = "ServerIn ";
386 d->context.iov_len = 10;
388 d = &triplet.decryption;
389 d->label.iov_base = "SMB2AESCCM";
390 d->label.iov_len = 11;
391 d->context.iov_base = "ServerOut";
392 d->context.iov_len = 10;
394 return generate_smb3signingkey(ses, &triplet);
398 generate_smb311signingkey(struct cifs_ses *ses)
401 struct derivation_triplet triplet;
402 struct derivation *d;
404 d = &triplet.signing;
405 d->label.iov_base = "SMBSigningKey";
406 d->label.iov_len = 14;
407 d->context.iov_base = ses->preauth_sha_hash;
408 d->context.iov_len = 64;
410 d = &triplet.encryption;
411 d->label.iov_base = "SMBC2SCipherKey";
412 d->label.iov_len = 16;
413 d->context.iov_base = ses->preauth_sha_hash;
414 d->context.iov_len = 64;
416 d = &triplet.decryption;
417 d->label.iov_base = "SMBS2CCipherKey";
418 d->label.iov_len = 16;
419 d->context.iov_base = ses->preauth_sha_hash;
420 d->context.iov_len = 64;
422 return generate_smb3signingkey(ses, &triplet);
426 smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
429 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
430 unsigned char *sigptr = smb3_signature;
431 struct kvec *iov = rqst->rq_iov;
432 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
433 struct cifs_ses *ses;
434 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
435 struct smb_rqst drqst;
437 ses = smb2_find_smb_ses(server, shdr->SessionId);
439 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
443 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
444 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
446 rc = crypto_shash_setkey(server->secmech.cmacaes,
447 ses->smb3signingkey, SMB2_CMACAES_SIZE);
449 cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
454 * we already allocate sdesccmacaes when we init smb3 signing key,
455 * so unlike smb2 case we do not have to check here if secmech are
458 rc = crypto_shash_init(shash);
460 cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
465 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
466 * data, that is, iov[0] should not contain a rfc1002 length.
468 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
469 * __cifs_calc_signature().
472 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
473 rc = crypto_shash_update(shash, iov[0].iov_base,
476 cifs_dbg(VFS, "%s: Could not update with payload\n",
484 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
486 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
491 /* must be called with server->srv_mutex held */
493 smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
496 struct smb2_sync_hdr *shdr =
497 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
499 if (!(shdr->Flags & SMB2_FLAGS_SIGNED) ||
500 server->tcpStatus == CifsNeedNegotiate)
503 if (!server->session_estab) {
504 strncpy(shdr->Signature, "BSRSPYL", 8);
508 rc = server->ops->calc_signature(rqst, server);
514 smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
517 char server_response_sig[16];
518 struct smb2_sync_hdr *shdr =
519 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
521 if ((shdr->Command == SMB2_NEGOTIATE) ||
522 (shdr->Command == SMB2_SESSION_SETUP) ||
523 (shdr->Command == SMB2_OPLOCK_BREAK) ||
524 (!server->session_estab))
528 * BB what if signatures are supposed to be on for session but
529 * server does not send one? BB
532 /* Do not need to verify session setups with signature "BSRSPYL " */
533 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
534 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
538 * Save off the origiginal signature so we can modify the smb and check
539 * our calculated signature against what the server sent.
541 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
543 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
545 mutex_lock(&server->srv_mutex);
546 rc = server->ops->calc_signature(rqst, server);
547 mutex_unlock(&server->srv_mutex);
552 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
559 * Set message id for the request. Should be called after wait_for_free_request
560 * and when srv_mutex is held.
563 smb2_seq_num_into_buf(struct TCP_Server_Info *server,
564 struct smb2_sync_hdr *shdr)
566 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
568 shdr->MessageId = get_next_mid64(server);
569 /* skip message numbers according to CreditCharge field */
570 for (i = 1; i < num; i++)
571 get_next_mid(server);
574 static struct mid_q_entry *
575 smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
576 struct TCP_Server_Info *server)
578 struct mid_q_entry *temp;
579 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
581 if (server == NULL) {
582 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
586 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
587 memset(temp, 0, sizeof(struct mid_q_entry));
588 kref_init(&temp->refcount);
589 temp->mid = le64_to_cpu(shdr->MessageId);
590 temp->credits = credits > 0 ? credits : 1;
591 temp->pid = current->pid;
592 temp->command = shdr->Command; /* Always LE */
593 temp->when_alloc = jiffies;
594 temp->server = server;
597 * The default is for the mid to be synchronous, so the
598 * default callback just wakes up the current task.
600 temp->callback = cifs_wake_up_task;
601 temp->callback_data = current;
603 atomic_inc(&midCount);
604 temp->mid_state = MID_REQUEST_ALLOCATED;
605 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
606 le16_to_cpu(shdr->Command), temp->mid);
611 smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_sync_hdr *shdr,
612 struct mid_q_entry **mid)
614 if (ses->server->tcpStatus == CifsExiting)
617 if (ses->server->tcpStatus == CifsNeedReconnect) {
618 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
622 if (ses->server->tcpStatus == CifsNeedNegotiate &&
623 shdr->Command != SMB2_NEGOTIATE)
626 if (ses->status == CifsNew) {
627 if ((shdr->Command != SMB2_SESSION_SETUP) &&
628 (shdr->Command != SMB2_NEGOTIATE))
630 /* else ok - we are setting up session */
633 if (ses->status == CifsExiting) {
634 if (shdr->Command != SMB2_LOGOFF)
636 /* else ok - we are shutting down the session */
639 *mid = smb2_mid_entry_alloc(shdr, ses->server);
642 spin_lock(&GlobalMid_Lock);
643 list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
644 spin_unlock(&GlobalMid_Lock);
650 smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
653 unsigned int len = mid->resp_buf_size;
655 struct smb_rqst rqst = { .rq_iov = iov,
658 iov[0].iov_base = (char *)mid->resp_buf;
659 iov[0].iov_len = len;
661 dump_smb(mid->resp_buf, min_t(u32, 80, len));
662 /* convert the length into a more usable form */
663 if (len > 24 && server->sign && !mid->decrypted) {
666 rc = smb2_verify_signature(&rqst, server);
668 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
672 return map_smb2_to_linux_error(mid->resp_buf, log_error);
676 smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
679 struct smb2_sync_hdr *shdr =
680 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
681 struct mid_q_entry *mid;
683 smb2_seq_num_into_buf(ses->server, shdr);
685 rc = smb2_get_mid_entry(ses, shdr, &mid);
687 revert_current_mid_from_hdr(ses->server, shdr);
691 rc = smb2_sign_rqst(rqst, ses->server);
693 revert_current_mid_from_hdr(ses->server, shdr);
694 cifs_delete_mid(mid);
702 smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
705 struct smb2_sync_hdr *shdr =
706 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
707 struct mid_q_entry *mid;
709 if (server->tcpStatus == CifsNeedNegotiate &&
710 shdr->Command != SMB2_NEGOTIATE)
711 return ERR_PTR(-EAGAIN);
713 smb2_seq_num_into_buf(server, shdr);
715 mid = smb2_mid_entry_alloc(shdr, server);
717 revert_current_mid_from_hdr(server, shdr);
718 return ERR_PTR(-ENOMEM);
721 rc = smb2_sign_rqst(rqst, server);
723 revert_current_mid_from_hdr(server, shdr);
724 DeleteMidQEntry(mid);
732 smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
734 struct crypto_aead *tfm;
736 if (!server->secmech.ccmaesencrypt) {
737 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
739 cifs_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
743 server->secmech.ccmaesencrypt = tfm;
746 if (!server->secmech.ccmaesdecrypt) {
747 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
749 crypto_free_aead(server->secmech.ccmaesencrypt);
750 server->secmech.ccmaesencrypt = NULL;
751 cifs_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
755 server->secmech.ccmaesdecrypt = tfm;