circpathbias.c 55 KB

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