Skip to content

Commit

Permalink
fix: avoid SIGBUS when reading non-std series segment files
Browse files Browse the repository at this point in the history
Some series files which are smaller than the standard
sizes cause SIGBUS in influx_inspect and influxd, because
entry iteration walks onto mapped memory not backed by the
the file.  Avoid walking off the end of the file while
iterating series entries in oddly sized files.

closes #24508
  • Loading branch information
davidby-influx committed Dec 6, 2023
1 parent 5cbade5 commit e82ffeb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
5 changes: 3 additions & 2 deletions cmd/influx_inspect/verify/seriesfile/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (v Verify) VerifySegment(segmentPath string, ids map[uint64]IDData) (valid
v.Logger = v.Logger.With(zap.String("segment", segmentName))
v.Logger.Info("Verifying segment")

// Open up the segment and grab it's data.
// Open up the segment and grab its data.
segmentID, err := tsdb.ParseSeriesSegmentFilename(segmentName)
if err != nil {
return false, err
Expand All @@ -195,7 +195,8 @@ func (v Verify) VerifySegment(segmentPath string, ids map[uint64]IDData) (valid
return false, nil
}
defer segment.Close()
buf := newBuffer(segment.Data())
// Only walk the file as it exists, not the whole mapping which may be bigger than the file.
buf := newBuffer(segment.Data()[:segment.Size()])

defer func() {
if rec := recover(); rec != nil {
Expand Down
4 changes: 2 additions & 2 deletions tsdb/series_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ func AppendSeriesKey(dst []byte, name []byte, tags models.Tags) []byte {
}

// ReadSeriesKey returns the series key from the beginning of the buffer.
func ReadSeriesKey(data []byte) (key, remainder []byte) {
func ReadSeriesKey(data []byte) (key []byte) {
sz, n := binary.Uvarint(data)
return data[:int(sz)+n], data[int(sz)+n:]
return data[:int(sz)+n]
}

func ReadSeriesKeyLen(data []byte) (sz int, remainder []byte) {
Expand Down
2 changes: 1 addition & 1 deletion tsdb/series_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (p *SeriesPartition) seriesKeyByOffset(offset int64) []byte {
continue
}

key, _ := ReadSeriesKey(segment.Slice(pos + SeriesEntryHeaderSize))
key := ReadSeriesKey(segment.Slice(pos + SeriesEntryHeaderSize))
return key
}

Expand Down
24 changes: 16 additions & 8 deletions tsdb/series_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func CreateSeriesSegment(id uint16, path string) (*SeriesSegment, error) {
// Open memory maps the data file at the file's path.
func (s *SeriesSegment) Open() error {
if err := func() (err error) {
st, err := os.Stat(s.path)
if err != nil {
return fmt.Errorf("cannot stat %s: %w", s.path, err)
}
s.size = uint32(st.Size())

// Memory map file data.
if s.data, err = mmap.Map(s.path, int64(SeriesSegmentSize(s.id))); err != nil {
return err
Expand Down Expand Up @@ -120,14 +126,16 @@ func (s *SeriesSegment) Path() string { return s.path }
// InitForWrite initializes a write handle for the segment.
// This is only used for the last segment in the series file.
func (s *SeriesSegment) InitForWrite() (err error) {
// Only calculcate segment data size if writing.
for s.size = uint32(SeriesSegmentHeaderSize); s.size < uint32(len(s.data)); {
flag, _, _, sz := ReadSeriesEntry(s.data[s.size:])
// Only recalculate segment data size if writing.
var size uint32
for size = uint32(SeriesSegmentHeaderSize); size < s.size; {
flag, _, _, sz := ReadSeriesEntry(s.data[size:])
if !IsValidSeriesEntryFlag(flag) {
break
}
s.size += uint32(sz)
size += uint32(sz)
}
s.size = size

// Open file handler for writing & seek to end of data.
if s.file, err = os.OpenFile(s.path, os.O_WRONLY|os.O_CREATE, 0666); err != nil {
Expand Down Expand Up @@ -243,8 +251,8 @@ func (s *SeriesSegment) MaxSeriesID() uint64 {

// ForEachEntry executes fn for every entry in the segment.
func (s *SeriesSegment) ForEachEntry(fn func(flag uint8, id uint64, offset int64, key []byte) error) error {
for pos := uint32(SeriesSegmentHeaderSize); pos < uint32(len(s.data)); {
flag, id, key, sz := ReadSeriesEntry(s.data[pos:])
for pos := uint32(SeriesSegmentHeaderSize); pos < s.size; {
flag, id, key, sz := ReadSeriesEntry(s.data[pos:s.size])
if !IsValidSeriesEntryFlag(flag) {
break
}
Expand Down Expand Up @@ -337,7 +345,7 @@ func ReadSeriesKeyFromSegments(a []*SeriesSegment, offset int64) []byte {
return nil
}
buf := segment.Slice(pos)
key, _ := ReadSeriesKey(buf)
key := ReadSeriesKey(buf)
return key
}

Expand Down Expand Up @@ -425,7 +433,7 @@ func ReadSeriesEntry(data []byte) (flag uint8, id uint64, key []byte, sz int64)
id, data = binary.BigEndian.Uint64(data), data[8:]
switch flag {
case SeriesEntryInsertFlag:
key, _ = ReadSeriesKey(data)
key = ReadSeriesKey(data)
}
return flag, id, key, int64(SeriesEntryHeaderSize + len(key))
}
Expand Down

0 comments on commit e82ffeb

Please sign in to comment.