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);
103 int smb2_get_sign_key(__u64 ses_id, struct TCP_Server_Info *server, u8 *key)
105 struct cifs_chan *chan;
106 struct cifs_ses *ses = NULL;
107 struct TCP_Server_Info *it = NULL;
111 spin_lock(&cifs_tcp_ses_lock);
113 list_for_each_entry(it, &cifs_tcp_ses_list, tcp_ses_list) {
114 list_for_each_entry(ses, &it->smb_ses_list, smb_ses_list) {
115 if (ses->Suid == ses_id)
119 cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
127 * If we are in the process of binding a new channel
128 * to an existing session, use the master connection
131 memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
136 * Otherwise, use the channel key.
139 for (i = 0; i < ses->chan_count; i++) {
140 chan = ses->chans + i;
141 if (chan->server == server) {
142 memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
148 "%s: Could not find channel signing key for session 0x%llx\n",
153 spin_unlock(&cifs_tcp_ses_lock);
157 static struct cifs_ses *
158 smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
160 struct cifs_ses *ses;
162 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
163 if (ses->Suid != ses_id)
172 smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
174 struct cifs_ses *ses;
176 spin_lock(&cifs_tcp_ses_lock);
177 ses = smb2_find_smb_ses_unlocked(server, ses_id);
178 spin_unlock(&cifs_tcp_ses_lock);
183 static struct cifs_tcon *
184 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
186 struct cifs_tcon *tcon;
188 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
189 if (tcon->tid != tid)
199 * Obtain tcon corresponding to the tid in the given
204 smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
206 struct cifs_ses *ses;
207 struct cifs_tcon *tcon;
209 spin_lock(&cifs_tcp_ses_lock);
210 ses = smb2_find_smb_ses_unlocked(server, ses_id);
212 spin_unlock(&cifs_tcp_ses_lock);
215 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
216 spin_unlock(&cifs_tcp_ses_lock);
222 smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
225 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
226 unsigned char *sigptr = smb2_signature;
227 struct kvec *iov = rqst->rq_iov;
228 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
229 struct cifs_ses *ses;
230 struct shash_desc *shash;
231 struct smb_rqst drqst;
233 ses = smb2_find_smb_ses(server, shdr->SessionId);
235 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
239 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
240 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
242 rc = smb2_crypto_shash_allocate(server);
244 cifs_server_dbg(VFS, "%s: sha256 alloc failed\n", __func__);
248 rc = crypto_shash_setkey(server->secmech.hmacsha256,
249 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
251 cifs_server_dbg(VFS, "%s: Could not update with response\n", __func__);
255 shash = &server->secmech.sdeschmacsha256->shash;
256 rc = crypto_shash_init(shash);
258 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
263 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
264 * data, that is, iov[0] should not contain a rfc1002 length.
266 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
267 * __cifs_calc_signature().
270 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
271 rc = crypto_shash_update(shash, iov[0].iov_base,
274 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
282 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
284 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
289 static int generate_key(struct cifs_ses *ses, struct kvec label,
290 struct kvec context, __u8 *key, unsigned int key_size)
292 unsigned char zero = 0x0;
293 __u8 i[4] = {0, 0, 0, 1};
294 __u8 L[4] = {0, 0, 0, 128};
296 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
297 unsigned char *hashptr = prfhash;
298 struct TCP_Server_Info *server = ses->server;
300 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
301 memset(key, 0x0, key_size);
303 rc = smb3_crypto_shash_allocate(server);
305 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
306 goto smb3signkey_ret;
309 rc = crypto_shash_setkey(server->secmech.hmacsha256,
310 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
312 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
313 goto smb3signkey_ret;
316 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
318 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
319 goto smb3signkey_ret;
322 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
325 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
326 goto smb3signkey_ret;
329 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
330 label.iov_base, label.iov_len);
332 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
333 goto smb3signkey_ret;
336 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
339 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
340 goto smb3signkey_ret;
343 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
344 context.iov_base, context.iov_len);
346 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
347 goto smb3signkey_ret;
350 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
353 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
354 goto smb3signkey_ret;
357 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
360 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
361 goto smb3signkey_ret;
364 memcpy(key, hashptr, key_size);
375 struct derivation_triplet {
376 struct derivation signing;
377 struct derivation encryption;
378 struct derivation decryption;
382 generate_smb3signingkey(struct cifs_ses *ses,
383 const struct derivation_triplet *ptriplet)
388 * All channels use the same encryption/decryption keys but
389 * they have their own signing key.
391 * When we generate the keys, check if it is for a new channel
392 * (binding) in which case we only need to generate a signing
393 * key and store it in the channel as to not overwrite the
394 * master connection signing key stored in the session
398 rc = generate_key(ses, ptriplet->signing.label,
399 ptriplet->signing.context,
400 cifs_ses_binding_channel(ses)->signkey,
405 rc = generate_key(ses, ptriplet->signing.label,
406 ptriplet->signing.context,
412 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
415 rc = generate_key(ses, ptriplet->encryption.label,
416 ptriplet->encryption.context,
417 ses->smb3encryptionkey,
419 rc = generate_key(ses, ptriplet->decryption.label,
420 ptriplet->decryption.context,
421 ses->smb3decryptionkey,
430 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
431 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
433 * The session id is opaque in terms of endianness, so we can't
434 * print it as a long long. we dump it as we got it on the wire
436 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
438 cifs_dbg(VFS, "Session Key %*ph\n",
439 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
440 cifs_dbg(VFS, "Signing Key %*ph\n",
441 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
442 cifs_dbg(VFS, "ServerIn Key %*ph\n",
443 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
444 cifs_dbg(VFS, "ServerOut Key %*ph\n",
445 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
451 generate_smb30signingkey(struct cifs_ses *ses)
454 struct derivation_triplet triplet;
455 struct derivation *d;
457 d = &triplet.signing;
458 d->label.iov_base = "SMB2AESCMAC";
459 d->label.iov_len = 12;
460 d->context.iov_base = "SmbSign";
461 d->context.iov_len = 8;
463 d = &triplet.encryption;
464 d->label.iov_base = "SMB2AESCCM";
465 d->label.iov_len = 11;
466 d->context.iov_base = "ServerIn ";
467 d->context.iov_len = 10;
469 d = &triplet.decryption;
470 d->label.iov_base = "SMB2AESCCM";
471 d->label.iov_len = 11;
472 d->context.iov_base = "ServerOut";
473 d->context.iov_len = 10;
475 return generate_smb3signingkey(ses, &triplet);
479 generate_smb311signingkey(struct cifs_ses *ses)
482 struct derivation_triplet triplet;
483 struct derivation *d;
485 d = &triplet.signing;
486 d->label.iov_base = "SMBSigningKey";
487 d->label.iov_len = 14;
488 d->context.iov_base = ses->preauth_sha_hash;
489 d->context.iov_len = 64;
491 d = &triplet.encryption;
492 d->label.iov_base = "SMBC2SCipherKey";
493 d->label.iov_len = 16;
494 d->context.iov_base = ses->preauth_sha_hash;
495 d->context.iov_len = 64;
497 d = &triplet.decryption;
498 d->label.iov_base = "SMBS2CCipherKey";
499 d->label.iov_len = 16;
500 d->context.iov_base = ses->preauth_sha_hash;
501 d->context.iov_len = 64;
503 return generate_smb3signingkey(ses, &triplet);
507 smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
510 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
511 unsigned char *sigptr = smb3_signature;
512 struct kvec *iov = rqst->rq_iov;
513 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
514 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
515 struct smb_rqst drqst;
516 u8 key[SMB3_SIGN_KEY_SIZE];
518 rc = smb2_get_sign_key(shdr->SessionId, server, key);
522 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
523 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
525 rc = crypto_shash_setkey(server->secmech.cmacaes,
526 key, SMB2_CMACAES_SIZE);
528 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
533 * we already allocate sdesccmacaes when we init smb3 signing key,
534 * so unlike smb2 case we do not have to check here if secmech are
537 rc = crypto_shash_init(shash);
539 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
544 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
545 * data, that is, iov[0] should not contain a rfc1002 length.
547 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
548 * __cifs_calc_signature().
551 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
552 rc = crypto_shash_update(shash, iov[0].iov_base,
555 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
563 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
565 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
570 /* must be called with server->srv_mutex held */
572 smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
575 struct smb2_sync_hdr *shdr;
576 struct smb2_sess_setup_req *ssr;
580 shdr = (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
581 ssr = (struct smb2_sess_setup_req *)shdr;
583 is_binding = shdr->Command == SMB2_SESSION_SETUP &&
584 (ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
585 is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
589 if (server->tcpStatus == CifsNeedNegotiate)
591 if (!is_binding && !server->session_estab) {
592 strncpy(shdr->Signature, "BSRSPYL", 8);
596 rc = server->ops->calc_signature(rqst, server);
602 smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
605 char server_response_sig[16];
606 struct smb2_sync_hdr *shdr =
607 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
609 if ((shdr->Command == SMB2_NEGOTIATE) ||
610 (shdr->Command == SMB2_SESSION_SETUP) ||
611 (shdr->Command == SMB2_OPLOCK_BREAK) ||
612 server->ignore_signature ||
613 (!server->session_estab))
617 * BB what if signatures are supposed to be on for session but
618 * server does not send one? BB
621 /* Do not need to verify session setups with signature "BSRSPYL " */
622 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
623 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
627 * Save off the origiginal signature so we can modify the smb and check
628 * our calculated signature against what the server sent.
630 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
632 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
634 mutex_lock(&server->srv_mutex);
635 rc = server->ops->calc_signature(rqst, server);
636 mutex_unlock(&server->srv_mutex);
641 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
648 * Set message id for the request. Should be called after wait_for_free_request
649 * and when srv_mutex is held.
652 smb2_seq_num_into_buf(struct TCP_Server_Info *server,
653 struct smb2_sync_hdr *shdr)
655 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
657 shdr->MessageId = get_next_mid64(server);
658 /* skip message numbers according to CreditCharge field */
659 for (i = 1; i < num; i++)
660 get_next_mid(server);
663 static struct mid_q_entry *
664 smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
665 struct TCP_Server_Info *server)
667 struct mid_q_entry *temp;
668 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
670 if (server == NULL) {
671 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
675 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
676 memset(temp, 0, sizeof(struct mid_q_entry));
677 kref_init(&temp->refcount);
678 temp->mid = le64_to_cpu(shdr->MessageId);
679 temp->credits = credits > 0 ? credits : 1;
680 temp->pid = current->pid;
681 temp->command = shdr->Command; /* Always LE */
682 temp->when_alloc = jiffies;
683 temp->server = server;
686 * The default is for the mid to be synchronous, so the
687 * default callback just wakes up the current task.
689 get_task_struct(current);
690 temp->creator = current;
691 temp->callback = cifs_wake_up_task;
692 temp->callback_data = current;
694 atomic_inc(&midCount);
695 temp->mid_state = MID_REQUEST_ALLOCATED;
696 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
697 le16_to_cpu(shdr->Command), temp->mid);
702 smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
703 struct smb2_sync_hdr *shdr, struct mid_q_entry **mid)
705 if (server->tcpStatus == CifsExiting)
708 if (server->tcpStatus == CifsNeedReconnect) {
709 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
713 if (server->tcpStatus == CifsNeedNegotiate &&
714 shdr->Command != SMB2_NEGOTIATE)
717 if (ses->status == CifsNew) {
718 if ((shdr->Command != SMB2_SESSION_SETUP) &&
719 (shdr->Command != SMB2_NEGOTIATE))
721 /* else ok - we are setting up session */
724 if (ses->status == CifsExiting) {
725 if (shdr->Command != SMB2_LOGOFF)
727 /* else ok - we are shutting down the session */
730 *mid = smb2_mid_entry_alloc(shdr, server);
733 spin_lock(&GlobalMid_Lock);
734 list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
735 spin_unlock(&GlobalMid_Lock);
741 smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
744 unsigned int len = mid->resp_buf_size;
746 struct smb_rqst rqst = { .rq_iov = iov,
749 iov[0].iov_base = (char *)mid->resp_buf;
750 iov[0].iov_len = len;
752 dump_smb(mid->resp_buf, min_t(u32, 80, len));
753 /* convert the length into a more usable form */
754 if (len > 24 && server->sign && !mid->decrypted) {
757 rc = smb2_verify_signature(&rqst, server);
759 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
763 return map_smb2_to_linux_error(mid->resp_buf, log_error);
767 smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
768 struct smb_rqst *rqst)
771 struct smb2_sync_hdr *shdr =
772 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
773 struct mid_q_entry *mid;
775 smb2_seq_num_into_buf(server, shdr);
777 rc = smb2_get_mid_entry(ses, server, shdr, &mid);
779 revert_current_mid_from_hdr(server, shdr);
783 rc = smb2_sign_rqst(rqst, server);
785 revert_current_mid_from_hdr(server, shdr);
786 cifs_delete_mid(mid);
794 smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
797 struct smb2_sync_hdr *shdr =
798 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
799 struct mid_q_entry *mid;
801 if (server->tcpStatus == CifsNeedNegotiate &&
802 shdr->Command != SMB2_NEGOTIATE)
803 return ERR_PTR(-EAGAIN);
805 smb2_seq_num_into_buf(server, shdr);
807 mid = smb2_mid_entry_alloc(shdr, server);
809 revert_current_mid_from_hdr(server, shdr);
810 return ERR_PTR(-ENOMEM);
813 rc = smb2_sign_rqst(rqst, server);
815 revert_current_mid_from_hdr(server, shdr);
816 DeleteMidQEntry(mid);
824 smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
826 struct crypto_aead *tfm;
828 if (!server->secmech.ccmaesencrypt) {
829 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
830 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
832 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
834 cifs_server_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
838 server->secmech.ccmaesencrypt = tfm;
841 if (!server->secmech.ccmaesdecrypt) {
842 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
843 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
845 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
847 crypto_free_aead(server->secmech.ccmaesencrypt);
848 server->secmech.ccmaesencrypt = NULL;
849 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
853 server->secmech.ccmaesdecrypt = tfm;