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