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

fix(EVM): Wrap access to calldata to prevent out-of-bounds EraVM panic #1097

Merged
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
86 changes: 62 additions & 24 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,13 @@ object "EvmEmulator" {
case 0x35 { // OP_CALLDATALOAD
evmGasLeft := chargeGas(evmGasLeft, 3)

stackHead := calldataload(accessStackHead(sp, stackHead))
let calldataOffset := accessStackHead(sp, stackHead)

stackHead := 0
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
stackHead := calldataload(calldataOffset)
}

ip := add(ip, 1)
}
Expand All @@ -1577,25 +1583,38 @@ object "EvmEmulator" {
case 0x37 { // OP_CALLDATACOPY
evmGasLeft := chargeGas(evmGasLeft, 3)

let destOffset, offset, size
let dstOffset, sourceOffset, len

popStackCheck(sp, 3)
destOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
offset, sp, stackHead:= popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(destOffset, size)
dstOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
sourceOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
len, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

if gt(offset, MAX_UINT64()) {
offset := MAX_UINT64()
}
checkMemIsAccessible(dstOffset, len)

// dynamicGas = 3 * minimum_word_size + memory_expansion_cost
// minimum_word_size = (size + 31) / 32
let dynamicGas := add(mul(3, shr(5, add(size, 31))), expandMemory(destOffset, size))
let dynamicGas := add(mul(3, shr(5, add(len, 31))), expandMemory(dstOffset, len))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

calldatacopy(add(destOffset, MEM_OFFSET()), offset, size)
dstOffset := add(dstOffset, MEM_OFFSET())

// EraVM will revert if offset + length overflows uint32
if gt(sourceOffset, UINT32_MAX()) {
sourceOffset := UINT32_MAX()
}

// Check bytecode out-of-bounds access
let truncatedLen := len
if gt(add(sourceOffset, len), UINT32_MAX()) {
truncatedLen := sub(UINT32_MAX(), sourceOffset) // truncate
$llvm_AlwaysInline_llvm$_memsetToZero(add(dstOffset, truncatedLen), sub(len, truncatedLen)) // pad with zeroes any out-of-bounds
}

if truncatedLen {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

ip := add(ip, 1)

}
Expand Down Expand Up @@ -4612,7 +4631,13 @@ object "EvmEmulator" {
case 0x35 { // OP_CALLDATALOAD
evmGasLeft := chargeGas(evmGasLeft, 3)

stackHead := calldataload(accessStackHead(sp, stackHead))
let calldataOffset := accessStackHead(sp, stackHead)

stackHead := 0
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
stackHead := calldataload(calldataOffset)
}

ip := add(ip, 1)
}
Expand All @@ -4625,25 +4650,38 @@ object "EvmEmulator" {
case 0x37 { // OP_CALLDATACOPY
evmGasLeft := chargeGas(evmGasLeft, 3)

let destOffset, offset, size
let dstOffset, sourceOffset, len

popStackCheck(sp, 3)
destOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
offset, sp, stackHead:= popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(destOffset, size)
dstOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
sourceOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
len, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

if gt(offset, MAX_UINT64()) {
offset := MAX_UINT64()
}
checkMemIsAccessible(dstOffset, len)

// dynamicGas = 3 * minimum_word_size + memory_expansion_cost
// minimum_word_size = (size + 31) / 32
let dynamicGas := add(mul(3, shr(5, add(size, 31))), expandMemory(destOffset, size))
let dynamicGas := add(mul(3, shr(5, add(len, 31))), expandMemory(dstOffset, len))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

calldatacopy(add(destOffset, MEM_OFFSET()), offset, size)
dstOffset := add(dstOffset, MEM_OFFSET())

// EraVM will revert if offset + length overflows uint32
if gt(sourceOffset, UINT32_MAX()) {
sourceOffset := UINT32_MAX()
}

// Check bytecode out-of-bounds access
let truncatedLen := len
if gt(add(sourceOffset, len), UINT32_MAX()) {
truncatedLen := sub(UINT32_MAX(), sourceOffset) // truncate
$llvm_AlwaysInline_llvm$_memsetToZero(add(dstOffset, truncatedLen), sub(len, truncatedLen)) // pad with zeroes any out-of-bounds
}

if truncatedLen {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

ip := add(ip, 1)

}
Expand Down
43 changes: 31 additions & 12 deletions system-contracts/evm-emulator/EvmEmulatorLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ for { } true { } {
case 0x35 { // OP_CALLDATALOAD
evmGasLeft := chargeGas(evmGasLeft, 3)

stackHead := calldataload(accessStackHead(sp, stackHead))
let calldataOffset := accessStackHead(sp, stackHead)

stackHead := 0
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
stackHead := calldataload(calldataOffset)
}

ip := add(ip, 1)
}
Expand All @@ -352,25 +358,38 @@ for { } true { } {
case 0x37 { // OP_CALLDATACOPY
evmGasLeft := chargeGas(evmGasLeft, 3)

let destOffset, offset, size
let dstOffset, sourceOffset, len

popStackCheck(sp, 3)
destOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
offset, sp, stackHead:= popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(destOffset, size)
dstOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
sourceOffset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
len, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

if gt(offset, MAX_UINT64()) {
offset := MAX_UINT64()
}
checkMemIsAccessible(dstOffset, len)

// dynamicGas = 3 * minimum_word_size + memory_expansion_cost
// minimum_word_size = (size + 31) / 32
let dynamicGas := add(mul(3, shr(5, add(size, 31))), expandMemory(destOffset, size))
let dynamicGas := add(mul(3, shr(5, add(len, 31))), expandMemory(dstOffset, len))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

calldatacopy(add(destOffset, MEM_OFFSET()), offset, size)
dstOffset := add(dstOffset, MEM_OFFSET())

// EraVM will revert if offset + length overflows uint32
if gt(sourceOffset, UINT32_MAX()) {
sourceOffset := UINT32_MAX()
}

// Check bytecode out-of-bounds access
let truncatedLen := len
if gt(add(sourceOffset, len), UINT32_MAX()) {
truncatedLen := sub(UINT32_MAX(), sourceOffset) // truncate
$llvm_AlwaysInline_llvm$_memsetToZero(add(dstOffset, truncatedLen), sub(len, truncatedLen)) // pad with zeroes any out-of-bounds
}

if truncatedLen {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

ip := add(ip, 1)

}
Expand Down
Loading