rendcache.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /* Copyright (c) 2015-2019, 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/rend/rendcommon.h"
  13. #include "feature/rend/rendparse.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. // PIRONION: v2 lookup
  521. /** Lookup the v2 service descriptor with base32-encoded <b>desc_id</b> and
  522. * copy the pointer to it to *<b>desc</b>. Return 1 on success, 0 on
  523. * well-formed-but-not-found, and -1 on failure.
  524. */
  525. int
  526. rend_cache_lookup_v2_desc_as_dir(const char *desc_id, const char **desc)
  527. {
  528. rend_cache_entry_t *e;
  529. char desc_id_digest[DIGEST_LEN];
  530. tor_assert(rend_cache_v2_dir);
  531. if (base32_decode(desc_id_digest, DIGEST_LEN,
  532. desc_id, REND_DESC_ID_V2_LEN_BASE32) < 0) {
  533. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  534. "Rejecting v2 rendezvous descriptor request -- descriptor ID "
  535. "contains illegal characters: %s",
  536. safe_str(desc_id));
  537. return -1;
  538. }
  539. /* Lookup descriptor and return. */
  540. e = digestmap_get(rend_cache_v2_dir, desc_id_digest);
  541. if (e) {
  542. *desc = e->desc;
  543. e->last_served = approx_time();
  544. return 1;
  545. }
  546. return 0;
  547. }
  548. // PIRONION: v2 store
  549. /** Parse the v2 service descriptor(s) in <b>desc</b> and store it/them to the
  550. * local rend cache. Don't attempt to decrypt the included list of introduction
  551. * points (as we don't have a descriptor cookie for it).
  552. *
  553. * If we have a newer descriptor with the same ID, ignore this one.
  554. * If we have an older descriptor with the same ID, replace it.
  555. *
  556. * Return 0 on success, or -1 if we couldn't parse any of them.
  557. *
  558. * We should only call this function for public (e.g. non bridge) relays.
  559. */
  560. int
  561. rend_cache_store_v2_desc_as_dir(const char *desc)
  562. {
  563. const or_options_t *options = get_options();
  564. rend_service_descriptor_t *parsed;
  565. char desc_id[DIGEST_LEN];
  566. char *intro_content;
  567. size_t intro_size;
  568. size_t encoded_size;
  569. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  570. int number_parsed = 0, number_stored = 0;
  571. const char *current_desc = desc;
  572. const char *next_desc;
  573. rend_cache_entry_t *e;
  574. time_t now = time(NULL);
  575. tor_assert(rend_cache_v2_dir);
  576. tor_assert(desc);
  577. while (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  578. &intro_size, &encoded_size,
  579. &next_desc, current_desc, 1) >= 0) {
  580. number_parsed++;
  581. /* We don't care about the introduction points. */
  582. tor_free(intro_content);
  583. /* For pretty log statements. */
  584. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  585. desc_id, DIGEST_LEN);
  586. /* Is descriptor too old? */
  587. if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  588. log_info(LD_REND, "Service descriptor with desc ID %s is too old.",
  589. safe_str(desc_id_base32));
  590. goto skip;
  591. }
  592. /* Is descriptor too far in the future? */
  593. if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
  594. log_info(LD_REND, "Service descriptor with desc ID %s is too far in the "
  595. "future.",
  596. safe_str(desc_id_base32));
  597. goto skip;
  598. }
  599. /* Do we already have a newer descriptor? */
  600. e = digestmap_get(rend_cache_v2_dir, desc_id);
  601. if (e && e->parsed->timestamp > parsed->timestamp) {
  602. log_info(LD_REND, "We already have a newer service descriptor with the "
  603. "same desc ID %s and version.",
  604. safe_str(desc_id_base32));
  605. goto skip;
  606. }
  607. /* Do we already have this descriptor? */
  608. if (e && !strcmp(desc, e->desc)) {
  609. log_info(LD_REND, "We already have this service descriptor with desc "
  610. "ID %s.", safe_str(desc_id_base32));
  611. goto skip;
  612. }
  613. /* Store received descriptor. */
  614. if (!e) {
  615. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  616. digestmap_set(rend_cache_v2_dir, desc_id, e);
  617. /* Treat something just uploaded as having been served a little
  618. * while ago, so that flooding with new descriptors doesn't help
  619. * too much.
  620. */
  621. e->last_served = approx_time() - 3600;
  622. } else {
  623. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  624. rend_service_descriptor_free(e->parsed);
  625. tor_free(e->desc);
  626. }
  627. e->parsed = parsed;
  628. e->desc = tor_strndup(current_desc, encoded_size);
  629. e->len = encoded_size;
  630. rend_cache_increment_allocation(rend_cache_entry_allocation(e));
  631. log_info(LD_REND, "Successfully stored service descriptor with desc ID "
  632. "'%s' and len %d.",
  633. safe_str(desc_id_base32), (int)encoded_size);
  634. /* Statistics: Note down this potentially new HS. */
  635. if (options->HiddenServiceStatistics) {
  636. rep_hist_stored_maybe_new_hs(e->parsed->pk);
  637. }
  638. number_stored++;
  639. goto advance;
  640. skip:
  641. rend_service_descriptor_free(parsed);
  642. advance:
  643. /* advance to next descriptor, if available. */
  644. current_desc = next_desc;
  645. /* check if there is a next descriptor. */
  646. if (!current_desc ||
  647. strcmpstart(current_desc, "rendezvous-service-descriptor "))
  648. break;
  649. }
  650. if (!number_parsed) {
  651. log_info(LD_REND, "Could not parse any descriptor.");
  652. return -1;
  653. }
  654. log_info(LD_REND, "Parsed %d and added %d descriptor%s.",
  655. number_parsed, number_stored, number_stored != 1 ? "s" : "");
  656. return 0;
  657. }
  658. /** Parse the v2 service descriptor in <b>desc</b> and store it to the
  659. * local service rend cache. Don't attempt to decrypt the included list of
  660. * introduction points.
  661. *
  662. * If we have a newer descriptor with the same ID, ignore this one.
  663. * If we have an older descriptor with the same ID, replace it.
  664. *
  665. * Return 0 on success, or -1 if we couldn't understand the descriptor.
  666. */
  667. int
  668. rend_cache_store_v2_desc_as_service(const char *desc)
  669. {
  670. rend_service_descriptor_t *parsed = NULL;
  671. char desc_id[DIGEST_LEN];
  672. char *intro_content = NULL;
  673. size_t intro_size;
  674. size_t encoded_size;
  675. const char *next_desc;
  676. char service_id[REND_SERVICE_ID_LEN_BASE32+1];
  677. rend_cache_entry_t *e;
  678. int retval = -1;
  679. tor_assert(rend_cache_local_service);
  680. tor_assert(desc);
  681. /* Parse the descriptor. */
  682. if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  683. &intro_size, &encoded_size,
  684. &next_desc, desc, 0) < 0) {
  685. log_warn(LD_REND, "Could not parse descriptor.");
  686. goto err;
  687. }
  688. /* Compute service ID from public key. */
  689. if (rend_get_service_id(parsed->pk, service_id)<0) {
  690. log_warn(LD_REND, "Couldn't compute service ID.");
  691. goto err;
  692. }
  693. /* Do we already have a newer descriptor? Allow new descriptors with a
  694. rounded timestamp equal to or newer than the current descriptor */
  695. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache_local_service,
  696. service_id);
  697. if (e && e->parsed->timestamp > parsed->timestamp) {
  698. log_info(LD_REND, "We already have a newer service descriptor for "
  699. "service ID %s.", safe_str_client(service_id));
  700. goto okay;
  701. }
  702. /* We don't care about the introduction points. */
  703. tor_free(intro_content);
  704. if (!e) {
  705. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  706. strmap_set_lc(rend_cache_local_service, service_id, e);
  707. } else {
  708. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  709. rend_service_descriptor_free(e->parsed);
  710. tor_free(e->desc);
  711. }
  712. e->parsed = parsed;
  713. e->desc = tor_malloc_zero(encoded_size + 1);
  714. strlcpy(e->desc, desc, encoded_size + 1);
  715. e->len = encoded_size;
  716. rend_cache_increment_allocation(rend_cache_entry_allocation(e));
  717. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  718. safe_str_client(service_id), (int)encoded_size);
  719. return 0;
  720. okay:
  721. retval = 0;
  722. err:
  723. rend_service_descriptor_free(parsed);
  724. tor_free(intro_content);
  725. return retval;
  726. }
  727. /** Parse the v2 service descriptor in <b>desc</b>, decrypt the included list
  728. * of introduction points with <b>descriptor_cookie</b> (which may also be
  729. * <b>NULL</b> if decryption is not necessary), and store the descriptor to
  730. * the local cache under its version and service id.
  731. *
  732. * If we have a newer v2 descriptor with the same ID, ignore this one.
  733. * If we have an older descriptor with the same ID, replace it.
  734. * If the descriptor's service ID does not match
  735. * <b>rend_query</b>-\>onion_address, reject it.
  736. *
  737. * If the descriptor's descriptor ID doesn't match <b>desc_id_base32</b>,
  738. * reject it.
  739. *
  740. * Return 0 on success, or -1 if we rejected the descriptor.
  741. * If entry is not NULL, set it with the cache entry pointer of the descriptor.
  742. */
  743. int
  744. rend_cache_store_v2_desc_as_client(const char *desc,
  745. const char *desc_id_base32,
  746. const rend_data_t *rend_query,
  747. rend_cache_entry_t **entry)
  748. {
  749. /*XXXX this seems to have a bit of duplicate code with
  750. * rend_cache_store_v2_desc_as_dir(). Fix that. */
  751. /* Though having similar elements, both functions were separated on
  752. * purpose:
  753. * - dirs don't care about encoded/encrypted introduction points, clients
  754. * do.
  755. * - dirs store descriptors in a separate cache by descriptor ID, whereas
  756. * clients store them by service ID; both caches are different data
  757. * structures and have different access methods.
  758. * - dirs store a descriptor only if they are responsible for its ID,
  759. * clients do so in every way (because they have requested it before).
  760. * - dirs can process multiple concatenated descriptors which is required
  761. * for replication, whereas clients only accept a single descriptor.
  762. * Thus, combining both methods would result in a lot of if statements
  763. * which probably would not improve, but worsen code readability. -KL */
  764. rend_service_descriptor_t *parsed = NULL;
  765. char desc_id[DIGEST_LEN];
  766. char *intro_content = NULL;
  767. size_t intro_size;
  768. size_t encoded_size;
  769. const char *next_desc;
  770. time_t now = time(NULL);
  771. char key[REND_SERVICE_ID_LEN_BASE32+2];
  772. char service_id[REND_SERVICE_ID_LEN_BASE32+1];
  773. char want_desc_id[DIGEST_LEN];
  774. rend_cache_entry_t *e;
  775. int retval = -1;
  776. rend_data_v2_t *rend_data = TO_REND_DATA_V2(rend_query);
  777. tor_assert(rend_cache);
  778. tor_assert(desc);
  779. tor_assert(desc_id_base32);
  780. memset(want_desc_id, 0, sizeof(want_desc_id));
  781. if (entry) {
  782. *entry = NULL;
  783. }
  784. if (base32_decode(want_desc_id, sizeof(want_desc_id),
  785. desc_id_base32, strlen(desc_id_base32)) != 0) {
  786. log_warn(LD_BUG, "Couldn't decode base32 %s for descriptor id.",
  787. escaped_safe_str_client(desc_id_base32));
  788. goto err;
  789. }
  790. /* Parse the descriptor. */
  791. if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
  792. &intro_size, &encoded_size,
  793. &next_desc, desc, 0) < 0) {
  794. log_warn(LD_REND, "Could not parse descriptor.");
  795. goto err;
  796. }
  797. /* Compute service ID from public key. */
  798. if (rend_get_service_id(parsed->pk, service_id)<0) {
  799. log_warn(LD_REND, "Couldn't compute service ID.");
  800. goto err;
  801. }
  802. if (rend_data->onion_address[0] != '\0' &&
  803. strcmp(rend_data->onion_address, service_id)) {
  804. log_warn(LD_REND, "Received service descriptor for service ID %s; "
  805. "expected descriptor for service ID %s.",
  806. service_id, safe_str(rend_data->onion_address));
  807. goto err;
  808. }
  809. if (tor_memneq(desc_id, want_desc_id, DIGEST_LEN)) {
  810. log_warn(LD_REND, "Received service descriptor for %s with incorrect "
  811. "descriptor ID.", service_id);
  812. goto err;
  813. }
  814. /* Decode/decrypt introduction points. */
  815. if (intro_content && intro_size > 0) {
  816. int n_intro_points;
  817. if (rend_data->auth_type != REND_NO_AUTH &&
  818. !tor_mem_is_zero(rend_data->descriptor_cookie,
  819. sizeof(rend_data->descriptor_cookie))) {
  820. char *ipos_decrypted = NULL;
  821. size_t ipos_decrypted_size;
  822. if (rend_decrypt_introduction_points(&ipos_decrypted,
  823. &ipos_decrypted_size,
  824. rend_data->descriptor_cookie,
  825. intro_content,
  826. intro_size) < 0) {
  827. log_warn(LD_REND, "Failed to decrypt introduction points. We are "
  828. "probably unable to parse the encoded introduction points.");
  829. } else {
  830. /* Replace encrypted with decrypted introduction points. */
  831. log_info(LD_REND, "Successfully decrypted introduction points.");
  832. tor_free(intro_content);
  833. intro_content = ipos_decrypted;
  834. intro_size = ipos_decrypted_size;
  835. }
  836. }
  837. n_intro_points = rend_parse_introduction_points(parsed, intro_content,
  838. intro_size);
  839. if (n_intro_points <= 0) {
  840. log_warn(LD_REND, "Failed to parse introduction points. Either the "
  841. "service has published a corrupt descriptor or you have "
  842. "provided invalid authorization data.");
  843. goto err;
  844. } else if (n_intro_points > MAX_INTRO_POINTS) {
  845. log_warn(LD_REND, "Found too many introduction points on a hidden "
  846. "service descriptor for %s. This is probably a (misguided) "
  847. "attempt to improve reliability, but it could also be an "
  848. "attempt to do a guard enumeration attack. Rejecting.",
  849. safe_str_client(service_id));
  850. goto err;
  851. }
  852. } else {
  853. log_info(LD_REND, "Descriptor does not contain any introduction points.");
  854. parsed->intro_nodes = smartlist_new();
  855. }
  856. /* We don't need the encoded/encrypted introduction points any longer. */
  857. tor_free(intro_content);
  858. /* Is descriptor too old? */
  859. if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
  860. log_warn(LD_REND, "Service descriptor with service ID %s is too old.",
  861. safe_str_client(service_id));
  862. goto err;
  863. }
  864. /* Is descriptor too far in the future? */
  865. if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
  866. log_warn(LD_REND, "Service descriptor with service ID %s is too far in "
  867. "the future.", safe_str_client(service_id));
  868. goto err;
  869. }
  870. /* Do we have the same exact copy already in our cache? */
  871. tor_snprintf(key, sizeof(key), "2%s", service_id);
  872. e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
  873. if (e && !strcmp(desc, e->desc)) {
  874. log_info(LD_REND,"We already have this service descriptor %s.",
  875. safe_str_client(service_id));
  876. goto okay;
  877. }
  878. /* Verify that we are not replacing an older descriptor. It's important to
  879. * avoid an evil HSDir serving old descriptor. We validate if the
  880. * timestamp is greater than and not equal because it's a rounded down
  881. * timestamp to the hour so if the descriptor changed in the same hour,
  882. * the rend cache failure will tell us if we have a new descriptor. */
  883. if (e && e->parsed->timestamp > parsed->timestamp) {
  884. log_info(LD_REND, "We already have a new enough service descriptor for "
  885. "service ID %s with the same desc ID and version.",
  886. safe_str_client(service_id));
  887. goto okay;
  888. }
  889. /* Lookup our failure cache for intro point that might be unusable. */
  890. validate_intro_point_failure(parsed, service_id);
  891. /* It's now possible that our intro point list is empty, which means that
  892. * this descriptor is useless to us because intro points have all failed
  893. * somehow before. Discard the descriptor. */
  894. if (smartlist_len(parsed->intro_nodes) == 0) {
  895. log_info(LD_REND, "Service descriptor with service ID %s has no "
  896. "usable intro points. Discarding it.",
  897. safe_str_client(service_id));
  898. goto err;
  899. }
  900. /* Now either purge the current one and replace its content or create a
  901. * new one and add it to the rend cache. */
  902. if (!e) {
  903. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  904. strmap_set_lc(rend_cache, key, e);
  905. } else {
  906. rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
  907. rend_cache_failure_remove(e->parsed);
  908. rend_service_descriptor_free(e->parsed);
  909. tor_free(e->desc);
  910. }
  911. e->parsed = parsed;
  912. e->desc = tor_malloc_zero(encoded_size + 1);
  913. strlcpy(e->desc, desc, encoded_size + 1);
  914. e->len = encoded_size;
  915. rend_cache_increment_allocation(rend_cache_entry_allocation(e));
  916. log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
  917. safe_str_client(service_id), (int)encoded_size);
  918. if (entry) {
  919. *entry = e;
  920. }
  921. return 0;
  922. okay:
  923. if (entry) {
  924. *entry = e;
  925. }
  926. retval = 0;
  927. err:
  928. rend_service_descriptor_free(parsed);
  929. tor_free(intro_content);
  930. return retval;
  931. }