Skip to content

libroach: ensure correct lifetime for resume_key on reverse iteration#32492

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
nvb:nvanbenschoten/fixRocksDBCorruption
Nov 20, 2018
Merged

libroach: ensure correct lifetime for resume_key on reverse iteration#32492
craig[bot] merged 1 commit intocockroachdb:masterfrom
nvb:nvanbenschoten/fixRocksDBCorruption

Conversation

@nvb
Copy link
Copy Markdown
Contributor

@nvb nvb commented Nov 19, 2018

Fixes #32149.

Before this change, it was possible for DBScanResults.resume_key to
point into memory owned by mvccScanner, which went out of scope after
MVCCScan returned. This allowed for memory corruption when returning
the key to Go.

This change fixes this corruption by copying the memory to the DBIterator
before returning, which should have a lifetime which exceeds that of the
DBScanResults.

Release note: None

Fixes cockroachdb#32149.

Before this change, it was possible for `DBScanResults.resume_key` to
point into memory owned by `mvccScanner`, which went out of scope after
`MVCCScan` returned. This allowed for memory corruption when returning
the key to Go.

This change fixes this corruption by copying the memory to the `DBIterator`
before returning, which should have a lifetime which exceeds that of the
`DBScanResults`.

Release note: None
@nvb nvb requested review from a team, benesch and petermattis November 19, 2018 23:34
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Nov 19, 2018

Found with lots of help from @benesch!

Copy link
Copy Markdown
Collaborator

@petermattis petermattis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained


c-deps/libroach/mvcc.h, line 141 at r1 (raw file):

        // instead of iter_rep_'s underlying storage if iterating in reverse (see
        // iterPeekPrev), so copy the key onto the DBIterator struct to ensure it
        // has a lifetime that outlives the DBScanResults.

Wow! Nice find.

@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Nov 20, 2018

bors r+

craig bot pushed a commit that referenced this pull request Nov 20, 2018
32492: libroach: ensure correct lifetime for resume_key on reverse iteration r=nvanbenschoten a=nvanbenschoten

Fixes #32149.

Before this change, it was possible for `DBScanResults.resume_key` to
point into memory owned by `mvccScanner`, which went out of scope after
`MVCCScan` returned. This allowed for memory corruption when returning
the key to Go.

This change fixes this corruption by copying the memory to the `DBIterator`
before returning, which should have a lifetime which exceeds that of the
`DBScanResults`.

Release note: None

Co-authored-by: Nathan VanBenschoten <nvanbenschoten@gmail.com>
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 20, 2018

Build succeeded

@craig craig bot merged commit 049fc0d into cockroachdb:master Nov 20, 2018
@nvb nvb deleted the nvanbenschoten/fixRocksDBCorruption branch November 27, 2018 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage: inverted range in intervalSkl.AddRange

3 participants