sendme.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /* Copyright (c) 2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file sendme.c
  5. * \brief Code that is related to SENDME cells both in terms of
  6. * creating/parsing cells and handling the content.
  7. */
  8. #define SENDME_PRIVATE
  9. #include "core/or/or.h"
  10. #include "app/config/config.h"
  11. #include "core/mainloop/connection.h"
  12. #include "core/or/cell_st.h"
  13. #include "core/or/circuitlist.h"
  14. #include "core/or/circuituse.h"
  15. #include "core/or/or_circuit_st.h"
  16. #include "core/or/relay.h"
  17. #include "core/or/sendme.h"
  18. #include "feature/nodelist/networkstatus.h"
  19. #include "lib/ctime/di_ops.h"
  20. #include "trunnel/sendme.h"
  21. /* The maximum supported version. Above that value, the cell can't be
  22. * recognized as a valid SENDME. */
  23. #define SENDME_MAX_SUPPORTED_VERSION 1
  24. /* The cell version constants for when emitting a cell. */
  25. #define SENDME_EMIT_MIN_VERSION_DEFAULT 0
  26. #define SENDME_EMIT_MIN_VERSION_MIN 0
  27. #define SENDME_EMIT_MIN_VERSION_MAX UINT8_MAX
  28. /* The cell version constants for when accepting a cell. */
  29. #define SENDME_ACCEPT_MIN_VERSION_DEFAULT 0
  30. #define SENDME_ACCEPT_MIN_VERSION_MIN 0
  31. #define SENDME_ACCEPT_MIN_VERSION_MAX UINT8_MAX
  32. /* Return the minimum version given by the consensus (if any) that should be
  33. * used when emitting a SENDME cell. */
  34. STATIC int
  35. get_emit_min_version(void)
  36. {
  37. return networkstatus_get_param(NULL, "sendme_emit_min_version",
  38. SENDME_EMIT_MIN_VERSION_DEFAULT,
  39. SENDME_EMIT_MIN_VERSION_MIN,
  40. SENDME_EMIT_MIN_VERSION_MAX);
  41. }
  42. /* Return the minimum version given by the consensus (if any) that should be
  43. * accepted when receiving a SENDME cell. */
  44. STATIC int
  45. get_accept_min_version(void)
  46. {
  47. return networkstatus_get_param(NULL, "sendme_accept_min_version",
  48. SENDME_ACCEPT_MIN_VERSION_DEFAULT,
  49. SENDME_ACCEPT_MIN_VERSION_MIN,
  50. SENDME_ACCEPT_MIN_VERSION_MAX);
  51. }
  52. /* Return true iff the given cell digest matches the first digest in the
  53. * circuit sendme list. */
  54. static bool
  55. v1_digest_matches(const circuit_t *circ, const uint8_t *cell_digest)
  56. {
  57. bool ret = false;
  58. uint8_t *circ_digest = NULL;
  59. tor_assert(circ);
  60. tor_assert(cell_digest);
  61. /* We shouldn't have received a SENDME if we have no digests. Log at
  62. * protocol warning because it can be tricked by sending many SENDMEs
  63. * without prior data cell. */
  64. if (circ->sendme_last_digests == NULL ||
  65. smartlist_len(circ->sendme_last_digests) == 0) {
  66. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  67. "We received a SENDME but we have no cell digests to match. "
  68. "Closing circuit.");
  69. goto no_match;
  70. }
  71. /* Pop the first element that was added (FIFO) and compare it. */
  72. circ_digest = smartlist_get(circ->sendme_last_digests, 0);
  73. smartlist_del_keeporder(circ->sendme_last_digests, 0);
  74. /* Compare the digest with the one in the SENDME. This cell is invalid
  75. * without a perfect match. */
  76. if (tor_memneq(circ_digest, cell_digest, TRUNNEL_SENDME_V1_DIGEST_LEN)) {
  77. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  78. "SENDME v1 cell digest do not match.");
  79. goto no_match;
  80. }
  81. /* Digests matches! */
  82. ret = true;
  83. no_match:
  84. /* This digest was popped from the circuit list. Regardless of what happens,
  85. * we have no more use for it. */
  86. tor_free(circ_digest);
  87. return ret;
  88. }
  89. /* Return true iff the given decoded SENDME version 1 cell is valid and
  90. * matches the expected digest on the circuit.
  91. *
  92. * Validation is done by comparing the digest in the cell from the previous
  93. * cell we saw which tells us that the other side has in fact seen that cell.
  94. * See proposal 289 for more details. */
  95. static bool
  96. cell_v1_is_valid(const sendme_cell_t *cell, const circuit_t *circ)
  97. {
  98. tor_assert(cell);
  99. tor_assert(circ);
  100. const uint8_t *cell_digest = sendme_cell_getconstarray_data_v1_digest(cell);
  101. return v1_digest_matches(circ, cell_digest);
  102. }
  103. /* Return true iff the given cell version can be handled or if the minimum
  104. * accepted version from the consensus is known to us. */
  105. STATIC bool
  106. cell_version_is_valid(uint8_t cell_version)
  107. {
  108. int accept_version = get_accept_min_version();
  109. /* Can we handle this version? */
  110. if (accept_version > SENDME_MAX_SUPPORTED_VERSION) {
  111. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  112. "Unable to handle SENDME version %u. We only support <= %d "
  113. "(from consensus). Probably your tor is too old?",
  114. accept_version, cell_version);
  115. goto invalid;
  116. }
  117. /* We only accept a SENDME cell from what the consensus tells us. */
  118. if (cell_version < accept_version) {
  119. log_info(LD_PROTOCOL, "Unacceptable SENDME version %d. Only "
  120. "accepting %u (taken from the consensus). "
  121. "Closing circuit.",
  122. cell_version, accept_version);
  123. goto invalid;
  124. }
  125. return 1;
  126. invalid:
  127. return 0;
  128. }
  129. /* Return true iff the encoded SENDME cell in cell_payload of length
  130. * cell_payload_len is valid. For each version:
  131. *
  132. * 0: No validation
  133. * 1: Authenticated with last cell digest.
  134. *
  135. * This is the main critical function to make sure we can continue to
  136. * send/recv cells on a circuit. If the SENDME is invalid, the circuit should
  137. * be mark for close. */
  138. STATIC bool
  139. sendme_is_valid(const circuit_t *circ, const uint8_t *cell_payload,
  140. size_t cell_payload_len)
  141. {
  142. uint8_t cell_version;
  143. sendme_cell_t *cell = NULL;
  144. tor_assert(circ);
  145. tor_assert(cell_payload);
  146. /* An empty payload means version 0 so skip trunnel parsing. We won't be
  147. * able to parse a 0 length buffer into a valid SENDME cell. */
  148. if (cell_payload_len == 0) {
  149. cell_version = 0;
  150. } else {
  151. /* First we'll decode the cell so we can get the version. */
  152. if (sendme_cell_parse(&cell, cell_payload, cell_payload_len) < 0) {
  153. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  154. "Unparseable SENDME cell received. Closing circuit.");
  155. goto invalid;
  156. }
  157. cell_version = sendme_cell_get_version(cell);
  158. }
  159. /* Validate that we can handle this cell version. */
  160. if (!cell_version_is_valid(cell_version)) {
  161. goto invalid;
  162. }
  163. /* Validate depending on the version now. */
  164. switch (cell_version) {
  165. case 0x01:
  166. if (!cell_v1_is_valid(cell, circ)) {
  167. goto invalid;
  168. }
  169. break;
  170. case 0x00:
  171. /* Fallthrough. Version 0, there is no work to be done on the payload so
  172. * it is necessarily valid if we pass the version validation. */
  173. default:
  174. /* Unknown version means we can't handle it so fallback to version 0. */
  175. break;
  176. }
  177. /* Valid cell. */
  178. sendme_cell_free(cell);
  179. return 1;
  180. invalid:
  181. sendme_cell_free(cell);
  182. return 0;
  183. }
  184. /* Build and encode a version 1 SENDME cell into payload, which must be at
  185. * least of RELAY_PAYLOAD_SIZE bytes, using the digest for the cell data.
  186. *
  187. * Return the size in bytes of the encoded cell in payload. A negative value
  188. * is returned on encoding failure. */
  189. STATIC ssize_t
  190. build_cell_payload_v1(crypto_digest_t *cell_digest, uint8_t *payload)
  191. {
  192. ssize_t len = -1;
  193. sendme_cell_t *cell = NULL;
  194. tor_assert(cell_digest);
  195. tor_assert(payload);
  196. cell = sendme_cell_new();
  197. /* Building a payload for version 1. */
  198. sendme_cell_set_version(cell, 0x01);
  199. /* Set the data length field for v1. */
  200. sendme_cell_set_data_len(cell, TRUNNEL_SENDME_V1_DIGEST_LEN);
  201. /* Copy the digest into the data payload. */
  202. crypto_digest_get_digest(cell_digest,
  203. (char *) sendme_cell_getarray_data_v1_digest(cell),
  204. sendme_cell_get_data_len(cell));
  205. /* Finally, encode the cell into the payload. */
  206. len = sendme_cell_encode(payload, RELAY_PAYLOAD_SIZE, cell);
  207. sendme_cell_free(cell);
  208. return len;
  209. }
  210. /* Send a circuit-level SENDME on the given circuit using the layer_hint if
  211. * not NULL. The digest is only used for version 1.
  212. *
  213. * Return 0 on success else a negative value and the circuit will be closed
  214. * because we failed to send the cell on it. */
  215. static int
  216. send_circuit_level_sendme(circuit_t *circ, crypt_path_t *layer_hint,
  217. crypto_digest_t *cell_digest)
  218. {
  219. uint8_t emit_version;
  220. uint8_t payload[RELAY_PAYLOAD_SIZE];
  221. ssize_t payload_len;
  222. tor_assert(circ);
  223. tor_assert(cell_digest);
  224. emit_version = get_emit_min_version();
  225. switch (emit_version) {
  226. case 0x01:
  227. payload_len = build_cell_payload_v1(cell_digest, payload);
  228. if (BUG(payload_len < 0)) {
  229. /* Unable to encode the cell, abort. We can recover from this by closing
  230. * the circuit but in theory it should never happen. */
  231. return -1;
  232. }
  233. log_debug(LD_PROTOCOL, "Emitting SENDME version 1 cell.");
  234. break;
  235. case 0x00:
  236. /* Fallthrough because default is to use v0. */
  237. default:
  238. /* Unknown version, fallback to version 0 meaning no payload. */
  239. payload_len = 0;
  240. break;
  241. }
  242. if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
  243. (char *) payload, payload_len,
  244. layer_hint) < 0) {
  245. log_warn(LD_CIRC,
  246. "SENDME relay_send_command_from_edge failed. Circuit's closed.");
  247. return -1; /* the circuit's closed, don't continue */
  248. }
  249. return 0;
  250. }
  251. /** Called when we've just received a relay data cell, when we've just
  252. * finished flushing all bytes to stream <b>conn</b>, or when we've flushed
  253. * *some* bytes to the stream <b>conn</b>.
  254. *
  255. * If conn->outbuf is not too full, and our deliver window is low, send back a
  256. * suitable number of stream-level sendme cells.
  257. */
  258. void
  259. sendme_connection_edge_consider_sending(edge_connection_t *conn)
  260. {
  261. tor_assert(conn);
  262. int log_domain = TO_CONN(conn)->type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
  263. /* Don't send it if we still have data to deliver. */
  264. if (connection_outbuf_too_full(TO_CONN(conn))) {
  265. goto end;
  266. }
  267. if (circuit_get_by_edge_conn(conn) == NULL) {
  268. /* This can legitimately happen if the destroy has already arrived and
  269. * torn down the circuit. */
  270. log_info(log_domain, "No circuit associated with edge connection. "
  271. "Skipping sending SENDME.");
  272. goto end;
  273. }
  274. while (conn->deliver_window <=
  275. (STREAMWINDOW_START - STREAMWINDOW_INCREMENT)) {
  276. log_debug(log_domain, "Outbuf %" TOR_PRIuSZ ", queuing stream SENDME.",
  277. TO_CONN(conn)->outbuf_flushlen);
  278. conn->deliver_window += STREAMWINDOW_INCREMENT;
  279. if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
  280. NULL, 0) < 0) {
  281. log_warn(LD_BUG, "connection_edge_send_command failed while sending "
  282. "a SENDME. Circuit probably closed, skipping.");
  283. goto end; /* The circuit's closed, don't continue */
  284. }
  285. }
  286. end:
  287. return;
  288. }
  289. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  290. * <b>layer_hint</b> if it's defined) is low enough that we should
  291. * send a circuit-level sendme back down the circuit. If so, send
  292. * enough sendmes that the window would be overfull if we sent any
  293. * more.
  294. */
  295. void
  296. sendme_circuit_consider_sending(circuit_t *circ, crypt_path_t *layer_hint)
  297. {
  298. crypto_digest_t *digest;
  299. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <=
  300. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  301. log_debug(LD_CIRC,"Queuing circuit sendme.");
  302. if (layer_hint) {
  303. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  304. digest = layer_hint->crypto.sendme_digest;
  305. } else {
  306. circ->deliver_window += CIRCWINDOW_INCREMENT;
  307. digest = TO_OR_CIRCUIT(circ)->crypto.sendme_digest;
  308. }
  309. if (send_circuit_level_sendme(circ, layer_hint, digest) < 0) {
  310. return; /* The circuit's closed, don't continue */
  311. }
  312. }
  313. }
  314. /* Process a circuit-level SENDME cell that we just received. The layer_hint,
  315. * if not NULL, is the Exit hop of the connection which means that we are a
  316. * client. In that case, circ must be an origin circuit. The cell_body_len is
  317. * the length of the SENDME cell payload (excluding the header). The
  318. * cell_payload is the payload.
  319. *
  320. * Return 0 on success that is the SENDME is valid and the package window has
  321. * been updated properly.
  322. *
  323. * On error, a negative value is returned which indicate that the circuit must
  324. * be closed using the value as the reason for it. */
  325. int
  326. sendme_process_circuit_level(crypt_path_t *layer_hint,
  327. circuit_t *circ, const uint8_t *cell_payload,
  328. uint16_t cell_payload_len)
  329. {
  330. tor_assert(circ);
  331. tor_assert(cell_payload);
  332. /* If we are the origin of the circuit, we are the Client so we use the
  333. * layer hint (the Exit hop) for the package window tracking. */
  334. if (CIRCUIT_IS_ORIGIN(circ)) {
  335. if ((layer_hint->package_window + CIRCWINDOW_INCREMENT) >
  336. CIRCWINDOW_START_MAX) {
  337. static struct ratelim_t exit_warn_ratelim = RATELIM_INIT(600);
  338. log_fn_ratelim(&exit_warn_ratelim, LOG_WARN, LD_PROTOCOL,
  339. "Unexpected sendme cell from exit relay. "
  340. "Closing circ.");
  341. return -END_CIRC_REASON_TORPROTOCOL;
  342. }
  343. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  344. log_debug(LD_APP, "circ-level sendme at origin, packagewindow %d.",
  345. layer_hint->package_window);
  346. /* We count circuit-level sendme's as valid delivered data because they
  347. * are rate limited. */
  348. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), cell_payload_len);
  349. } else {
  350. /* Validate the SENDME cell. Depending on the version, different
  351. * validation can be done. An invalid SENDME requires us to close the
  352. * circuit. It is only done if we are the Exit of the circuit. */
  353. if (!sendme_is_valid(circ, cell_payload, cell_payload_len)) {
  354. return -END_CIRC_REASON_TORPROTOCOL;
  355. }
  356. /* We aren't the origin of this circuit so we are the Exit and thus we
  357. * track the package window with the circuit object. */
  358. if ((circ->package_window + CIRCWINDOW_INCREMENT) >
  359. CIRCWINDOW_START_MAX) {
  360. static struct ratelim_t client_warn_ratelim = RATELIM_INIT(600);
  361. log_fn_ratelim(&client_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  362. "Unexpected sendme cell from client. "
  363. "Closing circ (window %d).", circ->package_window);
  364. return -END_CIRC_REASON_TORPROTOCOL;
  365. }
  366. circ->package_window += CIRCWINDOW_INCREMENT;
  367. log_debug(LD_EXIT, "circ-level sendme at non-origin, packagewindow %d.",
  368. circ->package_window);
  369. }
  370. return 0;
  371. }
  372. /* Process a stream-level SENDME cell that we just received. The conn is the
  373. * edge connection (stream) that the circuit circ is associated with. The
  374. * cell_body_len is the length of the payload (excluding the header).
  375. *
  376. * Return 0 on success that is the SENDME is valid and the package window has
  377. * been updated properly.
  378. *
  379. * On error, a negative value is returned which indicate that the circuit must
  380. * be closed using the value as the reason for it. */
  381. int
  382. sendme_process_stream_level(edge_connection_t *conn, circuit_t *circ,
  383. uint16_t cell_body_len)
  384. {
  385. tor_assert(conn);
  386. tor_assert(circ);
  387. /* Don't allow the other endpoint to request more than our maximum (i.e.
  388. * initial) stream SENDME window worth of data. Well-behaved stock clients
  389. * will not request more than this max (as per the check in the while loop
  390. * of sendme_connection_edge_consider_sending()). */
  391. if ((conn->package_window + STREAMWINDOW_INCREMENT) >
  392. STREAMWINDOW_START_MAX) {
  393. static struct ratelim_t stream_warn_ratelim = RATELIM_INIT(600);
  394. log_fn_ratelim(&stream_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  395. "Unexpected stream sendme cell. Closing circ (window %d).",
  396. conn->package_window);
  397. return -END_CIRC_REASON_TORPROTOCOL;
  398. }
  399. /* At this point, the stream sendme is valid */
  400. conn->package_window += STREAMWINDOW_INCREMENT;
  401. /* We count circuit-level sendme's as valid delivered data because they are
  402. * rate limited. */
  403. if (CIRCUIT_IS_ORIGIN(circ)) {
  404. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), cell_body_len);
  405. }
  406. log_debug(CIRCUIT_IS_ORIGIN(circ) ? LD_APP : LD_EXIT,
  407. "stream-level sendme, package_window now %d.",
  408. conn->package_window);
  409. return 0;
  410. }
  411. /* Called when a relay DATA cell is received on the given circuit. If
  412. * layer_hint is NULL, this means we are the Exit end point else we are the
  413. * Client. Update the deliver window and return its new value. */
  414. int
  415. sendme_circuit_data_received(circuit_t *circ, crypt_path_t *layer_hint)
  416. {
  417. int deliver_window, domain;
  418. if (CIRCUIT_IS_ORIGIN(circ)) {
  419. tor_assert(layer_hint);
  420. --layer_hint->deliver_window;
  421. deliver_window = layer_hint->deliver_window;
  422. domain = LD_APP;
  423. } else {
  424. tor_assert(!layer_hint);
  425. --circ->deliver_window;
  426. deliver_window = circ->deliver_window;
  427. domain = LD_EXIT;
  428. }
  429. log_debug(domain, "Circuit deliver_window now %d.", deliver_window);
  430. return deliver_window;
  431. }
  432. /* Called when a relay DATA cell is received for the given edge connection
  433. * conn. Update the deliver window and return its new value. */
  434. int
  435. sendme_stream_data_received(edge_connection_t *conn)
  436. {
  437. tor_assert(conn);
  438. return --conn->deliver_window;
  439. }
  440. /* Called when a relay DATA cell is packaged on the given circuit. If
  441. * layer_hint is NULL, this means we are the Exit end point else we are the
  442. * Client. Update the package window and return its new value. */
  443. int
  444. sendme_note_circuit_data_packaged(circuit_t *circ, crypt_path_t *layer_hint)
  445. {
  446. int package_window, domain;
  447. tor_assert(circ);
  448. if (CIRCUIT_IS_ORIGIN(circ)) {
  449. /* Client side. */
  450. tor_assert(layer_hint);
  451. --layer_hint->package_window;
  452. package_window = layer_hint->package_window;
  453. domain = LD_APP;
  454. } else {
  455. /* Exit side. */
  456. tor_assert(!layer_hint);
  457. --circ->package_window;
  458. package_window = circ->package_window;
  459. domain = LD_EXIT;
  460. }
  461. log_debug(domain, "Circuit package_window now %d.", package_window);
  462. return package_window;
  463. }
  464. /* Called when a relay DATA cell is packaged for the given edge connection
  465. * conn. Update the package window and return its new value. */
  466. int
  467. sendme_note_stream_data_packaged(edge_connection_t *conn)
  468. {
  469. tor_assert(conn);
  470. return --conn->package_window;
  471. }
  472. /* Note the cell digest in the circuit sendme last digests FIFO if applicable.
  473. * It is safe to pass a circuit that isn't meant to track those digests. */
  474. void
  475. sendme_note_cell_digest(circuit_t *circ)
  476. {
  477. uint8_t *digest;
  478. tor_assert(circ);
  479. /* We only keep the cell digest if we are the Exit on that circuit and if
  480. * this cell is the last one before the client should send a SENDME. */
  481. if (CIRCUIT_IS_ORIGIN(circ)) {
  482. return;
  483. }
  484. /* Is this the last cell before a SENDME? The idea is that if the
  485. * package_window reaches a multiple of the increment, after this cell, we
  486. * should expect a SENDME. */
  487. if (((circ->package_window - 1) % CIRCWINDOW_INCREMENT) != 0) {
  488. return;
  489. }
  490. /* Only note the digest if we actually have the digest of the previous cell
  491. * recorded. It should never happen in theory as we always record the last
  492. * digest for the v1 SENDME. */
  493. if (TO_OR_CIRCUIT(circ)->crypto.sendme_digest) {
  494. digest = tor_malloc_zero(TRUNNEL_SENDME_V1_DIGEST_LEN);
  495. crypto_digest_get_digest(TO_OR_CIRCUIT(circ)->crypto.sendme_digest,
  496. (char *) digest, TRUNNEL_SENDME_V1_DIGEST_LEN);
  497. if (circ->sendme_last_digests == NULL) {
  498. circ->sendme_last_digests = smartlist_new();
  499. }
  500. smartlist_add(circ->sendme_last_digests, digest);
  501. }
  502. }