Skip to content

wallet: Erase wtxOrderd wtx pointer on removeprunedfunds#13437

Merged
laanwj merged 1 commit into
bitcoin:masterfrom
maflcko:Mf1806-walletPrunedFundsSegfault
Jun 18, 2018
Merged

wallet: Erase wtxOrderd wtx pointer on removeprunedfunds#13437
laanwj merged 1 commit into
bitcoin:masterfrom
maflcko:Mf1806-walletPrunedFundsSegfault

Conversation

@maflcko

@maflcko maflcko commented Jun 11, 2018

Copy link
Copy Markdown
Member

This prevents segfaults, when reading from the freed memory.

@practicalswift

Copy link
Copy Markdown
Contributor

Oh, nice find! How was this issue found?

@maflcko

maflcko commented Jun 11, 2018

Copy link
Copy Markdown
Member Author

@practicalswift

  • Importprunedfunds
  • removeprunedfunds
  • listtransactions -> invalid read

See also (related?) issue #9729

I can reproduce on 0.15 and master, haven't checked 0.16.

Comment thread src/wallet/wallet.cpp
{
uint256 hash = wtxIn.GetHash();
CWalletTx& wtx = mapWallet.emplace(hash, wtxIn).first->second;
const auto& ins = mapWallet.emplace(hash, wtxIn);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

May be a good place to use std::tie for more self-documenting code.
http://en.cppreference.com/w/cpp/utility/tuple/tie

@jonasschnelli

Copy link
Copy Markdown
Contributor

@MarcoFalke: can you elaborate a bit more and/or add some source code documentations. A class self-referencing multimap in CWalletTx seems not ideal and I wonder if there are other ways to prevent the invalid access.

@maflcko

maflcko commented Jun 12, 2018

Copy link
Copy Markdown
Member Author

If someone finds a more elegant solution to erase from both (mapWallet and wtxOrdered) in ZapSelectTx, which is the only behaviour change in this pull, I am all for it.

@promag promag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK. Agree on @jonasschnelli, feels weird but not entirely bad. Alternative suggestion below.

Comment thread src/wallet/wallet.cpp
DBErrors nZapSelectTxRet = WalletBatch(*database,"cr+").ZapSelectTx(vHashIn, vHashOut);
for (uint256 hash : vHashOut)
mapWallet.erase(hash);
for (uint256 hash : vHashOut) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit, could change to const uint256&.

Comment thread src/wallet/wallet.h
char fFromMe;
std::string strFromAccount;
int64_t nOrderPos; //!< position in ordered transaction list
std::multimap<int64_t, std::pair<CWalletTx*, CAccountingEntry*>>::const_iterator m_it_wtxOrdered;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove, nOrderPos should be enough for fast lookup in wtxOrdered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@promag IIRC, nOrderPos is updated while the cache keeps the old value...

If I do this, I'd have to open a new pull request, since this is a different fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Either way storing a map/multimap iterator is safe (while the entry exists) and this is the most performant solution AFAIK.

Comment thread src/wallet/wallet.cpp
mapWallet.erase(hash);
for (uint256 hash : vHashOut) {
const auto& it = mapWallet.find(hash);
wtxOrdered.erase(it->second.m_it_wtxOrdered);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Something like:

for (const auto& it_wtxOrdered = wtxOrdered.lower_bound(it->second.nOrderPos; ...) {
    if (&it_wtxOrdered->second.first == &it->second) {
    }
}

@promag

promag commented Jun 14, 2018

Copy link
Copy Markdown
Contributor

Should backport?

@DrahtBot

DrahtBot commented Jun 14, 2018

Copy link
Copy Markdown
Contributor
Note to reviewers: This pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@laanwj

laanwj commented Jun 18, 2018

Copy link
Copy Markdown
Member

utACK faa18ca

If someone finds a more elegant solution to erase from both (mapWallet and wtxOrdered) in ZapSelectTx, which is the only behaviour change in this pull, I am all for it.

I kind of like this solution of storing the iterator to be able to remove it later.

@laanwj laanwj merged commit faa18ca into bitcoin:master Jun 18, 2018
@laanwj laanwj added this to the 0.16.2 milestone Jun 18, 2018
laanwj added a commit that referenced this pull request Jun 18, 2018
faa18ca wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke)

Pull request description:

  This prevents segfaults, when reading from the freed memory.

Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123
@jnewbery

Copy link
Copy Markdown
Contributor

tested ACK faa18ca

@maflcko maflcko deleted the Mf1806-walletPrunedFundsSegfault branch July 11, 2018 22:41
maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Jul 13, 2018
@fanquake

Copy link
Copy Markdown
Member

Should be backported in #13644.

SmarterHomes pushed a commit to chaincoin/chaincoin that referenced this pull request Jan 11, 2019
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 6, 2019
Summary:
faa18ca wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke)

Pull request description:

  This prevents segfaults, when reading from the freed memory.

Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123

Backport of Core PR13437
bitcoin/bitcoin#13437

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D4540
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 16, 2020
…nedfunds

faa18ca wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke)

Pull request description:

  This prevents segfaults, when reading from the freed memory.

Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123
jonspock pushed a commit to jonspock/devault that referenced this pull request Sep 29, 2020
Summary:
faa18ca046e9043b2cf68cb1bd17cc8c60fe26d9 wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke)

Pull request description:

  This prevents segfaults, when reading from the freed memory.

Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123

Backport of Core PR13437
bitcoin/bitcoin#13437

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D4540
jonspock pushed a commit to jonspock/devault that referenced this pull request Sep 29, 2020
Summary:
faa18ca046e9043b2cf68cb1bd17cc8c60fe26d9 wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke)

Pull request description:

  This prevents segfaults, when reading from the freed memory.

Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123

Backport of Core PR13437
bitcoin/bitcoin#13437

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D4540
jonspock pushed a commit to devaultcrypto/devault-legacy that referenced this pull request Oct 10, 2020
Summary:
faa18ca046e9043b2cf68cb1bd17cc8c60fe26d9 wallet: Erase wtxOrderd wtx pointer on removeprunedfunds (MarcoFalke)

Pull request description:

  This prevents segfaults, when reading from the freed memory.

Tree-SHA512: 04f8190dea7901cf1cc298d5db98c83b02858f27114c5ef4da738accd176d6647d6b81f3dc39f3d5912b1a981cf0599370fd391c4154ffbde97afc1fac389123

Backport of Core PR13437
bitcoin/bitcoin#13437

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D4540
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants