chiark / gitweb /
dirmngr: hkp: Avoid potential race condition when some hosts die.
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>
Sat, 29 Oct 2016 05:25:05 +0000 (01:25 -0400)
committerDaniel Kahn Gillmor <dkg@fifthhorseman.net>
Tue, 14 Feb 2017 00:29:34 +0000 (00:29 +0000)
commit385e4dbd6cf07653e50df1edc18a897dd8dff6e0
tree7d37acfb4184c2e8b664abc67ea94074247521cb
parentad441edb6d8f4e7f9faa32756e3604602e95c6ff
dirmngr: hkp: Avoid potential race condition when some hosts die.

* dirmngr/ks-engine-hkp.c (select_random_host): Use atomic pass
through the host table instead of risking out-of-bounds write.

--

Multiple threads may write to hosttable[x]->dead while
select_random_host() is running.  For example, a housekeeping thread
might clear the ->dead bit on some entries, or another connection to
dirmngr might manually mark a host as alive.

If one or more hosts are resurrected between the two loops over a
given table in select_random_host(), then the allocation of tbl might
not be large enough, resulting in a write past the end of tbl on the
second loop.

This change collapses the two loops into a single loop to avoid this
discrepancy: each host's "dead" bit is now only checked once.

As Werner points out, this isn't currently strictly necessary, since
npth will not switch threads unless a blocking system call is made,
and no blocking system call is made in these two loops.

However, in a subsequent change in this series, we will call a
function in this loop, and that function may sometimes write(2), or
call other functions, which may themselves block.  Keeping this as a
single-pass loop avoids the need to keep track of what might block and
what might not.

Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Gbp-Pq: Topic dirmngr-idling
Gbp-Pq: Name 0001-dirmngr-hkp-Avoid-potential-race-condition-when-some.patch
dirmngr/ks-engine-hkp.c