#define RENDCACHE_PRIVATE
#include "feature/rend/rendcache.h"
#include "app/config/config.h"
#include "feature/stats/rephist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/rend/rendcommon.h"
#include "feature/rend/rendparse.h"
#include "core/or/extend_info_st.h"
#include "feature/rend/rend_intro_point_st.h"
#include "feature/rend/rend_service_descriptor_st.h"
#include "lib/ctime/di_ops.h"
STATIC strmap_t *rend_cache = NULL;
static strmap_t *rend_cache_local_service = NULL;
STATIC digestmap_t *rend_cache_v2_dir = NULL;
STATIC strmap_t *rend_cache_failure = NULL;
STATIC size_t rend_cache_total_allocation = 0;
void
rend_cache_init(void)
{
rend_cache = strmap_new();
rend_cache_v2_dir = digestmap_new();
rend_cache_local_service = strmap_new();
rend_cache_failure = strmap_new();
}
STATIC size_t
rend_cache_entry_allocation(const rend_cache_entry_t *e)
{
if (!e)
return 0;
return sizeof(*e) + e->len + sizeof(*e->parsed);
}
size_t
rend_cache_get_total_allocation(void)
{
return rend_cache_total_allocation;
}
void
rend_cache_decrement_allocation(size_t n)
{
static int have_underflowed = 0;
if (rend_cache_total_allocation >= n) {
rend_cache_total_allocation -= n;
} else {
rend_cache_total_allocation = 0;
if (! have_underflowed) {
have_underflowed = 1;
log_warn(LD_BUG, "Underflow in rend_cache_decrement_allocation");
}
}
}
void
rend_cache_increment_allocation(size_t n)
{
static int have_overflowed = 0;
if (rend_cache_total_allocation <= SIZE_MAX - n) {
rend_cache_total_allocation += n;
} else {
rend_cache_total_allocation = SIZE_MAX;
if (! have_overflowed) {
have_overflowed = 1;
log_warn(LD_BUG, "Overflow in rend_cache_increment_allocation");
}
}
}
STATIC void
rend_cache_failure_intro_entry_free_(rend_cache_failure_intro_t *entry)
{
if (entry == NULL) {
return;
}
tor_free(entry);
}
static void
rend_cache_failure_intro_entry_free_void(void *entry)
{
rend_cache_failure_intro_entry_free_(entry);
}
STATIC rend_cache_failure_intro_t *
rend_cache_failure_intro_entry_new(rend_intro_point_failure_t failure)
{
rend_cache_failure_intro_t *entry = tor_malloc(sizeof(*entry));
entry->failure_type = failure;
entry->created_ts = time(NULL);
return entry;
}
STATIC void
rend_cache_failure_entry_free_(rend_cache_failure_t *entry)
{
if (entry == NULL) {
return;
}
digestmap_free(entry->intro_failures,
rend_cache_failure_intro_entry_free_void);
tor_free(entry);
}
STATIC void
rend_cache_failure_entry_free_void(void *entry)
{
rend_cache_failure_entry_free_(entry);
}
STATIC rend_cache_failure_t *
rend_cache_failure_entry_new(void)
{
rend_cache_failure_t *entry = tor_malloc(sizeof(*entry));
entry->intro_failures = digestmap_new();
return entry;
}
STATIC void
rend_cache_failure_remove(rend_service_descriptor_t *desc)
{
char service_id[REND_SERVICE_ID_LEN_BASE32 + 1];
rend_cache_failure_t *entry;
if (desc == NULL) {
return;
}
if (rend_get_service_id(desc->pk, service_id) < 0) {
return;
}
entry = strmap_get_lc(rend_cache_failure, service_id);
if (entry != NULL) {
strmap_remove_lc(rend_cache_failure, service_id);
rend_cache_failure_entry_free(entry);
}
}
STATIC void
rend_cache_entry_free_(rend_cache_entry_t *e)
{
if (!e)
return;
rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
rend_cache_failure_remove(e->parsed);
rend_service_descriptor_free(e->parsed);
tor_free(e->desc);
tor_free(e);
}
static void
rend_cache_entry_free_void(void *p)
{
rend_cache_entry_free_(p);
}
bool
rend_cache_intro_failure_exists(const char *service_id,
const uint8_t *intro_identity)
{
tor_assert(service_id);
tor_assert(intro_identity);
return cache_failure_intro_lookup(intro_identity, service_id, NULL);
}
void
rend_cache_free_all(void)
{
strmap_free(rend_cache, rend_cache_entry_free_void);
digestmap_free(rend_cache_v2_dir, rend_cache_entry_free_void);
strmap_free(rend_cache_local_service, rend_cache_entry_free_void);
strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void);
rend_cache = NULL;
rend_cache_v2_dir = NULL;
rend_cache_local_service = NULL;
rend_cache_failure = NULL;
rend_cache_total_allocation = 0;
}
void
rend_cache_failure_clean(time_t now)
{
time_t cutoff = now - REND_CACHE_FAILURE_MAX_AGE;
STRMAP_FOREACH_MODIFY(rend_cache_failure, key,
rend_cache_failure_t *, ent) {
DIGESTMAP_FOREACH_MODIFY(ent->intro_failures, ip_key,
rend_cache_failure_intro_t *, ip_ent) {
if (ip_ent->created_ts < cutoff) {
rend_cache_failure_intro_entry_free(ip_ent);
MAP_DEL_CURRENT(ip_key);
}
} DIGESTMAP_FOREACH_END;
if (digestmap_isempty(ent->intro_failures)) {
rend_cache_failure_entry_free(ent);
MAP_DEL_CURRENT(key);
}
} STRMAP_FOREACH_END;
}
void
rend_cache_clean(time_t now, rend_cache_type_t cache_type)
{
strmap_iter_t *iter;
const char *key;
void *val;
rend_cache_entry_t *ent;
time_t cutoff = now - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
strmap_t *cache = NULL;
if (cache_type == REND_CACHE_TYPE_CLIENT) {
cache = rend_cache;
} else if (cache_type == REND_CACHE_TYPE_SERVICE) {
cache = rend_cache_local_service;
}
tor_assert(cache);
for (iter = strmap_iter_init(cache); !strmap_iter_done(iter); ) {
strmap_iter_get(iter, &key, &val);
ent = (rend_cache_entry_t*)val;
if (ent->parsed->timestamp < cutoff) {
iter = strmap_iter_next_rmv(cache, iter);
rend_cache_entry_free(ent);
} else {
iter = strmap_iter_next(cache, iter);
}
}
}
void
rend_cache_purge(void)
{
if (rend_cache) {
log_info(LD_REND, "Purging HS v2 descriptor cache");
strmap_free(rend_cache, rend_cache_entry_free_void);
}
rend_cache = strmap_new();
}
void
rend_cache_failure_purge(void)
{
if (rend_cache_failure) {
log_info(LD_REND, "Purging HS v2 failure cache");
strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void);
}
rend_cache_failure = strmap_new();
}
STATIC int
cache_failure_intro_lookup(const uint8_t *identity, const char *service_id,
rend_cache_failure_intro_t **intro_entry)
{
rend_cache_failure_t *elem;
rend_cache_failure_intro_t *intro_elem;
tor_assert(rend_cache_failure);
if (intro_entry) {
*intro_entry = NULL;
}
elem = strmap_get_lc(rend_cache_failure, service_id);
if (elem == NULL) {
goto not_found;
}
intro_elem = digestmap_get(elem->intro_failures, (char *) identity);
if (intro_elem == NULL) {
goto not_found;
}
if (intro_entry) {
*intro_entry = intro_elem;
}
return 1;
not_found:
return 0;
}
static rend_cache_failure_intro_t *
cache_failure_intro_dup(const rend_cache_failure_intro_t *entry)
{
rend_cache_failure_intro_t *ent_dup =
rend_cache_failure_intro_entry_new(entry->failure_type);
ent_dup->created_ts = entry->created_ts;
return ent_dup;
}
STATIC void
cache_failure_intro_add(const uint8_t *identity, const char *service_id,
rend_intro_point_failure_t failure)
{
rend_cache_failure_t *fail_entry;
rend_cache_failure_intro_t *entry, *old_entry;
fail_entry = strmap_get_lc(rend_cache_failure, service_id);
if (fail_entry == NULL) {
fail_entry = rend_cache_failure_entry_new();
strmap_set_lc(rend_cache_failure, service_id, fail_entry);
}
entry = rend_cache_failure_intro_entry_new(failure);
old_entry = digestmap_set(fail_entry->intro_failures,
(char *) identity, entry);
rend_cache_failure_intro_entry_free(old_entry);
}
STATIC void
validate_intro_point_failure(const rend_service_descriptor_t *desc,
const char *service_id)
{
rend_cache_failure_t *new_entry, *cur_entry;
new_entry = tor_malloc(sizeof(*new_entry));
new_entry->intro_failures = digestmap_new();
tor_assert(desc);
SMARTLIST_FOREACH_BEGIN(desc->intro_nodes, rend_intro_point_t *, intro) {
int found;
rend_cache_failure_intro_t *entry;
const uint8_t *identity =
(uint8_t *) intro->extend_info->identity_digest;
found = cache_failure_intro_lookup(identity, service_id, &entry);
if (found) {
rend_cache_failure_intro_t *ent_dup = cache_failure_intro_dup(entry);
SMARTLIST_DEL_CURRENT(desc->intro_nodes, intro);
digestmap_set(new_entry->intro_failures, (char *) identity, ent_dup);
rend_intro_point_free(intro);
continue;
}
} SMARTLIST_FOREACH_END(intro);
cur_entry = strmap_get_lc(rend_cache_failure, service_id);
if (cur_entry != NULL) {
rend_cache_failure_entry_free(cur_entry);
}
strmap_set_lc(rend_cache_failure, service_id, new_entry);
}
void
rend_cache_intro_failure_note(rend_intro_point_failure_t failure,
const uint8_t *identity,
const char *service_id)
{
int found;
rend_cache_failure_intro_t *entry;
found = cache_failure_intro_lookup(identity, service_id, &entry);
if (!found) {
cache_failure_intro_add(identity, service_id, failure);
} else {
entry->failure_type = failure;
}
}
size_t
rend_cache_clean_v2_descs_as_dir(time_t cutoff)
{
digestmap_iter_t *iter;
size_t bytes_removed = 0;
for (iter = digestmap_iter_init(rend_cache_v2_dir);
!digestmap_iter_done(iter); ) {
const char *key;
void *val;
rend_cache_entry_t *ent;
digestmap_iter_get(iter, &key, &val);
ent = val;
if (ent->parsed->timestamp < cutoff) {
char key_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
base32_encode(key_base32, sizeof(key_base32), key, DIGEST_LEN);
log_info(LD_REND, "Removing descriptor with ID '%s' from cache",
safe_str_client(key_base32));
bytes_removed += rend_cache_entry_allocation(ent);
iter = digestmap_iter_next_rmv(rend_cache_v2_dir, iter);
rend_cache_entry_free(ent);
} else {
iter = digestmap_iter_next(rend_cache_v2_dir, iter);
}
}
return bytes_removed;
}
int
rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e)
{
int ret = 0;
char key[REND_SERVICE_ID_LEN_BASE32 + 2];
rend_cache_entry_t *entry = NULL;
static const int default_version = 2;
tor_assert(query);
if (!rend_cache) {
ret = -ENOENT;
goto end;
}
if (!rend_valid_v2_service_id(query)) {
ret = -EINVAL;
goto end;
}
switch (version) {
case 0:
log_warn(LD_REND, "Cache lookup of a v0 renddesc is deprecated.");
break;
case 2:
default:
tor_snprintf(key, sizeof(key), "%d%s", default_version, query);
entry = strmap_get_lc(rend_cache, key);
break;
}
if (!entry) {
ret = -ENOENT;
goto end;
}
tor_assert(entry->parsed && entry->parsed->intro_nodes);
if (e) {
*e = entry;
}
end:
return ret;
}
int
rend_cache_lookup_v2_desc_as_service(const char *query, rend_cache_entry_t **e)
{
int ret = 0;
rend_cache_entry_t *entry = NULL;
tor_assert(rend_cache_local_service);
tor_assert(query);
if (!rend_valid_v2_service_id(query)) {
ret = -EINVAL;
goto end;
}
entry = strmap_get_lc(rend_cache_local_service, query);
if (!entry) {
ret = -ENOENT;
goto end;
}
if (e) {
*e = entry;
}
end:
return ret;
}
int
rend_cache_lookup_v2_desc_as_dir(const char *desc_id, const char **desc)
{
rend_cache_entry_t *e;
char desc_id_digest[DIGEST_LEN];
tor_assert(rend_cache_v2_dir);
if (base32_decode(desc_id_digest, DIGEST_LEN,
desc_id, REND_DESC_ID_V2_LEN_BASE32) != DIGEST_LEN) {
log_fn(LOG_PROTOCOL_WARN, LD_REND,
"Rejecting v2 rendezvous descriptor request -- descriptor ID "
"has wrong length or illegal characters: %s",
safe_str(desc_id));
return -1;
}
e = digestmap_get(rend_cache_v2_dir, desc_id_digest);
if (e) {
*desc = e->desc;
e->last_served = approx_time();
return 1;
}
return 0;
}
int
rend_cache_store_v2_desc_as_dir(const char *desc)
{
const or_options_t *options = get_options();
rend_service_descriptor_t *parsed;
char desc_id[DIGEST_LEN];
char *intro_content;
size_t intro_size;
size_t encoded_size;
char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
int number_parsed = 0, number_stored = 0;
const char *current_desc = desc;
const char *next_desc;
rend_cache_entry_t *e;
time_t now = time(NULL);
tor_assert(rend_cache_v2_dir);
tor_assert(desc);
while (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
&intro_size, &encoded_size,
&next_desc, current_desc, 1) >= 0) {
number_parsed++;
tor_free(intro_content);
base32_encode(desc_id_base32, sizeof(desc_id_base32),
desc_id, DIGEST_LEN);
if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
log_info(LD_REND, "Service descriptor with desc ID %s is too old.",
safe_str(desc_id_base32));
goto skip;
}
if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
log_info(LD_REND, "Service descriptor with desc ID %s is too far in the "
"future.",
safe_str(desc_id_base32));
goto skip;
}
e = digestmap_get(rend_cache_v2_dir, desc_id);
if (e && e->parsed->timestamp > parsed->timestamp) {
log_info(LD_REND, "We already have a newer service descriptor with the "
"same desc ID %s and version.",
safe_str(desc_id_base32));
goto skip;
}
if (e && !strcmp(desc, e->desc)) {
log_info(LD_REND, "We already have this service descriptor with desc "
"ID %s.", safe_str(desc_id_base32));
goto skip;
}
if (!e) {
e = tor_malloc_zero(sizeof(rend_cache_entry_t));
digestmap_set(rend_cache_v2_dir, desc_id, e);
e->last_served = approx_time() - 3600;
} else {
rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
rend_service_descriptor_free(e->parsed);
tor_free(e->desc);
}
e->parsed = parsed;
e->desc = tor_strndup(current_desc, encoded_size);
e->len = encoded_size;
rend_cache_increment_allocation(rend_cache_entry_allocation(e));
log_info(LD_REND, "Successfully stored service descriptor with desc ID "
"'%s' and len %d.",
safe_str(desc_id_base32), (int)encoded_size);
if (options->HiddenServiceStatistics) {
rep_hist_stored_maybe_new_hs(e->parsed->pk);
}
number_stored++;
goto advance;
skip:
rend_service_descriptor_free(parsed);
advance:
current_desc = next_desc;
if (!current_desc ||
strcmpstart(current_desc, "rendezvous-service-descriptor "))
break;
}
if (!number_parsed) {
log_info(LD_REND, "Could not parse any descriptor.");
return -1;
}
log_info(LD_REND, "Parsed %d and added %d descriptor%s.",
number_parsed, number_stored, number_stored != 1 ? "s" : "");
return 0;
}
int
rend_cache_store_v2_desc_as_service(const char *desc)
{
rend_service_descriptor_t *parsed = NULL;
char desc_id[DIGEST_LEN];
char *intro_content = NULL;
size_t intro_size;
size_t encoded_size;
const char *next_desc;
char service_id[REND_SERVICE_ID_LEN_BASE32+1];
rend_cache_entry_t *e;
int retval = -1;
tor_assert(rend_cache_local_service);
tor_assert(desc);
if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
&intro_size, &encoded_size,
&next_desc, desc, 0) < 0) {
log_warn(LD_REND, "Could not parse descriptor.");
goto err;
}
if (rend_get_service_id(parsed->pk, service_id)<0) {
log_warn(LD_REND, "Couldn't compute service ID.");
goto err;
}
e = (rend_cache_entry_t*) strmap_get_lc(rend_cache_local_service,
service_id);
if (e && e->parsed->timestamp > parsed->timestamp) {
log_info(LD_REND, "We already have a newer service descriptor for "
"service ID %s.", safe_str_client(service_id));
goto okay;
}
tor_free(intro_content);
if (!e) {
e = tor_malloc_zero(sizeof(rend_cache_entry_t));
strmap_set_lc(rend_cache_local_service, service_id, e);
} else {
rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
rend_service_descriptor_free(e->parsed);
tor_free(e->desc);
}
e->parsed = parsed;
e->desc = tor_malloc_zero(encoded_size + 1);
strlcpy(e->desc, desc, encoded_size + 1);
e->len = encoded_size;
rend_cache_increment_allocation(rend_cache_entry_allocation(e));
log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
safe_str_client(service_id), (int)encoded_size);
return 0;
okay:
retval = 0;
err:
rend_service_descriptor_free(parsed);
tor_free(intro_content);
return retval;
}
int
rend_cache_store_v2_desc_as_client(const char *desc,
const char *desc_id_base32,
const rend_data_t *rend_query,
rend_cache_entry_t **entry)
{
rend_service_descriptor_t *parsed = NULL;
char desc_id[DIGEST_LEN];
char *intro_content = NULL;
size_t intro_size;
size_t encoded_size;
const char *next_desc;
time_t now = time(NULL);
char key[REND_SERVICE_ID_LEN_BASE32+2];
char service_id[REND_SERVICE_ID_LEN_BASE32+1];
char want_desc_id[DIGEST_LEN];
rend_cache_entry_t *e;
int retval = -1;
rend_data_v2_t *rend_data = TO_REND_DATA_V2(rend_query);
tor_assert(rend_cache);
tor_assert(desc);
tor_assert(desc_id_base32);
memset(want_desc_id, 0, sizeof(want_desc_id));
if (entry) {
*entry = NULL;
}
if (base32_decode(want_desc_id, sizeof(want_desc_id),
desc_id_base32, strlen(desc_id_base32)) !=
sizeof(want_desc_id)) {
log_warn(LD_BUG, "Couldn't decode base32 %s for descriptor id.",
escaped_safe_str_client(desc_id_base32));
goto err;
}
if (rend_parse_v2_service_descriptor(&parsed, desc_id, &intro_content,
&intro_size, &encoded_size,
&next_desc, desc, 0) < 0) {
log_warn(LD_REND, "Could not parse descriptor.");
goto err;
}
if (rend_get_service_id(parsed->pk, service_id)<0) {
log_warn(LD_REND, "Couldn't compute service ID.");
goto err;
}
if (rend_data->onion_address[0] != '\0' &&
strcmp(rend_data->onion_address, service_id)) {
log_warn(LD_REND, "Received service descriptor for service ID %s; "
"expected descriptor for service ID %s.",
service_id, safe_str(rend_data->onion_address));
goto err;
}
if (tor_memneq(desc_id, want_desc_id, DIGEST_LEN)) {
log_warn(LD_REND, "Received service descriptor for %s with incorrect "
"descriptor ID.", service_id);
goto err;
}
if (intro_content && intro_size > 0) {
int n_intro_points;
if (rend_data->auth_type != REND_NO_AUTH &&
!safe_mem_is_zero(rend_data->descriptor_cookie,
sizeof(rend_data->descriptor_cookie))) {
char *ipos_decrypted = NULL;
size_t ipos_decrypted_size;
if (rend_decrypt_introduction_points(&ipos_decrypted,
&ipos_decrypted_size,
rend_data->descriptor_cookie,
intro_content,
intro_size) < 0) {
log_warn(LD_REND, "Failed to decrypt introduction points. We are "
"probably unable to parse the encoded introduction points.");
} else {
log_info(LD_REND, "Successfully decrypted introduction points.");
tor_free(intro_content);
intro_content = ipos_decrypted;
intro_size = ipos_decrypted_size;
}
}
n_intro_points = rend_parse_introduction_points(parsed, intro_content,
intro_size);
if (n_intro_points <= 0) {
log_warn(LD_REND, "Failed to parse introduction points. Either the "
"service has published a corrupt descriptor or you have "
"provided invalid authorization data.");
goto err;
} else if (n_intro_points > MAX_INTRO_POINTS) {
log_warn(LD_REND, "Found too many introduction points on a hidden "
"service descriptor for %s. This is probably a (misguided) "
"attempt to improve reliability, but it could also be an "
"attempt to do a guard enumeration attack. Rejecting.",
safe_str_client(service_id));
goto err;
}
} else {
log_info(LD_REND, "Descriptor does not contain any introduction points.");
parsed->intro_nodes = smartlist_new();
}
tor_free(intro_content);
if (parsed->timestamp < now - REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
log_warn(LD_REND, "Service descriptor with service ID %s is too old.",
safe_str_client(service_id));
goto err;
}
if (parsed->timestamp > now + REND_CACHE_MAX_SKEW) {
log_warn(LD_REND, "Service descriptor with service ID %s is too far in "
"the future.", safe_str_client(service_id));
goto err;
}
tor_snprintf(key, sizeof(key), "2%s", service_id);
e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
if (e && !strcmp(desc, e->desc)) {
log_info(LD_REND,"We already have this service descriptor %s.",
safe_str_client(service_id));
goto okay;
}
if (e && e->parsed->timestamp > parsed->timestamp) {
log_info(LD_REND, "We already have a new enough service descriptor for "
"service ID %s with the same desc ID and version.",
safe_str_client(service_id));
goto okay;
}
validate_intro_point_failure(parsed, service_id);
if (smartlist_len(parsed->intro_nodes) == 0) {
log_info(LD_REND, "Service descriptor with service ID %s has no "
"usable intro points. Discarding it.",
safe_str_client(service_id));
goto err;
}
if (!e) {
e = tor_malloc_zero(sizeof(rend_cache_entry_t));
strmap_set_lc(rend_cache, key, e);
} else {
rend_cache_decrement_allocation(rend_cache_entry_allocation(e));
rend_cache_failure_remove(e->parsed);
rend_service_descriptor_free(e->parsed);
tor_free(e->desc);
}
e->parsed = parsed;
e->desc = tor_malloc_zero(encoded_size + 1);
strlcpy(e->desc, desc, encoded_size + 1);
e->len = encoded_size;
rend_cache_increment_allocation(rend_cache_entry_allocation(e));
log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
safe_str_client(service_id), (int)encoded_size);
if (entry) {
*entry = e;
}
return 0;
okay:
if (entry) {
*entry = e;
}
retval = 0;
err:
rend_service_descriptor_free(parsed);
tor_free(intro_content);
return retval;
}