Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 1 | /* |
| 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 Layton | fb308a6 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 33 | #include <linux/highmem.h> |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 34 | #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 Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 40 | #include "smb2glob.h" |
| 41 | |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 42 | int |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 43 | smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 44 | { |
| 45 | int i, rc; |
| 46 | unsigned char smb2_signature[SMB2_HMACSHA256_SIZE]; |
| 47 | unsigned char *sigptr = smb2_signature; |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 48 | struct kvec *iov = rqst->rq_iov; |
| 49 | int n_vec = rqst->rq_nvec; |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 50 | 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 Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 58 | cifs_dbg(VFS, "%s: Could not update with response\n", __func__); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 59 | return rc; |
| 60 | } |
| 61 | |
| 62 | rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash); |
| 63 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 64 | cifs_dbg(VFS, "%s: Could not init md5\n", __func__); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 65 | 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 Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 72 | cifs_dbg(VFS, "null iovec entry\n"); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 73 | 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 Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 93 | cifs_dbg(VFS, "%s: Could not update with payload\n", |
| 94 | __func__); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 95 | return rc; |
| 96 | } |
| 97 | } |
| 98 | |
Jeff Layton | fb308a6 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 99 | /* 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 Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 109 | rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash, |
| 110 | sigptr); |
| 111 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 112 | cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 113 | |
| 114 | memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE); |
| 115 | |
| 116 | return rc; |
| 117 | } |
| 118 | |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 119 | int |
| 120 | smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
| 121 | { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 122 | cifs_dbg(FYI, "smb3 signatures not supported yet\n"); |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 123 | return -EOPNOTSUPP; |
| 124 | } |
| 125 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 126 | /* must be called with server->srv_mutex held */ |
| 127 | static int |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 128 | smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 129 | { |
| 130 | int rc = 0; |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 131 | struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 132 | |
| 133 | if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) || |
| 134 | server->tcpStatus == CifsNeedNegotiate) |
| 135 | return rc; |
| 136 | |
| 137 | if (!server->session_estab) { |
| 138 | strncpy(smb2_pdu->Signature, "BSRSPYL", 8); |
| 139 | return rc; |
| 140 | } |
| 141 | |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 142 | rc = server->ops->calc_signature(rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 143 | |
| 144 | return rc; |
| 145 | } |
| 146 | |
| 147 | int |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 148 | smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 149 | { |
| 150 | unsigned int rc; |
| 151 | char server_response_sig[16]; |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 152 | struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 153 | |
| 154 | if ((smb2_pdu->Command == SMB2_NEGOTIATE) || |
| 155 | (smb2_pdu->Command == SMB2_OPLOCK_BREAK) || |
| 156 | (!server->session_estab)) |
| 157 | return 0; |
| 158 | |
| 159 | /* |
| 160 | * BB what if signatures are supposed to be on for session but |
| 161 | * server does not send one? BB |
| 162 | */ |
| 163 | |
| 164 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
| 165 | if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 166 | cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n", |
| 167 | smb2_pdu->Command); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Save off the origiginal signature so we can modify the smb and check |
| 171 | * our calculated signature against what the server sent. |
| 172 | */ |
| 173 | memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE); |
| 174 | |
| 175 | memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE); |
| 176 | |
| 177 | mutex_lock(&server->srv_mutex); |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 178 | rc = server->ops->calc_signature(rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 179 | mutex_unlock(&server->srv_mutex); |
| 180 | |
| 181 | if (rc) |
| 182 | return rc; |
| 183 | |
| 184 | if (memcmp(server_response_sig, smb2_pdu->Signature, |
| 185 | SMB2_SIGNATURE_SIZE)) |
| 186 | return -EACCES; |
| 187 | else |
| 188 | return 0; |
| 189 | } |
| 190 | |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 191 | /* |
| 192 | * Set message id for the request. Should be called after wait_for_free_request |
| 193 | * and when srv_mutex is held. |
| 194 | */ |
| 195 | static inline void |
| 196 | smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr) |
| 197 | { |
| 198 | hdr->MessageId = get_next_mid(server); |
| 199 | } |
| 200 | |
| 201 | static struct mid_q_entry * |
| 202 | smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer, |
| 203 | struct TCP_Server_Info *server) |
| 204 | { |
| 205 | struct mid_q_entry *temp; |
| 206 | |
| 207 | if (server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 208 | cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n"); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS); |
| 213 | if (temp == NULL) |
| 214 | return temp; |
| 215 | else { |
| 216 | memset(temp, 0, sizeof(struct mid_q_entry)); |
| 217 | temp->mid = smb_buffer->MessageId; /* always LE */ |
| 218 | temp->pid = current->pid; |
| 219 | temp->command = smb_buffer->Command; /* Always LE */ |
| 220 | temp->when_alloc = jiffies; |
| 221 | temp->server = server; |
| 222 | |
| 223 | /* |
| 224 | * The default is for the mid to be synchronous, so the |
| 225 | * default callback just wakes up the current task. |
| 226 | */ |
| 227 | temp->callback = cifs_wake_up_task; |
| 228 | temp->callback_data = current; |
| 229 | } |
| 230 | |
| 231 | atomic_inc(&midCount); |
| 232 | temp->mid_state = MID_REQUEST_ALLOCATED; |
| 233 | return temp; |
| 234 | } |
| 235 | |
| 236 | static int |
| 237 | smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf, |
| 238 | struct mid_q_entry **mid) |
| 239 | { |
| 240 | if (ses->server->tcpStatus == CifsExiting) |
| 241 | return -ENOENT; |
| 242 | |
| 243 | if (ses->server->tcpStatus == CifsNeedReconnect) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 244 | cifs_dbg(FYI, "tcp session dead - return to caller to retry\n"); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 245 | return -EAGAIN; |
| 246 | } |
| 247 | |
| 248 | if (ses->status != CifsGood) { |
| 249 | /* check if SMB2 session is bad because we are setting it up */ |
| 250 | if ((buf->Command != SMB2_SESSION_SETUP) && |
| 251 | (buf->Command != SMB2_NEGOTIATE)) |
| 252 | return -EAGAIN; |
| 253 | /* else ok - we are setting up session */ |
| 254 | } |
| 255 | *mid = smb2_mid_entry_alloc(buf, ses->server); |
| 256 | if (*mid == NULL) |
| 257 | return -ENOMEM; |
| 258 | spin_lock(&GlobalMid_Lock); |
| 259 | list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q); |
| 260 | spin_unlock(&GlobalMid_Lock); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | int |
| 265 | smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
| 266 | bool log_error) |
| 267 | { |
| 268 | unsigned int len = get_rfc1002_length(mid->resp_buf); |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 269 | struct kvec iov; |
| 270 | struct smb_rqst rqst = { .rq_iov = &iov, |
| 271 | .rq_nvec = 1 }; |
| 272 | |
| 273 | iov.iov_base = (char *)mid->resp_buf; |
| 274 | iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 275 | |
| 276 | dump_smb(mid->resp_buf, min_t(u32, 80, len)); |
| 277 | /* convert the length into a more usable form */ |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 278 | if ((len > 24) && |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 279 | (server->sec_mode & (SECMODE_SIGN_REQUIRED|SECMODE_SIGN_ENABLED))) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 280 | int rc; |
| 281 | |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 282 | rc = smb2_verify_signature(&rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 283 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 284 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
| 285 | rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 286 | } |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 287 | |
| 288 | return map_smb2_to_linux_error(mid->resp_buf, log_error); |
| 289 | } |
| 290 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 291 | struct mid_q_entry * |
| 292 | smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst) |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 293 | { |
| 294 | int rc; |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 295 | struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 296 | struct mid_q_entry *mid; |
| 297 | |
| 298 | smb2_seq_num_into_buf(ses->server, hdr); |
| 299 | |
| 300 | rc = smb2_get_mid_entry(ses, hdr, &mid); |
| 301 | if (rc) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 302 | return ERR_PTR(rc); |
| 303 | rc = smb2_sign_rqst(rqst, ses->server); |
| 304 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 305 | cifs_delete_mid(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 306 | return ERR_PTR(rc); |
| 307 | } |
| 308 | return mid; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 309 | } |
| 310 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 311 | struct mid_q_entry * |
| 312 | smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst) |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 313 | { |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 314 | int rc; |
| 315 | struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 316 | struct mid_q_entry *mid; |
| 317 | |
| 318 | smb2_seq_num_into_buf(server, hdr); |
| 319 | |
| 320 | mid = smb2_mid_entry_alloc(hdr, server); |
| 321 | if (mid == NULL) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 322 | return ERR_PTR(-ENOMEM); |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 323 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 324 | rc = smb2_sign_rqst(rqst, server); |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 325 | if (rc) { |
| 326 | DeleteMidQEntry(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 327 | return ERR_PTR(rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 330 | return mid; |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 331 | } |