circpathbias.c 55 KB

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