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 | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 34 | #include <crypto/aead.h> |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 35 | #include "smb2pdu.h" |
| 36 | #include "cifsglob.h" |
| 37 | #include "cifsproto.h" |
| 38 | #include "smb2proto.h" |
| 39 | #include "cifs_debug.h" |
| 40 | #include "smb2status.h" |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 41 | #include "smb2glob.h" |
| 42 | |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 43 | static int |
| 44 | smb2_crypto_shash_allocate(struct TCP_Server_Info *server) |
| 45 | { |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 46 | return cifs_alloc_hash("hmac(sha256)", |
| 47 | &server->secmech.hmacsha256, |
| 48 | &server->secmech.sdeschmacsha256); |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int |
| 52 | smb3_crypto_shash_allocate(struct TCP_Server_Info *server) |
| 53 | { |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 54 | struct cifs_secmech *p = &server->secmech; |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 55 | int rc; |
| 56 | |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 57 | rc = cifs_alloc_hash("hmac(sha256)", |
| 58 | &p->hmacsha256, |
| 59 | &p->sdeschmacsha256); |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 60 | if (rc) |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 61 | goto err; |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 62 | |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 63 | rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes); |
| 64 | if (rc) |
| 65 | goto err; |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 66 | |
| 67 | return 0; |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 68 | err: |
| 69 | cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256); |
| 70 | return rc; |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Aurelien Aptel | 5fcd7f3 | 2018-02-16 19:19:28 +0100 | [diff] [blame] | 73 | #ifdef CONFIG_CIFS_SMB311 |
| 74 | int |
| 75 | smb311_crypto_shash_allocate(struct TCP_Server_Info *server) |
| 76 | { |
| 77 | struct cifs_secmech *p = &server->secmech; |
| 78 | int rc = 0; |
| 79 | |
| 80 | rc = cifs_alloc_hash("hmac(sha256)", |
| 81 | &p->hmacsha256, |
| 82 | &p->sdeschmacsha256); |
| 83 | if (rc) |
| 84 | return rc; |
| 85 | |
| 86 | rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes); |
| 87 | if (rc) |
| 88 | goto err; |
| 89 | |
| 90 | rc = cifs_alloc_hash("sha512", &p->sha512, &p->sdescsha512); |
| 91 | if (rc) |
| 92 | goto err; |
| 93 | |
| 94 | return 0; |
| 95 | |
| 96 | err: |
| 97 | cifs_free_hash(&p->cmacaes, &p->sdesccmacaes); |
| 98 | cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256); |
| 99 | return rc; |
| 100 | } |
| 101 | #endif |
| 102 | |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 103 | static struct cifs_ses * |
| 104 | smb2_find_smb_ses_unlocked(struct TCP_Server_Info *server, __u64 ses_id) |
| 105 | { |
| 106 | struct cifs_ses *ses; |
| 107 | |
| 108 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
| 109 | if (ses->Suid != ses_id) |
| 110 | continue; |
| 111 | return ses; |
| 112 | } |
| 113 | |
| 114 | return NULL; |
| 115 | } |
| 116 | |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 117 | struct cifs_ses * |
| 118 | smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id) |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 119 | { |
| 120 | struct cifs_ses *ses; |
| 121 | |
| 122 | spin_lock(&cifs_tcp_ses_lock); |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 123 | ses = smb2_find_smb_ses_unlocked(server, ses_id); |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 124 | spin_unlock(&cifs_tcp_ses_lock); |
| 125 | |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 126 | return ses; |
| 127 | } |
| 128 | |
| 129 | static struct cifs_tcon * |
| 130 | smb2_find_smb_sess_tcon_unlocked(struct cifs_ses *ses, __u32 tid) |
| 131 | { |
| 132 | struct cifs_tcon *tcon; |
| 133 | |
| 134 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
| 135 | if (tcon->tid != tid) |
| 136 | continue; |
| 137 | ++tcon->tc_count; |
| 138 | return tcon; |
| 139 | } |
| 140 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 144 | /* |
| 145 | * Obtain tcon corresponding to the tid in the given |
| 146 | * cifs_ses |
| 147 | */ |
| 148 | |
| 149 | struct cifs_tcon * |
| 150 | smb2_find_smb_tcon(struct TCP_Server_Info *server, __u64 ses_id, __u32 tid) |
| 151 | { |
| 152 | struct cifs_ses *ses; |
| 153 | struct cifs_tcon *tcon; |
| 154 | |
| 155 | spin_lock(&cifs_tcp_ses_lock); |
| 156 | ses = smb2_find_smb_ses_unlocked(server, ses_id); |
| 157 | if (!ses) { |
| 158 | spin_unlock(&cifs_tcp_ses_lock); |
| 159 | return NULL; |
| 160 | } |
| 161 | tcon = smb2_find_smb_sess_tcon_unlocked(ses, tid); |
| 162 | spin_unlock(&cifs_tcp_ses_lock); |
| 163 | |
| 164 | return tcon; |
| 165 | } |
| 166 | |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 167 | int |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 168 | smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 169 | { |
Al Viro | 16c568e | 2015-11-12 22:46:49 -0500 | [diff] [blame] | 170 | int rc; |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 171 | unsigned char smb2_signature[SMB2_HMACSHA256_SIZE]; |
| 172 | unsigned char *sigptr = smb2_signature; |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 173 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 174 | struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base; |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 175 | struct cifs_ses *ses; |
| 176 | |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 177 | ses = smb2_find_smb_ses(server, shdr->SessionId); |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 178 | if (!ses) { |
| 179 | cifs_dbg(VFS, "%s: Could not find session\n", __func__); |
| 180 | return 0; |
| 181 | } |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 182 | |
| 183 | memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE); |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 184 | memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 185 | |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 186 | rc = smb2_crypto_shash_allocate(server); |
| 187 | if (rc) { |
| 188 | cifs_dbg(VFS, "%s: shah256 alloc failed\n", __func__); |
| 189 | return rc; |
| 190 | } |
| 191 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 192 | rc = crypto_shash_setkey(server->secmech.hmacsha256, |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 193 | ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 194 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 195 | cifs_dbg(VFS, "%s: Could not update with response\n", __func__); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 196 | return rc; |
| 197 | } |
| 198 | |
| 199 | rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash); |
| 200 | if (rc) { |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 201 | cifs_dbg(VFS, "%s: Could not init sha256", __func__); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 202 | return rc; |
| 203 | } |
| 204 | |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 205 | rc = __cifs_calc_signature(rqst, server, sigptr, |
Al Viro | 16c568e | 2015-11-12 22:46:49 -0500 | [diff] [blame] | 206 | &server->secmech.sdeschmacsha256->shash); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 207 | |
Al Viro | 16c568e | 2015-11-12 22:46:49 -0500 | [diff] [blame] | 208 | if (!rc) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 209 | memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 210 | |
| 211 | return rc; |
| 212 | } |
| 213 | |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 214 | static int generate_key(struct cifs_ses *ses, struct kvec label, |
| 215 | struct kvec context, __u8 *key, unsigned int key_size) |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 216 | { |
| 217 | unsigned char zero = 0x0; |
| 218 | __u8 i[4] = {0, 0, 0, 1}; |
| 219 | __u8 L[4] = {0, 0, 0, 128}; |
| 220 | int rc = 0; |
| 221 | unsigned char prfhash[SMB2_HMACSHA256_SIZE]; |
| 222 | unsigned char *hashptr = prfhash; |
| 223 | |
| 224 | memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE); |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 225 | memset(key, 0x0, key_size); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 226 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 227 | rc = smb3_crypto_shash_allocate(ses->server); |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 228 | if (rc) { |
| 229 | cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__); |
| 230 | goto smb3signkey_ret; |
| 231 | } |
| 232 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 233 | rc = crypto_shash_setkey(ses->server->secmech.hmacsha256, |
| 234 | ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 235 | if (rc) { |
| 236 | cifs_dbg(VFS, "%s: Could not set with session key\n", __func__); |
| 237 | goto smb3signkey_ret; |
| 238 | } |
| 239 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 240 | rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 241 | if (rc) { |
| 242 | cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__); |
| 243 | goto smb3signkey_ret; |
| 244 | } |
| 245 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 246 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash, |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 247 | i, 4); |
| 248 | if (rc) { |
| 249 | cifs_dbg(VFS, "%s: Could not update with n\n", __func__); |
| 250 | goto smb3signkey_ret; |
| 251 | } |
| 252 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 253 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash, |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 254 | label.iov_base, label.iov_len); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 255 | if (rc) { |
| 256 | cifs_dbg(VFS, "%s: Could not update with label\n", __func__); |
| 257 | goto smb3signkey_ret; |
| 258 | } |
| 259 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 260 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash, |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 261 | &zero, 1); |
| 262 | if (rc) { |
| 263 | cifs_dbg(VFS, "%s: Could not update with zero\n", __func__); |
| 264 | goto smb3signkey_ret; |
| 265 | } |
| 266 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 267 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash, |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 268 | context.iov_base, context.iov_len); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 269 | if (rc) { |
| 270 | cifs_dbg(VFS, "%s: Could not update with context\n", __func__); |
| 271 | goto smb3signkey_ret; |
| 272 | } |
| 273 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 274 | rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash, |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 275 | L, 4); |
| 276 | if (rc) { |
| 277 | cifs_dbg(VFS, "%s: Could not update with L\n", __func__); |
| 278 | goto smb3signkey_ret; |
| 279 | } |
| 280 | |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 281 | rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash, |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 282 | hashptr); |
| 283 | if (rc) { |
| 284 | cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__); |
| 285 | goto smb3signkey_ret; |
| 286 | } |
| 287 | |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 288 | memcpy(key, hashptr, key_size); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 289 | |
| 290 | smb3signkey_ret: |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 291 | return rc; |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 294 | struct derivation { |
| 295 | struct kvec label; |
| 296 | struct kvec context; |
| 297 | }; |
| 298 | |
| 299 | struct derivation_triplet { |
| 300 | struct derivation signing; |
| 301 | struct derivation encryption; |
| 302 | struct derivation decryption; |
| 303 | }; |
| 304 | |
| 305 | static int |
| 306 | generate_smb3signingkey(struct cifs_ses *ses, |
| 307 | const struct derivation_triplet *ptriplet) |
| 308 | { |
| 309 | int rc; |
| 310 | |
| 311 | rc = generate_key(ses, ptriplet->signing.label, |
| 312 | ptriplet->signing.context, ses->smb3signingkey, |
| 313 | SMB3_SIGN_KEY_SIZE); |
| 314 | if (rc) |
| 315 | return rc; |
| 316 | |
| 317 | rc = generate_key(ses, ptriplet->encryption.label, |
| 318 | ptriplet->encryption.context, ses->smb3encryptionkey, |
| 319 | SMB3_SIGN_KEY_SIZE); |
| 320 | if (rc) |
| 321 | return rc; |
| 322 | |
Aurélien Aptel | d38de3c6 | 2017-05-24 16:13:25 +0200 | [diff] [blame] | 323 | rc = generate_key(ses, ptriplet->decryption.label, |
| 324 | ptriplet->decryption.context, |
| 325 | ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE); |
| 326 | |
| 327 | if (rc) |
| 328 | return rc; |
| 329 | |
| 330 | #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS |
| 331 | cifs_dbg(VFS, "%s: dumping generated AES session keys\n", __func__); |
| 332 | /* |
| 333 | * The session id is opaque in terms of endianness, so we can't |
| 334 | * print it as a long long. we dump it as we got it on the wire |
| 335 | */ |
| 336 | cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid), |
| 337 | &ses->Suid); |
| 338 | cifs_dbg(VFS, "Session Key %*ph\n", |
| 339 | SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response); |
| 340 | cifs_dbg(VFS, "Signing Key %*ph\n", |
| 341 | SMB3_SIGN_KEY_SIZE, ses->smb3signingkey); |
| 342 | cifs_dbg(VFS, "ServerIn Key %*ph\n", |
| 343 | SMB3_SIGN_KEY_SIZE, ses->smb3encryptionkey); |
| 344 | cifs_dbg(VFS, "ServerOut Key %*ph\n", |
| 345 | SMB3_SIGN_KEY_SIZE, ses->smb3decryptionkey); |
| 346 | #endif |
| 347 | return rc; |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int |
| 351 | generate_smb30signingkey(struct cifs_ses *ses) |
| 352 | |
| 353 | { |
| 354 | struct derivation_triplet triplet; |
| 355 | struct derivation *d; |
| 356 | |
| 357 | d = &triplet.signing; |
| 358 | d->label.iov_base = "SMB2AESCMAC"; |
| 359 | d->label.iov_len = 12; |
| 360 | d->context.iov_base = "SmbSign"; |
| 361 | d->context.iov_len = 8; |
| 362 | |
| 363 | d = &triplet.encryption; |
| 364 | d->label.iov_base = "SMB2AESCCM"; |
| 365 | d->label.iov_len = 11; |
| 366 | d->context.iov_base = "ServerIn "; |
| 367 | d->context.iov_len = 10; |
| 368 | |
| 369 | d = &triplet.decryption; |
| 370 | d->label.iov_base = "SMB2AESCCM"; |
| 371 | d->label.iov_len = 11; |
| 372 | d->context.iov_base = "ServerOut"; |
| 373 | d->context.iov_len = 10; |
| 374 | |
| 375 | return generate_smb3signingkey(ses, &triplet); |
| 376 | } |
| 377 | |
Steve French | 06e2290 | 2017-09-25 20:11:58 -0500 | [diff] [blame] | 378 | #ifdef CONFIG_CIFS_SMB311 |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 379 | int |
| 380 | generate_smb311signingkey(struct cifs_ses *ses) |
| 381 | |
| 382 | { |
| 383 | struct derivation_triplet triplet; |
| 384 | struct derivation *d; |
| 385 | |
| 386 | d = &triplet.signing; |
Steve French | 06e2290 | 2017-09-25 20:11:58 -0500 | [diff] [blame] | 387 | d->label.iov_base = "SMBSigningKey"; |
| 388 | d->label.iov_len = 14; |
| 389 | d->context.iov_base = ses->preauth_sha_hash; |
| 390 | d->context.iov_len = 64; |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 391 | |
| 392 | d = &triplet.encryption; |
Steve French | 06e2290 | 2017-09-25 20:11:58 -0500 | [diff] [blame] | 393 | d->label.iov_base = "SMBC2SCipherKey"; |
| 394 | d->label.iov_len = 16; |
| 395 | d->context.iov_base = ses->preauth_sha_hash; |
| 396 | d->context.iov_len = 64; |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 397 | |
| 398 | d = &triplet.decryption; |
Steve French | 06e2290 | 2017-09-25 20:11:58 -0500 | [diff] [blame] | 399 | d->label.iov_base = "SMBS2CCipherKey"; |
| 400 | d->label.iov_len = 16; |
| 401 | d->context.iov_base = ses->preauth_sha_hash; |
| 402 | d->context.iov_len = 64; |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 403 | |
| 404 | return generate_smb3signingkey(ses, &triplet); |
| 405 | } |
Steve French | 06e2290 | 2017-09-25 20:11:58 -0500 | [diff] [blame] | 406 | #endif /* 311 */ |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 407 | |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 408 | int |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 409 | smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
| 410 | { |
Paulo Alcantara | 27c32b4 | 2018-06-23 14:52:23 -0300 | [diff] [blame^] | 411 | int rc; |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 412 | unsigned char smb3_signature[SMB2_CMACAES_SIZE]; |
| 413 | unsigned char *sigptr = smb3_signature; |
| 414 | struct kvec *iov = rqst->rq_iov; |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 415 | struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)iov[0].iov_base; |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 416 | struct cifs_ses *ses; |
Paulo Alcantara | 27c32b4 | 2018-06-23 14:52:23 -0300 | [diff] [blame^] | 417 | struct shash_desc *shash = &server->secmech.sdesccmacaes->shash; |
| 418 | struct smb_rqst drqst; |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 419 | |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 420 | ses = smb2_find_smb_ses(server, shdr->SessionId); |
Shirish Pargaonkar | 32811d2 | 2013-08-29 08:35:11 -0500 | [diff] [blame] | 421 | if (!ses) { |
| 422 | cifs_dbg(VFS, "%s: Could not find session\n", __func__); |
| 423 | return 0; |
| 424 | } |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 425 | |
| 426 | memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE); |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 427 | memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 428 | |
| 429 | rc = crypto_shash_setkey(server->secmech.cmacaes, |
Paulo Alcantara | 27c32b4 | 2018-06-23 14:52:23 -0300 | [diff] [blame^] | 430 | ses->smb3signingkey, SMB2_CMACAES_SIZE); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 431 | if (rc) { |
| 432 | cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__); |
| 433 | return rc; |
| 434 | } |
| 435 | |
Steve French | 95dc8dd | 2013-07-04 10:35:21 -0500 | [diff] [blame] | 436 | /* |
| 437 | * we already allocate sdesccmacaes when we init smb3 signing key, |
| 438 | * so unlike smb2 case we do not have to check here if secmech are |
| 439 | * initialized |
| 440 | */ |
Paulo Alcantara | 27c32b4 | 2018-06-23 14:52:23 -0300 | [diff] [blame^] | 441 | rc = crypto_shash_init(shash); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 442 | if (rc) { |
| 443 | cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__); |
| 444 | return rc; |
| 445 | } |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 446 | |
Paulo Alcantara | 27c32b4 | 2018-06-23 14:52:23 -0300 | [diff] [blame^] | 447 | /* |
| 448 | * For SMB2+, __cifs_calc_signature() expects to sign only the actual |
| 449 | * data, that is, iov[0] should not contain a rfc1002 length. |
| 450 | * |
| 451 | * Sign the rfc1002 length prior to passing the data (iov[1-N]) down to |
| 452 | * __cifs_calc_signature(). |
| 453 | */ |
| 454 | drqst = *rqst; |
| 455 | if (drqst.rq_nvec >= 2 && iov[0].iov_len == 4) { |
| 456 | rc = crypto_shash_update(shash, iov[0].iov_base, |
| 457 | iov[0].iov_len); |
| 458 | if (rc) { |
| 459 | cifs_dbg(VFS, "%s: Could not update with payload\n", |
| 460 | __func__); |
| 461 | return rc; |
| 462 | } |
| 463 | drqst.rq_iov++; |
| 464 | drqst.rq_nvec--; |
| 465 | } |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 466 | |
Paulo Alcantara | 27c32b4 | 2018-06-23 14:52:23 -0300 | [diff] [blame^] | 467 | rc = __cifs_calc_signature(&drqst, server, sigptr, shash); |
Al Viro | 16c568e | 2015-11-12 22:46:49 -0500 | [diff] [blame] | 468 | if (!rc) |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 469 | memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE); |
Steve French | 429b46f | 2013-06-26 23:45:05 -0500 | [diff] [blame] | 470 | |
| 471 | return rc; |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 472 | } |
| 473 | |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 474 | /* must be called with server->srv_mutex held */ |
| 475 | static int |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 476 | smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 477 | { |
| 478 | int rc = 0; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 479 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 480 | (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 481 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 482 | if (!(shdr->Flags & SMB2_FLAGS_SIGNED) || |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 483 | server->tcpStatus == CifsNeedNegotiate) |
| 484 | return rc; |
| 485 | |
| 486 | if (!server->session_estab) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 487 | strncpy(shdr->Signature, "BSRSPYL", 8); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 488 | return rc; |
| 489 | } |
| 490 | |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 491 | rc = server->ops->calc_signature(rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 492 | |
| 493 | return rc; |
| 494 | } |
| 495 | |
| 496 | int |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 497 | smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 498 | { |
| 499 | unsigned int rc; |
| 500 | char server_response_sig[16]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 501 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 502 | (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 503 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 504 | if ((shdr->Command == SMB2_NEGOTIATE) || |
| 505 | (shdr->Command == SMB2_SESSION_SETUP) || |
| 506 | (shdr->Command == SMB2_OPLOCK_BREAK) || |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 507 | (!server->session_estab)) |
| 508 | return 0; |
| 509 | |
| 510 | /* |
| 511 | * BB what if signatures are supposed to be on for session but |
| 512 | * server does not send one? BB |
| 513 | */ |
| 514 | |
| 515 | /* Do not need to verify session setups with signature "BSRSPYL " */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 516 | if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 517 | cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n", |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 518 | shdr->Command); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * Save off the origiginal signature so we can modify the smb and check |
| 522 | * our calculated signature against what the server sent. |
| 523 | */ |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 524 | memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 525 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 526 | memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 527 | |
| 528 | mutex_lock(&server->srv_mutex); |
Steve French | 38107d4 | 2012-12-08 22:08:06 -0600 | [diff] [blame] | 529 | rc = server->ops->calc_signature(rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 530 | mutex_unlock(&server->srv_mutex); |
| 531 | |
| 532 | if (rc) |
| 533 | return rc; |
| 534 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 535 | if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE)) |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 536 | return -EACCES; |
| 537 | else |
| 538 | return 0; |
| 539 | } |
| 540 | |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 541 | /* |
| 542 | * Set message id for the request. Should be called after wait_for_free_request |
| 543 | * and when srv_mutex is held. |
| 544 | */ |
| 545 | static inline void |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 546 | smb2_seq_num_into_buf(struct TCP_Server_Info *server, |
| 547 | struct smb2_sync_hdr *shdr) |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 548 | { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 549 | unsigned int i, num = le16_to_cpu(shdr->CreditCharge); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 550 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 551 | shdr->MessageId = get_next_mid64(server); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 552 | /* skip message numbers according to CreditCharge field */ |
| 553 | for (i = 1; i < num; i++) |
| 554 | get_next_mid(server); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static struct mid_q_entry * |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 558 | smb2_mid_entry_alloc(const struct smb2_sync_hdr *shdr, |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 559 | struct TCP_Server_Info *server) |
| 560 | { |
| 561 | struct mid_q_entry *temp; |
| 562 | |
| 563 | if (server == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 564 | cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n"); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS); |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 569 | memset(temp, 0, sizeof(struct mid_q_entry)); |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 570 | kref_init(&temp->refcount); |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 571 | temp->mid = le64_to_cpu(shdr->MessageId); |
| 572 | temp->pid = current->pid; |
| 573 | temp->command = shdr->Command; /* Always LE */ |
| 574 | temp->when_alloc = jiffies; |
| 575 | temp->server = server; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 576 | |
NeilBrown | a6f74e8 | 2017-04-10 12:08:53 +1000 | [diff] [blame] | 577 | /* |
| 578 | * The default is for the mid to be synchronous, so the |
| 579 | * default callback just wakes up the current task. |
| 580 | */ |
| 581 | temp->callback = cifs_wake_up_task; |
| 582 | temp->callback_data = current; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 583 | |
| 584 | atomic_inc(&midCount); |
| 585 | temp->mid_state = MID_REQUEST_ALLOCATED; |
| 586 | return temp; |
| 587 | } |
| 588 | |
| 589 | static int |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 590 | smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_sync_hdr *shdr, |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 591 | struct mid_q_entry **mid) |
| 592 | { |
| 593 | if (ses->server->tcpStatus == CifsExiting) |
| 594 | return -ENOENT; |
| 595 | |
| 596 | if (ses->server->tcpStatus == CifsNeedReconnect) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 597 | cifs_dbg(FYI, "tcp session dead - return to caller to retry\n"); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 598 | return -EAGAIN; |
| 599 | } |
| 600 | |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 601 | if (ses->status == CifsNew) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 602 | if ((shdr->Command != SMB2_SESSION_SETUP) && |
| 603 | (shdr->Command != SMB2_NEGOTIATE)) |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 604 | return -EAGAIN; |
| 605 | /* else ok - we are setting up session */ |
| 606 | } |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 607 | |
| 608 | if (ses->status == CifsExiting) { |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 609 | if (shdr->Command != SMB2_LOGOFF) |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 610 | return -EAGAIN; |
| 611 | /* else ok - we are shutting down the session */ |
| 612 | } |
| 613 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 614 | *mid = smb2_mid_entry_alloc(shdr, ses->server); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 615 | if (*mid == NULL) |
| 616 | return -ENOMEM; |
| 617 | spin_lock(&GlobalMid_Lock); |
| 618 | list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q); |
| 619 | spin_unlock(&GlobalMid_Lock); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | int |
| 624 | smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
| 625 | bool log_error) |
| 626 | { |
Ronnie Sahlberg | e19b2bc | 2018-04-09 18:06:28 +1000 | [diff] [blame] | 627 | unsigned int len = mid->resp_buf_size; |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 628 | struct kvec iov[1]; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 629 | struct smb_rqst rqst = { .rq_iov = iov, |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 630 | .rq_nvec = 1 }; |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 631 | |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 632 | iov[0].iov_base = (char *)mid->resp_buf; |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 633 | iov[0].iov_len = len; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 634 | |
| 635 | dump_smb(mid->resp_buf, min_t(u32, 80, len)); |
| 636 | /* convert the length into a more usable form */ |
Pavel Shilovsky | 4326ed2 | 2016-11-17 15:24:46 -0800 | [diff] [blame] | 637 | if (len > 24 && server->sign && !mid->decrypted) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 638 | int rc; |
| 639 | |
Jeff Layton | 0b688cf | 2012-09-18 16:20:34 -0700 | [diff] [blame] | 640 | rc = smb2_verify_signature(&rqst, server); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 641 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 642 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
| 643 | rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 644 | } |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 645 | |
| 646 | return map_smb2_to_linux_error(mid->resp_buf, log_error); |
| 647 | } |
| 648 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 649 | struct mid_q_entry * |
| 650 | smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst) |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 651 | { |
| 652 | int rc; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 653 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 654 | (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 655 | struct mid_q_entry *mid; |
| 656 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 657 | smb2_seq_num_into_buf(ses->server, shdr); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 658 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 659 | rc = smb2_get_mid_entry(ses, shdr, &mid); |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 660 | if (rc) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 661 | return ERR_PTR(rc); |
| 662 | rc = smb2_sign_rqst(rqst, ses->server); |
| 663 | if (rc) { |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 664 | cifs_delete_mid(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 665 | return ERR_PTR(rc); |
| 666 | } |
| 667 | return mid; |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 668 | } |
| 669 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 670 | struct mid_q_entry * |
| 671 | 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] | 672 | { |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 673 | int rc; |
Pavel Shilovsky | 738f9de | 2016-11-23 15:14:57 -0800 | [diff] [blame] | 674 | struct smb2_sync_hdr *shdr = |
Ronnie Sahlberg | c713c87 | 2018-06-12 08:00:58 +1000 | [diff] [blame] | 675 | (struct smb2_sync_hdr *)rqst->rq_iov[0].iov_base; |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 676 | struct mid_q_entry *mid; |
| 677 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 678 | smb2_seq_num_into_buf(server, shdr); |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 679 | |
Pavel Shilovsky | 31473fc | 2016-10-24 15:33:04 -0700 | [diff] [blame] | 680 | mid = smb2_mid_entry_alloc(shdr, server); |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 681 | if (mid == NULL) |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 682 | return ERR_PTR(-ENOMEM); |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 683 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 684 | rc = smb2_sign_rqst(rqst, server); |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 685 | if (rc) { |
| 686 | DeleteMidQEntry(mid); |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 687 | return ERR_PTR(rc); |
Pavel Shilovsky | 3c1bf7e | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Jeff Layton | fec344e | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 690 | return mid; |
Pavel Shilovsky | c95b8ee | 2012-07-11 14:45:28 +0400 | [diff] [blame] | 691 | } |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 692 | |
| 693 | int |
| 694 | smb3_crypto_aead_allocate(struct TCP_Server_Info *server) |
| 695 | { |
| 696 | struct crypto_aead *tfm; |
| 697 | |
| 698 | if (!server->secmech.ccmaesencrypt) { |
| 699 | tfm = crypto_alloc_aead("ccm(aes)", 0, 0); |
| 700 | if (IS_ERR(tfm)) { |
| 701 | cifs_dbg(VFS, "%s: Failed to alloc encrypt aead\n", |
| 702 | __func__); |
| 703 | return PTR_ERR(tfm); |
| 704 | } |
| 705 | server->secmech.ccmaesencrypt = tfm; |
| 706 | } |
| 707 | |
| 708 | if (!server->secmech.ccmaesdecrypt) { |
| 709 | tfm = crypto_alloc_aead("ccm(aes)", 0, 0); |
| 710 | if (IS_ERR(tfm)) { |
| 711 | crypto_free_aead(server->secmech.ccmaesencrypt); |
| 712 | server->secmech.ccmaesencrypt = NULL; |
| 713 | cifs_dbg(VFS, "%s: Failed to alloc decrypt aead\n", |
| 714 | __func__); |
| 715 | return PTR_ERR(tfm); |
| 716 | } |
| 717 | server->secmech.ccmaesdecrypt = tfm; |
| 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |