dos.c 18 KB

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