smb2transport.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. 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. int
  108. smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  109. {
  110. cFYI(1, "smb3 signatures not supported yet");
  111. return -EOPNOTSUPP;
  112. }
  113. /* must be called with server->srv_mutex held */
  114. static int
  115. smb2_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  116. {
  117. int rc = 0;
  118. struct smb2_hdr *smb2_pdu = rqst->rq_iov[0].iov_base;
  119. if (!(smb2_pdu->Flags & SMB2_FLAGS_SIGNED) ||
  120. server->tcpStatus == CifsNeedNegotiate)
  121. return rc;
  122. if (!server->session_estab) {
  123. strncpy(smb2_pdu->Signature, "BSRSPYL", 8);
  124. return rc;
  125. }
  126. rc = server->ops->calc_signature(rqst, server);
  127. return rc;
  128. }
  129. int
  130. smb2_verify_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
  131. {
  132. unsigned int rc;
  133. char server_response_sig[16];
  134. struct smb2_hdr *smb2_pdu = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  135. if ((smb2_pdu->Command == SMB2_NEGOTIATE) ||
  136. (smb2_pdu->Command == SMB2_OPLOCK_BREAK) ||
  137. (!server->session_estab))
  138. return 0;
  139. /*
  140. * BB what if signatures are supposed to be on for session but
  141. * server does not send one? BB
  142. */
  143. /* Do not need to verify session setups with signature "BSRSPYL " */
  144. if (memcmp(smb2_pdu->Signature, "BSRSPYL ", 8) == 0)
  145. cFYI(1, "dummy signature received for smb command 0x%x",
  146. smb2_pdu->Command);
  147. /*
  148. * Save off the origiginal signature so we can modify the smb and check
  149. * our calculated signature against what the server sent.
  150. */
  151. memcpy(server_response_sig, smb2_pdu->Signature, SMB2_SIGNATURE_SIZE);
  152. memset(smb2_pdu->Signature, 0, SMB2_SIGNATURE_SIZE);
  153. mutex_lock(&server->srv_mutex);
  154. rc = server->ops->calc_signature(rqst, server);
  155. mutex_unlock(&server->srv_mutex);
  156. if (rc)
  157. return rc;
  158. if (memcmp(server_response_sig, smb2_pdu->Signature,
  159. SMB2_SIGNATURE_SIZE))
  160. return -EACCES;
  161. else
  162. return 0;
  163. }
  164. /*
  165. * Set message id for the request. Should be called after wait_for_free_request
  166. * and when srv_mutex is held.
  167. */
  168. static inline void
  169. smb2_seq_num_into_buf(struct TCP_Server_Info *server, struct smb2_hdr *hdr)
  170. {
  171. hdr->MessageId = get_next_mid(server);
  172. }
  173. static struct mid_q_entry *
  174. smb2_mid_entry_alloc(const struct smb2_hdr *smb_buffer,
  175. struct TCP_Server_Info *server)
  176. {
  177. struct mid_q_entry *temp;
  178. if (server == NULL) {
  179. cERROR(1, "Null TCP session in smb2_mid_entry_alloc");
  180. return NULL;
  181. }
  182. temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
  183. if (temp == NULL)
  184. return temp;
  185. else {
  186. memset(temp, 0, sizeof(struct mid_q_entry));
  187. temp->mid = smb_buffer->MessageId; /* always LE */
  188. temp->pid = current->pid;
  189. temp->command = smb_buffer->Command; /* Always LE */
  190. temp->when_alloc = jiffies;
  191. temp->server = server;
  192. /*
  193. * The default is for the mid to be synchronous, so the
  194. * default callback just wakes up the current task.
  195. */
  196. temp->callback = cifs_wake_up_task;
  197. temp->callback_data = current;
  198. }
  199. atomic_inc(&midCount);
  200. temp->mid_state = MID_REQUEST_ALLOCATED;
  201. return temp;
  202. }
  203. static int
  204. smb2_get_mid_entry(struct cifs_ses *ses, struct smb2_hdr *buf,
  205. struct mid_q_entry **mid)
  206. {
  207. if (ses->server->tcpStatus == CifsExiting)
  208. return -ENOENT;
  209. if (ses->server->tcpStatus == CifsNeedReconnect) {
  210. cFYI(1, "tcp session dead - return to caller to retry");
  211. return -EAGAIN;
  212. }
  213. if (ses->status != CifsGood) {
  214. /* check if SMB2 session is bad because we are setting it up */
  215. if ((buf->Command != SMB2_SESSION_SETUP) &&
  216. (buf->Command != SMB2_NEGOTIATE))
  217. return -EAGAIN;
  218. /* else ok - we are setting up session */
  219. }
  220. *mid = smb2_mid_entry_alloc(buf, ses->server);
  221. if (*mid == NULL)
  222. return -ENOMEM;
  223. spin_lock(&GlobalMid_Lock);
  224. list_add_tail(&(*mid)->qhead, &ses->server->pending_mid_q);
  225. spin_unlock(&GlobalMid_Lock);
  226. return 0;
  227. }
  228. int
  229. smb2_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  230. bool log_error)
  231. {
  232. unsigned int len = get_rfc1002_length(mid->resp_buf);
  233. struct kvec iov;
  234. struct smb_rqst rqst = { .rq_iov = &iov,
  235. .rq_nvec = 1 };
  236. iov.iov_base = (char *)mid->resp_buf;
  237. iov.iov_len = get_rfc1002_length(mid->resp_buf) + 4;
  238. dump_smb(mid->resp_buf, min_t(u32, 80, len));
  239. /* convert the length into a more usable form */
  240. if ((len > 24) &&
  241. (server->sec_mode & (SECMODE_SIGN_REQUIRED|SECMODE_SIGN_ENABLED))) {
  242. int rc;
  243. rc = smb2_verify_signature(&rqst, server);
  244. if (rc)
  245. cERROR(1, "SMB signature verification returned error = "
  246. "%d", rc);
  247. }
  248. return map_smb2_to_linux_error(mid->resp_buf, log_error);
  249. }
  250. struct mid_q_entry *
  251. smb2_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
  252. {
  253. int rc;
  254. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  255. struct mid_q_entry *mid;
  256. smb2_seq_num_into_buf(ses->server, hdr);
  257. rc = smb2_get_mid_entry(ses, hdr, &mid);
  258. if (rc)
  259. return ERR_PTR(rc);
  260. rc = smb2_sign_rqst(rqst, ses->server);
  261. if (rc) {
  262. cifs_delete_mid(mid);
  263. return ERR_PTR(rc);
  264. }
  265. return mid;
  266. }
  267. struct mid_q_entry *
  268. smb2_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  269. {
  270. int rc;
  271. struct smb2_hdr *hdr = (struct smb2_hdr *)rqst->rq_iov[0].iov_base;
  272. struct mid_q_entry *mid;
  273. smb2_seq_num_into_buf(server, hdr);
  274. mid = smb2_mid_entry_alloc(hdr, server);
  275. if (mid == NULL)
  276. return ERR_PTR(-ENOMEM);
  277. rc = smb2_sign_rqst(rqst, server);
  278. if (rc) {
  279. DeleteMidQEntry(mid);
  280. return ERR_PTR(rc);
  281. }
  282. return mid;
  283. }