protover.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file protover.c
  5. * \brief Versioning information for different pieces of the Tor protocol.
  6. *
  7. * Starting in version 0.2.9.3-alpha, Tor places separate version numbers on
  8. * each of the different components of its protocol. Relays use these numbers
  9. * to advertise what versions of the protocols they can support, and clients
  10. * use them to find what they can ask a given relay to do. Authorities vote
  11. * on the supported protocol versions for each relay, and also vote on the
  12. * which protocols you should have to support in order to be on the Tor
  13. * network. All Tor instances use these required/recommended protocol versions
  14. * to tell what level of support for recent protocols each relay has, and
  15. * to decide whether they should be running given their current protocols.
  16. *
  17. * The main advantage of these protocol versions numbers over using Tor
  18. * version numbers is that they allow different implementations of the Tor
  19. * protocols to develop independently, without having to claim compatibility
  20. * with specific versions of Tor.
  21. **/
  22. #define PROTOVER_PRIVATE
  23. #include "or/or.h"
  24. #include "or/protover.h"
  25. #include "or/routerparse.h"
  26. #ifndef HAVE_RUST
  27. static const smartlist_t *get_supported_protocol_list(void);
  28. static int protocol_list_contains(const smartlist_t *protos,
  29. protocol_type_t pr, uint32_t ver);
  30. /** Mapping between protocol type string and protocol type. */
  31. /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
  32. static const struct {
  33. protocol_type_t protover_type;
  34. const char *name;
  35. } PROTOCOL_NAMES[] = {
  36. { PRT_LINK, "Link" },
  37. { PRT_LINKAUTH, "LinkAuth" },
  38. { PRT_RELAY, "Relay" },
  39. { PRT_DIRCACHE, "DirCache" },
  40. { PRT_HSDIR, "HSDir" },
  41. { PRT_HSINTRO, "HSIntro" },
  42. { PRT_HSREND, "HSRend" },
  43. { PRT_DESC, "Desc" },
  44. { PRT_MICRODESC, "Microdesc"},
  45. { PRT_CONS, "Cons" }
  46. };
  47. #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
  48. /* Maximum allowed length of any single subprotocol name. */
  49. // C_RUST_COUPLED: src/rust/protover/protover.rs
  50. // `MAX_PROTOCOL_NAME_LENGTH`
  51. static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
  52. /**
  53. * Given a protocol_type_t, return the corresponding string used in
  54. * descriptors.
  55. */
  56. STATIC const char *
  57. protocol_type_to_str(protocol_type_t pr)
  58. {
  59. unsigned i;
  60. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  61. if (PROTOCOL_NAMES[i].protover_type == pr)
  62. return PROTOCOL_NAMES[i].name;
  63. }
  64. /* LCOV_EXCL_START */
  65. tor_assert_nonfatal_unreached_once();
  66. return "UNKNOWN";
  67. /* LCOV_EXCL_STOP */
  68. }
  69. /**
  70. * Given a string, find the corresponding protocol type and store it in
  71. * <b>pr_out</b>. Return 0 on success, -1 on failure.
  72. */
  73. STATIC int
  74. str_to_protocol_type(const char *s, protocol_type_t *pr_out)
  75. {
  76. if (BUG(!pr_out))
  77. return -1;
  78. unsigned i;
  79. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  80. if (0 == strcmp(s, PROTOCOL_NAMES[i].name)) {
  81. *pr_out = PROTOCOL_NAMES[i].protover_type;
  82. return 0;
  83. }
  84. }
  85. return -1;
  86. }
  87. /**
  88. * Release all space held by a single proto_entry_t structure
  89. */
  90. STATIC void
  91. proto_entry_free_(proto_entry_t *entry)
  92. {
  93. if (!entry)
  94. return;
  95. tor_free(entry->name);
  96. SMARTLIST_FOREACH(entry->ranges, proto_range_t *, r, tor_free(r));
  97. smartlist_free(entry->ranges);
  98. tor_free(entry);
  99. }
  100. /** The largest possible protocol version. */
  101. #define MAX_PROTOCOL_VERSION (UINT32_MAX-1)
  102. /**
  103. * Given a string <b>s</b> and optional end-of-string pointer
  104. * <b>end_of_range</b>, parse the protocol range and store it in
  105. * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
  106. * U-U, where U is an unsigned 32-bit integer.
  107. */
  108. static int
  109. parse_version_range(const char *s, const char *end_of_range,
  110. uint32_t *low_out, uint32_t *high_out)
  111. {
  112. uint32_t low, high;
  113. char *next = NULL;
  114. int ok;
  115. tor_assert(high_out);
  116. tor_assert(low_out);
  117. if (BUG(!end_of_range))
  118. end_of_range = s + strlen(s); // LCOV_EXCL_LINE
  119. /* A range must start with a digit. */
  120. if (!TOR_ISDIGIT(*s)) {
  121. goto error;
  122. }
  123. /* Note that this wouldn't be safe if we didn't know that eventually,
  124. * we'd hit a NUL */
  125. low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
  126. if (!ok)
  127. goto error;
  128. if (next > end_of_range)
  129. goto error;
  130. if (next == end_of_range) {
  131. high = low;
  132. goto done;
  133. }
  134. if (*next != '-')
  135. goto error;
  136. s = next+1;
  137. /* ibid */
  138. if (!TOR_ISDIGIT(*s)) {
  139. goto error;
  140. }
  141. high = (uint32_t) tor_parse_ulong(s, 10, 0,
  142. MAX_PROTOCOL_VERSION, &ok, &next);
  143. if (!ok)
  144. goto error;
  145. if (next != end_of_range)
  146. goto error;
  147. if (low > high)
  148. goto error;
  149. done:
  150. *high_out = high;
  151. *low_out = low;
  152. return 0;
  153. error:
  154. return -1;
  155. }
  156. /** Parse a single protocol entry from <b>s</b> up to an optional
  157. * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
  158. * on error.
  159. *
  160. * A protocol entry has a keyword, an = sign, and zero or more ranges. */
  161. static proto_entry_t *
  162. parse_single_entry(const char *s, const char *end_of_entry)
  163. {
  164. proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
  165. const char *equals;
  166. out->ranges = smartlist_new();
  167. if (BUG (!end_of_entry))
  168. end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
  169. /* There must be an =. */
  170. equals = memchr(s, '=', end_of_entry - s);
  171. if (!equals)
  172. goto error;
  173. /* The name must be nonempty */
  174. if (equals == s)
  175. goto error;
  176. /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
  177. if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
  178. log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
  179. "protocol name. This is possibly an attack or a bug, unless "
  180. "the Tor network truly supports protocol names larger than "
  181. "%ud characters. The offending string was: %s",
  182. MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
  183. goto error;
  184. }
  185. out->name = tor_strndup(s, equals-s);
  186. tor_assert(equals < end_of_entry);
  187. s = equals + 1;
  188. while (s < end_of_entry) {
  189. const char *comma = memchr(s, ',', end_of_entry-s);
  190. proto_range_t *range = tor_malloc_zero(sizeof(proto_range_t));
  191. if (! comma)
  192. comma = end_of_entry;
  193. smartlist_add(out->ranges, range);
  194. if (parse_version_range(s, comma, &range->low, &range->high) < 0) {
  195. goto error;
  196. }
  197. s = comma;
  198. while (*s == ',' && s < end_of_entry)
  199. ++s;
  200. }
  201. return out;
  202. error:
  203. proto_entry_free(out);
  204. return NULL;
  205. }
  206. /**
  207. * Parse the protocol list from <b>s</b> and return it as a smartlist of
  208. * proto_entry_t
  209. */
  210. STATIC smartlist_t *
  211. parse_protocol_list(const char *s)
  212. {
  213. smartlist_t *entries = smartlist_new();
  214. while (*s) {
  215. /* Find the next space or the NUL. */
  216. const char *end_of_entry = strchr(s, ' ');
  217. proto_entry_t *entry;
  218. if (!end_of_entry)
  219. end_of_entry = s + strlen(s);
  220. entry = parse_single_entry(s, end_of_entry);
  221. if (! entry)
  222. goto error;
  223. smartlist_add(entries, entry);
  224. s = end_of_entry;
  225. while (*s == ' ')
  226. ++s;
  227. }
  228. return entries;
  229. error:
  230. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  231. smartlist_free(entries);
  232. return NULL;
  233. }
  234. /**
  235. * Return true if the unparsed protover in <b>s</b> would contain a protocol
  236. * name longer than MAX_PROTOCOL_NAME_LENGTH, and false otherwise.
  237. */
  238. bool
  239. protover_contains_long_protocol_names(const char *s)
  240. {
  241. smartlist_t *list = parse_protocol_list(s);
  242. if (!list)
  243. return true; /* yes, has a dangerous name */
  244. SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
  245. smartlist_free(list);
  246. return false; /* no, looks fine */
  247. }
  248. /**
  249. * Given a protocol type and version number, return true iff we know
  250. * how to speak that protocol.
  251. */
  252. int
  253. protover_is_supported_here(protocol_type_t pr, uint32_t ver)
  254. {
  255. const smartlist_t *ours = get_supported_protocol_list();
  256. return protocol_list_contains(ours, pr, ver);
  257. }
  258. /**
  259. * Return true iff "list" encodes a protocol list that includes support for
  260. * the indicated protocol and version.
  261. */
  262. int
  263. protocol_list_supports_protocol(const char *list, protocol_type_t tp,
  264. uint32_t version)
  265. {
  266. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  267. * up in profiles, we should memoize it.
  268. */
  269. smartlist_t *protocols = parse_protocol_list(list);
  270. if (!protocols) {
  271. return 0;
  272. }
  273. int contains = protocol_list_contains(protocols, tp, version);
  274. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  275. smartlist_free(protocols);
  276. return contains;
  277. }
  278. /**
  279. * Return true iff "list" encodes a protocol list that includes support for
  280. * the indicated protocol and version, or some later version.
  281. */
  282. int
  283. protocol_list_supports_protocol_or_later(const char *list,
  284. protocol_type_t tp,
  285. uint32_t version)
  286. {
  287. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  288. * up in profiles, we should memoize it.
  289. */
  290. smartlist_t *protocols = parse_protocol_list(list);
  291. if (!protocols) {
  292. return 0;
  293. }
  294. const char *pr_name = protocol_type_to_str(tp);
  295. int contains = 0;
  296. SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
  297. if (strcasecmp(proto->name, pr_name))
  298. continue;
  299. SMARTLIST_FOREACH_BEGIN(proto->ranges, const proto_range_t *, range) {
  300. if (range->high >= version) {
  301. contains = 1;
  302. goto found;
  303. }
  304. } SMARTLIST_FOREACH_END(range);
  305. } SMARTLIST_FOREACH_END(proto);
  306. found:
  307. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  308. smartlist_free(protocols);
  309. return contains;
  310. }
  311. /** Return the canonical string containing the list of protocols
  312. * that we support. */
  313. /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
  314. const char *
  315. protover_get_supported_protocols(void)
  316. {
  317. return
  318. "Cons=1-2 "
  319. "Desc=1-2 "
  320. "DirCache=1-2 "
  321. "HSDir=1-2 "
  322. "HSIntro=3-4 "
  323. "HSRend=1-2 "
  324. "Link=1-5 "
  325. "LinkAuth=1,3 "
  326. "Microdesc=1-2 "
  327. "Relay=1-2";
  328. }
  329. /** The protocols from protover_get_supported_protocols(), as parsed into a
  330. * list of proto_entry_t values. Access this via
  331. * get_supported_protocol_list. */
  332. static smartlist_t *supported_protocol_list = NULL;
  333. /** Return a pointer to a smartlist of proto_entry_t for the protocols
  334. * we support. */
  335. static const smartlist_t *
  336. get_supported_protocol_list(void)
  337. {
  338. if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
  339. supported_protocol_list =
  340. parse_protocol_list(protover_get_supported_protocols());
  341. }
  342. return supported_protocol_list;
  343. }
  344. /**
  345. * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
  346. * as one or more newly allocated strings.
  347. */
  348. static void
  349. proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
  350. {
  351. smartlist_add_asprintf(chunks, "%s=", entry->name);
  352. SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
  353. const char *comma = "";
  354. if (range_sl_idx != 0)
  355. comma = ",";
  356. if (range->low == range->high) {
  357. smartlist_add_asprintf(chunks, "%s%lu",
  358. comma, (unsigned long)range->low);
  359. } else {
  360. smartlist_add_asprintf(chunks, "%s%lu-%lu",
  361. comma, (unsigned long)range->low,
  362. (unsigned long)range->high);
  363. }
  364. } SMARTLIST_FOREACH_END(range);
  365. }
  366. /** Given a list of space-separated proto_entry_t items,
  367. * encode it into a newly allocated space-separated string. */
  368. STATIC char *
  369. encode_protocol_list(const smartlist_t *sl)
  370. {
  371. const char *separator = "";
  372. smartlist_t *chunks = smartlist_new();
  373. SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
  374. smartlist_add_strdup(chunks, separator);
  375. proto_entry_encode_into(chunks, ent);
  376. separator = " ";
  377. } SMARTLIST_FOREACH_END(ent);
  378. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  379. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  380. smartlist_free(chunks);
  381. return result;
  382. }
  383. /* We treat any protocol list with more than this many subprotocols in it
  384. * as a DoS attempt. */
  385. /// C_RUST_COUPLED: src/rust/protover/protover.rs
  386. /// `MAX_PROTOCOLS_TO_EXPAND`
  387. static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
  388. /** Voting helper: Given a list of proto_entry_t, return a newly allocated
  389. * smartlist of newly allocated strings, one for each included protocol
  390. * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
  391. * 'Foo=7'.)
  392. *
  393. * Do not list any protocol version more than once.
  394. *
  395. * Return NULL if the list would be too big.
  396. */
  397. static smartlist_t *
  398. expand_protocol_list(const smartlist_t *protos)
  399. {
  400. smartlist_t *expanded = smartlist_new();
  401. if (!protos)
  402. return expanded;
  403. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  404. const char *name = ent->name;
  405. if (strlen(name) > MAX_PROTOCOL_NAME_LENGTH) {
  406. log_warn(LD_NET, "When expanding a protocol entry, I got a very large "
  407. "protocol name. This is possibly an attack or a bug, unless "
  408. "the Tor network truly supports protocol names larger than "
  409. "%ud characters. The offending string was: %s",
  410. MAX_PROTOCOL_NAME_LENGTH, escaped(name));
  411. continue;
  412. }
  413. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  414. uint32_t u;
  415. for (u = range->low; u <= range->high; ++u) {
  416. smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
  417. if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
  418. goto too_many;
  419. }
  420. } SMARTLIST_FOREACH_END(range);
  421. } SMARTLIST_FOREACH_END(ent);
  422. smartlist_sort_strings(expanded);
  423. smartlist_uniq_strings(expanded); // This makes voting work. do not remove
  424. return expanded;
  425. too_many:
  426. SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
  427. smartlist_free(expanded);
  428. return NULL;
  429. }
  430. /** Voting helper: compare two singleton proto_entry_t items by version
  431. * alone. (A singleton item is one with a single range entry where
  432. * low==high.) */
  433. static int
  434. cmp_single_ent_by_version(const void **a_, const void **b_)
  435. {
  436. const proto_entry_t *ent_a = *a_;
  437. const proto_entry_t *ent_b = *b_;
  438. tor_assert(smartlist_len(ent_a->ranges) == 1);
  439. tor_assert(smartlist_len(ent_b->ranges) == 1);
  440. const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
  441. const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
  442. tor_assert(a->low == a->high);
  443. tor_assert(b->low == b->high);
  444. if (a->low < b->low) {
  445. return -1;
  446. } else if (a->low == b->low) {
  447. return 0;
  448. } else {
  449. return 1;
  450. }
  451. }
  452. /** Voting helper: Given a list of singleton protocol strings (of the form
  453. * Foo=7), return a canonical listing of all the protocol versions listed,
  454. * with as few ranges as possible, with protocol versions sorted lexically and
  455. * versions sorted in numerically increasing order, using as few range entries
  456. * as possible.
  457. **/
  458. static char *
  459. contract_protocol_list(const smartlist_t *proto_strings)
  460. {
  461. if (smartlist_len(proto_strings) == 0) {
  462. return tor_strdup("");
  463. }
  464. // map from name to list of single-version entries
  465. strmap_t *entry_lists_by_name = strmap_new();
  466. // list of protocol names
  467. smartlist_t *all_names = smartlist_new();
  468. // list of strings for the output we're building
  469. smartlist_t *chunks = smartlist_new();
  470. // Parse each item and stick it entry_lists_by_name. Build
  471. // 'all_names' at the same time.
  472. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  473. if (BUG(!s))
  474. continue;// LCOV_EXCL_LINE
  475. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  476. if (BUG(!ent))
  477. continue; // LCOV_EXCL_LINE
  478. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  479. if (!lst) {
  480. smartlist_add(all_names, ent->name);
  481. lst = smartlist_new();
  482. strmap_set(entry_lists_by_name, ent->name, lst);
  483. }
  484. smartlist_add(lst, ent);
  485. } SMARTLIST_FOREACH_END(s);
  486. // We want to output the protocols sorted by their name.
  487. smartlist_sort_strings(all_names);
  488. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  489. const int first_entry = (name_sl_idx == 0);
  490. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  491. tor_assert(lst);
  492. // Sort every entry with this name by version. They are
  493. // singletons, so there can't be overlap.
  494. smartlist_sort(lst, cmp_single_ent_by_version);
  495. if (! first_entry)
  496. smartlist_add_strdup(chunks, " ");
  497. /* We're going to construct this entry from the ranges. */
  498. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  499. entry->ranges = smartlist_new();
  500. entry->name = tor_strdup(name);
  501. // Now, find all the ranges of versions start..end where
  502. // all of start, start+1, start+2, ..end are included.
  503. int start_of_cur_series = 0;
  504. while (start_of_cur_series < smartlist_len(lst)) {
  505. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  506. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  507. const uint32_t ver_low = range->low;
  508. uint32_t ver_high = ver_low;
  509. int idx;
  510. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  511. ent = smartlist_get(lst, idx);
  512. range = smartlist_get(ent->ranges, 0);
  513. if (range->low != ver_high + 1)
  514. break;
  515. ver_high += 1;
  516. }
  517. // Now idx is either off the end of the list, or the first sequence
  518. // break in the list.
  519. start_of_cur_series = idx;
  520. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  521. new_range->low = ver_low;
  522. new_range->high = ver_high;
  523. smartlist_add(entry->ranges, new_range);
  524. }
  525. proto_entry_encode_into(chunks, entry);
  526. proto_entry_free(entry);
  527. } SMARTLIST_FOREACH_END(name);
  528. // Build the result...
  529. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  530. // And free all the stuff we allocated.
  531. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  532. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  533. tor_assert(lst);
  534. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  535. smartlist_free(lst);
  536. } SMARTLIST_FOREACH_END(name);
  537. strmap_free(entry_lists_by_name, NULL);
  538. smartlist_free(all_names);
  539. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  540. smartlist_free(chunks);
  541. return result;
  542. }
  543. /**
  544. * Protocol voting implementation.
  545. *
  546. * Given a list of strings describing protocol versions, return a newly
  547. * allocated string encoding all of the protocols that are listed by at
  548. * least <b>threshold</b> of the inputs.
  549. *
  550. * The string is minimal and sorted according to the rules of
  551. * contract_protocol_list above.
  552. */
  553. char *
  554. protover_compute_vote(const smartlist_t *list_of_proto_strings,
  555. int threshold)
  556. {
  557. if (smartlist_len(list_of_proto_strings) == 0) {
  558. return tor_strdup("");
  559. }
  560. smartlist_t *all_entries = smartlist_new();
  561. // First, parse the inputs and break them into singleton entries.
  562. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  563. smartlist_t *unexpanded = parse_protocol_list(vote);
  564. if (! unexpanded) {
  565. log_warn(LD_NET, "I failed with parsing a protocol list from "
  566. "an authority. The offending string was: %s",
  567. escaped(vote));
  568. continue;
  569. }
  570. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  571. if (this_vote == NULL) {
  572. log_warn(LD_NET, "When expanding a protocol list from an authority, I "
  573. "got too many protocols. This is possibly an attack or a bug, "
  574. "unless the Tor network truly has expanded to support over %d "
  575. "different subprotocol versions. The offending string was: %s",
  576. MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
  577. } else {
  578. smartlist_add_all(all_entries, this_vote);
  579. smartlist_free(this_vote);
  580. }
  581. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  582. smartlist_free(unexpanded);
  583. } SMARTLIST_FOREACH_END(vote);
  584. if (smartlist_len(all_entries) == 0) {
  585. smartlist_free(all_entries);
  586. return tor_strdup("");
  587. }
  588. // Now sort the singleton entries
  589. smartlist_sort_strings(all_entries);
  590. // Now find all the strings that appear at least 'threshold' times.
  591. smartlist_t *include_entries = smartlist_new();
  592. const char *cur_entry = smartlist_get(all_entries, 0);
  593. int n_times = 0;
  594. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  595. if (!strcmp(ent, cur_entry)) {
  596. n_times++;
  597. } else {
  598. if (n_times >= threshold && cur_entry)
  599. smartlist_add(include_entries, (void*)cur_entry);
  600. cur_entry = ent;
  601. n_times = 1 ;
  602. }
  603. } SMARTLIST_FOREACH_END(ent);
  604. if (n_times >= threshold && cur_entry)
  605. smartlist_add(include_entries, (void*)cur_entry);
  606. // Finally, compress that list.
  607. char *result = contract_protocol_list(include_entries);
  608. smartlist_free(include_entries);
  609. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  610. smartlist_free(all_entries);
  611. return result;
  612. }
  613. /** Return true if every protocol version described in the string <b>s</b> is
  614. * one that we support, and false otherwise. If <b>missing_out</b> is
  615. * provided, set it to the list of protocols we do not support.
  616. *
  617. * NOTE: This is quadratic, but we don't do it much: only a few times per
  618. * consensus. Checking signatures should be way more expensive than this
  619. * ever would be.
  620. **/
  621. int
  622. protover_all_supported(const char *s, char **missing_out)
  623. {
  624. int all_supported = 1;
  625. smartlist_t *missing_some;
  626. smartlist_t *missing_completely;
  627. smartlist_t *missing_all;
  628. if (!s) {
  629. return 1;
  630. }
  631. smartlist_t *entries = parse_protocol_list(s);
  632. if (BUG(entries == NULL)) {
  633. log_warn(LD_NET, "Received an unparseable protocol list %s"
  634. " from the consensus", escaped(s));
  635. return 1;
  636. }
  637. missing_some = smartlist_new();
  638. missing_completely = smartlist_new();
  639. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  640. protocol_type_t tp;
  641. if (str_to_protocol_type(ent->name, &tp) < 0) {
  642. if (smartlist_len(ent->ranges)) {
  643. goto unsupported;
  644. }
  645. continue;
  646. }
  647. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  648. proto_entry_t *unsupported = tor_malloc_zero(sizeof(proto_entry_t));
  649. proto_range_t *versions = tor_malloc_zero(sizeof(proto_range_t));
  650. uint32_t i;
  651. unsupported->name = tor_strdup(ent->name);
  652. unsupported->ranges = smartlist_new();
  653. for (i = range->low; i <= range->high; ++i) {
  654. if (!protover_is_supported_here(tp, i)) {
  655. if (versions->low == 0 && versions->high == 0) {
  656. versions->low = i;
  657. /* Pre-emptively add the high now, just in case we're in a single
  658. * version range (e.g. "Link=999"). */
  659. versions->high = i;
  660. }
  661. /* If the last one to be unsupported is one less than the current
  662. * one, we're in a continuous range, so set the high field. */
  663. if ((versions->high && versions->high == i - 1) ||
  664. /* Similarly, if the last high wasn't set and we're currently
  665. * one higher than the low, add current index as the highest
  666. * known high. */
  667. (!versions->high && versions->low == i - 1)) {
  668. versions->high = i;
  669. continue;
  670. }
  671. } else {
  672. /* If we hit a supported version, and we previously had a range,
  673. * we've hit a non-continuity. Copy the previous range and add it to
  674. * the unsupported->ranges list and zero-out the previous range for
  675. * the next iteration. */
  676. if (versions->low != 0 && versions->high != 0) {
  677. proto_range_t *versions_to_add = tor_malloc(sizeof(proto_range_t));
  678. versions_to_add->low = versions->low;
  679. versions_to_add->high = versions->high;
  680. smartlist_add(unsupported->ranges, versions_to_add);
  681. versions->low = 0;
  682. versions->high = 0;
  683. }
  684. }
  685. }
  686. /* Once we've run out of versions to check, see if we had any unsupported
  687. * ones and, if so, add them to unsupported->ranges. */
  688. if (versions->low != 0 && versions->high != 0) {
  689. smartlist_add(unsupported->ranges, versions);
  690. }
  691. /* Finally, if we had something unsupported, add it to the list of
  692. * missing_some things and mark that there was something missing. */
  693. if (smartlist_len(unsupported->ranges) != 0) {
  694. smartlist_add(missing_some, (void*) unsupported);
  695. all_supported = 0;
  696. } else {
  697. proto_entry_free(unsupported);
  698. tor_free(versions);
  699. }
  700. } SMARTLIST_FOREACH_END(range);
  701. continue;
  702. unsupported:
  703. all_supported = 0;
  704. smartlist_add(missing_completely, (void*) ent);
  705. } SMARTLIST_FOREACH_END(ent);
  706. /* We keep the two smartlists separate so that we can free the proto_entry_t
  707. * we created and put in missing_some, so here we add them together to build
  708. * the string. */
  709. missing_all = smartlist_new();
  710. smartlist_add_all(missing_all, missing_some);
  711. smartlist_add_all(missing_all, missing_completely);
  712. if (missing_out && !all_supported) {
  713. tor_assert(smartlist_len(missing_all) != 0);
  714. *missing_out = encode_protocol_list(missing_all);
  715. }
  716. SMARTLIST_FOREACH(missing_some, proto_entry_t *, ent, proto_entry_free(ent));
  717. smartlist_free(missing_some);
  718. smartlist_free(missing_completely);
  719. smartlist_free(missing_all);
  720. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  721. smartlist_free(entries);
  722. return all_supported;
  723. }
  724. /** Helper: Given a list of proto_entry_t, return true iff
  725. * <b>pr</b>=<b>ver</b> is included in that list. */
  726. static int
  727. protocol_list_contains(const smartlist_t *protos,
  728. protocol_type_t pr, uint32_t ver)
  729. {
  730. if (BUG(protos == NULL)) {
  731. return 0; // LCOV_EXCL_LINE
  732. }
  733. const char *pr_name = protocol_type_to_str(pr);
  734. if (BUG(pr_name == NULL)) {
  735. return 0; // LCOV_EXCL_LINE
  736. }
  737. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  738. if (strcasecmp(ent->name, pr_name))
  739. continue;
  740. /* name matches; check the ranges */
  741. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  742. if (ver >= range->low && ver <= range->high)
  743. return 1;
  744. } SMARTLIST_FOREACH_END(range);
  745. } SMARTLIST_FOREACH_END(ent);
  746. return 0;
  747. }
  748. /** Return a string describing the protocols supported by tor version
  749. * <b>version</b>, or an empty string if we cannot tell.
  750. *
  751. * Note that this is only used to infer protocols for Tor versions that
  752. * can't declare their own.
  753. **/
  754. /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
  755. const char *
  756. protover_compute_for_old_tor(const char *version)
  757. {
  758. if (version == NULL) {
  759. /* No known version; guess the oldest series that is still supported. */
  760. version = "0.2.5.15";
  761. }
  762. if (tor_version_as_new_as(version,
  763. FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
  764. return "";
  765. } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
  766. /* 0.2.9.1-alpha HSRend=2 */
  767. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
  768. "Link=1-4 LinkAuth=1 "
  769. "Microdesc=1-2 Relay=1-2";
  770. } else if (tor_version_as_new_as(version, "0.2.7.5")) {
  771. /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
  772. * ed25519 support. We'll call them present only in "stable" 027,
  773. * though. */
  774. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  775. "Link=1-4 LinkAuth=1 "
  776. "Microdesc=1-2 Relay=1-2";
  777. } else if (tor_version_as_new_as(version, "0.2.4.19")) {
  778. /* No currently supported Tor server versions are older than this, or
  779. * lack these protocols. */
  780. return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  781. "Link=1-4 LinkAuth=1 "
  782. "Microdesc=1 Relay=1-2";
  783. } else {
  784. /* Cannot infer protocols. */
  785. return "";
  786. }
  787. }
  788. /**
  789. * Release all storage held by static fields in protover.c
  790. */
  791. void
  792. protover_free_all(void)
  793. {
  794. if (supported_protocol_list) {
  795. smartlist_t *entries = supported_protocol_list;
  796. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  797. smartlist_free(entries);
  798. supported_protocol_list = NULL;
  799. }
  800. }
  801. #endif /* !defined(HAVE_RUST) */