circpathbias.c 55 KB

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