tsdb: guard chunk length overflow in head chunk reader (#17533)

Signed-off-by: 0xkato <0xkkato@gmail.com>
This commit is contained in:
0xkato 2025-11-15 21:09:00 +01:00 committed by GitHub
parent c64dd612ef
commit ae00fd45ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,6 +20,7 @@ import (
"fmt"
"hash"
"io"
"math"
"os"
"path/filepath"
"slices"
@ -768,8 +769,25 @@ func (cdm *ChunkDiskMapper) Chunk(ref ChunkDiskMapperRef) (chunkenc.Chunk, error
}
}
if chkDataLen > uint64(math.MaxInt) {
return nil, &CorruptionErr{
Dir: cdm.dir.Name(),
FileIndex: sgmIndex,
Err: fmt.Errorf("chunk length %d exceeds supported size", chkDataLen),
}
}
chkDataLenInt := int(chkDataLen)
if chkDataLenStart > math.MaxInt-n-chkDataLenInt {
return nil, &CorruptionErr{
Dir: cdm.dir.Name(),
FileIndex: sgmIndex,
Err: fmt.Errorf("chunk data end overflows supported size (start=%d, len=%d, n=%d)", chkDataLenStart, chkDataLenInt, n),
}
}
// Verify the chunk data end.
chkDataEnd := chkDataLenStart + n + int(chkDataLen)
chkDataEnd := chkDataLenStart + n + chkDataLenInt
if chkDataEnd > mmapFile.byteSlice.Len() {
return nil, &CorruptionErr{
Dir: cdm.dir.Name(),