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
);
73 #ifdef CONFIG_CIFS_SMB311
75 smb311_crypto_shash_allocate(struct TCP_Server_Info
*server
)
77 struct cifs_secmech
*p
= &server
->secmech
;
80 rc
= cifs_alloc_hash("hmac(sha256)",
86 rc
= cifs_alloc_hash("cmac(aes)", &p
->cmacaes
, &p
->sdesccmacaes
);
90 rc
= cifs_alloc_hash("sha512", &p
->sha512
, &p
->sdescsha512
);
97 cifs_free_hash(&p
->cmacaes
, &p
->sdesccmacaes
);
98 cifs_free_hash(&p
->hmacsha256
, &p
->sdeschmacsha256
);
103 static struct cifs_ses
*
104 smb2_find_smb_ses_unlocked(struct TCP_Server_Info
*server
, __u64 ses_id
)
106 struct cifs_ses
*ses
;
108 list_for_each_entry(ses
, &server
->smb_ses_list
, smb_ses_list
) {
109 if (ses
->Suid
!= ses_id
)
118 smb2_find_smb_ses(struct TCP_Server_Info
*server
, __u64 ses_id
)
120 struct cifs_ses
*ses
;
122 spin_lock(&cifs_tcp_ses_lock
);
123 ses
= smb2_find_smb_ses_unlocked(server
, ses_id
);
124 spin_unlock(&cifs_tcp_ses_lock
);
129 static struct cifs_tcon
*
130 smb2_find_smb_sess_tcon_unlocked(struct cifs_ses
*ses
, __u32 tid
)
132 struct cifs_tcon
*tcon
;
134 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
135 if (tcon
->tid
!= tid
)
145 * Obtain tcon corresponding to the tid in the given
150 smb2_find_smb_tcon(struct TCP_Server_Info
*server
, __u64 ses_id
, __u32 tid
)
152 struct cifs_ses
*ses
;
153 struct cifs_tcon
*tcon
;
155 spin_lock(&cifs_tcp_ses_lock
);
156 ses
= smb2_find_smb_ses_unlocked(server
, ses_id
);
158 spin_unlock(&cifs_tcp_ses_lock
);
161 tcon
= smb2_find_smb_sess_tcon_unlocked(ses
, tid
);
162 spin_unlock(&cifs_tcp_ses_lock
);
168 smb2_calc_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
171 unsigned char smb2_signature
[SMB2_HMACSHA256_SIZE
];
172 unsigned char *sigptr
= smb2_signature
;
173 struct kvec
*iov
= rqst
->rq_iov
;
174 int iov_hdr_index
= rqst
->rq_nvec
> 1 ? 1 : 0;
175 struct smb2_sync_hdr
*shdr
=
176 (struct smb2_sync_hdr
*)iov
[iov_hdr_index
].iov_base
;
177 struct cifs_ses
*ses
;
179 ses
= smb2_find_smb_ses(server
, shdr
->SessionId
);
181 cifs_dbg(VFS
, "%s: Could not find session\n", __func__
);
185 memset(smb2_signature
, 0x0, SMB2_HMACSHA256_SIZE
);
186 memset(shdr
->Signature
, 0x0, SMB2_SIGNATURE_SIZE
);
188 rc
= smb2_crypto_shash_allocate(server
);
190 cifs_dbg(VFS
, "%s: shah256 alloc failed\n", __func__
);
194 rc
= crypto_shash_setkey(server
->secmech
.hmacsha256
,
195 ses
->auth_key
.response
, SMB2_NTLMV2_SESSKEY_SIZE
);
197 cifs_dbg(VFS
, "%s: Could not update with response\n", __func__
);
201 rc
= crypto_shash_init(&server
->secmech
.sdeschmacsha256
->shash
);
203 cifs_dbg(VFS
, "%s: Could not init sha256", __func__
);
207 rc
= __cifs_calc_signature(rqst
, iov_hdr_index
, server
, sigptr
,
208 &server
->secmech
.sdeschmacsha256
->shash
);
211 memcpy(shdr
->Signature
, sigptr
, SMB2_SIGNATURE_SIZE
);
216 static int generate_key(struct cifs_ses
*ses
, struct kvec label
,
217 struct kvec context
, __u8
*key
, unsigned int key_size
)
219 unsigned char zero
= 0x0;
220 __u8 i
[4] = {0, 0, 0, 1};
221 __u8 L
[4] = {0, 0, 0, 128};
223 unsigned char prfhash
[SMB2_HMACSHA256_SIZE
];
224 unsigned char *hashptr
= prfhash
;
226 memset(prfhash
, 0x0, SMB2_HMACSHA256_SIZE
);
227 memset(key
, 0x0, key_size
);
229 rc
= smb3_crypto_shash_allocate(ses
->server
);
231 cifs_dbg(VFS
, "%s: crypto alloc failed\n", __func__
);
232 goto smb3signkey_ret
;
235 rc
= crypto_shash_setkey(ses
->server
->secmech
.hmacsha256
,
236 ses
->auth_key
.response
, SMB2_NTLMV2_SESSKEY_SIZE
);
238 cifs_dbg(VFS
, "%s: Could not set with session key\n", __func__
);
239 goto smb3signkey_ret
;
242 rc
= crypto_shash_init(&ses
->server
->secmech
.sdeschmacsha256
->shash
);
244 cifs_dbg(VFS
, "%s: Could not init sign hmac\n", __func__
);
245 goto smb3signkey_ret
;
248 rc
= crypto_shash_update(&ses
->server
->secmech
.sdeschmacsha256
->shash
,
251 cifs_dbg(VFS
, "%s: Could not update with n\n", __func__
);
252 goto smb3signkey_ret
;
255 rc
= crypto_shash_update(&ses
->server
->secmech
.sdeschmacsha256
->shash
,
256 label
.iov_base
, label
.iov_len
);
258 cifs_dbg(VFS
, "%s: Could not update with label\n", __func__
);
259 goto smb3signkey_ret
;
262 rc
= crypto_shash_update(&ses
->server
->secmech
.sdeschmacsha256
->shash
,
265 cifs_dbg(VFS
, "%s: Could not update with zero\n", __func__
);
266 goto smb3signkey_ret
;
269 rc
= crypto_shash_update(&ses
->server
->secmech
.sdeschmacsha256
->shash
,
270 context
.iov_base
, context
.iov_len
);
272 cifs_dbg(VFS
, "%s: Could not update with context\n", __func__
);
273 goto smb3signkey_ret
;
276 rc
= crypto_shash_update(&ses
->server
->secmech
.sdeschmacsha256
->shash
,
279 cifs_dbg(VFS
, "%s: Could not update with L\n", __func__
);
280 goto smb3signkey_ret
;
283 rc
= crypto_shash_final(&ses
->server
->secmech
.sdeschmacsha256
->shash
,
286 cifs_dbg(VFS
, "%s: Could not generate sha256 hash\n", __func__
);
287 goto smb3signkey_ret
;
290 memcpy(key
, hashptr
, key_size
);
301 struct derivation_triplet
{
302 struct derivation signing
;
303 struct derivation encryption
;
304 struct derivation decryption
;
308 generate_smb3signingkey(struct cifs_ses
*ses
,
309 const struct derivation_triplet
*ptriplet
)
313 rc
= generate_key(ses
, ptriplet
->signing
.label
,
314 ptriplet
->signing
.context
, ses
->smb3signingkey
,
319 rc
= generate_key(ses
, ptriplet
->encryption
.label
,
320 ptriplet
->encryption
.context
, ses
->smb3encryptionkey
,
325 rc
= generate_key(ses
, ptriplet
->decryption
.label
,
326 ptriplet
->decryption
.context
,
327 ses
->smb3decryptionkey
, SMB3_SIGN_KEY_SIZE
);
332 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
333 cifs_dbg(VFS
, "%s: dumping generated AES session keys\n", __func__
);
335 * The session id is opaque in terms of endianness, so we can't
336 * print it as a long long. we dump it as we got it on the wire
338 cifs_dbg(VFS
, "Session Id %*ph\n", (int)sizeof(ses
->Suid
),
340 cifs_dbg(VFS
, "Session Key %*ph\n",
341 SMB2_NTLMV2_SESSKEY_SIZE
, ses
->auth_key
.response
);
342 cifs_dbg(VFS
, "Signing Key %*ph\n",
343 SMB3_SIGN_KEY_SIZE
, ses
->smb3signingkey
);
344 cifs_dbg(VFS
, "ServerIn Key %*ph\n",
345 SMB3_SIGN_KEY_SIZE
, ses
->smb3encryptionkey
);
346 cifs_dbg(VFS
, "ServerOut Key %*ph\n",
347 SMB3_SIGN_KEY_SIZE
, ses
->smb3decryptionkey
);
353 generate_smb30signingkey(struct cifs_ses
*ses
)
356 struct derivation_triplet triplet
;
357 struct derivation
*d
;
359 d
= &triplet
.signing
;
360 d
->label
.iov_base
= "SMB2AESCMAC";
361 d
->label
.iov_len
= 12;
362 d
->context
.iov_base
= "SmbSign";
363 d
->context
.iov_len
= 8;
365 d
= &triplet
.encryption
;
366 d
->label
.iov_base
= "SMB2AESCCM";
367 d
->label
.iov_len
= 11;
368 d
->context
.iov_base
= "ServerIn ";
369 d
->context
.iov_len
= 10;
371 d
= &triplet
.decryption
;
372 d
->label
.iov_base
= "SMB2AESCCM";
373 d
->label
.iov_len
= 11;
374 d
->context
.iov_base
= "ServerOut";
375 d
->context
.iov_len
= 10;
377 return generate_smb3signingkey(ses
, &triplet
);
380 #ifdef CONFIG_CIFS_SMB311
382 generate_smb311signingkey(struct cifs_ses
*ses
)
385 struct derivation_triplet triplet
;
386 struct derivation
*d
;
388 d
= &triplet
.signing
;
389 d
->label
.iov_base
= "SMBSigningKey";
390 d
->label
.iov_len
= 14;
391 d
->context
.iov_base
= ses
->preauth_sha_hash
;
392 d
->context
.iov_len
= 64;
394 d
= &triplet
.encryption
;
395 d
->label
.iov_base
= "SMBC2SCipherKey";
396 d
->label
.iov_len
= 16;
397 d
->context
.iov_base
= ses
->preauth_sha_hash
;
398 d
->context
.iov_len
= 64;
400 d
= &triplet
.decryption
;
401 d
->label
.iov_base
= "SMBS2CCipherKey";
402 d
->label
.iov_len
= 16;
403 d
->context
.iov_base
= ses
->preauth_sha_hash
;
404 d
->context
.iov_len
= 64;
406 return generate_smb3signingkey(ses
, &triplet
);
411 smb3_calc_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
414 unsigned char smb3_signature
[SMB2_CMACAES_SIZE
];
415 unsigned char *sigptr
= smb3_signature
;
416 struct kvec
*iov
= rqst
->rq_iov
;
417 int iov_hdr_index
= rqst
->rq_nvec
> 1 ? 1 : 0;
418 struct smb2_sync_hdr
*shdr
=
419 (struct smb2_sync_hdr
*)iov
[iov_hdr_index
].iov_base
;
420 struct cifs_ses
*ses
;
422 ses
= smb2_find_smb_ses(server
, shdr
->SessionId
);
424 cifs_dbg(VFS
, "%s: Could not find session\n", __func__
);
428 memset(smb3_signature
, 0x0, SMB2_CMACAES_SIZE
);
429 memset(shdr
->Signature
, 0x0, SMB2_SIGNATURE_SIZE
);
431 rc
= crypto_shash_setkey(server
->secmech
.cmacaes
,
432 ses
->smb3signingkey
, SMB2_CMACAES_SIZE
);
435 cifs_dbg(VFS
, "%s: Could not set key for cmac aes\n", __func__
);
440 * we already allocate sdesccmacaes when we init smb3 signing key,
441 * so unlike smb2 case we do not have to check here if secmech are
444 rc
= crypto_shash_init(&server
->secmech
.sdesccmacaes
->shash
);
446 cifs_dbg(VFS
, "%s: Could not init cmac aes\n", __func__
);
450 rc
= __cifs_calc_signature(rqst
, iov_hdr_index
, server
, sigptr
,
451 &server
->secmech
.sdesccmacaes
->shash
);
454 memcpy(shdr
->Signature
, sigptr
, SMB2_SIGNATURE_SIZE
);
459 /* must be called with server->srv_mutex held */
461 smb2_sign_rqst(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
464 struct smb2_sync_hdr
*shdr
=
465 (struct smb2_sync_hdr
*)rqst
->rq_iov
[1].iov_base
;
467 if (!(shdr
->Flags
& SMB2_FLAGS_SIGNED
) ||
468 server
->tcpStatus
== CifsNeedNegotiate
)
471 if (!server
->session_estab
) {
472 strncpy(shdr
->Signature
, "BSRSPYL", 8);
476 rc
= server
->ops
->calc_signature(rqst
, server
);
482 smb2_verify_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
485 char server_response_sig
[16];
486 struct smb2_sync_hdr
*shdr
=
487 (struct smb2_sync_hdr
*)rqst
->rq_iov
[0].iov_base
;
489 if ((shdr
->Command
== SMB2_NEGOTIATE
) ||
490 (shdr
->Command
== SMB2_SESSION_SETUP
) ||
491 (shdr
->Command
== SMB2_OPLOCK_BREAK
) ||
492 (!server
->session_estab
))
496 * BB what if signatures are supposed to be on for session but
497 * server does not send one? BB
500 /* Do not need to verify session setups with signature "BSRSPYL " */
501 if (memcmp(shdr
->Signature
, "BSRSPYL ", 8) == 0)
502 cifs_dbg(FYI
, "dummy signature received for smb command 0x%x\n",
506 * Save off the origiginal signature so we can modify the smb and check
507 * our calculated signature against what the server sent.
509 memcpy(server_response_sig
, shdr
->Signature
, SMB2_SIGNATURE_SIZE
);
511 memset(shdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
513 mutex_lock(&server
->srv_mutex
);
514 rc
= server
->ops
->calc_signature(rqst
, server
);
515 mutex_unlock(&server
->srv_mutex
);
520 if (memcmp(server_response_sig
, shdr
->Signature
, SMB2_SIGNATURE_SIZE
))
527 * Set message id for the request. Should be called after wait_for_free_request
528 * and when srv_mutex is held.
531 smb2_seq_num_into_buf(struct TCP_Server_Info
*server
,
532 struct smb2_sync_hdr
*shdr
)
534 unsigned int i
, num
= le16_to_cpu(shdr
->CreditCharge
);
536 shdr
->MessageId
= get_next_mid64(server
);
537 /* skip message numbers according to CreditCharge field */
538 for (i
= 1; i
< num
; i
++)
539 get_next_mid(server
);
542 static struct mid_q_entry
*
543 smb2_mid_entry_alloc(const struct smb2_sync_hdr
*shdr
,
544 struct TCP_Server_Info
*server
)
546 struct mid_q_entry
*temp
;
548 if (server
== NULL
) {
549 cifs_dbg(VFS
, "Null TCP session in smb2_mid_entry_alloc\n");
553 temp
= mempool_alloc(cifs_mid_poolp
, GFP_NOFS
);
554 memset(temp
, 0, sizeof(struct mid_q_entry
));
555 temp
->mid
= le64_to_cpu(shdr
->MessageId
);
556 temp
->pid
= current
->pid
;
557 temp
->command
= shdr
->Command
; /* Always LE */
558 temp
->when_alloc
= jiffies
;
559 temp
->server
= server
;
562 * The default is for the mid to be synchronous, so the
563 * default callback just wakes up the current task.
565 temp
->callback
= cifs_wake_up_task
;
566 temp
->callback_data
= current
;
568 atomic_inc(&midCount
);
569 temp
->mid_state
= MID_REQUEST_ALLOCATED
;
574 smb2_get_mid_entry(struct cifs_ses
*ses
, struct smb2_sync_hdr
*shdr
,
575 struct mid_q_entry
**mid
)
577 if (ses
->server
->tcpStatus
== CifsExiting
)
580 if (ses
->server
->tcpStatus
== CifsNeedReconnect
) {
581 cifs_dbg(FYI
, "tcp session dead - return to caller to retry\n");
585 if (ses
->status
== CifsNew
) {
586 if ((shdr
->Command
!= SMB2_SESSION_SETUP
) &&
587 (shdr
->Command
!= SMB2_NEGOTIATE
))
589 /* else ok - we are setting up session */
592 if (ses
->status
== CifsExiting
) {
593 if (shdr
->Command
!= SMB2_LOGOFF
)
595 /* else ok - we are shutting down the session */
598 *mid
= smb2_mid_entry_alloc(shdr
, ses
->server
);
601 spin_lock(&GlobalMid_Lock
);
602 list_add_tail(&(*mid
)->qhead
, &ses
->server
->pending_mid_q
);
603 spin_unlock(&GlobalMid_Lock
);
608 smb2_check_receive(struct mid_q_entry
*mid
, struct TCP_Server_Info
*server
,
611 unsigned int len
= mid
->resp_buf_size
;
613 struct smb_rqst rqst
= { .rq_iov
= iov
,
616 iov
[0].iov_base
= (char *)mid
->resp_buf
;
617 iov
[0].iov_len
= len
;
619 dump_smb(mid
->resp_buf
, min_t(u32
, 80, len
));
620 /* convert the length into a more usable form */
621 if (len
> 24 && server
->sign
&& !mid
->decrypted
) {
624 rc
= smb2_verify_signature(&rqst
, server
);
626 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
630 return map_smb2_to_linux_error(mid
->resp_buf
, log_error
);
634 smb2_setup_request(struct cifs_ses
*ses
, struct smb_rqst
*rqst
)
637 struct smb2_sync_hdr
*shdr
=
638 (struct smb2_sync_hdr
*)rqst
->rq_iov
[1].iov_base
;
639 struct mid_q_entry
*mid
;
641 smb2_seq_num_into_buf(ses
->server
, shdr
);
643 rc
= smb2_get_mid_entry(ses
, shdr
, &mid
);
646 rc
= smb2_sign_rqst(rqst
, ses
->server
);
648 cifs_delete_mid(mid
);
655 smb2_setup_async_request(struct TCP_Server_Info
*server
, struct smb_rqst
*rqst
)
658 struct smb2_sync_hdr
*shdr
=
659 (struct smb2_sync_hdr
*)rqst
->rq_iov
[1].iov_base
;
660 struct mid_q_entry
*mid
;
662 smb2_seq_num_into_buf(server
, shdr
);
664 mid
= smb2_mid_entry_alloc(shdr
, server
);
666 return ERR_PTR(-ENOMEM
);
668 rc
= smb2_sign_rqst(rqst
, server
);
670 DeleteMidQEntry(mid
);
678 smb3_crypto_aead_allocate(struct TCP_Server_Info
*server
)
680 struct crypto_aead
*tfm
;
682 if (!server
->secmech
.ccmaesencrypt
) {
683 tfm
= crypto_alloc_aead("ccm(aes)", 0, 0);
685 cifs_dbg(VFS
, "%s: Failed to alloc encrypt aead\n",
689 server
->secmech
.ccmaesencrypt
= tfm
;
692 if (!server
->secmech
.ccmaesdecrypt
) {
693 tfm
= crypto_alloc_aead("ccm(aes)", 0, 0);
695 crypto_free_aead(server
->secmech
.ccmaesencrypt
);
696 server
->secmech
.ccmaesencrypt
= NULL
;
697 cifs_dbg(VFS
, "%s: Failed to alloc decrypt aead\n",
701 server
->secmech
.ccmaesdecrypt
= tfm
;