No longer ever reuse keypool indexes #10795

Merged
merged 1 commit into from Jul 18, 2017

Conversation

Projects
None yet
6 participants
Contributor

TheBlueMatt commented Jul 11, 2017

This fixes an issue where you could reserve a keypool entry, then
top up the keypool, writing out a new key at the given index, then
return they key from the pool. This isnt likely to cause issues,
but given there is no reason to ever re-use keypool indexes
(they're 64 bits...), best to avoid it alltogether.

Builds on #10235, should probably get a 15 tag.

laanwj added this to the 0.15.0 milestone Jul 11, 2017

laanwj added the Wallet label Jul 11, 2017

fanquake deleted a comment from MIGUELWAXX Jul 11, 2017

Contributor

morcos commented Jul 12, 2017

utACK 1c01ea6

bugfix needed for 0.15

src/wallet/wallet.cpp
- nEnd = std::max(nEnd, *(setExternalKeyPool.rbegin()) + 1);
- }
+ int64_t index = ++m_max_keypool_index;
+ assert(index >= 0); // How in the hell did you use so many keys?
@sipa

sipa Jul 13, 2017

Owner

Signed overflow is undefined behaviour, so the compiler is allowed to optimize this assert out (it can assume overflow never happens).

@morcos

morcos Jul 13, 2017

Contributor

heh, i almost commented that it would be clearer to check against max

@TheBlueMatt

TheBlueMatt Jul 14, 2017

Contributor

Heh, but in practice it doesnt, plus its just an assert, so whatever :).

@gmaxwell

gmaxwell Jul 17, 2017

Member

Code like this makes me go "wtf were the authors of this smoking": asserts like this are usually optimized out, so the code looks like it's saying that the author thought the signed overflow was kosher and that they thought it could happen here.

@laanwj

laanwj Jul 17, 2017 edited

Owner

Would be better (non-UB, but also more clear to read) to check against std::numeric_limits<int64_t>::max() before the increase.

Owner

sipa commented Jul 16, 2017

Needs rebase.

Contributor

TheBlueMatt commented Jul 16, 2017

Rebased.

Owner

laanwj commented Jul 17, 2017

LGTM, what a foresight Satoshi had to use 64 bit numbers for keypool indexes in walletdb.

Contributor

morcos commented Jul 17, 2017

re-utACK 44d0996

Member

gmaxwell commented Jul 17, 2017

@TheBlueMatt plz2rebase

@TheBlueMatt TheBlueMatt No longer ever reuse keypool indexes
This fixes an issue where you could reserve a keypool entry, then
top up the keypool, writing out a new key at the given index, then
return they key from the pool. This isnt likely to cause issues,
but given there is no reason to ever re-use keypool indexes
(they're 64 bits...), best to avoid it alltogether.
1fc8c3d
Contributor

TheBlueMatt commented Jul 17, 2017

Rebased.

Contributor

morcos commented Jul 17, 2017

re-re-utACK 1fc8c3d
(one more and I think git will do it for me)

@laanwj laanwj merged commit 1fc8c3d into bitcoin:master Jul 18, 2017

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@laanwj laanwj added a commit that referenced this pull request Jul 18, 2017

@laanwj laanwj Merge #10795: No longer ever reuse keypool indexes
1fc8c3d No longer ever reuse keypool indexes (Matt Corallo)

Pull request description:

  This fixes an issue where you could reserve a keypool entry, then
  top up the keypool, writing out a new key at the given index, then
  return they key from the pool. This isnt likely to cause issues,
  but given there is no reason to ever re-use keypool indexes
  (they're 64 bits...), best to avoid it alltogether.

  Builds on #10235, should probably get a 15 tag.

Tree-SHA512: c13a18a90f1076fb74307f2d64e9d80149811524c6bda259698ff2c65adaf8c6c3f2a3a07a5f4bf03251bc942ba8f5fd33a4427aa4256748c40b062991682caf
7b6e8bc
Member

jonasschnelli commented Jul 18, 2017

post merge ACK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment