smb2misc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * fs/cifs/smb2misc.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2011
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/ctype.h>
  24. #include "smb2pdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsproto.h"
  27. #include "smb2proto.h"
  28. #include "cifs_debug.h"
  29. #include "cifs_unicode.h"
  30. #include "smb2status.h"
  31. #include "smb2glob.h"
  32. static int
  33. check_smb2_hdr(struct smb2_sync_hdr *shdr, __u64 mid)
  34. {
  35. __u64 wire_mid = le64_to_cpu(shdr->MessageId);
  36. /*
  37. * Make sure that this really is an SMB, that it is a response,
  38. * and that the message ids match.
  39. */
  40. if ((shdr->ProtocolId == SMB2_PROTO_NUMBER) &&
  41. (mid == wire_mid)) {
  42. if (shdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
  43. return 0;
  44. else {
  45. /* only one valid case where server sends us request */
  46. if (shdr->Command == SMB2_OPLOCK_BREAK)
  47. return 0;
  48. else
  49. cifs_dbg(VFS, "Received Request not response\n");
  50. }
  51. } else { /* bad signature or mid */
  52. if (shdr->ProtocolId != SMB2_PROTO_NUMBER)
  53. cifs_dbg(VFS, "Bad protocol string signature header %x\n",
  54. le32_to_cpu(shdr->ProtocolId));
  55. if (mid != wire_mid)
  56. cifs_dbg(VFS, "Mids do not match: %llu and %llu\n",
  57. mid, wire_mid);
  58. }
  59. cifs_dbg(VFS, "Bad SMB detected. The Mid=%llu\n", wire_mid);
  60. return 1;
  61. }
  62. /*
  63. * The following table defines the expected "StructureSize" of SMB2 responses
  64. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS responses.
  65. *
  66. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  67. * indexed by command in host byte order
  68. */
  69. static const __le16 smb2_rsp_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  70. /* SMB2_NEGOTIATE */ cpu_to_le16(65),
  71. /* SMB2_SESSION_SETUP */ cpu_to_le16(9),
  72. /* SMB2_LOGOFF */ cpu_to_le16(4),
  73. /* SMB2_TREE_CONNECT */ cpu_to_le16(16),
  74. /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4),
  75. /* SMB2_CREATE */ cpu_to_le16(89),
  76. /* SMB2_CLOSE */ cpu_to_le16(60),
  77. /* SMB2_FLUSH */ cpu_to_le16(4),
  78. /* SMB2_READ */ cpu_to_le16(17),
  79. /* SMB2_WRITE */ cpu_to_le16(17),
  80. /* SMB2_LOCK */ cpu_to_le16(4),
  81. /* SMB2_IOCTL */ cpu_to_le16(49),
  82. /* BB CHECK this ... not listed in documentation */
  83. /* SMB2_CANCEL */ cpu_to_le16(0),
  84. /* SMB2_ECHO */ cpu_to_le16(4),
  85. /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(9),
  86. /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(9),
  87. /* SMB2_QUERY_INFO */ cpu_to_le16(9),
  88. /* SMB2_SET_INFO */ cpu_to_le16(2),
  89. /* BB FIXME can also be 44 for lease break */
  90. /* SMB2_OPLOCK_BREAK */ cpu_to_le16(24)
  91. };
  92. static __u32 get_neg_ctxt_len(struct smb2_sync_hdr *hdr, __u32 len,
  93. __u32 non_ctxlen)
  94. {
  95. __u16 neg_count;
  96. __u32 nc_offset, size_of_pad_before_neg_ctxts;
  97. struct smb2_negotiate_rsp *pneg_rsp = (struct smb2_negotiate_rsp *)hdr;
  98. /* Negotiate contexts are only valid for latest dialect SMB3.11 */
  99. neg_count = le16_to_cpu(pneg_rsp->NegotiateContextCount);
  100. if ((neg_count == 0) ||
  101. (pneg_rsp->DialectRevision != cpu_to_le16(SMB311_PROT_ID)))
  102. return 0;
  103. /* Make sure that negotiate contexts start after gss security blob */
  104. nc_offset = le32_to_cpu(pneg_rsp->NegotiateContextOffset);
  105. if (nc_offset < non_ctxlen) {
  106. printk_once(KERN_WARNING "invalid negotiate context offset\n");
  107. return 0;
  108. }
  109. size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen;
  110. /* Verify that at least minimal negotiate contexts fit within frame */
  111. if (len < nc_offset + (neg_count * sizeof(struct smb2_neg_context))) {
  112. printk_once(KERN_WARNING "negotiate context goes beyond end\n");
  113. return 0;
  114. }
  115. cifs_dbg(FYI, "length of negcontexts %d pad %d\n",
  116. len - nc_offset, size_of_pad_before_neg_ctxts);
  117. /* length of negcontexts including pad from end of sec blob to them */
  118. return (len - nc_offset) + size_of_pad_before_neg_ctxts;
  119. }
  120. int
  121. smb2_check_message(char *buf, unsigned int len, struct TCP_Server_Info *srvr)
  122. {
  123. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
  124. struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)shdr;
  125. __u64 mid;
  126. __u32 clc_len; /* calculated length */
  127. int command;
  128. int pdu_size = sizeof(struct smb2_sync_pdu);
  129. int hdr_size = sizeof(struct smb2_sync_hdr);
  130. /*
  131. * Add function to do table lookup of StructureSize by command
  132. * ie Validate the wct via smb2_struct_sizes table above
  133. */
  134. if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
  135. struct smb2_transform_hdr *thdr =
  136. (struct smb2_transform_hdr *)buf;
  137. struct cifs_ses *ses = NULL;
  138. struct list_head *tmp;
  139. /* decrypt frame now that it is completely read in */
  140. spin_lock(&cifs_tcp_ses_lock);
  141. list_for_each(tmp, &srvr->smb_ses_list) {
  142. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  143. if (ses->Suid == thdr->SessionId)
  144. break;
  145. ses = NULL;
  146. }
  147. spin_unlock(&cifs_tcp_ses_lock);
  148. if (ses == NULL) {
  149. cifs_dbg(VFS, "no decryption - session id not found\n");
  150. return 1;
  151. }
  152. }
  153. mid = le64_to_cpu(shdr->MessageId);
  154. if (len < pdu_size) {
  155. if ((len >= hdr_size)
  156. && (shdr->Status != 0)) {
  157. pdu->StructureSize2 = 0;
  158. /*
  159. * As with SMB/CIFS, on some error cases servers may
  160. * not return wct properly
  161. */
  162. return 0;
  163. } else {
  164. cifs_dbg(VFS, "Length less than SMB header size\n");
  165. }
  166. return 1;
  167. }
  168. if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE) {
  169. cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n",
  170. mid);
  171. return 1;
  172. }
  173. if (check_smb2_hdr(shdr, mid))
  174. return 1;
  175. if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
  176. cifs_dbg(VFS, "Illegal structure size %u\n",
  177. le16_to_cpu(shdr->StructureSize));
  178. return 1;
  179. }
  180. command = le16_to_cpu(shdr->Command);
  181. if (command >= NUMBER_OF_SMB2_COMMANDS) {
  182. cifs_dbg(VFS, "Illegal SMB2 command %d\n", command);
  183. return 1;
  184. }
  185. if (smb2_rsp_struct_sizes[command] != pdu->StructureSize2) {
  186. if (command != SMB2_OPLOCK_BREAK_HE && (shdr->Status == 0 ||
  187. pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2)) {
  188. /* error packets have 9 byte structure size */
  189. cifs_dbg(VFS, "Illegal response size %u for command %d\n",
  190. le16_to_cpu(pdu->StructureSize2), command);
  191. return 1;
  192. } else if (command == SMB2_OPLOCK_BREAK_HE
  193. && (shdr->Status == 0)
  194. && (le16_to_cpu(pdu->StructureSize2) != 44)
  195. && (le16_to_cpu(pdu->StructureSize2) != 36)) {
  196. /* special case for SMB2.1 lease break message */
  197. cifs_dbg(VFS, "Illegal response size %d for oplock break\n",
  198. le16_to_cpu(pdu->StructureSize2));
  199. return 1;
  200. }
  201. }
  202. clc_len = smb2_calc_size(buf, srvr);
  203. if (shdr->Command == SMB2_NEGOTIATE)
  204. clc_len += get_neg_ctxt_len(shdr, len, clc_len);
  205. if (len != clc_len) {
  206. cifs_dbg(FYI, "Calculated size %u length %u mismatch mid %llu\n",
  207. clc_len, len, mid);
  208. /* create failed on symlink */
  209. if (command == SMB2_CREATE_HE &&
  210. shdr->Status == STATUS_STOPPED_ON_SYMLINK)
  211. return 0;
  212. /* Windows 7 server returns 24 bytes more */
  213. if (clc_len + 24 == len && command == SMB2_OPLOCK_BREAK_HE)
  214. return 0;
  215. /* server can return one byte more due to implied bcc[0] */
  216. if (clc_len == len + 1)
  217. return 0;
  218. /*
  219. * Some windows servers (win2016) will pad also the final
  220. * PDU in a compound to 8 bytes.
  221. */
  222. if (((clc_len + 7) & ~7) == len)
  223. return 0;
  224. /*
  225. * MacOS server pads after SMB2.1 write response with 3 bytes
  226. * of junk. Other servers match RFC1001 len to actual
  227. * SMB2/SMB3 frame length (header + smb2 response specific data)
  228. * Some windows servers also pad up to 8 bytes when compounding.
  229. * If pad is longer than eight bytes, log the server behavior
  230. * (once), since may indicate a problem but allow it and continue
  231. * since the frame is parseable.
  232. */
  233. if (clc_len < len) {
  234. pr_warn_once(
  235. "srv rsp padded more than expected. Length %d not %d for cmd:%d mid:%llu\n",
  236. len, clc_len, command, mid);
  237. return 0;
  238. }
  239. pr_warn_once(
  240. "srv rsp too short, len %d not %d. cmd:%d mid:%llu\n",
  241. len, clc_len, command, mid);
  242. return 1;
  243. }
  244. return 0;
  245. }
  246. /*
  247. * The size of the variable area depends on the offset and length fields
  248. * located in different fields for various SMB2 responses. SMB2 responses
  249. * with no variable length info, show an offset of zero for the offset field.
  250. */
  251. static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
  252. /* SMB2_NEGOTIATE */ true,
  253. /* SMB2_SESSION_SETUP */ true,
  254. /* SMB2_LOGOFF */ false,
  255. /* SMB2_TREE_CONNECT */ false,
  256. /* SMB2_TREE_DISCONNECT */ false,
  257. /* SMB2_CREATE */ true,
  258. /* SMB2_CLOSE */ false,
  259. /* SMB2_FLUSH */ false,
  260. /* SMB2_READ */ true,
  261. /* SMB2_WRITE */ false,
  262. /* SMB2_LOCK */ false,
  263. /* SMB2_IOCTL */ true,
  264. /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */
  265. /* SMB2_ECHO */ false,
  266. /* SMB2_QUERY_DIRECTORY */ true,
  267. /* SMB2_CHANGE_NOTIFY */ true,
  268. /* SMB2_QUERY_INFO */ true,
  269. /* SMB2_SET_INFO */ false,
  270. /* SMB2_OPLOCK_BREAK */ false
  271. };
  272. /*
  273. * Returns the pointer to the beginning of the data area. Length of the data
  274. * area and the offset to it (from the beginning of the smb are also returned.
  275. */
  276. char *
  277. smb2_get_data_area_len(int *off, int *len, struct smb2_sync_hdr *shdr)
  278. {
  279. *off = 0;
  280. *len = 0;
  281. /* error responses do not have data area */
  282. if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED &&
  283. (((struct smb2_err_rsp *)shdr)->StructureSize) ==
  284. SMB2_ERROR_STRUCTURE_SIZE2)
  285. return NULL;
  286. /*
  287. * Following commands have data areas so we have to get the location
  288. * of the data buffer offset and data buffer length for the particular
  289. * command.
  290. */
  291. switch (shdr->Command) {
  292. case SMB2_NEGOTIATE:
  293. *off = le16_to_cpu(
  294. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferOffset);
  295. *len = le16_to_cpu(
  296. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferLength);
  297. break;
  298. case SMB2_SESSION_SETUP:
  299. *off = le16_to_cpu(
  300. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferOffset);
  301. *len = le16_to_cpu(
  302. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferLength);
  303. break;
  304. case SMB2_CREATE:
  305. *off = le32_to_cpu(
  306. ((struct smb2_create_rsp *)shdr)->CreateContextsOffset);
  307. *len = le32_to_cpu(
  308. ((struct smb2_create_rsp *)shdr)->CreateContextsLength);
  309. break;
  310. case SMB2_QUERY_INFO:
  311. *off = le16_to_cpu(
  312. ((struct smb2_query_info_rsp *)shdr)->OutputBufferOffset);
  313. *len = le32_to_cpu(
  314. ((struct smb2_query_info_rsp *)shdr)->OutputBufferLength);
  315. break;
  316. case SMB2_READ:
  317. /* TODO: is this a bug ? */
  318. *off = ((struct smb2_read_rsp *)shdr)->DataOffset;
  319. *len = le32_to_cpu(((struct smb2_read_rsp *)shdr)->DataLength);
  320. break;
  321. case SMB2_QUERY_DIRECTORY:
  322. *off = le16_to_cpu(
  323. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferOffset);
  324. *len = le32_to_cpu(
  325. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferLength);
  326. break;
  327. case SMB2_IOCTL:
  328. *off = le32_to_cpu(
  329. ((struct smb2_ioctl_rsp *)shdr)->OutputOffset);
  330. *len = le32_to_cpu(
  331. ((struct smb2_ioctl_rsp *)shdr)->OutputCount);
  332. break;
  333. case SMB2_CHANGE_NOTIFY:
  334. default:
  335. /* BB FIXME for unimplemented cases above */
  336. cifs_dbg(VFS, "no length check for command\n");
  337. break;
  338. }
  339. /*
  340. * Invalid length or offset probably means data area is invalid, but
  341. * we have little choice but to ignore the data area in this case.
  342. */
  343. if (*off > 4096) {
  344. cifs_dbg(VFS, "offset %d too large, data area ignored\n", *off);
  345. *len = 0;
  346. *off = 0;
  347. } else if (*off < 0) {
  348. cifs_dbg(VFS, "negative offset %d to data invalid ignore data area\n",
  349. *off);
  350. *off = 0;
  351. *len = 0;
  352. } else if (*len < 0) {
  353. cifs_dbg(VFS, "negative data length %d invalid, data area ignored\n",
  354. *len);
  355. *len = 0;
  356. } else if (*len > 128 * 1024) {
  357. cifs_dbg(VFS, "data area larger than 128K: %d\n", *len);
  358. *len = 0;
  359. }
  360. /* return pointer to beginning of data area, ie offset from SMB start */
  361. if ((*off != 0) && (*len != 0))
  362. return (char *)shdr + *off;
  363. else
  364. return NULL;
  365. }
  366. /*
  367. * Calculate the size of the SMB message based on the fixed header
  368. * portion, the number of word parameters and the data portion of the message.
  369. */
  370. unsigned int
  371. smb2_calc_size(void *buf, struct TCP_Server_Info *srvr)
  372. {
  373. struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)buf;
  374. struct smb2_sync_hdr *shdr = &pdu->sync_hdr;
  375. int offset; /* the offset from the beginning of SMB to data area */
  376. int data_length; /* the length of the variable length data area */
  377. /* Structure Size has already been checked to make sure it is 64 */
  378. int len = le16_to_cpu(shdr->StructureSize);
  379. /*
  380. * StructureSize2, ie length of fixed parameter area has already
  381. * been checked to make sure it is the correct length.
  382. */
  383. len += le16_to_cpu(pdu->StructureSize2);
  384. if (has_smb2_data_area[le16_to_cpu(shdr->Command)] == false)
  385. goto calc_size_exit;
  386. smb2_get_data_area_len(&offset, &data_length, shdr);
  387. cifs_dbg(FYI, "SMB2 data length %d offset %d\n", data_length, offset);
  388. if (data_length > 0) {
  389. /*
  390. * Check to make sure that data area begins after fixed area,
  391. * Note that last byte of the fixed area is part of data area
  392. * for some commands, typically those with odd StructureSize,
  393. * so we must add one to the calculation.
  394. */
  395. if (offset + 1 < len) {
  396. cifs_dbg(VFS, "data area offset %d overlaps SMB2 header %d\n",
  397. offset + 1, len);
  398. data_length = 0;
  399. } else {
  400. len = offset + data_length;
  401. }
  402. }
  403. calc_size_exit:
  404. cifs_dbg(FYI, "SMB2 len %d\n", len);
  405. return len;
  406. }
  407. /* Note: caller must free return buffer */
  408. __le16 *
  409. cifs_convert_path_to_utf16(const char *from, struct cifs_sb_info *cifs_sb)
  410. {
  411. int len;
  412. const char *start_of_path;
  413. __le16 *to;
  414. int map_type;
  415. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
  416. map_type = SFM_MAP_UNI_RSVD;
  417. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  418. map_type = SFU_MAP_UNI_RSVD;
  419. else
  420. map_type = NO_MAP_UNI_RSVD;
  421. /* Windows doesn't allow paths beginning with \ */
  422. if (from[0] == '\\')
  423. start_of_path = from + 1;
  424. /* SMB311 POSIX extensions paths do not include leading slash */
  425. else if (cifs_sb_master_tlink(cifs_sb) &&
  426. cifs_sb_master_tcon(cifs_sb)->posix_extensions &&
  427. (from[0] == '/')) {
  428. start_of_path = from + 1;
  429. } else
  430. start_of_path = from;
  431. to = cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len,
  432. cifs_sb->local_nls, map_type);
  433. return to;
  434. }
  435. __le32
  436. smb2_get_lease_state(struct cifsInodeInfo *cinode)
  437. {
  438. __le32 lease = 0;
  439. if (CIFS_CACHE_WRITE(cinode))
  440. lease |= SMB2_LEASE_WRITE_CACHING;
  441. if (CIFS_CACHE_HANDLE(cinode))
  442. lease |= SMB2_LEASE_HANDLE_CACHING;
  443. if (CIFS_CACHE_READ(cinode))
  444. lease |= SMB2_LEASE_READ_CACHING;
  445. return lease;
  446. }
  447. struct smb2_lease_break_work {
  448. struct work_struct lease_break;
  449. struct tcon_link *tlink;
  450. __u8 lease_key[16];
  451. __le32 lease_state;
  452. };
  453. static void
  454. cifs_ses_oplock_break(struct work_struct *work)
  455. {
  456. struct smb2_lease_break_work *lw = container_of(work,
  457. struct smb2_lease_break_work, lease_break);
  458. int rc = 0;
  459. rc = SMB2_lease_break(0, tlink_tcon(lw->tlink), lw->lease_key,
  460. lw->lease_state);
  461. cifs_dbg(FYI, "Lease release rc %d\n", rc);
  462. cifs_put_tlink(lw->tlink);
  463. kfree(lw);
  464. }
  465. static void
  466. smb2_queue_pending_open_break(struct tcon_link *tlink, __u8 *lease_key,
  467. __le32 new_lease_state)
  468. {
  469. struct smb2_lease_break_work *lw;
  470. lw = kmalloc(sizeof(struct smb2_lease_break_work), GFP_KERNEL);
  471. if (!lw) {
  472. cifs_put_tlink(tlink);
  473. return;
  474. }
  475. INIT_WORK(&lw->lease_break, cifs_ses_oplock_break);
  476. lw->tlink = tlink;
  477. lw->lease_state = new_lease_state;
  478. memcpy(lw->lease_key, lease_key, SMB2_LEASE_KEY_SIZE);
  479. queue_work(cifsiod_wq, &lw->lease_break);
  480. }
  481. static bool
  482. smb2_tcon_has_lease(struct cifs_tcon *tcon, struct smb2_lease_break *rsp)
  483. {
  484. __u8 lease_state;
  485. struct list_head *tmp;
  486. struct cifsFileInfo *cfile;
  487. struct cifsInodeInfo *cinode;
  488. int ack_req = le32_to_cpu(rsp->Flags &
  489. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  490. lease_state = le32_to_cpu(rsp->NewLeaseState);
  491. list_for_each(tmp, &tcon->openFileList) {
  492. cfile = list_entry(tmp, struct cifsFileInfo, tlist);
  493. cinode = CIFS_I(d_inode(cfile->dentry));
  494. if (memcmp(cinode->lease_key, rsp->LeaseKey,
  495. SMB2_LEASE_KEY_SIZE))
  496. continue;
  497. cifs_dbg(FYI, "found in the open list\n");
  498. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  499. lease_state);
  500. if (ack_req)
  501. cfile->oplock_break_cancelled = false;
  502. else
  503. cfile->oplock_break_cancelled = true;
  504. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  505. cfile->oplock_epoch = le16_to_cpu(rsp->Epoch);
  506. cfile->oplock_level = lease_state;
  507. cifs_queue_oplock_break(cfile);
  508. return true;
  509. }
  510. return false;
  511. }
  512. static struct cifs_pending_open *
  513. smb2_tcon_find_pending_open_lease(struct cifs_tcon *tcon,
  514. struct smb2_lease_break *rsp)
  515. {
  516. __u8 lease_state = le32_to_cpu(rsp->NewLeaseState);
  517. int ack_req = le32_to_cpu(rsp->Flags &
  518. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  519. struct cifs_pending_open *open;
  520. struct cifs_pending_open *found = NULL;
  521. list_for_each_entry(open, &tcon->pending_opens, olist) {
  522. if (memcmp(open->lease_key, rsp->LeaseKey,
  523. SMB2_LEASE_KEY_SIZE))
  524. continue;
  525. if (!found && ack_req) {
  526. found = open;
  527. }
  528. cifs_dbg(FYI, "found in the pending open list\n");
  529. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  530. lease_state);
  531. open->oplock = lease_state;
  532. }
  533. return found;
  534. }
  535. static bool
  536. smb2_is_valid_lease_break(char *buffer)
  537. {
  538. struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer;
  539. struct list_head *tmp, *tmp1, *tmp2;
  540. struct TCP_Server_Info *server;
  541. struct cifs_ses *ses;
  542. struct cifs_tcon *tcon;
  543. struct cifs_pending_open *open;
  544. cifs_dbg(FYI, "Checking for lease break\n");
  545. /* look up tcon based on tid & uid */
  546. spin_lock(&cifs_tcp_ses_lock);
  547. list_for_each(tmp, &cifs_tcp_ses_list) {
  548. server = list_entry(tmp, struct TCP_Server_Info, tcp_ses_list);
  549. list_for_each(tmp1, &server->smb_ses_list) {
  550. ses = list_entry(tmp1, struct cifs_ses, smb_ses_list);
  551. list_for_each(tmp2, &ses->tcon_list) {
  552. tcon = list_entry(tmp2, struct cifs_tcon,
  553. tcon_list);
  554. spin_lock(&tcon->open_file_lock);
  555. cifs_stats_inc(
  556. &tcon->stats.cifs_stats.num_oplock_brks);
  557. if (smb2_tcon_has_lease(tcon, rsp)) {
  558. spin_unlock(&tcon->open_file_lock);
  559. spin_unlock(&cifs_tcp_ses_lock);
  560. return true;
  561. }
  562. open = smb2_tcon_find_pending_open_lease(tcon,
  563. rsp);
  564. if (open) {
  565. __u8 lease_key[SMB2_LEASE_KEY_SIZE];
  566. struct tcon_link *tlink;
  567. tlink = cifs_get_tlink(open->tlink);
  568. memcpy(lease_key, open->lease_key,
  569. SMB2_LEASE_KEY_SIZE);
  570. spin_unlock(&tcon->open_file_lock);
  571. spin_unlock(&cifs_tcp_ses_lock);
  572. smb2_queue_pending_open_break(tlink,
  573. lease_key,
  574. rsp->NewLeaseState);
  575. return true;
  576. }
  577. spin_unlock(&tcon->open_file_lock);
  578. if (tcon->crfid.is_valid &&
  579. !memcmp(rsp->LeaseKey,
  580. tcon->crfid.fid->lease_key,
  581. SMB2_LEASE_KEY_SIZE)) {
  582. INIT_WORK(&tcon->crfid.lease_break,
  583. smb2_cached_lease_break);
  584. queue_work(cifsiod_wq,
  585. &tcon->crfid.lease_break);
  586. spin_unlock(&cifs_tcp_ses_lock);
  587. return true;
  588. }
  589. }
  590. }
  591. }
  592. spin_unlock(&cifs_tcp_ses_lock);
  593. cifs_dbg(FYI, "Can not process lease break - no lease matched\n");
  594. return false;
  595. }
  596. bool
  597. smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server)
  598. {
  599. struct smb2_oplock_break *rsp = (struct smb2_oplock_break *)buffer;
  600. struct list_head *tmp, *tmp1, *tmp2;
  601. struct cifs_ses *ses;
  602. struct cifs_tcon *tcon;
  603. struct cifsInodeInfo *cinode;
  604. struct cifsFileInfo *cfile;
  605. cifs_dbg(FYI, "Checking for oplock break\n");
  606. if (rsp->sync_hdr.Command != SMB2_OPLOCK_BREAK)
  607. return false;
  608. if (rsp->StructureSize !=
  609. smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) {
  610. if (le16_to_cpu(rsp->StructureSize) == 44)
  611. return smb2_is_valid_lease_break(buffer);
  612. else
  613. return false;
  614. }
  615. cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel);
  616. /* look up tcon based on tid & uid */
  617. spin_lock(&cifs_tcp_ses_lock);
  618. list_for_each(tmp, &server->smb_ses_list) {
  619. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  620. list_for_each(tmp1, &ses->tcon_list) {
  621. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  622. spin_lock(&tcon->open_file_lock);
  623. list_for_each(tmp2, &tcon->openFileList) {
  624. cfile = list_entry(tmp2, struct cifsFileInfo,
  625. tlist);
  626. if (rsp->PersistentFid !=
  627. cfile->fid.persistent_fid ||
  628. rsp->VolatileFid !=
  629. cfile->fid.volatile_fid)
  630. continue;
  631. cifs_dbg(FYI, "file id match, oplock break\n");
  632. cifs_stats_inc(
  633. &tcon->stats.cifs_stats.num_oplock_brks);
  634. cinode = CIFS_I(d_inode(cfile->dentry));
  635. spin_lock(&cfile->file_info_lock);
  636. if (!CIFS_CACHE_WRITE(cinode) &&
  637. rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE)
  638. cfile->oplock_break_cancelled = true;
  639. else
  640. cfile->oplock_break_cancelled = false;
  641. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  642. &cinode->flags);
  643. cfile->oplock_epoch = 0;
  644. cfile->oplock_level = rsp->OplockLevel;
  645. spin_unlock(&cfile->file_info_lock);
  646. cifs_queue_oplock_break(cfile);
  647. spin_unlock(&tcon->open_file_lock);
  648. spin_unlock(&cifs_tcp_ses_lock);
  649. return true;
  650. }
  651. spin_unlock(&tcon->open_file_lock);
  652. }
  653. }
  654. spin_unlock(&cifs_tcp_ses_lock);
  655. cifs_dbg(FYI, "No file id matched, oplock break ignored\n");
  656. return true;
  657. }
  658. void
  659. smb2_cancelled_close_fid(struct work_struct *work)
  660. {
  661. struct close_cancelled_open *cancelled = container_of(work,
  662. struct close_cancelled_open, work);
  663. cifs_dbg(VFS, "Close unmatched open\n");
  664. SMB2_close(0, cancelled->tcon, cancelled->fid.persistent_fid,
  665. cancelled->fid.volatile_fid);
  666. cifs_put_tcon(cancelled->tcon);
  667. kfree(cancelled);
  668. }
  669. /* Caller should already has an extra reference to @tcon */
  670. static int
  671. __smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
  672. __u64 volatile_fid)
  673. {
  674. struct close_cancelled_open *cancelled;
  675. cancelled = kzalloc(sizeof(*cancelled), GFP_ATOMIC);
  676. if (!cancelled)
  677. return -ENOMEM;
  678. cancelled->fid.persistent_fid = persistent_fid;
  679. cancelled->fid.volatile_fid = volatile_fid;
  680. cancelled->tcon = tcon;
  681. INIT_WORK(&cancelled->work, smb2_cancelled_close_fid);
  682. WARN_ON(queue_work(cifsiod_wq, &cancelled->work) == false);
  683. return 0;
  684. }
  685. int
  686. smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
  687. __u64 volatile_fid)
  688. {
  689. int rc;
  690. cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  691. spin_lock(&cifs_tcp_ses_lock);
  692. tcon->tc_count++;
  693. spin_unlock(&cifs_tcp_ses_lock);
  694. rc = __smb2_handle_cancelled_close(tcon, persistent_fid, volatile_fid);
  695. if (rc)
  696. cifs_put_tcon(tcon);
  697. return rc;
  698. }
  699. int
  700. smb2_handle_cancelled_mid(char *buffer, struct TCP_Server_Info *server)
  701. {
  702. struct smb2_sync_hdr *sync_hdr = (struct smb2_sync_hdr *)buffer;
  703. struct smb2_create_rsp *rsp = (struct smb2_create_rsp *)buffer;
  704. struct cifs_tcon *tcon;
  705. int rc;
  706. if (sync_hdr->Command != SMB2_CREATE ||
  707. sync_hdr->Status != STATUS_SUCCESS)
  708. return 0;
  709. tcon = smb2_find_smb_tcon(server, sync_hdr->SessionId,
  710. sync_hdr->TreeId);
  711. if (!tcon)
  712. return -ENOENT;
  713. rc = __smb2_handle_cancelled_close(tcon, rsp->PersistentFileId,
  714. rsp->VolatileFileId);
  715. if (rc)
  716. cifs_put_tcon(tcon);
  717. return rc;
  718. }
  719. /**
  720. * smb311_update_preauth_hash - update @ses hash with the packet data in @iov
  721. *
  722. * Assumes @iov does not contain the rfc1002 length and iov[0] has the
  723. * SMB2 header.
  724. */
  725. int
  726. smb311_update_preauth_hash(struct cifs_ses *ses, struct kvec *iov, int nvec)
  727. {
  728. int i, rc;
  729. struct sdesc *d;
  730. struct smb2_sync_hdr *hdr;
  731. if (ses->server->tcpStatus == CifsGood) {
  732. /* skip non smb311 connections */
  733. if (ses->server->dialect != SMB311_PROT_ID)
  734. return 0;
  735. /* skip last sess setup response */
  736. hdr = (struct smb2_sync_hdr *)iov[0].iov_base;
  737. if (hdr->Flags & SMB2_FLAGS_SIGNED)
  738. return 0;
  739. }
  740. rc = smb311_crypto_shash_allocate(ses->server);
  741. if (rc)
  742. return rc;
  743. d = ses->server->secmech.sdescsha512;
  744. rc = crypto_shash_init(&d->shash);
  745. if (rc) {
  746. cifs_dbg(VFS, "%s: could not init sha512 shash\n", __func__);
  747. return rc;
  748. }
  749. rc = crypto_shash_update(&d->shash, ses->preauth_sha_hash,
  750. SMB2_PREAUTH_HASH_SIZE);
  751. if (rc) {
  752. cifs_dbg(VFS, "%s: could not update sha512 shash\n", __func__);
  753. return rc;
  754. }
  755. for (i = 0; i < nvec; i++) {
  756. rc = crypto_shash_update(&d->shash,
  757. iov[i].iov_base, iov[i].iov_len);
  758. if (rc) {
  759. cifs_dbg(VFS, "%s: could not update sha512 shash\n",
  760. __func__);
  761. return rc;
  762. }
  763. }
  764. rc = crypto_shash_final(&d->shash, ses->preauth_sha_hash);
  765. if (rc) {
  766. cifs_dbg(VFS, "%s: could not finalize sha512 shash\n",
  767. __func__);
  768. return rc;
  769. }
  770. return 0;
  771. }