smb2misc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 ([email protected])
  7. * Pavel Shilovsky ([email protected]) 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 "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)
  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 = CIFS_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 (len < pdu_size) {
  162. if ((len >= hdr_size)
  163. && (shdr->Status != 0)) {
  164. pdu->StructureSize2 = 0;
  165. /*
  166. * As with SMB/CIFS, on some error cases servers may
  167. * not return wct properly
  168. */
  169. return 0;
  170. } else {
  171. cifs_dbg(VFS, "Length less than SMB header size\n");
  172. }
  173. return 1;
  174. }
  175. if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE) {
  176. cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n",
  177. mid);
  178. return 1;
  179. }
  180. if (check_smb2_hdr(shdr, mid))
  181. return 1;
  182. if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
  183. cifs_dbg(VFS, "Invalid structure size %u\n",
  184. le16_to_cpu(shdr->StructureSize));
  185. return 1;
  186. }
  187. command = le16_to_cpu(shdr->Command);
  188. if (command >= NUMBER_OF_SMB2_COMMANDS) {
  189. cifs_dbg(VFS, "Invalid SMB2 command %d\n", command);
  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. *off = 0;
  288. *len = 0;
  289. /* error responses do not have data area */
  290. if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED &&
  291. (((struct smb2_err_rsp *)shdr)->StructureSize) ==
  292. SMB2_ERROR_STRUCTURE_SIZE2_LE)
  293. return NULL;
  294. /*
  295. * Following commands have data areas so we have to get the location
  296. * of the data buffer offset and data buffer length for the particular
  297. * command.
  298. */
  299. switch (shdr->Command) {
  300. case SMB2_NEGOTIATE:
  301. *off = le16_to_cpu(
  302. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferOffset);
  303. *len = le16_to_cpu(
  304. ((struct smb2_negotiate_rsp *)shdr)->SecurityBufferLength);
  305. break;
  306. case SMB2_SESSION_SETUP:
  307. *off = le16_to_cpu(
  308. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferOffset);
  309. *len = le16_to_cpu(
  310. ((struct smb2_sess_setup_rsp *)shdr)->SecurityBufferLength);
  311. break;
  312. case SMB2_CREATE:
  313. *off = le32_to_cpu(
  314. ((struct smb2_create_rsp *)shdr)->CreateContextsOffset);
  315. *len = le32_to_cpu(
  316. ((struct smb2_create_rsp *)shdr)->CreateContextsLength);
  317. break;
  318. case SMB2_QUERY_INFO:
  319. *off = le16_to_cpu(
  320. ((struct smb2_query_info_rsp *)shdr)->OutputBufferOffset);
  321. *len = le32_to_cpu(
  322. ((struct smb2_query_info_rsp *)shdr)->OutputBufferLength);
  323. break;
  324. case SMB2_READ:
  325. /* TODO: is this a bug ? */
  326. *off = ((struct smb2_read_rsp *)shdr)->DataOffset;
  327. *len = le32_to_cpu(((struct smb2_read_rsp *)shdr)->DataLength);
  328. break;
  329. case SMB2_QUERY_DIRECTORY:
  330. *off = le16_to_cpu(
  331. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferOffset);
  332. *len = le32_to_cpu(
  333. ((struct smb2_query_directory_rsp *)shdr)->OutputBufferLength);
  334. break;
  335. case SMB2_IOCTL:
  336. *off = le32_to_cpu(
  337. ((struct smb2_ioctl_rsp *)shdr)->OutputOffset);
  338. *len = le32_to_cpu(
  339. ((struct smb2_ioctl_rsp *)shdr)->OutputCount);
  340. break;
  341. case SMB2_CHANGE_NOTIFY:
  342. *off = le16_to_cpu(
  343. ((struct smb2_change_notify_rsp *)shdr)->OutputBufferOffset);
  344. *len = le32_to_cpu(
  345. ((struct smb2_change_notify_rsp *)shdr)->OutputBufferLength);
  346. break;
  347. default:
  348. cifs_dbg(VFS, "no length check for command %d\n", le16_to_cpu(shdr->Command));
  349. break;
  350. }
  351. /*
  352. * Invalid length or offset probably means data area is invalid, but
  353. * we have little choice but to ignore the data area in this case.
  354. */
  355. if (*off > 4096) {
  356. cifs_dbg(VFS, "offset %d too large, data area ignored\n", *off);
  357. *len = 0;
  358. *off = 0;
  359. } else if (*off < 0) {
  360. cifs_dbg(VFS, "negative offset %d to data invalid ignore data area\n",
  361. *off);
  362. *off = 0;
  363. *len = 0;
  364. } else if (*len < 0) {
  365. cifs_dbg(VFS, "negative data length %d invalid, data area ignored\n",
  366. *len);
  367. *len = 0;
  368. } else if (*len > 128 * 1024) {
  369. cifs_dbg(VFS, "data area larger than 128K: %d\n", *len);
  370. *len = 0;
  371. }
  372. /* return pointer to beginning of data area, ie offset from SMB start */
  373. if ((*off != 0) && (*len != 0))
  374. return (char *)shdr + *off;
  375. else
  376. return NULL;
  377. }
  378. /*
  379. * Calculate the size of the SMB message based on the fixed header
  380. * portion, the number of word parameters and the data portion of the message.
  381. */
  382. unsigned int
  383. smb2_calc_size(void *buf)
  384. {
  385. struct smb2_pdu *pdu = buf;
  386. struct smb2_hdr *shdr = &pdu->hdr;
  387. int offset; /* the offset from the beginning of SMB to data area */
  388. int data_length; /* the length of the variable length data area */
  389. /* Structure Size has already been checked to make sure it is 64 */
  390. int len = le16_to_cpu(shdr->StructureSize);
  391. /*
  392. * StructureSize2, ie length of fixed parameter area has already
  393. * been checked to make sure it is the correct length.
  394. */
  395. len += le16_to_cpu(pdu->StructureSize2);
  396. if (has_smb2_data_area[le16_to_cpu(shdr->Command)] == false)
  397. goto calc_size_exit;
  398. smb2_get_data_area_len(&offset, &data_length, shdr);
  399. cifs_dbg(FYI, "SMB2 data length %d offset %d\n", data_length, offset);
  400. if (data_length > 0) {
  401. /*
  402. * Check to make sure that data area begins after fixed area,
  403. * Note that last byte of the fixed area is part of data area
  404. * for some commands, typically those with odd StructureSize,
  405. * so we must add one to the calculation.
  406. */
  407. if (offset + 1 < len) {
  408. cifs_dbg(VFS, "data area offset %d overlaps SMB2 header %d\n",
  409. offset + 1, len);
  410. data_length = 0;
  411. } else {
  412. len = offset + data_length;
  413. }
  414. }
  415. calc_size_exit:
  416. cifs_dbg(FYI, "SMB2 len %d\n", len);
  417. return len;
  418. }
  419. /* Note: caller must free return buffer */
  420. __le16 *
  421. cifs_convert_path_to_utf16(const char *from, struct cifs_sb_info *cifs_sb)
  422. {
  423. int len;
  424. const char *start_of_path;
  425. __le16 *to;
  426. int map_type;
  427. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
  428. map_type = SFM_MAP_UNI_RSVD;
  429. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  430. map_type = SFU_MAP_UNI_RSVD;
  431. else
  432. map_type = NO_MAP_UNI_RSVD;
  433. /* Windows doesn't allow paths beginning with \ */
  434. if (from[0] == '\\')
  435. start_of_path = from + 1;
  436. /* SMB311 POSIX extensions paths do not include leading slash */
  437. else if (cifs_sb_master_tlink(cifs_sb) &&
  438. cifs_sb_master_tcon(cifs_sb)->posix_extensions &&
  439. (from[0] == '/')) {
  440. start_of_path = from + 1;
  441. } else
  442. start_of_path = from;
  443. to = cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len,
  444. cifs_sb->local_nls, map_type);
  445. return to;
  446. }
  447. __le32
  448. smb2_get_lease_state(struct cifsInodeInfo *cinode)
  449. {
  450. __le32 lease = 0;
  451. if (CIFS_CACHE_WRITE(cinode))
  452. lease |= SMB2_LEASE_WRITE_CACHING_LE;
  453. if (CIFS_CACHE_HANDLE(cinode))
  454. lease |= SMB2_LEASE_HANDLE_CACHING_LE;
  455. if (CIFS_CACHE_READ(cinode))
  456. lease |= SMB2_LEASE_READ_CACHING_LE;
  457. return lease;
  458. }
  459. struct smb2_lease_break_work {
  460. struct work_struct lease_break;
  461. struct tcon_link *tlink;
  462. __u8 lease_key[16];
  463. __le32 lease_state;
  464. };
  465. static void
  466. cifs_ses_oplock_break(struct work_struct *work)
  467. {
  468. struct smb2_lease_break_work *lw = container_of(work,
  469. struct smb2_lease_break_work, lease_break);
  470. int rc = 0;
  471. rc = SMB2_lease_break(0, tlink_tcon(lw->tlink), lw->lease_key,
  472. lw->lease_state);
  473. cifs_dbg(FYI, "Lease release rc %d\n", rc);
  474. cifs_put_tlink(lw->tlink);
  475. kfree(lw);
  476. }
  477. static void
  478. smb2_queue_pending_open_break(struct tcon_link *tlink, __u8 *lease_key,
  479. __le32 new_lease_state)
  480. {
  481. struct smb2_lease_break_work *lw;
  482. lw = kmalloc(sizeof(struct smb2_lease_break_work), GFP_KERNEL);
  483. if (!lw) {
  484. cifs_put_tlink(tlink);
  485. return;
  486. }
  487. INIT_WORK(&lw->lease_break, cifs_ses_oplock_break);
  488. lw->tlink = tlink;
  489. lw->lease_state = new_lease_state;
  490. memcpy(lw->lease_key, lease_key, SMB2_LEASE_KEY_SIZE);
  491. queue_work(cifsiod_wq, &lw->lease_break);
  492. }
  493. static bool
  494. smb2_tcon_has_lease(struct cifs_tcon *tcon, struct smb2_lease_break *rsp)
  495. {
  496. __u8 lease_state;
  497. struct cifsFileInfo *cfile;
  498. struct cifsInodeInfo *cinode;
  499. int ack_req = le32_to_cpu(rsp->Flags &
  500. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  501. lease_state = le32_to_cpu(rsp->NewLeaseState);
  502. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  503. cinode = CIFS_I(d_inode(cfile->dentry));
  504. if (memcmp(cinode->lease_key, rsp->LeaseKey,
  505. SMB2_LEASE_KEY_SIZE))
  506. continue;
  507. cifs_dbg(FYI, "found in the open list\n");
  508. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  509. lease_state);
  510. if (ack_req)
  511. cfile->oplock_break_cancelled = false;
  512. else
  513. cfile->oplock_break_cancelled = true;
  514. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  515. cfile->oplock_epoch = le16_to_cpu(rsp->Epoch);
  516. cfile->oplock_level = lease_state;
  517. cifs_queue_oplock_break(cfile);
  518. return true;
  519. }
  520. return false;
  521. }
  522. static struct cifs_pending_open *
  523. smb2_tcon_find_pending_open_lease(struct cifs_tcon *tcon,
  524. struct smb2_lease_break *rsp)
  525. {
  526. __u8 lease_state = le32_to_cpu(rsp->NewLeaseState);
  527. int ack_req = le32_to_cpu(rsp->Flags &
  528. SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);
  529. struct cifs_pending_open *open;
  530. struct cifs_pending_open *found = NULL;
  531. list_for_each_entry(open, &tcon->pending_opens, olist) {
  532. if (memcmp(open->lease_key, rsp->LeaseKey,
  533. SMB2_LEASE_KEY_SIZE))
  534. continue;
  535. if (!found && ack_req) {
  536. found = open;
  537. }
  538. cifs_dbg(FYI, "found in the pending open list\n");
  539. cifs_dbg(FYI, "lease key match, lease break 0x%x\n",
  540. lease_state);
  541. open->oplock = lease_state;
  542. }
  543. return found;
  544. }
  545. static bool
  546. smb2_is_valid_lease_break(char *buffer, struct TCP_Server_Info *server)
  547. {
  548. struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer;
  549. struct TCP_Server_Info *pserver;
  550. struct cifs_ses *ses;
  551. struct cifs_tcon *tcon;
  552. struct cifs_pending_open *open;
  553. cifs_dbg(FYI, "Checking for lease break\n");
  554. /* If server is a channel, select the primary channel */
  555. pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
  556. /* look up tcon based on tid & uid */
  557. spin_lock(&cifs_tcp_ses_lock);
  558. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  559. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  560. spin_lock(&tcon->open_file_lock);
  561. cifs_stats_inc(
  562. &tcon->stats.cifs_stats.num_oplock_brks);
  563. if (smb2_tcon_has_lease(tcon, rsp)) {
  564. spin_unlock(&tcon->open_file_lock);
  565. spin_unlock(&cifs_tcp_ses_lock);
  566. return true;
  567. }
  568. open = smb2_tcon_find_pending_open_lease(tcon,
  569. rsp);
  570. if (open) {
  571. __u8 lease_key[SMB2_LEASE_KEY_SIZE];
  572. struct tcon_link *tlink;
  573. tlink = cifs_get_tlink(open->tlink);
  574. memcpy(lease_key, open->lease_key,
  575. SMB2_LEASE_KEY_SIZE);
  576. spin_unlock(&tcon->open_file_lock);
  577. spin_unlock(&cifs_tcp_ses_lock);
  578. smb2_queue_pending_open_break(tlink,
  579. lease_key,
  580. rsp->NewLeaseState);
  581. return true;
  582. }
  583. spin_unlock(&tcon->open_file_lock);
  584. if (cached_dir_lease_break(tcon, rsp->LeaseKey)) {
  585. spin_unlock(&cifs_tcp_ses_lock);
  586. return true;
  587. }
  588. }
  589. }
  590. spin_unlock(&cifs_tcp_ses_lock);
  591. cifs_dbg(FYI, "Can not process lease break - no lease matched\n");
  592. trace_smb3_lease_not_found(le32_to_cpu(rsp->CurrentLeaseState),
  593. le32_to_cpu(rsp->hdr.Id.SyncId.TreeId),
  594. le64_to_cpu(rsp->hdr.SessionId),
  595. *((u64 *)rsp->LeaseKey),
  596. *((u64 *)&rsp->LeaseKey[8]));
  597. return false;
  598. }
  599. bool
  600. smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server)
  601. {
  602. struct smb2_oplock_break *rsp = (struct smb2_oplock_break *)buffer;
  603. struct TCP_Server_Info *pserver;
  604. struct cifs_ses *ses;
  605. struct cifs_tcon *tcon;
  606. struct cifsInodeInfo *cinode;
  607. struct cifsFileInfo *cfile;
  608. cifs_dbg(FYI, "Checking for oplock break\n");
  609. if (rsp->hdr.Command != SMB2_OPLOCK_BREAK)
  610. return false;
  611. if (rsp->StructureSize !=
  612. smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) {
  613. if (le16_to_cpu(rsp->StructureSize) == 44)
  614. return smb2_is_valid_lease_break(buffer, server);
  615. else
  616. return false;
  617. }
  618. cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel);
  619. /* If server is a channel, select the primary channel */
  620. pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
  621. /* look up tcon based on tid & uid */
  622. spin_lock(&cifs_tcp_ses_lock);
  623. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  624. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  625. spin_lock(&tcon->open_file_lock);
  626. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  627. if (rsp->PersistentFid !=
  628. cfile->fid.persistent_fid ||
  629. rsp->VolatileFid !=
  630. cfile->fid.volatile_fid)
  631. continue;
  632. cifs_dbg(FYI, "file id match, oplock break\n");
  633. cifs_stats_inc(
  634. &tcon->stats.cifs_stats.num_oplock_brks);
  635. cinode = CIFS_I(d_inode(cfile->dentry));
  636. spin_lock(&cfile->file_info_lock);
  637. if (!CIFS_CACHE_WRITE(cinode) &&
  638. rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE)
  639. cfile->oplock_break_cancelled = true;
  640. else
  641. cfile->oplock_break_cancelled = false;
  642. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  643. &cinode->flags);
  644. cfile->oplock_epoch = 0;
  645. cfile->oplock_level = rsp->OplockLevel;
  646. spin_unlock(&cfile->file_info_lock);
  647. cifs_queue_oplock_break(cfile);
  648. spin_unlock(&tcon->open_file_lock);
  649. spin_unlock(&cifs_tcp_ses_lock);
  650. return true;
  651. }
  652. spin_unlock(&tcon->open_file_lock);
  653. }
  654. }
  655. spin_unlock(&cifs_tcp_ses_lock);
  656. cifs_dbg(FYI, "No file id matched, oplock break ignored\n");
  657. trace_smb3_oplock_not_found(0 /* no xid */, rsp->PersistentFid,
  658. le32_to_cpu(rsp->hdr.Id.SyncId.TreeId),
  659. le64_to_cpu(rsp->hdr.SessionId));
  660. return true;
  661. }
  662. void
  663. smb2_cancelled_close_fid(struct work_struct *work)
  664. {
  665. struct close_cancelled_open *cancelled = container_of(work,
  666. struct close_cancelled_open, work);
  667. struct cifs_tcon *tcon = cancelled->tcon;
  668. int rc;
  669. if (cancelled->mid)
  670. cifs_tcon_dbg(VFS, "Close unmatched open for MID:%llu\n",
  671. cancelled->mid);
  672. else
  673. cifs_tcon_dbg(VFS, "Close interrupted close\n");
  674. rc = SMB2_close(0, tcon, cancelled->fid.persistent_fid,
  675. cancelled->fid.volatile_fid);
  676. if (rc)
  677. cifs_tcon_dbg(VFS, "Close cancelled mid failed rc:%d\n", rc);
  678. cifs_put_tcon(tcon);
  679. kfree(cancelled);
  680. }
  681. /*
  682. * Caller should already has an extra reference to @tcon
  683. * This function is used to queue work to close a handle to prevent leaks
  684. * on the server.
  685. * We handle two cases. If an open was interrupted after we sent the
  686. * SMB2_CREATE to the server but before we processed the reply, and second
  687. * if a close was interrupted before we sent the SMB2_CLOSE to the server.
  688. */
  689. static int
  690. __smb2_handle_cancelled_cmd(struct cifs_tcon *tcon, __u16 cmd, __u64 mid,
  691. __u64 persistent_fid, __u64 volatile_fid)
  692. {
  693. struct close_cancelled_open *cancelled;
  694. cancelled = kzalloc(sizeof(*cancelled), GFP_KERNEL);
  695. if (!cancelled)
  696. return -ENOMEM;
  697. cancelled->fid.persistent_fid = persistent_fid;
  698. cancelled->fid.volatile_fid = volatile_fid;
  699. cancelled->tcon = tcon;
  700. cancelled->cmd = cmd;
  701. cancelled->mid = mid;
  702. INIT_WORK(&cancelled->work, smb2_cancelled_close_fid);
  703. WARN_ON(queue_work(cifsiod_wq, &cancelled->work) == false);
  704. return 0;
  705. }
  706. int
  707. smb2_handle_cancelled_close(struct cifs_tcon *tcon, __u64 persistent_fid,
  708. __u64 volatile_fid)
  709. {
  710. int rc;
  711. cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  712. spin_lock(&cifs_tcp_ses_lock);
  713. if (tcon->tc_count <= 0) {
  714. struct TCP_Server_Info *server = NULL;
  715. WARN_ONCE(tcon->tc_count < 0, "tcon refcount is negative");
  716. spin_unlock(&cifs_tcp_ses_lock);
  717. if (tcon->ses)
  718. server = tcon->ses->server;
  719. cifs_server_dbg(FYI, "tid=0x%x: tcon is closing, skipping async close retry of fid %llu %llu\n",
  720. tcon->tid, persistent_fid, volatile_fid);
  721. return 0;
  722. }
  723. tcon->tc_count++;
  724. spin_unlock(&cifs_tcp_ses_lock);
  725. rc = __smb2_handle_cancelled_cmd(tcon, SMB2_CLOSE_HE, 0,
  726. persistent_fid, volatile_fid);
  727. if (rc)
  728. cifs_put_tcon(tcon);
  729. return rc;
  730. }
  731. int
  732. smb2_handle_cancelled_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server)
  733. {
  734. struct smb2_hdr *hdr = mid->resp_buf;
  735. struct smb2_create_rsp *rsp = mid->resp_buf;
  736. struct cifs_tcon *tcon;
  737. int rc;
  738. if ((mid->optype & CIFS_CP_CREATE_CLOSE_OP) || hdr->Command != SMB2_CREATE ||
  739. hdr->Status != STATUS_SUCCESS)
  740. return 0;
  741. tcon = smb2_find_smb_tcon(server, le64_to_cpu(hdr->SessionId),
  742. le32_to_cpu(hdr->Id.SyncId.TreeId));
  743. if (!tcon)
  744. return -ENOENT;
  745. rc = __smb2_handle_cancelled_cmd(tcon,
  746. le16_to_cpu(hdr->Command),
  747. le64_to_cpu(hdr->MessageId),
  748. rsp->PersistentFileId,
  749. rsp->VolatileFileId);
  750. if (rc)
  751. cifs_put_tcon(tcon);
  752. return rc;
  753. }
  754. /**
  755. * smb311_update_preauth_hash - update @ses hash with the packet data in @iov
  756. *
  757. * Assumes @iov does not contain the rfc1002 length and iov[0] has the
  758. * SMB2 header.
  759. *
  760. * @ses: server session structure
  761. * @server: pointer to server info
  762. * @iov: array containing the SMB request we will send to the server
  763. * @nvec: number of array entries for the iov
  764. */
  765. int
  766. smb311_update_preauth_hash(struct cifs_ses *ses, struct TCP_Server_Info *server,
  767. struct kvec *iov, int nvec)
  768. {
  769. int i, rc;
  770. struct smb2_hdr *hdr;
  771. struct shash_desc *sha512 = NULL;
  772. hdr = (struct smb2_hdr *)iov[0].iov_base;
  773. /* neg prot are always taken */
  774. if (hdr->Command == SMB2_NEGOTIATE)
  775. goto ok;
  776. /*
  777. * If we process a command which wasn't a negprot it means the
  778. * neg prot was already done, so the server dialect was set
  779. * and we can test it. Preauth requires 3.1.1 for now.
  780. */
  781. if (server->dialect != SMB311_PROT_ID)
  782. return 0;
  783. if (hdr->Command != SMB2_SESSION_SETUP)
  784. return 0;
  785. /* skip last sess setup response */
  786. if ((hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
  787. && (hdr->Status == NT_STATUS_OK
  788. || (hdr->Status !=
  789. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))))
  790. return 0;
  791. ok:
  792. rc = smb311_crypto_shash_allocate(server);
  793. if (rc)
  794. return rc;
  795. sha512 = server->secmech.sha512;
  796. rc = crypto_shash_init(sha512);
  797. if (rc) {
  798. cifs_dbg(VFS, "%s: Could not init sha512 shash\n", __func__);
  799. return rc;
  800. }
  801. rc = crypto_shash_update(sha512, ses->preauth_sha_hash,
  802. SMB2_PREAUTH_HASH_SIZE);
  803. if (rc) {
  804. cifs_dbg(VFS, "%s: Could not update sha512 shash\n", __func__);
  805. return rc;
  806. }
  807. for (i = 0; i < nvec; i++) {
  808. rc = crypto_shash_update(sha512, iov[i].iov_base, iov[i].iov_len);
  809. if (rc) {
  810. cifs_dbg(VFS, "%s: Could not update sha512 shash\n",
  811. __func__);
  812. return rc;
  813. }
  814. }
  815. rc = crypto_shash_final(sha512, ses->preauth_sha_hash);
  816. if (rc) {
  817. cifs_dbg(VFS, "%s: Could not finalize sha512 shash\n",
  818. __func__);
  819. return rc;
  820. }
  821. return 0;
  822. }