Skip to content

Commit 07720b1

Browse files
committed
Merge bitcoin/bitcoin#29869: rpc, bugfix: Enforce maximum value for setmocktime
c2e0489 [rpc, bugfix] Enforce maximum value for setmocktime (dergoegge) Pull request description: The maximum value for our mocktime must be representable in nanoseconds, otherwise we end up with negative values returned from `NodeClock::now()`. Found through fuzzing: ``` $ echo "c2V0bW9ja3RpbWVcZTptYf9w/3NldG3///////////////9p////ZP///ymL//////89////Nv9L////////LXkBAABpAA==" | base64 --decode > rpc-8cab9148ab4418ebd1923c213e9d3fe9c9b49b39.crash $ FUZZ=rpc ./src/test/fuzz/fuzz rpc-8cab9148ab4418ebd1923c213e9d3fe9c9b49b39.crash fuzz_libfuzzer: util/time.cpp:28: static NodeClock::time_point NodeClock::now(): Assertion `ret > 0s' failed. ``` ACKs for top commit: maflcko: re-ACK c2e0489 brunoerg: crACK c2e0489 glozow: ACK c2e0489 Tree-SHA512: d7e237ca37bedd74a6b085fb6e726a142705371044c77488f593f35afe70aeca756fdba86920294b1d322c7a9b2cde9ce4e1b7d410a6ccc1fd7c6f3a6e77200a
2 parents df609a3 + c2e0489 commit 07720b1

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/rpc/node.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <univalue.h>
2727
#include <util/any.h>
2828
#include <util/check.h>
29+
#include <util/time.h>
2930

3031
#include <stdint.h>
3132
#ifdef HAVE_MALLOC_INFO
@@ -58,9 +59,11 @@ static RPCHelpMan setmocktime()
5859
LOCK(cs_main);
5960

6061
const int64_t time{request.params[0].getInt<int64_t>()};
61-
if (time < 0) {
62-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime cannot be negative: %s.", time));
62+
constexpr int64_t max_time{Ticks<std::chrono::seconds>(std::chrono::nanoseconds::max())};
63+
if (time < 0 || time > max_time) {
64+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime must be in the range [0, %s], not %s.", max_time, time));
6365
}
66+
6467
SetMockTime(time);
6568
const NodeContext& node_context{EnsureAnyNodeContext(request.context)};
6669
for (const auto& chain_client : node_context.chain_clients) {

test/functional/rpc_uptime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def run_test(self):
2323
self._test_uptime()
2424

2525
def _test_negative_time(self):
26-
assert_raises_rpc_error(-8, "Mocktime cannot be negative: -1.", self.nodes[0].setmocktime, -1)
26+
assert_raises_rpc_error(-8, "Mocktime must be in the range [0, 9223372036], not -1.", self.nodes[0].setmocktime, -1)
2727

2828
def _test_uptime(self):
2929
wait_time = 10

0 commit comments

Comments
 (0)