Skip to content

Commit

Permalink
Add hostile "forge" test
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Jul 14, 2023
1 parent 54578f7 commit 34ce465
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 13 deletions.
36 changes: 35 additions & 1 deletion soroban-env-host/src/test/hostile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use soroban_env_common::{
xdr::{ScErrorCode, ScErrorType},
Env, Symbol,
Env, Symbol, EnvBase,
};
use soroban_test_wasms::HOSTILE;

Expand Down Expand Up @@ -116,3 +116,37 @@ fn hostile_objs_traps() -> Result<(), HostError> {
));
Ok(())
}

#[test]
fn hostile_forged_objects_trap() -> Result<(), HostError> {
let host = Host::test_host_with_recording_footprint();
let contract_id_obj = host.register_test_contract_wasm(HOSTILE);

host.set_diagnostic_level(crate::DiagnosticLevel::Debug)?;
host.with_budget(|b| b.reset_default())?;
host.with_budget(|b| b.reset_unlimited_cpu())?;

let private_vec = host.vec_new_from_slice(&[1u32.into(), 2u32.into()])?;

let payload = private_vec.to_val().get_payload();
let lo = payload as u32;
let hi = (payload >> 32) as u32;
let args = host.vec_new_from_slice(&[lo.into(), hi.into()])?;

// Here we're passing a vector of two numbers that, when reassembled into a
// payload and cast to an object, might potentially alow access to the
// underlying `vec`. But they shouldn't, because that vec was not explicitly
// passed to the function as an argument (thus not installed in its relative
// object reference table).
let res = host.call(
contract_id_obj,
Symbol::try_from_small_str("forge")?, args);

assert!(HostError::result_matches_err(
res.clone(),
(ScErrorType::Context, ScErrorCode::InvalidInput)
));

Ok(())

}
31 changes: 21 additions & 10 deletions soroban-test-wasms/wasm-workspace/Cargo.lock

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

2 changes: 1 addition & 1 deletion soroban-test-wasms/wasm-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ soroban-env-guest = { path = "../../soroban-env-guest" }
soroban-env-host = { path = "../../soroban-env-host" }

[workspace.dependencies.soroban-sdk]
version = "0.9.1"
version = "0.9.2"
git = "https://github.com/stellar/rs-soroban-sdk"

# Always build using the local SDK. Usually the env change is accompanied with
Expand Down
8 changes: 7 additions & 1 deletion soroban-test-wasms/wasm-workspace/hostile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![no_std]
use soroban_sdk::{contract, contractimpl, Bytes, Env};
use soroban_sdk::{contract, contractimpl, Bytes, Env, Val, Vec, FromVal};

#[contract]
pub struct Contract;
Expand Down Expand Up @@ -57,4 +57,10 @@ impl Contract {
Bytes::from_slice(&env, &local_buf);
}
}

pub fn forge(env: Env, lo: u32, hi: u32) -> u32 {
let payload: u64 = lo as u64 | ((hi as u64) << 32);
let v: Vec<u32> = Vec::from_val(&env, &Val::from_payload(payload));
v.get(0).unwrap()
}
}
Binary file modified soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_complex.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fib.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_vec.wasm
Binary file not shown.
Binary file not shown.

0 comments on commit 34ce465

Please sign in to comment.