Skip to content

Prevent default/invalid CKey objects from allocating secure memory - #28500

Merged
fanquake merged 2 commits into
bitcoin:masterfrom
sipa:202309_no_null_key_alloc
Oct 2, 2023
Merged

fanquake merged 2 commits into
bitcoin:masterfrom
sipa:202309_no_null_key_alloc

Conversation

@sipa

@sipa sipa commented Sep 18, 2023

Copy link
Copy Markdown
Member

Bitcoin Core has secure_allocator, which allocates inside special "secure" (non-swappable) memory pages, which may be limited in availability. Currently, every CKey object uses 32 such secure bytes, even when the CKey object contains the (invalid) value zero.

Change this to not use memory when the CKey is invalid. This is particularly relevant for BIP324Cipher which briefly holds a CKey, but after receiving the remote's public key and initializing the encryption ciphers, the key is wiped. In case secure memory usage is in high demand, it'd be silly to waste it on P2P encryption keys instead of wallet keys.

@DrahtBot

DrahtBot commented Sep 18, 2023 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK john-moffett, ajtowns

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

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

Approach ACK f2156d613e6e8cc6a6c3d860b74d912515dc782a

Comment thread src/key.h Resolved
Comment thread src/key.h Resolved
Comment thread src/key.cpp Resolved
Comment thread src/key.cpp Resolved
@sipa
sipa force-pushed the 202309_no_null_key_alloc branch 2 times, most recently from 9752422 to cd23401 Compare September 22, 2023 22:58
Comment thread src/key.h Resolved
Comment thread src/key.h Resolved
@sipa
sipa force-pushed the 202309_no_null_key_alloc branch from cd23401 to 94d58fe Compare September 24, 2023 11:22
@ajtowns

ajtowns commented Sep 25, 2023

Copy link
Copy Markdown
Contributor

ACK 94d58fe942fd86fc8d3b11e3f75e4a4f82c86e38 lgtm

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

Approach ACK. One minor suggestion and one nit.

Comment thread src/support/allocators/secure.h Resolved
Comment thread src/key.h Resolved
ajtowns and others added 2 commits September 27, 2023 15:05
Co-authored-by: Pieter Wuille <[email protected]>
Instead of storing the key material as an std::vector (with secure allocator),
use a secure_unique_ptr to a 32-byte array, and use nullptr for invalid keys.
This means a smaller CKey type, and no secure/dynamic memory usage for invalid
keys.
@sipa
sipa force-pushed the 202309_no_null_key_alloc branch from 94d58fe to 6ef405d Compare September 27, 2023 19:05

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

ACK 6ef405d

@DrahtBot
DrahtBot requested a review from ajtowns September 28, 2023 13:31
@ajtowns

ajtowns commented Sep 28, 2023

Copy link
Copy Markdown
Contributor

ACK 6ef405d

@DrahtBot
DrahtBot removed the request for review from ajtowns September 28, 2023 14:50
@fanquake
fanquake merged commit 0f9307c into bitcoin:master Oct 2, 2023
Frank-GER pushed a commit to syscoin/syscoin that referenced this pull request Oct 5, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Oct 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants