circpathbias.c 56 KB

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