circpathbias.c 54 KB

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