Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit c570e3b

Browse files
committed
PrecompilesVM returns EVMC_REJECTD when address out of precompile range
1 parent ba28bf7 commit c570e3b

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

libaleth-precompiles/PrecompilesVM.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,25 @@ std::pair<bool, bytes> blake2Compression(bytesConstRef _in)
255255
evmc_result execute(evmc_vm*, const evmc_host_interface*, evmc_host_context*,
256256
enum evmc_revision _rev, const evmc_message* _msg, const uint8_t*, size_t) noexcept
257257
{
258-
static std::array<std::pair<Pricer, Executor>, 9> const precompiles = {
259-
{{ecrecoverPrice, ecrecover}, {sha256Price, sha256}, {ripemd160Price, ripemd160},
260-
{identityPrice, identity}, {modexpPrice, modexp},
261-
{alt_bn128_G1_addPrice, alt_bn128_G1_add}, {alt_bn128_G1_mulPrice, alt_bn128_G1_mul},
262-
{alt_bn128_pairing_productPrice, alt_bn128_pairing_product},
263-
{blake2CompressionPrice, blake2Compression}}};
264-
265-
// TODO check range
266-
267-
auto const precompile = precompiles[_msg->destination.bytes[sizeof(_msg->destination) - 1] - 1];
258+
static constexpr std::pair<Pricer, Executor> c_precompiles[] = {{ecrecoverPrice, ecrecover},
259+
{sha256Price, sha256}, {ripemd160Price, ripemd160}, {identityPrice, identity},
260+
{modexpPrice, modexp}, {alt_bn128_G1_addPrice, alt_bn128_G1_add},
261+
{alt_bn128_G1_mulPrice, alt_bn128_G1_mul},
262+
{alt_bn128_pairing_productPrice, alt_bn128_pairing_product},
263+
{blake2CompressionPrice, blake2Compression}};
264+
static constexpr size_t c_precompilesSize = sizeof(c_precompiles) / sizeof(c_precompiles[0]);
265+
static_assert(c_precompilesSize < 0xFF, "Assuming no more than 256 precompiles");
266+
static constexpr evmc::address c_maxPrecompileAddress =
267+
evmc_address{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, c_precompilesSize}};
268+
269+
// check that address is within the range of defined precompiles
270+
auto const destination = evmc::address{_msg->destination};
271+
if (destination == evmc::address{} || c_maxPrecompileAddress < destination)
272+
return evmc::make_result(EVMC_REJECTED, 0, nullptr, 0);
273+
274+
// convert address to array index
275+
auto const precompileAddressLSB = _msg->destination.bytes[sizeof(_msg->destination) - 1];
276+
auto const precompile = c_precompiles[precompileAddressLSB - 1];
268277

269278
bytesConstRef input{_msg->input_data, _msg->input_size};
270279

0 commit comments

Comments
 (0)