smb2transport.c 23 KB

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