glibc/resolv
Joseph Myers f120cda607 Fix p_secstodate overflow handling (bug 22463).
The resolv/res_debug.c function p_secstodate (which is a public
function exported from libresolv, taking an unsigned long argument)
does:

        struct tm timebuf;
        time = __gmtime_r(&clock, &timebuf);
        time->tm_year += 1900;
        time->tm_mon += 1;
        sprintf(output, "%04d%02d%02d%02d%02d%02d",
                time->tm_year, time->tm_mon, time->tm_mday,
                time->tm_hour, time->tm_min, time->tm_sec);

If __gmtime_r returns NULL (because the year overflows the range of
int), this will dereference a null pointer.  Otherwise, if the
computed year does not fit in four characters, this will cause a
buffer overrun of the fixed-size 15-byte buffer.  With current GCC
mainline, there is a compilation failure because of the possible
buffer overrun.

I couldn't find a specification for how this function is meant to
behave, but Paul pointed to RFC 4034 as relevant to the cases where
this function is called from within glibc.  The function's interface
is inherently problematic when dates beyond Y2038 might be involved,
because of the ambiguity in how to interpret 32-bit timestamps as such
dates (the RFC suggests interpreting times as being within 68 years of
the present date, which would mean some kind of interface whose
behavior depends on the present date).

This patch works on the basis of making a minimal fix in preparation
for obsoleting the function.  The function is made to handle times in
the interval [0, 0x7fffffff] only, on all platforms, with <overflow>
used as the output string in other cases (and errno set to EOVERFLOW
in such cases).  This seems to be a reasonable state for the function
to be in when made a compat symbol by a future patch, being compatible
with any existing uses for existing timestamps without trying to work
for later timestamps.  Results independent of the range of time_t also
simplify the testcase.

I couldn't persuade GCC to recognize the ranges of the struct tm
fields by adding explicit range checks with a call to
__builtin_unreachable if outside the range (this looks similar to
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80776>), so having added
a range check on the input, this patch then disables the
-Wformat-overflow= warning for the sprintf call (I prefer that to the
use of strftime, as being more transparently correct without knowing
what each of %m and %M etc. is).

I do not know why this build failure should be new with mainline GCC
(that is, I don't know what GCC change might have introduced it, when
the basic functionality for such warnings was already in GCC 7).

I do not know if this is a security issue (that is, if there are
plausible ways in which a date before -999 or after 9999 from an
untrusted source might end up in this function).  The system clock is
arguably an untrusted source (in that e.g. NTP is insecure), but
probably not to that extent (NTP can't communicate such wild
timestamps), and uses from within glibc are limited to 32-bit inputs.

Tested with build-many-glibcs.py that this restores the build for arm
with yesterday's mainline GCC.  Also tested for x86_64 and x86.

	[BZ #22463]
	* resolv/res_debug.c: Include <libc-diag.h>.
	(p_secstodate): Assert time_t at least as wide as u_long.  On
	overflow, use integer seconds since the epoch as output, or use
	"<overflow>" as output and set errno to EOVERFLOW if integer
	seconds since the epoch would be 14 or more characters.
	(p_secstodate) [__GNUC_PREREQ (7, 0)]: Disable -Wformat-overflow=
	for sprintf call.
	* resolv/tst-p_secstodate.c: New file.
	* resolv/Makefile (tests): Add tst-p_secstodate.
	($(objpfx)tst-p_secstodate): Depend on $(objpfx)libresolv.so.
2017-11-22 22:12:07 +00:00
..
arpa Update DNS RR type definitions [BZ #20593] 2016-12-31 21:16:27 +01:00
bits/types resolv: Introduce struct resolv_conf with extended resolver state 2017-07-03 20:57:28 +02:00
nss_dns resolv/nss_dns/dns-host.c: Fix typo in comment 2017-09-26 13:47:48 +02:00
rpc Install a dummy <rpc/netdb.h> when not building sunrpc/. 2015-07-08 13:38:50 -07:00
sys initial import 1995-02-18 01:27:10 +00:00
base64.c Convert 703 function definitions to prototype style. 2015-10-16 20:21:49 +00:00
compat-gethnamaddr.c resolv: Introduce struct resolv_context [BZ #21668] 2017-07-03 20:52:59 +02:00
compat-hooks.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
Depend Don't mention linuxthreads in Depend files. 2014-05-21 16:53:11 +00:00
gai_cancel.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
gai_error.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
gai_misc.c resolv: Remove internal_function attribute 2017-08-31 16:07:52 +02:00
gai_misc.h Mark internal functions with attribute_hidden [BZ #18822] 2017-10-01 15:07:23 -07:00
gai_notify.c resolv: Remove internal_function attribute 2017-08-31 16:07:52 +02:00
gai_sigqueue.c resolv: Remove internal_function attribute 2017-08-31 16:07:52 +02:00
gai_suspend.c resolv: Replace __builtin_expect with __glibc_unlikely/__glibc_likely 2017-04-19 14:29:24 +02:00
getaddrinfo_a.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
herror.c Consolidate non cancellable writev call 2017-08-21 15:37:45 -03:00
inet_addr.c resolv: Reindent preprocessor conditionals following cleanups 2016-04-28 16:53:56 +02:00
inet_net_ntop.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
inet_net_pton.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
inet_neta.c Consistently use uintN_t not u_intN_t everywhere. 2017-08-07 19:55:34 +00:00
inet_ntop.c resolv: Remove internal_function attribute 2017-08-31 16:07:52 +02:00
inet_pton.c inet_pton: Reject IPv6 addresses with many leading zeros [BZ #16637] 2017-06-23 22:51:06 +02:00
Makefile Fix p_secstodate overflow handling (bug 22463). 2017-11-22 22:12:07 +00:00
mapv4v6addr.h . 2007-07-31 13:33:18 +00:00
mapv4v6hostent.h Handle running out of buffer space with IPv6 mapping enabled. 2009-11-10 07:36:50 -08:00
netdb.h Remove __need macros from signal.h. 2017-05-20 19:04:43 -04:00
ns_date.c Consistently use uintN_t not u_intN_t everywhere. 2017-08-07 19:55:34 +00:00
ns_name.c resolv: ns_name_pton should report trailing \ as error [BZ #22413] 2017-11-11 11:41:45 +01:00
ns_netint.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
ns_parse.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
ns_print.c Remove obsolete DNSSEC support [BZ #20591] 2016-09-21 16:08:32 +02:00
ns_samedomain.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
ns_ttl.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
nsap_addr.c resolv: Remove SCCS and RCS keywords 2016-04-28 12:53:49 +02:00
README resolv: Remove unused resolv/res_debug.h header file 2017-06-30 11:31:29 +02:00
res_comp.c resolv: More precise checks in res_hnok, res_dnok [BZ #22409] [BZ #22412] 2017-11-11 11:51:08 +01:00
res_data.c resolv: Reformat resolv/res_data.c to GNU style 2017-06-30 11:32:04 +02:00
res_debug.c Fix p_secstodate overflow handling (bug 22463). 2017-11-22 22:12:07 +00:00
res_hconf.c Consistently use uintN_t not u_intN_t everywhere. 2017-08-07 19:55:34 +00:00
res_hconf.h resolv: Call _res_hconf_init from __res_vinit 2017-06-27 09:26:46 +02:00
res_init.c resolv: Fix memory leak with OOM during resolv.conf parsing [BZ #22095] 2017-09-06 15:20:25 +02:00
res_libc.c resolv: Automatically reload a changed /etc/resolv.conf file [BZ #984] 2017-07-03 21:06:23 +02:00
res_mkquery.c resolv: Introduce struct resolv_context [BZ #21668] 2017-07-03 20:52:59 +02:00
res_query.c resolv: Lift domain search list limits [BZ #19569] [BZ #21475] 2017-07-03 21:01:42 +02:00
res_randomid.c resolv: Move res_randomid to its own file 2017-06-19 14:24:23 +02:00
res_send.c resolv: Introduce struct resolv_context [BZ #21668] 2017-07-03 20:52:59 +02:00
res_use_inet6.h resolv: Introduce struct resolv_context [BZ #21668] 2017-07-03 20:52:59 +02:00
res-close.c Consolidate non cancellable close call 2017-08-18 18:38:55 -03:00
res-state.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
resolv_conf.c resolv: __resolv_conf_attach must not free passed conf object [BZ #22096] 2017-09-06 15:32:43 +02:00
resolv_conf.h resolv: Automatically reload a changed /etc/resolv.conf file [BZ #984] 2017-07-03 21:06:23 +02:00
resolv_context.c resolv: Automatically reload a changed /etc/resolv.conf file [BZ #984] 2017-07-03 21:06:23 +02:00
resolv_context.h resolv: Mirror the entire resolver configuration in struct resolv_conf 2017-07-03 21:03:21 +02:00
resolv-internal.h resolv: Automatically reload a changed /etc/resolv.conf file [BZ #984] 2017-07-03 21:06:23 +02:00
resolv.h resolv: Automatically reload a changed /etc/resolv.conf file [BZ #984] 2017-07-03 21:06:23 +02:00
tst-aton.c Modify several tests to use test-skeleton.c 2015-08-06 02:59:04 -04:00
tst-bug18665-tcp.c Add missing header files throughout the testsuite. 2017-02-16 17:33:18 -05:00
tst-bug18665.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
tst-inet_ntop.c Modify several tests to use test-skeleton.c 2014-11-05 15:24:08 +05:30
tst-inet_pton.c support: Add <support/next_to_fault.h> 2017-11-13 19:29:32 +01:00
tst-leaks2.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
tst-leaks.c Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
tst-ns_name_compress.c A third round of inclusion fixes for _ISOMAC testsuite. 2017-04-19 14:39:04 -04:00
tst-ns_name_pton.c resolv: ns_name_pton should report trailing \ as error [BZ #22413] 2017-11-11 11:41:45 +01:00
tst-ns_name.c resolv: Support an exactly sized buffer in ns_name_pack [BZ #21359] 2017-04-13 11:56:28 +02:00
tst-ns_name.data resolv: Add test coverage for ns_name_unpack, ns_name_ntop 2017-04-04 20:56:17 +02:00
tst-p_secstodate.c Fix p_secstodate overflow handling (bug 22463). 2017-11-22 22:12:07 +00:00
tst-res_hconf_reorder.c Add missing header files throughout the testsuite. 2017-02-16 17:33:18 -05:00
tst-res_hnok.c resolv: More precise checks in res_hnok, res_dnok [BZ #22409] [BZ #22412] 2017-11-11 11:51:08 +01:00
tst-res_use_inet6.c nss_dns: Remove dead PTR IPv4-to-IPv6 mapping code 2017-09-06 15:11:44 +02:00
tst-resolv-basic.c getaddrinfo: Fix error handling in gethosts [BZ #21915] [BZ #21922] 2017-09-01 09:34:29 +02:00
tst-resolv-canonname.c resolv: Add tst-resolv-canonname 2017-04-04 20:56:24 +02:00
tst-resolv-edns.c A third round of inclusion fixes for _ISOMAC testsuite. 2017-04-19 14:39:04 -04:00
tst-resolv-network.c resolv: Use test framework in tst-resolv-network 2017-11-11 10:54:56 +01:00
tst-resolv-qtypes.c Use "static const char domain[] =" 2017-09-07 13:57:15 -07:00
tst-resolv-res_init-multi.c resolv: Fix improper assert in __resolv_conf_attach 2017-07-04 11:18:34 +02:00
tst-resolv-res_init-skeleton.c hurd: fix resolv/tst-resolv-res_init-skeleton.c build 2017-09-03 19:44:07 +02:00
tst-resolv-res_init-thread.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
tst-resolv-res_init.c resolv: Tests for various versions of res_init 2017-06-02 15:50:36 +02:00
tst-resolv-res_ninit.c resolv: Introduce free list for resolv_conf index slosts 2017-07-03 21:07:11 +02:00
tst-resolv-rotate.c resolv: Make RES_ROTATE start with a random name server [BZ #19570] 2017-06-30 10:43:33 +02:00
tst-resolv-search.c Add missing header files throughout the testsuite. 2017-02-16 17:33:18 -05:00
tst-resolv-threads.c support: Add resolver testing mode which does not patch _res 2017-07-05 19:04:40 +02:00
Versions resolv: Introduce struct resolv_context [BZ #21668] 2017-07-03 20:52:59 +02:00

The resolver in the GNU C Library
*********************************

Starting with version 2.2, the resolver in the GNU C Library comes
from BIND 8.  Only a subset of the src/lib/resolv part of libbind is
included here; basically the parts that are needed to provide the
functionality present in the resolver from BIND 4.9.7 that was
included in the previous release of the GNU C Library, augmented by
the parts needed to provide thread-safety.  This means that support
for things as dynamic DNS updates and TSIG keys isn't included.  If
you need those facilities, please take a look at the full BIND
distribution.


Differences
===========

The resolver in the GNU C Library still differs from what's in BIND
8.2.3-T5B:

* The RES_DEBUG option (`options debug' in /etc/resolv.conf) has been
  disabled.

* The resolver in glibc allows underscores in domain names.

* The <resolv.h> header in glibc includes <netinet/in.h> and
  <arpa/nameser.h> to make it self-contained.

* The `res_close' function in glibc only tries to close open files
  referenced through `_res' if the RES_INIT bit is set in
  `_res.options'.  This fixes a potential security bug with programs
  that bogusly call `res_close' without initialising the resolver
  state first.  Note that the thread-safe `res_nclose' still doesn't
  check the RES_INIT bit.  By the way, you're not really supposed to
  call `res_close/res_nclose' directly.

* The resolver in glibc can connect to a nameserver over IPv6.  Just
  specify the IPv6 address in /etc/resolv.conf.  You cannot change the
  address of an IPv6 nameserver dynamically in your program though.


Using the resolver in multi-threaded code
=========================================

The traditional resolver interfaces `res_query', `res_search',
`res_mkquery', `res_send' and `res_init', used a static (global)
resolver state stored in the `_res' structure.  Therefore, these
interfaces are not thread-safe.  Therefore, BIND 8.2 introduced a set
of "new" interfaces `res_nquery', `res_nsearch', `res_nmkquery',
`res_nsend' and `res_ninit' that take a `res_state' as their first
argument, so you can use a per-thread resolver state.  In glibc, when
you link with -lpthread, such a per-thread resolver state is already
present.  It can be accessed using `_res', which has been redefined as
a macro, in a similar way to what has been done for the `errno' and
`h_errno' variables.  This per-thread resolver state is also used for
the `gethostby*' family of functions, which means that for example
`gethostbyname_r' is now fully thread-safe and re-entrant.  The
traditional resolver interfaces however, continue to use a single
resolver state and are therefore still thread-unsafe.  The resolver
state is the same resolver state that is used for the initial ("main")
thread.

This has the following consequences for existing binaries and source
code:

* Single-threaded programs will continue to work.  There should be no
  user-visible changes when you recompile them.

* Multi-threaded programs that use the traditional resolver interfaces
  in the "main" thread should continue to work, except that they no
  longer see any changes in the global resolver state caused by calls
  to, for example, `gethostbyname' in other threads.  Again there
  should be no user-visible changes when you recompile these programs.

* Multi-threaded programs that use the traditional resolver interfaces
  in more than one thread should be just as buggy as before (there are
  no problems if you use proper locking of course).  If you recompile
  these programs, manipulating the _res structure in threads other
  than the "main" thread will seem to have no effect though.

* In Multi-threaded that manipulate the _res structure, calls to
  functions like `gethostbyname' in threads other than the "main"
  thread won't be influenced by the those changes anymore.

We recommend to use the new thread-safe interfaces in new code, since
the traditional interfaces have been deprecated by the BIND folks.
For compatibility with other (older) systems you might want to
continue to use those interfaces though.


Using the resolver in C++ code
==============================

There resolver contains some hooks which will allow the user to
install some callback functions that make it possible to filter DNS
requests and responses.  Although we do not encourage you to make use
of this facility at all, C++ developers should realise that it isn't
safe to throw exceptions from such callback functions.


Source code
===========

The following files come from the BIND distribution (currently version
8.2.3-T5B):

src/include/
  arpa/nameser.h
  arpa/nameser_compat.h
  resolv.h

src/lib/resolv/
  herror.c
  res_comp.c
  res_data.c
  res_debug.c
  res_init.c
  res_mkquery.c
  res_query.c
  res_send.c

src/lib/nameser/
  ns_name.c
  ns_netint.c
  ns_parse.c
  ns_print.c
  ns_samedomain.c
  ns_ttl.c

src/lib/inet/
  inet_addr.c
  inet_net_ntop.c
  inet_net_pton.c
  inet_neta.c
  inet_ntop.c
  inet_pton.c
  nsap_addr.c

src/lib/isc/
  base64.c

Some of these files have been optimised a bit, and adaptations have
been made to make them fit in with the rest of glibc.

res_libc.c is home-brewn, although parts of it are taken from res_data.c.

res_hconf.c and res_hconf.h were contributed by David Mosberger, and
do not come from BIND.

The files gethnamaddr.c, mapv4v6addr.h and mapv4v6hostent.h are
leftovers from BIND 4.9.7.