circpathbias.c 55 KB

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