circpathbias.c 55 KB

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