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