Skip to content

Commit 359e539

Browse files
committed
reject negative token values in compressed stream receivers
Validate that token numbers read from compressed streams are non-negative. A negative token value would cause the return value of recv_*_token() to become positive, which callers interpret as literal data length, but no data pointer is set on this code path. While this only causes the receiver to crash (which is process-isolated and only affects the attacker's own connection), it's still undefined behavior. Reported-by: Will Sergeant <[email protected]>
1 parent 9e08984 commit 359e539

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

token.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,13 @@ static int32 recv_deflated_token(int f, char **data)
590590
if (flag & TOKEN_REL) {
591591
rx_token += flag & 0x3f;
592592
flag >>= 6;
593-
} else
593+
} else {
594594
rx_token = read_int(f);
595+
if (rx_token < 0) {
596+
rprintf(FERROR, "invalid token number in compressed stream\n");
597+
exit_cleanup(RERR_PROTOCOL);
598+
}
599+
}
595600
if (flag & 1) {
596601
rx_run = read_byte(f);
597602
rx_run += read_byte(f) << 8;
@@ -834,8 +839,13 @@ static int32 recv_zstd_token(int f, char **data)
834839
if (flag & TOKEN_REL) {
835840
rx_token += flag & 0x3f;
836841
flag >>= 6;
837-
} else
842+
} else {
838843
rx_token = read_int(f);
844+
if (rx_token < 0) {
845+
rprintf(FERROR, "invalid token number in compressed stream\n");
846+
exit_cleanup(RERR_PROTOCOL);
847+
}
848+
}
839849
if (flag & 1) {
840850
rx_run = read_byte(f);
841851
rx_run += read_byte(f) << 8;
@@ -998,8 +1008,13 @@ static int32 recv_compressed_token(int f, char **data)
9981008
if (flag & TOKEN_REL) {
9991009
rx_token += flag & 0x3f;
10001010
flag >>= 6;
1001-
} else
1011+
} else {
10021012
rx_token = read_int(f);
1013+
if (rx_token < 0) {
1014+
rprintf(FERROR, "invalid token number in compressed stream\n");
1015+
exit_cleanup(RERR_PROTOCOL);
1016+
}
1017+
}
10031018
if (flag & 1) {
10041019
rx_run = read_byte(f);
10051020
rx_run += read_byte(f) << 8;

0 commit comments

Comments
 (0)