Skip to content

Instantly share code, notes, and snippets.

@mpapierski
Last active July 9, 2019 11:25
Show Gist options
  • Save mpapierski/1168f9048dc96c38a452038dd0bc36ca to your computer and use it in GitHub Desktop.
Save mpapierski/1168f9048dc96c38a452038dd0bc36ca to your computer and use it in GitHub Desktop.
malicious_contract
#![no_std]
#![feature(alloc, cell_update, allocator_api)]
#[macro_use]
extern crate alloc;
extern crate core;
extern crate cl_std;
use cl_std::contract_api;
use cl_std::contract_api::pointers::ContractPointer;
use cl_std::key::Key;
use cl_std::uref::URef;
use cl_std::value::U512;
use alloc::collections::btree_map::BTreeMap;
use cl_std::contract_api::argsparser::ArgsParser;
#[no_mangle]
pub extern "C" fn do_nothing() {
// A function that does nothing.
}
use cl_std::bytesrepr::{FromBytes, ToBytes};
use alloc::alloc::{Alloc, Global};
use alloc::vec::Vec;
pub fn alloc_bytes(n: usize) -> *mut u8 {
if n == 0 {
// cannot allocate with size 0
0 as *mut u8
} else {
Global.alloc_array(n).unwrap().as_ptr()
}
}
mod malicious_ffi {
extern "C" {
pub fn call_contract(
key_ptr: *const u8,
key_size: usize,
args_ptr: *const u8,
args_size: usize,
// extra urefs known by the caller to make available to the callee
extra_urefs_ptr: *const u8,
extra_urefs_size: usize,
) -> usize;
}
}
#[allow(clippy::ptr_arg)]
pub fn my_call_contract<A: ArgsParser>(
c_ptr: ContractPointer,
args: &A,
) {
let contract_key: Key = c_ptr.into();
let (key_ptr, key_size, _bytes1) = to_ptr(&contract_key);
let (args_ptr, args_size, _bytes2) = ArgsParser::parse(args).map(|args| to_ptr(&args)).unwrap();
let mut extra_urefs = vec![255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let res_size = unsafe {
malicious_ffi::call_contract(
key_ptr, key_size, args_ptr, args_size, extra_urefs.as_mut_ptr(), extra_urefs.len(),
)
};
}
#[no_mangle]
pub extern "C" fn call() {
let do_nothing: ContractPointer = contract_api::store_function("do_nothing", BTreeMap::new());
// Allocate on heap
let mut key = vec![0; 128];
let mut args = vec![0; 128];
// This is the payload
let mut extra_urefs = vec![255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
my_call_contract(do_nothing.clone(), &());
}
extern crate casperlabs_engine_grpc_server;
extern crate common;
extern crate execution_engine;
extern crate grpc;
extern crate shared;
extern crate storage;
use std::collections::HashMap;
use test_support::{WasmTestBuilder, DEFAULT_BLOCK_TIME};
#[allow(dead_code)]
mod test_support;
const GENESIS_ADDR: [u8; 32] = [7u8; 32];
#[ignore]
#[test]
fn should_not_fail_deserializing() {
WasmTestBuilder::default()
.run_genesis(GENESIS_ADDR, HashMap::new())
.exec_with_args(
GENESIS_ADDR,
"deserialize_error.wasm",
DEFAULT_BLOCK_TIME,
1,
GENESIS_ADDR,
)
.commit()
.expect_success();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment