aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Gschwantner <tharre3@gmail.com>2019-09-10 05:20:53 +0200
committerThomas Gschwantner <tharre3@gmail.com>2019-09-10 05:56:18 +0200
commit2e03c617ba769bcbf0b8d1005b9df87f4e7a8c9c (patch)
treeb538fdb6d67e8dddcc9a7820b26a01409242faef
parentFix build issue that occurs on gcc debian 6.3.0-18 (diff)
downloadwg-dynamic-2e03c617ba769bcbf0b8d1005b9df87f4e7a8c9c.tar.xz
wg-dynamic-2e03c617ba769bcbf0b8d1005b9df87f4e7a8c9c.zip
Fix incorrect use of pubkey ptr in new_lease()
Before this, leases_ht would store the (temporary) pointer to pubkey that's given to new_lease() directly. The memory it's pointing to is overwritten as soon as a new connection comes in however and thus breaking the hashtable. Instead make our own copy of the pubkey and store the pointer to that.
-rw-r--r--lease.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/lease.c b/lease.c
index eac5a15..7551083 100644
--- a/lease.c
+++ b/lease.c
@@ -30,7 +30,7 @@ static time_t gexpires = TIME_T_MAX;
static bool synchronized;
KHASH_MAP_INIT_WGKEY(leaseht, struct wg_dynamic_lease *)
-khash_t(leaseht) * leases_ht;
+khash_t(leaseht) *leases_ht = NULL;
static time_t get_monotonic_time()
{
@@ -80,7 +80,13 @@ void leases_init(char *fname, struct mnl_socket *nlsock)
void leases_free()
{
+ if (leases_ht) {
+ for (khint_t k = 0; k < kh_end(leases_ht); ++k)
+ if (kh_exist(leases_ht, k))
+ free((char *)kh_key(leases_ht, k));
+ }
kh_destroy(leaseht, leases_ht);
+
ipp_free(&pool);
}
@@ -154,7 +160,12 @@ struct wg_dynamic_lease *new_lease(wg_key pubkey, uint32_t leasetime,
lease->leasetime = leasetime;
lease->next = NULL;
- k = kh_put(leaseht, leases_ht, pubkey, &ret);
+ wg_key *pubcopy = malloc(sizeof(wg_key));
+ if (!pubkey)
+ fatal("malloc()");
+
+ memcpy(pubcopy, pubkey, sizeof(wg_key));
+ k = kh_put(leaseht, leases_ht, *pubcopy, &ret);
if (ret < 0) {
fatal("kh_put()");
} else if (ret == 0) {
@@ -228,8 +239,10 @@ int leases_refresh()
}
}
- if (!kh_val(leases_ht, k))
+ if (!kh_val(leases_ht, k)) {
+ free((char *)kh_key(leases_ht, k));
kh_del(leaseht, leases_ht, k);
+ }
}
return MIN(INT_MAX / 1000, gexpires - cur_time);