circpathbias.c 57 KB

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