blob: 2af79093b78bcc2f7a7c60a4df0fb5c1fff83bc1 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002/*
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003 *
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 Shilovsky2dc7e1c2011-12-26 22:53:34 +040010 */
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 Laytonfb308a62012-09-18 16:20:35 -070020#include <linux/highmem.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070021#include <crypto/aead.h>
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040022#include "cifsglob.h"
23#include "cifsproto.h"
24#include "smb2proto.h"
25#include "cifs_debug.h"
26#include "smb2status.h"
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -070027#include "smb2glob.h"
28
Steve French95dc8dd2013-07-04 10:35:21 -050029static int
Steve French95dc8dd2013-07-04 10:35:21 -050030smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
31{
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010032 struct cifs_secmech *p = &server->secmech;
Steve French95dc8dd2013-07-04 10:35:21 -050033 int rc;
34
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010035 rc = cifs_alloc_hash("hmac(sha256)",
36 &p->hmacsha256,
37 &p->sdeschmacsha256);
Steve French95dc8dd2013-07-04 10:35:21 -050038 if (rc)
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010039 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050040
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010041 rc = cifs_alloc_hash("cmac(aes)", &p->cmacaes, &p->sdesccmacaes);
42 if (rc)
43 goto err;
Steve French95dc8dd2013-07-04 10:35:21 -050044
45 return 0;
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010046err:
47 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
48 return rc;
Steve French95dc8dd2013-07-04 10:35:21 -050049}
50
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +010051int
52smb311_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
73err:
74 cifs_free_hash(&p->cmacaes, &p->sdesccmacaes);
75 cifs_free_hash(&p->hmacsha256, &p->sdeschmacsha256);
76 return rc;
77}
Aurelien Aptel5fcd7f32018-02-16 19:19:28 +010078
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020079
80static
81int 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 Aptelcc95b672020-02-06 13:49:26 +010085 struct TCP_Server_Info *it = NULL;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +020086 int i;
87 int rc = 0;
88
89 spin_lock(&cifs_tcp_ses_lock);
90
Aurelien Aptelcc95b672020-02-06 13:49:26 +010091 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 Apteld70e9fa2019-09-20 06:31:10 +020093 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
102found:
Shyam Prasad N88b024f2021-11-19 14:16:57 +0000103 spin_lock(&ses->chan_lock);
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000104 if (cifs_chan_needs_reconnect(ses, server) &&
105 !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200106 /*
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 N88b024f2021-11-19 14:16:57 +0000112 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200113 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 N88b024f2021-11-19 14:16:57 +0000124 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200125 goto out;
126 }
127 }
Shyam Prasad N88b024f2021-11-19 14:16:57 +0000128 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200129
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
135out:
136 spin_unlock(&cifs_tcp_ses_lock);
137 return rc;
138}
139
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800140static struct cifs_ses *
141smb2_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 Ne695a9a2021-05-23 16:54:42 +0000148 ++ses->ses_count;
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800149 return ses;
150 }
151
152 return NULL;
153}
154
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700155struct cifs_ses *
156smb2_find_smb_ses(struct TCP_Server_Info *server, __u64 ses_id)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500157{
158 struct cifs_ses *ses;
159
160 spin_lock(&cifs_tcp_ses_lock);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800161 ses = smb2_find_smb_ses_unlocked(server, ses_id);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500162 spin_unlock(&cifs_tcp_ses_lock);
163
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800164 return ses;
165}
166
167static struct cifs_tcon *
168smb2_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 Pargaonkar32811d22013-08-29 08:35:11 -0500179 return NULL;
180}
181
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800182/*
183 * Obtain tcon corresponding to the tid in the given
184 * cifs_ses
185 */
186
187struct cifs_tcon *
188smb2_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 Ne695a9a2021-05-23 16:54:42 +0000200 if (!tcon) {
201 cifs_put_smb_ses(ses);
202 spin_unlock(&cifs_tcp_ses_lock);
203 return NULL;
204 }
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800205 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad Ne695a9a2021-05-23 16:54:42 +0000206 /* tcon already has a ref to ses, so we don't need ses anymore */
207 cifs_put_smb_ses(ses);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800208
209 return tcon;
210}
211
Steve French38107d42012-12-08 22:08:06 -0600212int
Long Lieda1c542020-03-31 16:21:43 -0700213smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
214 bool allocate_crypto)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700215{
Al Viro16c568e2015-11-12 22:46:49 -0500216 int rc;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700217 unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
218 unsigned char *sigptr = smb2_signature;
Jeff Layton0b688cf2012-09-18 16:20:34 -0700219 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900220 struct smb2_hdr *shdr = (struct smb2_hdr *)iov[0].iov_base;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500221 struct cifs_ses *ses;
Aurelien Aptela5c62f42018-08-02 16:39:52 +0200222 struct shash_desc *shash;
Long Lieda1c542020-03-31 16:21:43 -0700223 struct crypto_shash *hash;
224 struct sdesc *sdesc = NULL;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300225 struct smb_rqst drqst;
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500226
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900227 ses = smb2_find_smb_ses(server, le64_to_cpu(shdr->SessionId));
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500228 if (!ses) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000229 cifs_server_dbg(VFS, "%s: Could not find session\n", __func__);
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500230 return 0;
231 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700232
233 memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700234 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700235
Long Lieda1c542020-03-31 16:21:43 -0700236 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 Ne695a9a2021-05-23 16:54:42 +0000241 goto out;
Long Lieda1c542020-03-31 16:21:43 -0700242 }
243 shash = &sdesc->shash;
244 } else {
245 hash = server->secmech.hmacsha256;
246 shash = &server->secmech.sdeschmacsha256->shash;
Steve French95dc8dd2013-07-04 10:35:21 -0500247 }
248
Long Lieda1c542020-03-31 16:21:43 -0700249 rc = crypto_shash_setkey(hash, ses->auth_key.response,
250 SMB2_NTLMV2_SESSKEY_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700251 if (rc) {
Long Lieda1c542020-03-31 16:21:43 -0700252 cifs_server_dbg(VFS,
253 "%s: Could not update with response\n",
254 __func__);
255 goto out;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700256 }
257
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300258 rc = crypto_shash_init(shash);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700259 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000260 cifs_server_dbg(VFS, "%s: Could not init sha256", __func__);
Long Lieda1c542020-03-31 16:21:43 -0700261 goto out;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700262 }
263
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300264 /*
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 Lieda1c542020-03-31 16:21:43 -0700276 cifs_server_dbg(VFS,
277 "%s: Could not update with payload\n",
278 __func__);
279 goto out;
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300280 }
281 drqst.rq_iov++;
282 drqst.rq_nvec--;
283 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700284
Paulo Alcantara8de8c462018-06-23 14:52:24 -0300285 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500286 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700287 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700288
Long Lieda1c542020-03-31 16:21:43 -0700289out:
290 if (allocate_crypto)
291 cifs_free_hash(&hash, &sdesc);
Shyam Prasad Ne695a9a2021-05-23 16:54:42 +0000292 if (ses)
293 cifs_put_smb_ses(ses);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700294 return rc;
295}
296
Steve French373512e2015-12-18 13:05:30 -0600297static int generate_key(struct cifs_ses *ses, struct kvec label,
298 struct kvec context, __u8 *key, unsigned int key_size)
Steve French429b46f2013-06-26 23:45:05 -0500299{
300 unsigned char zero = 0x0;
301 __u8 i[4] = {0, 0, 0, 1};
Shyam Prasad N45a45462021-03-25 12:34:54 +0000302 __u8 L128[4] = {0, 0, 0, 128};
303 __u8 L256[4] = {0, 0, 1, 0};
Steve French429b46f2013-06-26 23:45:05 -0500304 int rc = 0;
305 unsigned char prfhash[SMB2_HMACSHA256_SIZE];
306 unsigned char *hashptr = prfhash;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000307 struct TCP_Server_Info *server = ses->server;
Steve French429b46f2013-06-26 23:45:05 -0500308
309 memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
Steve French373512e2015-12-18 13:05:30 -0600310 memset(key, 0x0, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500311
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000312 rc = smb3_crypto_shash_allocate(server);
Steve French95dc8dd2013-07-04 10:35:21 -0500313 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000314 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Steve French95dc8dd2013-07-04 10:35:21 -0500315 goto smb3signkey_ret;
316 }
317
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000318 rc = crypto_shash_setkey(server->secmech.hmacsha256,
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500319 ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500320 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000321 cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500322 goto smb3signkey_ret;
323 }
324
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000325 rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
Steve French429b46f2013-06-26 23:45:05 -0500326 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000327 cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500328 goto smb3signkey_ret;
329 }
330
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000331 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500332 i, 4);
333 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000334 cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500335 goto smb3signkey_ret;
336 }
337
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000338 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600339 label.iov_base, label.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500340 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000341 cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500342 goto smb3signkey_ret;
343 }
344
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000345 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500346 &zero, 1);
347 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000348 cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500349 goto smb3signkey_ret;
350 }
351
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000352 rc = crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
Steve French373512e2015-12-18 13:05:30 -0600353 context.iov_base, context.iov_len);
Steve French429b46f2013-06-26 23:45:05 -0500354 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000355 cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500356 goto smb3signkey_ret;
357 }
358
Shyam Prasad N45a45462021-03-25 12:34:54 +0000359 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 French429b46f2013-06-26 23:45:05 -0500367 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000368 cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500369 goto smb3signkey_ret;
370 }
371
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000372 rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
Steve French429b46f2013-06-26 23:45:05 -0500373 hashptr);
374 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000375 cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
Steve French429b46f2013-06-26 23:45:05 -0500376 goto smb3signkey_ret;
377 }
378
Steve French373512e2015-12-18 13:05:30 -0600379 memcpy(key, hashptr, key_size);
Steve French429b46f2013-06-26 23:45:05 -0500380
381smb3signkey_ret:
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500382 return rc;
Steve French429b46f2013-06-26 23:45:05 -0500383}
384
Steve French373512e2015-12-18 13:05:30 -0600385struct derivation {
386 struct kvec label;
387 struct kvec context;
388};
389
390struct derivation_triplet {
391 struct derivation signing;
392 struct derivation encryption;
393 struct derivation decryption;
394};
395
396static int
397generate_smb3signingkey(struct cifs_ses *ses,
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000398 struct TCP_Server_Info *server,
Steve French373512e2015-12-18 13:05:30 -0600399 const struct derivation_triplet *ptriplet)
400{
401 int rc;
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000402 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 French373512e2015-12-18 13:05:30 -0600410
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200411 /*
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 French373512e2015-12-18 13:05:30 -0600420
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000421 if (is_binding) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200422 rc = generate_key(ses, ptriplet->signing.label,
423 ptriplet->signing.context,
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000424 ses->chans[chan_index].signkey,
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200425 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)ff6b6f32019-11-22 12:30:57 -0300435
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000436 /* safe to access primary channel, since it will never go away */
Shyam Prasad N88b024f2021-11-19 14:16:57 +0000437 spin_lock(&ses->chan_lock);
Paulo Alcantara (SUSE)ff6b6f32019-11-22 12:30:57 -0300438 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
439 SMB3_SIGN_KEY_SIZE);
Shyam Prasad N88b024f2021-11-19 14:16:57 +0000440 spin_unlock(&ses->chan_lock);
Paulo Alcantara (SUSE)ff6b6f32019-11-22 12:30:57 -0300441
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200442 rc = generate_key(ses, ptriplet->encryption.label,
443 ptriplet->encryption.context,
444 ses->smb3encryptionkey,
Shyam Prasad N45a45462021-03-25 12:34:54 +0000445 SMB3_ENC_DEC_KEY_SIZE);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200446 rc = generate_key(ses, ptriplet->decryption.label,
447 ptriplet->decryption.context,
448 ses->smb3decryptionkey,
Shyam Prasad N45a45462021-03-25 12:34:54 +0000449 SMB3_ENC_DEC_KEY_SIZE);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200450 if (rc)
451 return rc;
452 }
Aurélien Apteld38de3c62017-05-24 16:13:25 +0200453
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 N45a45462021-03-25 12:34:54 +0000465 cifs_dbg(VFS, "Cipher type %d\n", server->cipher_type);
Aurélien Apteld38de3c62017-05-24 16:13:25 +0200466 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 N45a45462021-03-25 12:34:54 +0000470 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 Apteld38de3c62017-05-24 16:13:25 +0200482#endif
483 return rc;
Steve French373512e2015-12-18 13:05:30 -0600484}
485
486int
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000487generate_smb30signingkey(struct cifs_ses *ses,
488 struct TCP_Server_Info *server)
Steve French373512e2015-12-18 13:05:30 -0600489
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 Nf486ef82021-07-19 13:54:16 +0000512 return generate_smb3signingkey(ses, server, &triplet);
Steve French373512e2015-12-18 13:05:30 -0600513}
514
515int
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000516generate_smb311signingkey(struct cifs_ses *ses,
517 struct TCP_Server_Info *server)
Steve French373512e2015-12-18 13:05:30 -0600518
519{
520 struct derivation_triplet triplet;
521 struct derivation *d;
522
523 d = &triplet.signing;
Steve French06e22902017-09-25 20:11:58 -0500524 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 French373512e2015-12-18 13:05:30 -0600528
529 d = &triplet.encryption;
Steve French06e22902017-09-25 20:11:58 -0500530 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 French373512e2015-12-18 13:05:30 -0600534
535 d = &triplet.decryption;
Steve French06e22902017-09-25 20:11:58 -0500536 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 French373512e2015-12-18 13:05:30 -0600540
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000541 return generate_smb3signingkey(ses, server, &triplet);
Steve French373512e2015-12-18 13:05:30 -0600542}
543
Steve French429b46f2013-06-26 23:45:05 -0500544int
Long Lieda1c542020-03-31 16:21:43 -0700545smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server,
546 bool allocate_crypto)
Steve French38107d42012-12-08 22:08:06 -0600547{
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300548 int rc;
Steve French429b46f2013-06-26 23:45:05 -0500549 unsigned char smb3_signature[SMB2_CMACAES_SIZE];
550 unsigned char *sigptr = smb3_signature;
551 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900552 struct smb2_hdr *shdr = (struct smb2_hdr *)iov[0].iov_base;
Long Lieda1c542020-03-31 16:21:43 -0700553 struct shash_desc *shash;
554 struct crypto_shash *hash;
555 struct sdesc *sdesc = NULL;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300556 struct smb_rqst drqst;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200557 u8 key[SMB3_SIGN_KEY_SIZE];
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500558
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900559 rc = smb2_get_sign_key(le64_to_cpu(shdr->SessionId), server, key);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200560 if (rc)
Shirish Pargaonkar32811d22013-08-29 08:35:11 -0500561 return 0;
Steve French429b46f2013-06-26 23:45:05 -0500562
Long Lieda1c542020-03-31 16:21:43 -0700563 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 French429b46f2013-06-26 23:45:05 -0500574 memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700575 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500576
Long Lieda1c542020-03-31 16:21:43 -0700577 rc = crypto_shash_setkey(hash, key, SMB2_CMACAES_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500578 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000579 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
Long Lieda1c542020-03-31 16:21:43 -0700580 goto out;
Steve French429b46f2013-06-26 23:45:05 -0500581 }
582
Steve French95dc8dd2013-07-04 10:35:21 -0500583 /*
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 Alcantara27c32b42018-06-23 14:52:23 -0300588 rc = crypto_shash_init(shash);
Steve French429b46f2013-06-26 23:45:05 -0500589 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000590 cifs_server_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
Long Lieda1c542020-03-31 16:21:43 -0700591 goto out;
Steve French429b46f2013-06-26 23:45:05 -0500592 }
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100593
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300594 /*
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 Sahlbergafe6f652019-08-28 17:15:35 +1000606 cifs_server_dbg(VFS, "%s: Could not update with payload\n",
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300607 __func__);
Long Lieda1c542020-03-31 16:21:43 -0700608 goto out;
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300609 }
610 drqst.rq_iov++;
611 drqst.rq_nvec--;
612 }
Steve French429b46f2013-06-26 23:45:05 -0500613
Paulo Alcantara27c32b42018-06-23 14:52:23 -0300614 rc = __cifs_calc_signature(&drqst, server, sigptr, shash);
Al Viro16c568e2015-11-12 22:46:49 -0500615 if (!rc)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700616 memcpy(shdr->Signature, sigptr, SMB2_SIGNATURE_SIZE);
Steve French429b46f2013-06-26 23:45:05 -0500617
Long Lieda1c542020-03-31 16:21:43 -0700618out:
619 if (allocate_crypto)
620 cifs_free_hash(&hash, &sdesc);
Steve French429b46f2013-06-26 23:45:05 -0500621 return rc;
Steve French38107d42012-12-08 22:08:06 -0600622}
623
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700624/* must be called with server->srv_mutex held */
625static int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700626smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700627{
628 int rc = 0;
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900629 struct smb2_hdr *shdr;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200630 struct smb2_sess_setup_req *ssr;
631 bool is_binding;
632 bool is_signed;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700633
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900634 shdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200635 ssr = (struct smb2_sess_setup_req *)shdr;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700636
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200637 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 N080dc5e2021-07-19 17:05:53 +0000643 spin_lock(&cifs_tcp_ses_lock);
644 if (server->tcpStatus == CifsNeedNegotiate) {
645 spin_unlock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200646 return 0;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000647 }
648 spin_unlock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200649 if (!is_binding && !server->session_estab) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700650 strncpy(shdr->Signature, "BSRSPYL", 8);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +0200651 return 0;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700652 }
653
Long Lieda1c542020-03-31 16:21:43 -0700654 rc = server->ops->calc_signature(rqst, server, false);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700655
656 return rc;
657}
658
659int
Jeff Layton0b688cf2012-09-18 16:20:34 -0700660smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700661{
662 unsigned int rc;
Steve Frenchedad7342020-03-27 12:47:41 -0500663 char server_response_sig[SMB2_SIGNATURE_SIZE];
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900664 struct smb2_hdr *shdr =
665 (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700666
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700667 if ((shdr->Command == SMB2_NEGOTIATE) ||
668 (shdr->Command == SMB2_SESSION_SETUP) ||
669 (shdr->Command == SMB2_OPLOCK_BREAK) ||
Steve French4f5c10f2019-09-03 21:18:49 -0500670 server->ignore_signature ||
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700671 (!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 Shilovsky31473fc2016-10-24 15:33:04 -0700680 if (memcmp(shdr->Signature, "BSRSPYL ", 8) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500681 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700682 shdr->Command);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700683
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 Shilovsky31473fc2016-10-24 15:33:04 -0700688 memcpy(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700689
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700690 memset(shdr->Signature, 0, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700691
Long Lieda1c542020-03-31 16:21:43 -0700692 rc = server->ops->calc_signature(rqst, server, true);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700693
694 if (rc)
695 return rc;
696
Steve Frenchf460c502020-03-29 16:44:43 -0500697 if (memcmp(server_response_sig, shdr->Signature, SMB2_SIGNATURE_SIZE)) {
Steve French9692ea92020-04-15 01:12:34 -0500698 cifs_dbg(VFS, "sign fail cmd 0x%x message id 0x%llx\n",
699 shdr->Command, shdr->MessageId);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700700 return -EACCES;
Steve Frenchf460c502020-03-29 16:44:43 -0500701 } else
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700702 return 0;
703}
704
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400705/*
706 * Set message id for the request. Should be called after wait_for_free_request
707 * and when srv_mutex is held.
708 */
709static inline void
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700710smb2_seq_num_into_buf(struct TCP_Server_Info *server,
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900711 struct smb2_hdr *shdr)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400712{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700713 unsigned int i, num = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400714
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700715 shdr->MessageId = get_next_mid64(server);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400716 /* skip message numbers according to CreditCharge field */
717 for (i = 1; i < num; i++)
718 get_next_mid(server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400719}
720
721static struct mid_q_entry *
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900722smb2_mid_entry_alloc(const struct smb2_hdr *shdr,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400723 struct TCP_Server_Info *server)
724{
725 struct mid_q_entry *temp;
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800726 unsigned int credits = le16_to_cpu(shdr->CreditCharge);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400727
728 if (server == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500729 cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400730 return NULL;
731 }
732
733 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
NeilBrowna6f74e82017-04-10 12:08:53 +1000734 memset(temp, 0, sizeof(struct mid_q_entry));
Lars Persson696e4202018-06-25 14:05:25 +0200735 kref_init(&temp->refcount);
NeilBrowna6f74e82017-04-10 12:08:53 +1000736 temp->mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800737 temp->credits = credits > 0 ? credits : 1;
NeilBrowna6f74e82017-04-10 12:08:53 +1000738 temp->pid = current->pid;
739 temp->command = shdr->Command; /* Always LE */
740 temp->when_alloc = jiffies;
741 temp->server = server;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400742
NeilBrowna6f74e82017-04-10 12:08:53 +1000743 /*
744 * The default is for the mid to be synchronous, so the
745 * default callback just wakes up the current task.
746 */
Vincent Whitchurchf1f27ad2020-01-23 17:09:06 +0100747 get_task_struct(current);
748 temp->creator = current;
NeilBrowna6f74e82017-04-10 12:08:53 +1000749 temp->callback = cifs_wake_up_task;
750 temp->callback_data = current;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400751
752 atomic_inc(&midCount);
753 temp->mid_state = MID_REQUEST_ALLOCATED;
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900754 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 Shilovsky2dc7e1c2011-12-26 22:53:34 +0400757 return temp;
758}
759
760static int
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200761smb2_get_mid_entry(struct cifs_ses *ses, struct TCP_Server_Info *server,
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900762 struct smb2_hdr *shdr, struct mid_q_entry **mid)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400763{
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000764 spin_lock(&cifs_tcp_ses_lock);
765 if (server->tcpStatus == CifsExiting) {
766 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400767 return -ENOENT;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000768 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400769
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200770 if (server->tcpStatus == CifsNeedReconnect) {
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000771 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -0500772 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400773 return -EAGAIN;
774 }
775
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200776 if (server->tcpStatus == CifsNeedNegotiate &&
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000777 shdr->Command != SMB2_NEGOTIATE) {
778 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800779 return -EAGAIN;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000780 }
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800781
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500782 if (ses->status == CifsNew) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700783 if ((shdr->Command != SMB2_SESSION_SETUP) &&
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000784 (shdr->Command != SMB2_NEGOTIATE)) {
785 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400786 return -EAGAIN;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000787 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400788 /* else ok - we are setting up session */
789 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500790
791 if (ses->status == CifsExiting) {
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000792 if (shdr->Command != SMB2_LOGOFF) {
793 spin_unlock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500794 return -EAGAIN;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000795 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500796 /* else ok - we are shutting down the session */
797 }
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000798 spin_unlock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -0500799
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200800 *mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400801 if (*mid == NULL)
802 return -ENOMEM;
803 spin_lock(&GlobalMid_Lock);
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200804 list_add_tail(&(*mid)->qhead, &server->pending_mid_q);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400805 spin_unlock(&GlobalMid_Lock);
Steve French53a3e0d2019-02-26 21:26:20 -0600806
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400807 return 0;
808}
809
810int
811smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
812 bool log_error)
813{
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000814 unsigned int len = mid->resp_buf_size;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000815 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800816 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000817 .rq_nvec = 1 };
Jeff Layton0b688cf2012-09-18 16:20:34 -0700818
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800819 iov[0].iov_base = (char *)mid->resp_buf;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000820 iov[0].iov_len = len;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400821
822 dump_smb(mid->resp_buf, min_t(u32, 80, len));
823 /* convert the length into a more usable form */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800824 if (len > 24 && server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700825 int rc;
826
Jeff Layton0b688cf2012-09-18 16:20:34 -0700827 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700828 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000829 cifs_server_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500830 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700831 }
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400832
833 return map_smb2_to_linux_error(mid->resp_buf, log_error);
834}
835
Jeff Laytonfec344e2012-09-18 16:20:35 -0700836struct mid_q_entry *
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200837smb2_setup_request(struct cifs_ses *ses, struct TCP_Server_Info *server,
838 struct smb_rqst *rqst)
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400839{
840 int rc;
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900841 struct smb2_hdr *shdr =
842 (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400843 struct mid_q_entry *mid;
844
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200845 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400846
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200847 rc = smb2_get_mid_entry(ses, server, shdr, &mid);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800848 if (rc) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200849 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700850 return ERR_PTR(rc);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800851 }
852
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200853 rc = smb2_sign_rqst(rqst, server);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700854 if (rc) {
Aurelien Aptelf780bd32019-09-20 06:08:34 +0200855 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700856 cifs_delete_mid(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700857 return ERR_PTR(rc);
858 }
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800859
Jeff Laytonfec344e2012-09-18 16:20:35 -0700860 return mid;
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400861}
862
Jeff Laytonfec344e2012-09-18 16:20:35 -0700863struct mid_q_entry *
864smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400865{
Jeff Laytonfec344e2012-09-18 16:20:35 -0700866 int rc;
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900867 struct smb2_hdr *shdr =
868 (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400869 struct mid_q_entry *mid;
870
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000871 spin_lock(&cifs_tcp_ses_lock);
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800872 if (server->tcpStatus == CifsNeedNegotiate &&
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000873 shdr->Command != SMB2_NEGOTIATE) {
874 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800875 return ERR_PTR(-EAGAIN);
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000876 }
877 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovsky2084ed52019-03-05 15:51:55 -0800878
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700879 smb2_seq_num_into_buf(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400880
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700881 mid = smb2_mid_entry_alloc(shdr, server);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800882 if (mid == NULL) {
883 revert_current_mid_from_hdr(server, shdr);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700884 return ERR_PTR(-ENOMEM);
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800885 }
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400886
Jeff Laytonfec344e2012-09-18 16:20:35 -0700887 rc = smb2_sign_rqst(rqst, server);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400888 if (rc) {
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800889 revert_current_mid_from_hdr(server, shdr);
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400890 DeleteMidQEntry(mid);
Jeff Laytonfec344e2012-09-18 16:20:35 -0700891 return ERR_PTR(rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700892 }
893
Jeff Laytonfec344e2012-09-18 16:20:35 -0700894 return mid;
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400895}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700896
897int
898smb3_crypto_aead_allocate(struct TCP_Server_Info *server)
899{
900 struct crypto_aead *tfm;
901
902 if (!server->secmech.ccmaesencrypt) {
Steve French63ca5652020-10-15 23:41:40 -0500903 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
904 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve French2b2f7542019-06-07 15:16:10 -0500905 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
906 else
907 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700908 if (IS_ERR(tfm)) {
Steve French63ca5652020-10-15 23:41:40 -0500909 cifs_server_dbg(VFS, "%s: Failed alloc encrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700910 __func__);
911 return PTR_ERR(tfm);
912 }
913 server->secmech.ccmaesencrypt = tfm;
914 }
915
916 if (!server->secmech.ccmaesdecrypt) {
Steve French63ca5652020-10-15 23:41:40 -0500917 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
918 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve French2b2f7542019-06-07 15:16:10 -0500919 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
920 else
921 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700922 if (IS_ERR(tfm)) {
923 crypto_free_aead(server->secmech.ccmaesencrypt);
924 server->secmech.ccmaesencrypt = NULL;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000925 cifs_server_dbg(VFS, "%s: Failed to alloc decrypt aead\n",
Pavel Shilovsky026e93d2016-11-03 16:47:37 -0700926 __func__);
927 return PTR_ERR(tfm);
928 }
929 server->secmech.ccmaesdecrypt = tfm;
930 }
931
932 return 0;
933}