rendcache.c 34 KB

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