protover.c 29 KB

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