tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

circpathbias.c (60435B)


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