Submitted by 3docSec, also found by 0x007
https://github.com/code-423n4/2024-11-nibiru/blob/8ed91a036f664b421182e183f19f6cef1a4e28ea/x/evm/precompile/funtoken.go#L149
https://github.com/code-423n4/2024-11-nibiru/blob/8ed91a036f664b421182e183f19f6cef1a4e28ea/x/evm/precompile/funtoken.go#L285
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:
We see that for fetching the EVM info, it calls the evmKeeper.ERC20().BalanceOf function:
Which in turn calls LoadERC20BigInt:
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:
The Burn call in sendToBank is instead secure because it only applies to ERC20 tokens deployed from Coins whose EVM contract is safe.
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:
This test can be run while monitoring the memory consumption of the localnet nibid process:
For the sendToBank/transfer attack path, the infinite recursion can be tested by changing the transfer function in the TestERC20MaliciousTransfer test contract as follows:
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.
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:
k-yang (Nibiru) confirmed and commented:
onikonychev (Nibiru) commented:
Nibiru mitigated:
Status: Mitigation confirmed. 
