Skip to content

Commit 2c38a31

Browse files
author
Bryan McQuade
committed
Bound maximum size of random_git buffer.
1 parent 61135c5 commit 2c38a31

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

include/proxy-wasm/limits.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@
2323
#ifndef PROXY_WASM_HOST_MAX_WASM_MEMORY_SIZE_BYTES
2424
#define PROXY_WASM_HOST_MAX_WASM_MEMORY_SIZE_BYTES (1024 * 1024 * 1024)
2525
#endif
26+
27+
// Maximum allowed random_get buffer size. This value is consistent with
28+
// the JavaScript Crypto.getRandomValues() maximum buffer size.
29+
#ifndef PROXY_WASM_HOST_WASI_RANDOM_GET_MAX_SIZE_BYTES
30+
#define PROXY_WASM_HOST_WASI_RANDOM_GET_MAX_SIZE_BYTES (64 * 1024)
31+
#endif

src/exports.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515
//
16+
#include "include/proxy-wasm/limits.h"
1617
#include "include/proxy-wasm/wasm.h"
1718

1819
#include <openssl/rand.h>
@@ -884,6 +885,9 @@ Word wasi_unstable_clock_time_get(Word clock_id, uint64_t /*precision*/,
884885

885886
// __wasi_errno_t __wasi_random_get(uint8_t *buf, size_t buf_len);
886887
Word wasi_unstable_random_get(Word result_buf_ptr, Word buf_len) {
888+
if (buf_len > PROXY_WASM_HOST_WASI_RANDOM_GET_MAX_SIZE_BYTES) {
889+
return 28; // __WASI_EINVAL
890+
}
887891
auto *context = contextOrEffectiveContext();
888892
std::vector<uint8_t> random(buf_len);
889893
RAND_bytes(random.data(), random.size());

0 commit comments

Comments
 (0)