Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cheatcodes): restrict cheatcode usage on precompiles #4905

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion evm/src/executor/inspector/cheatcodes/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::{
abi::HEVMCalls,
executor::{
backend::DatabaseExt,
inspector::cheatcodes::{util::with_journaled_account, DealRecord},
inspector::cheatcodes::{
util::{is_potential_precompile, with_journaled_account},
DealRecord,
},
},
utils::{b160_to_h160, h160_to_b160, ru256_to_u256, u256_to_ru256},
};
Expand Down Expand Up @@ -224,6 +227,7 @@ pub fn apply<DB: DatabaseExt>(
Bytes::new()
}
HEVMCalls::Store(inner) => {
ensure!(!is_potential_precompile(inner.0), "Store cannot be used on precompile addresses (N < 10). Please use an address bigger than 10 instead");
data.journaled_state.load_account(h160_to_b160(inner.0), data.db)?;
// ensure the account is touched
data.journaled_state.touch(&h160_to_b160(inner.0));
Expand All @@ -237,6 +241,7 @@ pub fn apply<DB: DatabaseExt>(
Bytes::new()
}
HEVMCalls::Load(inner) => {
ensure!(!is_potential_precompile(inner.0), "Load cannot be used on precompile addresses (N < 10). Please use an address bigger than 10 instead");
// TODO: Does this increase gas usage?
data.journaled_state.load_account(h160_to_b160(inner.0), data.db)?;
let (val, _) = data.journaled_state.sload(
Expand All @@ -249,6 +254,7 @@ pub fn apply<DB: DatabaseExt>(
HEVMCalls::Breakpoint0(inner) => add_breakpoint(state, caller, &inner.0, true)?,
HEVMCalls::Breakpoint1(inner) => add_breakpoint(state, caller, &inner.0, inner.1)?,
HEVMCalls::Etch(inner) => {
ensure!(!is_potential_precompile(inner.0), "Etch cannot be used on precompile addresses (N < 10). Please use an address bigger than 10 instead");
let code = inner.1.clone();
trace!(address=?inner.0, code=?hex::encode(&code), "etch cheatcode");
// TODO: Does this increase gas usage?
Expand All @@ -258,6 +264,7 @@ pub fn apply<DB: DatabaseExt>(
Bytes::new()
}
HEVMCalls::Deal(inner) => {
ensure!(!is_potential_precompile(inner.0), "Deal cannot be used on precompile addresses (N < 10). Please use an address bigger than 10 instead");
Evalir marked this conversation as resolved.
Show resolved Hide resolved
let who = inner.0;
let value = inner.1;
trace!(?who, ?value, "deal cheatcode");
Expand Down
5 changes: 5 additions & 0 deletions evm/src/executor/inspector/cheatcodes/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ pub fn check_if_fixed_gas_limit<DB: DatabaseExt>(
&& call_gas_limit > 2300
}

/// Small utility function that checks if an address is a potential precompile.
pub fn is_potential_precompile(address: H160) -> bool {
address < H160::from_low_u64_be(10) && address != H160::zero()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion testdata/cheats/Deal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ contract DealTest is DSTest {
Cheats constant cheats = Cheats(HEVM_ADDRESS);

function testDeal(uint256 amount) public {
address target = address(1);
address target = address(10);
assertEq(target.balance, 0, "initial balance incorrect");

// Give half the amount
Expand Down