circpathbias.c 56 KB

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