circpathbias.c 56 KB

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