aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-10-28 14:33:55 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-10-28 14:33:55 -0400
commitbd2843167200f2607972c0defb916daa79ca265f (patch)
tree3edb33cf37728040b3d15155a165ae00c24ad8ac
parent6cfb3a33746990602c790199adc2debb2c4bbb87 (diff)
downloadpostgresql-bd2843167200f2607972c0defb916daa79ca265f.tar.gz
postgresql-bd2843167200f2607972c0defb916daa79ca265f.zip
Guard against enormously long input in pg_saslprep().
Coverity complained that pg_saslprep() could suffer integer overflow, leading to under-allocation of the output buffer, if the input string exceeds SIZE_MAX/4. This hazard seems largely hypothetical, but it's easy enough to defend against, so let's do so. This patch creates a third place in src/common/ where we are locally defining MaxAllocSize so that we can test against that in the same way in backend and frontend compiles. That seems like about two places too many, so the next patch will move that into common/fe_memutils.h. I'm hesitant to do that in back branches however. Back-patch to v14. The code looks similar in older branches, but before commit 67a472d71 there was a separate test on the input string length that prevented this hazard. Per Coverity report.
-rw-r--r--src/common/saslprep.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/common/saslprep.c b/src/common/saslprep.c
index 78f6fcbd805..0a840352802 100644
--- a/src/common/saslprep.c
+++ b/src/common/saslprep.c
@@ -21,8 +21,13 @@
*/
#ifndef FRONTEND
#include "postgres.h"
+#include "utils/memutils.h"
#else
#include "postgres_fe.h"
+
+/* It's possible we could use a different value for this in frontend code */
+#define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
+
#endif
#include "common/saslprep.h"
@@ -1079,6 +1084,8 @@ pg_saslprep(const char *input, char **output)
input_size = pg_utf8_string_len(input);
if (input_size < 0)
return SASLPREP_INVALID_UTF8;
+ if (input_size >= MaxAllocSize / sizeof(pg_wchar))
+ goto oom;
input_chars = ALLOC((input_size + 1) * sizeof(pg_wchar));
if (!input_chars)