test_policy.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /* Copyright (c) 2013-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #define CONFIG_PRIVATE
  5. #include "config.h"
  6. #include "router.h"
  7. #include "routerparse.h"
  8. #define POLICIES_PRIVATE
  9. #include "policies.h"
  10. #include "test.h"
  11. /* Helper: assert that short_policy parses and writes back out as itself,
  12. or as <b>expected</b> if that's provided. */
  13. static void
  14. test_short_policy_parse(const char *input,
  15. const char *expected)
  16. {
  17. short_policy_t *short_policy = NULL;
  18. char *out = NULL;
  19. if (expected == NULL)
  20. expected = input;
  21. short_policy = parse_short_policy(input);
  22. tt_assert(short_policy);
  23. out = write_short_policy(short_policy);
  24. tt_str_op(out, OP_EQ, expected);
  25. done:
  26. tor_free(out);
  27. short_policy_free(short_policy);
  28. }
  29. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  30. * that policies_summarize() produces the string <b>expected_summary</b> from
  31. * it. */
  32. static void
  33. test_policy_summary_helper(const char *policy_str,
  34. const char *expected_summary)
  35. {
  36. config_line_t line;
  37. smartlist_t *policy = smartlist_new();
  38. char *summary = NULL;
  39. char *summary_after = NULL;
  40. int r;
  41. short_policy_t *short_policy = NULL;
  42. line.key = (char*)"foo";
  43. line.value = (char *)policy_str;
  44. line.next = NULL;
  45. r = policies_parse_exit_policy(&line, &policy,
  46. EXIT_POLICY_IPV6_ENABLED |
  47. EXIT_POLICY_ADD_DEFAULT, NULL);
  48. tt_int_op(r,OP_EQ, 0);
  49. summary = policy_summarize(policy, AF_INET);
  50. tt_assert(summary != NULL);
  51. tt_str_op(summary,OP_EQ, expected_summary);
  52. short_policy = parse_short_policy(summary);
  53. tt_assert(short_policy);
  54. summary_after = write_short_policy(short_policy);
  55. tt_str_op(summary,OP_EQ, summary_after);
  56. done:
  57. tor_free(summary_after);
  58. tor_free(summary);
  59. if (policy)
  60. addr_policy_list_free(policy);
  61. short_policy_free(short_policy);
  62. }
  63. /** Run unit tests for generating summary lines of exit policies */
  64. static void
  65. test_policies_general(void *arg)
  66. {
  67. int i;
  68. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  69. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  70. *policy7 = NULL, *policy8 = NULL, *policy9 = NULL,
  71. *policy10 = NULL, *policy11 = NULL, *policy12 = NULL;
  72. addr_policy_t *p;
  73. tor_addr_t tar, tar2;
  74. smartlist_t *addr_list = NULL;
  75. config_line_t line;
  76. smartlist_t *sm = NULL;
  77. char *policy_str = NULL;
  78. short_policy_t *short_parsed = NULL;
  79. int malformed_list = -1;
  80. (void)arg;
  81. policy = smartlist_new();
  82. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*", -1,
  83. &malformed_list);
  84. tt_assert(p != NULL);
  85. tt_int_op(ADDR_POLICY_REJECT,OP_EQ, p->policy_type);
  86. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  87. tt_int_op(0,OP_EQ, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  88. tt_int_op(16,OP_EQ, p->maskbits);
  89. tt_int_op(1,OP_EQ, p->prt_min);
  90. tt_int_op(65535,OP_EQ, p->prt_max);
  91. smartlist_add(policy, p);
  92. tor_addr_from_ipv4h(&tar, 0x01020304u);
  93. tt_assert(ADDR_POLICY_ACCEPTED ==
  94. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  95. tor_addr_make_unspec(&tar);
  96. tt_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  97. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  98. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  99. tt_assert(ADDR_POLICY_REJECTED ==
  100. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  101. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy2,
  102. EXIT_POLICY_IPV6_ENABLED |
  103. EXIT_POLICY_REJECT_PRIVATE |
  104. EXIT_POLICY_ADD_DEFAULT, NULL));
  105. tt_assert(policy2);
  106. tor_addr_from_ipv4h(&tar, 0x0306090cu);
  107. tor_addr_parse(&tar2, "[2000::1234]");
  108. addr_list = smartlist_new();
  109. smartlist_add(addr_list, &tar);
  110. smartlist_add(addr_list, &tar2);
  111. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy12,
  112. EXIT_POLICY_IPV6_ENABLED |
  113. EXIT_POLICY_REJECT_PRIVATE |
  114. EXIT_POLICY_ADD_DEFAULT,
  115. addr_list));
  116. smartlist_free(addr_list);
  117. addr_list = NULL;
  118. tt_assert(policy12);
  119. policy3 = smartlist_new();
  120. p = router_parse_addr_policy_item_from_string("reject *:*", -1,
  121. &malformed_list);
  122. tt_assert(p != NULL);
  123. smartlist_add(policy3, p);
  124. p = router_parse_addr_policy_item_from_string("accept *:*", -1,
  125. &malformed_list);
  126. tt_assert(p != NULL);
  127. smartlist_add(policy3, p);
  128. policy4 = smartlist_new();
  129. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  130. &malformed_list);
  131. tt_assert(p != NULL);
  132. smartlist_add(policy4, p);
  133. p = router_parse_addr_policy_item_from_string("accept *:443", -1,
  134. &malformed_list);
  135. tt_assert(p != NULL);
  136. smartlist_add(policy4, p);
  137. policy5 = smartlist_new();
  138. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*", -1,
  139. &malformed_list);
  140. tt_assert(p != NULL);
  141. smartlist_add(policy5, p);
  142. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*", -1,
  143. &malformed_list);
  144. tt_assert(p != NULL);
  145. smartlist_add(policy5, p);
  146. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*", -1,
  147. &malformed_list);
  148. tt_assert(p != NULL);
  149. smartlist_add(policy5, p);
  150. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",
  151. -1, &malformed_list);
  152. tt_assert(p != NULL);
  153. smartlist_add(policy5, p);
  154. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*", -1,
  155. &malformed_list);
  156. tt_assert(p != NULL);
  157. smartlist_add(policy5, p);
  158. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*", -1,
  159. &malformed_list);
  160. tt_assert(p != NULL);
  161. smartlist_add(policy5, p);
  162. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*", -1,
  163. &malformed_list);
  164. tt_assert(p != NULL);
  165. smartlist_add(policy5, p);
  166. p = router_parse_addr_policy_item_from_string("reject *:1-65534", -1,
  167. &malformed_list);
  168. tt_assert(p != NULL);
  169. smartlist_add(policy5, p);
  170. p = router_parse_addr_policy_item_from_string("reject *:65535", -1,
  171. &malformed_list);
  172. tt_assert(p != NULL);
  173. smartlist_add(policy5, p);
  174. p = router_parse_addr_policy_item_from_string("accept *:1-65535", -1,
  175. &malformed_list);
  176. tt_assert(p != NULL);
  177. smartlist_add(policy5, p);
  178. policy6 = smartlist_new();
  179. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*", -1,
  180. &malformed_list);
  181. tt_assert(p != NULL);
  182. smartlist_add(policy6, p);
  183. policy7 = smartlist_new();
  184. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*", -1,
  185. &malformed_list);
  186. tt_assert(p != NULL);
  187. smartlist_add(policy7, p);
  188. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy8,
  189. EXIT_POLICY_IPV6_ENABLED |
  190. EXIT_POLICY_REJECT_PRIVATE |
  191. EXIT_POLICY_ADD_DEFAULT,
  192. NULL));
  193. tt_assert(policy8);
  194. tt_int_op(0, OP_EQ, policies_parse_exit_policy(NULL, &policy9,
  195. EXIT_POLICY_REJECT_PRIVATE |
  196. EXIT_POLICY_ADD_DEFAULT,
  197. NULL));
  198. tt_assert(policy9);
  199. /* accept6 * and reject6 * produce IPv6 wildcards only */
  200. policy10 = smartlist_new();
  201. p = router_parse_addr_policy_item_from_string("accept6 *:*", -1,
  202. &malformed_list);
  203. tt_assert(p != NULL);
  204. smartlist_add(policy10, p);
  205. policy11 = smartlist_new();
  206. p = router_parse_addr_policy_item_from_string("reject6 *:*", -1,
  207. &malformed_list);
  208. tt_assert(p != NULL);
  209. smartlist_add(policy11, p);
  210. tt_assert(!exit_policy_is_general_exit(policy));
  211. tt_assert(exit_policy_is_general_exit(policy2));
  212. tt_assert(!exit_policy_is_general_exit(NULL));
  213. tt_assert(!exit_policy_is_general_exit(policy3));
  214. tt_assert(!exit_policy_is_general_exit(policy4));
  215. tt_assert(!exit_policy_is_general_exit(policy5));
  216. tt_assert(!exit_policy_is_general_exit(policy6));
  217. tt_assert(!exit_policy_is_general_exit(policy7));
  218. tt_assert(exit_policy_is_general_exit(policy8));
  219. tt_assert(exit_policy_is_general_exit(policy9));
  220. tt_assert(!exit_policy_is_general_exit(policy10));
  221. tt_assert(!exit_policy_is_general_exit(policy11));
  222. tt_assert(cmp_addr_policies(policy, policy2));
  223. tt_assert(cmp_addr_policies(policy, NULL));
  224. tt_assert(!cmp_addr_policies(policy2, policy2));
  225. tt_assert(!cmp_addr_policies(NULL, NULL));
  226. tt_assert(!policy_is_reject_star(policy2, AF_INET));
  227. tt_assert(policy_is_reject_star(policy, AF_INET));
  228. tt_assert(policy_is_reject_star(policy10, AF_INET));
  229. tt_assert(!policy_is_reject_star(policy10, AF_INET6));
  230. tt_assert(policy_is_reject_star(policy11, AF_INET));
  231. tt_assert(policy_is_reject_star(policy11, AF_INET6));
  232. tt_assert(policy_is_reject_star(NULL, AF_INET));
  233. tt_assert(policy_is_reject_star(NULL, AF_INET6));
  234. addr_policy_list_free(policy);
  235. policy = NULL;
  236. /* make sure compacting logic works. */
  237. policy = NULL;
  238. line.key = (char*)"foo";
  239. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  240. line.next = NULL;
  241. tt_int_op(0, OP_EQ, policies_parse_exit_policy(&line,&policy,
  242. EXIT_POLICY_IPV6_ENABLED |
  243. EXIT_POLICY_ADD_DEFAULT, NULL));
  244. tt_assert(policy);
  245. //test_streq(policy->string, "accept *:80");
  246. //test_streq(policy->next->string, "reject *:*");
  247. tt_int_op(smartlist_len(policy),OP_EQ, 4);
  248. /* test policy summaries */
  249. /* check if we properly ignore private IP addresses */
  250. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  251. "reject 0.0.0.0/8:*,"
  252. "reject 10.0.0.0/8:*,"
  253. "accept *:10-30,"
  254. "accept *:90,"
  255. "reject *:*",
  256. "accept 10-30,90");
  257. /* check all accept policies, and proper counting of rejects */
  258. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  259. "reject 12.0.0.0/9:80,"
  260. "reject 13.0.0.0/9:80,"
  261. "reject 14.0.0.0/9:80,"
  262. "accept *:*", "accept 1-65535");
  263. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  264. "reject 12.0.0.0/9:80,"
  265. "reject 13.0.0.0/9:80,"
  266. "reject 14.0.0.0/9:80,"
  267. "reject 15.0.0.0:81,"
  268. "accept *:*", "accept 1-65535");
  269. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  270. "reject 12.0.0.0/9:80,"
  271. "reject 13.0.0.0/9:80,"
  272. "reject 14.0.0.0/9:80,"
  273. "reject 15.0.0.0:80,"
  274. "accept *:*",
  275. "reject 80");
  276. /* no exits */
  277. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  278. "reject *:*",
  279. "reject 1-65535");
  280. /* port merging */
  281. test_policy_summary_helper("accept *:80,"
  282. "accept *:81,"
  283. "accept *:100-110,"
  284. "accept *:111,"
  285. "reject *:*",
  286. "accept 80-81,100-111");
  287. /* border ports */
  288. test_policy_summary_helper("accept *:1,"
  289. "accept *:3,"
  290. "accept *:65535,"
  291. "reject *:*",
  292. "accept 1,3,65535");
  293. /* holes */
  294. test_policy_summary_helper("accept *:1,"
  295. "accept *:3,"
  296. "accept *:5,"
  297. "accept *:7,"
  298. "reject *:*",
  299. "accept 1,3,5,7");
  300. test_policy_summary_helper("reject *:1,"
  301. "reject *:3,"
  302. "reject *:5,"
  303. "reject *:7,"
  304. "accept *:*",
  305. "reject 1,3,5,7");
  306. /* Short policies with unrecognized formats should get accepted. */
  307. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  308. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  309. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  310. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  311. /* Short policies with nil entries are accepted too. */
  312. test_short_policy_parse("accept 1,,3", "accept 1,3");
  313. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  314. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  315. /* Try parsing various broken short policies */
  316. #define TT_BAD_SHORT_POLICY(s) \
  317. do { \
  318. tt_ptr_op(NULL, OP_EQ, (short_parsed = parse_short_policy((s)))); \
  319. } while (0)
  320. TT_BAD_SHORT_POLICY("accept 200-199");
  321. TT_BAD_SHORT_POLICY("");
  322. TT_BAD_SHORT_POLICY("rejekt 1,2,3");
  323. TT_BAD_SHORT_POLICY("reject ");
  324. TT_BAD_SHORT_POLICY("reject");
  325. TT_BAD_SHORT_POLICY("rej");
  326. TT_BAD_SHORT_POLICY("accept 2,3,100000");
  327. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  328. TT_BAD_SHORT_POLICY("accept 2,3x,4");
  329. TT_BAD_SHORT_POLICY("accept 2-");
  330. TT_BAD_SHORT_POLICY("accept 2-x");
  331. TT_BAD_SHORT_POLICY("accept 1-,3");
  332. TT_BAD_SHORT_POLICY("accept 1-,3");
  333. /* Make sure that IPv4 addresses are ignored in accept6/reject6 lines. */
  334. p = router_parse_addr_policy_item_from_string("accept6 1.2.3.4:*", -1,
  335. &malformed_list);
  336. tt_assert(p == NULL);
  337. tt_assert(!malformed_list);
  338. p = router_parse_addr_policy_item_from_string("reject6 2.4.6.0/24:*", -1,
  339. &malformed_list);
  340. tt_assert(p == NULL);
  341. tt_assert(!malformed_list);
  342. p = router_parse_addr_policy_item_from_string("accept6 *4:*", -1,
  343. &malformed_list);
  344. tt_assert(p == NULL);
  345. tt_assert(!malformed_list);
  346. /* Make sure malformed policies are detected as such. */
  347. p = router_parse_addr_policy_item_from_string("bad_token *4:*", -1,
  348. &malformed_list);
  349. tt_assert(p == NULL);
  350. tt_assert(malformed_list);
  351. p = router_parse_addr_policy_item_from_string("accept6 **:*", -1,
  352. &malformed_list);
  353. tt_assert(p == NULL);
  354. tt_assert(malformed_list);
  355. p = router_parse_addr_policy_item_from_string("accept */15:*", -1,
  356. &malformed_list);
  357. tt_assert(p == NULL);
  358. tt_assert(malformed_list);
  359. p = router_parse_addr_policy_item_from_string("reject6 */:*", -1,
  360. &malformed_list);
  361. tt_assert(p == NULL);
  362. tt_assert(malformed_list);
  363. p = router_parse_addr_policy_item_from_string("accept 127.0.0.1/33:*", -1,
  364. &malformed_list);
  365. tt_assert(p == NULL);
  366. tt_assert(malformed_list);
  367. p = router_parse_addr_policy_item_from_string("accept6 [::1]/129:*", -1,
  368. &malformed_list);
  369. tt_assert(p == NULL);
  370. tt_assert(malformed_list);
  371. p = router_parse_addr_policy_item_from_string("reject 8.8.8.8/-1:*", -1,
  372. &malformed_list);
  373. tt_assert(p == NULL);
  374. tt_assert(malformed_list);
  375. p = router_parse_addr_policy_item_from_string("reject 8.8.4.4:10-5", -1,
  376. &malformed_list);
  377. tt_assert(p == NULL);
  378. tt_assert(malformed_list);
  379. p = router_parse_addr_policy_item_from_string("reject 1.2.3.4:-1", -1,
  380. &malformed_list);
  381. tt_assert(p == NULL);
  382. tt_assert(malformed_list);
  383. /* Test a too-long policy. */
  384. {
  385. int i;
  386. char *policy = NULL;
  387. smartlist_t *chunks = smartlist_new();
  388. smartlist_add(chunks, tor_strdup("accept "));
  389. for (i=1; i<10000; ++i)
  390. smartlist_add_asprintf(chunks, "%d,", i);
  391. smartlist_add(chunks, tor_strdup("20000"));
  392. policy = smartlist_join_strings(chunks, "", 0, NULL);
  393. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  394. smartlist_free(chunks);
  395. short_parsed = parse_short_policy(policy);/* shouldn't be accepted */
  396. tor_free(policy);
  397. tt_ptr_op(NULL, OP_EQ, short_parsed);
  398. }
  399. /* truncation ports */
  400. sm = smartlist_new();
  401. for (i=1; i<2000; i+=2) {
  402. char buf[POLICY_BUF_LEN];
  403. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  404. smartlist_add(sm, tor_strdup(buf));
  405. }
  406. smartlist_add(sm, tor_strdup("accept *:*"));
  407. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  408. test_policy_summary_helper( policy_str,
  409. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  410. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  411. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  412. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  413. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  414. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  415. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  416. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  417. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  418. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  419. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  420. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  421. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  422. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  423. done:
  424. addr_policy_list_free(policy);
  425. addr_policy_list_free(policy2);
  426. addr_policy_list_free(policy3);
  427. addr_policy_list_free(policy4);
  428. addr_policy_list_free(policy5);
  429. addr_policy_list_free(policy6);
  430. addr_policy_list_free(policy7);
  431. addr_policy_list_free(policy8);
  432. addr_policy_list_free(policy9);
  433. addr_policy_list_free(policy10);
  434. addr_policy_list_free(policy11);
  435. addr_policy_list_free(policy12);
  436. tor_free(policy_str);
  437. if (sm) {
  438. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  439. smartlist_free(sm);
  440. }
  441. short_policy_free(short_parsed);
  442. }
  443. /** Helper: Check that policy_list contains address */
  444. static int
  445. test_policy_has_address_helper(const smartlist_t *policy_list,
  446. const tor_addr_t *addr)
  447. {
  448. int found = 0;
  449. tt_assert(policy_list);
  450. tt_assert(addr);
  451. SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t*, p) {
  452. if (tor_addr_eq(&p->addr, addr)) {
  453. found = 1;
  454. }
  455. } SMARTLIST_FOREACH_END(p);
  456. return found;
  457. done:
  458. return 0;
  459. }
  460. #define TEST_IPV4_ADDR (0x01020304)
  461. #define TEST_IPV6_ADDR ("2002::abcd")
  462. /** Run unit tests for rejecting the configured addresses on this exit relay
  463. * using policies_parse_exit_policy_reject_private */
  464. static void
  465. test_policies_reject_exit_address(void *arg)
  466. {
  467. smartlist_t *policy = NULL;
  468. tor_addr_t ipv4_addr, ipv6_addr;
  469. smartlist_t *ipv4_list, *ipv6_list, *both_list, *dupl_list;
  470. (void)arg;
  471. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  472. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  473. ipv4_list = smartlist_new();
  474. ipv6_list = smartlist_new();
  475. both_list = smartlist_new();
  476. dupl_list = smartlist_new();
  477. smartlist_add(ipv4_list, &ipv4_addr);
  478. smartlist_add(both_list, &ipv4_addr);
  479. smartlist_add(dupl_list, &ipv4_addr);
  480. smartlist_add(dupl_list, &ipv4_addr);
  481. smartlist_add(dupl_list, &ipv4_addr);
  482. smartlist_add(ipv6_list, &ipv6_addr);
  483. smartlist_add(both_list, &ipv6_addr);
  484. smartlist_add(dupl_list, &ipv6_addr);
  485. smartlist_add(dupl_list, &ipv6_addr);
  486. /* IPv4-Only Exits */
  487. /* test that IPv4 addresses are rejected on an IPv4-only exit */
  488. policies_parse_exit_policy_reject_private(&policy, 0, ipv4_list, 0, 0);
  489. tt_assert(policy);
  490. tt_assert(smartlist_len(policy) == 1);
  491. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  492. addr_policy_list_free(policy);
  493. policy = NULL;
  494. /* test that IPv6 addresses are NOT rejected on an IPv4-only exit
  495. * (all IPv6 addresses are rejected by policies_parse_exit_policy_internal
  496. * on IPv4-only exits, so policies_parse_exit_policy_reject_private doesn't
  497. * need to do anything) */
  498. policies_parse_exit_policy_reject_private(&policy, 0, ipv6_list, 0, 0);
  499. tt_assert(policy == NULL);
  500. /* test that only IPv4 addresses are rejected on an IPv4-only exit */
  501. policies_parse_exit_policy_reject_private(&policy, 0, both_list, 0, 0);
  502. tt_assert(policy);
  503. tt_assert(smartlist_len(policy) == 1);
  504. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  505. addr_policy_list_free(policy);
  506. policy = NULL;
  507. /* Test that lists with duplicate entries produce the same results */
  508. policies_parse_exit_policy_reject_private(&policy, 0, dupl_list, 0, 0);
  509. tt_assert(policy);
  510. tt_assert(smartlist_len(policy) == 1);
  511. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  512. addr_policy_list_free(policy);
  513. policy = NULL;
  514. /* IPv4/IPv6 Exits */
  515. /* test that IPv4 addresses are rejected on an IPv4/IPv6 exit */
  516. policies_parse_exit_policy_reject_private(&policy, 1, ipv4_list, 0, 0);
  517. tt_assert(policy);
  518. tt_assert(smartlist_len(policy) == 1);
  519. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  520. addr_policy_list_free(policy);
  521. policy = NULL;
  522. /* test that IPv6 addresses are rejected on an IPv4/IPv6 exit */
  523. policies_parse_exit_policy_reject_private(&policy, 1, ipv6_list, 0, 0);
  524. tt_assert(policy);
  525. tt_assert(smartlist_len(policy) == 1);
  526. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  527. addr_policy_list_free(policy);
  528. policy = NULL;
  529. /* test that IPv4 and IPv6 addresses are rejected on an IPv4/IPv6 exit */
  530. policies_parse_exit_policy_reject_private(&policy, 1, both_list, 0, 0);
  531. tt_assert(policy);
  532. tt_assert(smartlist_len(policy) == 2);
  533. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  534. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  535. addr_policy_list_free(policy);
  536. policy = NULL;
  537. /* Test that lists with duplicate entries produce the same results */
  538. policies_parse_exit_policy_reject_private(&policy, 1, dupl_list, 0, 0);
  539. tt_assert(policy);
  540. tt_assert(smartlist_len(policy) == 2);
  541. tt_assert(test_policy_has_address_helper(policy, &ipv4_addr));
  542. tt_assert(test_policy_has_address_helper(policy, &ipv6_addr));
  543. addr_policy_list_free(policy);
  544. policy = NULL;
  545. done:
  546. addr_policy_list_free(policy);
  547. smartlist_free(ipv4_list);
  548. smartlist_free(ipv6_list);
  549. smartlist_free(both_list);
  550. smartlist_free(dupl_list);
  551. }
  552. static smartlist_t *test_configured_ports = NULL;
  553. const smartlist_t *mock_get_configured_ports(void);
  554. /** Returns test_configured_ports */
  555. const smartlist_t *
  556. mock_get_configured_ports(void)
  557. {
  558. return test_configured_ports;
  559. }
  560. /** Run unit tests for rejecting publicly routable configured port addresses
  561. * on this exit relay using policies_parse_exit_policy_reject_private */
  562. static void
  563. test_policies_reject_port_address(void *arg)
  564. {
  565. smartlist_t *policy = NULL;
  566. port_cfg_t *ipv4_port = NULL;
  567. port_cfg_t *ipv6_port = NULL;
  568. (void)arg;
  569. test_configured_ports = smartlist_new();
  570. ipv4_port = port_cfg_new(0);
  571. tor_addr_from_ipv4h(&ipv4_port->addr, TEST_IPV4_ADDR);
  572. smartlist_add(test_configured_ports, ipv4_port);
  573. ipv6_port = port_cfg_new(0);
  574. tor_addr_parse(&ipv6_port->addr, TEST_IPV6_ADDR);
  575. smartlist_add(test_configured_ports, ipv6_port);
  576. MOCK(get_configured_ports, mock_get_configured_ports);
  577. /* test that an IPv4 port is rejected on an IPv4-only exit, but an IPv6 port
  578. * is NOT rejected (all IPv6 addresses are rejected by
  579. * policies_parse_exit_policy_internal on IPv4-only exits, so
  580. * policies_parse_exit_policy_reject_private doesn't need to do anything
  581. * with IPv6 addresses on IPv4-only exits) */
  582. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 1);
  583. tt_assert(policy);
  584. tt_assert(smartlist_len(policy) == 1);
  585. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  586. addr_policy_list_free(policy);
  587. policy = NULL;
  588. /* test that IPv4 and IPv6 ports are rejected on an IPv4/IPv6 exit */
  589. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 0, 1);
  590. tt_assert(policy);
  591. tt_assert(smartlist_len(policy) == 2);
  592. tt_assert(test_policy_has_address_helper(policy, &ipv4_port->addr));
  593. tt_assert(test_policy_has_address_helper(policy, &ipv6_port->addr));
  594. addr_policy_list_free(policy);
  595. policy = NULL;
  596. done:
  597. addr_policy_list_free(policy);
  598. if (test_configured_ports) {
  599. SMARTLIST_FOREACH(test_configured_ports,
  600. port_cfg_t *, p, port_cfg_free(p));
  601. smartlist_free(test_configured_ports);
  602. test_configured_ports = NULL;
  603. }
  604. UNMOCK(get_configured_ports);
  605. }
  606. smartlist_t *mock_ipv4_addrs = NULL;
  607. smartlist_t *mock_ipv6_addrs = NULL;
  608. /* mock get_interface_address6_list, returning a deep copy of the template
  609. * address list ipv4_interface_address_list or ipv6_interface_address_list */
  610. static smartlist_t *
  611. mock_get_interface_address6_list(int severity,
  612. sa_family_t family,
  613. int include_internal)
  614. {
  615. (void)severity;
  616. (void)include_internal;
  617. smartlist_t *clone_list = smartlist_new();
  618. smartlist_t *template_list = NULL;
  619. if (family == AF_INET) {
  620. template_list = mock_ipv4_addrs;
  621. } else if (family == AF_INET6) {
  622. template_list = mock_ipv6_addrs;
  623. } else {
  624. return NULL;
  625. }
  626. tt_assert(template_list);
  627. SMARTLIST_FOREACH_BEGIN(template_list, tor_addr_t *, src_addr) {
  628. tor_addr_t *dest_addr = malloc(sizeof(tor_addr_t));
  629. memset(dest_addr, 0, sizeof(*dest_addr));
  630. tor_addr_copy_tight(dest_addr, src_addr);
  631. smartlist_add(clone_list, dest_addr);
  632. } SMARTLIST_FOREACH_END(src_addr);
  633. return clone_list;
  634. done:
  635. free_interface_address6_list(clone_list);
  636. return NULL;
  637. }
  638. /** Run unit tests for rejecting publicly routable interface addresses on this
  639. * exit relay using policies_parse_exit_policy_reject_private */
  640. static void
  641. test_policies_reject_interface_address(void *arg)
  642. {
  643. smartlist_t *policy = NULL;
  644. smartlist_t *public_ipv4_addrs =
  645. get_interface_address6_list(LOG_INFO, AF_INET, 0);
  646. smartlist_t *public_ipv6_addrs =
  647. get_interface_address6_list(LOG_INFO, AF_INET6, 0);
  648. tor_addr_t ipv4_addr, ipv6_addr;
  649. (void)arg;
  650. /* test that no addresses are rejected when none are supplied/requested */
  651. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  652. tt_assert(policy == NULL);
  653. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  654. * (and allow for duplicates)
  655. */
  656. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  657. if (policy) {
  658. tt_assert(smartlist_len(policy) <= smartlist_len(public_ipv4_addrs));
  659. addr_policy_list_free(policy);
  660. policy = NULL;
  661. }
  662. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  663. * exit (and allow for duplicates) */
  664. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  665. if (policy) {
  666. tt_assert(smartlist_len(policy) <= (smartlist_len(public_ipv4_addrs)
  667. + smartlist_len(public_ipv6_addrs)));
  668. addr_policy_list_free(policy);
  669. policy = NULL;
  670. }
  671. /* Now do it all again, but mocked */
  672. tor_addr_from_ipv4h(&ipv4_addr, TEST_IPV4_ADDR);
  673. mock_ipv4_addrs = smartlist_new();
  674. smartlist_add(mock_ipv4_addrs, (void *)&ipv4_addr);
  675. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  676. mock_ipv6_addrs = smartlist_new();
  677. smartlist_add(mock_ipv6_addrs, (void *)&ipv6_addr);
  678. MOCK(get_interface_address6_list, mock_get_interface_address6_list);
  679. /* test that no addresses are rejected when none are supplied/requested */
  680. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 0, 0);
  681. tt_assert(policy == NULL);
  682. /* test that only IPv4 interface addresses are rejected on an IPv4-only exit
  683. */
  684. policies_parse_exit_policy_reject_private(&policy, 0, NULL, 1, 0);
  685. tt_assert(policy);
  686. tt_assert(smartlist_len(policy) == smartlist_len(mock_ipv4_addrs));
  687. addr_policy_list_free(policy);
  688. policy = NULL;
  689. /* test that IPv4 and IPv6 interface addresses are rejected on an IPv4/IPv6
  690. * exit */
  691. policies_parse_exit_policy_reject_private(&policy, 1, NULL, 1, 0);
  692. tt_assert(policy);
  693. tt_assert(smartlist_len(policy) == (smartlist_len(mock_ipv4_addrs)
  694. + smartlist_len(mock_ipv6_addrs)));
  695. addr_policy_list_free(policy);
  696. policy = NULL;
  697. done:
  698. addr_policy_list_free(policy);
  699. free_interface_address6_list(public_ipv4_addrs);
  700. free_interface_address6_list(public_ipv6_addrs);
  701. UNMOCK(get_interface_address6_list);
  702. /* we don't use free_interface_address6_list on these lists because their
  703. * address pointers are stack-based */
  704. smartlist_free(mock_ipv4_addrs);
  705. smartlist_free(mock_ipv6_addrs);
  706. }
  707. #undef TEST_IPV4_ADDR
  708. #undef TEST_IPV6_ADDR
  709. static void
  710. test_dump_exit_policy_to_string(void *arg)
  711. {
  712. char *ep;
  713. addr_policy_t *policy_entry;
  714. int malformed_list = -1;
  715. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  716. (void)arg;
  717. ri->policy_is_reject_star = 1;
  718. ri->exit_policy = NULL; // expecting "reject *:*"
  719. ep = router_dump_exit_policy_to_string(ri,1,1);
  720. tt_str_op("reject *:*",OP_EQ, ep);
  721. tor_free(ep);
  722. ri->exit_policy = smartlist_new();
  723. ri->policy_is_reject_star = 0;
  724. policy_entry = router_parse_addr_policy_item_from_string("accept *:*", -1,
  725. &malformed_list);
  726. smartlist_add(ri->exit_policy,policy_entry);
  727. ep = router_dump_exit_policy_to_string(ri,1,1);
  728. tt_str_op("accept *:*",OP_EQ, ep);
  729. tor_free(ep);
  730. policy_entry = router_parse_addr_policy_item_from_string("reject *:25", -1,
  731. &malformed_list);
  732. smartlist_add(ri->exit_policy,policy_entry);
  733. ep = router_dump_exit_policy_to_string(ri,1,1);
  734. tt_str_op("accept *:*\nreject *:25",OP_EQ, ep);
  735. tor_free(ep);
  736. policy_entry =
  737. router_parse_addr_policy_item_from_string("reject 8.8.8.8:*", -1,
  738. &malformed_list);
  739. smartlist_add(ri->exit_policy,policy_entry);
  740. ep = router_dump_exit_policy_to_string(ri,1,1);
  741. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*",OP_EQ, ep);
  742. tor_free(ep);
  743. policy_entry =
  744. router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*", -1,
  745. &malformed_list);
  746. smartlist_add(ri->exit_policy,policy_entry);
  747. ep = router_dump_exit_policy_to_string(ri,1,1);
  748. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  749. "reject6 [fc00::]/7:*",OP_EQ, ep);
  750. tor_free(ep);
  751. policy_entry =
  752. router_parse_addr_policy_item_from_string("accept6 [c000::]/3:*", -1,
  753. &malformed_list);
  754. smartlist_add(ri->exit_policy,policy_entry);
  755. ep = router_dump_exit_policy_to_string(ri,1,1);
  756. tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
  757. "reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",OP_EQ, ep);
  758. done:
  759. if (ri->exit_policy) {
  760. SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
  761. entry, addr_policy_free(entry));
  762. smartlist_free(ri->exit_policy);
  763. }
  764. tor_free(ri);
  765. tor_free(ep);
  766. }
  767. static routerinfo_t *mock_desc_routerinfo = NULL;
  768. static const routerinfo_t *
  769. mock_router_get_my_routerinfo(void)
  770. {
  771. return mock_desc_routerinfo;
  772. }
  773. #define DEFAULT_POLICY_STRING "reject *:*"
  774. #define TEST_IPV4_ADDR (0x02040608)
  775. #define TEST_IPV6_ADDR ("2003::ef01")
  776. static or_options_t mock_options;
  777. static const or_options_t *
  778. mock_get_options(void)
  779. {
  780. return &mock_options;
  781. }
  782. /** Run unit tests for generating summary lines of exit policies */
  783. static void
  784. test_policies_getinfo_helper_policies(void *arg)
  785. {
  786. (void)arg;
  787. int rv = 0;
  788. size_t ipv4_len = 0, ipv6_len = 0;
  789. char *answer = NULL;
  790. const char *errmsg = NULL;
  791. routerinfo_t mock_my_routerinfo;
  792. rv = getinfo_helper_policies(NULL, "exit-policy/default", &answer, &errmsg);
  793. tt_assert(rv == 0);
  794. tt_assert(answer != NULL);
  795. tt_assert(strlen(answer) > 0);
  796. tor_free(answer);
  797. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/default",
  798. &answer, &errmsg);
  799. tt_assert(rv == 0);
  800. tt_assert(answer != NULL);
  801. tt_assert(strlen(answer) > 0);
  802. tor_free(answer);
  803. memset(&mock_my_routerinfo, 0, sizeof(routerinfo_t));
  804. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  805. mock_my_routerinfo.exit_policy = smartlist_new();
  806. mock_desc_routerinfo = &mock_my_routerinfo;
  807. memset(&mock_options, 0, sizeof(or_options_t));
  808. MOCK(get_options, mock_get_options);
  809. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  810. &answer, &errmsg);
  811. tt_assert(rv == 0);
  812. tt_assert(answer != NULL);
  813. tt_assert(strlen(answer) == 0);
  814. tor_free(answer);
  815. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  816. &errmsg);
  817. tt_assert(rv == 0);
  818. tt_assert(answer != NULL);
  819. ipv4_len = strlen(answer);
  820. tt_assert(ipv4_len == 0 || ipv4_len == strlen(DEFAULT_POLICY_STRING));
  821. tt_assert(ipv4_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  822. tor_free(answer);
  823. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  824. &errmsg);
  825. tt_assert(rv == 0);
  826. tt_assert(answer != NULL);
  827. ipv6_len = strlen(answer);
  828. tt_assert(ipv6_len == 0 || ipv6_len == strlen(DEFAULT_POLICY_STRING));
  829. tt_assert(ipv6_len == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  830. tor_free(answer);
  831. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  832. &errmsg);
  833. tt_assert(rv == 0);
  834. tt_assert(answer != NULL);
  835. /* It's either empty or it's the default */
  836. tt_assert(strlen(answer) == 0 || !strcasecmp(answer, DEFAULT_POLICY_STRING));
  837. tor_free(answer);
  838. mock_my_routerinfo.addr = TEST_IPV4_ADDR;
  839. tor_addr_parse(&mock_my_routerinfo.ipv6_addr, TEST_IPV6_ADDR);
  840. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "accept *4:*");
  841. append_exit_policy_string(&mock_my_routerinfo.exit_policy, "reject *6:*");
  842. mock_options.IPv6Exit = 1;
  843. mock_options.ExitPolicyRejectPrivate = 1;
  844. tor_addr_from_ipv4h(&mock_options.OutboundBindAddressIPv4_, TEST_IPV4_ADDR);
  845. tor_addr_parse(&mock_options.OutboundBindAddressIPv6_, TEST_IPV6_ADDR);
  846. rv = getinfo_helper_policies(NULL, "exit-policy/reject-private/relay",
  847. &answer, &errmsg);
  848. tt_assert(rv == 0);
  849. tt_assert(answer != NULL);
  850. tt_assert(strlen(answer) > 0);
  851. tor_free(answer);
  852. rv = getinfo_helper_policies(NULL, "exit-policy/ipv4", &answer,
  853. &errmsg);
  854. tt_assert(rv == 0);
  855. tt_assert(answer != NULL);
  856. ipv4_len = strlen(answer);
  857. tt_assert(ipv4_len > 0);
  858. tor_free(answer);
  859. rv = getinfo_helper_policies(NULL, "exit-policy/ipv6", &answer,
  860. &errmsg);
  861. tt_assert(rv == 0);
  862. tt_assert(answer != NULL);
  863. ipv6_len = strlen(answer);
  864. tt_assert(ipv6_len > 0);
  865. tor_free(answer);
  866. rv = getinfo_helper_policies(NULL, "exit-policy/full", &answer,
  867. &errmsg);
  868. tt_assert(rv == 0);
  869. tt_assert(answer != NULL);
  870. tt_assert(strlen(answer) > 0);
  871. tt_assert(strlen(answer) == ipv4_len + ipv6_len + 1);
  872. tor_free(answer);
  873. done:
  874. tor_free(answer);
  875. UNMOCK(get_options);
  876. UNMOCK(router_get_my_routerinfo);
  877. smartlist_free(mock_my_routerinfo.exit_policy);
  878. }
  879. #undef DEFAULT_POLICY_STRING
  880. #undef TEST_IPV4_ADDR
  881. #undef TEST_IPV6_ADDR
  882. struct testcase_t policy_tests[] = {
  883. { "router_dump_exit_policy_to_string", test_dump_exit_policy_to_string, 0,
  884. NULL, NULL },
  885. { "general", test_policies_general, 0, NULL, NULL },
  886. { "getinfo_helper_policies", test_policies_getinfo_helper_policies, 0, NULL,
  887. NULL },
  888. { "reject_exit_address", test_policies_reject_exit_address, 0, NULL, NULL },
  889. { "reject_interface_address", test_policies_reject_interface_address, 0,
  890. NULL, NULL },
  891. { "reject_port_address", test_policies_reject_port_address, 0, NULL, NULL },
  892. END_OF_TESTCASES
  893. };