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