rendcache.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* Copyright (c) 2015-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file rendcache.c
  5. * \brief Hidden service descriptor cache.
  6. **/
  7. #define RENDCACHE_PRIVATE
  8. #include "rendcache.h"
  9. #include "config.h"
  10. #include "rephist.h"
  11. #include "routerlist.h"
  12. #include "routerparse.h"
  13. #include "rendcommon.h"
  14. #include "rend_service_descriptor_st.h"
  15. /** Map from service id (as generated by rend_get_service_id) to
  16. * rend_cache_entry_t. */
  17. STATIC strmap_t *rend_cache = NULL;
  18. /** Map from service id to rend_cache_entry_t; only for hidden services. */
  19. static strmap_t *rend_cache_local_service = NULL;
  20. /** Map from descriptor id to rend_cache_entry_t; only for hidden service
  21. * directories. */
  22. STATIC digestmap_t *rend_cache_v2_dir = NULL;
  23. /** (Client side only) Map from service id to rend_cache_failure_t. This
  24. * cache is used to track intro point(IP) failures so we know when to keep
  25. * or discard a new descriptor we just fetched. Here is a description of the
  26. * cache behavior.
  27. *
  28. * Everytime tor discards an IP (ex: receives a NACK), we add an entry to
  29. * this cache noting the identity digest of the IP and it's failure type for
  30. * the service ID. The reason we indexed this cache by service ID is to
  31. * differentiate errors that can occur only for a specific service like a
  32. * NACK for instance. It applies for one but maybe not for the others.
  33. *
  34. * Once a service descriptor is fetched and considered valid, each IP is
  35. * looked up in this cache and if present, it is discarded from the fetched
  36. * descriptor. At the end, all IP(s) in the cache, for a specific service
  37. * ID, that were NOT present in the descriptor are removed from this cache.
  38. * Which means that if at least one IP was not in this cache, thus usuable,
  39. * it's considered a new descriptor so we keep it. Else, if all IPs were in
  40. * this cache, we discard the descriptor as it's considered unusable.
  41. *
  42. * Once a descriptor is removed from the rend cache or expires, the entry
  43. * in this cache is also removed for the service ID.
  44. *
  45. * This scheme allows us to not rely on the descriptor's timestamp (which
  46. * is rounded down to the hour) to know if we have a newer descriptor. We
  47. * only rely on the usability of intro points from an internal state. */
  48. STATIC strmap_t *rend_cache_failure = NULL;
  49. /* DOCDOC */
  50. STATIC size_t rend_cache_total_allocation = 0;
  51. /** Initializes the service descriptor cache.
  52. */
  53. void
  54. rend_cache_init(void)
  55. {
  56. rend_cache = strmap_new();
  57. rend_cache_v2_dir = digestmap_new();
  58. rend_cache_local_service = strmap_new();
  59. rend_cache_failure = strmap_new();
  60. }
  61. /** Return the approximate number of bytes needed to hold <b>e</b>. */
  62. STATIC size_t
  63. rend_cache_entry_allocation(const rend_cache_entry_t *e)
  64. {
  65. if (!e)
  66. return 0;
  67. /* This doesn't count intro_nodes or key size */
  68. return sizeof(*e) + e->len + sizeof(*e->parsed);
  69. }
  70. /* DOCDOC */
  71. size_t
  72. rend_cache_get_total_allocation(void)
  73. {
  74. return rend_cache_total_allocation;
  75. }
  76. /** Decrement the total bytes attributed to the rendezvous cache by n. */
  77. void
  78. rend_cache_decrement_allocation(size_t n)
  79. {
  80. static int have_underflowed = 0;
  81. if (rend_cache_total_allocation >= n) {
  82. rend_cache_total_allocation -= n;
  83. } else {
  84. rend_cache_total_allocation = 0;
  85. if (! have_underflowed) {
  86. have_underflowed = 1;
  87. log_warn(LD_BUG, "Underflow in rend_cache_decrement_allocation");
  88. }
  89. }
  90. }
  91. /** Increase the total bytes attributed to the rendezvous cache by n. */
  92. void
  93. rend_cache_increment_allocation(size_t n)
  94. {
  95. static int have_overflowed = 0;
  96. if (rend_cache_total_allocation <= SIZE_MAX - n) {
  97. rend_cache_total_allocation += n;
  98. } else {
  99. rend_cache_total_allocation = SIZE_MAX;
  100. if (! have_overflowed) {
  101. have_overflowed = 1;
  102. log_warn(LD_BUG, "Overflow in rend_cache_increment_allocation");
  103. }
  104. }
  105. }
  106. /** Helper: free a rend cache failure intro object. */
  107. STATIC void
  108. rend_cache_failure_intro_entry_free_(rend_cache_failure_intro_t *entry)
  109. {
  110. if (entry == NULL) {
  111. return;
  112. }
  113. tor_free(entry);
  114. }
  115. static void
  116. rend_cache_failure_intro_entry_free_void(void *entry)
  117. {
  118. rend_cache_failure_intro_entry_free_(entry);
  119. }
  120. /** Allocate a rend cache failure intro object and return it. <b>failure</b>
  121. * is set into the object. This function can not fail. */
  122. STATIC rend_cache_failure_intro_t *
  123. rend_cache_failure_intro_entry_new(rend_intro_point_failure_t failure)
  124. {
  125. rend_cache_failure_intro_t *entry = tor_malloc(sizeof(*entry));
  126. entry->failure_type = failure;
  127. entry->created_ts = time(NULL);
  128. return entry;
  129. }
  130. /** Helper: free a rend cache failure object. */
  131. STATIC void
  132. rend_cache_failure_entry_free_(rend_cache_failure_t *entry)
  133. {
  134. if (entry == NULL) {
  135. return;
  136. }
  137. /* Free and remove every intro failure object. */
  138. digestmap_free(entry->intro_failures,
  139. rend_cache_failure_intro_entry_free_void);
  140. tor_free(entry);
  141. }
  142. /** Helper: deallocate a rend_cache_failure_t. (Used with strmap_free(),
  143. * which requires a function pointer whose argument is void*). */
  144. STATIC void
  145. rend_cache_failure_entry_free_void(void *entry)
  146. {
  147. rend_cache_failure_entry_free_(entry);
  148. }
  149. /** Allocate a rend cache failure object and return it. This function can
  150. * not fail. */
  151. STATIC rend_cache_failure_t *
  152. rend_cache_failure_entry_new(void)
  153. {
  154. rend_cache_failure_t *entry = tor_malloc(sizeof(*entry));
  155. entry->intro_failures = digestmap_new();
  156. return entry;
  157. }
  158. /** Remove failure cache entry for the service ID in the given descriptor
  159. * <b>desc</b>. */
  160. STATIC void
  161. rend_cache_failure_remove(rend_service_descriptor_t *desc)
  162. {
  163. char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
  164. rend_cache_failure_t *entry;
  165. if (desc == NULL) {
  166. return;
  167. }
  168. if (rend_get_service_id(desc->pk, service_id) < 0) {
  169. return;
  170. }
  171. entry = strmap_get_lc(rend_cache_failure, service_id);
  172. if (entry != NULL) {
  173. strmap_remove_lc(rend_cache_failure, service_id);
  174. rend_cache_failure_entry_free(entry);
  175. }
  176. }
  177. /** Helper: free storage held by a single service descriptor cache entry. */
  178. STATIC void
  179. rend_cache_entry_free_(rend_cache_entry_t *e)
  180. {
  181. if (!e)
  182. return;
  183. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  184. /* We are about to remove a descriptor from the cache so remove the entry
  185. * in the failure cache. */
  186. rend_cache_failure_remove(e->parsed);
  187. rend_service_descriptor_free(e->parsed);
  188. tor_free(e->desc);
  189. tor_free(e);
  190. }
  191. /** Helper: deallocate a rend_cache_entry_t. (Used with strmap_free(), which
  192. * requires a function pointer whose argument is void*). */
  193. static void
  194. rend_cache_entry_free_void(void *p)
  195. {
  196. rend_cache_entry_free_(p);
  197. }
  198. /** Free all storage held by the service descriptor cache. */
  199. void
  200. rend_cache_free_all(void)
  201. {
  202. strmap_free(rend_cache, rend_cache_entry_free_void);
  203. digestmap_free(rend_cache_v2_dir, rend_cache_entry_free_void);
  204. strmap_free(rend_cache_local_service, rend_cache_entry_free_void);
  205. strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void);
  206. rend_cache = NULL;
  207. rend_cache_v2_dir = NULL;
  208. rend_cache_local_service = NULL;
  209. rend_cache_failure = NULL;
  210. rend_cache_total_allocation = 0;
  211. }
  212. /** Remove all entries that re REND_CACHE_FAILURE_MAX_AGE old. This is
  213. * called every second.
  214. *
  215. * We have to clean these regurlarly else if for whatever reasons an hidden
  216. * service goes offline and a client tries to connect to it during that
  217. * time, a failure entry is created and the client will be unable to connect
  218. * for a while even though the service has return online. */
  219. void
  220. rend_cache_failure_clean(time_t now)
  221. {
  222. time_t cutoff = now - REND_CACHE_FAILURE_MAX_AGE;
  223. STRMAP_FOREACH_MODIFY(rend_cache_failure, key,
  224. rend_cache_failure_t *, ent) {
  225. /* Free and remove every intro failure object that match the cutoff. */
  226. DIGESTMAP_FOREACH_MODIFY(ent->intro_failures, ip_key,
  227. rend_cache_failure_intro_t *, ip_ent) {
  228. if (ip_ent->created_ts < cutoff) {
  229. rend_cache_failure_intro_entry_free(ip_ent);
  230. MAP_DEL_CURRENT(ip_key);
  231. }
  232. } DIGESTMAP_FOREACH_END;
  233. /* If the entry is now empty of intro point failures, remove it. */
  234. if (digestmap_isempty(ent->intro_failures)) {
  235. rend_cache_failure_entry_free(ent);
  236. MAP_DEL_CURRENT(key);
  237. }
  238. } STRMAP_FOREACH_END;
  239. }
  240. /** Removes all old entries from the client or service descriptor cache.
  241. */
  242. void
  243. rend_cache_clean(time_t now, rend_cache_type_t cache_type)
  244. {
  245. strmap_iter_t *iter;
  246. const char *key;
  247. void *val;
  248. rend_cache_entry_t *ent;
  249. time_t cutoff = now - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
  250. strmap_t *cache = NULL;
  251. if (cache_type == REND_CACHE_TYPE_CLIENT) {
  252. cache = rend_cache;
  253. } else if (cache_type == REND_CACHE_TYPE_SERVICE) {
  254. cache = rend_cache_local_service;
  255. }
  256. tor_assert(cache);
  257. for (iter = strmap_iter_init(cache); !strmap_iter_done(iter); ) {
  258. strmap_iter_get(iter, &key, &val);
  259. ent = (rend_cache_entry_t*)val;
  260. if (ent->parsed->timestamp < cutoff) {
  261. iter = strmap_iter_next_rmv(cache, iter);
  262. rend_cache_entry_free(ent);
  263. } else {
  264. iter = strmap_iter_next(cache, iter);
  265. }
  266. }
  267. }
  268. /** Remove ALL entries from the rendezvous service descriptor cache.
  269. */
  270. void
  271. rend_cache_purge(void)
  272. {
  273. if (rend_cache) {
  274. log_info(LD_REND, "Purging HS v2 descriptor cache");
  275. strmap_free(rend_cache, rend_cache_entry_free_void);
  276. }
  277. rend_cache = strmap_new();
  278. }
  279. /** Remove ALL entries from the failure cache. This is also called when a
  280. * NEWNYM signal is received. */
  281. void
  282. rend_cache_failure_purge(void)
  283. {
  284. if (rend_cache_failure) {
  285. log_info(LD_REND, "Purging HS v2 failure cache");
  286. strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void);
  287. }
  288. rend_cache_failure = strmap_new();
  289. }
  290. /** Lookup the rend failure cache using a relay identity digest in
  291. * <b>identity</b> which has DIGEST_LEN bytes and service ID <b>service_id</b>
  292. * which is a null-terminated string. If found, the intro failure is set in
  293. * <b>intro_entry</b> else it stays untouched. Return 1 iff found else 0. */
  294. STATIC int
  295. cache_failure_intro_lookup(const uint8_t *identity, const char *service_id,
  296. rend_cache_failure_intro_t **intro_entry)
  297. {
  298. rend_cache_failure_t *elem;
  299. rend_cache_failure_intro_t *intro_elem;
  300. tor_assert(rend_cache_failure);
  301. if (intro_entry) {
  302. *intro_entry = NULL;
  303. }
  304. /* Lookup descriptor and return it. */
  305. elem = strmap_get_lc(rend_cache_failure, service_id);
  306. if (elem == NULL) {
  307. goto not_found;
  308. }
  309. intro_elem = digestmap_get(elem->intro_failures, (char *) identity);
  310. if (intro_elem == NULL) {
  311. goto not_found;
  312. }
  313. if (intro_entry) {
  314. *intro_entry = intro_elem;
  315. }
  316. return 1;
  317. not_found:
  318. return 0;
  319. }
  320. /** Allocate a new cache failure intro object and copy the content from
  321. * <b>entry</b> to this newly allocated object. Return it. */
  322. static rend_cache_failure_intro_t *
  323. cache_failure_intro_dup(const rend_cache_failure_intro_t *entry)
  324. {
  325. rend_cache_failure_intro_t *ent_dup =
  326. rend_cache_failure_intro_entry_new(entry->failure_type);
  327. ent_dup->created_ts = entry->created_ts;
  328. return ent_dup;
  329. }
  330. /** Add an intro point failure to the failure cache using the relay
  331. * <b>identity</b> and service ID <b>service_id</b>. Record the
  332. * <b>failure</b> in that object. */
  333. STATIC void
  334. cache_failure_intro_add(const uint8_t *identity, const char *service_id,
  335. rend_intro_point_failure_t failure)
  336. {
  337. rend_cache_failure_t *fail_entry;
  338. rend_cache_failure_intro_t *entry, *old_entry;
  339. /* Make sure we have a failure object for this service ID and if not,
  340. * create it with this new intro failure entry. */
  341. fail_entry = strmap_get_lc(rend_cache_failure, service_id);
  342. if (fail_entry == NULL) {
  343. fail_entry = rend_cache_failure_entry_new();
  344. /* Add failure entry to global rend failure cache. */
  345. strmap_set_lc(rend_cache_failure, service_id, fail_entry);
  346. }
  347. entry = rend_cache_failure_intro_entry_new(failure);
  348. old_entry = digestmap_set(fail_entry->intro_failures,
  349. (char *) identity, entry);
  350. /* This _should_ be NULL, but in case it isn't, free it. */
  351. rend_cache_failure_intro_entry_free(old_entry);
  352. }
  353. /** Using a parsed descriptor <b>desc</b>, check if the introduction points
  354. * are present in the failure cache and if so they are removed from the
  355. * descriptor and kept into the failure cache. Then, each intro points that
  356. * are NOT in the descriptor but in the failure cache for the given
  357. * <b>service_id</b> are removed from the failure cache. */
  358. STATIC void
  359. validate_intro_point_failure(const rend_service_descriptor_t *desc,
  360. const char *service_id)
  361. {
  362. rend_cache_failure_t *new_entry, *cur_entry;
  363. /* New entry for the service ID that will be replacing the one in the
  364. * failure cache since we have a new descriptor. In the case where all
  365. * intro points are removed, we are assured that the new entry is the same
  366. * as the current one. */
  367. new_entry = tor_malloc(sizeof(*new_entry));
  368. new_entry->intro_failures = digestmap_new();
  369. tor_assert(desc);
  370. SMARTLIST_FOREACH_BEGIN(desc->intro_nodes, rend_intro_point_t *, intro) {
  371. int found;
  372. rend_cache_failure_intro_t *entry;
  373. const uint8_t *identity =
  374. (uint8_t *) intro->extend_info->identity_digest;
  375. found = cache_failure_intro_lookup(identity, service_id, &entry);
  376. if (found) {
  377. /* Dup here since it will be freed at the end when removing the
  378. * original entry in the cache. */
  379. rend_cache_failure_intro_t *ent_dup = cache_failure_intro_dup(entry);
  380. /* This intro point is in our cache, discard it from the descriptor
  381. * because chances are that it's unusable. */
  382. SMARTLIST_DEL_CURRENT(desc->intro_nodes, intro);
  383. /* Keep it for our new entry. */
  384. digestmap_set(new_entry->intro_failures, (char *) identity, ent_dup);
  385. /* Only free it when we're done looking at it. */
  386. rend_intro_point_free(intro);
  387. continue;
  388. }
  389. } SMARTLIST_FOREACH_END(intro);
  390. /* Swap the failure entry in the cache and free the current one. */
  391. cur_entry = strmap_get_lc(rend_cache_failure, service_id);
  392. if (cur_entry != NULL) {
  393. rend_cache_failure_entry_free(cur_entry);
  394. }
  395. strmap_set_lc(rend_cache_failure, service_id, new_entry);
  396. }
  397. /** Note down an intro failure in the rend failure cache using the type of
  398. * failure in <b>failure</b> for the relay identity digest in
  399. * <b>identity</b> and service ID <b>service_id</b>. If an entry already
  400. * exists in the cache, the failure type is changed with <b>failure</b>. */
  401. void
  402. rend_cache_intro_failure_note(rend_intro_point_failure_t failure,
  403. const uint8_t *identity,
  404. const char *service_id)
  405. {
  406. int found;
  407. rend_cache_failure_intro_t *entry;
  408. found = cache_failure_intro_lookup(identity, service_id, &entry);
  409. if (!found) {
  410. cache_failure_intro_add(identity, service_id, failure);
  411. } else {
  412. /* Replace introduction point failure with this one. */
  413. entry->failure_type = failure;
  414. }
  415. }
  416. /** Remove all old v2 descriptors and those for which this hidden service
  417. * directory is not responsible for any more. The cutoff is the time limit for
  418. * which we want to keep the cache entry. In other words, any entry created
  419. * before will be removed. */
  420. size_t
  421. rend_cache_clean_v2_descs_as_dir(time_t cutoff)
  422. {
  423. digestmap_iter_t *iter;
  424. size_t bytes_removed = 0;
  425. for (iter = digestmap_iter_init(rend_cache_v2_dir);
  426. !digestmap_iter_done(iter); ) {
  427. const char *key;
  428. void *val;
  429. rend_cache_entry_t *ent;
  430. digestmap_iter_get(iter, &key, &val);
  431. ent = val;
  432. if (ent->parsed->timestamp < cutoff) {
  433. char key_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  434. base32_encode(key_base32, sizeof(key_base32), key, DIGEST_LEN);
  435. log_info(LD_REND, "Removing descriptor with ID '%s' from cache",
  436. safe_str_client(key_base32));
  437. bytes_removed += rend_cache_entry_allocation(ent);
  438. iter = digestmap_iter_next_rmv(rend_cache_v2_dir, iter);
  439. rend_cache_entry_free(ent);
  440. } else {
  441. iter = digestmap_iter_next(rend_cache_v2_dir, iter);
  442. }
  443. }
  444. return bytes_removed;
  445. }
  446. /** Lookup in the client cache the given service ID <b>query</b> for
  447. * <b>version</b>.
  448. *
  449. * Return 0 if found and if <b>e</b> is non NULL, set it with the entry
  450. * found. Else, a negative value is returned and <b>e</b> is untouched.
  451. * -EINVAL means that <b>query</b> is not a valid service id.
  452. * -ENOENT means that no entry in the cache was found. */
  453. int
  454. rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e)
  455. {
  456. int ret = 0;
  457. char key[REND_SERVICE_ID_LEN_BASE32 + 2]; /* <version><query>\0 */
  458. rend_cache_entry_t *entry = NULL;
  459. static const int default_version = 2;
  460. tor_assert(rend_cache);
  461. tor_assert(query);
  462. if (!rend_valid_v2_service_id(query)) {
  463. ret = -EINVAL;
  464. goto end;
  465. }
  466. switch (version) {
  467. case 0:
  468. log_warn(LD_REND, "Cache lookup of a v0 renddesc is deprecated.");
  469. break;
  470. case 2:
  471. /* Default is version 2. */
  472. default:
  473. tor_snprintf(key, sizeof(key), "%d%s", default_version, query);
  474. entry = strmap_get_lc(rend_cache, key);
  475. break;
  476. }
  477. if (!entry) {
  478. ret = -ENOENT;
  479. goto end;
  480. }
  481. tor_assert(entry->parsed && entry->parsed->intro_nodes);
  482. if (e) {
  483. *e = entry;
  484. }
  485. end:
  486. return ret;
  487. }
  488. /*
  489. * Lookup the v2 service descriptor with the service ID <b>query</b> in the
  490. * local service descriptor cache. Return 0 if found and if <b>e</b> is
  491. * non NULL, set it with the entry found. Else, a negative value is returned
  492. * and <b>e</b> is untouched.
  493. * -EINVAL means that <b>query</b> is not a valid service id.
  494. * -ENOENT means that no entry in the cache was found. */
  495. int
  496. rend_cache_lookup_v2_desc_as_service(const char *query, rend_cache_entry_t **e)
  497. {
  498. int ret = 0;
  499. rend_cache_entry_t *entry = NULL;
  500. tor_assert(rend_cache_local_service);
  501. tor_assert(query);
  502. if (!rend_valid_v2_service_id(query)) {
  503. ret = -EINVAL;
  504. goto end;
  505. }
  506. /* Lookup descriptor and return. */
  507. entry = strmap_get_lc(rend_cache_local_service, query);
  508. if (!entry) {
  509. ret = -ENOENT;
  510. goto end;
  511. }
  512. if (e) {
  513. *e = entry;
  514. }
  515. end:
  516. return ret;
  517. }
  518. /** Lookup the v2 service descriptor with base32-encoded <b>desc_id</b> and
  519. * copy the pointer to it to *<b>desc</b>. Return 1 on success, 0 on
  520. * well-formed-but-not-found, and -1 on failure.
  521. */
  522. int
  523. rend_cache_lookup_v2_desc_as_dir(const char *desc_id, const char **desc)
  524. {
  525. rend_cache_entry_t *e;
  526. char desc_id_digest[DIGEST_LEN];
  527. tor_assert(rend_cache_v2_dir);
  528. if (base32_decode(desc_id_digest, DIGEST_LEN,
  529. desc_id, REND_DESC_ID_V2_LEN_BASE32) < 0) {
  530. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  531. "Rejecting v2 rendezvous descriptor request -- descriptor ID "
  532. "contains illegal characters: %s",
  533. safe_str(desc_id));
  534. return -1;
  535. }
  536. /* Lookup descriptor and return. */
  537. e = digestmap_get(rend_cache_v2_dir, desc_id_digest);
  538. if (e) {
  539. *desc = e->desc;
  540. e->last_served = approx_time();
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. /** Parse the v2 service descriptor(s) in <b>desc</b> and store it/them to the
  546. * local rend cache. Don't attempt to decrypt the included list of introduction
  547. * points (as we don't have a descriptor cookie for it).
  548. *
  549. * If we have a newer descriptor with the same ID, ignore this one.
  550. * If we have an older descriptor with the same ID, replace it.
  551. *
  552. * Return 0 on success, or -1 if we couldn't parse any of them.
  553. *
  554. * We should only call this function for public (e.g. non bridge) relays.
  555. */
  556. int
  557. rend_cache_store_v2_desc_as_dir(const char *desc)
  558. {
  559. const or_options_t *options = get_options();
  560. rend_service_descriptor_t *parsed;
  561. char desc_id[DIGEST_LEN];
  562. char *intro_content;
  563. size_t intro_size;
  564. size_t encoded_size;
  565. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  566. int number_parsed = 0, number_stored = 0;
  567. const char *current_desc = desc;
  568. const char *next_desc;
  569. rend_cache_entry_t *e;
  570. time_t now = time(NULL);
  571. tor_assert(rend_cache_v2_dir);
  572. tor_assert(desc);
  573. while (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  574. &intro_size, &encoded_size,
  575. &next_desc, current_desc, 1) >= 0) {
  576. number_parsed++;
  577. /* We don't care about the introduction points. */
  578. tor_free(intro_content);
  579. /* For pretty log statements. */
  580. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  581. desc_id, DIGEST_LEN);
  582. /* Is descriptor too old? */
  583. if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  584. log_info(LD_REND, "Service descriptor with desc ID %s is too old.",
  585. safe_str(desc_id_base32));
  586. goto skip;
  587. }
  588. /* Is descriptor too far in the future? */
  589. if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
  590. log_info(LD_REND, "Service descriptor with desc ID %s is too far in the "
  591. "future.",
  592. safe_str(desc_id_base32));
  593. goto skip;
  594. }
  595. /* Do we already have a newer descriptor? */
  596. e = digestmap_get(rend_cache_v2_dir, desc_id);
  597. if (e && e->parsed->timestamp > parsed->timestamp) {
  598. log_info(LD_REND, "We already have a newer service descriptor with the "
  599. "same desc ID %s and version.",
  600. safe_str(desc_id_base32));
  601. goto skip;
  602. }
  603. /* Do we already have this descriptor? */
  604. if (e && !strcmp(desc, e->desc)) {
  605. log_info(LD_REND, "We already have this service descriptor with desc "
  606. "ID %s.", safe_str(desc_id_base32));
  607. goto skip;
  608. }
  609. /* Store received descriptor. */
  610. if (!e) {
  611. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  612. digestmap_set(rend_cache_v2_dir, desc_id, e);
  613. /* Treat something just uploaded as having been served a little
  614. * while ago, so that flooding with new descriptors doesn't help
  615. * too much.
  616. */
  617. e->last_served = approx_time() - 3600;
  618. } else {
  619. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  620. rend_service_descriptor_free(e->parsed);
  621. tor_free(e->desc);
  622. }
  623. e->parsed = parsed;
  624. e->desc = tor_strndup(current_desc, encoded_size);
  625. e->len = encoded_size;
  626. rend_cache_increment_allocation(rend_cache_entry_allocation(e));
  627. log_info(LD_REND, "Successfully stored service descriptor with desc ID "
  628. "'%s' and len %d.",
  629. safe_str(desc_id_base32), (int)encoded_size);
  630. /* Statistics: Note down this potentially new HS. */
  631. if (options->HiddenServiceStatistics) {
  632. rep_hist_stored_maybe_new_hs(e->parsed->pk);
  633. }
  634. number_stored++;
  635. goto advance;
  636. skip:
  637. rend_service_descriptor_free(parsed);
  638. advance:
  639. /* advance to next descriptor, if available. */
  640. current_desc = next_desc;
  641. /* check if there is a next descriptor. */
  642. if (!current_desc ||
  643. strcmpstart(current_desc, "rendezvous-service-descriptor "))
  644. break;
  645. }
  646. if (!number_parsed) {
  647. log_info(LD_REND, "Could not parse any descriptor.");
  648. return -1;
  649. }
  650. log_info(LD_REND, "Parsed %d and added %d descriptor%s.",
  651. number_parsed, number_stored, number_stored != 1 ? "s" : "");
  652. return 0;
  653. }
  654. /** Parse the v2 service descriptor in <b>desc</b> and store it to the
  655. * local service rend cache. Don't attempt to decrypt the included list of
  656. * introduction points.
  657. *
  658. * If we have a newer descriptor with the same ID, ignore this one.
  659. * If we have an older descriptor with the same ID, replace it.
  660. *
  661. * Return 0 on success, or -1 if we couldn't understand the descriptor.
  662. */
  663. int
  664. rend_cache_store_v2_desc_as_service(const char *desc)
  665. {
  666. rend_service_descriptor_t *parsed = NULL;
  667. char desc_id[DIGEST_LEN];
  668. char *intro_content = NULL;
  669. size_t intro_size;
  670. size_t encoded_size;
  671. const char *next_desc;
  672. char service_id[REND_SERVICE_ID_LEN_BASE32+1];
  673. rend_cache_entry_t *e;
  674. int retval = -1;
  675. tor_assert(rend_cache_local_service);
  676. tor_assert(desc);
  677. /* Parse the descriptor. */
  678. if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  679. &intro_size, &encoded_size,
  680. &next_desc, desc, 0) < 0) {
  681. log_warn(LD_REND, "Could not parse descriptor.");
  682. goto err;
  683. }
  684. /* Compute service ID from public key. */
  685. if (rend_get_service_id(parsed->pk, service_id)<0) {
  686. log_warn(LD_REND, "Couldn't compute service ID.");
  687. goto err;
  688. }
  689. /* Do we already have a newer descriptor? Allow new descriptors with a
  690. rounded timestamp equal to or newer than the current descriptor */
  691. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache_local_service,
  692. service_id);
  693. if (e && e->parsed->timestamp > parsed->timestamp) {
  694. log_info(LD_REND, "We already have a newer service descriptor for "
  695. "service ID %s.", safe_str_client(service_id));
  696. goto okay;
  697. }
  698. /* We don't care about the introduction points. */
  699. tor_free(intro_content);
  700. if (!e) {
  701. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  702. strmap_set_lc(rend_cache_local_service, service_id, e);
  703. } else {
  704. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  705. rend_service_descriptor_free(e->parsed);
  706. tor_free(e->desc);
  707. }
  708. e->parsed = parsed;
  709. e->desc = tor_malloc_zero(encoded_size + 1);
  710. strlcpy(e->desc, desc, encoded_size + 1);
  711. e->len = encoded_size;
  712. rend_cache_increment_allocation(rend_cache_entry_allocation(e));
  713. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  714. safe_str_client(service_id), (int)encoded_size);
  715. return 0;
  716. okay:
  717. retval = 0;
  718. err:
  719. rend_service_descriptor_free(parsed);
  720. tor_free(intro_content);
  721. return retval;
  722. }
  723. /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
  724. * of introduction points with <b>descriptor_cookie</b> (which may also be
  725. * <b>NULL</b> if decryption is not necessary), and store the descriptor to
  726. * the local cache under its version and service id.
  727. *
  728. * If we have a newer v2 descriptor with the same ID, ignore this one.
  729. * If we have an older descriptor with the same ID, replace it.
  730. * If the descriptor's service ID does not match
  731. * <b>rend_query</b>-\>onion_address, reject it.
  732. *
  733. * If the descriptor's descriptor ID doesn't match <b>desc_id_base32</b>,
  734. * reject it.
  735. *
  736. * Return 0 on success, or -1 if we rejected the descriptor.
  737. * If entry is not NULL, set it with the cache entry pointer of the descriptor.
  738. */
  739. int
  740. rend_cache_store_v2_desc_as_client(const char *desc,
  741. const char *desc_id_base32,
  742. const rend_data_t *rend_query,
  743. rend_cache_entry_t **entry)
  744. {
  745. /*XXXX this seems to have a bit of duplicate code with
  746. * rend_cache_store_v2_desc_as_dir(). Fix that. */
  747. /* Though having similar elements, both functions were separated on
  748. * purpose:
  749. * - dirs don't care about encoded/encrypted introduction points, clients
  750. * do.
  751. * - dirs store descriptors in a separate cache by descriptor ID, whereas
  752. * clients store them by service ID; both caches are different data
  753. * structures and have different access methods.
  754. * - dirs store a descriptor only if they are responsible for its ID,
  755. * clients do so in every way (because they have requested it before).
  756. * - dirs can process multiple concatenated descriptors which is required
  757. * for replication, whereas clients only accept a single descriptor.
  758. * Thus, combining both methods would result in a lot of if statements
  759. * which probably would not improve, but worsen code readability. -KL */
  760. rend_service_descriptor_t *parsed = NULL;
  761. char desc_id[DIGEST_LEN];
  762. char *intro_content = NULL;
  763. size_t intro_size;
  764. size_t encoded_size;
  765. const char *next_desc;
  766. time_t now = time(NULL);
  767. char key[REND_SERVICE_ID_LEN_BASE32+2];
  768. char service_id[REND_SERVICE_ID_LEN_BASE32+1];
  769. char want_desc_id[DIGEST_LEN];
  770. rend_cache_entry_t *e;
  771. int retval = -1;
  772. rend_data_v2_t *rend_data = TO_REND_DATA_V2(rend_query);
  773. tor_assert(rend_cache);
  774. tor_assert(desc);
  775. tor_assert(desc_id_base32);
  776. memset(want_desc_id, 0, sizeof(want_desc_id));
  777. if (entry) {
  778. *entry = NULL;
  779. }
  780. if (base32_decode(want_desc_id, sizeof(want_desc_id),
  781. desc_id_base32, strlen(desc_id_base32)) != 0) {
  782. log_warn(LD_BUG, "Couldn't decode base32 %s for descriptor id.",
  783. escaped_safe_str_client(desc_id_base32));
  784. goto err;
  785. }
  786. /* Parse the descriptor. */
  787. if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  788. &intro_size, &encoded_size,
  789. &next_desc, desc, 0) < 0) {
  790. log_warn(LD_REND, "Could not parse descriptor.");
  791. goto err;
  792. }
  793. /* Compute service ID from public key. */
  794. if (rend_get_service_id(parsed->pk, service_id)<0) {
  795. log_warn(LD_REND, "Couldn't compute service ID.");
  796. goto err;
  797. }
  798. if (rend_data->onion_address[0] != '\0' &&
  799. strcmp(rend_data->onion_address, service_id)) {
  800. log_warn(LD_REND, "Received service descriptor for service ID %s; "
  801. "expected descriptor for service ID %s.",
  802. service_id, safe_str(rend_data->onion_address));
  803. goto err;
  804. }
  805. if (tor_memneq(desc_id, want_desc_id, DIGEST_LEN)) {
  806. log_warn(LD_REND, "Received service descriptor for %s with incorrect "
  807. "descriptor ID.", service_id);
  808. goto err;
  809. }
  810. /* Decode/decrypt introduction points. */
  811. if (intro_content && intro_size > 0) {
  812. int n_intro_points;
  813. if (rend_data->auth_type != REND_NO_AUTH &&
  814. !tor_mem_is_zero(rend_data->descriptor_cookie,
  815. sizeof(rend_data->descriptor_cookie))) {
  816. char *ipos_decrypted = NULL;
  817. size_t ipos_decrypted_size;
  818. if (rend_decrypt_introduction_points(&ipos_decrypted,
  819. &ipos_decrypted_size,
  820. rend_data->descriptor_cookie,
  821. intro_content,
  822. intro_size) < 0) {
  823. log_warn(LD_REND, "Failed to decrypt introduction points. We are "
  824. "probably unable to parse the encoded introduction points.");
  825. } else {
  826. /* Replace encrypted with decrypted introduction points. */
  827. log_info(LD_REND, "Successfully decrypted introduction points.");
  828. tor_free(intro_content);
  829. intro_content = ipos_decrypted;
  830. intro_size = ipos_decrypted_size;
  831. }
  832. }
  833. n_intro_points = rend_parse_introduction_points(parsed, intro_content,
  834. intro_size);
  835. if (n_intro_points <= 0) {
  836. log_warn(LD_REND, "Failed to parse introduction points. Either the "
  837. "service has published a corrupt descriptor or you have "
  838. "provided invalid authorization data, or (maybe!) the "
  839. "server is deliberately serving broken data in an attempt "
  840. "to crash you with bug 21018.");
  841. goto err;
  842. } else if (n_intro_points > MAX_INTRO_POINTS) {
  843. log_warn(LD_REND, "Found too many introduction points on a hidden "
  844. "service descriptor for %s. This is probably a (misguided) "
  845. "attempt to improve reliability, but it could also be an "
  846. "attempt to do a guard enumeration attack. Rejecting.",
  847. safe_str_client(service_id));
  848. goto err;
  849. }
  850. } else {
  851. log_info(LD_REND, "Descriptor does not contain any introduction points.");
  852. parsed->intro_nodes = smartlist_new();
  853. }
  854. /* We don't need the encoded/encrypted introduction points any longer. */
  855. tor_free(intro_content);
  856. /* Is descriptor too old? */
  857. if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  858. log_warn(LD_REND, "Service descriptor with service ID %s is too old.",
  859. safe_str_client(service_id));
  860. goto err;
  861. }
  862. /* Is descriptor too far in the future? */
  863. if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
  864. log_warn(LD_REND, "Service descriptor with service ID %s is too far in "
  865. "the future.", safe_str_client(service_id));
  866. goto err;
  867. }
  868. /* Do we have the same exact copy already in our cache? */
  869. tor_snprintf(key, sizeof(key), "2%s", service_id);
  870. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
  871. if (e && !strcmp(desc, e->desc)) {
  872. log_info(LD_REND,"We already have this service descriptor %s.",
  873. safe_str_client(service_id));
  874. goto okay;
  875. }
  876. /* Verify that we are not replacing an older descriptor. It's important to
  877. * avoid an evil HSDir serving old descriptor. We validate if the
  878. * timestamp is greater than and not equal because it's a rounded down
  879. * timestamp to the hour so if the descriptor changed in the same hour,
  880. * the rend cache failure will tell us if we have a new descriptor. */
  881. if (e && e->parsed->timestamp > parsed->timestamp) {
  882. log_info(LD_REND, "We already have a new enough service descriptor for "
  883. "service ID %s with the same desc ID and version.",
  884. safe_str_client(service_id));
  885. goto okay;
  886. }
  887. /* Lookup our failure cache for intro point that might be unusable. */
  888. validate_intro_point_failure(parsed, service_id);
  889. /* It's now possible that our intro point list is empty, which means that
  890. * this descriptor is useless to us because intro points have all failed
  891. * somehow before. Discard the descriptor. */
  892. if (smartlist_len(parsed->intro_nodes) == 0) {
  893. log_info(LD_REND, "Service descriptor with service ID %s has no "
  894. "usable intro points. Discarding it.",
  895. safe_str_client(service_id));
  896. goto err;
  897. }
  898. /* Now either purge the current one and replace its content or create a
  899. * new one and add it to the rend cache. */
  900. if (!e) {
  901. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  902. strmap_set_lc(rend_cache, key, e);
  903. } else {
  904. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  905. rend_cache_failure_remove(e->parsed);
  906. rend_service_descriptor_free(e->parsed);
  907. tor_free(e->desc);
  908. }
  909. e->parsed = parsed;
  910. e->desc = tor_malloc_zero(encoded_size + 1);
  911. strlcpy(e->desc, desc, encoded_size + 1);
  912. e->len = encoded_size;
  913. rend_cache_increment_allocation(rend_cache_entry_allocation(e));
  914. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  915. safe_str_client(service_id), (int)encoded_size);
  916. if (entry) {
  917. *entry = e;
  918. }
  919. return 0;
  920. okay:
  921. if (entry) {
  922. *entry = e;
  923. }
  924. retval = 0;
  925. err:
  926. rend_service_descriptor_free(parsed);
  927. tor_free(intro_content);
  928. return retval;
  929. }