smb2misc.c 26 KB

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