The bankBalance function failed to handle errors correctly
The bankBalance function in a precompile did not check the error returned when decoding call arguments, so it proceeded to call GetBalance with invalid data. This could cause an unexpected panic that is not caught by the out-of-gas handler, potentially crashing the program.
Submitted by shaflow2, also found by Rhaydden
The bankBalance function does not handle errors after decoding the call parameters. As a result, p.evmKeeper.Bank.GetBalance may throw a panic, and this erroneous panic cannot be recovered by HandleOutOfGasPanic, leading to the erroneous panic being propagated further up the program.
Proof of Concept
func (p precompileFunToken) bankBalance(
start OnRunStartResult,
contract *vm.Contract,
) (bz []byte, err error) {
method, args, ctx := start.Method, start.Args, start.CacheCtx
defer func() {
if err != nil {
err = ErrMethodCalled(method, err)
}
}()
if err := assertContractQuery(contract); err != nil {
return bz, err
}
@> addrEth, addrBech32, bankDenom, err := p.parseArgsBankBalance(args)
bankBal := p.evmKeeper.Bank.GetBalance(ctx, addrBech32, bankDenom).Amount.BigInt()
return method.Outputs.Pack([]any{
bankBal,
struct {
EthAddr gethcommon.Address `json:"ethAddr"`
Bech32Addr string `json:"bech32Addr"`
}{
EthAddr: addrEth,
Bech32Addr: addrBech32.String(),
},
}...)
}
It can be observed that even if parseArgsBankBalance returns an error during decoding, the program will still proceed to call p.evmKeeper.Bank.GetBalance using incorrect data.
// GetBalance returns the balance of a specific denomination for a given account
// by address.
func (k BaseViewKeeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
accountStore := k.getAccountStore(ctx, addr)
bz := accountStore.Get([]byte(denom))
balance, err := UnmarshalBalanceCompat(k.cdc, bz, denom)
if err != nil {
panic(err)
}
return balance
}
This is likely to cause GetBalance to throw a panic. This panic is unexpected and, therefore, cannot be caught by HandleOutOfGasPanic, resulting in the program further throwing an exception.
func HandleOutOfGasPanic(err *error) func() {
return func() {
if r := recover(); r != nil {
switch r.(type) {
case sdk.ErrorOutOfGas:
*err = vm.ErrOutOfGas
default:
panic(r)
}
}
}
}
Recommended mitigation steps
func (p precompileFunToken) bankBalance(
start OnRunStartResult,
contract *vm.Contract,
) (bz []byte, err error) {
method, args, ctx := start.Method, start.Args, start.CacheCtx
defer func() {
if err != nil {
err = ErrMethodCalled(method, err)
}
}()
if err := assertContractQuery(contract); err != nil {
return bz, err
}
addrEth, addrBech32, bankDenom, err := p.parseArgsBankBalance(args)
+ if err != nil {
+ err = ErrInvalidArgs(err)
+ return
+ }
bankBal := p.evmKeeper.Bank.GetBalance(ctx, addrBech32, bankDenom).Amount.BigInt()
return method.Outputs.Pack([]any{
bankBal,
struct {
EthAddr gethcommon.Address `json:"ethAddr"`
Bech32Addr string `json:"bech32Addr"`
}{
EthAddr: addrEth,
Bech32Addr: addrBech32.String(),
},
}...)
}
Unique-Divine (Nibiru) confirmed
Nibiru mitigated:
PR-2116 - Fixed bug where the
err != nilcheck is missing in thebankBalanceprecompile method.
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.
How it worked
Root cause
Likely the missing error check after parsing arguments allows invalid data to reach GetBalance, which panics on unmarshalling failure.
Impact
An attacker could potentially trigger an unexpected panic that is not recovered by the out-of-gas handler, causing the program to crash.
What the attacker needed
Ability to call the precompile with malformed arguments
The fix
The code was fixed by adding an error check after parsing arguments and returning an invalid-args error if parsing fails.
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.
A function that reads a bank balance forgot to check whether the input it received was valid. When given bad input, it tried to use it anyway, which could make the whole program crash instead of returning a normal error.
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.