From a5e837cfdafe0aac1246201775934a237ed2346a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Tue, 22 Jun 2021 13:05:15 +0200 Subject: [PATCH 1/2] Don't set locale globally, just use it when needed Previously, we would set the locale on a global level and that could possibly lead to different behaviour in underlying functions. In this commit, we change to code to use the system locale only when calling the libidn2 functions and reset the locale back to "POSIX" when exiting the libidn2 code. (cherry picked from commit 0d35b3f1a95ba0fd90fd93f1e146a4bf4396f794) --- bin/dig/dighost.c | 41 ++++++++++++++++++++++++++++++----------- bin/dig/host.c | 5 +---- bin/named/main.c | 18 ++++++++++++++++++ config.h.win32 | 6 ++++++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c index 9a39c965f8..7e88b6b63c 100644 --- a/bin/dig/dighost.c +++ b/bin/dig/dighost.c @@ -99,6 +99,14 @@ #define NS_IN6ADDRSZ 16 #endif /* if !defined(NS_IN6ADDRSZ) */ +#if HAVE_SETLOCALE +#define systemlocale(l) (void)setlocale(l, "") +#define resetlocale(l) (void)setlocale(l, "C") +#else +#define systemlocale(l) +#define resetlocale(l) +#endif /* HAVE_SETLOCALE */ + dig_lookuplist_t lookup_list; dig_serverlist_t server_list; dig_searchlistlist_t search_list; @@ -1339,11 +1347,6 @@ setup_system(bool ipv4only, bool ipv6only) { irs_resconf_destroy(&resconf); -#ifdef HAVE_SETLOCALE - /* Set locale */ - (void)setlocale(LC_ALL, ""); -#endif /* ifdef HAVE_SETLOCALE */ - if (keyfile[0] != 0) { setup_file_key(); } else if (keysecret[0] != 0) { @@ -4418,8 +4421,9 @@ destroy_libs(void) { #ifdef HAVE_LIBIDN2 static isc_result_t idn_output_filter(isc_buffer_t *buffer, unsigned int used_org) { - char src[MXNAME], *dst; + char src[MXNAME], *dst = NULL; size_t srclen, dstlen; + isc_result_t result = ISC_R_SUCCESS; /* * Copy name from 'buffer' to 'src' and terminate it with NULL. @@ -4427,23 +4431,27 @@ idn_output_filter(isc_buffer_t *buffer, unsigned int used_org) { srclen = isc_buffer_usedlength(buffer) - used_org; if (srclen >= sizeof(src)) { warn("Input name too long to perform IDN conversion"); - return (ISC_R_SUCCESS); + goto cleanup; } memmove(src, (char *)isc_buffer_base(buffer) + used_org, srclen); src[srclen] = '\0'; + systemlocale(LC_ALL); + /* * Convert 'src' to the current locale's character encoding. */ idn_ace_to_locale(src, &dst); + resetlocale(LC_ALL); + /* * Check whether the converted name will fit back into 'buffer'. */ dstlen = strlen(dst); if (isc_buffer_length(buffer) < used_org + dstlen) { - idn2_free(dst); - return (ISC_R_NOSPACE); + result = ISC_R_NOSPACE; + goto cleanup; } /* @@ -4456,9 +4464,12 @@ idn_output_filter(isc_buffer_t *buffer, unsigned int used_org) { /* * Clean up. */ - idn2_free(dst); +cleanup: + if (dst != NULL) { + idn2_free(dst); + } - return (ISC_R_SUCCESS); + return (result); } /*% @@ -4474,6 +4485,8 @@ idn_locale_to_ace(const char *src, char *dst, size_t dstlen) { char *ascii_src; int res; + systemlocale(LC_ALL); + /* * We trust libidn2 to return an error if 'src' is too large to be a * valid domain name. @@ -4494,6 +4507,8 @@ idn_locale_to_ace(const char *src, char *dst, size_t dstlen) { (void)strlcpy(dst, final_src, dstlen); idn2_free(ascii_src); + + resetlocale(LC_ALL); } /*% @@ -4508,6 +4523,8 @@ idn_ace_to_locale(const char *src, char **dst) { char *local_src, *utf8_src; int res; + systemlocale(LC_ALL); + /* * We need to: * @@ -4573,5 +4590,7 @@ idn_ace_to_locale(const char *src, char **dst) { idn2_free(utf8_src); *dst = local_src; + + resetlocale(LC_ALL); } #endif /* HAVE_LIBIDN2 */ diff --git a/bin/dig/host.c b/bin/dig/host.c index 0490e0e4a7..c346445819 100644 --- a/bin/dig/host.c +++ b/bin/dig/host.c @@ -13,13 +13,10 @@ #include #include +#include #include #include -#ifdef HAVE_LOCALE_H -#include -#endif /* ifdef HAVE_LOCALE_H */ - #include #include #include diff --git a/bin/named/main.c b/bin/named/main.c index 2291b72dd8..f62f82c20a 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -18,6 +18,10 @@ #include #include +#if HAVE_LOCALE_H +#include +#endif /* HAVE_LOCALE_H */ + #ifdef HAVE_DNSTAP #include #endif @@ -1475,6 +1479,20 @@ main(int argc, char *argv[]) { xmlInitThreads(); #endif /* HAVE_LIBXML2 */ + /* + * Technically, this call is superfluous because on startup of the main + * program, the portable "C" locale is selected by default. This + * explicit call here is for a reference that the BIND 9 code base is + * not locale aware and the locale MUST be set to "C" (or "POSIX") when + * calling any BIND 9 library code. If you are calling external + * libraries that use locale, such calls must be wrapped into + * setlocale(LC_ALL, ""); before the call and setlocale(LC_ALL, "C"); + * after the call, and no BIND 9 library calls must be made in between. + */ +#if HAVE_SETLOCALE + setlocale(LC_ALL, "C"); +#endif /* HAVE_SETLOCALE */ + /* * Record version in core image. * strings named.core | grep "named version:" diff --git a/config.h.win32 b/config.h.win32 index 094cd8f406..e3d871feff 100644 --- a/config.h.win32 +++ b/config.h.win32 @@ -376,6 +376,12 @@ typedef __int64 off_t; /* Define to 1 if you have the `SSL_CTX_up_ref' function. */ #define SSL_CTX_UP_REF 1 +/* Define to 1 if you have the `setlocale' function. */ +#define HAVE_SETLOCALE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LOCALE_H 1 + /* * Define to nothing if C supports flexible array members, and to 1 if it does * not. That way, with a declaration like `struct s { int n; double From 0167c4a898b1a0b1a3db68781966d74d0b7c0893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Tue, 22 Jun 2021 13:08:17 +0200 Subject: [PATCH 2/2] Use POSIX tolower(), toupper() and isupper() functions In the code that rdataset_setownercase() and rdataset_getownercase() we now use tolower()/toupper()/isupper() functions appropriately instead of rolling our own code. (cherry picked from commit 7ccbe520608317594bd338d989b09dfa1a19eb35) --- lib/dns/rbtdb.c | 46 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c index 9dfd219dce..0364beee63 100644 --- a/lib/dns/rbtdb.c +++ b/lib/dns/rbtdb.c @@ -13,6 +13,7 @@ /* #define inline */ +#include #include #include @@ -9856,11 +9857,9 @@ setownercase(rdatasetheader_t *header, const dns_name_t *name) { memset(header->upper, 0, sizeof(header->upper)); fully_lower = true; for (i = 0; i < name->length; i++) { - if (name->ndata[i] >= 'A' && name->ndata[i] <= 'Z') { - { - header->upper[i / 8] |= 1 << (i % 8); - fully_lower = false; - } + if (isupper(name->ndata[i])) { + header->upper[i / 8] |= 1 << (i % 8); + fully_lower = false; } } RDATASET_ATTR_SET(header, RDATASET_ATTR_CASESET); @@ -9885,24 +9884,6 @@ rdataset_setownercase(dns_rdataset_t *rdataset, const dns_name_t *name) { isc_rwlocktype_write); } -static const unsigned char maptolower[256] = { - ['A'] = 'a', ['B'] = 'b', ['C'] = 'c', ['D'] = 'd', ['E'] = 'e', - ['F'] = 'f', ['G'] = 'g', ['H'] = 'h', ['I'] = 'i', ['J'] = 'j', - ['K'] = 'k', ['L'] = 'l', ['M'] = 'm', ['N'] = 'n', ['O'] = 'o', - ['P'] = 'p', ['Q'] = 'q', ['R'] = 'r', ['S'] = 's', ['T'] = 't', - ['U'] = 'u', ['V'] = 'v', ['W'] = 'w', ['X'] = 'x', ['Y'] = 'y', - ['Z'] = 'z', -}; - -static const unsigned char maptoupper[256] = { - ['a'] = 'A', ['b'] = 'B', ['c'] = 'C', ['d'] = 'D', ['e'] = 'E', - ['f'] = 'F', ['g'] = 'G', ['h'] = 'H', ['i'] = 'I', ['j'] = 'J', - ['k'] = 'K', ['l'] = 'L', ['m'] = 'M', ['n'] = 'N', ['o'] = 'O', - ['p'] = 'P', ['q'] = 'Q', ['r'] = 'R', ['s'] = 'S', ['t'] = 'T', - ['u'] = 'U', ['v'] = 'V', ['w'] = 'W', ['x'] = 'X', ['y'] = 'Y', - ['z'] = 'Z', -}; - static void rdataset_getownercase(const dns_rdataset_t *rdataset, dns_name_t *name) { dns_rbtdb_t *rbtdb = rdataset->private1; @@ -9923,15 +9904,10 @@ rdataset_getownercase(const dns_rdataset_t *rdataset, dns_name_t *name) { if (ISC_LIKELY(CASEFULLYLOWER(header))) { for (size_t i = 0; i < name->length; i++) { - uint8_t c = name->ndata[i]; - if (c >= 'A' && c <= 'Z') { - name->ndata[i] = maptolower[c]; - } + name->ndata[i] = tolower(name->ndata[i]); } } else { for (size_t i = 0; i < name->length; i++) { - uint8_t c = name->ndata[i]; - if (mask == (1 << 7)) { bits = header->upper[i / 8]; mask = 1; @@ -9939,15 +9915,9 @@ rdataset_getownercase(const dns_rdataset_t *rdataset, dns_name_t *name) { mask <<= 1; } - if (c >= 'a' && c <= 'z') { - if ((bits & mask) != 0) { - name->ndata[i] = maptoupper[c]; - } - } else if (c >= 'A' && c <= 'Z') { - if ((bits & mask) == 0) { - name->ndata[i] = maptolower[c]; - } - } + name->ndata[i] = ((bits & mask) != 0) + ? toupper(name->ndata[i]) + : tolower(name->ndata[i]); } }