]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Make palloc_array() and friends safe against integer overflow.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 11 May 2026 12:13:46 +0000 (05:13 -0700)
committerNoah Misch <noah@leadboat.com>
Mon, 11 May 2026 12:13:46 +0000 (05:13 -0700)
Sufficiently large "count" arguments could result in undetected
overflow, causing the allocated memory chunk to be much smaller
than what the caller will subsequently write into it.  This is
unlikely to be a hazard with 64-bit size_t but can sometimes
happen on 32-bit builds, primarily where a function allocates
workspace that's significantly larger than its input data.
Rather than trying to patch the at-risk callers piecemeal,
let's just redefine these macros so that they always check.

To do that, move the longstanding add_size() and mul_size() functions
into palloc.h and mcxt.c, and adjust them to not be specific to
shared-memory allocation.  Then invent palloc_mul(), palloc0_mul(),
palloc_mul_extended() to use these functions.  Actually, the latter
use inlined copies to save one function call.  repalloc_array() gets
similar treatment.  I didn't bother trying to inline the calls for
repalloc0_array() though.

In v14 and v15, this also adds repalloc_extended(), which previously
was only available in v16 and up.

We need copies of all this in fe_memutils.[hc] as well, since that
module also provides palloc_array() etc.

Reported-by: Xint Code
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Backpatch-through: 14
Security: CVE-2026-6473

src/backend/storage/ipc/shmem.c
src/backend/utils/mmgr/mcxt.c
src/common/fe_memutils.c
src/include/common/fe_memutils.h
src/include/storage/shmem.h
src/include/utils/memutils.h
src/include/utils/palloc.h

index a4346b9bf9c1d1cfdd299c9e618d37c25ada034c..f1f7cd3a4ff2cb3851d4bfe5b73ba9544303de53 100644 (file)
 #include <unistd.h>
 
 #include "access/slru.h"
-#include "common/int.h"
 #include "fmgr.h"
 #include "funcapi.h"
 #include "miscadmin.h"
@@ -1041,36 +1040,6 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr)
        return ptr;
 }
 
-/*
- * Add two Size values, checking for overflow
- */
-Size
-add_size(Size s1, Size s2)
-{
-       Size            result;
-
-       if (pg_add_size_overflow(s1, s2, &result))
-               ereport(ERROR,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg("requested shared memory size overflows size_t")));
-       return result;
-}
-
-/*
- * Multiply two Size values, checking for overflow
- */
-Size
-mul_size(Size s1, Size s2)
-{
-       Size            result;
-
-       if (pg_mul_size_overflow(s1, s2, &result))
-               ereport(ERROR,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg("requested shared memory size overflows size_t")));
-       return result;
-}
-
 /* SQL SRF showing allocated shared memory */
 Datum
 pg_get_shmem_allocations(PG_FUNCTION_ARGS)
index 073bdb35d2acfb25bea9cefe200892fd707bab7f..930fc4573281188bd46299f981f80be4520fa2ae 100644 (file)
@@ -38,6 +38,7 @@
 
 #include "postgres.h"
 
+#include "common/int.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
 #include "utils/memdebug.h"
@@ -186,6 +187,8 @@ static void MemoryContextStatsInternal(MemoryContext context, int level,
 static void MemoryContextStatsPrint(MemoryContext context, void *passthru,
                                                                        const char *stats_string,
                                                                        bool print_to_stderr);
+pg_noreturn static pg_noinline void add_size_error(Size s1, Size s2);
+pg_noreturn static pg_noinline void mul_size_error(Size s1, Size s2);
 
 /*
  * You should not do memory allocations within a critical section, because
@@ -1715,6 +1718,132 @@ repalloc0(void *pointer, Size oldsize, Size size)
        return ret;
 }
 
+/*
+ * Support for safe calculation of memory request sizes
+ *
+ * These functions perform the requested calculation, but throw error if the
+ * result overflows.
+ *
+ * An important property of these functions is that if an argument was a
+ * negative signed int before promotion (implying overflow in calculating it)
+ * we will detect that as an error.  That happens because we reject results
+ * larger than SIZE_MAX / 2 later on, in the actual allocation step.
+ */
+Size
+add_size(Size s1, Size s2)
+{
+       Size            result;
+
+       if (unlikely(pg_add_size_overflow(s1, s2, &result)))
+               add_size_error(s1, s2);
+       return result;
+}
+
+pg_noreturn static pg_noinline void
+add_size_error(Size s1, Size s2)
+{
+       ereport(ERROR,
+                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                        errmsg("invalid memory allocation request size %zu + %zu",
+                                       s1, s2)));
+}
+
+Size
+mul_size(Size s1, Size s2)
+{
+       Size            result;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &result)))
+               mul_size_error(s1, s2);
+       return result;
+}
+
+pg_noreturn static pg_noinline void
+mul_size_error(Size s1, Size s2)
+{
+       ereport(ERROR,
+                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                        errmsg("invalid memory allocation request size %zu * %zu",
+                                       s1, s2)));
+}
+
+/*
+ * palloc_mul
+ *             Equivalent to palloc(mul_size(s1, s2)).
+ */
+void *
+palloc_mul(Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
+               mul_size_error(s1, s2);
+       return palloc(req);
+}
+
+/*
+ * palloc0_mul
+ *             Equivalent to palloc0(mul_size(s1, s2)).
+ *
+ * This is comparable to standard calloc's behavior.
+ */
+void *
+palloc0_mul(Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
+               mul_size_error(s1, s2);
+       return palloc0(req);
+}
+
+/*
+ * palloc_mul_extended
+ *             Equivalent to palloc_extended(mul_size(s1, s2), flags).
+ */
+void *
+palloc_mul_extended(Size s1, Size s2, int flags)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
+               mul_size_error(s1, s2);
+       return palloc_extended(req, flags);
+}
+
+/*
+ * repalloc_mul
+ *             Equivalent to repalloc(p, mul_size(s1, s2)).
+ */
+void *
+repalloc_mul(void *p, Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
+               mul_size_error(s1, s2);
+       return repalloc(p, req);
+}
+
+/*
+ * repalloc_mul_extended
+ *             Equivalent to repalloc_extended(p, mul_size(s1, s2), flags).
+ */
+void *
+repalloc_mul_extended(void *p, Size s1, Size s2, int flags)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req)))
+               mul_size_error(s1, s2);
+       return repalloc_extended(p, req, flags);
+}
+
 /*
  * MemoryContextAllocHuge
  *             Allocate (possibly-expansive) space within the specified context.
index d24a5f67704139ccdac09ca0569ef5513869f776..e6f8e74929c6af43df62edfbe5ba2e771df65cce 100644 (file)
 
 #include "postgres_fe.h"
 
+#include "common/int.h"
+
+pg_noreturn static pg_noinline void add_size_error(Size s1, Size s2);
+pg_noreturn static pg_noinline void mul_size_error(Size s1, Size s2);
+
+
 static inline void *
 pg_malloc_internal(size_t size, int flags)
 {
@@ -173,3 +179,185 @@ repalloc(void *pointer, Size size)
 {
        return pg_realloc(pointer, size);
 }
+
+/*
+ * Support for safe calculation of memory request sizes
+ *
+ * These functions perform the requested calculation, but throw error if the
+ * result overflows.
+ *
+ * An important property of these functions is that if an argument was a
+ * negative signed int before promotion (implying overflow in calculating it)
+ * we will detect that as an error.  That happens because we reject results
+ * larger than SIZE_MAX / 2.  In the backend we rely on later checks to do
+ * that, but in frontend we must do it here.
+ */
+Size
+add_size(Size s1, Size s2)
+{
+       Size            result;
+
+       if (unlikely(pg_add_size_overflow(s1, s2, &result) ||
+                                result > (SIZE_MAX / 2)))
+               add_size_error(s1, s2);
+       return result;
+}
+
+pg_noreturn static pg_noinline void
+add_size_error(Size s1, Size s2)
+{
+       fprintf(stderr, _("invalid memory allocation request size %zu + %zu\n"),
+                       s1, s2);
+       exit(EXIT_FAILURE);
+}
+
+Size
+mul_size(Size s1, Size s2)
+{
+       Size            result;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &result) ||
+                                result > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return result;
+}
+
+pg_noreturn static pg_noinline void
+mul_size_error(Size s1, Size s2)
+{
+       fprintf(stderr, _("invalid memory allocation request size %zu * %zu\n"),
+                       s1, s2);
+       exit(EXIT_FAILURE);
+}
+
+/*
+ * pg_malloc_mul
+ *             Equivalent to pg_malloc(mul_size(s1, s2)).
+ */
+void *
+pg_malloc_mul(Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return pg_malloc(req);
+}
+
+/*
+ * pg_malloc0_mul
+ *             Equivalent to pg_malloc0(mul_size(s1, s2)).
+ *
+ * This is comparable to standard calloc's behavior.
+ */
+void *
+pg_malloc0_mul(Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return pg_malloc0(req);
+}
+
+/*
+ * pg_malloc_mul_extended
+ *             Equivalent to pg_malloc_extended(mul_size(s1, s2), flags).
+ */
+void *
+pg_malloc_mul_extended(Size s1, Size s2, int flags)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return pg_malloc_extended(req, flags);
+}
+
+/*
+ * pg_realloc_mul
+ *             Equivalent to pg_realloc(p, mul_size(s1, s2)).
+ */
+void *
+pg_realloc_mul(void *p, Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return pg_realloc(p, req);
+}
+
+/*
+ * palloc_mul
+ *             Equivalent to palloc(mul_size(s1, s2)).
+ */
+void *
+palloc_mul(Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return palloc(req);
+}
+
+/*
+ * palloc0_mul
+ *             Equivalent to palloc0(mul_size(s1, s2)).
+ *
+ * This is comparable to standard calloc's behavior.
+ */
+void *
+palloc0_mul(Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return palloc0(req);
+}
+
+/*
+ * palloc_mul_extended
+ *             Equivalent to palloc_extended(mul_size(s1, s2), flags).
+ */
+void *
+palloc_mul_extended(Size s1, Size s2, int flags)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return palloc_extended(req, flags);
+}
+
+/*
+ * repalloc_mul
+ *             Equivalent to repalloc(p, mul_size(s1, s2)).
+ */
+void *
+repalloc_mul(void *p, Size s1, Size s2)
+{
+       /* inline mul_size() for efficiency */
+       Size            req;
+
+       if (unlikely(pg_mul_size_overflow(s1, s2, &req) ||
+                                req > (SIZE_MAX / 2)))
+               mul_size_error(s1, s2);
+       return repalloc(p, req);
+}
index eccdfcab0866c378a52518afc5b6b5ae6e7f5207..62bb9dcd9eb9e3d31ddcda4b4429f7f209906d21 100644 (file)
@@ -41,6 +41,16 @@ extern void *pg_malloc_extended(size_t size, int flags);
 extern void *pg_realloc(void *ptr, size_t size);
 extern void pg_free(void *ptr);
 
+/*
+ * Support for safe calculation of memory request sizes
+ */
+extern Size add_size(Size s1, Size s2);
+extern Size mul_size(Size s1, Size s2);
+extern void *pg_malloc_mul(Size s1, Size s2);
+extern void *pg_malloc0_mul(Size s1, Size s2);
+extern void *pg_malloc_mul_extended(Size s1, Size s2, int flags);
+extern void *pg_realloc_mul(void *p, Size s1, Size s2);
+
 /*
  * Variants with easier notation and more type safety
  */
@@ -54,14 +64,15 @@ extern void pg_free(void *ptr);
 /*
  * Allocate space for "count" objects of type "type"
  */
-#define pg_malloc_array(type, count) ((type *) pg_malloc(sizeof(type) * (count)))
-#define pg_malloc0_array(type, count) ((type *) pg_malloc0(sizeof(type) * (count)))
+#define pg_malloc_array(type, count) ((type *) pg_malloc_mul(sizeof(type), count))
+#define pg_malloc0_array(type, count) ((type *) pg_malloc0_mul(sizeof(type), count))
+#define pg_malloc_array_extended(type, count, flags) ((type *) pg_malloc_mul_extended(sizeof(type), count, flags))
 
 /*
  * Change size of allocation pointed to by "pointer" to have space for "count"
  * objects of type "type"
  */
-#define pg_realloc_array(pointer, type, count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
+#define pg_realloc_array(pointer, type, count) ((type *) pg_realloc_mul(pointer, sizeof(type), count))
 
 /* Equivalent functions, deliberately named the same as backend functions */
 extern char *pstrdup(const char *in);
@@ -71,12 +82,17 @@ extern void *palloc0(Size size);
 extern void *palloc_extended(Size size, int flags);
 extern void *repalloc(void *pointer, Size size);
 extern void pfree(void *pointer);
+extern void *palloc_mul(Size s1, Size s2);
+extern void *palloc0_mul(Size s1, Size s2);
+extern void *palloc_mul_extended(Size s1, Size s2, int flags);
+extern void *repalloc_mul(void *p, Size s1, Size s2);
 
 #define palloc_object(type) ((type *) palloc(sizeof(type)))
 #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
-#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
-#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
-#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
+#define palloc_array(type, count) ((type *) palloc_mul(sizeof(type), count))
+#define palloc0_array(type, count) ((type *) palloc0_mul(sizeof(type), count))
+#define palloc_array_extended(type, count, flags) ((type *) palloc_mul_extended(sizeof(type), count, flags))
+#define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count))
 
 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
 extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
index af7fe893bc4b3cb70a2f03386b27145aa796927e..43b636868c711f7414856f7e83c4d50ba8b62627 100644 (file)
@@ -189,9 +189,6 @@ extern HTAB *ShmemInitHash(const char *name, int64 nelems,
 extern void *ShmemAlloc(Size size);
 extern void *ShmemAllocNoError(Size size);
 
-extern Size add_size(Size s1, Size s2);
-extern Size mul_size(Size s1, Size s2);
-
 extern PGDLLIMPORT Size pg_get_shmem_pagesize(void);
 
 /* ipci.c */
index 11ab1717a16266457eeaab4edcb9259bf1c4091d..38036c7c703d517156517d4cc452e91595c67887 100644 (file)
@@ -41,7 +41,7 @@
 
 #define AllocSizeIsValid(size) ((Size) (size) <= MaxAllocSize)
 
-/* Must be less than SIZE_MAX */
+/* Do not make this any bigger; see add_size() and mul_size() */
 #define MaxAllocHugeSize       (SIZE_MAX / 2)
 
 #define InvalidAllocSize       SIZE_MAX
index ac53f189a4e4cbcb3a808fb85fb12d442a5a885b..afca0107a2beecd28e994558491c8c77a2711c4f 100644 (file)
@@ -85,6 +85,18 @@ pg_nodiscard extern void *repalloc_extended(void *pointer,
 pg_nodiscard extern void *repalloc0(void *pointer, Size oldsize, Size size);
 extern void pfree(void *pointer);
 
+/*
+ * Support for safe calculation of memory request sizes
+ */
+extern Size add_size(Size s1, Size s2);
+extern Size mul_size(Size s1, Size s2);
+extern void *palloc_mul(Size s1, Size s2);
+extern void *palloc0_mul(Size s1, Size s2);
+extern void *palloc_mul_extended(Size s1, Size s2, int flags);
+pg_nodiscard extern void *repalloc_mul(void *p, Size s1, Size s2);
+pg_nodiscard extern void *repalloc_mul_extended(void *p, Size s1, Size s2,
+                                                                                               int flags);
+
 /*
  * Variants with easier notation and more type safety
  */
@@ -98,15 +110,17 @@ extern void pfree(void *pointer);
 /*
  * Allocate space for "count" objects of type "type"
  */
-#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
-#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
+#define palloc_array(type, count) ((type *) palloc_mul(sizeof(type), count))
+#define palloc0_array(type, count) ((type *) palloc0_mul(sizeof(type), count))
+#define palloc_array_extended(type, count, flags) ((type *) palloc_mul_extended(sizeof(type), count, flags))
 
 /*
  * Change size of allocation pointed to by "pointer" to have space for "count"
  * objects of type "type"
  */
-#define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
-#define repalloc0_array(pointer, type, oldcount, count) ((type *) repalloc0(pointer, sizeof(type) * (oldcount), sizeof(type) * (count)))
+#define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count))
+#define repalloc0_array(pointer, type, oldcount, count) ((type *) repalloc0(pointer, mul_size(sizeof(type), oldcount), mul_size(sizeof(type), count)))
+#define repalloc_array_extended(pointer, type, count, flags) ((type *) repalloc_mul_extended(pointer, sizeof(type), count, flags))
 
 /* Higher-limit allocators. */
 extern void *MemoryContextAllocHuge(MemoryContext context, Size size);