-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feeat] td detection and updated usecase
- Loading branch information
1 parent
aea774d
commit f301120
Showing
9 changed files
with
78 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.69.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,85 @@ | ||
use move_vm_types::loaded_data::runtime_types::Type; | ||
use vm::{errors::{PartialVMResult, VMResult}, file_format::Bytecode}; | ||
use crate::{ | ||
plugin::{Plugin, PluginContext, collections::verification::specification::*}, | ||
plugin::{collections::verification::specification::*, Plugin, PluginContext}, | ||
runtime::{ | ||
context::TypeContext, | ||
loader::{Loader, Function}}, types::values::{SymValue}, types::values::SymIntegerValue, | ||
loader::{Function, Loader}, | ||
}, | ||
types::values::SymIntegerValue, | ||
types::values::SymValue, | ||
}; | ||
use move_core_types::identifier::Identifier; | ||
use crate::types::memory::SymMemory; | ||
|
||
pub struct TDDetectionPlugin(); | ||
use move_vm_types::loaded_data::runtime_types::Type; | ||
use vm::{ | ||
errors::{PartialVMResult, VMResult}, | ||
}; | ||
use z3::{ | ||
ast::{exists_const, forall_const, Ast, Bool, Datatype, Dynamic}, | ||
Context, Goal, SatResult, Solver, Tactic, | ||
}; | ||
pub struct TDDetectionPlugin { | ||
now_microseconds_used: bool, | ||
timestamp_symbol: Option<u64>, | ||
} | ||
|
||
impl TDDetectionPlugin { | ||
pub fn new() -> Self { | ||
Self {} | ||
Self { | ||
now_microseconds_used: false, | ||
timestamp_symbol: None, | ||
} | ||
} | ||
} | ||
|
||
impl Plugin for TDDetectionPlugin { | ||
/** Get the initial change set and insert into SymMemory one by one */ | ||
fn on_before_call<'ctx>( | ||
&self, | ||
plugin_ctx: &mut dyn PluginContext<'ctx>, | ||
func: &Function, | ||
_ty_args: Vec<Type>, | ||
) -> PartialVMResult<bool> { | ||
println!("[TD]: Change Set into SymMemory {:?}", func.name()); | ||
let args = plugin_ctx.data_store(); | ||
let z3_ctx = plugin_ctx.z3_ctx(); | ||
let ty_ctx = plugin_ctx.ty_ctx(); | ||
|
||
let memory = plugin_ctx.memory_mut(); | ||
let solver = plugin_ctx.solver(); | ||
Ok(true) | ||
if func.name() == "now_microseconds" { | ||
self.now_microseconds_used = true; | ||
let ty_ctx = plugin_ctx.ty_ctx(); | ||
let z3_ctx = plugin_ctx.z3_ctx(); | ||
self.timestamp_symbol = Some(rand::random::<u64>()); | ||
let model = plugin_ctx.solver(); | ||
let mem_key_sort = ty_ctx.memory_key_sort(); | ||
let timestamp_val = Datatype::fresh_const(z3_ctx, "timestamp", &mem_key_sort.sort); | ||
plugin_ctx | ||
.memory_mut() | ||
.write_resource(z3_ctx, ty_ctx, timestamp_val); | ||
Ok(true) | ||
} | ||
Ok(false) | ||
} | ||
|
||
fn on_after_execute<'ctx>( | ||
&self, | ||
_plugin_context: &mut dyn PluginContext<'ctx>, | ||
_return_values: &[SymValue<'ctx>], | ||
plugin_ctx: &mut dyn PluginContext<'ctx>, | ||
return_values: &[SymValue<'ctx>], | ||
) -> VMResult<()> { | ||
if self.now_microseconds_used { | ||
if let Some(timestamp_symbol) = self.timestamp_symbol { | ||
let solver = plugin_ctx.solver(); | ||
let model = solver.get_model().unwrap(); | ||
let ty_ctx = plugin_ctx.ty_ctx(); | ||
let z3_ctx = plugin_ctx.z3_ctx(); | ||
let timestamp_val = | ||
plugin_ctx | ||
.memory_mut() | ||
.load_resource(z3_ctx, ty_ctx, &model, "timestamp").unwrap(); | ||
let bv_r = SymIntegerValue::U64(); | ||
let manipulation_cond = timestamp_val.gt(bv_r); | ||
solver.assert(&manipulation_cond.not()); | ||
|
||
match solver.check() { | ||
SatResult::Sat => { | ||
println!("Block Timestamp Manipulation detected!"); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters