Hardcoded gas used in ERC20 queries allows for block production halt from infinite recursion
Submitted by 3docSec, also found by 0x007
Vulnerability details
The funtoken precompile allows an EVM caller to access information about tokens that coexist in the Cosmos (“coin”) and EVM (“ERC20”) spaces.
Some operations performed by this precompile consist of EVM calls; for example, if we look at the balance method:
File: funtoken.go
265: func (p precompileFunToken) balance(
266: start OnRunStartResult,
267: contract *vm.Contract,
268: ) (bz []byte, err error) {
---
285: erc20Bal, err := p.evmKeeper.ERC20().BalanceOf(funtoken.Erc20Addr.Address, addrEth, ctx)
286: if err != nil {
287: return
288: }
We see that for fetching the EVM info, it calls the evmKeeper.ERC20().BalanceOf function:
File: erc20.go
125: func (e erc20Calls) BalanceOf(
126: contract, account gethcommon.Address,
127: ctx sdk.Context,
128: ) (out *big.Int, err error) {
129: return e.LoadERC20BigInt(ctx, e.ABI, contract, "balanceOf", account)
130: }
Which in turn calls LoadERC20BigInt:
File: erc20.go
222: func (k Keeper) LoadERC20BigInt(
223: ctx sdk.Context,
224: abi *gethabi.ABI,
225: contract gethcommon.Address,
226: methodName string,
227: args ...any,
228: ) (out *big.Int, err error) {
229: res, err := k.CallContract(
230: ctx,
231: abi,
232: evm.EVM_MODULE_ADDRESS, // @audit from
233: &contract,
234: false, // @audit commit = false
235: Erc20GasLimitQuery, // @audit 100_000
236: methodName,
237: args...,
238: )
239: if err != nil {
240: return nil, err
241: }
If we look closely to how this callback to the EVM is done, we see that the gas allowed for this call is hardcoded to 100_000 and is charged only after the call returned.
This is problematic because 100_000 is allocated regardless of the gas limit used to call the funtoken precompile, and this breaks the core invariant of the 63/64 gas allocation that ultimately secures EVM implementation from infinite recursions, which can halt block production and cause the validator to be slashed.
While the balance example was described in detail, the same applies to the Transfer call in sendToBank:
File: funtoken.go
109: func (p precompileFunToken) sendToBank(
110: startResult OnRunStartResult,
111: caller gethcommon.Address,
112: readOnly bool,
113: ) (bz []byte, err error) {
---
149: gotAmount, transferResp, err := p.evmKeeper.ERC20().Transfer(erc20, caller, transferTo, amount, ctx)
150: if err != nil {
151: return nil, fmt.Errorf("error in ERC20.transfer from caller to EVM account: %w", err)
152: }
The Burn call in sendToBank is instead secure because it only applies to ERC20 tokens deployed from Coins whose EVM contract is safe.
Proof of Concept
For the balance/balanceOf attack path, (and a more comprehensive, but slower, end-to-end test), this GitHub Gist includes a coded PoC in the form of an e2e test that:
- Creates an attack ERC-20 token with a function that calls itself through the
funtokenprecompile. - Registers this token as
funtoken(the call takes a fee but is otherwise permissionless). - Calls the infinite recursing function.
This test can be run while monitoring the memory consumption of the localnet nibid process:
-
Before the test, the
nibdprocess consumes steadily~100Mbof memory. -
After the
go()call is triggered:- Memory consumption increases at
~10Mb/secuntilnibdgets eventually killed. - Even during memory ramp up, the test network stops producing blocks due to consensus timeout.
- Memory consumption increases at
For the sendToBank/transfer attack path, the infinite recursion can be tested by changing the transfer function in the TestERC20MaliciousTransfer test contract as follows:
import "@openzeppelin/contracts/utils/Strings.sol";
// ...
function transfer(address to, uint256 amount) public override returns (bool) {
(bool res, bytes memory data) = address(0x800).call(
abi.encodeWithSignature(
"sendToBank(address,uint256,string)",
address(this),
Strings.toHexString(uint160(to))
)
);
require(res, string(data));
return true;
}
Then, running the TestFunTokenFromERC20MaliciousTransfer test in x/evm/keeper/funtoken_from_erc20_test.go will hang in an infinite recursion that will quickly eat up all memory available.
Recommended Mitigation Steps
Consider refactoring the evmKeeper.ERC20().BalanceOf and evmKeeper.ERC20().Transfer calls to accept as argument, and use at most, 63/64 of the EVM gas available.
Unique-Divine (Nibiru) commented:
Please label this as
sponsor confirmed. I’ve not yet run the steps to reproduce the error case, but it seems legit from reading the description.
k-yang (Nibiru) confirmed and commented:
Agreed it’s a valid issue.
onikonychev (Nibiru) commented:
This is a great catch, indeed! I was able to crash my localnet with either recursive
balanceOf()ortransfer().
Nibiru mitigated:
PR-2129 - Resolved an infinite recursion issue in ERC20 FunToken contracts.
Status: Mitigation confirmed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A high-severity doS bug in Nibiru. Code4rena made it public on 24 February 2025. Found in the 2024-11-nibiru audit contest (finding H-06), also found by 1 other warden.
About DoS
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.