smb2misc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  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. */
  10. #include <linux/ctype.h>
  11. #include "cifsglob.h"
  12. #include "cifsproto.h"
  13. #include "smb2proto.h"
  14. #include "cifs_debug.h"
  15. #include "cifs_unicode.h"
  16. #include "../common/smb2status.h"
  17. #include "smb2glob.h"
  18. #include "nterr.h"
  19. #include "cached_dir.h"
  20. static int
  21. check_smb2_hdr(struct smb2_hdr *shdr, __u64 mid)
  22. {
  23. __u64 wire_mid = le64_to_cpu(shdr->MessageId);
  24. /*
  25. * Make sure that this really is an SMB, that it is a response,
  26. * and that the message ids match.
  27. */
  28. if ((shdr->ProtocolId == SMB2_PROTO_NUMBER) &&
  29. (mid == wire_mid)) {
  30. if (shdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
  31. return 0;
  32. else {
  33. /* only one valid case where server sends us request */
  34. if (shdr->Command == SMB2_OPLOCK_BREAK)
  35. return 0;
  36. else
  37. cifs_dbg(VFS, "Received Request not response\n");
  38. }
  39. } else { /* bad signature or mid */
  40. if (shdr->ProtocolId != SMB2_PROTO_NUMBER)
  41. cifs_dbg(VFS, "Bad protocol string signature header %x\n",
  42. le32_to_cpu(shdr->ProtocolId));
  43. if (mid != wire_mid)
  44. cifs_dbg(VFS, "Mids do not match: %llu and %llu\n",
  45. mid, wire_mid);
  46. }
  47. cifs_dbg(VFS, "Bad SMB detected. The Mid=%llu\n", wire_mid);
  48. return 1;
  49. }
  50. /*
  51. * The following table defines the expected "StructureSize" of SMB2 responses
  52. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS responses.
  53. *
  54. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  55. * indexed by command in host byte order
  56. */
  57. static const __le16 smb2_rsp_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  58. /* SMB2_NEGOTIATE */ cpu_to_le16(65),
  59. /* SMB2_SESSION_SETUP */ cpu_to_le16(9),
  60. /* SMB2_LOGOFF */ cpu_to_le16(4),
  61. /* SMB2_TREE_CONNECT */ cpu_to_le16(16),
  62. /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4),
  63. /* SMB2_CREATE */ cpu_to_le16(89),
  64. /* SMB2_CLOSE */ cpu_to_le16(60),
  65. /* SMB2_FLUSH */ cpu_to_le16(4),
  66. /* SMB2_READ */ cpu_to_le16(17),
  67. /* SMB2_WRITE */ cpu_to_le16(17),
  68. /* SMB2_LOCK */ cpu_to_le16(4),
  69. /* SMB2_IOCTL */ cpu_to_le16(49),
  70. /* BB CHECK this ... not listed in documentation */
  71. /* SMB2_CANCEL */ cpu_to_le16(0),
  72. /* SMB2_ECHO */ cpu_to_le16(4),
  73. /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(9),
  74. /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(9),
  75. /* SMB2_QUERY_INFO */ cpu_to_le16(9),
  76. /* SMB2_SET_INFO */ cpu_to_le16(2),
  77. /* BB FIXME can also be 44 for lease break */
  78. /* SMB2_OPLOCK_BREAK */ cpu_to_le16(24)
  79. };
  80. #define SMB311_NEGPROT_BASE_SIZE (sizeof(struct smb2_hdr) + sizeof(struct smb2_negotiate_rsp))
  81. static __u32 get_neg_ctxt_len(struct smb2_hdr *hdr, __u32 len,
  82. __u32 non_ctxlen)
  83. {
  84. __u16 neg_count;
  85. __u32 nc_offset, size_of_pad_before_neg_ctxts;
  86. struct smb2_negotiate_rsp *pneg_rsp = (struct smb2_negotiate_rsp *)hdr;
  87. /* Negotiate contexts are only valid for latest dialect SMB3.11 */
  88. neg_count = le16_to_cpu(pneg_rsp->NegotiateContextCount);
  89. if ((neg_count == 0) ||
  90. (pneg_rsp->DialectRevision != cpu_to_le16(SMB311_PROT_ID)))
  91. return 0;
  92. /*
  93. * if SPNEGO blob present (ie the RFC2478 GSS info which indicates
  94. * which security mechanisms the server supports) make sure that
  95. * the negotiate contexts start after it
  96. */
  97. nc_offset = le32_to_cpu(pneg_rsp->NegotiateContextOffset);
  98. /*
  99. * non_ctxlen is at least shdr->StructureSize + pdu->StructureSize2
  100. * and the latter is 1 byte bigger than the fix-sized area of the
  101. * NEGOTIATE response
  102. */
  103. if (nc_offset + 1 < non_ctxlen) {
  104. pr_warn_once("Invalid negotiate context offset %d\n", nc_offset);
  105. return 0;
  106. } else if (nc_offset + 1 == non_ctxlen) {
  107. cifs_dbg(FYI, "no SPNEGO security blob in negprot rsp\n");
  108. size_of_pad_before_neg_ctxts = 0;
  109. } else if (non_ctxlen == SMB311_NEGPROT_BASE_SIZE + 1)
  110. /* has padding, but no SPNEGO blob */
  111. size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen + 1;
  112. else
  113. size_of_pad_before_neg_ctxts = nc_offset - non_ctxlen;
  114. /* Verify that at least minimal negotiate contexts fit within frame */
  115. if (len < nc_offset + (neg_count * sizeof(struct smb2_neg_context))) {
  116. pr_warn_once("negotiate context goes beyond end\n");
  117. return 0;
  118. }
  119. cifs_dbg(FYI, "length of negcontexts %d pad %d\n",
  120. len - nc_offset, size_of_pad_before_neg_ctxts);
  121. /* length of negcontexts including pad from end of sec blob to them */
  122. return (len - nc_offset) + size_of_pad_before_neg_ctxts;
  123. }
  124. int
  125. smb2_check_message(char *buf, unsigned int len, struct TCP_Server_Info *server)
  126. {
  127. struct TCP_Server_Info *pserver;
  128. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  129. struct smb2_pdu *pdu = (struct smb2_pdu *)shdr;
  130. int hdr_size = sizeof(struct smb2_hdr);
  131. int pdu_size = sizeof(struct smb2_pdu);
  132. int command;
  133. __u32 calc_len; /* calculated length */
  134. __u64 mid;
  135. /* If server is a channel, select the primary channel */
  136. pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  137. /*
  138. * Add function to do table lookup of StructureSize by command
  139. * ie Validate the wct via smb2_struct_sizes table above
  140. */
  141. if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
  142. struct smb2_transform_hdr *thdr =
  143. (struct smb2_transform_hdr *)buf;
  144. struct cifs_ses *ses = NULL;
  145. struct cifs_ses *iter;
  146. /* decrypt frame now that it is completely read in */
  147. spin_lock(&cifs_tcp_ses_lock);
  148. list_for_each_entry(iter, &pserver->smb_ses_list, smb_ses_list) {
  149. if (iter->Suid == le64_to_cpu(thdr->SessionId)) {
  150. ses = iter;
  151. break;
  152. }
  153. }
  154. spin_unlock(&cifs_tcp_ses_lock);
  155. if (!ses) {
  156. cifs_dbg(VFS, "no decryption - session id not found\n");
  157. return 1;
  158. }
  159. }
  160. mid = le64_to_cpu(shdr->MessageId);
  161. if (check_smb2_hdr(shdr, mid))
  162. return 1;
  163. if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
  164. cifs_dbg(VFS, "Invalid structure size %u\n",
  165. le16_to_cpu(shdr->StructureSize));
  166. return 1;
  167. }
  168. command = le16_to_cpu(shdr->Command);
  169. if (command >= NUMBER_OF_SMB2_COMMANDS) {
  170. cifs_dbg(VFS, "Invalid SMB2 command %d\n", command);
  171. return 1;
  172. }
  173. if (len < pdu_size) {
  174. if ((len >= hdr_size)
  175. && (shdr->Status != 0)) {
  176. pdu->StructureSize2 = 0;
  177. /*
  178. * As with SMB/CIFS, on some error cases servers may
  179. * not return wct properly
  180. */
  181. return 0;
  182. } else {
  183. cifs_dbg(VFS, "Length less than SMB header size\n");
  184. }
  185. return 1;
  186. }
  187. if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE) {
  188. cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n",
  189. mid);
  190. return 1;
  191. }
  192. if (smb2_rsp_struct_sizes[command] != pdu->StructureSize2) {
  193. if (command != SMB2_OPLOCK_BREAK_HE && (shdr->Status == 0 ||
  194. pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2_LE)) {
  195. /* error packets have 9 byte structure size */
  196. cifs_dbg(VFS, "Invalid response size %u for command %d\n",
  197. le16_to_cpu(pdu->StructureSize2), command);
  198. return 1;
  199. } else if (command == SMB2_OPLOCK_BREAK_HE
  200. && (shdr->Status == 0)
  201. && (le16_to_cpu(pdu->StructureSize2) != 44)
  202. && (le16_to_cpu(pdu->StructureSize2) != 36)) {
  203. /* special case for SMB2.1 lease break message */
  204. cifs_dbg(VFS, "Invalid response size %d for oplock break\n",
  205. le16_to_cpu(pdu->StructureSize2));
  206. return 1;
  207. }
  208. }
  209. calc_len = smb2_calc_size(buf);
  210. /* For SMB2_IOCTL, OutputOffset and OutputLength are optional, so might
  211. * be 0, and not a real miscalculation */
  212. if (command == SMB2_IOCTL_HE && calc_len == 0)
  213. return 0;
  214. if (command == SMB2_NEGOTIATE_HE)
  215. calc_len += get_neg_ctxt_len(shdr, len, calc_len);
  216. if (len != calc_len) {
  217. /* create failed on symlink */
  218. if (command == SMB2_CREATE_HE &&
  219. shdr->Status == STATUS_STOPPED_ON_SYMLINK)
  220. return 0;
  221. /* Windows 7 server returns 24 bytes more */
  222. if (calc_len + 24 == len && command == SMB2_OPLOCK_BREAK_HE)
  223. return 0;
  224. /* server can return one byte more due to implied bcc[0] */
  225. if (calc_len == len + 1)
  226. return 0;
  227. /*
  228. * Some windows servers (win2016) will pad also the final
  229. * PDU in a compound to 8 bytes.
  230. */
  231. if (ALIGN(calc_len, 8) == len)
  232. return 0;
  233. /*
  234. * MacOS server pads after SMB2.1 write response with 3 bytes
  235. * of junk. Other servers match RFC1001 len to actual
  236. * SMB2/SMB3 frame length (header + smb2 response specific data)
  237. * Some windows servers also pad up to 8 bytes when compounding.
  238. */
  239. if (calc_len < len)
  240. return 0;
  241. /* Only log a message if len was really miscalculated */
  242. if (unlikely(cifsFYI))
  243. cifs_dbg(FYI, "Server response too short: calculated "
  244. "length %u doesn't match read length %u (cmd=%d, mid=%llu)\n",
  245. calc_len, len, command, mid);
  246. else
  247. pr_warn("Server response too short: calculated length "
  248. "%u doesn't match read length %u (cmd=%d, mid=%llu)\n",
  249. calc_len, len, command, mid);
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. /*
  255. * The size of the variable area depends on the offset and length fields
  256. * located in different fields for various SMB2 responses. SMB2 responses
  257. * with no variable length info, show an offset of zero for the offset field.
  258. */
  259. static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
  260. /* SMB2_NEGOTIATE */ true,
  261. /* SMB2_SESSION_SETUP */ true,
  262. /* SMB2_LOGOFF */ false,
  263. /* SMB2_TREE_CONNECT */ false,
  264. /* SMB2_TREE_DISCONNECT */ false,
  265. /* SMB2_CREATE */ true,
  266. /* SMB2_CLOSE */ false,
  267. /* SMB2_FLUSH */ false,
  268. /* SMB2_READ */ true,
  269. /* SMB2_WRITE */ false,
  270. /* SMB2_LOCK */ false,
  271. /* SMB2_IOCTL */ true,
  272. /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */
  273. /* SMB2_ECHO */ false,
  274. /* SMB2_QUERY_DIRECTORY */ true,
  275. /* SMB2_CHANGE_NOTIFY */ true,
  276. /* SMB2_QUERY_INFO */ true,
  277. /* SMB2_SET_INFO */ false,
  278. /* SMB2_OPLOCK_BREAK */ false
  279. };
  280. /*
  281. * Returns the pointer to the beginning of the data area. Length of the data
  282. * area and the offset to it (from the beginning of the smb are also returned.
  283. */
  284. char *
  285. smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *shdr)
  286. {
  287. const int max_off = 4096;
  288. const int max_len = 128 * 1024;
  289. *off = 0;
  290. *len = 0;
  291. /* error responses do not have data area */
  292. if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED &&
  293. (((struct smb2_err_rsp *)shdr)->StructureSize) ==
  294. SMB2_ERROR_STRUCTURE_SIZE2_LE)
  295. return NULL;
  296. /*
  297. * Following commands have data areas so we have to get the location
  298. * of the data buffer offset and data buffer length for the particular
  299. * command.
  300. */
  301. switch (shdr->Command) {
  302. case SMB2_NEGOTIATE:
  303. *off = le16_to_cpu(
  304. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferOffset);
  305. *len = le16_to_cpu(
  306. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferLength);
  307. break;
  308. case SMB2_SESSION_SETUP:
  309. *off = le16_to_cpu(
  310. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferOffset);
  311. *len = le16_to_cpu(
  312. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferLength);
  313. break;
  314. case SMB2_CREATE:
  315. *off = le32_to_cpu(
  316. ((struct smb2_create_rsp *)shdr)->CreateContextsOffset);
  317. *len = le32_to_cpu(
  318. ((struct smb2_create_rsp *)shdr)->CreateContextsLength);
  319. break;
  320. case SMB2_QUERY_INFO:
  321. *off = le16_to_cpu(
  322. ((struct smb2_query_info_rsp *)shdr)->OutputBufferOffset);
  323. *len = le32_to_cpu(
  324. ((struct smb2_query_info_rsp *)shdr)->OutputBufferLength);
  325. break;
  326. case SMB2_READ:
  327. /* TODO: is this a bug ? */
  328. *off = ((struct smb2_read_rsp *)shdr)->DataOffset;
  329. *len = le32_to_cpu(((struct smb2_read_rsp *)shdr)->DataLength);
  330. break;
  331. case SMB2_QUERY_DIRECTORY:
  332. *off = le16_to_cpu(
  333. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferOffset);
  334. *len = le32_to_cpu(
  335. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferLength);
  336. break;
  337. case SMB2_IOCTL:
  338. *off = le32_to_cpu(
  339. ((struct smb2_ioctl_rsp *)shdr)->OutputOffset);
  340. *len = le32_to_cpu(
  341. ((struct smb2_ioctl_rsp *)shdr)->OutputCount);
  342. break;
  343. case SMB2_CHANGE_NOTIFY:
  344. *off = le16_to_cpu(
  345. ((struct smb2_change_notify_rsp *)shdr)->OutputBufferOffset);
  346. *len = le32_to_cpu(
  347. ((struct smb2_change_notify_rsp *)shdr)->OutputBufferLength);
  348. break;
  349. default:
  350. cifs_dbg(VFS, "no length check for command %d\n", le16_to_cpu(shdr->Command));
  351. break;
  352. }
  353. /*
  354. * Invalid length or offset probably means data area is invalid, but
  355. * we have little choice but to ignore the data area in this case.
  356. */
  357. if (unlikely(*off < 0 || *off > max_off ||
  358. *len < 0 || *len > max_len)) {
  359. cifs_dbg(VFS, "%s: invalid data area (off=%d len=%d)\n",
  360. __func__, *off, *len);
  361. *off = 0;
  362. *len = 0;
  363. } else if (*off == 0) {
  364. *len = 0;
  365. }
  366. /* return pointer to beginning of data area, ie offset from SMB start */
  367. if (*off > 0 && *len > 0)
  368. return (char *)shdr + *off;
  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)
  377. {
  378. struct smb2_pdu *pdu = buf;
  379. struct smb2_hdr *shdr = &pdu->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_LE;
  446. if (CIFS_CACHE_HANDLE(cinode))
  447. lease |= SMB2_LEASE_HANDLE_CACHING_LE;
  448. if (CIFS_CACHE_READ(cinode))
  449. lease |= SMB2_LEASE_READ_CACHING_LE;
  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 cifsFileInfo *cfile;
  491. struct cifsInodeInfo *cinode;
  492. int ack_req = le32_to_cpu(rsp->Flags &
  493. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  494. lease_state = le32_to_cpu(rsp->NewLeaseState);
  495. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  496. cinode = CIFS_I(d_inode(cfile->dentry));
  497. if (memcmp(cinode->lease_key, rsp->LeaseKey,
  498. SMB2_LEASE_KEY_SIZE))
  499. continue;
  500. cifs_dbg(FYI, "found in the open list\n");
  501. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  502. lease_state);
  503. if (ack_req)
  504. cfile->oplock_break_cancelled = false;
  505. else
  506. cfile->oplock_break_cancelled = true;
  507. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  508. cfile->oplock_epoch = le16_to_cpu(rsp->Epoch);
  509. cfile->oplock_level = lease_state;
  510. cifs_queue_oplock_break(cfile);
  511. return true;
  512. }
  513. return false;
  514. }
  515. static struct cifs_pending_open *
  516. smb2_tcon_find_pending_open_lease(struct cifs_tcon *tcon,
  517. struct smb2_lease_break *rsp)
  518. {
  519. __u8 lease_state = le32_to_cpu(rsp->NewLeaseState);
  520. int ack_req = le32_to_cpu(rsp->Flags &
  521. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  522. struct cifs_pending_open *open;
  523. struct cifs_pending_open *found = NULL;
  524. list_for_each_entry(open, &tcon->pending_opens, olist) {
  525. if (memcmp(open->lease_key, rsp->LeaseKey,
  526. SMB2_LEASE_KEY_SIZE))
  527. continue;
  528. if (!found && ack_req) {
  529. found = open;
  530. }
  531. cifs_dbg(FYI, "found in the pending open list\n");
  532. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  533. lease_state);
  534. open->oplock = lease_state;
  535. }
  536. return found;
  537. }
  538. static bool
  539. smb2_is_valid_lease_break(char *buffer, struct TCP_Server_Info *server)
  540. {
  541. struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer;
  542. struct TCP_Server_Info *pserver;
  543. struct cifs_ses *ses;
  544. struct cifs_tcon *tcon;
  545. struct cifs_pending_open *open;
  546. cifs_dbg(FYI, "Checking for lease break\n");
  547. /* If server is a channel, select the primary channel */
  548. pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  549. /* look up tcon based on tid & uid */
  550. spin_lock(&cifs_tcp_ses_lock);
  551. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  552. if (cifs_ses_exiting(ses))
  553. continue;
  554. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  555. spin_lock(&tcon->open_file_lock);
  556. cifs_stats_inc(
  557. &tcon->stats.cifs_stats.num_oplock_brks);
  558. if (smb2_tcon_has_lease(tcon, rsp)) {
  559. spin_unlock(&tcon->open_file_lock);
  560. spin_unlock(&cifs_tcp_ses_lock);
  561. return true;
  562. }
  563. open = smb2_tcon_find_pending_open_lease(tcon,
  564. rsp);
  565. if (open) {
  566. __u8 lease_key[SMB2_LEASE_KEY_SIZE];
  567. struct tcon_link *tlink;
  568. tlink = cifs_get_tlink(open->tlink);
  569. memcpy(lease_key, open->lease_key,
  570. SMB2_LEASE_KEY_SIZE);
  571. spin_unlock(&tcon->open_file_lock);
  572. spin_unlock(&cifs_tcp_ses_lock);
  573. smb2_queue_pending_open_break(tlink,
  574. lease_key,
  575. rsp->NewLeaseState);
  576. return true;
  577. }
  578. spin_unlock(&tcon->open_file_lock);
  579. if (cached_dir_lease_break(tcon, rsp->LeaseKey)) {
  580. spin_unlock(&cifs_tcp_ses_lock);
  581. return true;
  582. }
  583. }
  584. }
  585. spin_unlock(&cifs_tcp_ses_lock);
  586. cifs_dbg(FYI, "Can not process lease break - no lease matched\n");
  587. trace_smb3_lease_not_found(le32_to_cpu(rsp->CurrentLeaseState),
  588. le32_to_cpu(rsp->hdr.Id.SyncId.TreeId),
  589. le64_to_cpu(rsp->hdr.SessionId),
  590. *((u64 *)rsp->LeaseKey),
  591. *((u64 *)&rsp->LeaseKey[8]));
  592. return false;
  593. }
  594. bool
  595. smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server)
  596. {
  597. struct smb2_oplock_break *rsp = (struct smb2_oplock_break *)buffer;
  598. struct TCP_Server_Info *pserver;
  599. struct cifs_ses *ses;
  600. struct cifs_tcon *tcon;
  601. struct cifsInodeInfo *cinode;
  602. struct cifsFileInfo *cfile;
  603. cifs_dbg(FYI, "Checking for oplock break\n");
  604. if (rsp->hdr.Command != SMB2_OPLOCK_BREAK)
  605. return false;
  606. if (rsp->StructureSize !=
  607. smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) {
  608. if (le16_to_cpu(rsp->StructureSize) == 44)
  609. return smb2_is_valid_lease_break(buffer, server);
  610. else
  611. return false;
  612. }
  613. cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel);
  614. /* If server is a channel, select the primary channel */
  615. pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  616. /* look up tcon based on tid & uid */
  617. spin_lock(&cifs_tcp_ses_lock);
  618. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  619. if (cifs_ses_exiting(ses))
  620. continue;
  621. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  622. spin_lock(&tcon->open_file_lock);
  623. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  624. if (rsp->PersistentFid !=
  625. cfile->fid.persistent_fid ||
  626. rsp->VolatileFid !=
  627. cfile->fid.volatile_fid)
  628. continue;
  629. cifs_dbg(FYI, "file id match, oplock break\n");
  630. cifs_stats_inc(
  631. &tcon->stats.cifs_stats.num_oplock_brks);
  632. cinode = CIFS_I(d_inode(cfile->dentry));
  633. spin_lock(&cfile->file_info_lock);
  634. if (!CIFS_CACHE_WRITE(cinode) &&
  635. rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE)
  636. cfile->oplock_break_cancelled = true;
  637. else
  638. cfile->oplock_break_cancelled = false;
  639. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  640. &cinode->flags);
  641. cfile->oplock_epoch = 0;
  642. cfile->oplock_level = rsp->OplockLevel;
  643. spin_unlock(&cfile->file_info_lock);
  644. cifs_queue_oplock_break(cfile);
  645. spin_unlock(&tcon->open_file_lock);
  646. spin_unlock(&cifs_tcp_ses_lock);
  647. return true;
  648. }
  649. spin_unlock(&tcon->open_file_lock);
  650. }
  651. }
  652. spin_unlock(&cifs_tcp_ses_lock);
  653. cifs_dbg(FYI, "No file id matched, oplock break ignored\n");
  654. trace_smb3_oplock_not_found(0 /* no xid */, rsp->PersistentFid,
  655. le32_to_cpu(rsp->hdr.Id.SyncId.TreeId),
  656. le64_to_cpu(rsp->hdr.SessionId));
  657. return true;
  658. }
  659. void
  660. smb2_cancelled_close_fid(struct work_struct *work)
  661. {
  662. struct close_cancelled_open *cancelled = container_of(work,
  663. struct close_cancelled_open, work);
  664. struct cifs_tcon *tcon = cancelled->tcon;
  665. int rc;
  666. if (cancelled->mid)
  667. cifs_tcon_dbg(VFS, "Close unmatched open for MID:%llu\n",
  668. cancelled->mid);
  669. else
  670. cifs_tcon_dbg(VFS, "Close interrupted close\n");
  671. rc = SMB2_close(0, tcon, cancelled->fid.persistent_fid,
  672. cancelled->fid.volatile_fid);
  673. if (rc)
  674. cifs_tcon_dbg(VFS, "Close cancelled mid failed rc:%d\n", rc);
  675. cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cancelled_close_fid);
  676. kfree(cancelled);
  677. }
  678. /*
  679. * Caller should already has an extra reference to @tcon
  680. * This function is used to queue work to close a handle to prevent leaks
  681. * on the server.
  682. * We handle two cases. If an open was interrupted after we sent the
  683. * SMB2_CREATE to the server but before we processed the reply, and second
  684. * if a close was interrupted before we sent the SMB2_CLOSE to the server.
  685. */
  686. static int
  687. __smb2_handle_cancelled_cmd(struct cifs_tcon *tcon, __u16 cmd, __u64 mid,
  688. __u64 persistent_fid, __u64 volatile_fid)
  689. {
  690. struct close_cancelled_open *cancelled;
  691. cancelled = kzalloc(sizeof(*cancelled), GFP_KERNEL);
  692. if (!cancelled)
  693. return -ENOMEM;
  694. cancelled->fid.persistent_fid = persistent_fid;
  695. cancelled->fid.volatile_fid = volatile_fid;
  696. cancelled->tcon = tcon;
  697. cancelled->cmd = cmd;
  698. cancelled->mid = mid;
  699. INIT_WORK(&cancelled->work, smb2_cancelled_close_fid);
  700. WARN_ON(queue_work(cifsiod_wq, &cancelled->work) == false);
  701. return 0;
  702. }
  703. int
  704. smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
  705. __u64 volatile_fid)
  706. {
  707. int rc;
  708. cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  709. spin_lock(&cifs_tcp_ses_lock);
  710. if (tcon->tc_count <= 0) {
  711. struct TCP_Server_Info *server = NULL;
  712. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
  713. netfs_trace_tcon_ref_see_cancelled_close);
  714. WARN_ONCE(tcon->tc_count < 0, "tcon refcount is negative");
  715. spin_unlock(&cifs_tcp_ses_lock);
  716. if (tcon->ses) {
  717. server = tcon->ses->server;
  718. cifs_server_dbg(FYI,
  719. "tid=0x%x: tcon is closing, skipping async close retry of fid %llu %llu\n",
  720. tcon->tid, persistent_fid, volatile_fid);
  721. }
  722. return 0;
  723. }
  724. tcon->tc_count++;
  725. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
  726. netfs_trace_tcon_ref_get_cancelled_close);
  727. spin_unlock(&cifs_tcp_ses_lock);
  728. rc = __smb2_handle_cancelled_cmd(tcon, SMB2_CLOSE_HE, 0,
  729. persistent_fid, volatile_fid);
  730. if (rc)
  731. cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cancelled_close);
  732. return rc;
  733. }
  734. int
  735. smb2_handle_cancelled_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  736. {
  737. struct smb2_hdr *hdr = mid->resp_buf;
  738. struct smb2_create_rsp *rsp = mid->resp_buf;
  739. struct cifs_tcon *tcon;
  740. int rc;
  741. if ((mid->optype & CIFS_CP_CREATE_CLOSE_OP) || hdr->Command != SMB2_CREATE ||
  742. hdr->Status != STATUS_SUCCESS)
  743. return 0;
  744. tcon = smb2_find_smb_tcon(server, le64_to_cpu(hdr->SessionId),
  745. le32_to_cpu(hdr->Id.SyncId.TreeId));
  746. if (!tcon)
  747. return -ENOENT;
  748. rc = __smb2_handle_cancelled_cmd(tcon,
  749. le16_to_cpu(hdr->Command),
  750. le64_to_cpu(hdr->MessageId),
  751. rsp->PersistentFileId,
  752. rsp->VolatileFileId);
  753. if (rc)
  754. cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cancelled_mid);
  755. return rc;
  756. }
  757. /**
  758. * smb311_update_preauth_hash - update @ses hash with the packet data in @iov
  759. *
  760. * Assumes @iov does not contain the rfc1002 length and iov[0] has the
  761. * SMB2 header.
  762. *
  763. * @ses: server session structure
  764. * @server: pointer to server info
  765. * @iov: array containing the SMB request we will send to the server
  766. * @nvec: number of array entries for the iov
  767. */
  768. int
  769. smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,
  770. struct kvec *iov, int nvec)
  771. {
  772. int i, rc;
  773. struct smb2_hdr *hdr;
  774. struct shash_desc *sha512 = NULL;
  775. hdr = (struct smb2_hdr *)iov[0].iov_base;
  776. /* neg prot are always taken */
  777. if (hdr->Command == SMB2_NEGOTIATE)
  778. goto ok;
  779. /*
  780. * If we process a command which wasn't a negprot it means the
  781. * neg prot was already done, so the server dialect was set
  782. * and we can test it. Preauth requires 3.1.1 for now.
  783. */
  784. if (server->dialect != SMB311_PROT_ID)
  785. return 0;
  786. if (hdr->Command != SMB2_SESSION_SETUP)
  787. return 0;
  788. /* skip last sess setup response */
  789. if ((hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
  790. && (hdr->Status == NT_STATUS_OK
  791. || (hdr->Status !=
  792. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))))
  793. return 0;
  794. ok:
  795. rc = smb311_crypto_shash_allocate(server);
  796. if (rc)
  797. return rc;
  798. sha512 = server->secmech.sha512;
  799. rc = crypto_shash_init(sha512);
  800. if (rc) {
  801. cifs_dbg(VFS, "%s: Could not init sha512 shash\n", __func__);
  802. return rc;
  803. }
  804. rc = crypto_shash_update(sha512, ses->preauth_sha_hash,
  805. SMB2_PREAUTH_HASH_SIZE);
  806. if (rc) {
  807. cifs_dbg(VFS, "%s: Could not update sha512 shash\n", __func__);
  808. return rc;
  809. }
  810. for (i = 0; i < nvec; i++) {
  811. rc = crypto_shash_update(sha512, iov[i].iov_base, iov[i].iov_len);
  812. if (rc) {
  813. cifs_dbg(VFS, "%s: Could not update sha512 shash\n",
  814. __func__);
  815. return rc;
  816. }
  817. }
  818. rc = crypto_shash_final(sha512, ses->preauth_sha_hash);
  819. if (rc) {
  820. cifs_dbg(VFS, "%s: Could not finalize sha512 shash\n",
  821. __func__);
  822. return rc;
  823. }
  824. return 0;
  825. }