resolved: set stream type during DnsStream creation
authorDan Streetman <ddstreet@canonical.com>
Wed, 23 Oct 2019 18:47:59 +0000 (14:47 -0400)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 25 Oct 2019 04:36:29 +0000 (13:36 +0900)
commit1c089741d3b56ab096c5c401089f68b293b5fa38
tree79a268f96092e98a2fc18965afd544024d10e1cf
parentf2106b178990a4b7d015f5777485e0fd66bda39f
resolved: set stream type during DnsStream creation

The DnsStreamType was added to track different types of DNS TCP streams,
instead of refcounting all of them together.  However, the stream type was
not actually set into the stream->type field, so while the reference count
was correctly incremented per-stream-type, the reference count was always
decremented in the cleanup function for stream type 0, leading to
underflow for the type 0 stream (unsigned) refcount, and preventing new
type 0 streams from being created.

Since type 0 is DNS_STREAM_LOOKUP, which is used to communicate with
upstream nameservers, once the refcount underflows the stub resolver
no longer is able to successfully fall back to TCP upstream lookups
for any truncated UDP packets.

This was found because lookups of A records with a large number of
addresses, too much to fit into a single 512 byte DNS UDP reply,
were causing getaddrinfo() to fall back to TCP and trigger this bug,
which then caused the TCP fallback for later large record lookups
to fail with 'connection timed out; no servers could be reached'.

The stream type was introduced in commit:
652ba568c6624bf40d735645f029d83d21bdeaa6
src/resolve/resolved-dns-stream.c