Skip to content

Bound maximum size of random_get buffer. #284

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

Merged
merged 8 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions include/proxy-wasm/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@
#ifndef PROXY_WASM_HOST_MAX_WASM_MEMORY_SIZE_BYTES
#define PROXY_WASM_HOST_MAX_WASM_MEMORY_SIZE_BYTES (1024 * 1024 * 1024)
#endif

// Maximum allowed random_get buffer size. This value is consistent with
// the JavaScript Crypto.getRandomValues() maximum buffer size.
// See: https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues
#ifndef PROXY_WASM_HOST_WASI_RANDOM_GET_MAX_SIZE_BYTES
#define PROXY_WASM_HOST_WASI_RANDOM_GET_MAX_SIZE_BYTES (64 * 1024)
#endif
7 changes: 7 additions & 0 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "include/proxy-wasm/limits.h"
#include "include/proxy-wasm/wasm.h"

#include <openssl/rand.h>
Expand Down Expand Up @@ -884,6 +885,12 @@ Word wasi_unstable_clock_time_get(Word clock_id, uint64_t /*precision*/,

// __wasi_errno_t __wasi_random_get(uint8_t *buf, size_t buf_len);
Word wasi_unstable_random_get(Word result_buf_ptr, Word buf_len) {
if (buf_len > PROXY_WASM_HOST_WASI_RANDOM_GET_MAX_SIZE_BYTES) {
return 28; // __WASI_EINVAL
}
if (buf_len == 0) {
return 0; // __WASI_ESUCCESS
}
auto *context = contextOrEffectiveContext();
std::vector<uint8_t> random(buf_len);
RAND_bytes(random.data(), random.size());
Expand Down
2 changes: 2 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ cc_test(
data = [
"//test/test_data:clock.wasm",
"//test/test_data:env.wasm",
"//test/test_data:random.wasm",
],
linkstatic = 1,
shard_count = 8,
deps = [
":utility_lib",
"//:lib",
Expand Down
68 changes: 68 additions & 0 deletions test/exports_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,73 @@ TEST_P(TestVm, Clock) {
EXPECT_TRUE(context->isLogged("realtime: "));
}

TEST_P(TestVm, RandomZero) {
auto source = readTestWasmFile("random.wasm");
ASSERT_FALSE(source.empty());
auto wasm = TestWasm(std::move(vm_));
ASSERT_TRUE(wasm.load(source, false));
ASSERT_TRUE(wasm.initialize());

WasmCallVoid<1> run;
wasm.wasm_vm()->getFunction("run", &run);
ASSERT_TRUE(run != nullptr);
run(wasm.vm_context(), Word{0});

// Check application logs.
auto *context = dynamic_cast<TestContext *>(wasm.vm_context());
EXPECT_TRUE(context->isLogged("random_get(0) succeeded."));
}

TEST_P(TestVm, RandomSmall) {
auto source = readTestWasmFile("random.wasm");
ASSERT_FALSE(source.empty());
auto wasm = TestWasm(std::move(vm_));
ASSERT_TRUE(wasm.load(source, false));
ASSERT_TRUE(wasm.initialize());

WasmCallVoid<1> run;
wasm.wasm_vm()->getFunction("run", &run);
ASSERT_TRUE(run != nullptr);
run(wasm.vm_context(), Word{32});

// Check application logs.
auto *context = dynamic_cast<TestContext *>(wasm.vm_context());
EXPECT_TRUE(context->isLogged("random_get(32) succeeded."));
}

TEST_P(TestVm, RandomLarge) {
auto source = readTestWasmFile("random.wasm");
ASSERT_FALSE(source.empty());
auto wasm = TestWasm(std::move(vm_));
ASSERT_TRUE(wasm.load(source, false));
ASSERT_TRUE(wasm.initialize());

WasmCallVoid<1> run;
wasm.wasm_vm()->getFunction("run", &run);
ASSERT_TRUE(run != nullptr);
run(wasm.vm_context(), Word{64 * 1024});

// Check application logs.
auto *context = dynamic_cast<TestContext *>(wasm.vm_context());
EXPECT_TRUE(context->isLogged("random_get(65536) succeeded."));
}

TEST_P(TestVm, RandomTooLarge) {
auto source = readTestWasmFile("random.wasm");
ASSERT_FALSE(source.empty());
auto wasm = TestWasm(std::move(vm_));
ASSERT_TRUE(wasm.load(source, false));
ASSERT_TRUE(wasm.initialize());

WasmCallVoid<1> run;
wasm.wasm_vm()->getFunction("run", &run);
ASSERT_TRUE(run != nullptr);
run(wasm.vm_context(), Word{65 * 1024});

// Check application logs.
auto *context = dynamic_cast<TestContext *>(wasm.vm_context());
EXPECT_TRUE(context->isLogged("random_get(66560) failed."));
}

} // namespace
} // namespace proxy_wasm
6 changes: 6 additions & 0 deletions test/test_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ wasm_rust_binary(
srcs = ["clock.rs"],
wasi = True,
)

wasm_rust_binary(
name = "random.wasm",
srcs = ["random.rs"],
wasi = True,
)
38 changes: 38 additions & 0 deletions test/test_data/random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[no_mangle]
pub extern "C" fn proxy_abi_version_0_2_0() {}

#[no_mangle]
pub extern "C" fn proxy_on_memory_allocate(_: usize) -> *mut u8 {
std::ptr::null_mut()
}

// TODO(PiotrSikora): switch to "getrandom" crate.
pub mod wasi_snapshot_preview1 {
#[link(wasm_import_module = "wasi_snapshot_preview1")]
extern "C" {
pub fn random_get(buf: *mut u8, buf_len: usize) -> u16;
}
}

#[no_mangle]
pub extern "C" fn run(size: usize) {
let mut buf: Vec<u8> = Vec::with_capacity(size);
match unsafe { wasi_snapshot_preview1::random_get(buf.as_mut_ptr(), size) } {
0 => println!("random_get({}) succeeded.", size),
_ => println!("random_get({}) failed.", size),
}
}