protover.c 28 KB

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