protover.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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.h"
  24. #include "protover.h"
  25. #include "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. if (!parse_protocol_list(s))
  242. return true;
  243. return false;
  244. }
  245. /**
  246. * Given a protocol type and version number, return true iff we know
  247. * how to speak that protocol.
  248. */
  249. int
  250. protover_is_supported_here(protocol_type_t pr, uint32_t ver)
  251. {
  252. const smartlist_t *ours = get_supported_protocol_list();
  253. return protocol_list_contains(ours, pr, ver);
  254. }
  255. /**
  256. * Return true iff "list" encodes a protocol list that includes support for
  257. * the indicated protocol and version.
  258. */
  259. int
  260. protocol_list_supports_protocol(const char *list, protocol_type_t tp,
  261. uint32_t version)
  262. {
  263. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  264. * up in profiles, we should memoize it.
  265. */
  266. smartlist_t *protocols = parse_protocol_list(list);
  267. if (!protocols) {
  268. return 0;
  269. }
  270. int contains = protocol_list_contains(protocols, tp, version);
  271. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  272. smartlist_free(protocols);
  273. return contains;
  274. }
  275. /**
  276. * Return true iff "list" encodes a protocol list that includes support for
  277. * the indicated protocol and version, or some later version.
  278. */
  279. int
  280. protocol_list_supports_protocol_or_later(const char *list,
  281. protocol_type_t tp,
  282. uint32_t version)
  283. {
  284. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  285. * up in profiles, we should memoize it.
  286. */
  287. smartlist_t *protocols = parse_protocol_list(list);
  288. if (!protocols) {
  289. return 0;
  290. }
  291. const char *pr_name = protocol_type_to_str(tp);
  292. int contains = 0;
  293. SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
  294. if (strcasecmp(proto->name, pr_name))
  295. continue;
  296. SMARTLIST_FOREACH_BEGIN(proto->ranges, const proto_range_t *, range) {
  297. if (range->high >= version) {
  298. contains = 1;
  299. goto found;
  300. }
  301. } SMARTLIST_FOREACH_END(range);
  302. } SMARTLIST_FOREACH_END(proto);
  303. found:
  304. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  305. smartlist_free(protocols);
  306. return contains;
  307. }
  308. /** Return the canonical string containing the list of protocols
  309. * that we support. */
  310. /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
  311. const char *
  312. protover_get_supported_protocols(void)
  313. {
  314. return
  315. "Cons=1-2 "
  316. "Desc=1-2 "
  317. "DirCache=1-2 "
  318. "HSDir=1-2 "
  319. "HSIntro=3-4 "
  320. "HSRend=1-2 "
  321. "Link=1-5 "
  322. "LinkAuth=1,3 "
  323. "Microdesc=1-2 "
  324. "Relay=1-2";
  325. }
  326. /** The protocols from protover_get_supported_protocols(), as parsed into a
  327. * list of proto_entry_t values. Access this via
  328. * get_supported_protocol_list. */
  329. static smartlist_t *supported_protocol_list = NULL;
  330. /** Return a pointer to a smartlist of proto_entry_t for the protocols
  331. * we support. */
  332. static const smartlist_t *
  333. get_supported_protocol_list(void)
  334. {
  335. if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
  336. supported_protocol_list =
  337. parse_protocol_list(protover_get_supported_protocols());
  338. }
  339. return supported_protocol_list;
  340. }
  341. /**
  342. * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
  343. * as one or more newly allocated strings.
  344. */
  345. static void
  346. proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
  347. {
  348. smartlist_add_asprintf(chunks, "%s=", entry->name);
  349. SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
  350. const char *comma = "";
  351. if (range_sl_idx != 0)
  352. comma = ",";
  353. if (range->low == range->high) {
  354. smartlist_add_asprintf(chunks, "%s%lu",
  355. comma, (unsigned long)range->low);
  356. } else {
  357. smartlist_add_asprintf(chunks, "%s%lu-%lu",
  358. comma, (unsigned long)range->low,
  359. (unsigned long)range->high);
  360. }
  361. } SMARTLIST_FOREACH_END(range);
  362. }
  363. /** Given a list of space-separated proto_entry_t items,
  364. * encode it into a newly allocated space-separated string. */
  365. STATIC char *
  366. encode_protocol_list(const smartlist_t *sl)
  367. {
  368. const char *separator = "";
  369. smartlist_t *chunks = smartlist_new();
  370. SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
  371. smartlist_add_strdup(chunks, separator);
  372. proto_entry_encode_into(chunks, ent);
  373. separator = " ";
  374. } SMARTLIST_FOREACH_END(ent);
  375. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  376. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  377. smartlist_free(chunks);
  378. return result;
  379. }
  380. /* We treat any protocol list with more than this many subprotocols in it
  381. * as a DoS attempt. */
  382. /// C_RUST_COUPLED: src/rust/protover/protover.rs
  383. /// `MAX_PROTOCOLS_TO_EXPAND`
  384. static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
  385. /** Voting helper: Given a list of proto_entry_t, return a newly allocated
  386. * smartlist of newly allocated strings, one for each included protocol
  387. * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
  388. * 'Foo=7'.)
  389. *
  390. * Do not list any protocol version more than once.
  391. *
  392. * Return NULL if the list would be too big.
  393. */
  394. static smartlist_t *
  395. expand_protocol_list(const smartlist_t *protos)
  396. {
  397. smartlist_t *expanded = smartlist_new();
  398. if (!protos)
  399. return expanded;
  400. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  401. const char *name = ent->name;
  402. if (strlen(name) > MAX_PROTOCOL_NAME_LENGTH) {
  403. log_warn(LD_NET, "When expanding a protocol entry, I got a very large "
  404. "protocol name. This is possibly an attack or a bug, unless "
  405. "the Tor network truly supports protocol names larger than "
  406. "%ud characters. The offending string was: %s",
  407. MAX_PROTOCOL_NAME_LENGTH, escaped(name));
  408. continue;
  409. }
  410. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  411. uint32_t u;
  412. for (u = range->low; u <= range->high; ++u) {
  413. smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
  414. if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
  415. goto too_many;
  416. }
  417. } SMARTLIST_FOREACH_END(range);
  418. } SMARTLIST_FOREACH_END(ent);
  419. smartlist_sort_strings(expanded);
  420. smartlist_uniq_strings(expanded); // This makes voting work. do not remove
  421. return expanded;
  422. too_many:
  423. SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
  424. smartlist_free(expanded);
  425. return NULL;
  426. }
  427. /** Voting helper: compare two singleton proto_entry_t items by version
  428. * alone. (A singleton item is one with a single range entry where
  429. * low==high.) */
  430. static int
  431. cmp_single_ent_by_version(const void **a_, const void **b_)
  432. {
  433. const proto_entry_t *ent_a = *a_;
  434. const proto_entry_t *ent_b = *b_;
  435. tor_assert(smartlist_len(ent_a->ranges) == 1);
  436. tor_assert(smartlist_len(ent_b->ranges) == 1);
  437. const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
  438. const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
  439. tor_assert(a->low == a->high);
  440. tor_assert(b->low == b->high);
  441. if (a->low < b->low) {
  442. return -1;
  443. } else if (a->low == b->low) {
  444. return 0;
  445. } else {
  446. return 1;
  447. }
  448. }
  449. /** Voting helper: Given a list of singleton protocol strings (of the form
  450. * Foo=7), return a canonical listing of all the protocol versions listed,
  451. * with as few ranges as possible, with protocol versions sorted lexically and
  452. * versions sorted in numerically increasing order, using as few range entries
  453. * as possible.
  454. **/
  455. static char *
  456. contract_protocol_list(const smartlist_t *proto_strings)
  457. {
  458. if (smartlist_len(proto_strings) == 0) {
  459. return tor_strdup("");
  460. }
  461. // map from name to list of single-version entries
  462. strmap_t *entry_lists_by_name = strmap_new();
  463. // list of protocol names
  464. smartlist_t *all_names = smartlist_new();
  465. // list of strings for the output we're building
  466. smartlist_t *chunks = smartlist_new();
  467. // Parse each item and stick it entry_lists_by_name. Build
  468. // 'all_names' at the same time.
  469. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  470. if (BUG(!s))
  471. continue;// LCOV_EXCL_LINE
  472. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  473. if (BUG(!ent))
  474. continue; // LCOV_EXCL_LINE
  475. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  476. if (!lst) {
  477. smartlist_add(all_names, ent->name);
  478. lst = smartlist_new();
  479. strmap_set(entry_lists_by_name, ent->name, lst);
  480. }
  481. smartlist_add(lst, ent);
  482. } SMARTLIST_FOREACH_END(s);
  483. // We want to output the protocols sorted by their name.
  484. smartlist_sort_strings(all_names);
  485. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  486. const int first_entry = (name_sl_idx == 0);
  487. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  488. tor_assert(lst);
  489. // Sort every entry with this name by version. They are
  490. // singletons, so there can't be overlap.
  491. smartlist_sort(lst, cmp_single_ent_by_version);
  492. if (! first_entry)
  493. smartlist_add_strdup(chunks, " ");
  494. /* We're going to construct this entry from the ranges. */
  495. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  496. entry->ranges = smartlist_new();
  497. entry->name = tor_strdup(name);
  498. // Now, find all the ranges of versions start..end where
  499. // all of start, start+1, start+2, ..end are included.
  500. int start_of_cur_series = 0;
  501. while (start_of_cur_series < smartlist_len(lst)) {
  502. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  503. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  504. const uint32_t ver_low = range->low;
  505. uint32_t ver_high = ver_low;
  506. int idx;
  507. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  508. ent = smartlist_get(lst, idx);
  509. range = smartlist_get(ent->ranges, 0);
  510. if (range->low != ver_high + 1)
  511. break;
  512. ver_high += 1;
  513. }
  514. // Now idx is either off the end of the list, or the first sequence
  515. // break in the list.
  516. start_of_cur_series = idx;
  517. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  518. new_range->low = ver_low;
  519. new_range->high = ver_high;
  520. smartlist_add(entry->ranges, new_range);
  521. }
  522. proto_entry_encode_into(chunks, entry);
  523. proto_entry_free(entry);
  524. } SMARTLIST_FOREACH_END(name);
  525. // Build the result...
  526. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  527. // And free all the stuff we allocated.
  528. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  529. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  530. tor_assert(lst);
  531. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  532. smartlist_free(lst);
  533. } SMARTLIST_FOREACH_END(name);
  534. strmap_free(entry_lists_by_name, NULL);
  535. smartlist_free(all_names);
  536. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  537. smartlist_free(chunks);
  538. return result;
  539. }
  540. /**
  541. * Protocol voting implementation.
  542. *
  543. * Given a list of strings describing protocol versions, return a newly
  544. * allocated string encoding all of the protocols that are listed by at
  545. * least <b>threshold</b> of the inputs.
  546. *
  547. * The string is minimal and sorted according to the rules of
  548. * contract_protocol_list above.
  549. */
  550. char *
  551. protover_compute_vote(const smartlist_t *list_of_proto_strings,
  552. int threshold)
  553. {
  554. if (smartlist_len(list_of_proto_strings) == 0) {
  555. return tor_strdup("");
  556. }
  557. smartlist_t *all_entries = smartlist_new();
  558. // First, parse the inputs and break them into singleton entries.
  559. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  560. smartlist_t *unexpanded = parse_protocol_list(vote);
  561. if (! unexpanded) {
  562. log_warn(LD_NET, "I failed with parsing a protocol list from "
  563. "an authority. The offending string was: %s",
  564. escaped(vote));
  565. continue;
  566. }
  567. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  568. if (this_vote == NULL) {
  569. log_warn(LD_NET, "When expanding a protocol list from an authority, I "
  570. "got too many protocols. This is possibly an attack or a bug, "
  571. "unless the Tor network truly has expanded to support over %d "
  572. "different subprotocol versions. The offending string was: %s",
  573. MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
  574. } else {
  575. smartlist_add_all(all_entries, this_vote);
  576. smartlist_free(this_vote);
  577. }
  578. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  579. smartlist_free(unexpanded);
  580. } SMARTLIST_FOREACH_END(vote);
  581. if (smartlist_len(all_entries) == 0) {
  582. smartlist_free(all_entries);
  583. return tor_strdup("");
  584. }
  585. // Now sort the singleton entries
  586. smartlist_sort_strings(all_entries);
  587. // Now find all the strings that appear at least 'threshold' times.
  588. smartlist_t *include_entries = smartlist_new();
  589. const char *cur_entry = smartlist_get(all_entries, 0);
  590. int n_times = 0;
  591. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  592. if (!strcmp(ent, cur_entry)) {
  593. n_times++;
  594. } else {
  595. if (n_times >= threshold && cur_entry)
  596. smartlist_add(include_entries, (void*)cur_entry);
  597. cur_entry = ent;
  598. n_times = 1 ;
  599. }
  600. } SMARTLIST_FOREACH_END(ent);
  601. if (n_times >= threshold && cur_entry)
  602. smartlist_add(include_entries, (void*)cur_entry);
  603. // Finally, compress that list.
  604. char *result = contract_protocol_list(include_entries);
  605. smartlist_free(include_entries);
  606. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  607. smartlist_free(all_entries);
  608. return result;
  609. }
  610. /** Return true if every protocol version described in the string <b>s</b> is
  611. * one that we support, and false otherwise. If <b>missing_out</b> is
  612. * provided, set it to the list of protocols we do not support.
  613. *
  614. * NOTE: This is quadratic, but we don't do it much: only a few times per
  615. * consensus. Checking signatures should be way more expensive than this
  616. * ever would be.
  617. **/
  618. int
  619. protover_all_supported(const char *s, char **missing_out)
  620. {
  621. int all_supported = 1;
  622. smartlist_t *missing_some;
  623. smartlist_t *missing_completely;
  624. smartlist_t *missing_all;
  625. if (!s) {
  626. return 1;
  627. }
  628. smartlist_t *entries = parse_protocol_list(s);
  629. if (BUG(entries == NULL)) {
  630. log_warn(LD_NET, "Received an unparseable protocol list %s"
  631. " from the consensus", escaped(s));
  632. return 1;
  633. }
  634. missing_some = smartlist_new();
  635. missing_completely = smartlist_new();
  636. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  637. protocol_type_t tp;
  638. if (str_to_protocol_type(ent->name, &tp) < 0) {
  639. if (smartlist_len(ent->ranges)) {
  640. goto unsupported;
  641. }
  642. continue;
  643. }
  644. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  645. proto_entry_t *unsupported = tor_malloc_zero(sizeof(proto_entry_t));
  646. proto_range_t *versions = tor_malloc_zero(sizeof(proto_range_t));
  647. uint32_t i;
  648. unsupported->name = tor_strdup(ent->name);
  649. unsupported->ranges = smartlist_new();
  650. for (i = range->low; i <= range->high; ++i) {
  651. if (!protover_is_supported_here(tp, i)) {
  652. if (versions->low == 0 && versions->high == 0) {
  653. versions->low = i;
  654. /* Pre-emptively add the high now, just in case we're in a single
  655. * version range (e.g. "Link=999"). */
  656. versions->high = i;
  657. }
  658. /* If the last one to be unsupported is one less than the current
  659. * one, we're in a continuous range, so set the high field. */
  660. if ((versions->high && versions->high == i - 1) ||
  661. /* Similarly, if the last high wasn't set and we're currently
  662. * one higher than the low, add current index as the highest
  663. * known high. */
  664. (!versions->high && versions->low == i - 1)) {
  665. versions->high = i;
  666. continue;
  667. }
  668. } else {
  669. /* If we hit a supported version, and we previously had a range,
  670. * we've hit a non-continuity. Copy the previous range and add it to
  671. * the unsupported->ranges list and zero-out the previous range for
  672. * the next iteration. */
  673. if (versions->low != 0 && versions->high != 0) {
  674. proto_range_t *versions_to_add = tor_malloc(sizeof(proto_range_t));
  675. versions_to_add->low = versions->low;
  676. versions_to_add->high = versions->high;
  677. smartlist_add(unsupported->ranges, versions_to_add);
  678. versions->low = 0;
  679. versions->high = 0;
  680. }
  681. }
  682. }
  683. /* Once we've run out of versions to check, see if we had any unsupported
  684. * ones and, if so, add them to unsupported->ranges. */
  685. if (versions->low != 0 && versions->high != 0) {
  686. smartlist_add(unsupported->ranges, versions);
  687. }
  688. /* Finally, if we had something unsupported, add it to the list of
  689. * missing_some things and mark that there was something missing. */
  690. if (smartlist_len(unsupported->ranges) != 0) {
  691. smartlist_add(missing_some, (void*) unsupported);
  692. all_supported = 0;
  693. } else {
  694. proto_entry_free(unsupported);
  695. tor_free(versions);
  696. }
  697. } SMARTLIST_FOREACH_END(range);
  698. continue;
  699. unsupported:
  700. all_supported = 0;
  701. smartlist_add(missing_completely, (void*) ent);
  702. } SMARTLIST_FOREACH_END(ent);
  703. /* We keep the two smartlists separate so that we can free the proto_entry_t
  704. * we created and put in missing_some, so here we add them together to build
  705. * the string. */
  706. missing_all = smartlist_new();
  707. smartlist_add_all(missing_all, missing_some);
  708. smartlist_add_all(missing_all, missing_completely);
  709. if (missing_out && !all_supported) {
  710. tor_assert(smartlist_len(missing_all) != 0);
  711. *missing_out = encode_protocol_list(missing_all);
  712. }
  713. SMARTLIST_FOREACH(missing_some, proto_entry_t *, ent, proto_entry_free(ent));
  714. smartlist_free(missing_some);
  715. smartlist_free(missing_completely);
  716. smartlist_free(missing_all);
  717. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  718. smartlist_free(entries);
  719. return all_supported;
  720. }
  721. /** Helper: Given a list of proto_entry_t, return true iff
  722. * <b>pr</b>=<b>ver</b> is included in that list. */
  723. static int
  724. protocol_list_contains(const smartlist_t *protos,
  725. protocol_type_t pr, uint32_t ver)
  726. {
  727. if (BUG(protos == NULL)) {
  728. return 0; // LCOV_EXCL_LINE
  729. }
  730. const char *pr_name = protocol_type_to_str(pr);
  731. if (BUG(pr_name == NULL)) {
  732. return 0; // LCOV_EXCL_LINE
  733. }
  734. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  735. if (strcasecmp(ent->name, pr_name))
  736. continue;
  737. /* name matches; check the ranges */
  738. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  739. if (ver >= range->low && ver <= range->high)
  740. return 1;
  741. } SMARTLIST_FOREACH_END(range);
  742. } SMARTLIST_FOREACH_END(ent);
  743. return 0;
  744. }
  745. /** Return a string describing the protocols supported by tor version
  746. * <b>version</b>, or an empty string if we cannot tell.
  747. *
  748. * Note that this is only used to infer protocols for Tor versions that
  749. * can't declare their own.
  750. **/
  751. /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
  752. const char *
  753. protover_compute_for_old_tor(const char *version)
  754. {
  755. if (version == NULL) {
  756. /* No known version; guess the oldest series that is still supported. */
  757. version = "0.2.5.15";
  758. }
  759. if (tor_version_as_new_as(version,
  760. FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
  761. return "";
  762. } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
  763. /* 0.2.9.1-alpha HSRend=2 */
  764. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
  765. "Link=1-4 LinkAuth=1 "
  766. "Microdesc=1-2 Relay=1-2";
  767. } else if (tor_version_as_new_as(version, "0.2.7.5")) {
  768. /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
  769. * ed25519 support. We'll call them present only in "stable" 027,
  770. * though. */
  771. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  772. "Link=1-4 LinkAuth=1 "
  773. "Microdesc=1-2 Relay=1-2";
  774. } else if (tor_version_as_new_as(version, "0.2.4.19")) {
  775. /* No currently supported Tor server versions are older than this, or
  776. * lack these protocols. */
  777. return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  778. "Link=1-4 LinkAuth=1 "
  779. "Microdesc=1 Relay=1-2";
  780. } else {
  781. /* Cannot infer protocols. */
  782. return "";
  783. }
  784. }
  785. /**
  786. * Release all storage held by static fields in protover.c
  787. */
  788. void
  789. protover_free_all(void)
  790. {
  791. if (supported_protocol_list) {
  792. smartlist_t *entries = supported_protocol_list;
  793. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  794. smartlist_free(entries);
  795. supported_protocol_list = NULL;
  796. }
  797. }
  798. #endif /* !defined(HAVE_RUST) */