dos.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /*
  4. * \file dos.c
  5. * \brief Implement Denial of Service mitigation subsystem.
  6. */
  7. #define DOS_PRIVATE
  8. #include "or.h"
  9. #include "channel.h"
  10. #include "config.h"
  11. #include "geoip.h"
  12. #include "main.h"
  13. #include "networkstatus.h"
  14. #include "dos.h"
  15. /*
  16. * Circuit creation denial of service mitigation.
  17. *
  18. * Namespace used for this mitigation framework is "dos_cc_" where "cc" is for
  19. * Circuit Creation.
  20. */
  21. /* Is the circuit creation DoS mitigation enabled? */
  22. static unsigned int dos_cc_enabled = 0;
  23. /* Consensus parameters. They can be changed when a new consensus arrives.
  24. * They are initialized with the hardcoded default values. */
  25. static uint32_t dos_cc_min_concurrent_conn;
  26. static uint32_t dos_cc_circuit_rate_tenths;
  27. static uint32_t dos_cc_circuit_burst;
  28. static dos_cc_defense_type_t dos_cc_defense_type;
  29. static int32_t dos_cc_defense_time_period;
  30. /* Keep some stats for the heartbeat so we can report out. */
  31. static uint64_t cc_num_rejected_cells;
  32. static uint32_t cc_num_marked_addrs;
  33. /*
  34. * Concurrent connection denial of service mitigation.
  35. *
  36. * Namespace used for this mitigation framework is "dos_conn_".
  37. */
  38. /* Is the connection DoS mitigation enabled? */
  39. static unsigned int dos_conn_enabled = 0;
  40. /* Consensus parameters. They can be changed when a new consensus arrives.
  41. * They are initialized with the hardcoded default values. */
  42. static uint32_t dos_conn_max_concurrent_count;
  43. static dos_conn_defense_type_t dos_conn_defense_type;
  44. /*
  45. * General interface of the denial of service mitigation subsystem.
  46. */
  47. /* Return true iff the circuit creation mitigation is enabled. We look at the
  48. * consensus for this else a default value is returned. */
  49. MOCK_IMPL(STATIC unsigned int,
  50. get_param_cc_enabled, (const networkstatus_t *ns))
  51. {
  52. if (get_options()->DoSCircuitCreationEnabled != -1) {
  53. return get_options()->DoSCircuitCreationEnabled;
  54. }
  55. return !!networkstatus_get_param(ns, "DoSCircuitCreationEnabled",
  56. DOS_CC_ENABLED_DEFAULT, 0, 1);
  57. }
  58. /* Return the parameter for the minimum concurrent connection at which we'll
  59. * start counting circuit for a specific client address. */
  60. STATIC uint32_t
  61. get_param_cc_min_concurrent_connection(const networkstatus_t *ns)
  62. {
  63. if (get_options()->DoSCircuitCreationMinConnections) {
  64. return get_options()->DoSCircuitCreationMinConnections;
  65. }
  66. return networkstatus_get_param(ns, "DoSCircuitCreationMinConnections",
  67. DOS_CC_MIN_CONCURRENT_CONN_DEFAULT,
  68. 1, INT32_MAX);
  69. }
  70. /* Return the parameter for the time rate that is how many circuits over this
  71. * time span. */
  72. static uint32_t
  73. get_param_cc_circuit_rate_tenths(const networkstatus_t *ns)
  74. {
  75. /* This is in seconds. */
  76. if (get_options()->DoSCircuitCreationRateTenths) {
  77. return get_options()->DoSCircuitCreationRateTenths;
  78. }
  79. return networkstatus_get_param(ns, "DoSCircuitCreationRateTenths",
  80. DOS_CC_CIRCUIT_RATE_TENTHS_DEFAULT,
  81. 1, INT32_MAX);
  82. }
  83. /* Return the parameter for the maximum circuit count for the circuit time
  84. * rate. */
  85. STATIC uint32_t
  86. get_param_cc_circuit_burst(const networkstatus_t *ns)
  87. {
  88. if (get_options()->DoSCircuitCreationBurst) {
  89. return get_options()->DoSCircuitCreationBurst;
  90. }
  91. return networkstatus_get_param(ns, "DoSCircuitCreationBurst",
  92. DOS_CC_CIRCUIT_BURST_DEFAULT,
  93. 1, INT32_MAX);
  94. }
  95. /* Return the consensus parameter of the circuit creation defense type. */
  96. static uint32_t
  97. get_param_cc_defense_type(const networkstatus_t *ns)
  98. {
  99. if (get_options()->DoSCircuitCreationDefenseType) {
  100. return get_options()->DoSCircuitCreationDefenseType;
  101. }
  102. return networkstatus_get_param(ns, "DoSCircuitCreationDefenseType",
  103. DOS_CC_DEFENSE_TYPE_DEFAULT,
  104. DOS_CC_DEFENSE_NONE, DOS_CC_DEFENSE_MAX);
  105. }
  106. /* Return the consensus parameter of the defense time period which is how much
  107. * time should we defend against a malicious client address. */
  108. static int32_t
  109. get_param_cc_defense_time_period(const networkstatus_t *ns)
  110. {
  111. /* Time in seconds. */
  112. if (get_options()->DoSCircuitCreationDefenseTimePeriod) {
  113. return get_options()->DoSCircuitCreationDefenseTimePeriod;
  114. }
  115. return networkstatus_get_param(ns, "DoSCircuitCreationDefenseTimePeriod",
  116. DOS_CC_DEFENSE_TIME_PERIOD_DEFAULT,
  117. 0, INT32_MAX);
  118. }
  119. /* Return true iff connection mitigation is enabled. We look at the consensus
  120. * for this else a default value is returned. */
  121. MOCK_IMPL(STATIC unsigned int,
  122. get_param_conn_enabled, (const networkstatus_t *ns))
  123. {
  124. if (get_options()->DoSConnectionEnabled != -1) {
  125. return get_options()->DoSConnectionEnabled;
  126. }
  127. return !!networkstatus_get_param(ns, "DoSConnectionEnabled",
  128. DOS_CONN_ENABLED_DEFAULT, 0, 1);
  129. }
  130. /* Return the consensus parameter for the maximum concurrent connection
  131. * allowed. */
  132. STATIC uint32_t
  133. get_param_conn_max_concurrent_count(const networkstatus_t *ns)
  134. {
  135. if (get_options()->DoSConnectionMaxConcurrentCount) {
  136. return get_options()->DoSConnectionMaxConcurrentCount;
  137. }
  138. return networkstatus_get_param(ns, "DoSConnectionMaxConcurrentCount",
  139. DOS_CONN_MAX_CONCURRENT_COUNT_DEFAULT,
  140. 1, INT32_MAX);
  141. }
  142. /* Return the consensus parameter of the connection defense type. */
  143. static uint32_t
  144. get_param_conn_defense_type(const networkstatus_t *ns)
  145. {
  146. if (get_options()->DoSConnectionDefenseType) {
  147. return get_options()->DoSConnectionDefenseType;
  148. }
  149. return networkstatus_get_param(ns, "DoSConnectionDefenseType",
  150. DOS_CONN_DEFENSE_TYPE_DEFAULT,
  151. DOS_CONN_DEFENSE_NONE, DOS_CONN_DEFENSE_MAX);
  152. }
  153. /* Set circuit creation parameters located in the consensus or their default
  154. * if none are present. Called at initialization or when the consensus
  155. * changes. */
  156. static void
  157. set_dos_parameters(const networkstatus_t *ns)
  158. {
  159. /* Get the default consensus param values. */
  160. dos_cc_enabled = get_param_cc_enabled(ns);
  161. dos_cc_min_concurrent_conn = get_param_cc_min_concurrent_connection(ns);
  162. dos_cc_circuit_rate_tenths = get_param_cc_circuit_rate_tenths(ns);
  163. dos_cc_circuit_burst = get_param_cc_circuit_burst(ns);
  164. dos_cc_defense_time_period = get_param_cc_defense_time_period(ns);
  165. dos_cc_defense_type = get_param_cc_defense_type(ns);
  166. /* Connection detection. */
  167. dos_conn_enabled = get_param_conn_enabled(ns);
  168. dos_conn_max_concurrent_count = get_param_conn_max_concurrent_count(ns);
  169. dos_conn_defense_type = get_param_conn_defense_type(ns);
  170. }
  171. /* Free everything for the circuit creation DoS mitigation subsystem. */
  172. static void
  173. cc_free_all(void)
  174. {
  175. /* If everything is freed, the circuit creation subsystem is not enabled. */
  176. dos_cc_enabled = 0;
  177. }
  178. /* Called when the consensus has changed. Do appropriate actions for the
  179. * circuit creation subsystem. */
  180. static void
  181. cc_consensus_has_changed(const networkstatus_t *ns)
  182. {
  183. /* Looking at the consensus, is the circuit creation subsystem enabled? If
  184. * not and it was enabled before, clean it up. */
  185. if (dos_cc_enabled && !get_param_cc_enabled(ns)) {
  186. cc_free_all();
  187. }
  188. }
  189. /** Return the number of circuits we allow per second under the current
  190. * configuration. */
  191. STATIC uint32_t
  192. get_circuit_rate_per_second(void)
  193. {
  194. int64_t circ_rate;
  195. /* We take the burst divided by the rate which is in tenths of a second so
  196. * convert to get a circuit rate per second. */
  197. circ_rate = dos_cc_circuit_rate_tenths / 10;
  198. if (circ_rate < 0) {
  199. /* Safety check, never allow it to go below 0 else the bucket will always
  200. * be empty resulting in every address to be detected. */
  201. circ_rate = 1;
  202. }
  203. /* Clamp it down to a 32 bit value because a rate of 2^32 circuits per
  204. * second is just too much in any circumstances. */
  205. if (circ_rate > UINT32_MAX) {
  206. circ_rate = UINT32_MAX;
  207. }
  208. return (uint32_t) circ_rate;
  209. }
  210. /* Given the circuit creation client statistics object, refill the circuit
  211. * bucket if needed. This also works if the bucket was never filled in the
  212. * first place. The addr is only used for logging purposes. */
  213. STATIC void
  214. cc_stats_refill_bucket(cc_client_stats_t *stats, const tor_addr_t *addr)
  215. {
  216. uint32_t new_circuit_bucket_count, circuit_rate = 0, num_token;
  217. time_t now, elapsed_time_last_refill;
  218. tor_assert(stats);
  219. tor_assert(addr);
  220. now = approx_time();
  221. /* We've never filled the bucket so fill it with the maximum being the burst
  222. * and we are done. */
  223. if (stats->last_circ_bucket_refill_ts == 0) {
  224. num_token = dos_cc_circuit_burst;
  225. goto end;
  226. }
  227. /* At this point, we know we might need to add token to the bucket. We'll
  228. * first compute the circuit rate that is how many circuit are we allowed to
  229. * do per second. */
  230. circuit_rate = get_circuit_rate_per_second();
  231. /* How many seconds have elapsed between now and the last refill? */
  232. elapsed_time_last_refill = now - stats->last_circ_bucket_refill_ts;
  233. /* If the elapsed time is below 0 it means our clock jumped backward so in
  234. * that case, lets be safe and fill it up to the maximum. Not filling it
  235. * could trigger a detection for a valid client. Also, if the clock jumped
  236. * negative but we didn't notice until the elapsed time became positive
  237. * again, then we potentially spent many seconds not refilling the bucket
  238. * when we should have been refilling it. But the fact that we didn't notice
  239. * until now means that no circuit creation requests came in during that
  240. * time, so the client doesn't end up punished that much from this hopefully
  241. * rare situation.*/
  242. if (elapsed_time_last_refill < 0) {
  243. /* Dividing the burst by the circuit rate gives us the time span that will
  244. * give us the maximum allowed value of token. */
  245. elapsed_time_last_refill = (dos_cc_circuit_burst / circuit_rate);
  246. }
  247. /* Compute how many circuits we are allowed in that time frame which we'll
  248. * add to the bucket. This can be big but it is cap to a maximum after. */
  249. num_token = elapsed_time_last_refill * circuit_rate;
  250. end:
  251. /* We cap the bucket to the burst value else this could grow to infinity
  252. * over time. */
  253. new_circuit_bucket_count = MIN(stats->circuit_bucket + num_token,
  254. dos_cc_circuit_burst);
  255. log_debug(LD_DOS, "DoS address %s has its circuit bucket value: %" PRIu32
  256. ". Filling it to %" PRIu32 ". Circuit rate is %" PRIu32,
  257. fmt_addr(addr), stats->circuit_bucket, new_circuit_bucket_count,
  258. circuit_rate);
  259. stats->circuit_bucket = new_circuit_bucket_count;
  260. stats->last_circ_bucket_refill_ts = now;
  261. return;
  262. }
  263. /* Return true iff the circuit bucket is down to 0 and the number of
  264. * concurrent connections is greater or equal the minimum threshold set the
  265. * consensus parameter. */
  266. static int
  267. cc_has_exhausted_circuits(const dos_client_stats_t *stats)
  268. {
  269. tor_assert(stats);
  270. return stats->cc_stats.circuit_bucket == 0 &&
  271. stats->concurrent_count >= dos_cc_min_concurrent_conn;
  272. }
  273. /* Mark client address by setting a timestamp in the stats object which tells
  274. * us until when it is marked as positively detected. */
  275. static void
  276. cc_mark_client(cc_client_stats_t *stats)
  277. {
  278. tor_assert(stats);
  279. /* We add a random offset of a maximum of half the defense time so it is
  280. * less predictable. */
  281. stats->marked_until_ts =
  282. approx_time() + dos_cc_defense_time_period +
  283. crypto_rand_int_range(1, dos_cc_defense_time_period / 2);
  284. }
  285. /* Return true iff the given channel address is marked as malicious. This is
  286. * called a lot and part of the fast path of handling cells. It has to remain
  287. * as fast as we can. */
  288. static int
  289. cc_channel_addr_is_marked(channel_t *chan)
  290. {
  291. time_t now;
  292. tor_addr_t addr;
  293. clientmap_entry_t *entry;
  294. cc_client_stats_t *stats = NULL;
  295. if (chan == NULL) {
  296. goto end;
  297. }
  298. /* Must be a client connection else we ignore. */
  299. if (!channel_is_client(chan)) {
  300. goto end;
  301. }
  302. /* Without an IP address, nothing can work. */
  303. if (!channel_get_addr_if_possible(chan, &addr)) {
  304. goto end;
  305. }
  306. /* We are only interested in client connection from the geoip cache. */
  307. entry = geoip_lookup_client(&addr, NULL, GEOIP_CLIENT_CONNECT);
  308. if (entry == NULL) {
  309. /* We can have a connection creating circuits but not tracked by the geoip
  310. * cache. Once this DoS subsystem is enabled, we can end up here with no
  311. * entry for the channel. */
  312. goto end;
  313. }
  314. now = approx_time();
  315. stats = &entry->dos_stats.cc_stats;
  316. end:
  317. return stats && stats->marked_until_ts >= now;
  318. }
  319. /* Concurrent connection private API. */
  320. /* Free everything for the connection DoS mitigation subsystem. */
  321. static void
  322. conn_free_all(void)
  323. {
  324. dos_conn_enabled = 0;
  325. }
  326. /* Called when the consensus has changed. Do appropriate actions for the
  327. * connection mitigation subsystem. */
  328. static void
  329. conn_consensus_has_changed(const networkstatus_t *ns)
  330. {
  331. /* Looking at the consensus, is the connection mitigation subsystem enabled?
  332. * If not and it was enabled before, clean it up. */
  333. if (dos_conn_enabled && !get_param_conn_enabled(ns)) {
  334. conn_free_all();
  335. }
  336. }
  337. /* General private API */
  338. /* Return true iff we have at least one DoS detection enabled. This is used to
  339. * decide if we need to allocate any kind of high level DoS object. */
  340. static inline int
  341. dos_is_enabled(void)
  342. {
  343. return (dos_cc_enabled || dos_conn_enabled);
  344. }
  345. /* Circuit creation public API. */
  346. /* Called when a CREATE cell is received from the given channel. */
  347. void
  348. dos_cc_new_create_cell(channel_t *chan)
  349. {
  350. tor_addr_t addr;
  351. clientmap_entry_t *entry;
  352. tor_assert(chan);
  353. /* Skip everything if not enabled. */
  354. if (!dos_cc_enabled) {
  355. goto end;
  356. }
  357. /* Must be a client connection else we ignore. */
  358. if (!channel_is_client(chan)) {
  359. goto end;
  360. }
  361. /* Without an IP address, nothing can work. */
  362. if (!channel_get_addr_if_possible(chan, &addr)) {
  363. goto end;
  364. }
  365. /* We are only interested in client connection from the geoip cache. */
  366. entry = geoip_lookup_client(&addr, NULL, GEOIP_CLIENT_CONNECT);
  367. if (entry == NULL) {
  368. /* We can have a connection creating circuits but not tracked by the geoip
  369. * cache. Once this DoS subsystem is enabled, we can end up here with no
  370. * entry for the channel. */
  371. goto end;
  372. }
  373. /* General comment. Even though the client can already be marked as
  374. * malicious, we continue to track statistics. If it keeps going above
  375. * threshold while marked, the defense period time will grow longer. There
  376. * is really no point at unmarking a client that keeps DoSing us. */
  377. /* First of all, we'll try to refill the circuit bucket opportunistically
  378. * before we assess. */
  379. cc_stats_refill_bucket(&entry->dos_stats.cc_stats, &addr);
  380. /* Take a token out of the circuit bucket if we are above 0 so we don't
  381. * underflow the bucket. */
  382. if (entry->dos_stats.cc_stats.circuit_bucket > 0) {
  383. entry->dos_stats.cc_stats.circuit_bucket--;
  384. }
  385. /* This is the detection. Assess at every CREATE cell if the client should
  386. * get marked as malicious. This should be kept as fast as possible. */
  387. if (cc_has_exhausted_circuits(&entry->dos_stats)) {
  388. /* If this is the first time we mark this entry, log it a info level.
  389. * Under heavy DDoS, logging each time we mark would results in lots and
  390. * lots of logs. */
  391. if (entry->dos_stats.cc_stats.marked_until_ts == 0) {
  392. log_debug(LD_DOS, "Detected circuit creation DoS by address: %s",
  393. fmt_addr(&addr));
  394. cc_num_marked_addrs++;
  395. }
  396. cc_mark_client(&entry->dos_stats.cc_stats);
  397. }
  398. end:
  399. return;
  400. }
  401. /* Return the defense type that should be used for this circuit.
  402. *
  403. * This is part of the fast path and called a lot. */
  404. dos_cc_defense_type_t
  405. dos_cc_get_defense_type(channel_t *chan)
  406. {
  407. tor_assert(chan);
  408. /* Skip everything if not enabled. */
  409. if (!dos_cc_enabled) {
  410. goto end;
  411. }
  412. /* On an OR circuit, we'll check if the previous channel is a marked client
  413. * connection detected by our DoS circuit creation mitigation subsystem. */
  414. if (cc_channel_addr_is_marked(chan)) {
  415. /* We've just assess that this circuit should trigger a defense for the
  416. * cell it just seen. Note it down. */
  417. cc_num_rejected_cells++;
  418. return dos_cc_defense_type;
  419. }
  420. end:
  421. return DOS_CC_DEFENSE_NONE;
  422. }
  423. /* Concurrent connection detection public API. */
  424. /* General API */
  425. /* Called when a new client connection has been established on the given
  426. * address. */
  427. void
  428. dos_new_client_conn(or_connection_t *or_conn)
  429. {
  430. clientmap_entry_t *entry;
  431. tor_assert(or_conn);
  432. /* Past that point, we know we have at least one DoS detection subsystem
  433. * enabled so we'll start allocating stuff. */
  434. if (!dos_is_enabled()) {
  435. goto end;
  436. }
  437. /* We are only interested in client connection from the geoip cache. */
  438. entry = geoip_lookup_client(&or_conn->real_addr, NULL,
  439. GEOIP_CLIENT_CONNECT);
  440. if (BUG(entry == NULL)) {
  441. /* Should never happen because we note down the address in the geoip
  442. * cache before this is called. */
  443. goto end;
  444. }
  445. entry->dos_stats.concurrent_count++;
  446. or_conn->tracked_for_dos_mitigation = 1;
  447. log_debug(LD_DOS, "Client address %s has now %u concurrent connections.",
  448. fmt_addr(&or_conn->real_addr),
  449. entry->dos_stats.concurrent_count);
  450. end:
  451. return;
  452. }
  453. /* Called when a client connection for the given IP address has been closed. */
  454. void
  455. dos_close_client_conn(const or_connection_t *or_conn)
  456. {
  457. clientmap_entry_t *entry;
  458. tor_assert(or_conn);
  459. /* We have to decrement the count on tracked connection only even if the
  460. * subsystem has been disabled at runtime because it might be re-enabled
  461. * after and we need to keep a synchronized counter at all time. */
  462. if (!or_conn->tracked_for_dos_mitigation) {
  463. goto end;
  464. }
  465. /* We are only interested in client connection from the geoip cache. */
  466. entry = geoip_lookup_client(&or_conn->real_addr, NULL,
  467. GEOIP_CLIENT_CONNECT);
  468. if (entry == NULL) {
  469. /* This can happen because we can close a connection before the channel
  470. * got to be noted down in the geoip cache. */
  471. goto end;
  472. }
  473. /* Extra super duper safety. Going below 0 means an underflow which could
  474. * lead to most likely a false positive. In theory, this should never happen
  475. * but lets be extra safe. */
  476. if (BUG(entry->dos_stats.concurrent_count == 0)) {
  477. goto end;
  478. }
  479. entry->dos_stats.concurrent_count--;
  480. log_debug(LD_DOS, "Client address %s has lost a connection. Concurrent "
  481. "connections are now at %u",
  482. fmt_addr(&or_conn->real_addr),
  483. entry->dos_stats.concurrent_count);
  484. end:
  485. return;
  486. }
  487. /* Called when the consensus has changed. We might have new consensus
  488. * parameters to look at. */
  489. void
  490. dos_consensus_has_changed(const networkstatus_t *ns)
  491. {
  492. cc_consensus_has_changed(ns);
  493. conn_consensus_has_changed(ns);
  494. /* We were already enabled or we just became enabled but either way, set the
  495. * consensus parameters for all subsystems. */
  496. set_dos_parameters(ns);
  497. }
  498. /* Return true iff the DoS mitigation subsystem is enabled. */
  499. int
  500. dos_enabled(void)
  501. {
  502. return dos_is_enabled();
  503. }
  504. /* Free everything from the Denial of Service subsystem. */
  505. void
  506. dos_free_all(void)
  507. {
  508. /* Free the circuit creation mitigation subsystem. It is safe to do this
  509. * even if it wasn't initialized. */
  510. cc_free_all();
  511. /* Free the connection mitigation subsystem. It is safe to do this even if
  512. * it wasn't initialized. */
  513. conn_free_all();
  514. }
  515. /* Initialize the Denial of Service subsystem. */
  516. void
  517. dos_init(void)
  518. {
  519. /* To initialize, we only need to get the parameters. */
  520. set_dos_parameters(NULL);
  521. }