circpathbias.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circpathbias.c
  8. *
  9. * \brief Code to track success/failure rates of circuits built through
  10. * different tor nodes, in an attempt to detect attacks where
  11. * an attacker deliberately causes circuits to fail until the client
  12. * choses a path they like.
  13. *
  14. * This code is currently configured in a warning-only mode, though false
  15. * positives appear to be rare in practice. There is also support for
  16. * disabling really bad guards, but it's quite experimental and may have bad
  17. * anonymity effects.
  18. *
  19. * The information here is associated with the entry_guard_t object for
  20. * each guard, and stored persistently in the state file.
  21. */
  22. #include "core/or/or.h"
  23. #include "core/or/channel.h"
  24. #include "feature/client/circpathbias.h"
  25. #include "core/or/circuitbuild.h"
  26. #include "core/or/circuitlist.h"
  27. #include "core/or/circuituse.h"
  28. #include "core/or/circuitstats.h"
  29. #include "core/or/connection_edge.h"
  30. #include "app/config/config.h"
  31. #include "lib/crypt_ops/crypto_rand.h"
  32. #include "feature/client/entrynodes.h"
  33. #include "feature/nodelist/networkstatus.h"
  34. #include "core/or/relay.h"
  35. #include "lib/math/fp.h"
  36. #include "lib/math/laplace.h"
  37. #include "core/or/cell_st.h"
  38. #include "core/or/cpath_build_state_st.h"
  39. #include "core/or/crypt_path_st.h"
  40. #include "core/or/extend_info_st.h"
  41. #include "core/or/origin_circuit_st.h"
  42. static void pathbias_count_successful_close(origin_circuit_t *circ);
  43. static void pathbias_count_collapse(origin_circuit_t *circ);
  44. static void pathbias_count_use_failed(origin_circuit_t *circ);
  45. static void pathbias_measure_use_rate(entry_guard_t *guard);
  46. static void pathbias_measure_close_rate(entry_guard_t *guard);
  47. static void pathbias_scale_use_rates(entry_guard_t *guard);
  48. static void pathbias_scale_close_rates(entry_guard_t *guard);
  49. static int entry_guard_inc_circ_attempt_count(entry_guard_t *guard);
  50. /** Increment the number of times we successfully extended a circuit to
  51. * <b>guard</b>, first checking if the failure rate is high enough that
  52. * we should eliminate the guard. Return -1 if the guard looks no good;
  53. * return 0 if the guard looks fine.
  54. */
  55. static int
  56. entry_guard_inc_circ_attempt_count(entry_guard_t *guard)
  57. {
  58. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  59. entry_guards_changed();
  60. pathbias_measure_close_rate(guard);
  61. if (pb->path_bias_disabled)
  62. return -1;
  63. pathbias_scale_close_rates(guard);
  64. pb->circ_attempts++;
  65. log_info(LD_CIRC, "Got success count %f/%f for guard %s",
  66. pb->circ_successes, pb->circ_attempts,
  67. entry_guard_describe(guard));
  68. return 0;
  69. }
  70. /** The minimum number of circuit attempts before we start
  71. * thinking about warning about path bias and dropping guards */
  72. static int
  73. pathbias_get_min_circs(const or_options_t *options)
  74. {
  75. #define DFLT_PATH_BIAS_MIN_CIRC 150
  76. if (options->PathBiasCircThreshold >= 5)
  77. return options->PathBiasCircThreshold;
  78. else
  79. return networkstatus_get_param(NULL, "pb_mincircs",
  80. DFLT_PATH_BIAS_MIN_CIRC,
  81. 5, INT32_MAX);
  82. }
  83. /** The circuit success rate below which we issue a notice */
  84. static double
  85. pathbias_get_notice_rate(const or_options_t *options)
  86. {
  87. #define DFLT_PATH_BIAS_NOTICE_PCT 70
  88. if (options->PathBiasNoticeRate >= 0.0)
  89. return options->PathBiasNoticeRate;
  90. else
  91. return networkstatus_get_param(NULL, "pb_noticepct",
  92. DFLT_PATH_BIAS_NOTICE_PCT, 0, 100)/100.0;
  93. }
  94. /** The circuit success rate below which we issue a warn */
  95. static double
  96. pathbias_get_warn_rate(const or_options_t *options)
  97. {
  98. #define DFLT_PATH_BIAS_WARN_PCT 50
  99. if (options->PathBiasWarnRate >= 0.0)
  100. return options->PathBiasWarnRate;
  101. else
  102. return networkstatus_get_param(NULL, "pb_warnpct",
  103. DFLT_PATH_BIAS_WARN_PCT, 0, 100)/100.0;
  104. }
  105. /* XXXX I'd like to have this be static again, but entrynodes.c needs it. */
  106. /**
  107. * The extreme rate is the rate at which we would drop the guard,
  108. * if pb_dropguard is also set. Otherwise we just warn.
  109. */
  110. double
  111. pathbias_get_extreme_rate(const or_options_t *options)
  112. {
  113. #define DFLT_PATH_BIAS_EXTREME_PCT 30
  114. if (options->PathBiasExtremeRate >= 0.0)
  115. return options->PathBiasExtremeRate;
  116. else
  117. return networkstatus_get_param(NULL, "pb_extremepct",
  118. DFLT_PATH_BIAS_EXTREME_PCT, 0, 100)/100.0;
  119. }
  120. /* XXXX I'd like to have this be static again, but entrynodes.c needs it. */
  121. /**
  122. * If 1, we actually disable use of guards that fall below
  123. * the extreme_pct.
  124. */
  125. int
  126. pathbias_get_dropguards(const or_options_t *options)
  127. {
  128. #define DFLT_PATH_BIAS_DROP_GUARDS 0
  129. if (options->PathBiasDropGuards >= 0)
  130. return options->PathBiasDropGuards;
  131. else
  132. return networkstatus_get_param(NULL, "pb_dropguards",
  133. DFLT_PATH_BIAS_DROP_GUARDS, 0, 1);
  134. }
  135. /**
  136. * This is the number of circuits at which we scale our
  137. * counts by mult_factor/scale_factor. Note, this count is
  138. * not exact, as we only perform the scaling in the event
  139. * of no integer truncation.
  140. */
  141. static int
  142. pathbias_get_scale_threshold(const or_options_t *options)
  143. {
  144. #define DFLT_PATH_BIAS_SCALE_THRESHOLD 300
  145. if (options->PathBiasScaleThreshold >= 10)
  146. return options->PathBiasScaleThreshold;
  147. else
  148. return networkstatus_get_param(NULL, "pb_scalecircs",
  149. DFLT_PATH_BIAS_SCALE_THRESHOLD, 10,
  150. INT32_MAX);
  151. }
  152. /**
  153. * Compute the path bias scaling ratio from the consensus
  154. * parameters pb_multfactor/pb_scalefactor.
  155. *
  156. * Returns a value in (0, 1.0] which we multiply our pathbias
  157. * counts with to scale them down.
  158. */
  159. static double
  160. pathbias_get_scale_ratio(const or_options_t *options)
  161. {
  162. (void) options;
  163. /*
  164. * The scale factor is the denominator for our scaling
  165. * of circuit counts for our path bias window.
  166. *
  167. * Note that our use of doubles for the path bias state
  168. * file means that powers of 2 work best here.
  169. */
  170. int denominator = networkstatus_get_param(NULL, "pb_scalefactor",
  171. 2, 2, INT32_MAX);
  172. tor_assert(denominator > 0);
  173. /**
  174. * The mult factor is the numerator for our scaling
  175. * of circuit counts for our path bias window. It
  176. * allows us to scale by fractions.
  177. */
  178. return networkstatus_get_param(NULL, "pb_multfactor",
  179. 1, 1, denominator)/((double)denominator);
  180. }
  181. /** The minimum number of circuit usage attempts before we start
  182. * thinking about warning about path use bias and dropping guards */
  183. static int
  184. pathbias_get_min_use(const or_options_t *options)
  185. {
  186. #define DFLT_PATH_BIAS_MIN_USE 20
  187. if (options->PathBiasUseThreshold >= 3)
  188. return options->PathBiasUseThreshold;
  189. else
  190. return networkstatus_get_param(NULL, "pb_minuse",
  191. DFLT_PATH_BIAS_MIN_USE,
  192. 3, INT32_MAX);
  193. }
  194. /** The circuit use success rate below which we issue a notice */
  195. static double
  196. pathbias_get_notice_use_rate(const or_options_t *options)
  197. {
  198. #define DFLT_PATH_BIAS_NOTICE_USE_PCT 80
  199. if (options->PathBiasNoticeUseRate >= 0.0)
  200. return options->PathBiasNoticeUseRate;
  201. else
  202. return networkstatus_get_param(NULL, "pb_noticeusepct",
  203. DFLT_PATH_BIAS_NOTICE_USE_PCT,
  204. 0, 100)/100.0;
  205. }
  206. /**
  207. * The extreme use rate is the rate at which we would drop the guard,
  208. * if pb_dropguard is also set. Otherwise we just warn.
  209. */
  210. double
  211. pathbias_get_extreme_use_rate(const or_options_t *options)
  212. {
  213. #define DFLT_PATH_BIAS_EXTREME_USE_PCT 60
  214. if (options->PathBiasExtremeUseRate >= 0.0)
  215. return options->PathBiasExtremeUseRate;
  216. else
  217. return networkstatus_get_param(NULL, "pb_extremeusepct",
  218. DFLT_PATH_BIAS_EXTREME_USE_PCT,
  219. 0, 100)/100.0;
  220. }
  221. /**
  222. * This is the number of circuits at which we scale our
  223. * use counts by mult_factor/scale_factor. Note, this count is
  224. * not exact, as we only perform the scaling in the event
  225. * of no integer truncation.
  226. */
  227. static int
  228. pathbias_get_scale_use_threshold(const or_options_t *options)
  229. {
  230. #define DFLT_PATH_BIAS_SCALE_USE_THRESHOLD 100
  231. if (options->PathBiasScaleUseThreshold >= 10)
  232. return options->PathBiasScaleUseThreshold;
  233. else
  234. return networkstatus_get_param(NULL, "pb_scaleuse",
  235. DFLT_PATH_BIAS_SCALE_USE_THRESHOLD,
  236. 10, INT32_MAX);
  237. }
  238. /**
  239. * Convert a Guard's path state to string.
  240. */
  241. const char *
  242. pathbias_state_to_string(path_state_t state)
  243. {
  244. switch (state) {
  245. case PATH_STATE_NEW_CIRC:
  246. return "new";
  247. case PATH_STATE_BUILD_ATTEMPTED:
  248. return "build attempted";
  249. case PATH_STATE_BUILD_SUCCEEDED:
  250. return "build succeeded";
  251. case PATH_STATE_USE_ATTEMPTED:
  252. return "use attempted";
  253. case PATH_STATE_USE_SUCCEEDED:
  254. return "use succeeded";
  255. case PATH_STATE_USE_FAILED:
  256. return "use failed";
  257. case PATH_STATE_ALREADY_COUNTED:
  258. return "already counted";
  259. }
  260. return "unknown";
  261. }
  262. /**
  263. * This function decides if a circuit has progressed far enough to count
  264. * as a circuit "attempt". As long as end-to-end tagging is possible,
  265. * we assume the adversary will use it over hop-to-hop failure. Therefore,
  266. * we only need to account bias for the last hop. This should make us
  267. * much more resilient to ambient circuit failure, and also make that
  268. * failure easier to measure (we only need to measure Exit failure rates).
  269. */
  270. static int
  271. pathbias_is_new_circ_attempt(origin_circuit_t *circ)
  272. {
  273. #define N2N_TAGGING_IS_POSSIBLE
  274. #ifdef N2N_TAGGING_IS_POSSIBLE
  275. /* cpath is a circular list. We want circs with more than one hop,
  276. * and the second hop must be waiting for keys still (it's just
  277. * about to get them). */
  278. return circ->cpath &&
  279. circ->cpath->next != circ->cpath &&
  280. circ->cpath->next->state == CPATH_STATE_AWAITING_KEYS;
  281. #else /* !(defined(N2N_TAGGING_IS_POSSIBLE)) */
  282. /* If tagging attacks are no longer possible, we probably want to
  283. * count bias from the first hop. However, one could argue that
  284. * timing-based tagging is still more useful than per-hop failure.
  285. * In which case, we'd never want to use this.
  286. */
  287. return circ->cpath &&
  288. circ->cpath->state == CPATH_STATE_AWAITING_KEYS;
  289. #endif /* defined(N2N_TAGGING_IS_POSSIBLE) */
  290. }
  291. /**
  292. * Decide if the path bias code should count a circuit.
  293. *
  294. * @returns 1 if we should count it, 0 otherwise.
  295. */
  296. static int
  297. pathbias_should_count(origin_circuit_t *circ)
  298. {
  299. #define PATHBIAS_COUNT_INTERVAL (600)
  300. static ratelim_t count_limit =
  301. RATELIM_INIT(PATHBIAS_COUNT_INTERVAL);
  302. char *rate_msg = NULL;
  303. /* We can't do path bias accounting without entry guards.
  304. * Testing and controller circuits also have no guards.
  305. *
  306. * We also don't count server-side rends, because their
  307. * endpoint could be chosen maliciously.
  308. * Similarly, we can't count client-side intro attempts,
  309. * because clients can be manipulated into connecting to
  310. * malicious intro points. */
  311. if (get_options()->UseEntryGuards == 0 ||
  312. circ->base_.purpose == CIRCUIT_PURPOSE_TESTING ||
  313. circ->base_.purpose == CIRCUIT_PURPOSE_CONTROLLER ||
  314. circ->base_.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
  315. circ->base_.purpose == CIRCUIT_PURPOSE_S_REND_JOINED ||
  316. (circ->base_.purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  317. circ->base_.purpose <= CIRCUIT_PURPOSE_C_INTRODUCE_ACKED)) {
  318. /* Check to see if the shouldcount result has changed due to a
  319. * unexpected purpose change that would affect our results.
  320. *
  321. * The reason we check the path state too here is because for the
  322. * cannibalized versions of these purposes, we count them as successful
  323. * before their purpose change.
  324. */
  325. if (circ->pathbias_shouldcount == PATHBIAS_SHOULDCOUNT_COUNTED
  326. && circ->path_state != PATH_STATE_ALREADY_COUNTED) {
  327. log_info(LD_BUG,
  328. "Circuit %d is now being ignored despite being counted "
  329. "in the past. Purpose is %s, path state is %s",
  330. circ->global_identifier,
  331. circuit_purpose_to_string(circ->base_.purpose),
  332. pathbias_state_to_string(circ->path_state));
  333. }
  334. circ->pathbias_shouldcount = PATHBIAS_SHOULDCOUNT_IGNORED;
  335. return 0;
  336. }
  337. /* Completely ignore one hop circuits */
  338. if (circ->build_state->onehop_tunnel ||
  339. circ->build_state->desired_path_len == 1) {
  340. /* Check for inconsistency */
  341. if (circ->build_state->desired_path_len != 1 ||
  342. !circ->build_state->onehop_tunnel) {
  343. if ((rate_msg = rate_limit_log(&count_limit, approx_time()))) {
  344. log_info(LD_BUG,
  345. "One-hop circuit has length %d. Path state is %s. "
  346. "Circuit is a %s currently %s.%s",
  347. circ->build_state->desired_path_len,
  348. pathbias_state_to_string(circ->path_state),
  349. circuit_purpose_to_string(circ->base_.purpose),
  350. circuit_state_to_string(circ->base_.state),
  351. rate_msg);
  352. tor_free(rate_msg);
  353. }
  354. tor_fragile_assert();
  355. }
  356. /* Check to see if the shouldcount result has changed due to a
  357. * unexpected change that would affect our results */
  358. if (circ->pathbias_shouldcount == PATHBIAS_SHOULDCOUNT_COUNTED) {
  359. log_info(LD_BUG,
  360. "One-hop circuit %d is now being ignored despite being counted "
  361. "in the past. Purpose is %s, path state is %s",
  362. circ->global_identifier,
  363. circuit_purpose_to_string(circ->base_.purpose),
  364. pathbias_state_to_string(circ->path_state));
  365. }
  366. circ->pathbias_shouldcount = PATHBIAS_SHOULDCOUNT_IGNORED;
  367. return 0;
  368. }
  369. /* Check to see if the shouldcount result has changed due to a
  370. * unexpected purpose change that would affect our results */
  371. if (circ->pathbias_shouldcount == PATHBIAS_SHOULDCOUNT_IGNORED) {
  372. log_info(LD_CIRC,
  373. "Circuit %d is not being counted by pathbias because it was "
  374. "ignored in the past. Purpose is %s, path state is %s",
  375. circ->global_identifier,
  376. circuit_purpose_to_string(circ->base_.purpose),
  377. pathbias_state_to_string(circ->path_state));
  378. return 0;
  379. }
  380. circ->pathbias_shouldcount = PATHBIAS_SHOULDCOUNT_COUNTED;
  381. return 1;
  382. }
  383. /**
  384. * Check our circuit state to see if this is a successful circuit attempt.
  385. * If so, record it in the current guard's path bias circ_attempt count.
  386. *
  387. * Also check for several potential error cases for bug #6475.
  388. */
  389. int
  390. pathbias_count_build_attempt(origin_circuit_t *circ)
  391. {
  392. #define CIRC_ATTEMPT_NOTICE_INTERVAL (600)
  393. static ratelim_t circ_attempt_notice_limit =
  394. RATELIM_INIT(CIRC_ATTEMPT_NOTICE_INTERVAL);
  395. char *rate_msg = NULL;
  396. if (!pathbias_should_count(circ)) {
  397. return 0;
  398. }
  399. if (pathbias_is_new_circ_attempt(circ)) {
  400. /* Help track down the real cause of bug #6475: */
  401. if (circ->has_opened && circ->path_state != PATH_STATE_BUILD_ATTEMPTED) {
  402. if ((rate_msg = rate_limit_log(&circ_attempt_notice_limit,
  403. approx_time()))) {
  404. log_info(LD_BUG,
  405. "Opened circuit is in strange path state %s. "
  406. "Circuit is a %s currently %s.%s",
  407. pathbias_state_to_string(circ->path_state),
  408. circuit_purpose_to_string(circ->base_.purpose),
  409. circuit_state_to_string(circ->base_.state),
  410. rate_msg);
  411. tor_free(rate_msg);
  412. }
  413. }
  414. /* Don't re-count cannibalized circs.. */
  415. if (!circ->has_opened) {
  416. entry_guard_t *guard = NULL;
  417. if (circ->cpath && circ->cpath->extend_info) {
  418. guard = entry_guard_get_by_id_digest(
  419. circ->cpath->extend_info->identity_digest);
  420. } else if (circ->base_.n_chan) {
  421. guard =
  422. entry_guard_get_by_id_digest(circ->base_.n_chan->identity_digest);
  423. }
  424. if (guard) {
  425. if (circ->path_state == PATH_STATE_NEW_CIRC) {
  426. circ->path_state = PATH_STATE_BUILD_ATTEMPTED;
  427. if (entry_guard_inc_circ_attempt_count(guard) < 0) {
  428. /* Bogus guard; we already warned. */
  429. return -END_CIRC_REASON_TORPROTOCOL;
  430. }
  431. } else {
  432. if ((rate_msg = rate_limit_log(&circ_attempt_notice_limit,
  433. approx_time()))) {
  434. log_info(LD_BUG,
  435. "Unopened circuit has strange path state %s. "
  436. "Circuit is a %s currently %s.%s",
  437. pathbias_state_to_string(circ->path_state),
  438. circuit_purpose_to_string(circ->base_.purpose),
  439. circuit_state_to_string(circ->base_.state),
  440. rate_msg);
  441. tor_free(rate_msg);
  442. }
  443. }
  444. } else {
  445. if ((rate_msg = rate_limit_log(&circ_attempt_notice_limit,
  446. approx_time()))) {
  447. log_info(LD_CIRC,
  448. "Unopened circuit has no known guard. "
  449. "Circuit is a %s currently %s.%s",
  450. circuit_purpose_to_string(circ->base_.purpose),
  451. circuit_state_to_string(circ->base_.state),
  452. rate_msg);
  453. tor_free(rate_msg);
  454. }
  455. }
  456. }
  457. }
  458. return 0;
  459. }
  460. /**
  461. * Check our circuit state to see if this is a successful circuit
  462. * completion. If so, record it in the current guard's path bias
  463. * success count.
  464. *
  465. * Also check for several potential error cases for bug #6475.
  466. */
  467. void
  468. pathbias_count_build_success(origin_circuit_t *circ)
  469. {
  470. #define SUCCESS_NOTICE_INTERVAL (600)
  471. static ratelim_t success_notice_limit =
  472. RATELIM_INIT(SUCCESS_NOTICE_INTERVAL);
  473. char *rate_msg = NULL;
  474. entry_guard_t *guard = NULL;
  475. if (!pathbias_should_count(circ)) {
  476. return;
  477. }
  478. /* Don't count cannibalized/reused circs for path bias
  479. * "build" success, since they get counted under "use" success. */
  480. if (!circ->has_opened) {
  481. if (circ->cpath && circ->cpath->extend_info) {
  482. guard = entry_guard_get_by_id_digest(
  483. circ->cpath->extend_info->identity_digest);
  484. }
  485. if (guard) {
  486. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  487. if (circ->path_state == PATH_STATE_BUILD_ATTEMPTED) {
  488. circ->path_state = PATH_STATE_BUILD_SUCCEEDED;
  489. pb->circ_successes++;
  490. entry_guards_changed();
  491. log_info(LD_CIRC, "Got success count %f/%f for guard %s",
  492. pb->circ_successes, pb->circ_attempts,
  493. entry_guard_describe(guard));
  494. } else {
  495. if ((rate_msg = rate_limit_log(&success_notice_limit,
  496. approx_time()))) {
  497. log_info(LD_BUG,
  498. "Succeeded circuit is in strange path state %s. "
  499. "Circuit is a %s currently %s.%s",
  500. pathbias_state_to_string(circ->path_state),
  501. circuit_purpose_to_string(circ->base_.purpose),
  502. circuit_state_to_string(circ->base_.state),
  503. rate_msg);
  504. tor_free(rate_msg);
  505. }
  506. }
  507. if (pb->circ_attempts < pb->circ_successes) {
  508. log_notice(LD_BUG, "Unexpectedly high successes counts (%f/%f) "
  509. "for guard %s",
  510. pb->circ_successes, pb->circ_attempts,
  511. entry_guard_describe(guard));
  512. }
  513. /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
  514. * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
  515. * No need to log that case. */
  516. } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
  517. if ((rate_msg = rate_limit_log(&success_notice_limit,
  518. approx_time()))) {
  519. log_info(LD_CIRC,
  520. "Completed circuit has no known guard. "
  521. "Circuit is a %s currently %s.%s",
  522. circuit_purpose_to_string(circ->base_.purpose),
  523. circuit_state_to_string(circ->base_.state),
  524. rate_msg);
  525. tor_free(rate_msg);
  526. }
  527. }
  528. } else {
  529. if (circ->path_state < PATH_STATE_BUILD_SUCCEEDED) {
  530. if ((rate_msg = rate_limit_log(&success_notice_limit,
  531. approx_time()))) {
  532. log_info(LD_BUG,
  533. "Opened circuit is in strange path state %s. "
  534. "Circuit is a %s currently %s.%s",
  535. pathbias_state_to_string(circ->path_state),
  536. circuit_purpose_to_string(circ->base_.purpose),
  537. circuit_state_to_string(circ->base_.state),
  538. rate_msg);
  539. tor_free(rate_msg);
  540. }
  541. }
  542. }
  543. }
  544. /**
  545. * Record an attempt to use a circuit. Changes the circuit's
  546. * path state and update its guard's usage counter.
  547. *
  548. * Used for path bias usage accounting.
  549. */
  550. void
  551. pathbias_count_use_attempt(origin_circuit_t *circ)
  552. {
  553. if (!pathbias_should_count(circ)) {
  554. return;
  555. }
  556. if (circ->path_state < PATH_STATE_BUILD_SUCCEEDED) {
  557. log_notice(LD_BUG,
  558. "Used circuit is in strange path state %s. "
  559. "Circuit is a %s currently %s.",
  560. pathbias_state_to_string(circ->path_state),
  561. circuit_purpose_to_string(circ->base_.purpose),
  562. circuit_state_to_string(circ->base_.state));
  563. } else if (circ->path_state < PATH_STATE_USE_ATTEMPTED) {
  564. entry_guard_t *guard = entry_guard_get_by_id_digest(
  565. circ->cpath->extend_info->identity_digest);
  566. if (guard) {
  567. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  568. pathbias_measure_use_rate(guard);
  569. pathbias_scale_use_rates(guard);
  570. pb->use_attempts++;
  571. entry_guards_changed();
  572. log_debug(LD_CIRC,
  573. "Marked circuit %d (%f/%f) as used for guard %s.",
  574. circ->global_identifier,
  575. pb->use_successes, pb->use_attempts,
  576. entry_guard_describe(guard));
  577. }
  578. circ->path_state = PATH_STATE_USE_ATTEMPTED;
  579. } else {
  580. /* Harmless but educational log message */
  581. log_info(LD_CIRC,
  582. "Used circuit %d is already in path state %s. "
  583. "Circuit is a %s currently %s.",
  584. circ->global_identifier,
  585. pathbias_state_to_string(circ->path_state),
  586. circuit_purpose_to_string(circ->base_.purpose),
  587. circuit_state_to_string(circ->base_.state));
  588. }
  589. return;
  590. }
  591. /**
  592. * Check the circuit's path state is appropriate and mark it as
  593. * successfully used. Used for path bias usage accounting.
  594. *
  595. * We don't actually increment the guard's counters until
  596. * pathbias_check_close(), because the circuit can still transition
  597. * back to PATH_STATE_USE_ATTEMPTED if a stream fails later (this
  598. * is done so we can probe the circuit for liveness at close).
  599. */
  600. void
  601. pathbias_mark_use_success(origin_circuit_t *circ)
  602. {
  603. if (!pathbias_should_count(circ)) {
  604. return;
  605. }
  606. if (circ->path_state < PATH_STATE_USE_ATTEMPTED) {
  607. log_notice(LD_BUG,
  608. "Used circuit %d is in strange path state %s. "
  609. "Circuit is a %s currently %s.",
  610. circ->global_identifier,
  611. pathbias_state_to_string(circ->path_state),
  612. circuit_purpose_to_string(circ->base_.purpose),
  613. circuit_state_to_string(circ->base_.state));
  614. pathbias_count_use_attempt(circ);
  615. }
  616. /* We don't do any accounting at the guard until actual circuit close */
  617. circ->path_state = PATH_STATE_USE_SUCCEEDED;
  618. return;
  619. }
  620. /**
  621. * If a stream ever detatches from a circuit in a retriable way,
  622. * we need to mark this circuit as still needing either another
  623. * successful stream, or in need of a probe.
  624. *
  625. * An adversary could let the first stream request succeed (ie the
  626. * resolve), but then tag and timeout the remainder (via cell
  627. * dropping), forcing them on new circuits.
  628. *
  629. * Rolling back the state will cause us to probe such circuits, which
  630. * should lead to probe failures in the event of such tagging due to
  631. * either unrecognized cells coming in while we wait for the probe,
  632. * or the cipher state getting out of sync in the case of dropped cells.
  633. */
  634. void
  635. pathbias_mark_use_rollback(origin_circuit_t *circ)
  636. {
  637. if (circ->path_state == PATH_STATE_USE_SUCCEEDED) {
  638. log_info(LD_CIRC,
  639. "Rolling back pathbias use state to 'attempted' for detached "
  640. "circuit %d", circ->global_identifier);
  641. circ->path_state = PATH_STATE_USE_ATTEMPTED;
  642. }
  643. }
  644. /**
  645. * Actually count a circuit success towards a guard's usage counters
  646. * if the path state is appropriate.
  647. */
  648. static void
  649. pathbias_count_use_success(origin_circuit_t *circ)
  650. {
  651. entry_guard_t *guard;
  652. if (!pathbias_should_count(circ)) {
  653. return;
  654. }
  655. if (circ->path_state != PATH_STATE_USE_SUCCEEDED) {
  656. log_notice(LD_BUG,
  657. "Successfully used circuit %d is in strange path state %s. "
  658. "Circuit is a %s currently %s.",
  659. circ->global_identifier,
  660. pathbias_state_to_string(circ->path_state),
  661. circuit_purpose_to_string(circ->base_.purpose),
  662. circuit_state_to_string(circ->base_.state));
  663. } else {
  664. guard = entry_guard_get_by_id_digest(
  665. circ->cpath->extend_info->identity_digest);
  666. if (guard) {
  667. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  668. pb->use_successes++;
  669. entry_guards_changed();
  670. if (pb->use_attempts < pb->use_successes) {
  671. log_notice(LD_BUG, "Unexpectedly high use successes counts (%f/%f) "
  672. "for guard %s",
  673. pb->use_successes, pb->use_attempts,
  674. entry_guard_describe(guard));
  675. }
  676. log_debug(LD_CIRC,
  677. "Marked circuit %d (%f/%f) as used successfully for guard %s",
  678. circ->global_identifier, pb->use_successes,
  679. pb->use_attempts,
  680. entry_guard_describe(guard));
  681. }
  682. }
  683. return;
  684. }
  685. /**
  686. * Send a probe down a circuit that the client attempted to use,
  687. * but for which the stream timed out/failed. The probe is a
  688. * RELAY_BEGIN cell with a 0.a.b.c destination address, which
  689. * the exit will reject and reply back, echoing that address.
  690. *
  691. * The reason for such probes is because it is possible to bias
  692. * a user's paths simply by causing timeouts, and these timeouts
  693. * are not possible to differentiate from unresponsive servers.
  694. *
  695. * The probe is sent at the end of the circuit lifetime for two
  696. * reasons: to prevent cryptographic taggers from being able to
  697. * drop cells to cause timeouts, and to prevent easy recognition
  698. * of probes before any real client traffic happens.
  699. *
  700. * Returns -1 if we couldn't probe, 0 otherwise.
  701. */
  702. static int
  703. pathbias_send_usable_probe(circuit_t *circ)
  704. {
  705. /* Based on connection_ap_handshake_send_begin() */
  706. char payload[CELL_PAYLOAD_SIZE];
  707. int payload_len;
  708. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  709. crypt_path_t *cpath_layer = NULL;
  710. char *probe_nonce = NULL;
  711. tor_assert(ocirc);
  712. cpath_layer = ocirc->cpath->prev;
  713. if (cpath_layer->state != CPATH_STATE_OPEN) {
  714. /* This can happen for cannibalized circuits. Their
  715. * last hop isn't yet open */
  716. log_info(LD_CIRC,
  717. "Got pathbias probe request for unopened circuit %d. "
  718. "Opened %d, len %d", ocirc->global_identifier,
  719. ocirc->has_opened, ocirc->build_state->desired_path_len);
  720. return -1;
  721. }
  722. /* We already went down this road. */
  723. if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING &&
  724. ocirc->pathbias_probe_id) {
  725. log_info(LD_CIRC,
  726. "Got pathbias probe request for circuit %d with "
  727. "outstanding probe", ocirc->global_identifier);
  728. return -1;
  729. }
  730. /* Can't probe if the channel isn't open */
  731. if (circ->n_chan == NULL ||
  732. (!CHANNEL_IS_OPEN(circ->n_chan)
  733. && !CHANNEL_IS_MAINT(circ->n_chan))) {
  734. log_info(LD_CIRC,
  735. "Skipping pathbias probe for circuit %d: Channel is not open.",
  736. ocirc->global_identifier);
  737. return -1;
  738. }
  739. circuit_change_purpose(circ, CIRCUIT_PURPOSE_PATH_BIAS_TESTING);
  740. /* Update timestamp for when circuit_expire_building() should kill us */
  741. tor_gettimeofday(&circ->timestamp_began);
  742. /* Generate a random address for the nonce */
  743. crypto_rand((char*)&ocirc->pathbias_probe_nonce,
  744. sizeof(ocirc->pathbias_probe_nonce));
  745. ocirc->pathbias_probe_nonce &= 0x00ffffff;
  746. probe_nonce = tor_dup_ip(ocirc->pathbias_probe_nonce);
  747. tor_snprintf(payload,RELAY_PAYLOAD_SIZE, "%s:25", probe_nonce);
  748. payload_len = (int)strlen(payload)+1;
  749. // XXX: need this? Can we assume ipv4 will always be supported?
  750. // If not, how do we tell?
  751. //if (payload_len <= RELAY_PAYLOAD_SIZE - 4 && edge_conn->begincell_flags) {
  752. // set_uint32(payload + payload_len, htonl(edge_conn->begincell_flags));
  753. // payload_len += 4;
  754. //}
  755. /* Generate+Store stream id, make sure it's non-zero */
  756. ocirc->pathbias_probe_id = get_unique_stream_id_by_circ(ocirc);
  757. if (ocirc->pathbias_probe_id==0) {
  758. log_warn(LD_CIRC,
  759. "Ran out of stream IDs on circuit %u during "
  760. "pathbias probe attempt.", ocirc->global_identifier);
  761. tor_free(probe_nonce);
  762. return -1;
  763. }
  764. log_info(LD_CIRC,
  765. "Sending pathbias testing cell to %s:25 on stream %d for circ %d.",
  766. probe_nonce, ocirc->pathbias_probe_id, ocirc->global_identifier);
  767. tor_free(probe_nonce);
  768. /* Send a test relay cell */
  769. if (relay_send_command_from_edge(ocirc->pathbias_probe_id, circ,
  770. RELAY_COMMAND_BEGIN, payload,
  771. payload_len, cpath_layer) < 0) {
  772. log_notice(LD_CIRC,
  773. "Failed to send pathbias probe cell on circuit %d.",
  774. ocirc->global_identifier);
  775. return -1;
  776. }
  777. /* Mark it freshly dirty so it doesn't get expired in the meantime */
  778. circ->timestamp_dirty = time(NULL);
  779. return 0;
  780. }
  781. /**
  782. * Check the response to a pathbias probe, to ensure the
  783. * cell is recognized and the nonce and other probe
  784. * characteristics are as expected.
  785. *
  786. * If the response is valid, return 0. Otherwise return < 0.
  787. */
  788. int
  789. pathbias_check_probe_response(circuit_t *circ, const cell_t *cell)
  790. {
  791. /* Based on connection_edge_process_relay_cell() */
  792. relay_header_t rh;
  793. int reason;
  794. uint32_t ipv4_host;
  795. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  796. tor_assert(cell);
  797. tor_assert(ocirc);
  798. tor_assert(circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING);
  799. relay_header_unpack(&rh, cell->payload);
  800. reason = rh.length > 0 ?
  801. get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
  802. if (rh.command == RELAY_COMMAND_END &&
  803. reason == END_STREAM_REASON_EXITPOLICY &&
  804. ocirc->pathbias_probe_id == rh.stream_id) {
  805. /* Check length+extract host: It is in network order after the reason code.
  806. * See connection_edge_end(). */
  807. if (rh.length < 9) { /* reason+ipv4+dns_ttl */
  808. log_notice(LD_PROTOCOL,
  809. "Short path bias probe response length field (%d).", rh.length);
  810. return - END_CIRC_REASON_TORPROTOCOL;
  811. }
  812. ipv4_host = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
  813. /* Check nonce */
  814. if (ipv4_host == ocirc->pathbias_probe_nonce) {
  815. pathbias_mark_use_success(ocirc);
  816. circuit_read_valid_data(ocirc, rh.length);
  817. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  818. log_info(LD_CIRC,
  819. "Got valid path bias probe back for circ %d, stream %d.",
  820. ocirc->global_identifier, ocirc->pathbias_probe_id);
  821. return 0;
  822. } else {
  823. log_notice(LD_CIRC,
  824. "Got strange probe value 0x%x vs 0x%x back for circ %d, "
  825. "stream %d.", ipv4_host, ocirc->pathbias_probe_nonce,
  826. ocirc->global_identifier, ocirc->pathbias_probe_id);
  827. return -1;
  828. }
  829. }
  830. log_info(LD_CIRC,
  831. "Got another cell back back on pathbias probe circuit %d: "
  832. "Command: %d, Reason: %d, Stream-id: %d",
  833. ocirc->global_identifier, rh.command, reason, rh.stream_id);
  834. return -1;
  835. }
  836. /**
  837. * Check if a cell is counts as valid data for a circuit,
  838. * and if so, count it as valid.
  839. */
  840. void
  841. pathbias_count_valid_cells(circuit_t *circ, const cell_t *cell)
  842. {
  843. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  844. relay_header_t rh;
  845. relay_header_unpack(&rh, cell->payload);
  846. /* Check to see if this is a cell from a previous connection,
  847. * or is a request to close the circuit. */
  848. switch (rh.command) {
  849. case RELAY_COMMAND_TRUNCATED:
  850. /* Truncated cells can arrive on path bias circs. When they do,
  851. * just process them. This closes the circ, but it was junk anyway.
  852. * No reason to wait for the probe. */
  853. circuit_read_valid_data(ocirc, rh.length);
  854. circuit_truncated(TO_ORIGIN_CIRCUIT(circ),
  855. get_uint8(cell->payload + RELAY_HEADER_SIZE));
  856. break;
  857. case RELAY_COMMAND_END:
  858. if (connection_half_edge_is_valid_end(ocirc->half_streams,
  859. rh.stream_id)) {
  860. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  861. }
  862. break;
  863. case RELAY_COMMAND_DATA:
  864. if (connection_half_edge_is_valid_data(ocirc->half_streams,
  865. rh.stream_id)) {
  866. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  867. }
  868. break;
  869. case RELAY_COMMAND_SENDME:
  870. if (connection_half_edge_is_valid_sendme(ocirc->half_streams,
  871. rh.stream_id)) {
  872. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  873. }
  874. break;
  875. case RELAY_COMMAND_CONNECTED:
  876. if (connection_half_edge_is_valid_connected(ocirc->half_streams,
  877. rh.stream_id)) {
  878. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  879. }
  880. break;
  881. case RELAY_COMMAND_RESOLVED:
  882. if (connection_half_edge_is_valid_resolved(ocirc->half_streams,
  883. rh.stream_id)) {
  884. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  885. }
  886. break;
  887. }
  888. }
  889. /**
  890. * Check if a circuit was used and/or closed successfully.
  891. *
  892. * If we attempted to use the circuit to carry a stream but failed
  893. * for whatever reason, or if the circuit mysteriously died before
  894. * we could attach any streams, record these two cases.
  895. *
  896. * If we *have* successfully used the circuit, or it appears to
  897. * have been closed by us locally, count it as a success.
  898. *
  899. * Returns 0 if we're done making decisions with the circ,
  900. * or -1 if we want to probe it first.
  901. */
  902. int
  903. pathbias_check_close(origin_circuit_t *ocirc, int reason)
  904. {
  905. circuit_t *circ = &ocirc->base_;
  906. if (!pathbias_should_count(ocirc)) {
  907. return 0;
  908. }
  909. switch (ocirc->path_state) {
  910. /* If the circuit was closed after building, but before use, we need
  911. * to ensure we were the ones who tried to close it (and not a remote
  912. * actor). */
  913. case PATH_STATE_BUILD_SUCCEEDED:
  914. if (reason & END_CIRC_REASON_FLAG_REMOTE) {
  915. /* Remote circ close reasons on an unused circuit all could be bias */
  916. log_info(LD_CIRC,
  917. "Circuit %d remote-closed without successful use for reason %d. "
  918. "Circuit purpose %d currently %d,%s. Len %d.",
  919. ocirc->global_identifier,
  920. reason, circ->purpose, ocirc->has_opened,
  921. circuit_state_to_string(circ->state),
  922. ocirc->build_state->desired_path_len);
  923. pathbias_count_collapse(ocirc);
  924. } else if ((reason & ~END_CIRC_REASON_FLAG_REMOTE)
  925. == END_CIRC_REASON_CHANNEL_CLOSED &&
  926. circ->n_chan &&
  927. circ->n_chan->reason_for_closing
  928. != CHANNEL_CLOSE_REQUESTED) {
  929. /* If we didn't close the channel ourselves, it could be bias */
  930. /* XXX: Only count bias if the network is live?
  931. * What about clock jumps/suspends? */
  932. log_info(LD_CIRC,
  933. "Circuit %d's channel closed without successful use for reason "
  934. "%d, channel reason %d. Circuit purpose %d currently %d,%s. Len "
  935. "%d.", ocirc->global_identifier,
  936. reason, circ->n_chan->reason_for_closing,
  937. circ->purpose, ocirc->has_opened,
  938. circuit_state_to_string(circ->state),
  939. ocirc->build_state->desired_path_len);
  940. pathbias_count_collapse(ocirc);
  941. } else {
  942. pathbias_count_successful_close(ocirc);
  943. }
  944. break;
  945. /* If we tried to use a circuit but failed, we should probe it to ensure
  946. * it has not been tampered with. */
  947. case PATH_STATE_USE_ATTEMPTED:
  948. /* XXX: Only probe and/or count failure if the network is live?
  949. * What about clock jumps/suspends? */
  950. if (pathbias_send_usable_probe(circ) == 0)
  951. return -1;
  952. else
  953. pathbias_count_use_failed(ocirc);
  954. /* Any circuit where there were attempted streams but no successful
  955. * streams could be bias */
  956. log_info(LD_CIRC,
  957. "Circuit %d closed without successful use for reason %d. "
  958. "Circuit purpose %d currently %d,%s. Len %d.",
  959. ocirc->global_identifier,
  960. reason, circ->purpose, ocirc->has_opened,
  961. circuit_state_to_string(circ->state),
  962. ocirc->build_state->desired_path_len);
  963. break;
  964. case PATH_STATE_USE_SUCCEEDED:
  965. pathbias_count_successful_close(ocirc);
  966. pathbias_count_use_success(ocirc);
  967. break;
  968. case PATH_STATE_USE_FAILED:
  969. pathbias_count_use_failed(ocirc);
  970. break;
  971. case PATH_STATE_NEW_CIRC:
  972. case PATH_STATE_BUILD_ATTEMPTED:
  973. case PATH_STATE_ALREADY_COUNTED:
  974. default:
  975. // Other states are uninteresting. No stats to count.
  976. break;
  977. }
  978. ocirc->path_state = PATH_STATE_ALREADY_COUNTED;
  979. return 0;
  980. }
  981. /**
  982. * Count a successfully closed circuit.
  983. */
  984. static void
  985. pathbias_count_successful_close(origin_circuit_t *circ)
  986. {
  987. entry_guard_t *guard = NULL;
  988. if (!pathbias_should_count(circ)) {
  989. return;
  990. }
  991. if (circ->cpath && circ->cpath->extend_info) {
  992. guard = entry_guard_get_by_id_digest(
  993. circ->cpath->extend_info->identity_digest);
  994. }
  995. if (guard) {
  996. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  997. /* In the long run: circuit_success ~= successful_circuit_close +
  998. * circ_failure + stream_failure */
  999. pb->successful_circuits_closed++;
  1000. entry_guards_changed();
  1001. } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
  1002. /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
  1003. * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
  1004. * No need to log that case. */
  1005. log_info(LD_CIRC,
  1006. "Successfully closed circuit has no known guard. "
  1007. "Circuit is a %s currently %s",
  1008. circuit_purpose_to_string(circ->base_.purpose),
  1009. circuit_state_to_string(circ->base_.state));
  1010. }
  1011. }
  1012. /**
  1013. * Count a circuit that fails after it is built, but before it can
  1014. * carry any traffic.
  1015. *
  1016. * This is needed because there are ways to destroy a
  1017. * circuit after it has successfully completed. Right now, this is
  1018. * used for purely informational/debugging purposes.
  1019. */
  1020. static void
  1021. pathbias_count_collapse(origin_circuit_t *circ)
  1022. {
  1023. entry_guard_t *guard = NULL;
  1024. if (!pathbias_should_count(circ)) {
  1025. return;
  1026. }
  1027. if (circ->cpath && circ->cpath->extend_info) {
  1028. guard = entry_guard_get_by_id_digest(
  1029. circ->cpath->extend_info->identity_digest);
  1030. }
  1031. if (guard) {
  1032. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1033. pb->collapsed_circuits++;
  1034. entry_guards_changed();
  1035. } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
  1036. /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
  1037. * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
  1038. * No need to log that case. */
  1039. log_info(LD_CIRC,
  1040. "Destroyed circuit has no known guard. "
  1041. "Circuit is a %s currently %s",
  1042. circuit_purpose_to_string(circ->base_.purpose),
  1043. circuit_state_to_string(circ->base_.state));
  1044. }
  1045. }
  1046. /**
  1047. * Count a known failed circuit (because we could not probe it).
  1048. *
  1049. * This counter is informational.
  1050. */
  1051. static void
  1052. pathbias_count_use_failed(origin_circuit_t *circ)
  1053. {
  1054. entry_guard_t *guard = NULL;
  1055. if (!pathbias_should_count(circ)) {
  1056. return;
  1057. }
  1058. if (circ->cpath && circ->cpath->extend_info) {
  1059. guard = entry_guard_get_by_id_digest(
  1060. circ->cpath->extend_info->identity_digest);
  1061. }
  1062. if (guard) {
  1063. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1064. pb->unusable_circuits++;
  1065. entry_guards_changed();
  1066. } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
  1067. /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
  1068. * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
  1069. * No need to log that case. */
  1070. /* XXX note cut-and-paste code in this function compared to nearby
  1071. * functions. Would be nice to refactor. -RD */
  1072. log_info(LD_CIRC,
  1073. "Stream-failing circuit has no known guard. "
  1074. "Circuit is a %s currently %s",
  1075. circuit_purpose_to_string(circ->base_.purpose),
  1076. circuit_state_to_string(circ->base_.state));
  1077. }
  1078. }
  1079. /**
  1080. * Count timeouts for path bias log messages.
  1081. *
  1082. * These counts are purely informational.
  1083. */
  1084. void
  1085. pathbias_count_timeout(origin_circuit_t *circ)
  1086. {
  1087. entry_guard_t *guard = NULL;
  1088. if (!pathbias_should_count(circ)) {
  1089. return;
  1090. }
  1091. /* For hidden service circs, they can actually be used
  1092. * successfully and then time out later (because
  1093. * the other side declines to use them). */
  1094. if (circ->path_state == PATH_STATE_USE_SUCCEEDED) {
  1095. return;
  1096. }
  1097. if (circ->cpath && circ->cpath->extend_info) {
  1098. guard = entry_guard_get_by_id_digest(
  1099. circ->cpath->extend_info->identity_digest);
  1100. }
  1101. if (guard) {
  1102. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1103. pb->timeouts++;
  1104. entry_guards_changed();
  1105. }
  1106. }
  1107. /**
  1108. * Helper function to count all of the currently opened circuits
  1109. * for a guard that are in a given path state range. The state
  1110. * range is inclusive on both ends.
  1111. */
  1112. static int
  1113. pathbias_count_circs_in_states(entry_guard_t *guard,
  1114. path_state_t from,
  1115. path_state_t to)
  1116. {
  1117. int open_circuits = 0;
  1118. /* Count currently open circuits. Give them the benefit of the doubt. */
  1119. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1120. origin_circuit_t *ocirc = NULL;
  1121. if (!CIRCUIT_IS_ORIGIN(circ) || /* didn't originate here */
  1122. circ->marked_for_close) /* already counted */
  1123. continue;
  1124. ocirc = TO_ORIGIN_CIRCUIT(circ);
  1125. if (!ocirc->cpath || !ocirc->cpath->extend_info)
  1126. continue;
  1127. if (ocirc->path_state >= from &&
  1128. ocirc->path_state <= to &&
  1129. pathbias_should_count(ocirc) &&
  1130. fast_memeq(entry_guard_get_rsa_id_digest(guard),
  1131. ocirc->cpath->extend_info->identity_digest,
  1132. DIGEST_LEN)) {
  1133. log_debug(LD_CIRC, "Found opened circuit %d in path_state %s",
  1134. ocirc->global_identifier,
  1135. pathbias_state_to_string(ocirc->path_state));
  1136. open_circuits++;
  1137. }
  1138. }
  1139. SMARTLIST_FOREACH_END(circ);
  1140. return open_circuits;
  1141. }
  1142. /**
  1143. * Return the number of circuits counted as successfully closed for
  1144. * this guard.
  1145. *
  1146. * Also add in the currently open circuits to give them the benefit
  1147. * of the doubt.
  1148. */
  1149. double
  1150. pathbias_get_close_success_count(entry_guard_t *guard)
  1151. {
  1152. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1153. return pb->successful_circuits_closed +
  1154. pathbias_count_circs_in_states(guard,
  1155. PATH_STATE_BUILD_SUCCEEDED,
  1156. PATH_STATE_USE_SUCCEEDED);
  1157. }
  1158. /**
  1159. * Return the number of circuits counted as successfully used
  1160. * this guard.
  1161. *
  1162. * Also add in the currently open circuits that we are attempting
  1163. * to use to give them the benefit of the doubt.
  1164. */
  1165. double
  1166. pathbias_get_use_success_count(entry_guard_t *guard)
  1167. {
  1168. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1169. return pb->use_successes +
  1170. pathbias_count_circs_in_states(guard,
  1171. PATH_STATE_USE_ATTEMPTED,
  1172. PATH_STATE_USE_SUCCEEDED);
  1173. }
  1174. /**
  1175. * Check the path bias use rate against our consensus parameter limits.
  1176. *
  1177. * Emits a log message if the use success rates are too low.
  1178. *
  1179. * If pathbias_get_dropguards() is set, we also disable the use of
  1180. * very failure prone guards.
  1181. */
  1182. static void
  1183. pathbias_measure_use_rate(entry_guard_t *guard)
  1184. {
  1185. const or_options_t *options = get_options();
  1186. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1187. if (pb->use_attempts > pathbias_get_min_use(options)) {
  1188. /* Note: We rely on the < comparison here to allow us to set a 0
  1189. * rate and disable the feature entirely. If refactoring, don't
  1190. * change to <= */
  1191. if (pathbias_get_use_success_count(guard)/pb->use_attempts
  1192. < pathbias_get_extreme_use_rate(options)) {
  1193. /* Dropping is currently disabled by default. */
  1194. if (pathbias_get_dropguards(options)) {
  1195. if (!pb->path_bias_disabled) {
  1196. log_warn(LD_CIRC,
  1197. "Guard %s is failing to carry an extremely large "
  1198. "amount of stream on its circuits. "
  1199. "To avoid potential route manipulation attacks, Tor has "
  1200. "disabled use of this guard. "
  1201. "Use counts are %ld/%ld. Success counts are %ld/%ld. "
  1202. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1203. "and %ld timed out. "
  1204. "For reference, your timeout cutoff is %ld seconds.",
  1205. entry_guard_describe(guard),
  1206. tor_lround(pathbias_get_use_success_count(guard)),
  1207. tor_lround(pb->use_attempts),
  1208. tor_lround(pathbias_get_close_success_count(guard)),
  1209. tor_lround(pb->circ_attempts),
  1210. tor_lround(pb->circ_successes),
  1211. tor_lround(pb->unusable_circuits),
  1212. tor_lround(pb->collapsed_circuits),
  1213. tor_lround(pb->timeouts),
  1214. tor_lround(get_circuit_build_close_time_ms()/1000));
  1215. pb->path_bias_disabled = 1;
  1216. return;
  1217. }
  1218. } else if (!pb->path_bias_use_extreme) {
  1219. pb->path_bias_use_extreme = 1;
  1220. log_warn(LD_CIRC,
  1221. "Guard %s is failing to carry an extremely large "
  1222. "amount of streams on its circuits. "
  1223. "This could indicate a route manipulation attack, network "
  1224. "overload, bad local network connectivity, or a bug. "
  1225. "Use counts are %ld/%ld. Success counts are %ld/%ld. "
  1226. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1227. "and %ld timed out. "
  1228. "For reference, your timeout cutoff is %ld seconds.",
  1229. entry_guard_describe(guard),
  1230. tor_lround(pathbias_get_use_success_count(guard)),
  1231. tor_lround(pb->use_attempts),
  1232. tor_lround(pathbias_get_close_success_count(guard)),
  1233. tor_lround(pb->circ_attempts),
  1234. tor_lround(pb->circ_successes),
  1235. tor_lround(pb->unusable_circuits),
  1236. tor_lround(pb->collapsed_circuits),
  1237. tor_lround(pb->timeouts),
  1238. tor_lround(get_circuit_build_close_time_ms()/1000));
  1239. }
  1240. } else if (pathbias_get_use_success_count(guard)/pb->use_attempts
  1241. < pathbias_get_notice_use_rate(options)) {
  1242. if (!pb->path_bias_use_noticed) {
  1243. pb->path_bias_use_noticed = 1;
  1244. log_notice(LD_CIRC,
  1245. "Guard %s is failing to carry more streams on its "
  1246. "circuits than usual. "
  1247. "Most likely this means the Tor network is overloaded "
  1248. "or your network connection is poor. "
  1249. "Use counts are %ld/%ld. Success counts are %ld/%ld. "
  1250. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1251. "and %ld timed out. "
  1252. "For reference, your timeout cutoff is %ld seconds.",
  1253. entry_guard_describe(guard),
  1254. tor_lround(pathbias_get_use_success_count(guard)),
  1255. tor_lround(pb->use_attempts),
  1256. tor_lround(pathbias_get_close_success_count(guard)),
  1257. tor_lround(pb->circ_attempts),
  1258. tor_lround(pb->circ_successes),
  1259. tor_lround(pb->unusable_circuits),
  1260. tor_lround(pb->collapsed_circuits),
  1261. tor_lround(pb->timeouts),
  1262. tor_lround(get_circuit_build_close_time_ms()/1000));
  1263. }
  1264. }
  1265. }
  1266. }
  1267. /**
  1268. * Check the path bias circuit close status rates against our consensus
  1269. * parameter limits.
  1270. *
  1271. * Emits a log message if the use success rates are too low.
  1272. *
  1273. * If pathbias_get_dropguards() is set, we also disable the use of
  1274. * very failure prone guards.
  1275. *
  1276. * XXX: This function shares similar log messages and checks to
  1277. * pathbias_measure_use_rate(). It may be possible to combine them
  1278. * eventually, especially if we can ever remove the need for 3
  1279. * levels of closure warns (if the overall circuit failure rate
  1280. * goes down with ntor). One way to do so would be to multiply
  1281. * the build rate with the use rate to get an idea of the total
  1282. * fraction of the total network paths the user is able to use.
  1283. * See ticket #8159.
  1284. */
  1285. static void
  1286. pathbias_measure_close_rate(entry_guard_t *guard)
  1287. {
  1288. const or_options_t *options = get_options();
  1289. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1290. if (pb->circ_attempts > pathbias_get_min_circs(options)) {
  1291. /* Note: We rely on the < comparison here to allow us to set a 0
  1292. * rate and disable the feature entirely. If refactoring, don't
  1293. * change to <= */
  1294. if (pathbias_get_close_success_count(guard)/pb->circ_attempts
  1295. < pathbias_get_extreme_rate(options)) {
  1296. /* Dropping is currently disabled by default. */
  1297. if (pathbias_get_dropguards(options)) {
  1298. if (!pb->path_bias_disabled) {
  1299. log_warn(LD_CIRC,
  1300. "Guard %s is failing an extremely large "
  1301. "amount of circuits. "
  1302. "To avoid potential route manipulation attacks, Tor has "
  1303. "disabled use of this guard. "
  1304. "Success counts are %ld/%ld. Use counts are %ld/%ld. "
  1305. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1306. "and %ld timed out. "
  1307. "For reference, your timeout cutoff is %ld seconds.",
  1308. entry_guard_describe(guard),
  1309. tor_lround(pathbias_get_close_success_count(guard)),
  1310. tor_lround(pb->circ_attempts),
  1311. tor_lround(pathbias_get_use_success_count(guard)),
  1312. tor_lround(pb->use_attempts),
  1313. tor_lround(pb->circ_successes),
  1314. tor_lround(pb->unusable_circuits),
  1315. tor_lround(pb->collapsed_circuits),
  1316. tor_lround(pb->timeouts),
  1317. tor_lround(get_circuit_build_close_time_ms()/1000));
  1318. pb->path_bias_disabled = 1;
  1319. return;
  1320. }
  1321. } else if (!pb->path_bias_extreme) {
  1322. pb->path_bias_extreme = 1;
  1323. log_warn(LD_CIRC,
  1324. "Guard %s is failing an extremely large "
  1325. "amount of circuits. "
  1326. "This could indicate a route manipulation attack, "
  1327. "extreme network overload, or a bug. "
  1328. "Success counts are %ld/%ld. Use counts are %ld/%ld. "
  1329. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1330. "and %ld timed out. "
  1331. "For reference, your timeout cutoff is %ld seconds.",
  1332. entry_guard_describe(guard),
  1333. tor_lround(pathbias_get_close_success_count(guard)),
  1334. tor_lround(pb->circ_attempts),
  1335. tor_lround(pathbias_get_use_success_count(guard)),
  1336. tor_lround(pb->use_attempts),
  1337. tor_lround(pb->circ_successes),
  1338. tor_lround(pb->unusable_circuits),
  1339. tor_lround(pb->collapsed_circuits),
  1340. tor_lround(pb->timeouts),
  1341. tor_lround(get_circuit_build_close_time_ms()/1000));
  1342. }
  1343. } else if (pathbias_get_close_success_count(guard)/pb->circ_attempts
  1344. < pathbias_get_warn_rate(options)) {
  1345. if (!pb->path_bias_warned) {
  1346. pb->path_bias_warned = 1;
  1347. log_warn(LD_CIRC,
  1348. "Guard %s is failing a very large "
  1349. "amount of circuits. "
  1350. "Most likely this means the Tor network is "
  1351. "overloaded, but it could also mean an attack against "
  1352. "you or potentially the guard itself. "
  1353. "Success counts are %ld/%ld. Use counts are %ld/%ld. "
  1354. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1355. "and %ld timed out. "
  1356. "For reference, your timeout cutoff is %ld seconds.",
  1357. entry_guard_describe(guard),
  1358. tor_lround(pathbias_get_close_success_count(guard)),
  1359. tor_lround(pb->circ_attempts),
  1360. tor_lround(pathbias_get_use_success_count(guard)),
  1361. tor_lround(pb->use_attempts),
  1362. tor_lround(pb->circ_successes),
  1363. tor_lround(pb->unusable_circuits),
  1364. tor_lround(pb->collapsed_circuits),
  1365. tor_lround(pb->timeouts),
  1366. tor_lround(get_circuit_build_close_time_ms()/1000));
  1367. }
  1368. } else if (pathbias_get_close_success_count(guard)/pb->circ_attempts
  1369. < pathbias_get_notice_rate(options)) {
  1370. if (!pb->path_bias_noticed) {
  1371. pb->path_bias_noticed = 1;
  1372. log_notice(LD_CIRC,
  1373. "Guard %s is failing more circuits than "
  1374. "usual. "
  1375. "Most likely this means the Tor network is overloaded. "
  1376. "Success counts are %ld/%ld. Use counts are %ld/%ld. "
  1377. "%ld circuits completed, %ld were unusable, %ld collapsed, "
  1378. "and %ld timed out. "
  1379. "For reference, your timeout cutoff is %ld seconds.",
  1380. entry_guard_describe(guard),
  1381. tor_lround(pathbias_get_close_success_count(guard)),
  1382. tor_lround(pb->circ_attempts),
  1383. tor_lround(pathbias_get_use_success_count(guard)),
  1384. tor_lround(pb->use_attempts),
  1385. tor_lround(pb->circ_successes),
  1386. tor_lround(pb->unusable_circuits),
  1387. tor_lround(pb->collapsed_circuits),
  1388. tor_lround(pb->timeouts),
  1389. tor_lround(get_circuit_build_close_time_ms()/1000));
  1390. }
  1391. }
  1392. }
  1393. }
  1394. /**
  1395. * This function scales the path bias use rates if we have
  1396. * more data than the scaling threshold. This allows us to
  1397. * be more sensitive to recent measurements.
  1398. *
  1399. * XXX: The attempt count transfer stuff here might be done
  1400. * better by keeping separate pending counters that get
  1401. * transferred at circuit close. See ticket #8160.
  1402. */
  1403. static void
  1404. pathbias_scale_close_rates(entry_guard_t *guard)
  1405. {
  1406. const or_options_t *options = get_options();
  1407. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1408. /* If we get a ton of circuits, just scale everything down */
  1409. if (pb->circ_attempts > pathbias_get_scale_threshold(options)) {
  1410. double scale_ratio = pathbias_get_scale_ratio(options);
  1411. int opened_attempts = pathbias_count_circs_in_states(guard,
  1412. PATH_STATE_BUILD_ATTEMPTED, PATH_STATE_BUILD_ATTEMPTED);
  1413. int opened_built = pathbias_count_circs_in_states(guard,
  1414. PATH_STATE_BUILD_SUCCEEDED,
  1415. PATH_STATE_USE_FAILED);
  1416. /* Verify that the counts are sane before and after scaling */
  1417. int counts_are_sane = (pb->circ_attempts >= pb->circ_successes);
  1418. pb->circ_attempts -= (opened_attempts+opened_built);
  1419. pb->circ_successes -= opened_built;
  1420. pb->circ_attempts *= scale_ratio;
  1421. pb->circ_successes *= scale_ratio;
  1422. pb->timeouts *= scale_ratio;
  1423. pb->successful_circuits_closed *= scale_ratio;
  1424. pb->collapsed_circuits *= scale_ratio;
  1425. pb->unusable_circuits *= scale_ratio;
  1426. pb->circ_attempts += (opened_attempts+opened_built);
  1427. pb->circ_successes += opened_built;
  1428. entry_guards_changed();
  1429. log_info(LD_CIRC,
  1430. "Scaled pathbias counts to (%f,%f)/%f (%d/%d open) for guard "
  1431. "%s",
  1432. pb->circ_successes, pb->successful_circuits_closed,
  1433. pb->circ_attempts, opened_built, opened_attempts,
  1434. entry_guard_describe(guard));
  1435. /* Have the counts just become invalid by this scaling attempt? */
  1436. if (counts_are_sane && pb->circ_attempts < pb->circ_successes) {
  1437. log_notice(LD_BUG,
  1438. "Scaling has mangled pathbias counts to %f/%f (%d/%d open) "
  1439. "for guard %s",
  1440. pb->circ_successes, pb->circ_attempts, opened_built,
  1441. opened_attempts,
  1442. entry_guard_describe(guard));
  1443. }
  1444. }
  1445. }
  1446. /**
  1447. * This function scales the path bias circuit close rates if we have
  1448. * more data than the scaling threshold. This allows us to be more
  1449. * sensitive to recent measurements.
  1450. *
  1451. * XXX: The attempt count transfer stuff here might be done
  1452. * better by keeping separate pending counters that get
  1453. * transferred at circuit close. See ticket #8160.
  1454. */
  1455. void
  1456. pathbias_scale_use_rates(entry_guard_t *guard)
  1457. {
  1458. const or_options_t *options = get_options();
  1459. guard_pathbias_t *pb = entry_guard_get_pathbias_state(guard);
  1460. /* If we get a ton of circuits, just scale everything down */
  1461. if (pb->use_attempts > pathbias_get_scale_use_threshold(options)) {
  1462. double scale_ratio = pathbias_get_scale_ratio(options);
  1463. int opened_attempts = pathbias_count_circs_in_states(guard,
  1464. PATH_STATE_USE_ATTEMPTED, PATH_STATE_USE_SUCCEEDED);
  1465. /* Verify that the counts are sane before and after scaling */
  1466. int counts_are_sane = (pb->use_attempts >= pb->use_successes);
  1467. pb->use_attempts -= opened_attempts;
  1468. pb->use_attempts *= scale_ratio;
  1469. pb->use_successes *= scale_ratio;
  1470. pb->use_attempts += opened_attempts;
  1471. log_info(LD_CIRC,
  1472. "Scaled pathbias use counts to %f/%f (%d open) for guard %s",
  1473. pb->use_successes, pb->use_attempts, opened_attempts,
  1474. entry_guard_describe(guard));
  1475. /* Have the counts just become invalid by this scaling attempt? */
  1476. if (counts_are_sane && pb->use_attempts < pb->use_successes) {
  1477. log_notice(LD_BUG,
  1478. "Scaling has mangled pathbias usage counts to %f/%f "
  1479. "(%d open) for guard %s",
  1480. pb->circ_successes, pb->circ_attempts,
  1481. opened_attempts, entry_guard_describe(guard));
  1482. }
  1483. entry_guards_changed();
  1484. }
  1485. }