summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2024-02-15 15:21:33 +0000
committerVsevolod Stakhov <vsevolod@rspamd.com>2024-02-15 15:21:33 +0000
commita3866372a68651ae0acf3a67bf3009b512f003ec (patch)
tree2aec4e67ceab6705beb18e7aff0d66346ed1d0b5
parentc5da2ff5e9eaf9410e25c0bb36c4833547e5103f (diff)
downloadrspamd-a3866372a68651ae0acf3a67bf3009b512f003ec.tar.gz
rspamd-a3866372a68651ae0acf3a67bf3009b512f003ec.zip
[Minor] Add refcounts on fuzzy keys for shared usage
-rw-r--r--src/fuzzy_storage.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c
index 7307dcb4a..6c48b3665 100644
--- a/src/fuzzy_storage.c
+++ b/src/fuzzy_storage.c
@@ -133,6 +133,7 @@ struct fuzzy_key {
struct fuzzy_key_stat *stat;
khash_t(fuzzy_key_flag_stat) * flags_stat;
khash_t(fuzzy_key_ids_set) * forbidden_ids;
+ ref_entry_t ref;
};
KHASH_INIT(rspamd_fuzzy_keys_hash,
@@ -519,7 +520,7 @@ fuzzy_hash_table_dtor(khash_t(rspamd_fuzzy_keys_hash) * hash)
{
struct fuzzy_key *key;
kh_foreach_value(hash, key, {
- fuzzy_key_dtor(key);
+ REF_RELEASE(key);
});
kh_destroy(rspamd_fuzzy_keys_hash, hash);
}
@@ -1503,8 +1504,6 @@ rspamd_fuzzy_decrypt_command(struct fuzzy_session *s, guchar *buf, gsize buflen)
key = kh_val(s->ctx->keys, k);
}
- s->key = key;
-
/* Now process the remote pubkey */
rk = rspamd_pubkey_from_bin(hdr.pubkey, sizeof(hdr.pubkey),
RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519);
@@ -1529,6 +1528,9 @@ rspamd_fuzzy_decrypt_command(struct fuzzy_session *s, guchar *buf, gsize buflen)
return FALSE;
}
+ s->key = key;
+ REF_RETAIN(key);
+
memcpy(s->nm, rspamd_pubkey_get_nm(rk, key->key), sizeof(s->nm));
rspamd_pubkey_unref(rk);
@@ -1784,6 +1786,10 @@ fuzzy_session_destroy(gpointer d)
g_free(session->extensions);
}
+ if (session->key) {
+ REF_RELEASE(session->key);
+ }
+
g_free(session);
}
@@ -2636,6 +2642,7 @@ fuzzy_parse_keypair(rspamd_mempool_t *pool,
}
key = g_malloc0(sizeof(*key));
+ REF_INIT_RETAIN(key, fuzzy_key_dtor);
key->key = kp;
keystat = g_malloc0(sizeof(*keystat));
REF_INIT_RETAIN(keystat, fuzzy_key_stat_dtor);
@@ -2660,14 +2667,14 @@ fuzzy_parse_keypair(rspamd_mempool_t *pool,
if (r == 0) {
msg_err("duplicate keypair found: pk=%*bs",
32, pk);
- fuzzy_key_dtor(key);
+ REF_RELEASE(key);
return FALSE;
}
else if (r == -1) {
msg_err("hash insertion error: pk=%*bs",
32, pk);
- fuzzy_key_dtor(key);
+ REF_RELEASE(key);
return FALSE;
}