rendcache.c 34 KB

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