Skip to content

Introduce SafeDbt to handle Dbt with free or memory_cleanse raii-style#14268

Merged
laanwj merged 3 commits into
bitcoin:masterfrom
Empact:malloced-dbt
Jan 16, 2019
Merged

Introduce SafeDbt to handle Dbt with free or memory_cleanse raii-style#14268
laanwj merged 3 commits into
bitcoin:masterfrom
Empact:malloced-dbt

Conversation

@Empact

@Empact Empact commented Sep 19, 2018

Copy link
Copy Markdown
Contributor

This provides additional exception-safety and case handling for the proper
freeing of the associated buffers.

Comment thread src/wallet/db.h Outdated
Comment thread src/wallet/db.h Outdated

@l2a5b1 l2a5b1 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.

utACK 985892e

Comment thread src/wallet/db.h Outdated
@DrahtBot

Copy link
Copy Markdown
Contributor
Coverage Change (pull 14268) Reference (master)
Lines +0.0099 % 87.0361 %
Functions +0.0179 % 84.1130 %
Branches -0.0022 % 51.5451 %

@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.

utACK 985892e.

nit, first commit kind of unrelated — could mention the dead code cleanup in the PR description.

Comment thread src/wallet/db.h Outdated
@achow101

achow101 commented Oct 9, 2018

Copy link
Copy Markdown
Member

utACK 985892e5aa75b76f71a354c8d835d7729048cccc

@meshcollider

meshcollider commented Nov 10, 2018

Copy link
Copy Markdown
Contributor

utACK 985892e

Style-nit: the developer notes say "No indentation for public/protected/private or for namespace", so you might want to change the MallocedDbt class to fit that.

First commit is unrelated as promag said, but a nice cleanup, the last use of setRange was in ListAccountCreditDebit which was removed when accounts were.

@Empact Empact changed the title Introduce MallocedDbt to handle DB_DBT_MALLOC raii-style Introduce SafeDbt to handle Dbt with free or memory_cleanse raii-style Nov 12, 2018
@Empact Empact changed the title Introduce SafeDbt to handle Dbt with free or memory_cleanse raii-style Introduce SafeDbt to handle Dbt with free or memory_cleanse raii-style Nov 12, 2018
@Empact

Empact commented Nov 12, 2018

Copy link
Copy Markdown
Contributor Author

Ok just pushed some significant changes:

  • Now called SafeDbt, and applied in all cases where memory_cleanse was used with Dbt
  • No longer in an anonymous namespace as that is a violation of DCL59-CPP, instead nest it inside BerkeleyBatch, as all uses are therein.
  • Implementation is now in db.cpp

This provides additional exception-safety and case handling for the proper
freeing of the associated buffers.
@Empact

Empact commented Nov 13, 2018

Copy link
Copy Markdown
Contributor Author

Appveyor failure looks unrelated.

@ken2812221

Copy link
Copy Markdown
Contributor

utACK 1a9f9f7

Comment thread src/wallet/db.cpp
return m_dbt.get_size();
}

BerkeleyBatch::SafeDbt::operator Dbt*()

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.

const? :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's returning a mutable pointer to the internal structure, I don't think it'd make sense to make the method const (unless you mean it should return a const* pointer too, but that's likely not good enough for the BDB functions)

@laanwj

laanwj commented Nov 23, 2018

Copy link
Copy Markdown
Member

utACK 1a9f9f7

Comment thread src/wallet/db.cpp
{
if (m_dbt.get_data() != nullptr) {
// Clear memory, e.g. in case it was a private key
memory_cleanse(m_dbt.get_data(), m_dbt.get_size());

@laanwj laanwj Nov 23, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This changes behavior: in the case of Write it was also cleansing the dtabase key, now it no longer is.
Not sure if this is necessary, I don't think the database keys ever contain sensitive information, but it should be noted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Empact

Empact commented Nov 25, 2018

Copy link
Copy Markdown
Contributor Author

Added another commit:

  • Make SafeDbt DB_DBT_MALLOC on default initialization
  • Ran clang-format

Separate for easier review, can squash.

If we're constructing the SafeDbt without provided data, it is always malloced,
so that is the case we expose.

Also run clang-format.
@Empact

Empact commented Nov 25, 2018

Copy link
Copy Markdown
Contributor Author

Appveyor failure looks unrelated: "ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine"

@laanwj laanwj merged commit 4a86a0a into bitcoin:master Jan 16, 2019
laanwj added a commit that referenced this pull request Jan 16, 2019
…anse raii-style

4a86a0a Make SafeDbt DB_DBT_MALLOC on default initialization (Ben Woosley)
1a9f9f7 Introduce SafeDbt to handle DB_DBT_MALLOC raii-style (Ben Woosley)
951a44e Drop unused setRange arg to BerkeleyBatch::ReadAtCursor (Ben Woosley)

Pull request description:

  This provides additional exception-safety and case handling for the proper
  freeing of the associated buffers.

Tree-SHA512: a038d728290cdb3905e7d881608052a6675b6425729ceaf7cfe69a6e91c2ee293cdb01e4b695a20963459ffdd9d4a1f9a08b3c07b1b5ba1aa8590a8149f686db
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 2, 2020
Summary:
bitcoin/bitcoin@951a44e

---

Partial backport of Core [[bitcoin/bitcoin#14268 | PR14268]]

Test Plan:
  ninja check check-functional

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7321
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 2, 2020
Summary:
This provides additional exception-safety and case handling for the proper
freeing of the associated buffers.

bitcoin/bitcoin@1a9f9f7

---

Depends on D7321

Partial backport of Core [[bitcoin/bitcoin#14268 | PR14268]]

Test Plan:
  ninja check check-functional

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7322
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 2, 2020
Summary:
If we're constructing the SafeDbt without provided data, it is always malloced,
so that is the case we expose.

Also run clang-format.

bitcoin/bitcoin@4a86a0a

---

Depends on D7322

Concludes backport of Core [[bitcoin/bitcoin#14268 | PR14268]]

Test Plan:
  ninja check check-functional

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7323
dzutto pushed a commit to dzutto/dash that referenced this pull request Oct 1, 2021
…ory_cleanse raii-style

4a86a0a Make SafeDbt DB_DBT_MALLOC on default initialization (Ben Woosley)
1a9f9f7 Introduce SafeDbt to handle DB_DBT_MALLOC raii-style (Ben Woosley)
951a44e Drop unused setRange arg to BerkeleyBatch::ReadAtCursor (Ben Woosley)

Pull request description:

  This provides additional exception-safety and case handling for the proper
  freeing of the associated buffers.

Tree-SHA512: a038d728290cdb3905e7d881608052a6675b6425729ceaf7cfe69a6e91c2ee293cdb01e4b695a20963459ffdd9d4a1f9a08b3c07b1b5ba1aa8590a8149f686db
UdjinM6 added a commit to dashpay/dash that referenced this pull request Oct 1, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants