Incorrect Builtin ERC4626 Call Signature
Vyper's built-in ERC4626 interface defines functions with optional arguments, but the compiler generates a function signature that omits those arguments when default values are used. This causes calls that rely on defaults to target a different function selector, potentially leading to call failures or unintended function execution.
- Disclosed after under an hour
#37634 [SC-Low] Incorrect Builtin ERC4626 Call Signature
Submitted on Dec 11th 2024 at 08:45:58 UTC by @anatomist for Attackathon | Ethereum Protocol
- Report ID: #37634
- Report Type: Smart Contract
- Report severity: Low
- Target: https://github.com/vyperlang/vyper
- Impacts:
- (Compiler) Unexpected behavior
- (Compiler) Incorrect bytecode generation leading to incorrect behavior
Description
Brief/Intro
Vyper provides several common builtin interfaces to save developer the troubles of having to define those themselves. Unfortunately, the interface of ERC4626 may produce incorrect signatures, potentially leading to call failures or calling incorrect functions.
Vulnerability Details
The ERC4626.vyi builtin interface provided by vyper contains several functions with optional arguments.
@external
def deposit(assets: uint256, receiver: address=msg.sender) -> uint256:
...
@external
def mint(shares: uint256, receiver: address=msg.sender) -> uint256:
...
@external
def withdraw(assets: uint256, receiver: address=msg.sender, owner: address=msg.sender) -> uint256:
...
@external
def redeem(shares: uint256, receiver: address=msg.sender, owner: address=msg.sender) -> uint256:
...
However, it is overlooked that for optional arguments in contract abi functions, the function signature is calculated WITHOUT the arguments using default values. This means if developers call the builtin interface function while using default values, they'll end up calling an incorrect function.
For example, the two calls in the contract below ends up resolving to different signatures, and only the first signature is correct.
from ethereum.ercs import IERC4626
@deploy
def __init__(x: IERC4626):
extcall x.withdraw(0, self, self) #0xb460af94 "withdraw(uint256,address,address)
extcall x.withdraw(0) #0x2e1a7d4d "withdraw(uint256)"
Impact Details
In most cases, using an incorrect interface will likely result in a call failure, which prevents normal operation of the contract. In certain scenarios, the incorrect call might result in calling the fallback function or another function with matching interface. In such cases, unexpected actions may be performed on behalf of the contract, resulting in more severe consequences.
References
https://github.com/vyperlang/vyper/blob/12ab4919cc4618fcac4f5d24d45a0e7fdbc4a48c/vyper/builtins/interfaces/IERC4626.vyi#L47
Proof of Concept
Proof of Concept
Already shown in Vulnerability Details section.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the compiler computed the function selector from the full parameter list including optional arguments, instead of excluding parameters that use default values.
Impact
In most cases the incorrect call would fail, preventing normal operation; in some cases it could invoke a fallback or another function with a matching selector, causing unexpected actions on behalf of the contract.
What the attacker needed
Use Vyper's built-in ERC4626 interface; call a function relying on default arguments
The fix
The compiler should generate the correct function selector by excluding optional arguments that use default values when computing the signature.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Smart contracts talk to each other by calling functions identified by a short code derived from the function's name and parameter types. Vyper provides ready-made interfaces for common standards like ERC4626, but here the interface allowed optional parameters. When a developer left those parameters out, Vyper generated a different code than expected, so the call went to the wrong function or failed.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.