Blame SOURCES/postgresql-CVE-2023-5869.patch

14dc50
This patch was created based on upstream commit:
14dc50
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=18b585155a891784ca8985f595ebc0dde94e0d43
14dc50
14dc50
The upstream regression test is not applicable for PG10
14dc50
14dc50
diff -urN -x '*cscope*' postgresql-10.23/src/include/common/int.h postgresql-10-patched/src/include/common/int.h
14dc50
--- postgresql-10.23/src/include/common/int.h	1970-01-01 01:00:00.000000000 +0100
14dc50
+++ postgresql-10-patched/src/include/common/int.h	2023-11-09 16:44:19.000000000 +0100
14dc50
@@ -0,0 +1,273 @@
14dc50
+/*-------------------------------------------------------------------------
14dc50
+ *
14dc50
+ * int.h
14dc50
+ *	  Routines to perform integer math, while checking for overflows.
14dc50
+ *
14dc50
+ * The routines in this file are intended to be well defined C, without
14dc50
+ * relying on compiler flags like -fwrapv.
14dc50
+ *
14dc50
+ * To reduce the overhead of these routines try to use compiler intrinsics
14dc50
+ * where available. That's not that important for the 16, 32 bit cases, but
14dc50
+ * the 64 bit cases can be considerably faster with intrinsics. In case no
14dc50
+ * intrinsics are available 128 bit math is used where available.
14dc50
+ *
14dc50
+ * Copyright (c) 2017-2018, PostgreSQL Global Development Group
14dc50
+ *
14dc50
+ * src/include/common/int.h
14dc50
+ *
14dc50
+ *-------------------------------------------------------------------------
14dc50
+ */
14dc50
+#ifndef COMMON_INT_H
14dc50
+#define COMMON_INT_H
14dc50
+
14dc50
+/*
14dc50
+ * If a + b overflows, return true, otherwise store the result of a + b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_add_s16_overflow(int16 a, int16 b, int16 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_add_overflow(a, b, result);
14dc50
+#else
14dc50
+	int32		res = (int32) a + (int32) b;
14dc50
+
14dc50
+	if (res > PG_INT16_MAX || res < PG_INT16_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int16) res;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a - b overflows, return true, otherwise store the result of a - b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_sub_overflow(a, b, result);
14dc50
+#else
14dc50
+	int32		res = (int32) a - (int32) b;
14dc50
+
14dc50
+	if (res > PG_INT16_MAX || res < PG_INT16_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int16) res;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a * b overflows, return true, otherwise store the result of a * b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_mul_s16_overflow(int16 a, int16 b, int16 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_mul_overflow(a, b, result);
14dc50
+#else
14dc50
+	int32		res = (int32) a * (int32) b;
14dc50
+
14dc50
+	if (res > PG_INT16_MAX || res < PG_INT16_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int16) res;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a + b overflows, return true, otherwise store the result of a + b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_add_s32_overflow(int32 a, int32 b, int32 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_add_overflow(a, b, result);
14dc50
+#else
14dc50
+	int64		res = (int64) a + (int64) b;
14dc50
+
14dc50
+	if (res > PG_INT32_MAX || res < PG_INT32_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int32) res;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a - b overflows, return true, otherwise store the result of a - b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_sub_overflow(a, b, result);
14dc50
+#else
14dc50
+	int64		res = (int64) a - (int64) b;
14dc50
+
14dc50
+	if (res > PG_INT32_MAX || res < PG_INT32_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int32) res;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a * b overflows, return true, otherwise store the result of a * b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_mul_overflow(a, b, result);
14dc50
+#else
14dc50
+	int64		res = (int64) a * (int64) b;
14dc50
+
14dc50
+	if (res > PG_INT32_MAX || res < PG_INT32_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int32) res;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a + b overflows, return true, otherwise store the result of a + b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_add_s64_overflow(int64 a, int64 b, int64 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_add_overflow(a, b, result);
14dc50
+#elif defined(HAVE_INT128)
14dc50
+	int128		res = (int128) a + (int128) b;
14dc50
+
14dc50
+	if (res > PG_INT64_MAX || res < PG_INT64_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int64) res;
14dc50
+	return false;
14dc50
+#else
14dc50
+	if ((a > 0 && b > 0 && a > PG_INT64_MAX - b) ||
14dc50
+		(a < 0 && b < 0 && a < PG_INT64_MIN - b))
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = a + b;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a - b overflows, return true, otherwise store the result of a - b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_sub_overflow(a, b, result);
14dc50
+#elif defined(HAVE_INT128)
14dc50
+	int128		res = (int128) a - (int128) b;
14dc50
+
14dc50
+	if (res > PG_INT64_MAX || res < PG_INT64_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int64) res;
14dc50
+	return false;
14dc50
+#else
14dc50
+	if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) ||
14dc50
+		(a > 0 && b < 0 && a > PG_INT64_MAX + b))
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = a - b;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+/*
14dc50
+ * If a * b overflows, return true, otherwise store the result of a * b into
14dc50
+ * *result. The content of *result is implementation defined in case of
14dc50
+ * overflow.
14dc50
+ */
14dc50
+static inline bool
14dc50
+pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
14dc50
+{
14dc50
+#if defined(HAVE__BUILTIN_OP_OVERFLOW)
14dc50
+	return __builtin_mul_overflow(a, b, result);
14dc50
+#elif defined(HAVE_INT128)
14dc50
+	int128		res = (int128) a * (int128) b;
14dc50
+
14dc50
+	if (res > PG_INT64_MAX || res < PG_INT64_MIN)
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = (int64) res;
14dc50
+	return false;
14dc50
+#else
14dc50
+	/*
14dc50
+	 * Overflow can only happen if at least one value is outside the range
14dc50
+	 * sqrt(min)..sqrt(max) so check that first as the division can be quite a
14dc50
+	 * bit more expensive than the multiplication.
14dc50
+	 *
14dc50
+	 * Multiplying by 0 or 1 can't overflow of course and checking for 0
14dc50
+	 * separately avoids any risk of dividing by 0.  Be careful about dividing
14dc50
+	 * INT_MIN by -1 also, note reversing the a and b to ensure we're always
14dc50
+	 * dividing it by a positive value.
14dc50
+	 *
14dc50
+	 */
14dc50
+	if ((a > PG_INT32_MAX || a < PG_INT32_MIN ||
14dc50
+		 b > PG_INT32_MAX || b < PG_INT32_MIN) &&
14dc50
+		a != 0 && a != 1 && b != 0 && b != 1 &&
14dc50
+		((a > 0 && b > 0 && a > PG_INT64_MAX / b) ||
14dc50
+		 (a > 0 && b < 0 && b < PG_INT64_MIN / a) ||
14dc50
+		 (a < 0 && b > 0 && a < PG_INT64_MIN / b) ||
14dc50
+		 (a < 0 && b < 0 && a < PG_INT64_MAX / b)))
14dc50
+	{
14dc50
+		*result = 0x5EED;		/* to avoid spurious warnings */
14dc50
+		return true;
14dc50
+	}
14dc50
+	*result = a * b;
14dc50
+	return false;
14dc50
+#endif
14dc50
+}
14dc50
+
14dc50
+#endif							/* COMMON_INT_H */
14dc50
diff -urN -x '*cscope*' postgresql-10.23/src/backend/utils/adt/arrayfuncs.c postgresql-10-patched/src/backend/utils/adt/arrayfuncs.c
14dc50
--- postgresql-10.23/src/backend/utils/adt/arrayfuncs.c	2022-11-07 22:51:10.000000000 +0100
14dc50
+++ postgresql-10-patched/src/backend/utils/adt/arrayfuncs.c	2023-11-09 14:36:30.000000000 +0100
14dc50
@@ -31,7 +31,7 @@
14dc50
 #include "utils/lsyscache.h"
14dc50
 #include "utils/memutils.h"
14dc50
 #include "utils/typcache.h"
14dc50
-
14dc50
+#include "common/int.h"
14dc50
 
14dc50
 /*
14dc50
  * GUC parameter
14dc50
@@ -2309,22 +2309,35 @@
14dc50
 	addedbefore = addedafter = 0;
14dc50
 
14dc50
 	/*
14dc50
-	 * Check subscripts
14dc50
-	 */
14dc50
+         * Check subscripts.  We assume the existing subscripts passed
14dc50
+         * ArrayCheckBounds, so that dim[i] + lb[i] can be computed without
14dc50
+         * overflow.  But we must beware of other overflows in our calculations of
14dc50
+         * new dim[] values.
14dc50
+         */
14dc50
 	if (ndim == 1)
14dc50
 	{
14dc50
 		if (indx[0] < lb[0])
14dc50
 		{
14dc50
-			addedbefore = lb[0] - indx[0];
14dc50
-			dim[0] += addedbefore;
14dc50
+			if (pg_sub_s32_overflow(lb[0], indx[0], &addedbefore) ||
14dc50
+                                pg_add_s32_overflow(dim[0], addedbefore, &dim[0]))
14dc50
+                                ereport(ERROR,
14dc50
+                                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
14dc50
+                                                 errmsg("array size exceeds the maximum allowed (%d)",
14dc50
+                                                                (int) MaxArraySize)));
14dc50
+
14dc50
 			lb[0] = indx[0];
14dc50
 			if (addedbefore > 1)
14dc50
 				newhasnulls = true; /* will insert nulls */
14dc50
 		}
14dc50
 		if (indx[0] >= (dim[0] + lb[0]))
14dc50
 		{
14dc50
-			addedafter = indx[0] - (dim[0] + lb[0]) + 1;
14dc50
-			dim[0] += addedafter;
14dc50
+			if (pg_sub_s32_overflow(indx[0], dim[0] + lb[0], &addedafter) ||
14dc50
+                                pg_add_s32_overflow(addedafter, 1, &addedafter) ||
14dc50
+                                pg_add_s32_overflow(dim[0], addedafter, &dim[0]))
14dc50
+                                ereport(ERROR,
14dc50
+                                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
14dc50
+                                                 errmsg("array size exceeds the maximum allowed (%d)",
14dc50
+                                                                (int) MaxArraySize)));
14dc50
 			if (addedafter > 1)
14dc50
 				newhasnulls = true; /* will insert nulls */
14dc50
 		}
14dc50
@@ -2568,14 +2581,21 @@
14dc50
 	addedbefore = addedafter = 0;
14dc50
 
14dc50
 	/*
14dc50
-	 * Check subscripts (this logic matches original array_set_element)
14dc50
-	 */
14dc50
+         * Check subscripts (this logic must match array_set_element).  We assume
14dc50
+         * the existing subscripts passed ArrayCheckBounds, so that dim[i] + lb[i]
14dc50
+         * can be computed without overflow.  But we must beware of other
14dc50
+         * overflows in our calculations of new dim[] values.
14dc50
+         */
14dc50
 	if (ndim == 1)
14dc50
 	{
14dc50
 		if (indx[0] < lb[0])
14dc50
 		{
14dc50
-			addedbefore = lb[0] - indx[0];
14dc50
-			dim[0] += addedbefore;
14dc50
+			if (pg_sub_s32_overflow(lb[0], indx[0], &addedbefore) ||
14dc50
+                                pg_add_s32_overflow(dim[0], addedbefore, &dim[0]))
14dc50
+                                ereport(ERROR,
14dc50
+                                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
14dc50
+                                                 errmsg("array size exceeds the maximum allowed (%d)",
14dc50
+                                                                (int) MaxArraySize)));
14dc50
 			lb[0] = indx[0];
14dc50
 			dimschanged = true;
14dc50
 			if (addedbefore > 1)
14dc50
@@ -2583,8 +2603,13 @@
14dc50
 		}
14dc50
 		if (indx[0] >= (dim[0] + lb[0]))
14dc50
 		{
14dc50
-			addedafter = indx[0] - (dim[0] + lb[0]) + 1;
14dc50
-			dim[0] += addedafter;
14dc50
+			if (pg_sub_s32_overflow(indx[0], dim[0] + lb[0], &addedafter) ||
14dc50
+                                pg_add_s32_overflow(addedafter, 1, &addedafter) ||
14dc50
+                                pg_add_s32_overflow(dim[0], addedafter, &dim[0]))
14dc50
+                                ereport(ERROR,
14dc50
+                                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
14dc50
+                                                 errmsg("array size exceeds the maximum allowed (%d)",
14dc50
+                                                                (int) MaxArraySize)));
14dc50
 			dimschanged = true;
14dc50
 			if (addedafter > 1)
14dc50
 				newhasnulls = true; /* will insert nulls */
14dc50
@@ -2866,8 +2891,11 @@
14dc50
 	addedbefore = addedafter = 0;
14dc50
 
14dc50
 	/*
14dc50
-	 * Check subscripts
14dc50
-	 */
14dc50
+         * Check subscripts.  We assume the existing subscripts passed
14dc50
+         * ArrayCheckBounds, so that dim[i] + lb[i] can be computed without
14dc50
+         * overflow.  But we must beware of other overflows in our calculations of
14dc50
+         * new dim[] values.
14dc50
+         */
14dc50
 	if (ndim == 1)
14dc50
 	{
14dc50
 		Assert(nSubscripts == 1);
14dc50
@@ -2881,18 +2909,31 @@
14dc50
 					 errmsg("upper bound cannot be less than lower bound")));
14dc50
 		if (lowerIndx[0] < lb[0])
14dc50
 		{
14dc50
-			if (upperIndx[0] < lb[0] - 1)
14dc50
-				newhasnulls = true; /* will insert nulls */
14dc50
-			addedbefore = lb[0] - lowerIndx[0];
14dc50
-			dim[0] += addedbefore;
14dc50
+			//addedbefore = lb[0] - lowerIndx[0];
14dc50
+			//dim[0] += addedbefore;
14dc50
+			if (pg_sub_s32_overflow(lb[0], lowerIndx[0], &addedbefore) ||
14dc50
+                                pg_add_s32_overflow(dim[0], addedbefore, &dim[0]))
14dc50
+                                ereport(ERROR,
14dc50
+                                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
14dc50
+                                                 errmsg("array size exceeds the maximum allowed (%d)",
14dc50
+                                                                (int) MaxArraySize)));
14dc50
 			lb[0] = lowerIndx[0];
14dc50
+			if (addedbefore > 1)
14dc50
+                                newhasnulls = true; /* will insert nulls */
14dc50
 		}
14dc50
 		if (upperIndx[0] >= (dim[0] + lb[0]))
14dc50
 		{
14dc50
-			if (lowerIndx[0] > (dim[0] + lb[0]))
14dc50
-				newhasnulls = true; /* will insert nulls */
14dc50
-			addedafter = upperIndx[0] - (dim[0] + lb[0]) + 1;
14dc50
-			dim[0] += addedafter;
14dc50
+			//addedafter = upperIndx[0] - (dim[0] + lb[0]) + 1;
14dc50
+			//dim[0] += addedafter;
14dc50
+			if (pg_sub_s32_overflow(upperIndx[0], dim[0] + lb[0], &addedafter) ||
14dc50
+                                pg_add_s32_overflow(addedafter, 1, &addedafter) ||
14dc50
+                                pg_add_s32_overflow(dim[0], addedafter, &dim[0]))
14dc50
+                                ereport(ERROR,
14dc50
+                                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
14dc50
+                                                 errmsg("array size exceeds the maximum allowed (%d)",
14dc50
+                                                                (int) MaxArraySize)));
14dc50
+		if (addedafter > 1)
14dc50
+                                newhasnulls = true; /* will insert nulls */
14dc50
 		}
14dc50
 	}
14dc50
 	else
14dc50
diff -urN -x '*cscope*' postgresql-10.23/src/backend/utils/adt/arrayutils.c postgresql-10-patched/src/backend/utils/adt/arrayutils.c
14dc50
--- postgresql-10.23/src/backend/utils/adt/arrayutils.c	2022-11-07 22:51:10.000000000 +0100
14dc50
+++ postgresql-10-patched/src/backend/utils/adt/arrayutils.c	2023-11-09 14:38:52.000000000 +0100
14dc50
@@ -63,10 +63,6 @@
14dc50
  * This must do overflow checking, since it is used to validate that a user
14dc50
  * dimensionality request doesn't overflow what we can handle.
14dc50
  *
14dc50
- * We limit array sizes to at most about a quarter billion elements,
14dc50
- * so that it's not necessary to check for overflow in quite so many
14dc50
- * places --- for instance when palloc'ing Datum arrays.
14dc50
- *
14dc50
  * The multiplication overflow check only works on machines that have int64
14dc50
  * arithmetic, but that is nearly all platforms these days, and doing check
14dc50
  * divides for those that don't seems way too expensive.
14dc50
@@ -77,7 +73,6 @@
14dc50
 	int32		ret;
14dc50
 	int			i;
14dc50
 
14dc50
-#define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
14dc50
 
14dc50
 	if (ndim <= 0)
14dc50
 		return 0;
14dc50
diff -urN -x '*cscope*' postgresql-10.23/src/include/utils/array.h postgresql-10-patched/src/include/utils/array.h
14dc50
--- postgresql-10.23/src/include/utils/array.h	2022-11-07 22:51:10.000000000 +0100
14dc50
+++ postgresql-10-patched/src/include/utils/array.h	2023-11-09 14:41:48.000000000 +0100
14dc50
@@ -66,6 +66,13 @@
14dc50
 
14dc50
 
14dc50
 /*
14dc50
+ * Maximum number of elements in an array.  We limit this to at most about a
14dc50
+ * quarter billion elements, so that it's not necessary to check for overflow
14dc50
+ * in quite so many places --- for instance when palloc'ing Datum arrays.
14dc50
+ */
14dc50
+#define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
14dc50
+
14dc50
+/*
14dc50
  * Arrays are varlena objects, so must meet the varlena convention that
14dc50
  * the first int32 of the object contains the total object size in bytes.
14dc50
  * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though!