Commit 6f732ff
committed
Merge bitcoin/bitcoin#28774: wallet: avoid returning a reference to vMasterKey after releasing the mutex that guards it
32a9f13 wallet: avoid returning a reference to vMasterKey after releasing the mutex that guards it (Vasil Dimov)
Pull request description:
`CWallet::GetEncryptionKey()` would return a reference to the internal
`CWallet::vMasterKey`, guarded by `CWallet::cs_wallet`, which is unsafe.
Returning a copy would be a shorter solution, but could have security
implications of the master key remaining somewhere in the memory even
after `CWallet::Lock()` (the current calls to
`CWallet::GetEncryptionKey()` are safe, but that is not future proof).
So, instead of `EncryptSecret(m_storage.GetEncryptionKey(), ...)`
change the `GetEncryptionKey()` method to provide the encryption
key to a given callback:
`m_storage.WithEncryptionKey([](const CKeyingMaterial& k) { EncryptSecret(k, ...); })`
This silences the following (clang 18):
```
wallet/wallet.cpp:3520:12: error: returning variable 'vMasterKey' by reference requires holding mutex 'cs_wallet' [-Werror,-Wthread-safety-reference-return]
3520 | return vMasterKey;
| ^
```
---
_Previously this PR modified both ArgsManager and wallet code. But the ArgsManager commit bitcoin/bitcoin@856c887 was merged in bitcoin/bitcoin#29040 so now this only affects wallet code. The previous PR description was:_
Avoid this unsafe pattern from `ArgsManager` and `CWallet`:
```cpp
class A
{
Mutex mutex;
Foo member GUARDED_BY(mutex);
const Foo& Get()
{
LOCK(mutex);
return member;
} // callers of `Get()` will have access to `member` without owning the mutex.
```
ACKs for top commit:
achow101:
ACK 32a9f13
ryanofsky:
Code review ACK 32a9f13. This seems like a potentially real race condition, and the fix here is pretty simple.
furszy:
ACK 32a9f13
Tree-SHA512: 133da84691642afc1a73cf14ad004a7266cb4be1a6a3ec634d131dca5dbcdef52522c1d5eb04f5b6c4e06e1fc3e6ac57315f8fe1e207b464ca025c2b4edefdc1File tree
4 files changed
+20
-8
lines changed- src/wallet
4 files changed
+20
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
811 | 811 | | |
812 | 812 | | |
813 | 813 | | |
814 | | - | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
815 | 817 | | |
816 | 818 | | |
817 | 819 | | |
| |||
997 | 999 | | |
998 | 1000 | | |
999 | 1001 | | |
1000 | | - | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
1001 | 1005 | | |
1002 | 1006 | | |
1003 | 1007 | | |
| |||
2128 | 2132 | | |
2129 | 2133 | | |
2130 | 2134 | | |
2131 | | - | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
2132 | 2138 | | |
2133 | 2139 | | |
2134 | 2140 | | |
| |||
2262 | 2268 | | |
2263 | 2269 | | |
2264 | 2270 | | |
2265 | | - | |
| 2271 | + | |
| 2272 | + | |
| 2273 | + | |
2266 | 2274 | | |
2267 | 2275 | | |
2268 | 2276 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | | - | |
| 50 | + | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3513 | 3513 | | |
3514 | 3514 | | |
3515 | 3515 | | |
3516 | | - | |
| 3516 | + | |
3517 | 3517 | | |
3518 | | - | |
| 3518 | + | |
| 3519 | + | |
3519 | 3520 | | |
3520 | 3521 | | |
3521 | 3522 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
962 | 962 | | |
963 | 963 | | |
964 | 964 | | |
965 | | - | |
| 965 | + | |
| 966 | + | |
966 | 967 | | |
967 | 968 | | |
968 | 969 | | |
| |||
0 commit comments