circpathbias.c 55 KB

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