Skip to content

Commit

Permalink
Forbid XVM re-entrancy.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Aug 9, 2023
1 parent da91bca commit b028df4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion chain-extensions/types/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl From<CallError> for XvmExecutionResult {
InvalidTarget => 3,
InputTooLarge => 4,
ExceedMaxWeightLimit => 5,
ExecutionFailed(_) => 6,
Reentrancy => 6,
ExecutionFailed(_) => 7,
};
Self::Err(error_code)
}
Expand Down
2 changes: 2 additions & 0 deletions pallets/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
environmental = { workspace = true }
log = { workspace = true }
serde = { workspace = true, optional = true }

Expand Down Expand Up @@ -42,6 +43,7 @@ sp-io = { workspace = true }
[features]
default = ["std"]
std = [
"environmental/std",
"log/std",
"parity-scale-codec/std",
"frame-support/std",
Expand Down
20 changes: 18 additions & 2 deletions pallets/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub use pallet::*;

pub type WeightInfoOf<T> = <T as Config>::WeightInfo;

environmental::thread_local_impl!(static IN_XVM: environmental::RefCell<bool> = environmental::RefCell::new(false));

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -152,7 +154,15 @@ where
}
);

match vm_id {
// Set `IN_XVM` to true & check reentrancy.
if IN_XVM.with(|in_evm| in_evm.replace(true)) {
CallErrorWithWeight {
error: CallError::Reentrancy,
used_weight: overheads,
};

Check failure on line 162 in pallets/xvm/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

statement with no effect

error: statement with no effect --> pallets/xvm/src/lib.rs:159:13 | 159 | / CallErrorWithWeight { 160 | | error: CallError::Reentrancy, 161 | | used_weight: overheads, 162 | | }; | |______________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#no_effect = note: `-D clippy::no-effect` implied by `-D clippy::complexity`
}

let res = match vm_id {
VmId::Evm => Pallet::<T>::evm_call(
context,
source,
Expand All @@ -171,7 +181,13 @@ where
overheads,
skip_execution,
),
}
};

// Set `IN_XVM` to false.
// We should make sure that this line is executed whatever the execution path.
let _ = IN_XVM.with(|in_evm| in_evm.take());

res
}

fn evm_call(
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/xvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum CallError {
InputTooLarge,
/// Exceed max weight limit.
ExceedMaxWeightLimit,
/// Reentrancy is not allowed.
Reentrancy,
/// The call failed on EVM or WASM execution.
ExecutionFailed(Vec<u8>),
}
Expand Down

0 comments on commit b028df4

Please sign in to comment.