From 995aded58724a1a07704493b311be5222b3f82a2 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Tue, 11 Sep 2018 13:54:49 +0900 Subject: [PATCH] dirmngr: Serialize access to hosttable. * dirmngr/dirmngr.h (ks_hkp_init): New. * dirmngr/dirmngr.c (main): Call ks_hkp_init. * dirmngr/ks-engine-hkp.c (ks_hkp_init): New. (ks_hkp_mark_host): Serialize access to hosttable. (ks_hkp_print_hosttable, make_host_part): Likewise. (ks_hkp_housekeeping, ks_hkp_reload): Likewise. -- Signed-off-by: NIIBE Yutaka --- dirmngr/dirmngr.c | 5 ++++ dirmngr/dirmngr.h | 2 +- dirmngr/ks-engine-hkp.c | 65 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/dirmngr/dirmngr.c b/dirmngr/dirmngr.c index 6fdfe36c2..1696be578 100644 --- a/dirmngr/dirmngr.c +++ b/dirmngr/dirmngr.c @@ -1143,6 +1143,7 @@ main (int argc, char **argv) thread_init (); cert_cache_init (hkp_cacert_filenames); crl_cache_init (); + ks_hkp_init (); http_register_netactivity_cb (netactivity_action); start_command_handler (ASSUAN_INVALID_FD, 0); shutdown_reaper (); @@ -1178,6 +1179,7 @@ main (int argc, char **argv) thread_init (); cert_cache_init (hkp_cacert_filenames); crl_cache_init (); + ks_hkp_init (); http_register_netactivity_cb (netactivity_action); handle_connections (3); shutdown_reaper (); @@ -1399,6 +1401,7 @@ main (int argc, char **argv) thread_init (); cert_cache_init (hkp_cacert_filenames); crl_cache_init (); + ks_hkp_init (); http_register_netactivity_cb (netactivity_action); handle_connections (fd); shutdown_reaper (); @@ -1421,6 +1424,7 @@ main (int argc, char **argv) thread_init (); cert_cache_init (hkp_cacert_filenames); crl_cache_init (); + ks_hkp_init (); if (!argc) rc = crl_cache_load (&ctrlbuf, NULL); else @@ -1444,6 +1448,7 @@ main (int argc, char **argv) thread_init (); cert_cache_init (hkp_cacert_filenames); crl_cache_init (); + ks_hkp_init (); rc = crl_fetch (&ctrlbuf, argv[0], &reader); if (rc) log_error (_("fetching CRL from '%s' failed: %s\n"), diff --git a/dirmngr/dirmngr.h b/dirmngr/dirmngr.h index edaf46394..9c26c09e6 100644 --- a/dirmngr/dirmngr.h +++ b/dirmngr/dirmngr.h @@ -218,7 +218,7 @@ int dirmngr_use_tor (void); /*-- Various housekeeping functions. --*/ void ks_hkp_housekeeping (time_t curtime); void ks_hkp_reload (void); - +void ks_hkp_init (void); /*-- server.c --*/ ldap_server_t get_ldapservers_from_ctrl (ctrl_t ctrl); diff --git a/dirmngr/ks-engine-hkp.c b/dirmngr/ks-engine-hkp.c index 3c7a8a019..9c234ec44 100644 --- a/dirmngr/ks-engine-hkp.c +++ b/dirmngr/ks-engine-hkp.c @@ -35,6 +35,7 @@ # include #endif /*!HAVE_W32_SYSTEM*/ +#include #include "dirmngr.h" #include "misc.h" #include "../common/userids.h" @@ -108,6 +109,8 @@ struct hostinfo_s resolved from a pool name and its allocated size.*/ static hostinfo_t *hosttable; static int hosttable_size; +/* A mutex used to serialize access to the hosttable. */ +static npth_mutex_t hosttable_lock; /* The number of host slots we initially allocate for HOSTTABLE. */ #define INITIAL_HOSTTABLE_SIZE 50 @@ -753,9 +756,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive) if (!name || !*name || !strcmp (name, "localhost")) return 0; + if (npth_mutex_lock (&hosttable_lock)) + log_fatal ("failed to acquire mutex\n"); + idx = find_hostinfo (name); if (idx == -1) - return gpg_error (GPG_ERR_NOT_FOUND); + { + err = gpg_error (GPG_ERR_NOT_FOUND); + goto leave; + } hi = hosttable[idx]; if (alive && hi->dead) @@ -814,6 +823,10 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive) } } + leave: + if (npth_mutex_unlock (&hosttable_lock)) + log_fatal ("failed to release mutex\n"); + return err; } @@ -834,7 +847,9 @@ ks_hkp_print_hosttable (ctrl_t ctrl) if (err) return err; - /* FIXME: We need a lock for the hosttable. */ + if (npth_mutex_lock (&hosttable_lock)) + log_fatal ("failed to acquire mutex\n"); + curtime = gnupg_get_time (); for (idx=0; idx < hosttable_size; idx++) if ((hi=hosttable[idx])) @@ -927,12 +942,12 @@ ks_hkp_print_hosttable (ctrl_t ctrl) diedstr? ")":"" ); xfree (died); if (err) - return err; + goto leave; if (hi->cname) err = ks_printf_help (ctrl, " . %s", hi->cname); if (err) - return err; + goto leave; if (hi->pool) { @@ -947,14 +962,21 @@ ks_hkp_print_hosttable (ctrl_t ctrl) put_membuf( &mb, "", 1); p = get_membuf (&mb, NULL); if (!p) - return gpg_error_from_syserror (); + { + err = gpg_error_from_syserror (); + goto leave; + } err = ks_print_help (ctrl, p); xfree (p); if (err) - return err; + goto leave; } } - return 0; + + leave: + if (npth_mutex_unlock (&hosttable_lock)) + log_fatal ("failed to release mutex\n"); + return err; } @@ -1023,9 +1045,16 @@ make_host_part (ctrl_t ctrl, protocol = KS_PROTOCOL_HKP; } + if (npth_mutex_lock (&hosttable_lock)) + log_fatal ("failed to acquire mutex\n"); + portstr[0] = 0; err = map_host (ctrl, host, srvtag, force_reselect, protocol, &hostname, portstr, r_httpflags, r_httphost); + + if (npth_mutex_unlock (&hosttable_lock)) + log_fatal ("failed to release mutex\n"); + if (err) return err; @@ -1099,6 +1128,9 @@ ks_hkp_housekeeping (time_t curtime) int idx; hostinfo_t hi; + if (npth_mutex_lock (&hosttable_lock)) + log_fatal ("failed to acquire mutex\n"); + for (idx=0; idx < hosttable_size; idx++) { hi = hosttable[idx]; @@ -1115,6 +1147,9 @@ ks_hkp_housekeeping (time_t curtime) log_info ("resurrected host '%s'", hi->name); } } + + if (npth_mutex_unlock (&hosttable_lock)) + log_fatal ("failed to release mutex\n"); } @@ -1126,6 +1161,9 @@ ks_hkp_reload (void) int idx, count; hostinfo_t hi; + if (npth_mutex_lock (&hosttable_lock)) + log_fatal ("failed to acquire mutex\n"); + for (idx=count=0; idx < hosttable_size; idx++) { hi = hosttable[idx]; @@ -1139,6 +1177,9 @@ ks_hkp_reload (void) } if (count) log_info ("number of resurrected hosts: %d", count); + + if (npth_mutex_unlock (&hosttable_lock)) + log_fatal ("failed to release mutex\n"); } @@ -1754,3 +1795,13 @@ ks_hkp_put (ctrl_t ctrl, parsed_uri_t uri, const void *data, size_t datalen) xfree (httphost); return err; } + +void +ks_hkp_init (void) +{ + int err; + + err = npth_mutex_init (&hosttable_lock, NULL); + if (err) + log_fatal ("error initializing mutex: %s\n", strerror (err)); +}