Skip to content

Commit

Permalink
fix(tsi): close series id iterator after merging
Browse files Browse the repository at this point in the history
This use-after-free bug may lead to segfault. The iterators that have
reference to the underlying index files were closed too early while
the bitmaps were still used afterwards. If a compaction occurs
concurrently and removes the index files, it would result in accessing
unmap'd memory address.
  • Loading branch information
foobar authored and Tristan Su committed Jan 25, 2021
1 parent 10fcc2b commit 0be081b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tsdb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,10 @@ func IntersectSeriesIDIterators(itr0, itr1 SeriesIDIterator) SeriesIDIterator {

// Create series id set, if available.
if a := NewSeriesIDSetIterators([]SeriesIDIterator{itr0, itr1}); a != nil {
ss := a[0].SeriesIDSet().And(a[1].SeriesIDSet())
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(a[0].SeriesIDSet().And(a[1].SeriesIDSet()))
return NewSeriesIDSetIterator(ss)
}

return &seriesIDIntersectIterator{itrs: [2]SeriesIDIterator{itr0, itr1}}
Expand Down Expand Up @@ -646,10 +647,10 @@ func UnionSeriesIDIterators(itr0, itr1 SeriesIDIterator) SeriesIDIterator {

// Create series id set, if available.
if a := NewSeriesIDSetIterators([]SeriesIDIterator{itr0, itr1}); a != nil {
itr0.Close()
itr1.Close()
ss := NewSeriesIDSet()
ss.Merge(a[0].SeriesIDSet(), a[1].SeriesIDSet())
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(ss)
}

Expand Down Expand Up @@ -733,9 +734,10 @@ func DifferenceSeriesIDIterators(itr0, itr1 SeriesIDIterator) SeriesIDIterator {

// Create series id set, if available.
if a := NewSeriesIDSetIterators([]SeriesIDIterator{itr0, itr1}); a != nil {
ss := a[0].SeriesIDSet().AndNot(a[1].SeriesIDSet())
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(a[0].SeriesIDSet().AndNot(a[1].SeriesIDSet()))
return NewSeriesIDSetIterator(ss)
}

return &seriesIDDifferenceIterator{itrs: [2]SeriesIDIterator{itr0, itr1}}
Expand Down

0 comments on commit 0be081b

Please sign in to comment.