Skip to content

Commit 0b63e3c

Browse files
committed
Clamp walletpassphrase timeout to 2^(30) seconds and check its bounds
Clamps the timeout of walletpassphrase to 2^(30) seconds, which is ~34 years. Any number greater than that will be forced to be 2^(30). This avoids the sign flipping problem with large values which can result in a negative time used. Also perform bounds checks to ensure that the timeout is positive to avoid immediate relocking of the wallet.
1 parent 5691028 commit 0b63e3c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,8 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
22692269
"This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
22702270
"\nArguments:\n"
22712271
"1. \"passphrase\" (string, required) The wallet passphrase\n"
2272-
"2. timeout (numeric, required) The time to keep the decryption key in seconds.\n"
2272+
"2. timeout (numeric, required) The time to keep the decryption key in seconds. Limited to at most 1073741824 (2^30) seconds.\n"
2273+
" Any value greater than 1073741824 seconds will be set to 1073741824 seconds.\n"
22732274
"\nNote:\n"
22742275
"Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
22752276
"time that overrides the old one.\n"
@@ -2298,6 +2299,17 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
22982299
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
22992300
strWalletPass = request.params[0].get_str().c_str();
23002301

2302+
// Get the timeout
2303+
int64_t nSleepTime = request.params[1].get_int64();
2304+
// Timeout cannot be negative, otherwise it will relock immediately
2305+
if (nSleepTime < 0) {
2306+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
2307+
}
2308+
// Clamp timeout to 2^30 seconds
2309+
if (nSleepTime > (int64_t)1 << 30) {
2310+
nSleepTime = (int64_t)1 << 30;
2311+
}
2312+
23012313
if (strWalletPass.length() > 0)
23022314
{
23032315
if (!pwallet->Unlock(strWalletPass)) {
@@ -2311,7 +2323,6 @@ UniValue walletpassphrase(const JSONRPCRequest& request)
23112323

23122324
pwallet->TopUpKeyPool();
23132325

2314-
int64_t nSleepTime = request.params[1].get_int64();
23152326
pwallet->nRelockTime = GetTime() + nSleepTime;
23162327
RPCRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), boost::bind(LockWallet, pwallet), nSleepTime);
23172328

0 commit comments

Comments
 (0)