smb2misc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
  4. * Copyright (C) 2018 Samsung Electronics Co., Ltd.
  5. */
  6. #include "glob.h"
  7. #include "nterr.h"
  8. #include "smb_common.h"
  9. #include "../common/smb2status.h"
  10. #include "mgmt/user_session.h"
  11. #include "connection.h"
  12. static int check_smb2_hdr(struct smb2_hdr *hdr)
  13. {
  14. /*
  15. * Make sure that this really is an SMB, that it is a response.
  16. */
  17. if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
  18. return 1;
  19. return 0;
  20. }
  21. /*
  22. * The following table defines the expected "StructureSize" of SMB2 requests
  23. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  24. *
  25. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  26. * indexed by command in host byte order
  27. */
  28. static const __le16 smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  29. /* SMB2_NEGOTIATE */ cpu_to_le16(36),
  30. /* SMB2_SESSION_SETUP */ cpu_to_le16(25),
  31. /* SMB2_LOGOFF */ cpu_to_le16(4),
  32. /* SMB2_TREE_CONNECT */ cpu_to_le16(9),
  33. /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4),
  34. /* SMB2_CREATE */ cpu_to_le16(57),
  35. /* SMB2_CLOSE */ cpu_to_le16(24),
  36. /* SMB2_FLUSH */ cpu_to_le16(24),
  37. /* SMB2_READ */ cpu_to_le16(49),
  38. /* SMB2_WRITE */ cpu_to_le16(49),
  39. /* SMB2_LOCK */ cpu_to_le16(48),
  40. /* SMB2_IOCTL */ cpu_to_le16(57),
  41. /* SMB2_CANCEL */ cpu_to_le16(4),
  42. /* SMB2_ECHO */ cpu_to_le16(4),
  43. /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(33),
  44. /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(32),
  45. /* SMB2_QUERY_INFO */ cpu_to_le16(41),
  46. /* SMB2_SET_INFO */ cpu_to_le16(33),
  47. /* use 44 for lease break */
  48. /* SMB2_OPLOCK_BREAK */ cpu_to_le16(36)
  49. };
  50. /*
  51. * The size of the variable area depends on the offset and length fields
  52. * located in different fields for various SMB2 requests. SMB2 requests
  53. * with no variable length info, show an offset of zero for the offset field.
  54. */
  55. static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
  56. /* SMB2_NEGOTIATE */ true,
  57. /* SMB2_SESSION_SETUP */ true,
  58. /* SMB2_LOGOFF */ false,
  59. /* SMB2_TREE_CONNECT */ true,
  60. /* SMB2_TREE_DISCONNECT */ false,
  61. /* SMB2_CREATE */ true,
  62. /* SMB2_CLOSE */ false,
  63. /* SMB2_FLUSH */ false,
  64. /* SMB2_READ */ true,
  65. /* SMB2_WRITE */ true,
  66. /* SMB2_LOCK */ true,
  67. /* SMB2_IOCTL */ true,
  68. /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */
  69. /* SMB2_ECHO */ false,
  70. /* SMB2_QUERY_DIRECTORY */ true,
  71. /* SMB2_CHANGE_NOTIFY */ false,
  72. /* SMB2_QUERY_INFO */ true,
  73. /* SMB2_SET_INFO */ true,
  74. /* SMB2_OPLOCK_BREAK */ false
  75. };
  76. /*
  77. * Set length of the data area and the offset to arguments.
  78. * if they are invalid, return error.
  79. */
  80. static int smb2_get_data_area_len(unsigned int *off, unsigned int *len,
  81. struct smb2_hdr *hdr)
  82. {
  83. int ret = 0;
  84. *off = 0;
  85. *len = 0;
  86. /*
  87. * Following commands have data areas so we have to get the location
  88. * of the data buffer offset and data buffer length for the particular
  89. * command.
  90. */
  91. switch (hdr->Command) {
  92. case SMB2_SESSION_SETUP:
  93. *off = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferOffset);
  94. *len = le16_to_cpu(((struct smb2_sess_setup_req *)hdr)->SecurityBufferLength);
  95. break;
  96. case SMB2_TREE_CONNECT:
  97. *off = max_t(unsigned short int,
  98. le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathOffset),
  99. offsetof(struct smb2_tree_connect_req, Buffer));
  100. *len = le16_to_cpu(((struct smb2_tree_connect_req *)hdr)->PathLength);
  101. break;
  102. case SMB2_CREATE:
  103. {
  104. unsigned short int name_off =
  105. max_t(unsigned short int,
  106. le16_to_cpu(((struct smb2_create_req *)hdr)->NameOffset),
  107. offsetof(struct smb2_create_req, Buffer));
  108. unsigned short int name_len =
  109. le16_to_cpu(((struct smb2_create_req *)hdr)->NameLength);
  110. if (((struct smb2_create_req *)hdr)->CreateContextsLength) {
  111. *off = le32_to_cpu(((struct smb2_create_req *)
  112. hdr)->CreateContextsOffset);
  113. *len = le32_to_cpu(((struct smb2_create_req *)
  114. hdr)->CreateContextsLength);
  115. if (!name_len)
  116. break;
  117. if (name_off + name_len < (u64)*off + *len)
  118. break;
  119. }
  120. *off = name_off;
  121. *len = name_len;
  122. break;
  123. }
  124. case SMB2_QUERY_INFO:
  125. *off = max_t(unsigned int,
  126. le16_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferOffset),
  127. offsetof(struct smb2_query_info_req, Buffer));
  128. *len = le32_to_cpu(((struct smb2_query_info_req *)hdr)->InputBufferLength);
  129. break;
  130. case SMB2_SET_INFO:
  131. *off = max_t(unsigned int,
  132. le16_to_cpu(((struct smb2_set_info_req *)hdr)->BufferOffset),
  133. offsetof(struct smb2_set_info_req, Buffer));
  134. *len = le32_to_cpu(((struct smb2_set_info_req *)hdr)->BufferLength);
  135. break;
  136. case SMB2_READ:
  137. *off = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoOffset);
  138. *len = le16_to_cpu(((struct smb2_read_req *)hdr)->ReadChannelInfoLength);
  139. break;
  140. case SMB2_WRITE:
  141. if (((struct smb2_write_req *)hdr)->DataOffset ||
  142. ((struct smb2_write_req *)hdr)->Length) {
  143. *off = max_t(unsigned short int,
  144. le16_to_cpu(((struct smb2_write_req *)hdr)->DataOffset),
  145. offsetof(struct smb2_write_req, Buffer));
  146. *len = le32_to_cpu(((struct smb2_write_req *)hdr)->Length);
  147. break;
  148. }
  149. *off = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoOffset);
  150. *len = le16_to_cpu(((struct smb2_write_req *)hdr)->WriteChannelInfoLength);
  151. break;
  152. case SMB2_QUERY_DIRECTORY:
  153. *off = max_t(unsigned short int,
  154. le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameOffset),
  155. offsetof(struct smb2_query_directory_req, Buffer));
  156. *len = le16_to_cpu(((struct smb2_query_directory_req *)hdr)->FileNameLength);
  157. break;
  158. case SMB2_LOCK:
  159. {
  160. unsigned short lock_count;
  161. lock_count = le16_to_cpu(((struct smb2_lock_req *)hdr)->LockCount);
  162. if (lock_count > 0) {
  163. *off = offsetof(struct smb2_lock_req, locks);
  164. *len = sizeof(struct smb2_lock_element) * lock_count;
  165. }
  166. break;
  167. }
  168. case SMB2_IOCTL:
  169. *off = max_t(unsigned int,
  170. le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputOffset),
  171. offsetof(struct smb2_ioctl_req, Buffer));
  172. *len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount);
  173. break;
  174. default:
  175. ksmbd_debug(SMB, "no length check for command\n");
  176. break;
  177. }
  178. if (*off > 4096) {
  179. ksmbd_debug(SMB, "offset %d too large\n", *off);
  180. ret = -EINVAL;
  181. } else if ((u64)*off + *len > MAX_STREAM_PROT_LEN) {
  182. ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n",
  183. MAX_STREAM_PROT_LEN, (u64)*off + *len);
  184. ret = -EINVAL;
  185. }
  186. return ret;
  187. }
  188. /*
  189. * Calculate the size of the SMB message based on the fixed header
  190. * portion, the number of word parameters and the data portion of the message.
  191. */
  192. static int smb2_calc_size(void *buf, unsigned int *len)
  193. {
  194. struct smb2_pdu *pdu = (struct smb2_pdu *)buf;
  195. struct smb2_hdr *hdr = &pdu->hdr;
  196. unsigned int offset; /* the offset from the beginning of SMB to data area */
  197. unsigned int data_length; /* the length of the variable length data area */
  198. int ret;
  199. /* Structure Size has already been checked to make sure it is 64 */
  200. *len = le16_to_cpu(hdr->StructureSize);
  201. /*
  202. * StructureSize2, ie length of fixed parameter area has already
  203. * been checked to make sure it is the correct length.
  204. */
  205. *len += le16_to_cpu(pdu->StructureSize2);
  206. /*
  207. * StructureSize2 of smb2_lock pdu is set to 48, indicating
  208. * the size of smb2 lock request with single smb2_lock_element
  209. * regardless of number of locks. Subtract single
  210. * smb2_lock_element for correct buffer size check.
  211. */
  212. if (hdr->Command == SMB2_LOCK)
  213. *len -= sizeof(struct smb2_lock_element);
  214. if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false)
  215. goto calc_size_exit;
  216. ret = smb2_get_data_area_len(&offset, &data_length, hdr);
  217. if (ret)
  218. return ret;
  219. ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length,
  220. offset);
  221. if (data_length > 0) {
  222. /*
  223. * Check to make sure that data area begins after fixed area,
  224. * Note that last byte of the fixed area is part of data area
  225. * for some commands, typically those with odd StructureSize,
  226. * so we must add one to the calculation.
  227. */
  228. if (offset + 1 < *len) {
  229. ksmbd_debug(SMB,
  230. "data area offset %d overlaps SMB2 header %u\n",
  231. offset + 1, *len);
  232. return -EINVAL;
  233. }
  234. *len = offset + data_length;
  235. }
  236. calc_size_exit:
  237. ksmbd_debug(SMB, "SMB2 len %u\n", *len);
  238. return 0;
  239. }
  240. static inline int smb2_query_info_req_len(struct smb2_query_info_req *h)
  241. {
  242. return le32_to_cpu(h->InputBufferLength) +
  243. le32_to_cpu(h->OutputBufferLength);
  244. }
  245. static inline int smb2_set_info_req_len(struct smb2_set_info_req *h)
  246. {
  247. return le32_to_cpu(h->BufferLength);
  248. }
  249. static inline int smb2_read_req_len(struct smb2_read_req *h)
  250. {
  251. return le32_to_cpu(h->Length);
  252. }
  253. static inline int smb2_write_req_len(struct smb2_write_req *h)
  254. {
  255. return le32_to_cpu(h->Length);
  256. }
  257. static inline int smb2_query_dir_req_len(struct smb2_query_directory_req *h)
  258. {
  259. return le32_to_cpu(h->OutputBufferLength);
  260. }
  261. static inline int smb2_ioctl_req_len(struct smb2_ioctl_req *h)
  262. {
  263. return le32_to_cpu(h->InputCount) +
  264. le32_to_cpu(h->OutputCount);
  265. }
  266. static inline int smb2_ioctl_resp_len(struct smb2_ioctl_req *h)
  267. {
  268. return le32_to_cpu(h->MaxInputResponse) +
  269. le32_to_cpu(h->MaxOutputResponse);
  270. }
  271. static int smb2_validate_credit_charge(struct ksmbd_conn *conn,
  272. struct smb2_hdr *hdr)
  273. {
  274. unsigned int req_len = 0, expect_resp_len = 0, calc_credit_num, max_len;
  275. unsigned short credit_charge = le16_to_cpu(hdr->CreditCharge);
  276. void *__hdr = hdr;
  277. int ret = 0;
  278. switch (hdr->Command) {
  279. case SMB2_QUERY_INFO:
  280. req_len = smb2_query_info_req_len(__hdr);
  281. break;
  282. case SMB2_SET_INFO:
  283. req_len = smb2_set_info_req_len(__hdr);
  284. break;
  285. case SMB2_READ:
  286. req_len = smb2_read_req_len(__hdr);
  287. break;
  288. case SMB2_WRITE:
  289. req_len = smb2_write_req_len(__hdr);
  290. break;
  291. case SMB2_QUERY_DIRECTORY:
  292. req_len = smb2_query_dir_req_len(__hdr);
  293. break;
  294. case SMB2_IOCTL:
  295. req_len = smb2_ioctl_req_len(__hdr);
  296. expect_resp_len = smb2_ioctl_resp_len(__hdr);
  297. break;
  298. case SMB2_CANCEL:
  299. return 0;
  300. default:
  301. req_len = 1;
  302. break;
  303. }
  304. credit_charge = max_t(unsigned short, credit_charge, 1);
  305. max_len = max_t(unsigned int, req_len, expect_resp_len);
  306. calc_credit_num = DIV_ROUND_UP(max_len, SMB2_MAX_BUFFER_SIZE);
  307. if (credit_charge < calc_credit_num) {
  308. ksmbd_debug(SMB, "Insufficient credit charge, given: %d, needed: %d\n",
  309. credit_charge, calc_credit_num);
  310. return 1;
  311. } else if (credit_charge > conn->vals->max_credits) {
  312. ksmbd_debug(SMB, "Too large credit charge: %d\n", credit_charge);
  313. return 1;
  314. }
  315. spin_lock(&conn->credits_lock);
  316. if (credit_charge > conn->total_credits) {
  317. ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n",
  318. credit_charge, conn->total_credits);
  319. ret = 1;
  320. }
  321. if ((u64)conn->outstanding_credits + credit_charge > conn->total_credits) {
  322. ksmbd_debug(SMB, "Limits exceeding the maximum allowable outstanding requests, given : %u, pending : %u\n",
  323. credit_charge, conn->outstanding_credits);
  324. ret = 1;
  325. } else
  326. conn->outstanding_credits += credit_charge;
  327. spin_unlock(&conn->credits_lock);
  328. return ret;
  329. }
  330. int ksmbd_smb2_check_message(struct ksmbd_work *work)
  331. {
  332. struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
  333. struct smb2_hdr *hdr = &pdu->hdr;
  334. int command;
  335. __u32 clc_len; /* calculated length */
  336. __u32 len = get_rfc1002_len(work->request_buf);
  337. __u32 req_struct_size, next_cmd = le32_to_cpu(hdr->NextCommand);
  338. if ((u64)work->next_smb2_rcv_hdr_off + next_cmd > len) {
  339. pr_err("next command(%u) offset exceeds smb msg size\n",
  340. next_cmd);
  341. return 1;
  342. }
  343. if (next_cmd > 0)
  344. len = next_cmd;
  345. else if (work->next_smb2_rcv_hdr_off)
  346. len -= work->next_smb2_rcv_hdr_off;
  347. if (check_smb2_hdr(hdr))
  348. return 1;
  349. if (hdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) {
  350. ksmbd_debug(SMB, "Illegal structure size %u\n",
  351. le16_to_cpu(hdr->StructureSize));
  352. return 1;
  353. }
  354. command = le16_to_cpu(hdr->Command);
  355. if (command >= NUMBER_OF_SMB2_COMMANDS) {
  356. ksmbd_debug(SMB, "Illegal SMB2 command %d\n", command);
  357. return 1;
  358. }
  359. if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {
  360. if (!(command == SMB2_OPLOCK_BREAK_HE &&
  361. (le16_to_cpu(pdu->StructureSize2) == OP_BREAK_STRUCT_SIZE_20 ||
  362. le16_to_cpu(pdu->StructureSize2) == OP_BREAK_STRUCT_SIZE_21))) {
  363. /* special case for SMB2.1 lease break message */
  364. ksmbd_debug(SMB,
  365. "Illegal request size %u for command %d\n",
  366. le16_to_cpu(pdu->StructureSize2), command);
  367. return 1;
  368. }
  369. }
  370. req_struct_size = le16_to_cpu(pdu->StructureSize2) +
  371. __SMB2_HEADER_STRUCTURE_SIZE;
  372. if (command == SMB2_LOCK_HE)
  373. req_struct_size -= sizeof(struct smb2_lock_element);
  374. if (req_struct_size > len + 1)
  375. return 1;
  376. if (smb2_calc_size(hdr, &clc_len))
  377. return 1;
  378. if (len != clc_len) {
  379. /* client can return one byte more due to implied bcc[0] */
  380. if (clc_len == len + 1)
  381. goto validate_credit;
  382. /*
  383. * Some windows servers (win2016) will pad also the final
  384. * PDU in a compound to 8 bytes.
  385. */
  386. if (ALIGN(clc_len, 8) == len)
  387. goto validate_credit;
  388. /*
  389. * SMB2 NEGOTIATE request will be validated when message
  390. * handling proceeds.
  391. */
  392. if (command == SMB2_NEGOTIATE_HE)
  393. goto validate_credit;
  394. /*
  395. * Allow a message that padded to 8byte boundary.
  396. * Linux 4.19.217 with smb 3.0.2 are sometimes
  397. * sending messages where the cls_len is exactly
  398. * 8 bytes less than len.
  399. */
  400. if (clc_len < len && (len - clc_len) <= 8)
  401. goto validate_credit;
  402. pr_err_ratelimited(
  403. "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
  404. len, clc_len, command,
  405. le64_to_cpu(hdr->MessageId));
  406. return 1;
  407. }
  408. validate_credit:
  409. if ((work->conn->vals->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) &&
  410. smb2_validate_credit_charge(work->conn, hdr))
  411. return 1;
  412. return 0;
  413. }
  414. int smb2_negotiate_request(struct ksmbd_work *work)
  415. {
  416. return ksmbd_smb_negotiate_common(work, SMB2_NEGOTIATE_HE);
  417. }