test_dos.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define DOS_PRIVATE
  4. #define TOR_CHANNEL_INTERNAL_
  5. #define CIRCUITLIST_PRIVATE
  6. #include "or.h"
  7. #include "dos.h"
  8. #include "circuitlist.h"
  9. #include "crypto_rand.h"
  10. #include "geoip.h"
  11. #include "channel.h"
  12. #include "microdesc.h"
  13. #include "networkstatus.h"
  14. #include "nodelist.h"
  15. #include "routerlist.h"
  16. #include "test.h"
  17. #include "log_test_helpers.h"
  18. static networkstatus_t *dummy_ns = NULL;
  19. static networkstatus_t *
  20. mock_networkstatus_get_latest_consensus(void)
  21. {
  22. return dummy_ns;
  23. }
  24. static networkstatus_t *
  25. mock_networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  26. {
  27. tor_assert(f == FLAV_MICRODESC);
  28. return dummy_ns;
  29. }
  30. /* Number of address a single node_t can have. Default to the production
  31. * value. This is to control the size of the bloom filter. */
  32. static int addr_per_node = 2;
  33. static int
  34. mock_get_estimated_address_per_node(void)
  35. {
  36. return addr_per_node;
  37. }
  38. static unsigned int
  39. mock_enable_dos_protection(const networkstatus_t *ns)
  40. {
  41. (void) ns;
  42. return 1;
  43. }
  44. /** Test that the connection tracker of the DoS subsystem will block clients
  45. * who try to establish too many connections */
  46. static void
  47. test_dos_conn_creation(void *arg)
  48. {
  49. (void) arg;
  50. MOCK(get_param_cc_enabled, mock_enable_dos_protection);
  51. MOCK(get_param_conn_enabled, mock_enable_dos_protection);
  52. /* Initialize test data */
  53. or_connection_t or_conn;
  54. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  55. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.real_addr,
  56. "18.0.0.1"));
  57. tor_addr_t *addr = &or_conn.real_addr;
  58. /* Get DoS subsystem limits */
  59. dos_init();
  60. uint32_t max_concurrent_conns = get_param_conn_max_concurrent_count(NULL);
  61. /* Introduce new client */
  62. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, addr, NULL, now);
  63. { /* Register many conns from this client but not enough to get it blocked */
  64. unsigned int i;
  65. for (i = 0; i < max_concurrent_conns; i++) {
  66. dos_new_client_conn(&or_conn);
  67. }
  68. }
  69. /* Check that new conns are still permitted */
  70. tt_int_op(DOS_CONN_DEFENSE_NONE, OP_EQ,
  71. dos_conn_addr_get_defense_type(addr));
  72. /* Register another conn and check that new conns are not allowed anymore */
  73. dos_new_client_conn(&or_conn);
  74. tt_int_op(DOS_CONN_DEFENSE_CLOSE, OP_EQ,
  75. dos_conn_addr_get_defense_type(addr));
  76. /* Close a client conn and see that a new conn will be permitted again */
  77. dos_close_client_conn(&or_conn);
  78. tt_int_op(DOS_CONN_DEFENSE_NONE, OP_EQ,
  79. dos_conn_addr_get_defense_type(addr));
  80. /* Register another conn and see that defense measures get reactivated */
  81. dos_new_client_conn(&or_conn);
  82. tt_int_op(DOS_CONN_DEFENSE_CLOSE, OP_EQ,
  83. dos_conn_addr_get_defense_type(addr));
  84. done:
  85. dos_free_all();
  86. }
  87. /** Helper mock: Place a fake IP addr for this channel in <b>addr_out</b> */
  88. static int
  89. mock_channel_get_addr_if_possible(channel_t *chan, tor_addr_t *addr_out)
  90. {
  91. (void)chan;
  92. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(addr_out, "18.0.0.1"));
  93. return 1;
  94. done:
  95. return 0;
  96. }
  97. /** Test that the circuit tracker of the DoS subsystem will block clients who
  98. * try to establish too many circuits. */
  99. static void
  100. test_dos_circuit_creation(void *arg)
  101. {
  102. (void) arg;
  103. unsigned int i;
  104. MOCK(get_param_cc_enabled, mock_enable_dos_protection);
  105. MOCK(get_param_conn_enabled, mock_enable_dos_protection);
  106. MOCK(channel_get_addr_if_possible,
  107. mock_channel_get_addr_if_possible);
  108. /* Initialize channels/conns/circs that will be used */
  109. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  110. channel_init(chan);
  111. chan->is_client = 1;
  112. /* Initialize test data */
  113. or_connection_t or_conn;
  114. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  115. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.real_addr,
  116. "18.0.0.1"));
  117. tor_addr_t *addr = &or_conn.real_addr;
  118. /* Get DoS subsystem limits */
  119. dos_init();
  120. uint32_t max_circuit_count = get_param_cc_circuit_burst(NULL);
  121. uint32_t min_conc_conns_for_cc =
  122. get_param_cc_min_concurrent_connection(NULL);
  123. /* Introduce new client and establish enough connections to activate the
  124. * circuit counting subsystem */
  125. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, addr, NULL, now);
  126. for (i = 0; i < min_conc_conns_for_cc ; i++) {
  127. dos_new_client_conn(&or_conn);
  128. }
  129. /* Register new circuits for this client and conn, but not enough to get
  130. * detected as dos */
  131. for (i=0; i < max_circuit_count-1; i++) {
  132. dos_cc_new_create_cell(chan);
  133. }
  134. /* see that we didn't get detected for dosing */
  135. tt_int_op(DOS_CC_DEFENSE_NONE, OP_EQ, dos_cc_get_defense_type(chan));
  136. /* Register another CREATE cell that will push us over the limit. Check that
  137. * the cell gets refused. */
  138. dos_cc_new_create_cell(chan);
  139. tt_int_op(DOS_CC_DEFENSE_REFUSE_CELL, OP_EQ, dos_cc_get_defense_type(chan));
  140. /* TODO: Wait a few seconds before sending the cell, and check that the
  141. buckets got refilled properly. */
  142. /* TODO: Actually send a Tor cell (instead of calling the DoS function) and
  143. * check that it will get refused */
  144. done:
  145. tor_free(chan);
  146. dos_free_all();
  147. }
  148. /** Test that the DoS subsystem properly refills the circuit token buckets. */
  149. static void
  150. test_dos_bucket_refill(void *arg)
  151. {
  152. (void) arg;
  153. int i;
  154. /* For this test, this variable is set to the current circ count of the token
  155. * bucket. */
  156. uint32_t current_circ_count;
  157. MOCK(get_param_cc_enabled, mock_enable_dos_protection);
  158. MOCK(get_param_conn_enabled, mock_enable_dos_protection);
  159. MOCK(channel_get_addr_if_possible,
  160. mock_channel_get_addr_if_possible);
  161. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  162. update_approx_time(now);
  163. /* Initialize channels/conns/circs that will be used */
  164. channel_t *chan = tor_malloc_zero(sizeof(channel_t));
  165. channel_init(chan);
  166. chan->is_client = 1;
  167. or_connection_t or_conn;
  168. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.real_addr,
  169. "18.0.0.1"));
  170. tor_addr_t *addr = &or_conn.real_addr;
  171. /* Initialize DoS subsystem and get relevant limits */
  172. dos_init();
  173. uint32_t max_circuit_count = get_param_cc_circuit_burst(NULL);
  174. uint64_t circ_rate = get_circuit_rate_per_second();
  175. /* Check that the circuit rate is a positive number and smaller than the max
  176. * circuit count */
  177. tt_u64_op(circ_rate, OP_GT, 1);
  178. tt_u64_op(circ_rate, OP_LT, max_circuit_count);
  179. /* Register this client */
  180. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, addr, NULL, now);
  181. dos_new_client_conn(&or_conn);
  182. /* Fetch this client from the geoip cache and get its DoS structs */
  183. clientmap_entry_t *entry = geoip_lookup_client(addr, NULL,
  184. GEOIP_CLIENT_CONNECT);
  185. tt_assert(entry);
  186. dos_client_stats_t* dos_stats = &entry->dos_stats;
  187. /* Check that the circuit bucket is still uninitialized */
  188. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, 0);
  189. /* Send a create cell: then check that the circ token bucket got initialized
  190. * and one circ was subtracted. */
  191. dos_cc_new_create_cell(chan);
  192. current_circ_count = max_circuit_count - 1;
  193. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  194. /* Now send 29 more CREATEs and ensure that the bucket is missing 30
  195. * tokens */
  196. for (i=0; i < 29; i++) {
  197. dos_cc_new_create_cell(chan);
  198. current_circ_count--;
  199. }
  200. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  201. /* OK! Progress time forward one sec, refill the bucket and check that the
  202. * refill happened correctly. */
  203. now += 1;
  204. update_approx_time(now);
  205. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  206. /* check refill */
  207. current_circ_count += circ_rate;
  208. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  209. /* Now send as many CREATE cells as needed to deplete our token bucket
  210. * completely */
  211. for (; current_circ_count != 0; current_circ_count--) {
  212. dos_cc_new_create_cell(chan);
  213. }
  214. tt_uint_op(current_circ_count, OP_EQ, 0);
  215. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  216. /* Now progress time a week forward, and check that the token bucket does not
  217. * have more than max_circs allowance, even tho we let it simmer for so
  218. * long. */
  219. now += 604800; /* a week */
  220. update_approx_time(now);
  221. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  222. current_circ_count += max_circuit_count;
  223. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  224. /* Now send as many CREATE cells as needed to deplete our token bucket
  225. * completely */
  226. for (; current_circ_count != 0; current_circ_count--) {
  227. dos_cc_new_create_cell(chan);
  228. }
  229. tt_uint_op(current_circ_count, OP_EQ, 0);
  230. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  231. /* Now use a very large time, and check that the token bucket does not have
  232. * more than max_circs allowance, even tho we let it simmer for so long. */
  233. now = INT32_MAX; /* 2038? */
  234. update_approx_time(now);
  235. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  236. current_circ_count += max_circuit_count;
  237. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  238. /* Now send as many CREATE cells as needed to deplete our token bucket
  239. * completely */
  240. for (; current_circ_count != 0; current_circ_count--) {
  241. dos_cc_new_create_cell(chan);
  242. }
  243. tt_uint_op(current_circ_count, OP_EQ, 0);
  244. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  245. /* Now use a very small time, and check that the token bucket has exactly
  246. * the max_circs allowance, because backward clock jumps are rare. */
  247. now = INT32_MIN; /* 19?? */
  248. update_approx_time(now);
  249. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  250. current_circ_count += max_circuit_count;
  251. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  252. /* Now send as many CREATE cells as needed to deplete our token bucket
  253. * completely */
  254. for (; current_circ_count != 0; current_circ_count--) {
  255. dos_cc_new_create_cell(chan);
  256. }
  257. tt_uint_op(current_circ_count, OP_EQ, 0);
  258. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  259. /* Progress time forward one sec again, refill the bucket and check that the
  260. * refill happened correctly. */
  261. now += 1;
  262. update_approx_time(now);
  263. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  264. /* check refill */
  265. current_circ_count += circ_rate;
  266. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  267. /* Now send as many CREATE cells as needed to deplete our token bucket
  268. * completely */
  269. for (; current_circ_count != 0; current_circ_count--) {
  270. dos_cc_new_create_cell(chan);
  271. }
  272. tt_uint_op(current_circ_count, OP_EQ, 0);
  273. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  274. /* Now use a very large time (again), and check that the token bucket does
  275. * not have more than max_circs allowance, even tho we let it simmer for so
  276. * long. */
  277. now = INT32_MAX; /* 2038? */
  278. update_approx_time(now);
  279. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  280. current_circ_count += max_circuit_count;
  281. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  282. /* Now send as many CREATE cells as needed to deplete our token bucket
  283. * completely */
  284. for (; current_circ_count != 0; current_circ_count--) {
  285. dos_cc_new_create_cell(chan);
  286. }
  287. tt_uint_op(current_circ_count, OP_EQ, 0);
  288. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  289. /* This code resets the time to zero with 32-bit time_t, which triggers the
  290. * code that initialises the bucket. */
  291. #if SIZEOF_TIME_T == 8
  292. /* Now use a very very small time, and check that the token bucket has
  293. * exactly the max_circs allowance, because backward clock jumps are rare.
  294. */
  295. now = (time_t)INT64_MIN; /* ???? */
  296. update_approx_time(now);
  297. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  298. current_circ_count += max_circuit_count;
  299. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  300. /* Now send as many CREATE cells as needed to deplete our token bucket
  301. * completely */
  302. for (; current_circ_count != 0; current_circ_count--) {
  303. dos_cc_new_create_cell(chan);
  304. }
  305. tt_uint_op(current_circ_count, OP_EQ, 0);
  306. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  307. /* Progress time forward one sec again, refill the bucket and check that the
  308. * refill happened correctly. */
  309. now += 1;
  310. update_approx_time(now);
  311. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  312. /* check refill */
  313. current_circ_count += circ_rate;
  314. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  315. /* Now send as many CREATE cells as needed to deplete our token bucket
  316. * completely */
  317. for (; current_circ_count != 0; current_circ_count--) {
  318. dos_cc_new_create_cell(chan);
  319. }
  320. tt_uint_op(current_circ_count, OP_EQ, 0);
  321. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  322. /* Now use a very very small time, and check that the token bucket has
  323. * exactly the max_circs allowance, because backward clock jumps are rare.
  324. */
  325. now = (time_t)INT64_MIN; /* ???? */
  326. update_approx_time(now);
  327. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  328. current_circ_count += max_circuit_count;
  329. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  330. /* Now send as many CREATE cells as needed to deplete our token bucket
  331. * completely */
  332. for (; current_circ_count != 0; current_circ_count--) {
  333. dos_cc_new_create_cell(chan);
  334. }
  335. tt_uint_op(current_circ_count, OP_EQ, 0);
  336. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  337. /* Now use a very very large time, and check that the token bucket does not
  338. * have more than max_circs allowance, even tho we let it simmer for so
  339. * long. */
  340. now = (time_t)INT64_MAX; /* ???? */
  341. update_approx_time(now);
  342. cc_stats_refill_bucket(&dos_stats->cc_stats, addr);
  343. current_circ_count += max_circuit_count;
  344. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  345. /* Now send as many CREATE cells as needed to deplete our token bucket
  346. * completely */
  347. for (; current_circ_count != 0; current_circ_count--) {
  348. dos_cc_new_create_cell(chan);
  349. }
  350. tt_uint_op(current_circ_count, OP_EQ, 0);
  351. tt_uint_op(dos_stats->cc_stats.circuit_bucket, OP_EQ, current_circ_count);
  352. #endif
  353. done:
  354. tor_free(chan);
  355. dos_free_all();
  356. }
  357. /* Test if we avoid counting a known relay. */
  358. static void
  359. test_known_relay(void *arg)
  360. {
  361. clientmap_entry_t *entry = NULL;
  362. routerstatus_t *rs = NULL; microdesc_t *md = NULL; routerinfo_t *ri = NULL;
  363. (void) arg;
  364. MOCK(networkstatus_get_latest_consensus,
  365. mock_networkstatus_get_latest_consensus);
  366. MOCK(networkstatus_get_latest_consensus_by_flavor,
  367. mock_networkstatus_get_latest_consensus_by_flavor);
  368. MOCK(get_estimated_address_per_node,
  369. mock_get_estimated_address_per_node);
  370. MOCK(get_param_cc_enabled, mock_enable_dos_protection);
  371. dos_init();
  372. dummy_ns = tor_malloc_zero(sizeof(*dummy_ns));
  373. dummy_ns->flavor = FLAV_MICRODESC;
  374. dummy_ns->routerstatus_list = smartlist_new();
  375. /* Setup an OR conn so we can pass it to the DoS subsystem. */
  376. or_connection_t or_conn;
  377. tor_addr_parse(&or_conn.real_addr, "42.42.42.42");
  378. rs = tor_malloc_zero(sizeof(*rs));
  379. rs->addr = tor_addr_to_ipv4h(&or_conn.real_addr);
  380. crypto_rand(rs->identity_digest, sizeof(rs->identity_digest));
  381. smartlist_add(dummy_ns->routerstatus_list, rs);
  382. /* This will make the nodelist bloom filter very large
  383. * (the_nodelist->node_addrs) so we will fail the contain test rarely. */
  384. addr_per_node = 1024;
  385. nodelist_set_consensus(dummy_ns);
  386. /* We have now a node in our list so we'll make sure we don't count it as a
  387. * client connection. */
  388. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &or_conn.real_addr, NULL, 0);
  389. /* Suppose we have 5 connections in rapid succession, the counter should
  390. * always be 0 because we should ignore this. */
  391. dos_new_client_conn(&or_conn);
  392. dos_new_client_conn(&or_conn);
  393. dos_new_client_conn(&or_conn);
  394. dos_new_client_conn(&or_conn);
  395. dos_new_client_conn(&or_conn);
  396. entry = geoip_lookup_client(&or_conn.real_addr, NULL, GEOIP_CLIENT_CONNECT);
  397. tt_assert(entry);
  398. /* We should have a count of 0. */
  399. tt_uint_op(entry->dos_stats.concurrent_count, OP_EQ, 0);
  400. /* To make sure that his is working properly, make a unknown client
  401. * connection and see if we do get it. */
  402. tor_addr_parse(&or_conn.real_addr, "42.42.42.43");
  403. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &or_conn.real_addr, NULL, 0);
  404. dos_new_client_conn(&or_conn);
  405. dos_new_client_conn(&or_conn);
  406. entry = geoip_lookup_client(&or_conn.real_addr, NULL, GEOIP_CLIENT_CONNECT);
  407. tt_assert(entry);
  408. /* We should have a count of 2. */
  409. tt_uint_op(entry->dos_stats.concurrent_count, OP_EQ, 2);
  410. done:
  411. routerstatus_free(rs); routerinfo_free(ri); microdesc_free(md);
  412. smartlist_clear(dummy_ns->routerstatus_list);
  413. networkstatus_vote_free(dummy_ns);
  414. dos_free_all();
  415. UNMOCK(networkstatus_get_latest_consensus);
  416. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  417. UNMOCK(get_estimated_address_per_node);
  418. UNMOCK(get_param_cc_enabled);
  419. }
  420. struct testcase_t dos_tests[] = {
  421. { "conn_creation", test_dos_conn_creation, TT_FORK, NULL, NULL },
  422. { "circuit_creation", test_dos_circuit_creation, TT_FORK, NULL, NULL },
  423. { "bucket_refill", test_dos_bucket_refill, TT_FORK, NULL, NULL },
  424. { "known_relay" , test_known_relay, TT_FORK,
  425. NULL, NULL },
  426. END_OF_TESTCASES
  427. };