Skip to content

Commit d130a64

Browse files
committed
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 67a472d there was a separate test on the input string length that prevented this hazard. Per Coverity report.
1 parent 5532725 commit d130a64

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/common/saslprep.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121
*/
2222
#ifndef FRONTEND
2323
#include "postgres.h"
24+
#include "utils/memutils.h"
2425
#else
2526
#include "postgres_fe.h"
27+
28+
/* It's possible we could use a different value for this in frontend code */
29+
#define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
30+
2631
#endif
2732

2833
#include "common/saslprep.h"
@@ -1077,6 +1082,8 @@ pg_saslprep(const char *input, char **output)
10771082
input_size = pg_utf8_string_len(input);
10781083
if (input_size < 0)
10791084
return SASLPREP_INVALID_UTF8;
1085+
if (input_size >= MaxAllocSize / sizeof(pg_wchar))
1086+
goto oom;
10801087

10811088
input_chars = ALLOC((input_size + 1) * sizeof(pg_wchar));
10821089
if (!input_chars)

0 commit comments

Comments
 (0)