smb2transport.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  42. {
  43. int i, rc;
  44. unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
  45. unsigned char *sigptr = smb2_signature;
  46. struct kvec *iov = rqst->rq_iov;
  47. int n_vec = rqst->rq_nvec;
  48. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)iov[0].iov_base;
  49. memset(smb2_signature, 0x0, SMB2_HMACSHA256_SIZE);
  50. memset(smb2_pdu->Signature, 0x0, SMB2_SIGNATURE_SIZE);
  51. rc = crypto_shash_setkey(server->secmech.hmacsha256,
  52. server->session_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
  53. if (rc) {
  54. cERROR(1, "%s: Could not update with response\n", __func__);
  55. return rc;
  56. }
  57. rc = crypto_shash_init(&server->secmech.sdeschmacsha256->shash);
  58. if (rc) {
  59. cERROR(1, "%s: Could not init md5\n", __func__);
  60. return rc;
  61. }
  62. for (i = 0; i < n_vec; i++) {
  63. if (iov[i].iov_len == 0)
  64. continue;
  65. if (iov[i].iov_base == NULL) {
  66. cERROR(1, "null iovec entry");
  67. return -EIO;
  68. }
  69. /*
  70. * The first entry includes a length field (which does not get
  71. * signed that occupies the first 4 bytes before the header).
  72. */
  73. if (i == 0) {
  74. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  75. break; /* nothing to sign or corrupt header */
  76. rc =
  77. crypto_shash_update(
  78. &server->secmech.sdeschmacsha256->shash,
  79. iov[i].iov_base + 4, iov[i].iov_len - 4);
  80. } else {
  81. rc =
  82. crypto_shash_update(
  83. &server->secmech.sdeschmacsha256->shash,
  84. iov[i].iov_base, iov[i].iov_len);
  85. }
  86. if (rc) {
  87. cERROR(1, "%s: Could not update with payload\n",
  88. __func__);
  89. return rc;
  90. }
  91. }
  92. /* now hash over the rq_pages array */
  93. for (i = 0; i < rqst->rq_npages; i++) {
  94. struct kvec p_iov;
  95. cifs_rqst_page_to_kvec(rqst, i, &p_iov);
  96. crypto_shash_update(&server->secmech.sdeschmacsha256->shash,
  97. p_iov.iov_base, p_iov.iov_len);
  98. kunmap(rqst->rq_pages[i]);
  99. }
  100. rc = crypto_shash_final(&server->secmech.sdeschmacsha256->shash,
  101. sigptr);
  102. if (rc)
  103. cERROR(1, "%s: Could not generate sha256 hash\n", __func__);
  104. memcpy(smb2_pdu->Signature, sigptr, SMB2_SIGNATURE_SIZE);
  105. return rc;
  106. }
  107. /* must be called with server->srv_mutex held */
  108. static int
  109. smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  110. {
  111. int rc = 0;
  112. struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
  113. if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
  114. server->tcpStatus == CifsNeedNegotiate)
  115. return rc;
  116. if (!server->session_estab) {
  117. strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
  118. return rc;
  119. }
  120. rc = smb2_calc_signature(rqst, server);
  121. return rc;
  122. }
  123. int
  124. smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  125. {
  126. unsigned int rc;
  127. char server_response_sig[16];
  128. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  129. if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
  130. (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
  131. (!server->session_estab))
  132. return 0;
  133. /*
  134. * BB what if signatures are supposed to be on for session but
  135. * server does not send one? BB
  136. */
  137. /* Do not need to verify session setups with signature "BSRSPYL " */
  138. if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
  139. cFYI(1, "dummy signature received for smb command 0x%x",
  140. smb2_pdu->Command);
  141. /*
  142. * Save off the origiginal signature so we can modify the smb and check
  143. * our calculated signature against what the server sent.
  144. */
  145. memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
  146. memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
  147. mutex_lock(&server->srv_mutex);
  148. rc = smb2_calc_signature(rqst, server);
  149. mutex_unlock(&server->srv_mutex);
  150. if (rc)
  151. return rc;
  152. if (memcmp(server_response_sig, smb2_pdu->Signature,
  153. SMB2_SIGNATURE_SIZE))
  154. return -EACCES;
  155. else
  156. return 0;
  157. }
  158. /*
  159. * Set message id for the request. Should be called after wait_for_free_request
  160. * and when srv_mutex is held.
  161. */
  162. static inline void
  163. smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
  164. {
  165. hdr->MessageId = get_next_mid(server);
  166. }
  167. static struct mid_q_entry *
  168. smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
  169. struct TCP_Server_Info *server)
  170. {
  171. struct mid_q_entry *temp;
  172. if (server == NULL) {
  173. cERROR(1, "Null TCP session in smb2_mid_entry_alloc");
  174. return NULL;
  175. }
  176. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  177. if (temp == NULL)
  178. return temp;
  179. else {
  180. memset(temp, 0, sizeof(struct mid_q_entry));
  181. temp->mid = smb_buffer->MessageId; /* always LE */
  182. temp->pid = current->pid;
  183. temp->command = smb_buffer->Command; /* Always LE */
  184. temp->when_alloc = jiffies;
  185. temp->server = server;
  186. /*
  187. * The default is for the mid to be synchronous, so the
  188. * default callback just wakes up the current task.
  189. */
  190. temp->callback = cifs_wake_up_task;
  191. temp->callback_data = current;
  192. }
  193. atomic_inc(&midCount);
  194. temp->mid_state = MID_REQUEST_ALLOCATED;
  195. return temp;
  196. }
  197. static int
  198. smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
  199. struct mid_q_entry **mid)
  200. {
  201. if (ses->server->tcpStatus == CifsExiting)
  202. return -ENOENT;
  203. if (ses->server->tcpStatus == CifsNeedReconnect) {
  204. cFYI(1, "tcp session dead - return to caller to retry");
  205. return -EAGAIN;
  206. }
  207. if (ses->status != CifsGood) {
  208. /* check if SMB2 session is bad because we are setting it up */
  209. if ((buf->Command != SMB2_SESSION_SETUP) &&
  210. (buf->Command != SMB2_NEGOTIATE))
  211. return -EAGAIN;
  212. /* else ok - we are setting up session */
  213. }
  214. *mid = smb2_mid_entry_alloc(buf, ses->server);
  215. if (*mid == NULL)
  216. return -ENOMEM;
  217. spin_lock(&GlobalMid_Lock);
  218. list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
  219. spin_unlock(&GlobalMid_Lock);
  220. return 0;
  221. }
  222. int
  223. smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  224. bool log_error)
  225. {
  226. unsigned int len = get_rfc1002_length(mid->resp_buf);
  227. struct kvec iov;
  228. struct smb_rqst rqst = { .rq_iov = &iov,
  229. .rq_nvec = 1 };
  230. iov.iov_base = (char *)mid->resp_buf;
  231. iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
  232. dump_smb(mid->resp_buf, min_t(u32, 80, len));
  233. /* convert the length into a more usable form */
  234. if ((len > 24) &&
  235. (server->sec_mode & (SECMODE_SIGN_REQUIRED|SECMODE_SIGN_ENABLED))) {
  236. int rc;
  237. rc = smb2_verify_signature(&rqst, server);
  238. if (rc)
  239. cERROR(1, "SMB signature verification returned error = "
  240. "%d", rc);
  241. }
  242. return map_smb2_to_linux_error(mid->resp_buf, log_error);
  243. }
  244. struct mid_q_entry *
  245. smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  246. {
  247. int rc;
  248. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  249. struct mid_q_entry *mid;
  250. smb2_seq_num_into_buf(ses->server, hdr);
  251. rc = smb2_get_mid_entry(ses, hdr, &mid);
  252. if (rc)
  253. return ERR_PTR(rc);
  254. rc = smb2_sign_rqst(rqst, ses->server);
  255. if (rc) {
  256. cifs_delete_mid(mid);
  257. return ERR_PTR(rc);
  258. }
  259. return mid;
  260. }
  261. struct mid_q_entry *
  262. smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  263. {
  264. int rc;
  265. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  266. struct mid_q_entry *mid;
  267. smb2_seq_num_into_buf(server, hdr);
  268. mid = smb2_mid_entry_alloc(hdr, server);
  269. if (mid == NULL)
  270. return ERR_PTR(-ENOMEM);
  271. rc = smb2_sign_rqst(rqst, server);
  272. if (rc) {
  273. DeleteMidQEntry(mid);
  274. return ERR_PTR(rc);
  275. }
  276. return mid;
  277. }