blob: 1850d9ec3c9042f355bc0f6069079f65c1854e1b [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>
33#include "smb2pdu.h"
34#include "cifsglob.h"
35#include "cifsproto.h"
36#include "smb2proto.h"
37#include "cifs_debug.h"
38#include "smb2status.h"
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070039#include "smb2glob.h"
40
41static int
Jeff Layton0b688cf2012-09-18 16:20:34 -070042smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070043{
44 int i, rc;
45 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
46 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -070047 struct kvec *iov = rqst->rq_iov;
48 int n_vec = rqst->rq_nvec;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070049 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
50
51 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
52 memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
53
54 rc = crypto_shash_setkey(server->secmech.hmacsha256,
55 server->session_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
56 if (rc) {
57 cERROR(1, "%s: Could not update with response\n", __func__);
58 return rc;
59 }
60
61 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
62 if (rc) {
63 cERROR(1, "%s: Could not init md5\n", __func__);
64 return rc;
65 }
66
67 for (i = 0; i < n_vec; i++) {
68 if (iov[i].iov_len == 0)
69 continue;
70 if (iov[i].iov_base == NULL) {
71 cERROR(1, "null iovec entry");
72 return -EIO;
73 }
74 /*
75 * The first entry includes a length field (which does not get
76 * signed that occupies the first 4 bytes before the header).
77 */
78 if (i == 0) {
79 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
80 break; /* nothing to sign or corrupt header */
81 rc =
82 crypto_shash_update(
83 &server->secmech.sdeschmacsha256->shash,
84 iov[i].iov_base + 4, iov[i].iov_len - 4);
85 } else {
86 rc =
87 crypto_shash_update(
88 &server->secmech.sdeschmacsha256->shash,
89 iov[i].iov_base, iov[i].iov_len);
90 }
91 if (rc) {
92 cERROR(1, "%s: Could not update with payload\n",
93 __func__);
94 return rc;
95 }
96 }
97
98 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
99 sigptr);
100 if (rc)
101 cERROR(1, "%s: Could not generate sha256 hash\n", __func__);
102
103 memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
104
105 return rc;
106}
107
108/* must be called with server->srv_mutex held */
109static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700110smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700111{
112 int rc = 0;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700113 struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700114
115 if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
116 server->tcpStatus == CifsNeedNegotiate)
117 return rc;
118
119 if (!server->session_estab) {
120 strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
121 return rc;
122 }
123
Jeff Layton0b688cf2012-09-18 16:20:34 -0700124 rc = smb2_calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700125
126 return rc;
127}
128
129int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700130smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700131{
132 unsigned int rc;
133 char server_response_sig[16];
Jeff Layton0b688cf2012-09-18 16:20:34 -0700134 struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700135
136 if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
137 (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
138 (!server->session_estab))
139 return 0;
140
141 /*
142 * BB what if signatures are supposed to be on for session but
143 * server does not send one? BB
144 */
145
146 /* Do not need to verify session setups with signature "BSRSPYL " */
147 if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
148 cFYI(1, "dummy signature received for smb command 0x%x",
149 smb2_pdu->Command);
150
151 /*
152 * Save off the origiginal signature so we can modify the smb and check
153 * our calculated signature against what the server sent.
154 */
155 memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
156
157 memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
158
159 mutex_lock(&server->srv_mutex);
Jeff Layton0b688cf2012-09-18 16:20:34 -0700160 rc = smb2_calc_signature(rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700161 mutex_unlock(&server->srv_mutex);
162
163 if (rc)
164 return rc;
165
166 if (memcmp(server_response_sig, smb2_pdu->Signature,
167 SMB2_SIGNATURE_SIZE))
168 return -EACCES;
169 else
170 return 0;
171}
172
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400173/*
174 * Set message id for the request. Should be called after wait_for_free_request
175 * and when srv_mutex is held.
176 */
177static inline void
178smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
179{
180 hdr->MessageId = get_next_mid(server);
181}
182
183static struct mid_q_entry *
184smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
185 struct TCP_Server_Info *server)
186{
187 struct mid_q_entry *temp;
188
189 if (server == NULL) {
190 cERROR(1, "Null TCP session in smb2_mid_entry_alloc");
191 return NULL;
192 }
193
194 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
195 if (temp == NULL)
196 return temp;
197 else {
198 memset(temp, 0, sizeof(struct mid_q_entry));
199 temp->mid = smb_buffer->MessageId; /* always LE */
200 temp->pid = current->pid;
201 temp->command = smb_buffer->Command; /* Always LE */
202 temp->when_alloc = jiffies;
203 temp->server = server;
204
205 /*
206 * The default is for the mid to be synchronous, so the
207 * default callback just wakes up the current task.
208 */
209 temp->callback = cifs_wake_up_task;
210 temp->callback_data = current;
211 }
212
213 atomic_inc(&midCount);
214 temp->mid_state = MID_REQUEST_ALLOCATED;
215 return temp;
216}
217
218static int
219smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
220 struct mid_q_entry **mid)
221{
222 if (ses->server->tcpStatus == CifsExiting)
223 return -ENOENT;
224
225 if (ses->server->tcpStatus == CifsNeedReconnect) {
226 cFYI(1, "tcp session dead - return to caller to retry");
227 return -EAGAIN;
228 }
229
230 if (ses->status != CifsGood) {
231 /* check if SMB2 session is bad because we are setting it up */
232 if ((buf->Command != SMB2_SESSION_SETUP) &&
233 (buf->Command != SMB2_NEGOTIATE))
234 return -EAGAIN;
235 /* else ok - we are setting up session */
236 }
237 *mid = smb2_mid_entry_alloc(buf, ses->server);
238 if (*mid == NULL)
239 return -ENOMEM;
240 spin_lock(&GlobalMid_Lock);
241 list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
242 spin_unlock(&GlobalMid_Lock);
243 return 0;
244}
245
246int
247smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
248 bool log_error)
249{
250 unsigned int len = get_rfc1002_length(mid->resp_buf);
Jeff Layton0b688cf2012-09-18 16:20:34 -0700251 struct kvec iov;
252 struct smb_rqst rqst = { .rq_iov = &iov,
253 .rq_nvec = 1 };
254
255 iov.iov_base = (char *)mid->resp_buf;
256 iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400257
258 dump_smb(mid->resp_buf, min_t(u32, 80, len));
259 /* convert the length into a more usable form */
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700260 if ((len > 24) &&
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400261 (server->sec_mode & (SECMODE_SIGN_REQUIRED|SECMODE_SIGN_ENABLED))) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700262 int rc;
263
Jeff Layton0b688cf2012-09-18 16:20:34 -0700264 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700265 if (rc)
266 cERROR(1, "SMB signature verification returned error = "
267 "%d", rc);
268 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400269
270 return map_smb2_to_linux_error(mid->resp_buf, log_error);
271}
272
273int
274smb2_setup_request(struct cifs_ses *ses, struct kvec *iov,
275 unsigned int nvec, struct mid_q_entry **ret_mid)
276{
277 int rc;
278 struct smb2_hdr *hdr = (struct smb2_hdr *)iov[0].iov_base;
279 struct mid_q_entry *mid;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700280 struct smb_rqst rqst = { .rq_iov = iov,
281 .rq_nvec = nvec };
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400282
283 smb2_seq_num_into_buf(ses->server, hdr);
284
285 rc = smb2_get_mid_entry(ses, hdr, &mid);
286 if (rc)
287 return rc;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700288 rc = smb2_sign_rqst(&rqst, ses->server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400289 if (rc)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700290 cifs_delete_mid(mid);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400291 *ret_mid = mid;
292 return rc;
293}
294
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400295int
296smb2_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
297 unsigned int nvec, struct mid_q_entry **ret_mid)
298{
299 int rc = 0;
300 struct smb2_hdr *hdr = (struct smb2_hdr *)iov[0].iov_base;
301 struct mid_q_entry *mid;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700302 struct smb_rqst rqst = { .rq_iov = iov,
303 .rq_nvec = nvec };
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400304
305 smb2_seq_num_into_buf(server, hdr);
306
307 mid = smb2_mid_entry_alloc(hdr, server);
308 if (mid == NULL)
309 return -ENOMEM;
310
Jeff Layton0b688cf2012-09-18 16:20:34 -0700311 rc = smb2_sign_rqst(&rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400312 if (rc) {
313 DeleteMidQEntry(mid);
314 return rc;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700315 }
316
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400317 *ret_mid = mid;
318 return rc;
319}