Skip to content

Commit 9b5e831

Browse files
committed
Check return values of sensitive system library calls.
PostgreSQL already checked the vast majority of these, missing this handful that nearly cannot fail. If putenv() failed with ENOMEM in pg_GSS_recvauth(), authentication would proceed with the wrong keytab file. If strftime() returned zero in cache_locale_time(), using the unspecified buffer contents could lead to information exposure or a crash. Back-patch to 9.0 (all supported versions). Other unchecked calls to these functions, especially those in frontend code, pose negligible security concern. This patch does not address them. Nonetheless, it is always better to check return values whose specification provides for indicating an error. In passing, fix an off-by-one error in strftime_win32()'s invocation of WideCharToMultiByte(). Upon retrieving a value of exactly MAX_L10N_DATA bytes, strftime_win32() would overrun the caller's buffer by one byte. MAX_L10N_DATA is chosen to exceed the length of every possible value, so the vulnerable scenario probably does not arise. Security: CVE-2015-3166
1 parent b08c7af commit 9b5e831

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

src/backend/libpq/auth.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,15 +1014,16 @@ pg_GSS_recvauth(Port *port)
10141014
size_t kt_len = strlen(pg_krb_server_keyfile) + 14;
10151015
char *kt_path = malloc(kt_len);
10161016

1017-
if (!kt_path)
1017+
if (!kt_path ||
1018+
snprintf(kt_path, kt_len, "KRB5_KTNAME=%s",
1019+
pg_krb_server_keyfile) != kt_len - 2 ||
1020+
putenv(kt_path) != 0)
10181021
{
10191022
ereport(LOG,
10201023
(errcode(ERRCODE_OUT_OF_MEMORY),
10211024
errmsg("out of memory")));
10221025
return STATUS_ERROR;
10231026
}
1024-
snprintf(kt_path, kt_len, "KRB5_KTNAME=%s", pg_krb_server_keyfile);
1025-
putenv(kt_path);
10261027
}
10271028
}
10281029

src/backend/utils/adt/pg_locale.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -558,24 +558,34 @@ PGLC_localeconv(void)
558558
* pg_strftime(), which isn't locale-aware and does not need to be replaced.
559559
*/
560560
static size_t
561-
strftime_win32(char *dst, size_t dstlen, const wchar_t *format, const struct tm * tm)
561+
strftime_win32(char *dst, size_t dstlen,
562+
const char *format, const struct tm * tm)
562563
{
563564
size_t len;
565+
wchar_t wformat[8]; /* formats used below need 3 bytes */
564566
wchar_t wbuf[MAX_L10N_DATA];
565567
int encoding;
566568

567569
encoding = GetDatabaseEncoding();
568570

569-
len = wcsftime(wbuf, MAX_L10N_DATA, format, tm);
571+
/* get a wchar_t version of the format string */
572+
len = MultiByteToWideChar(CP_UTF8, 0, format, -1,
573+
wformat, lengthof(wformat));
574+
if (len == 0)
575+
elog(ERROR, "could not convert format string from UTF-8: error code %lu",
576+
GetLastError());
577+
578+
len = wcsftime(wbuf, MAX_L10N_DATA, wformat, tm);
570579
if (len == 0)
571580

572581
/*
573-
* strftime call failed - return 0 with the contents of dst
574-
* unspecified
582+
* strftime failed, possibly because the result would not fit in
583+
* MAX_L10N_DATA. Return 0 with the contents of dst unspecified.
575584
*/
576585
return 0;
577586

578-
len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen, NULL, NULL);
587+
len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen - 1,
588+
NULL, NULL);
579589
if (len == 0)
580590
elog(ERROR,
581591
"could not convert string to UTF-8:error %lu", GetLastError());
@@ -596,9 +606,33 @@ strftime_win32(char *dst, size_t dstlen, const wchar_t *format, const struct tm
596606
}
597607

598608
/* redefine strftime() */
599-
#define strftime(a,b,c,d) strftime_win32(a,b,L##c,d)
609+
#define strftime(a,b,c,d) strftime_win32(a,b,c,d)
600610
#endif /* WIN32 */
601611

612+
/* Subroutine for cache_locale_time(). */
613+
static void
614+
cache_single_time(char **dst, const char *format, const struct tm * tm)
615+
{
616+
char buf[MAX_L10N_DATA];
617+
char *ptr;
618+
619+
/*
620+
* MAX_L10N_DATA is sufficient buffer space for every known locale, and
621+
* POSIX defines no strftime() errors. (Buffer space exhaustion is not an
622+
* error.) An implementation might report errors (e.g. ENOMEM) by
623+
* returning 0 (or, less plausibly, a negative value) and setting errno.
624+
* Report errno just in case the implementation did that, but clear it in
625+
* advance of the call so we don't emit a stale, unrelated errno.
626+
*/
627+
errno = 0;
628+
if (strftime(buf, MAX_L10N_DATA, format, tm) <= 0)
629+
elog(ERROR, "strftime(%s) failed: %m", format);
630+
631+
ptr = MemoryContextStrdup(TopMemoryContext, buf);
632+
if (*dst)
633+
pfree(*dst);
634+
*dst = ptr;
635+
}
602636

603637
/*
604638
* Update the lc_time localization cache variables if needed.
@@ -609,8 +643,6 @@ cache_locale_time(void)
609643
char *save_lc_time;
610644
time_t timenow;
611645
struct tm *timeinfo;
612-
char buf[MAX_L10N_DATA];
613-
char *ptr;
614646
int i;
615647

616648
#ifdef WIN32
@@ -657,35 +689,17 @@ cache_locale_time(void)
657689
for (i = 0; i < 7; i++)
658690
{
659691
timeinfo->tm_wday = i;
660-
strftime(buf, MAX_L10N_DATA, "%a", timeinfo);
661-
ptr = MemoryContextStrdup(TopMemoryContext, buf);
662-
if (localized_abbrev_days[i])
663-
pfree(localized_abbrev_days[i]);
664-
localized_abbrev_days[i] = ptr;
665-
666-
strftime(buf, MAX_L10N_DATA, "%A", timeinfo);
667-
ptr = MemoryContextStrdup(TopMemoryContext, buf);
668-
if (localized_full_days[i])
669-
pfree(localized_full_days[i]);
670-
localized_full_days[i] = ptr;
692+
cache_single_time(&localized_abbrev_days[i], "%a", timeinfo);
693+
cache_single_time(&localized_full_days[i], "%A", timeinfo);
671694
}
672695

673696
/* localized months */
674697
for (i = 0; i < 12; i++)
675698
{
676699
timeinfo->tm_mon = i;
677700
timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
678-
strftime(buf, MAX_L10N_DATA, "%b", timeinfo);
679-
ptr = MemoryContextStrdup(TopMemoryContext, buf);
680-
if (localized_abbrev_months[i])
681-
pfree(localized_abbrev_months[i]);
682-
localized_abbrev_months[i] = ptr;
683-
684-
strftime(buf, MAX_L10N_DATA, "%B", timeinfo);
685-
ptr = MemoryContextStrdup(TopMemoryContext, buf);
686-
if (localized_full_months[i])
687-
pfree(localized_full_months[i]);
688-
localized_full_months[i] = ptr;
701+
cache_single_time(&localized_abbrev_months[i], "%b", timeinfo);
702+
cache_single_time(&localized_full_months[i], "%B", timeinfo);
689703
}
690704

691705
/* try to restore internal settings */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy