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

whitelist certain wasms #175

Merged
merged 11 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
18 changes: 14 additions & 4 deletions service/pool/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import PoW "./PoW";
import Logs "./Logs";
import Metrics "./Metrics";
import Wasm "canister:wasm-utils";
import Blob "mo:base/Blob";
import Buffer "mo:base/Buffer";
import Nat32 "mo:base/Nat32";
sesi200 marked this conversation as resolved.
Show resolved Hide resolved

shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
let IC : ICType.Self = actor "aaaaa-aa";
Expand Down Expand Up @@ -119,7 +122,7 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
await getExpiredCanisterInfo();
};

public func installCode(info : Types.CanisterInfo, args : Types.InstallArgs, profiling : Bool) : async Types.CanisterInfo {
public shared ({ caller }) func installCode(info : Types.CanisterInfo, args : Types.InstallArgs, profiling : Bool, is_whitelisted : Bool) : async Types.CanisterInfo {
if (info.timestamp == 0) {
stats := Logs.updateStats(stats, #mismatch);
throw Error.reject "Cannot install removed canister";
Expand All @@ -134,7 +137,13 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
limit_stable_memory_page = ?(16384 : Nat32); // Limit to 1G of stable memory
backend_canister_id = ?Principal.fromActor(this);
};
let wasm = await Wasm.transform(args.wasm_module, config);
let wasm = if (caller == controller) {
args.wasm_module;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to bypass the controller for testing? If so, we only want to bypass when is_whitelisted is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the bypass is required for testing. But restricting to is_whitelisted is fine

} else if (is_whitelisted) {
await Wasm.is_whitelisted(args.wasm_module);
} else {
await Wasm.transform(args.wasm_module, config);
};
let newArgs = {
arg = args.arg;
wasm_module = wasm;
Expand Down Expand Up @@ -290,11 +299,12 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
wasm_module : ICType.wasm_module;
mode : { #reinstall; #upgrade; #install };
canister_id : ICType.canister_id;
is_whitelisted : Bool;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrapper for the management canister, so we cannot change the interface. We can set this to false, as it's only used by the canister to spawn child canisters.

}) : async () {
switch (sanitizeInputs(caller, canister_id)) {
case (#ok info) {
let args = { arg; wasm_module; mode; canister_id };
ignore await installCode(info, args, pool.profiling caller); // inherit the profiling of the parent
ignore await installCode(info, args, pool.profiling caller, is_whitelisted); // inherit the profiling of the parent
};
case (#err makeMsg) throw Error.reject(makeMsg "install_code");
};
Expand Down Expand Up @@ -359,7 +369,7 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
msg : {
#GCCanisters : Any;
#balance : Any;
#callForward: Any;
#callForward : Any;
#dump : Any;
#getCanisterId : Any;
#getSubtree : Any;
Expand Down
2 changes: 2 additions & 0 deletions service/wasm-utils/Cargo.lock

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

2 changes: 2 additions & 0 deletions service/wasm-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
hex = "0.4.3"
ic-cdk = "0.8.0-beta.0"
ic-cdk-macros = "0.8.0-beta.0"
serde = "1.0"
serde_bytes = "0.11"
candid = "0.9.0-beta.2"
ic-wasm = { version = "0.3.7", default-features = false }
sha2 = "0.10.6"
walrus = "0.19"

[profile.release]
Expand Down
15 changes: 15 additions & 0 deletions service/wasm-utils/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use candid::{CandidType, Deserialize};
use serde_bytes::ByteBuf;

use ic_wasm::*;
use sha2::Digest;

#[derive(CandidType, Deserialize)]
struct Config {
Expand All @@ -11,6 +12,20 @@ struct Config {
backend_canister_id: Option<candid::Principal>,
}

const WHITELISTED_WASMS: [&str; 1] = [
"651425d92d3796ddae581191452e0e87484eeff4ff6352fe9a59c7e1f97a2310", // dfx 0.14.1 frontend canister
];

#[ic_cdk_macros::query]
fn is_whitelisted(wasm: ByteBuf) -> ByteBuf {
let wasm_hash = hex::encode(sha2::Sha256::digest(&wasm));
if WHITELISTED_WASMS.contains(&wasm_hash.as_str()) {
wasm
} else {
ic_cdk::trap("Wasm is not whitelisted")
}
}

#[ic_cdk_macros::query]
fn transform(wasm: ByteBuf, config: Config) -> ByteBuf {
let mut m = walrus::Module::from_buffer(&wasm).unwrap();
Expand Down
10 changes: 8 additions & 2 deletions service/wasm-utils/wasm-utils.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
type Config = record { profiling: bool; remove_cycles_add: bool; limit_stable_memory_page: opt nat32; backend_canister_id: opt principal};
type Config = record {
profiling : bool;
remove_cycles_add : bool;
limit_stable_memory_page : opt nat32;
backend_canister_id : opt principal;
};

service : {
transform : (blob, Config) -> (blob);
transform : (blob, Config) -> (blob) query;
is_whitelisted : (blob) -> (blob) query;
}