circpathbias.c 55 KB

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