Skip to content

Commit bd28431

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 6cfb3a3 commit bd28431

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"
@@ -1079,6 +1084,8 @@ pg_saslprep(const char *input, char **output)
10791084
input_size = pg_utf8_string_len(input);
10801085
if (input_size < 0)
10811086
return SASLPREP_INVALID_UTF8;
1087+
if (input_size >= MaxAllocSize / sizeof(pg_wchar))
1088+
goto oom;
10821089

10831090
input_chars = ALLOC((input_size + 1) * sizeof(pg_wchar));
10841091
if (!input_chars)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy