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;
110 spin_lock(&cifs_tcp_ses_lock);
112 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
113 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
114 if (ses->Suid == ses_id)
118 cifs_server_dbg(VFS, "%s: Could not find session 0x%llx\n",
126 * If we are in the process of binding a new channel
127 * to an existing session, use the master connection
130 memcpy(key, ses->smb3signingkey, SMB3_SIGN_KEY_SIZE);
135 * Otherwise, use the channel key.
138 for (i = 0; i < ses->chan_count; i++) {
139 chan = ses->chans + i;
140 if (chan->server == server) {
141 memcpy(key, chan->signkey, SMB3_SIGN_KEY_SIZE);
147 "%s: Could not find channel signing key for session 0x%llx\n",
152 spin_unlock(&cifs_tcp_ses_lock);
156 static struct cifs_ses *
157 smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id)
159 struct cifs_ses *ses;
161 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
162 if (ses->Suid != ses_id)
171 smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
173 struct cifs_ses *ses;
175 spin_lock(&cifs_tcp_ses_lock);
176 ses = smb2_find_smb_ses_unlocked(server, ses_id);
177 spin_unlock(&cifs_tcp_ses_lock);
182 static struct cifs_tcon *
183 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid)
185 struct cifs_tcon *tcon;
187 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
188 if (tcon->tid != tid)
198 * Obtain tcon corresponding to the tid in the given
203 smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid)
205 struct cifs_ses *ses;
206 struct cifs_tcon *tcon;
208 spin_lock(&cifs_tcp_ses_lock);
209 ses = smb2_find_smb_ses_unlocked(server, ses_id);
211 spin_unlock(&cifs_tcp_ses_lock);
214 tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid);
215 spin_unlock(&cifs_tcp_ses_lock);
221 smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
224 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
225 unsigned char *sigptr = smb2_signature;
226 struct kvec *iov = rqst->rq_iov;
227 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
228 struct cifs_ses *ses;
229 struct shash_desc *shash;
230 struct smb_rqst drqst;
232 ses = smb2_find_smb_ses(server, shdr->SessionId);
234 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
238 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
239 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
241 rc = smb2_crypto_shash_allocate(server);
243 cifs_server_dbg(VFS, "%s: sha256 alloc failed\n", __func__);
247 rc = crypto_shash_setkey(server->secmech.hmacsha256,
248 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
250 cifs_server_dbg(VFS, "%s: Could not update with response\n", __func__);
254 shash = &server->secmech.sdeschmacsha256->shash;
255 rc = crypto_shash_init(shash);
257 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
262 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
263 * data, that is, iov[0] should not contain a rfc1002 length.
265 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
266 * __cifs_calc_signature().
269 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
270 rc = crypto_shash_update(shash, iov[0].iov_base,
273 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
281 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
283 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
288 static int generate_key(struct cifs_ses *ses, struct kvec label,
289 struct kvec context, __u8 *key, unsigned int key_size)
291 unsigned char zero = 0x0;
292 __u8 i[4] = {0, 0, 0, 1};
293 __u8 L[4] = {0, 0, 0, 128};
295 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
296 unsigned char *hashptr = prfhash;
297 struct TCP_Server_Info *server = ses->server;
299 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
300 memset(key, 0x0, key_size);
302 rc = smb3_crypto_shash_allocate(server);
304 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
305 goto smb3signkey_ret;
308 rc = crypto_shash_setkey(server->secmech.hmacsha256,
309 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
311 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
312 goto smb3signkey_ret;
315 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
317 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
318 goto smb3signkey_ret;
321 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
324 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
325 goto smb3signkey_ret;
328 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
329 label.iov_base, label.iov_len);
331 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
332 goto smb3signkey_ret;
335 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
338 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
339 goto smb3signkey_ret;
342 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
343 context.iov_base, context.iov_len);
345 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
346 goto smb3signkey_ret;
349 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
352 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
353 goto smb3signkey_ret;
356 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
359 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
360 goto smb3signkey_ret;
363 memcpy(key, hashptr, key_size);
374 struct derivation_triplet {
375 struct derivation signing;
376 struct derivation encryption;
377 struct derivation decryption;
381 generate_smb3signingkey(struct cifs_ses *ses,
382 const struct derivation_triplet *ptriplet)
387 * All channels use the same encryption/decryption keys but
388 * they have their own signing key.
390 * When we generate the keys, check if it is for a new channel
391 * (binding) in which case we only need to generate a signing
392 * key and store it in the channel as to not overwrite the
393 * master connection signing key stored in the session
397 rc = generate_key(ses, ptriplet->signing.label,
398 ptriplet->signing.context,
399 cifs_ses_binding_channel(ses)->signkey,
404 rc = generate_key(ses, ptriplet->signing.label,
405 ptriplet->signing.context,
411 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
414 rc = generate_key(ses, ptriplet->encryption.label,
415 ptriplet->encryption.context,
416 ses->smb3encryptionkey,
418 rc = generate_key(ses, ptriplet->decryption.label,
419 ptriplet->decryption.context,
420 ses->smb3decryptionkey,
429 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
430 cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__);
432 * The session id is opaque in terms of endianness, so we can't
433 * print it as a long long. we dump it as we got it on the wire
435 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
437 cifs_dbg(VFS, "Session Key %*ph\n",
438 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
439 cifs_dbg(VFS, "Signing Key %*ph\n",
440 SMB3_SIGN_KEY_SIZE, ses->smb3signingkey);
441 cifs_dbg(VFS, "ServerIn Key %*ph\n",
442 SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey);
443 cifs_dbg(VFS, "ServerOut Key %*ph\n",
444 SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey);
450 generate_smb30signingkey(struct cifs_ses *ses)
453 struct derivation_triplet triplet;
454 struct derivation *d;
456 d = &triplet.signing;
457 d->label.iov_base = "SMB2AESCMAC";
458 d->label.iov_len = 12;
459 d->context.iov_base = "SmbSign";
460 d->context.iov_len = 8;
462 d = &triplet.encryption;
463 d->label.iov_base = "SMB2AESCCM";
464 d->label.iov_len = 11;
465 d->context.iov_base = "ServerIn ";
466 d->context.iov_len = 10;
468 d = &triplet.decryption;
469 d->label.iov_base = "SMB2AESCCM";
470 d->label.iov_len = 11;
471 d->context.iov_base = "ServerOut";
472 d->context.iov_len = 10;
474 return generate_smb3signingkey(ses, &triplet);
478 generate_smb311signingkey(struct cifs_ses *ses)
481 struct derivation_triplet triplet;
482 struct derivation *d;
484 d = &triplet.signing;
485 d->label.iov_base = "SMBSigningKey";
486 d->label.iov_len = 14;
487 d->context.iov_base = ses->preauth_sha_hash;
488 d->context.iov_len = 64;
490 d = &triplet.encryption;
491 d->label.iov_base = "SMBC2SCipherKey";
492 d->label.iov_len = 16;
493 d->context.iov_base = ses->preauth_sha_hash;
494 d->context.iov_len = 64;
496 d = &triplet.decryption;
497 d->label.iov_base = "SMBS2CCipherKey";
498 d->label.iov_len = 16;
499 d->context.iov_base = ses->preauth_sha_hash;
500 d->context.iov_len = 64;
502 return generate_smb3signingkey(ses, &triplet);
506 smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
509 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
510 unsigned char *sigptr = smb3_signature;
511 struct kvec *iov = rqst->rq_iov;
512 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base;
513 struct shash_desc *shash = &server->secmech.sdesccmacaes->shash;
514 struct smb_rqst drqst;
515 u8 key[SMB3_SIGN_KEY_SIZE];
517 rc = smb2_get_sign_key(shdr->SessionId, server, key);
521 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
522 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
524 rc = crypto_shash_setkey(server->secmech.cmacaes,
525 key, SMB2_CMACAES_SIZE);
527 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
532 * we already allocate sdesccmacaes when we init smb3 signing key,
533 * so unlike smb2 case we do not have to check here if secmech are
536 rc = crypto_shash_init(shash);
538 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
543 * For SMB2+, __cifs_calc_signature() expects to sign only the actual
544 * data, that is, iov[0] should not contain a rfc1002 length.
546 * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to
547 * __cifs_calc_signature().
550 if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) {
551 rc = crypto_shash_update(shash, iov[0].iov_base,
554 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
562 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
564 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
569 /* must be called with server->srv_mutex held */
571 smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
574 struct smb2_sync_hdr *shdr;
575 struct smb2_sess_setup_req *ssr;
579 shdr = (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
580 ssr = (struct smb2_sess_setup_req *)shdr;
582 is_binding = shdr->Command == SMB2_SESSION_SETUP &&
583 (ssr->Flags & SMB2_SESSION_REQ_FLAG_BINDING);
584 is_signed = shdr->Flags & SMB2_FLAGS_SIGNED;
588 if (server->tcpStatus == CifsNeedNegotiate)
590 if (!is_binding && !server->session_estab) {
591 strncpy(shdr->Signature, "BSRSPYL", 8);
595 rc = server->ops->calc_signature(rqst, server);
601 smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
604 char server_response_sig[16];
605 struct smb2_sync_hdr *shdr =
606 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
608 if ((shdr->Command == SMB2_NEGOTIATE) ||
609 (shdr->Command == SMB2_SESSION_SETUP) ||
610 (shdr->Command == SMB2_OPLOCK_BREAK) ||
611 server->ignore_signature ||
612 (!server->session_estab))
616 * BB what if signatures are supposed to be on for session but
617 * server does not send one? BB
620 /* Do not need to verify session setups with signature "BSRSPYL " */
621 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
622 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
626 * Save off the origiginal signature so we can modify the smb and check
627 * our calculated signature against what the server sent.
629 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
631 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
633 mutex_lock(&server->srv_mutex);
634 rc = server->ops->calc_signature(rqst, server);
635 mutex_unlock(&server->srv_mutex);
640 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE))
647 * Set message id for the request. Should be called after wait_for_free_request
648 * and when srv_mutex is held.
651 smb2_seq_num_into_buf(struct TCP_Server_Info *server,
652 struct smb2_sync_hdr *shdr)
654 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
656 shdr->MessageId = get_next_mid64(server);
657 /* skip message numbers according to CreditCharge field */
658 for (i = 1; i < num; i++)
659 get_next_mid(server);
662 static struct mid_q_entry *
663 smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr,
664 struct TCP_Server_Info *server)
666 struct mid_q_entry *temp;
667 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
669 if (server == NULL) {
670 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
674 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
675 memset(temp, 0, sizeof(struct mid_q_entry));
676 kref_init(&temp->refcount);
677 temp->mid = le64_to_cpu(shdr->MessageId);
678 temp->credits = credits > 0 ? credits : 1;
679 temp->pid = current->pid;
680 temp->command = shdr->Command; /* Always LE */
681 temp->when_alloc = jiffies;
682 temp->server = server;
685 * The default is for the mid to be synchronous, so the
686 * default callback just wakes up the current task.
688 get_task_struct(current);
689 temp->creator = current;
690 temp->callback = cifs_wake_up_task;
691 temp->callback_data = current;
693 atomic_inc(&midCount);
694 temp->mid_state = MID_REQUEST_ALLOCATED;
695 trace_smb3_cmd_enter(shdr->TreeId, shdr->SessionId,
696 le16_to_cpu(shdr->Command), temp->mid);
701 smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
702 struct smb2_sync_hdr *shdr, struct mid_q_entry **mid)
704 if (server->tcpStatus == CifsExiting)
707 if (server->tcpStatus == CifsNeedReconnect) {
708 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
712 if (server->tcpStatus == CifsNeedNegotiate &&
713 shdr->Command != SMB2_NEGOTIATE)
716 if (ses->status == CifsNew) {
717 if ((shdr->Command != SMB2_SESSION_SETUP) &&
718 (shdr->Command != SMB2_NEGOTIATE))
720 /* else ok - we are setting up session */
723 if (ses->status == CifsExiting) {
724 if (shdr->Command != SMB2_LOGOFF)
726 /* else ok - we are shutting down the session */
729 *mid = smb2_mid_entry_alloc(shdr, server);
732 spin_lock(&GlobalMid_Lock);
733 list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
734 spin_unlock(&GlobalMid_Lock);
740 smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
743 unsigned int len = mid->resp_buf_size;
745 struct smb_rqst rqst = { .rq_iov = iov,
748 iov[0].iov_base = (char *)mid->resp_buf;
749 iov[0].iov_len = len;
751 dump_smb(mid->resp_buf, min_t(u32, 80, len));
752 /* convert the length into a more usable form */
753 if (len > 24 && server->sign && !mid->decrypted) {
756 rc = smb2_verify_signature(&rqst, server);
758 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
762 return map_smb2_to_linux_error(mid->resp_buf, log_error);
766 smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
767 struct smb_rqst *rqst)
770 struct smb2_sync_hdr *shdr =
771 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
772 struct mid_q_entry *mid;
774 smb2_seq_num_into_buf(server, shdr);
776 rc = smb2_get_mid_entry(ses, server, shdr, &mid);
778 revert_current_mid_from_hdr(server, shdr);
782 rc = smb2_sign_rqst(rqst, server);
784 revert_current_mid_from_hdr(server, shdr);
785 cifs_delete_mid(mid);
793 smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
796 struct smb2_sync_hdr *shdr =
797 (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base;
798 struct mid_q_entry *mid;
800 if (server->tcpStatus == CifsNeedNegotiate &&
801 shdr->Command != SMB2_NEGOTIATE)
802 return ERR_PTR(-EAGAIN);
804 smb2_seq_num_into_buf(server, shdr);
806 mid = smb2_mid_entry_alloc(shdr, server);
808 revert_current_mid_from_hdr(server, shdr);
809 return ERR_PTR(-ENOMEM);
812 rc = smb2_sign_rqst(rqst, server);
814 revert_current_mid_from_hdr(server, shdr);
815 DeleteMidQEntry(mid);
823 smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
825 struct crypto_aead *tfm;
827 if (!server->secmech.ccmaesencrypt) {
828 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
829 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
831 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
833 cifs_server_dbg(VFS, "%s: Failed to alloc encrypt aead\n",
837 server->secmech.ccmaesencrypt = tfm;
840 if (!server->secmech.ccmaesdecrypt) {
841 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
842 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
844 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
846 crypto_free_aead(server->secmech.ccmaesencrypt);
847 server->secmech.ccmaesencrypt = NULL;
848 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
852 server->secmech.ccmaesdecrypt = tfm;