Skip to content

Commit a6dd59b

Browse files
committed
Fix size_t overflow in Malloc() argument in ReadParams()
There were still two issues after commit b0eabca (Update fcgiapp.c, Fixing an integer overflow (CVE-2025-23016)): * Signed int overflow in "nameLen + valueLen + 2" expression. * Sizes of size_t and int types are in general unrelated. This fix resolves both of the issues. Related to CVE-2025-23016. Resolve #67.
1 parent 1ad4873 commit a6dd59b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

libfcgi/fcgiapp.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory.h> /* for memchr() */
1919
#include <stdarg.h>
2020
#include <stdio.h>
21+
#include <stdint.h>
2122
#include <stdlib.h>
2223
#include <string.h>
2324
#include <sys/types.h>
@@ -1160,6 +1161,7 @@ char *FCGX_GetParam(const char *name, FCGX_ParamArray envp)
11601161
static int ReadParams(Params *paramsPtr, FCGX_Stream *stream)
11611162
{
11621163
int nameLen, valueLen;
1164+
size_t totalLen;
11631165
unsigned char lenBuff[3];
11641166
char *nameValue;
11651167

@@ -1175,7 +1177,7 @@ static int ReadParams(Params *paramsPtr, FCGX_Stream *stream)
11751177
}
11761178
nameLen = ((nameLen & 0x7f) << 24) + (lenBuff[0] << 16)
11771179
+ (lenBuff[1] << 8) + lenBuff[2];
1178-
if (nameLen >= INT_MAX) {
1180+
if (nameLen >= INT_MAX || nameLen >= SIZE_MAX) {
11791181
SetError(stream, FCGX_PARAMS_ERROR);
11801182
return -1;
11811183
}
@@ -1191,16 +1193,21 @@ static int ReadParams(Params *paramsPtr, FCGX_Stream *stream)
11911193
}
11921194
valueLen = ((valueLen & 0x7f) << 24) + (lenBuff[0] << 16)
11931195
+ (lenBuff[1] << 8) + lenBuff[2];
1194-
if (valueLen >= INT_MAX) {
1196+
if (valueLen >= INT_MAX || valueLen >= SIZE_MAX) {
11951197
SetError(stream, FCGX_PARAMS_ERROR);
11961198
return -1;
11971199
}
11981200
}
1201+
totalLen = (size_t)nameLen + (size_t)valueLen + 2u;
1202+
if (totalLen < (size_t)nameLen || totalLen < (size_t)valueLen) {
1203+
SetError(stream, FCGX_PARAMS_ERROR);
1204+
return -1;
1205+
}
11991206
/*
12001207
* nameLen and valueLen are now valid; read the name and value
12011208
* from stream and construct a standard environment entry.
12021209
*/
1203-
nameValue = (char *)Malloc(nameLen + valueLen + 2);
1210+
nameValue = (char *)Malloc(totalLen);
12041211
if(FCGX_GetStr(nameValue, nameLen, stream) != nameLen) {
12051212
SetError(stream, FCGX_PARAMS_ERROR);
12061213
free(nameValue);

0 commit comments

Comments
 (0)