versions.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file versions.c
  8. * \brief Code to manipulate, parse, and compare Tor versions.
  9. */
  10. #include "core/or/or.h"
  11. #include "core/or/protover.h"
  12. #include "core/or/versions.h"
  13. #include "lib/crypt_ops/crypto_util.h"
  14. #include "core/or/tor_version_st.h"
  15. /** Return VS_RECOMMENDED if <b>myversion</b> is contained in
  16. * <b>versionlist</b>. Else, return VS_EMPTY if versionlist has no
  17. * entries. Else, return VS_OLD if every member of
  18. * <b>versionlist</b> is newer than <b>myversion</b>. Else, return
  19. * VS_NEW_IN_SERIES if there is at least one member of <b>versionlist</b> in
  20. * the same series (major.minor.micro) as <b>myversion</b>, but no such member
  21. * is newer than <b>myversion.</b>. Else, return VS_NEW if every member of
  22. * <b>versionlist</b> is older than <b>myversion</b>. Else, return
  23. * VS_UNRECOMMENDED.
  24. *
  25. * (versionlist is a comma-separated list of version strings,
  26. * optionally prefixed with "Tor". Versions that can't be parsed are
  27. * ignored.)
  28. */
  29. version_status_t
  30. tor_version_is_obsolete(const char *myversion, const char *versionlist)
  31. {
  32. tor_version_t mine, other;
  33. int found_newer = 0, found_older = 0, found_newer_in_series = 0,
  34. found_any_in_series = 0, r, same;
  35. version_status_t ret = VS_UNRECOMMENDED;
  36. smartlist_t *version_sl;
  37. log_debug(LD_CONFIG,"Checking whether version '%s' is in '%s'",
  38. myversion, versionlist);
  39. if (tor_version_parse(myversion, &mine)) {
  40. log_err(LD_BUG,"I couldn't parse my own version (%s)", myversion);
  41. tor_assert(0);
  42. }
  43. version_sl = smartlist_new();
  44. smartlist_split_string(version_sl, versionlist, ",", SPLIT_SKIP_SPACE, 0);
  45. if (!strlen(versionlist)) { /* no authorities cared or agreed */
  46. ret = VS_EMPTY;
  47. goto done;
  48. }
  49. SMARTLIST_FOREACH_BEGIN(version_sl, const char *, cp) {
  50. if (!strcmpstart(cp, "Tor "))
  51. cp += 4;
  52. if (tor_version_parse(cp, &other)) {
  53. /* Couldn't parse other; it can't be a match. */
  54. } else {
  55. same = tor_version_same_series(&mine, &other);
  56. if (same)
  57. found_any_in_series = 1;
  58. r = tor_version_compare(&mine, &other);
  59. if (r==0) {
  60. ret = VS_RECOMMENDED;
  61. goto done;
  62. } else if (r<0) {
  63. found_newer = 1;
  64. if (same)
  65. found_newer_in_series = 1;
  66. } else if (r>0) {
  67. found_older = 1;
  68. }
  69. }
  70. } SMARTLIST_FOREACH_END(cp);
  71. /* We didn't find the listed version. Is it new or old? */
  72. if (found_any_in_series && !found_newer_in_series && found_newer) {
  73. ret = VS_NEW_IN_SERIES;
  74. } else if (found_newer && !found_older) {
  75. ret = VS_OLD;
  76. } else if (found_older && !found_newer) {
  77. ret = VS_NEW;
  78. } else {
  79. ret = VS_UNRECOMMENDED;
  80. }
  81. done:
  82. SMARTLIST_FOREACH(version_sl, char *, version, tor_free(version));
  83. smartlist_free(version_sl);
  84. return ret;
  85. }
  86. /** Extract a Tor version from a <b>platform</b> line from a router
  87. * descriptor, and place the result in <b>router_version</b>.
  88. *
  89. * Return 1 on success, -1 on parsing failure, and 0 if the
  90. * platform line does not indicate some version of Tor.
  91. *
  92. * If <b>strict</b> is non-zero, finding any weird version components
  93. * (like negative numbers) counts as a parsing failure.
  94. */
  95. int
  96. tor_version_parse_platform(const char *platform,
  97. tor_version_t *router_version,
  98. int strict)
  99. {
  100. char tmp[128];
  101. char *s, *s2, *start;
  102. if (strcmpstart(platform,"Tor ")) /* nonstandard Tor; say 0. */
  103. return 0;
  104. start = (char *)eat_whitespace(platform+3);
  105. if (!*start) return -1;
  106. s = (char *)find_whitespace(start); /* also finds '\0', which is fine */
  107. s2 = (char*)eat_whitespace(s);
  108. if (!strcmpstart(s2, "(r") || !strcmpstart(s2, "(git-"))
  109. s = (char*)find_whitespace(s2);
  110. if ((size_t)(s-start+1) >= sizeof(tmp)) /* too big, no */
  111. return -1;
  112. strlcpy(tmp, start, s-start+1);
  113. if (tor_version_parse(tmp, router_version)<0) {
  114. log_info(LD_DIR,"Router version '%s' unparseable.",tmp);
  115. return -1;
  116. }
  117. if (strict) {
  118. if (router_version->major < 0 ||
  119. router_version->minor < 0 ||
  120. router_version->micro < 0 ||
  121. router_version->patchlevel < 0 ||
  122. router_version->svn_revision < 0) {
  123. return -1;
  124. }
  125. }
  126. return 1;
  127. }
  128. /** Parse the Tor version of the platform string <b>platform</b>,
  129. * and compare it to the version in <b>cutoff</b>. Return 1 if
  130. * the router is at least as new as the cutoff, else return 0.
  131. */
  132. int
  133. tor_version_as_new_as(const char *platform, const char *cutoff)
  134. {
  135. tor_version_t cutoff_version, router_version;
  136. int r;
  137. tor_assert(platform);
  138. if (tor_version_parse(cutoff, &cutoff_version)<0) {
  139. log_warn(LD_BUG,"cutoff version '%s' unparseable.",cutoff);
  140. return 0;
  141. }
  142. r = tor_version_parse_platform(platform, &router_version, 0);
  143. if (r == 0) {
  144. /* nonstandard Tor; be safe and say yes */
  145. return 1;
  146. } else if (r < 0) {
  147. /* unparseable version; be safe and say yes. */
  148. return 1;
  149. }
  150. /* Here's why we don't need to do any special handling for svn revisions:
  151. * - If neither has an svn revision, we're fine.
  152. * - If the router doesn't have an svn revision, we can't assume that it
  153. * is "at least" any svn revision, so we need to return 0.
  154. * - If the target version doesn't have an svn revision, any svn revision
  155. * (or none at all) is good enough, so return 1.
  156. * - If both target and router have an svn revision, we compare them.
  157. */
  158. return tor_version_compare(&router_version, &cutoff_version) >= 0;
  159. }
  160. /** Parse a tor version from <b>s</b>, and store the result in <b>out</b>.
  161. * Return 0 on success, -1 on failure. */
  162. int
  163. tor_version_parse(const char *s, tor_version_t *out)
  164. {
  165. char *eos=NULL;
  166. const char *cp=NULL;
  167. int ok = 1;
  168. /* Format is:
  169. * "Tor " ? NUM dot NUM [ dot NUM [ ( pre | rc | dot ) NUM ] ] [ - tag ]
  170. */
  171. tor_assert(s);
  172. tor_assert(out);
  173. memset(out, 0, sizeof(tor_version_t));
  174. out->status = VER_RELEASE;
  175. if (!strcasecmpstart(s, "Tor "))
  176. s += 4;
  177. cp = s;
  178. #define NUMBER(m) \
  179. do { \
  180. if (!cp || *cp < '0' || *cp > '9') \
  181. return -1; \
  182. out->m = (int)tor_parse_uint64(cp, 10, 0, INT32_MAX, &ok, &eos); \
  183. if (!ok) \
  184. return -1; \
  185. if (!eos || eos == cp) \
  186. return -1; \
  187. cp = eos; \
  188. } while (0)
  189. #define DOT() \
  190. do { \
  191. if (*cp != '.') \
  192. return -1; \
  193. ++cp; \
  194. } while (0)
  195. NUMBER(major);
  196. DOT();
  197. NUMBER(minor);
  198. if (*cp == 0)
  199. return 0;
  200. else if (*cp == '-')
  201. goto status_tag;
  202. DOT();
  203. NUMBER(micro);
  204. /* Get status */
  205. if (*cp == 0) {
  206. return 0;
  207. } else if (*cp == '.') {
  208. ++cp;
  209. } else if (*cp == '-') {
  210. goto status_tag;
  211. } else if (0==strncmp(cp, "pre", 3)) {
  212. out->status = VER_PRE;
  213. cp += 3;
  214. } else if (0==strncmp(cp, "rc", 2)) {
  215. out->status = VER_RC;
  216. cp += 2;
  217. } else {
  218. return -1;
  219. }
  220. NUMBER(patchlevel);
  221. status_tag:
  222. /* Get status tag. */
  223. if (*cp == '-' || *cp == '.')
  224. ++cp;
  225. eos = (char*) find_whitespace(cp);
  226. if (eos-cp >= (int)sizeof(out->status_tag))
  227. strlcpy(out->status_tag, cp, sizeof(out->status_tag));
  228. else {
  229. memcpy(out->status_tag, cp, eos-cp);
  230. out->status_tag[eos-cp] = 0;
  231. }
  232. cp = eat_whitespace(eos);
  233. if (!strcmpstart(cp, "(r")) {
  234. cp += 2;
  235. out->svn_revision = (int) strtol(cp,&eos,10);
  236. } else if (!strcmpstart(cp, "(git-")) {
  237. char *close_paren = strchr(cp, ')');
  238. int hexlen;
  239. char digest[DIGEST_LEN];
  240. if (! close_paren)
  241. return -1;
  242. cp += 5;
  243. if (close_paren-cp > HEX_DIGEST_LEN)
  244. return -1;
  245. hexlen = (int)(close_paren-cp);
  246. memwipe(digest, 0, sizeof(digest));
  247. if ( hexlen == 0 || (hexlen % 2) == 1)
  248. return -1;
  249. if (base16_decode(digest, hexlen/2, cp, hexlen) != hexlen/2)
  250. return -1;
  251. memcpy(out->git_tag, digest, hexlen/2);
  252. out->git_tag_len = hexlen/2;
  253. }
  254. return 0;
  255. #undef NUMBER
  256. #undef DOT
  257. }
  258. /** Compare two tor versions; Return <0 if a < b; 0 if a ==b, >0 if a >
  259. * b. */
  260. int
  261. tor_version_compare(tor_version_t *a, tor_version_t *b)
  262. {
  263. int i;
  264. tor_assert(a);
  265. tor_assert(b);
  266. /* We take this approach to comparison to ensure the same (bogus!) behavior
  267. * on all inputs as we would have seen before bug #21278 was fixed. The
  268. * only important difference here is that this method doesn't cause
  269. * a signed integer underflow.
  270. */
  271. #define CMP(field) do { \
  272. unsigned aval = (unsigned) a->field; \
  273. unsigned bval = (unsigned) b->field; \
  274. int result = (int) (aval - bval); \
  275. if (result < 0) \
  276. return -1; \
  277. else if (result > 0) \
  278. return 1; \
  279. } while (0)
  280. CMP(major);
  281. CMP(minor);
  282. CMP(micro);
  283. CMP(status);
  284. CMP(patchlevel);
  285. if ((i = strcmp(a->status_tag, b->status_tag)))
  286. return i;
  287. CMP(svn_revision);
  288. CMP(git_tag_len);
  289. if (a->git_tag_len)
  290. return fast_memcmp(a->git_tag, b->git_tag, a->git_tag_len);
  291. else
  292. return 0;
  293. #undef CMP
  294. }
  295. /** Return true iff versions <b>a</b> and <b>b</b> belong to the same series.
  296. */
  297. int
  298. tor_version_same_series(tor_version_t *a, tor_version_t *b)
  299. {
  300. tor_assert(a);
  301. tor_assert(b);
  302. return ((a->major == b->major) &&
  303. (a->minor == b->minor) &&
  304. (a->micro == b->micro));
  305. }
  306. /** Helper: Given pointers to two strings describing tor versions, return -1
  307. * if _a precedes _b, 1 if _b precedes _a, and 0 if they are equivalent.
  308. * Used to sort a list of versions. */
  309. static int
  310. compare_tor_version_str_ptr_(const void **_a, const void **_b)
  311. {
  312. const char *a = *_a, *b = *_b;
  313. int ca, cb;
  314. tor_version_t va, vb;
  315. ca = tor_version_parse(a, &va);
  316. cb = tor_version_parse(b, &vb);
  317. /* If they both parse, compare them. */
  318. if (!ca && !cb)
  319. return tor_version_compare(&va,&vb);
  320. /* If one parses, it comes first. */
  321. if (!ca && cb)
  322. return -1;
  323. if (ca && !cb)
  324. return 1;
  325. /* If neither parses, compare strings. Also, the directory server admin
  326. ** needs to be smacked upside the head. But Tor is tolerant and gentle. */
  327. return strcmp(a,b);
  328. }
  329. /** Sort a list of string-representations of versions in ascending order. */
  330. void
  331. sort_version_list(smartlist_t *versions, int remove_duplicates)
  332. {
  333. smartlist_sort(versions, compare_tor_version_str_ptr_);
  334. if (remove_duplicates)
  335. smartlist_uniq(versions, compare_tor_version_str_ptr_, tor_free_);
  336. }
  337. /** Summarize the protocols listed in <b>protocols</b> into <b>out</b>,
  338. * falling back or correcting them based on <b>version</b> as appropriate.
  339. */
  340. void
  341. summarize_protover_flags(protover_summary_flags_t *out,
  342. const char *protocols,
  343. const char *version)
  344. {
  345. tor_assert(out);
  346. memset(out, 0, sizeof(*out));
  347. if (protocols) {
  348. out->protocols_known = 1;
  349. out->supports_extend2_cells =
  350. protocol_list_supports_protocol(protocols, PRT_RELAY, 2);
  351. out->supports_ed25519_link_handshake_compat =
  352. protocol_list_supports_protocol(protocols, PRT_LINKAUTH, 3);
  353. out->supports_ed25519_link_handshake_any =
  354. protocol_list_supports_protocol_or_later(protocols, PRT_LINKAUTH, 3);
  355. out->supports_ed25519_hs_intro =
  356. protocol_list_supports_protocol(protocols, PRT_HSINTRO, 4);
  357. out->supports_v3_hsdir =
  358. protocol_list_supports_protocol(protocols, PRT_HSDIR,
  359. PROTOVER_HSDIR_V3);
  360. out->supports_v3_rendezvous_point =
  361. protocol_list_supports_protocol(protocols, PRT_HSREND,
  362. PROTOVER_HS_RENDEZVOUS_POINT_V3);
  363. }
  364. if (version && !strcmpstart(version, "Tor ")) {
  365. if (!out->protocols_known) {
  366. /* The version is a "Tor" version, and where there is no
  367. * list of protocol versions that we should be looking at instead. */
  368. out->supports_extend2_cells =
  369. tor_version_as_new_as(version, "0.2.4.8-alpha");
  370. out->protocols_known = 1;
  371. } else {
  372. /* Bug #22447 forces us to filter on this version. */
  373. if (!tor_version_as_new_as(version, "0.3.0.8")) {
  374. out->supports_v3_hsdir = 0;
  375. }
  376. }
  377. }
  378. }