smb2transport.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * fs/cifs/smb2transport.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002, 2011
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Jeremy Allison (jra@samba.org) 2006
  8. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/list.h>
  26. #include <linux/wait.h>
  27. #include <linux/net.h>
  28. #include <linux/delay.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/processor.h>
  31. #include <linux/mempool.h>
  32. #include <linux/highmem.h>
  33. #include "smb2pdu.h"
  34. #include "cifsglob.h"
  35. #include "cifsproto.h"
  36. #include "smb2proto.h"
  37. #include "cifs_debug.h"
  38. #include "smb2status.h"
  39. #include "smb2glob.h"
  40. static int
  41. smb2_crypto_shash_allocate(struct TCP_Server_Info *server)
  42. {
  43. int rc;
  44. unsigned int size;
  45. if (server->secmech.sdeschmacsha256 != NULL)
  46. return 0; /* already allocated */
  47. server->secmech.hmacsha256 = crypto_alloc_shash("hmac(sha256)", 0, 0);
  48. if (IS_ERR(server->secmech.hmacsha256)) {
  49. cifs_dbg(VFS, "could not allocate crypto hmacsha256\n");
  50. rc = PTR_ERR(server->secmech.hmacsha256);
  51. server->secmech.hmacsha256 = NULL;
  52. return rc;
  53. }
  54. size = sizeof(struct shash_desc) +
  55. crypto_shash_descsize(server->secmech.hmacsha256);
  56. server->secmech.sdeschmacsha256 = kmalloc(size, GFP_KERNEL);
  57. if (!server->secmech.sdeschmacsha256) {
  58. crypto_free_shash(server->secmech.hmacsha256);
  59. server->secmech.hmacsha256 = NULL;
  60. return -ENOMEM;
  61. }
  62. server->secmech.sdeschmacsha256->shash.tfm = server->secmech.hmacsha256;
  63. server->secmech.sdeschmacsha256->shash.flags = 0x0;
  64. return 0;
  65. }
  66. static int
  67. smb3_crypto_shash_allocate(struct TCP_Server_Info *server)
  68. {
  69. unsigned int size;
  70. int rc;
  71. if (server->secmech.sdesccmacaes != NULL)
  72. return 0; /* already allocated */
  73. rc = smb2_crypto_shash_allocate(server);
  74. if (rc)
  75. return rc;
  76. server->secmech.cmacaes = crypto_alloc_shash("cmac(aes)", 0, 0);
  77. if (IS_ERR(server->secmech.cmacaes)) {
  78. cifs_dbg(VFS, "could not allocate crypto cmac-aes");
  79. kfree(server->secmech.sdeschmacsha256);
  80. server->secmech.sdeschmacsha256 = NULL;
  81. crypto_free_shash(server->secmech.hmacsha256);
  82. server->secmech.hmacsha256 = NULL;
  83. rc = PTR_ERR(server->secmech.cmacaes);
  84. server->secmech.cmacaes = NULL;
  85. return rc;
  86. }
  87. size = sizeof(struct shash_desc) +
  88. crypto_shash_descsize(server->secmech.cmacaes);
  89. server->secmech.sdesccmacaes = kmalloc(size, GFP_KERNEL);
  90. if (!server->secmech.sdesccmacaes) {
  91. cifs_dbg(VFS, "%s: Can't alloc cmacaes\n", __func__);
  92. kfree(server->secmech.sdeschmacsha256);
  93. server->secmech.sdeschmacsha256 = NULL;
  94. crypto_free_shash(server->secmech.hmacsha256);
  95. crypto_free_shash(server->secmech.cmacaes);
  96. server->secmech.hmacsha256 = NULL;
  97. server->secmech.cmacaes = NULL;
  98. return -ENOMEM;
  99. }
  100. server->secmech.sdesccmacaes->shash.tfm = server->secmech.cmacaes;
  101. server->secmech.sdesccmacaes->shash.flags = 0x0;
  102. return 0;
  103. }
  104. static struct cifs_ses *
  105. smb2_find_smb_ses(struct smb2_hdr *smb2hdr, struct TCP_Server_Info *server)
  106. {
  107. struct cifs_ses *ses;
  108. spin_lock(&cifs_tcp_ses_lock);
  109. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  110. if (ses->Suid != smb2hdr->SessionId)
  111. continue;
  112. spin_unlock(&cifs_tcp_ses_lock);
  113. return ses;
  114. }
  115. spin_unlock(&cifs_tcp_ses_lock);
  116. return NULL;
  117. }
  118. int
  119. smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  120. {
  121. int rc;
  122. unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
  123. unsigned char *sigptr = smb2_signature;
  124. struct kvec *iov = rqst->rq_iov;
  125. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  126. struct cifs_ses *ses;
  127. ses = smb2_find_smb_ses(smb2_pdu, server);
  128. if (!ses) {
  129. cifs_dbg(VFS, "%s: Could not find session\n", __func__);
  130. return 0;
  131. }
  132. memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
  133. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  134. rc = smb2_crypto_shash_allocate(server);
  135. if (rc) {
  136. cifs_dbg(VFS, "%s: shah256 alloc failed\n", __func__);
  137. return rc;
  138. }
  139. rc = crypto_shash_setkey(server->secmech.hmacsha256,
  140. ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  141. if (rc) {
  142. cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
  143. return rc;
  144. }
  145. rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
  146. if (rc) {
  147. cifs_dbg(VFS, "%s: Could not init sha256", __func__);
  148. return rc;
  149. }
  150. rc = __cifs_calc_signature(rqst, server, sigptr,
  151. &server->secmech.sdeschmacsha256->shash);
  152. if (!rc)
  153. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  154. return rc;
  155. }
  156. static int generate_key(struct cifs_ses *ses, struct kvec label,
  157. struct kvec context, __u8 *key, unsigned int key_size)
  158. {
  159. unsigned char zero = 0x0;
  160. __u8 i[4] = {0, 0, 0, 1};
  161. __u8 L[4] = {0, 0, 0, 128};
  162. int rc = 0;
  163. unsigned char prfhash[SMB2_HMACSHA256_SIZE];
  164. unsigned char *hashptr = prfhash;
  165. memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
  166. memset(key, 0x0, key_size);
  167. rc = smb3_crypto_shash_allocate(ses->server);
  168. if (rc) {
  169. cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
  170. goto smb3signkey_ret;
  171. }
  172. rc = crypto_shash_setkey(ses->server->secmech.hmacsha256,
  173. ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  174. if (rc) {
  175. cifs_dbg(VFS, "%s: Could not set with session key\n", __func__);
  176. goto smb3signkey_ret;
  177. }
  178. rc = crypto_shash_init(&ses->server->secmech.sdeschmacsha256->shash);
  179. if (rc) {
  180. cifs_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
  181. goto smb3signkey_ret;
  182. }
  183. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  184. i, 4);
  185. if (rc) {
  186. cifs_dbg(VFS, "%s: Could not update with n\n", __func__);
  187. goto smb3signkey_ret;
  188. }
  189. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  190. label.iov_base, label.iov_len);
  191. if (rc) {
  192. cifs_dbg(VFS, "%s: Could not update with label\n", __func__);
  193. goto smb3signkey_ret;
  194. }
  195. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  196. &zero, 1);
  197. if (rc) {
  198. cifs_dbg(VFS, "%s: Could not update with zero\n", __func__);
  199. goto smb3signkey_ret;
  200. }
  201. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  202. context.iov_base, context.iov_len);
  203. if (rc) {
  204. cifs_dbg(VFS, "%s: Could not update with context\n", __func__);
  205. goto smb3signkey_ret;
  206. }
  207. rc = crypto_shash_update(&ses->server->secmech.sdeschmacsha256->shash,
  208. L, 4);
  209. if (rc) {
  210. cifs_dbg(VFS, "%s: Could not update with L\n", __func__);
  211. goto smb3signkey_ret;
  212. }
  213. rc = crypto_shash_final(&ses->server->secmech.sdeschmacsha256->shash,
  214. hashptr);
  215. if (rc) {
  216. cifs_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
  217. goto smb3signkey_ret;
  218. }
  219. memcpy(key, hashptr, key_size);
  220. smb3signkey_ret:
  221. return rc;
  222. }
  223. struct derivation {
  224. struct kvec label;
  225. struct kvec context;
  226. };
  227. struct derivation_triplet {
  228. struct derivation signing;
  229. struct derivation encryption;
  230. struct derivation decryption;
  231. };
  232. static int
  233. generate_smb3signingkey(struct cifs_ses *ses,
  234. const struct derivation_triplet *ptriplet)
  235. {
  236. int rc;
  237. rc = generate_key(ses, ptriplet->signing.label,
  238. ptriplet->signing.context, ses->smb3signingkey,
  239. SMB3_SIGN_KEY_SIZE);
  240. if (rc)
  241. return rc;
  242. rc = generate_key(ses, ptriplet->encryption.label,
  243. ptriplet->encryption.context, ses->smb3encryptionkey,
  244. SMB3_SIGN_KEY_SIZE);
  245. if (rc)
  246. return rc;
  247. return generate_key(ses, ptriplet->decryption.label,
  248. ptriplet->decryption.context,
  249. ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
  250. }
  251. int
  252. generate_smb30signingkey(struct cifs_ses *ses)
  253. {
  254. struct derivation_triplet triplet;
  255. struct derivation *d;
  256. d = &triplet.signing;
  257. d->label.iov_base = "SMB2AESCMAC";
  258. d->label.iov_len = 12;
  259. d->context.iov_base = "SmbSign";
  260. d->context.iov_len = 8;
  261. d = &triplet.encryption;
  262. d->label.iov_base = "SMB2AESCCM";
  263. d->label.iov_len = 11;
  264. d->context.iov_base = "ServerIn ";
  265. d->context.iov_len = 10;
  266. d = &triplet.decryption;
  267. d->label.iov_base = "SMB2AESCCM";
  268. d->label.iov_len = 11;
  269. d->context.iov_base = "ServerOut";
  270. d->context.iov_len = 10;
  271. return generate_smb3signingkey(ses, &triplet);
  272. }
  273. int
  274. generate_smb311signingkey(struct cifs_ses *ses)
  275. {
  276. struct derivation_triplet triplet;
  277. struct derivation *d;
  278. d = &triplet.signing;
  279. d->label.iov_base = "SMB2AESCMAC";
  280. d->label.iov_len = 12;
  281. d->context.iov_base = "SmbSign";
  282. d->context.iov_len = 8;
  283. d = &triplet.encryption;
  284. d->label.iov_base = "SMB2AESCCM";
  285. d->label.iov_len = 11;
  286. d->context.iov_base = "ServerIn ";
  287. d->context.iov_len = 10;
  288. d = &triplet.decryption;
  289. d->label.iov_base = "SMB2AESCCM";
  290. d->label.iov_len = 11;
  291. d->context.iov_base = "ServerOut";
  292. d->context.iov_len = 10;
  293. return generate_smb3signingkey(ses, &triplet);
  294. }
  295. int
  296. smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  297. {
  298. int rc = 0;
  299. unsigned char smb3_signature[SMB2_CMACAES_SIZE];
  300. unsigned char *sigptr = smb3_signature;
  301. struct kvec *iov = rqst->rq_iov;
  302. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  303. struct cifs_ses *ses;
  304. ses = smb2_find_smb_ses(smb2_pdu, server);
  305. if (!ses) {
  306. cifs_dbg(VFS, "%s: Could not find session\n", __func__);
  307. return 0;
  308. }
  309. memset(smb3_signature, 0x0, SMB2_CMACAES_SIZE);
  310. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  311. rc = crypto_shash_setkey(server->secmech.cmacaes,
  312. ses->smb3signingkey, SMB2_CMACAES_SIZE);
  313. if (rc) {
  314. cifs_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
  315. return rc;
  316. }
  317. /*
  318. * we already allocate sdesccmacaes when we init smb3 signing key,
  319. * so unlike smb2 case we do not have to check here if secmech are
  320. * initialized
  321. */
  322. rc = crypto_shash_init(&server->secmech.sdesccmacaes->shash);
  323. if (rc) {
  324. cifs_dbg(VFS, "%s: Could not init cmac aes\n", __func__);
  325. return rc;
  326. }
  327. rc = __cifs_calc_signature(rqst, server, sigptr,
  328. &server->secmech.sdesccmacaes->shash);
  329. if (!rc)
  330. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  331. return rc;
  332. }
  333. /* must be called with server->srv_mutex held */
  334. static int
  335. smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  336. {
  337. int rc = 0;
  338. struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
  339. if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
  340. server->tcpStatus == CifsNeedNegotiate)
  341. return rc;
  342. if (!server->session_estab) {
  343. strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
  344. return rc;
  345. }
  346. rc = server->ops->calc_signature(rqst, server);
  347. return rc;
  348. }
  349. int
  350. smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  351. {
  352. unsigned int rc;
  353. char server_response_sig[16];
  354. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  355. if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
  356. (smb2_pdu->Command == SMB2_SESSION_SETUP) ||
  357. (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
  358. (!server->session_estab))
  359. return 0;
  360. /*
  361. * BB what if signatures are supposed to be on for session but
  362. * server does not send one? BB
  363. */
  364. /* Do not need to verify session setups with signature "BSRSPYL " */
  365. if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
  366. cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
  367. smb2_pdu->Command);
  368. /*
  369. * Save off the origiginal signature so we can modify the smb and check
  370. * our calculated signature against what the server sent.
  371. */
  372. memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
  373. memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
  374. mutex_lock(&server->srv_mutex);
  375. rc = server->ops->calc_signature(rqst, server);
  376. mutex_unlock(&server->srv_mutex);
  377. if (rc)
  378. return rc;
  379. if (memcmp(server_response_sig, smb2_pdu->Signature,
  380. SMB2_SIGNATURE_SIZE))
  381. return -EACCES;
  382. else
  383. return 0;
  384. }
  385. /*
  386. * Set message id for the request. Should be called after wait_for_free_request
  387. * and when srv_mutex is held.
  388. */
  389. static inline void
  390. smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
  391. {
  392. unsigned int i, num = le16_to_cpu(hdr->CreditCharge);
  393. hdr->MessageId = get_next_mid64(server);
  394. /* skip message numbers according to CreditCharge field */
  395. for (i = 1; i < num; i++)
  396. get_next_mid(server);
  397. }
  398. static struct mid_q_entry *
  399. smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
  400. struct TCP_Server_Info *server)
  401. {
  402. struct mid_q_entry *temp;
  403. if (server == NULL) {
  404. cifs_dbg(VFS, "Null TCP session in smb2_mid_entry_alloc\n");
  405. return NULL;
  406. }
  407. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  408. if (temp == NULL)
  409. return temp;
  410. else {
  411. memset(temp, 0, sizeof(struct mid_q_entry));
  412. temp->mid = le64_to_cpu(smb_buffer->MessageId);
  413. temp->pid = current->pid;
  414. temp->command = smb_buffer->Command; /* Always LE */
  415. temp->when_alloc = jiffies;
  416. temp->server = server;
  417. /*
  418. * The default is for the mid to be synchronous, so the
  419. * default callback just wakes up the current task.
  420. */
  421. temp->callback = cifs_wake_up_task;
  422. temp->callback_data = current;
  423. }
  424. atomic_inc(&midCount);
  425. temp->mid_state = MID_REQUEST_ALLOCATED;
  426. return temp;
  427. }
  428. static int
  429. smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
  430. struct mid_q_entry **mid)
  431. {
  432. if (ses->server->tcpStatus == CifsExiting)
  433. return -ENOENT;
  434. if (ses->server->tcpStatus == CifsNeedReconnect) {
  435. cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
  436. return -EAGAIN;
  437. }
  438. if (ses->status == CifsNew) {
  439. if ((buf->Command != SMB2_SESSION_SETUP) &&
  440. (buf->Command != SMB2_NEGOTIATE))
  441. return -EAGAIN;
  442. /* else ok - we are setting up session */
  443. }
  444. if (ses->status == CifsExiting) {
  445. if (buf->Command != SMB2_LOGOFF)
  446. return -EAGAIN;
  447. /* else ok - we are shutting down the session */
  448. }
  449. *mid = smb2_mid_entry_alloc(buf, ses->server);
  450. if (*mid == NULL)
  451. return -ENOMEM;
  452. spin_lock(&GlobalMid_Lock);
  453. list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
  454. spin_unlock(&GlobalMid_Lock);
  455. return 0;
  456. }
  457. int
  458. smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  459. bool log_error)
  460. {
  461. unsigned int len = get_rfc1002_length(mid->resp_buf);
  462. struct kvec iov;
  463. struct smb_rqst rqst = { .rq_iov = &iov,
  464. .rq_nvec = 1 };
  465. iov.iov_base = (char *)mid->resp_buf;
  466. iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
  467. dump_smb(mid->resp_buf, min_t(u32, 80, len));
  468. /* convert the length into a more usable form */
  469. if (len > 24 && server->sign) {
  470. int rc;
  471. rc = smb2_verify_signature(&rqst, server);
  472. if (rc)
  473. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  474. rc);
  475. }
  476. return map_smb2_to_linux_error(mid->resp_buf, log_error);
  477. }
  478. struct mid_q_entry *
  479. smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  480. {
  481. int rc;
  482. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  483. struct mid_q_entry *mid;
  484. smb2_seq_num_into_buf(ses->server, hdr);
  485. rc = smb2_get_mid_entry(ses, hdr, &mid);
  486. if (rc)
  487. return ERR_PTR(rc);
  488. rc = smb2_sign_rqst(rqst, ses->server);
  489. if (rc) {
  490. cifs_delete_mid(mid);
  491. return ERR_PTR(rc);
  492. }
  493. return mid;
  494. }
  495. struct mid_q_entry *
  496. smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  497. {
  498. int rc;
  499. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  500. struct mid_q_entry *mid;
  501. smb2_seq_num_into_buf(server, hdr);
  502. mid = smb2_mid_entry_alloc(hdr, server);
  503. if (mid == NULL)
  504. return ERR_PTR(-ENOMEM);
  505. rc = smb2_sign_rqst(rqst, server);
  506. if (rc) {
  507. DeleteMidQEntry(mid);
  508. return ERR_PTR(rc);
  509. }
  510. return mid;
  511. }