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 Feb 10, 2021
1 parent 262cdba commit ee2439b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ RPM packages, which has been left unchanged.
1. [20702](https://github.com/influxdata/influxdb/pull/20702): Fix loading config when `INFLUXD_CONFIG_PATH` points to a directory with `.` in its name.
1. [20678](https://github.com/influxdata/influxdb/pull/20678): Fix infinite loop in Flux parser caused by invalid array expressions.
1. [20360](https://github.com/influxdata/influxdb/pull/20360): Update API spec to document Flux dictionary features.
1. [19936](https://github.com/influxdata/influxdb/pull/19936): Close series id iterator after merging

## v2.0.3 [2020-12-14]

Expand Down
13 changes: 9 additions & 4 deletions tsdb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,11 @@ 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())
// `a` holds references to itr0/itr1 so itr0/itr1 should not be closed when `a` is still in use
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 +648,11 @@ 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())
// `a` holds references to itr0/itr1 so itr0/itr1 should not be closed when `a` is still in use
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(ss)
}

Expand Down Expand Up @@ -733,9 +736,11 @@ 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())
// `a` holds references to itr0/itr1 so itr0/itr1 should not be closed when `a` is still in use
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 ee2439b

Please sign in to comment.