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

Allow RO reentrancy for the hook target #90

Merged
merged 3 commits into from
Apr 2, 2024
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
10 changes: 6 additions & 4 deletions src/EVault/Dispatch.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ abstract contract Dispatch is
function delegateToModuleView(address module) private view {
assembly {
// Construct optimized custom call data for `this.viewDelegate()`
// [selector 4B][module address 32B][calldata with stripped proxy metadata]
// [selector 4B][module address 32B][calldata with stripped proxy metadata][caller address 32B]
// Proxy metadata will be appended back by the proxy on staticcall
mstore(0, 0x1fe8b95300000000000000000000000000000000000000000000000000000000)
mstore(4, module)
calldatacopy(36, 0, calldatasize())
// insize: calldatasize + 36 (signature and address) - proxy metadata size
let result := staticcall(gas(), address(), 0, sub(add(calldatasize(), 36), PROXY_METADATA_LENGTH), 0, 0)
let strippedCalldataSize := sub(calldatasize(), PROXY_METADATA_LENGTH)
calldatacopy(36, 0, strippedCalldataSize)
mstore(add(36, strippedCalldataSize), caller())
// insize: stripped calldatasize + 36 (signature and module address) + 32 (caller address)
let result := staticcall(gas(), address(), 0, add(strippedCalldataSize, 68), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 { revert(0, returndatasize()) }
Expand Down
14 changes: 13 additions & 1 deletion src/EVault/shared/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.0;

import {EVCClient} from "./EVCClient.sol";
import {Cache} from "./Cache.sol";
import {ProxyUtils} from "./lib/ProxyUtils.sol";
import {RevertBytes} from "./lib/RevertBytes.sol";

import {IProtocolConfig} from "../../ProtocolConfig/IProtocolConfig.sol";
Expand Down Expand Up @@ -45,7 +46,18 @@ abstract contract Base is EVCClient, Cache {
}

modifier nonReentrantView() {
if (vaultStorage.reentrancyLocked) revert E_Reentrancy();
if (vaultStorage.reentrancyLocked) {
address hookTarget = vaultStorage.hookTarget;

// the hook target is allowed to bypass the RO-reentrancy lock. the hook target can either be a msg.sender
// when the view function is inlined in the EVault.sol or the hook target should be taked from the trailing
// data appended by the delegateToModuleView function used by useView modifier. in the latter case, it is
// safe to consume the trailing data as we know we are inside the useView because msg.sender == address(this)
if (msg.sender != hookTarget && !(msg.sender == address(this) && ProxyUtils.useViewCaller() == hookTarget))
kasperpawlowski marked this conversation as resolved.
Show resolved Hide resolved
{
revert E_Reentrancy();
}
}
_;
}

Expand Down
8 changes: 8 additions & 0 deletions src/EVault/shared/lib/ProxyUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pragma solidity ^0.8.0;
import {IERC20} from "../../IEVault.sol";
import {IPriceOracle} from "../../../interfaces/IPriceOracle.sol";

import "../Constants.sol";

/// @title ProxyUtils Library
/// @author Euler Labs (https://www.eulerlabs.com/)
/// @notice The library provides a helper function for working with proxy meta data
Expand All @@ -16,4 +18,10 @@ library ProxyUtils {
unitOfAccount := shr(96, calldataload(sub(calldatasize(), 20)))
}
}

function useViewCaller() internal pure returns (address viewCaller) {
assembly {
viewCaller := shr(96, calldataload(sub(calldatasize(), add(PROXY_METADATA_LENGTH, 20))))
}
}
}
4 changes: 4 additions & 0 deletions test/unit/evault/EVaultTestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ contract MockHook {
function deposit(uint256, address) external view {
address asset = IEVault(msg.sender).asset();

// these calls are just to test if there's no RO-reentrancy for the hook target
IEVault(msg.sender).totalBorrows();
IEVault(msg.sender).balanceOf(address(this));

if (asset != caller()) revert E_OnlyAssetCanDeposit();
}

Expand Down
Loading