From d42476aa58fca07b17ee39d388639807624f884a Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Fri, 26 Jul 2024 15:27:25 +0200 Subject: [PATCH] fix: Handle block offset exceeding chunk length in memchunk.go (#13661) --- pkg/chunkenc/memchunk.go | 3 +++ pkg/storage/chunk/client/object_client.go | 3 ++- pkg/storage/chunk/fetcher/fetcher.go | 4 +--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/chunkenc/memchunk.go b/pkg/chunkenc/memchunk.go index f6197888a854..3d87aec705dc 100644 --- a/pkg/chunkenc/memchunk.go +++ b/pkg/chunkenc/memchunk.go @@ -476,6 +476,9 @@ func newByteChunk(b []byte, blockSize, targetSize int, fromCheckpoint bool) (*Me blk.uncompressedSize = db.uvarint() } l := db.uvarint() + if blk.offset+l > len(b) { + return nil, fmt.Errorf("block %d offset %d + length %d exceeds chunk length %d", i, blk.offset, l, len(b)) + } blk.b = b[blk.offset : blk.offset+l] // Verify checksums. diff --git a/pkg/storage/chunk/client/object_client.go b/pkg/storage/chunk/client/object_client.go index 2f5c3e263e85..5dfb1945a5f3 100644 --- a/pkg/storage/chunk/client/object_client.go +++ b/pkg/storage/chunk/client/object_client.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/base64" + "fmt" "io" "strings" "time" @@ -186,7 +187,7 @@ func (o *client) getChunk(ctx context.Context, decodeContext *chunk.DecodeContex } if err := c.Decode(decodeContext, buf.Bytes()); err != nil { - return chunk.Chunk{}, errors.WithStack(err) + return chunk.Chunk{}, errors.WithStack(fmt.Errorf("failed to decode chunk '%s': %w", key, err)) } return c, nil } diff --git a/pkg/storage/chunk/fetcher/fetcher.go b/pkg/storage/chunk/fetcher/fetcher.go index cf763b9cbedc..45b6970045a9 100644 --- a/pkg/storage/chunk/fetcher/fetcher.go +++ b/pkg/storage/chunk/fetcher/fetcher.go @@ -9,7 +9,6 @@ import ( "github.com/opentracing/opentracing-go" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/prometheus/promql" "github.com/grafana/loki/v3/pkg/logqlmodel/stats" "github.com/grafana/loki/v3/pkg/storage/chunk" @@ -218,8 +217,7 @@ func (c *Fetcher) FetchChunks(ctx context.Context, chunks []chunk.Chunk) ([]chun } if err != nil { - // Don't rely on Cortex error translation here. - return nil, promql.ErrStorage{Err: err} + level.Error(log).Log("msg", "failed downloading chunks", "err", err) } allChunks := append(fromCache, fromStorage...)