rendcache.c 29 KB

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