rendcache.c 34 KB

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