blob: 87563ee58d306942bf73ac50a2c596db6e8c423d [file] [log] [blame]
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001/*
2 * fs/cifs/smb2transport.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002, 2011
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006
8 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 *
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.
14 *
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.
19 *
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
23 */
24
25#include <linux/fs.h>
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>
Jeff Laytonfb308a62012-09-18 16:20:35 -070033#include <linux/highmem.h>
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040034#include "smb2pdu.h"
35#include "cifsglob.h"
36#include "cifsproto.h"
37#include "smb2proto.h"
38#include "cifs_debug.h"
39#include "smb2status.h"
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070040#include "smb2glob.h"
41
Steve French38107d42012-12-08 22:08:06 -060042int
Jeff Layton0b688cf2012-09-18 16:20:34 -070043smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070044{
45 int i, rc;
46 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
47 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -070048 struct kvec *iov = rqst->rq_iov;
49 int n_vec = rqst->rq_nvec;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070050 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
51
52 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
53 memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
54
55 rc = crypto_shash_setkey(server->secmech.hmacsha256,
56 server->session_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
57 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050058 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070059 return rc;
60 }
61
62 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
63 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050064 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070065 return rc;
66 }
67
68 for (i = 0; i < n_vec; i++) {
69 if (iov[i].iov_len == 0)
70 continue;
71 if (iov[i].iov_base == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -050072 cifs_dbg(VFS, "null iovec entry\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070073 return -EIO;
74 }
75 /*
76 * The first entry includes a length field (which does not get
77 * signed that occupies the first 4 bytes before the header).
78 */
79 if (i == 0) {
80 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
81 break; /* nothing to sign or corrupt header */
82 rc =
83 crypto_shash_update(
84 &server->secmech.sdeschmacsha256->shash,
85 iov[i].iov_base + 4, iov[i].iov_len - 4);
86 } else {
87 rc =
88 crypto_shash_update(
89 &server->secmech.sdeschmacsha256->shash,
90 iov[i].iov_base, iov[i].iov_len);
91 }
92 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050093 cifs_dbg(VFS, "%s: Could not update with payload\n",
94 __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070095 return rc;
96 }
97 }
98
Jeff Laytonfb308a62012-09-18 16:20:35 -070099 /* now hash over the rq_pages array */
100 for (i = 0; i < rqst->rq_npages; i++) {
101 struct kvec p_iov;
102
103 cifs_rqst_page_to_kvec(rqst, i, &p_iov);
104 crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
105 p_iov.iov_base, p_iov.iov_len);
106 kunmap(rqst->rq_pages[i]);
107 }
108
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700109 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
110 sigptr);
111 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500112 cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700113
114 memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
115
116 return rc;
117}
118
Steve French38107d42012-12-08 22:08:06 -0600119int
Steve French429b46f2013-06-26 23:45:05 -0500120generate_smb3signingkey(struct TCP_Server_Info *server)
121{
122 unsigned char zero = 0x0;
123 __u8 i[4] = {0, 0, 0, 1};
124 __u8 L[4] = {0, 0, 0, 128};
125 int rc = 0;
126 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
127 unsigned char *hashptr = prfhash;
128
129 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
130 memset(server->smb3signingkey, 0x0, SMB3_SIGNKEY_SIZE);
131
132 rc = crypto_shash_setkey(server->secmech.hmacsha256,
133 server->session_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
134 if (rc) {
135 cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
136 goto smb3signkey_ret;
137 }
138
139 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
140 if (rc) {
141 cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
142 goto smb3signkey_ret;
143 }
144
145 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
146 i, 4);
147 if (rc) {
148 cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
149 goto smb3signkey_ret;
150 }
151
152 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
153 "SMB2AESCMAC", 12);
154 if (rc) {
155 cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
156 goto smb3signkey_ret;
157 }
158
159 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
160 &zero, 1);
161 if (rc) {
162 cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
163 goto smb3signkey_ret;
164 }
165
166 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
167 "SmbSign", 8);
168 if (rc) {
169 cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
170 goto smb3signkey_ret;
171 }
172
173 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
174 L, 4);
175 if (rc) {
176 cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
177 goto smb3signkey_ret;
178 }
179
180 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
181 hashptr);
182 if (rc) {
183 cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
184 goto smb3signkey_ret;
185 }
186
187 memcpy(server->smb3signingkey, hashptr, SMB3_SIGNKEY_SIZE);
188
189smb3signkey_ret:
190 return rc;
191}
192
193int
Steve French38107d42012-12-08 22:08:06 -0600194smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
195{
Steve French429b46f2013-06-26 23:45:05 -0500196 int i, rc;
197 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
198 unsigned char *sigptr = smb3_signature;
199 struct kvec *iov = rqst->rq_iov;
200 int n_vec = rqst->rq_nvec;
201 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
202
203 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
204 memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
205
206 rc = crypto_shash_setkey(server->secmech.cmacaes,
207 server->smb3signingkey, SMB2_CMACAES_SIZE);
208 if (rc) {
209 cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
210 return rc;
211 }
212
213 rc = crypto_shash_init(&server->secmech.sdesccmacaes->shash);
214 if (rc) {
215 cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
216 return rc;
217 }
218
219 for (i = 0; i < n_vec; i++) {
220 if (iov[i].iov_len == 0)
221 continue;
222 if (iov[i].iov_base == NULL) {
223 cifs_dbg(VFS, "null iovec entry");
224 return -EIO;
225 }
226 /*
227 * The first entry includes a length field (which does not get
228 * signed that occupies the first 4 bytes before the header).
229 */
230 if (i == 0) {
231 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
232 break; /* nothing to sign or corrupt header */
233 rc =
234 crypto_shash_update(
235 &server->secmech.sdesccmacaes->shash,
236 iov[i].iov_base + 4, iov[i].iov_len - 4);
237 } else {
238 rc =
239 crypto_shash_update(
240 &server->secmech.sdesccmacaes->shash,
241 iov[i].iov_base, iov[i].iov_len);
242 }
243 if (rc) {
244 cifs_dbg(VFS, "%s: Couldn't update cmac aes with payload\n",
245 __func__);
246 return rc;
247 }
248 }
249
250 /* now hash over the rq_pages array */
251 for (i = 0; i < rqst->rq_npages; i++) {
252 struct kvec p_iov;
253
254 cifs_rqst_page_to_kvec(rqst, i, &p_iov);
255 crypto_shash_update(&server->secmech.sdesccmacaes->shash,
256 p_iov.iov_base, p_iov.iov_len);
257 kunmap(rqst->rq_pages[i]);
258 }
259
260 rc = crypto_shash_final(&server->secmech.sdesccmacaes->shash,
261 sigptr);
262 if (rc)
263 cifs_dbg(VFS, "%s: Could not generate cmac aes\n", __func__);
264
265 memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
266
267 return rc;
Steve French38107d42012-12-08 22:08:06 -0600268}
269
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700270/* must be called with server->srv_mutex held */
271static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700272smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700273{
274 int rc = 0;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700275 struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700276
277 if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
278 server->tcpStatus == CifsNeedNegotiate)
279 return rc;
280
281 if (!server->session_estab) {
282 strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
283 return rc;
284 }
285
Steve French38107d42012-12-08 22:08:06 -0600286 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700287
288 return rc;
289}
290
291int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700292smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700293{
294 unsigned int rc;
295 char server_response_sig[16];
Jeff Layton0b688cf2012-09-18 16:20:34 -0700296 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700297
298 if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
299 (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
300 (!server->session_estab))
301 return 0;
302
303 /*
304 * BB what if signatures are supposed to be on for session but
305 * server does not send one? BB
306 */
307
308 /* Do not need to verify session setups with signature "BSRSPYL " */
309 if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500310 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
311 smb2_pdu->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700312
313 /*
314 * Save off the origiginal signature so we can modify the smb and check
315 * our calculated signature against what the server sent.
316 */
317 memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
318
319 memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
320
321 mutex_lock(&server->srv_mutex);
Steve French38107d42012-12-08 22:08:06 -0600322 rc = server->ops->calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700323 mutex_unlock(&server->srv_mutex);
324
325 if (rc)
326 return rc;
327
328 if (memcmp(server_response_sig, smb2_pdu->Signature,
329 SMB2_SIGNATURE_SIZE))
330 return -EACCES;
331 else
332 return 0;
333}
334
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400335/*
336 * Set message id for the request. Should be called after wait_for_free_request
337 * and when srv_mutex is held.
338 */
339static inline void
340smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
341{
342 hdr->MessageId = get_next_mid(server);
343}
344
345static struct mid_q_entry *
346smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
347 struct TCP_Server_Info *server)
348{
349 struct mid_q_entry *temp;
350
351 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500352 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400353 return NULL;
354 }
355
356 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
357 if (temp == NULL)
358 return temp;
359 else {
360 memset(temp, 0, sizeof(struct mid_q_entry));
361 temp->mid = smb_buffer->MessageId; /* always LE */
362 temp->pid = current->pid;
363 temp->command = smb_buffer->Command; /* Always LE */
364 temp->when_alloc = jiffies;
365 temp->server = server;
366
367 /*
368 * The default is for the mid to be synchronous, so the
369 * default callback just wakes up the current task.
370 */
371 temp->callback = cifs_wake_up_task;
372 temp->callback_data = current;
373 }
374
375 atomic_inc(&midCount);
376 temp->mid_state = MID_REQUEST_ALLOCATED;
377 return temp;
378}
379
380static int
381smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
382 struct mid_q_entry **mid)
383{
384 if (ses->server->tcpStatus == CifsExiting)
385 return -ENOENT;
386
387 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500388 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400389 return -EAGAIN;
390 }
391
392 if (ses->status != CifsGood) {
393 /* check if SMB2 session is bad because we are setting it up */
394 if ((buf->Command != SMB2_SESSION_SETUP) &&
395 (buf->Command != SMB2_NEGOTIATE))
396 return -EAGAIN;
397 /* else ok - we are setting up session */
398 }
399 *mid = smb2_mid_entry_alloc(buf, ses->server);
400 if (*mid == NULL)
401 return -ENOMEM;
402 spin_lock(&GlobalMid_Lock);
403 list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
404 spin_unlock(&GlobalMid_Lock);
405 return 0;
406}
407
408int
409smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
410 bool log_error)
411{
412 unsigned int len = get_rfc1002_length(mid->resp_buf);
Jeff Layton0b688cf2012-09-18 16:20:34 -0700413 struct kvec iov;
414 struct smb_rqst rqst = { .rq_iov = &iov,
415 .rq_nvec = 1 };
416
417 iov.iov_base = (char *)mid->resp_buf;
418 iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400419
420 dump_smb(mid->resp_buf, min_t(u32, 80, len));
421 /* convert the length into a more usable form */
Jeff Layton38d77c52013-05-26 07:01:00 -0400422 if (len > 24 && server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700423 int rc;
424
Jeff Layton0b688cf2012-09-18 16:20:34 -0700425 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700426 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500427 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
428 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700429 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400430
431 return map_smb2_to_linux_error(mid->resp_buf, log_error);
432}
433
Jeff Laytonfec344e2012-09-18 16:20:35 -0700434struct mid_q_entry *
435smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400436{
437 int rc;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700438 struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400439 struct mid_q_entry *mid;
440
441 smb2_seq_num_into_buf(ses->server, hdr);
442
443 rc = smb2_get_mid_entry(ses, hdr, &mid);
444 if (rc)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700445 return ERR_PTR(rc);
446 rc = smb2_sign_rqst(rqst, ses->server);
447 if (rc) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700448 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700449 return ERR_PTR(rc);
450 }
451 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400452}
453
Jeff Laytonfec344e2012-09-18 16:20:35 -0700454struct mid_q_entry *
455smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400456{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700457 int rc;
458 struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400459 struct mid_q_entry *mid;
460
461 smb2_seq_num_into_buf(server, hdr);
462
463 mid = smb2_mid_entry_alloc(hdr, server);
464 if (mid == NULL)
Jeff Laytonfec344e2012-09-18 16:20:35 -0700465 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400466
Jeff Laytonfec344e2012-09-18 16:20:35 -0700467 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400468 if (rc) {
469 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700470 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700471 }
472
Jeff Laytonfec344e2012-09-18 16:20:35 -0700473 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400474}