Skip to content

Commit c7978be

Browse files
committed
Merge #12101: Clamp walletpassphrase timeout to 2^30 seconds and check its bounds
134cdc7 Test walletpassphrase timeout bounds and clamping (Andrew Chow) 0b63e3c Clamp walletpassphrase timeout to 2^(30) seconds and check its bounds (Andrew Chow) Pull request description: Fixes #12100 Makes the timeout be clamped to 2^30 seconds to avoid the issue with sign flipping with large timeout values and thus relocking the wallet instantly. Unlocking for at most ~34 years should be sufficient. Also checks that the timeout is not negative to avoid instant relocks. Tree-SHA512: 426922f08c54e323d259e25dcdbebc2cd560708a65111ce6051493a7e7c61e79d9da1ea4026cc0d68807d728f5d7c0d7c58168c6ef4167b94cf6c2877af88794
2 parents adce1de + 134cdc7 commit c7978be

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,8 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
22832283
"This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
22842284
"\nArguments:\n"
22852285
"1. \"passphrase\" (string, required) The wallet passphrase\n"
2286-
"2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2286+
"2. timeout (numeric, required) The time to keep the decryption key in seconds. Limited to at most 1073741824 (2^30) seconds.\n"
2287+
" Any value greater than 1073741824 seconds will be set to 1073741824 seconds.\n"
22872288
"\nNote:\n"
22882289
"Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
22892290
"time that overrides the old one.\n"
@@ -2312,6 +2313,17 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
23122313
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
23132314
strWalletPass = request.params[0].get_str().c_str();
23142315

2316+
// Get the timeout
2317+
int64_t nSleepTime = request.params[1].get_int64();
2318+
// Timeout cannot be negative, otherwise it will relock immediately
2319+
if (nSleepTime < 0) {
2320+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
2321+
}
2322+
// Clamp timeout to 2^30 seconds
2323+
if (nSleepTime > (int64_t)1 << 30) {
2324+
nSleepTime = (int64_t)1 << 30;
2325+
}
2326+
23152327
if (strWalletPass.length() > 0)
23162328
{
23172329
if (!pwallet->Unlock(strWalletPass)) {
@@ -2325,7 +2337,6 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
23252337

23262338
pwallet->TopUpKeyPool();
23272339

2328-
int64_t nSleepTime = request.params[1].get_int64();
23292340
pwallet->nRelockTime = GetTime() + nSleepTime;
23302341
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), boost::bind(LockWallet, pwallet), nSleepTime);
23312342

test/functional/wallet-encryption.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from test_framework.util import (
1111
assert_equal,
1212
assert_raises_rpc_error,
13+
assert_greater_than,
14+
assert_greater_than_or_equal,
1315
)
1416

1517
class WalletEncryptionTest(BitcoinTestFramework):
@@ -56,6 +58,23 @@ def run_test(self):
5658
assert_raises_rpc_error(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase, 10)
5759
self.nodes[0].walletpassphrase(passphrase2, 10)
5860
assert_equal(privkey, self.nodes[0].dumpprivkey(address))
61+
self.nodes[0].walletlock()
62+
63+
# Test timeout bounds
64+
assert_raises_rpc_error(-8, "Timeout cannot be negative.", self.nodes[0].walletpassphrase, passphrase2, -10)
65+
# Check the timeout
66+
# Check a time less than the limit
67+
expected_time = int(time.time()) + (1 << 30) - 600
68+
self.nodes[0].walletpassphrase(passphrase2, (1 << 30) - 600)
69+
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
70+
assert_greater_than_or_equal(actual_time, expected_time)
71+
assert_greater_than(expected_time + 5, actual_time) # 5 second buffer
72+
# Check a time greater than the limit
73+
expected_time = int(time.time()) + (1 << 30) - 1
74+
self.nodes[0].walletpassphrase(passphrase2, (1 << 33))
75+
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
76+
assert_greater_than_or_equal(actual_time, expected_time)
77+
assert_greater_than(expected_time + 5, actual_time) # 5 second buffer
5978

6079
if __name__ == '__main__':
6180
WalletEncryptionTest().main()

0 commit comments

Comments
 (0)