CVE-2014-0063 (C, published 2014-03-31T14:58Z)
Description: value: Multiple stack-based buffer overflows in PostgreSQL before 8.4.20, 9.0.x before 9.0.16, 9.1.x before 9.1.12, 9.2.x before 9.2.7, and 9.3.x before 9.3.3 allow remote authenticated users to cause a denial of service (crash) or possibly execute arbitrary code via vectors related to an incorrect MAXDATELEN constant and datetime values involving (1) intervals, (2) timestamps, or (3) timezones, a different vulnerability than CVE-2014-0065.
Severity: MEDIUM (CVSS v3 base score: )
Weakness: CWE-119 \u2014 Improper Restriction of Operations within the Bounds of a Memory Buffer
The product performs operations on a memory buffer, but it reads from or writes to a memory location outside the buffer's intended boundary. This may result in read or write operations on unexpected memory locations that could be linked to other variables, data structures, or internal program data.
Repository: https://github.com/postgres/postgres
Files changed:
src/include/utils/datetime.h
src/interfaces/ecpg/pgtypeslib/datetime.c
src/interfaces/ecpg/pgtypeslib/dt.h
src/interfaces/ecpg/pgtypeslib/dt_common.c
src/interfaces/ecpg/pgtypeslib/interval.c
src/interfaces/ecpg/pgtypeslib/timestamp.c
src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c
src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.stdout
src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
src/test/regress/expected/interval.out
src/test/regress/sql/interval.sql
Commit message: Fix handling of wide datetime input/output.

Many server functions use the MAXDATELEN constant to size a buffer for
parsing or displaying a datetime value.  It was much too small for the
longest possible interval output and slightly too small for certain
valid timestamp input, particularly input with a long timezone name.
The long input was rejected needlessly; the long output caused
interval_out() to overrun its buffer.  ECPG's pgtypes library has a copy
of the vulnerable functions, which bore the same vulnerabilities along
with some of its own.  In contrast to the server, certain long inputs
caused stack overflow rather than failing cleanly.  Back-patch to 8.4
(all supported versions).

Reported by Daniel Schüssler, reviewed by Tom Lane.

Security: CVE-2014-0063


Vulnerable code:
#define MAXDATELEN		63		/* maximum possible length of an input date
								 * string (not counting tr. null) */
#define MAXDATEFIELDS	25		/* maximum possible number of fields in a date
								 * string */
#define TOKMAXLEN		10		/* only this many chars are stored in
								 * datetktbl */
	char		lowstr[MAXDATELEN + 1];
	if (strlen(str) >= sizeof(lowstr))
#define MAXDATELEN		63		/* maximum possible length of an input date
								 * string (not counting tr. null) */
#define MAXDATEFIELDS	25		/* maximum possible number of fields in a date
								 * string */
#define TOKMAXLEN		10		/* only this many chars are stored in
								 * datetktbl */
		char		fstr[MAXDATELEN + 1];
		 * string and then do the conversion to an integer.
		strcpy(fstr, (cp + 1));
		strcpy(fstr + strlen(fstr), "000000");
		*(fstr + 6) = '\0';
			char		fstr[MAXDATELEN + 1];
			 * OK, we have at most six digits to work with. Let's construct a
			 * string and then do the conversion to an integer.
			strncpy(fstr, (cp + 1), 7);
			strcpy(fstr + strlen(fstr), "000000");
			*(fstr + 6) = '\0';
		if (nf > MAXDATEFIELDS)
			return -1;
	if (strlen(str) >= sizeof(lowstr))
	if (strlen(str) >= sizeof(lowstr))
#line 52 "dt_test2.pgc"
#line 53 "dt_test2.pgc"
#line 54 "dt_test2.pgc"
#line 55 "dt_test2.pgc"
#line 56 "dt_test2.pgc"
#line 57 "dt_test2.pgc"

Fixed code:
/*
 * Working buffer size for input and output of interval, timestamp, etc.
 * Inputs that need more working space will be rejected early.  Longer outputs
 * will overrun buffers, so this must suffice for all possible output.  As of
 * this writing, interval_out() needs the most space at ~90 bytes.
 */
#define MAXDATELEN		128
/* maximum possible number of fields in a date string */
#define MAXDATEFIELDS	25
/* only this many chars are stored in datetktbl */
#define TOKMAXLEN		10
	char		lowstr[MAXDATELEN + MAXDATEFIELDS];
	if (strlen(str) > MAXDATELEN)
/*
 * Working buffer size for input and output of interval, timestamp, etc.
 * Inputs that need more working space will be rejected early.  Longer outputs
 * will overrun buffers, so this must suffice for all possible output.  As of
 * this writing, PGTYPESinterval_to_asc() needs the most space at ~90 bytes.
 */
#define MAXDATELEN		128
/* maximum possible number of fields in a date string */
#define MAXDATEFIELDS	25
/* only this many chars are stored in datetktbl */
#define TOKMAXLEN		10
		char		fstr[7];
		int			i;

		cp++;
		 * string with those digits, zero-padded on the right, and then do
		 * the conversion to an integer.
		 *
		 * XXX This truncates the seventh digit, unlike rounding it as do
		 * the backend and the !HAVE_INT64_TIMESTAMP case.
		for (i = 0; i < 6; i++)
			fstr[i] = *cp != '\0' ? *cp++ : '0';
		fstr[i] = '\0';
			char		fstr[7];
			int			i;

			cp++;
			 * OK, we have at most six digits to care about. Let's construct a
			 * string with those digits, zero-padded on the right, and then do
			 * the conversion to an integer.
			 *
			 * XXX This truncates the seventh digit, unlike rounding it as do
			 * the backend and the !HAVE_INT64_TIMESTAMP case.
			for (i = 0; i < 6; i++)
				fstr[i] = *cp != '\0' ? *cp++ : '0';
			fstr[i] = '\0';
 *
 * The "lowstr" work buffer must have at least strlen(timestr) + MAXDATEFIELDS
 * bytes of space.  On output, field[] entries will point into it.
		/* Record start of current field */
		if (nf >= MAXDATEFIELDS)
			return -1;
	if (strlen(str) > MAXDATELEN)
	if (strlen(str) > MAXDATELEN)
				  /*
				   * Maximize space usage in ParseDateTime() with 25
				   * (MAXDATEFIELDS) fields and 128 (MAXDATELEN) total length.
				   */
				  "........................Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
				  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
				  /* 26 fields */
				  ".........................aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
				  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
				  "13:24:40.123456789+3",
#line 62 "dt_test2.pgc"
#line 63 "dt_test2.pgc"
#line 64 "dt_test2.pgc"
#line 65 "dt_test2.pgc"
#line 66 "dt_test2.pgc"
#line 67 "dt_test2.pgc"
TS[3,4]: 1999-01-08 13:24:40.123456
TS[4,4]: 1999-01-08 13:24:40.123456
TS[5,4]: 1999-01-08 13:24:40.123456
TS[6,4]: 1999-01-18 13:24:40.123456
TS[7,4]: 2003-01-02 13:24:40.123456
TS[8,4]: 1999-01-08 13:24:40.123456
TS[9,4]: 1999-01-08 13:24:40.123456
TS[10,4]: 1999-01-08 13:24:40.123456
TS[11,4]: 1999-01-08 13:24:40.123456
TS[12,4]: 1999-01-08 13:24:40.123456
TS[13,4]: 2006-01-08 13:24:40.123456
TS[14,4]: 1999-01-08 13:24:40.123456
TS[15,4]: 1999-01-08 13:24:40.123456
TS[16,4]: 1999-01-08 13:24:40.123456
TS[17,4]: 1999-01-08 13:24:40.123456
TS[18,4]: 1999-01-08 13:24:40.123456
TS[19,4]: 0099-01-08 13:24:40.123456 BC
Date[20]: - (N - T)
Date[21]: - (N - T)
				  /*
				   * Maximize space usage in ParseDateTime() with 25
				   * (MAXDATEFIELDS) fields and 128 (MAXDATELEN) total length.
				   */
				  "........................Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
				  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
				  /* 26 fields */
				  ".........................aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
				  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
				  "13:24:40.123456789+3",
-- test long interval output
select '100000000y 10mon -1000000000d -1000000000h -10min -10.000001s ago'::interval;
                                         interval                                          
-------------------------------------------------------------------------------------------
 @ 100000000 years 10 mons -1000000000 days -1000000000 hours -10 mins -10.000001 secs ago
(1 row)

-- test long interval output
select '100000000y 10mon -1000000000d -1000000000h -10min -10.000001s ago'::interval;