Block gas utilization is slightly lower than the actual gas utilization due to the gas snapshot being taken too early, resulting in an inaccurate base fee calculation
The block gas utilization was calculated using a gas snapshot taken too early, before some gas-consuming operations, so the base fee was slightly underestimated. An attacker could not directly exploit this, but it could lead to lower fees during high network usage.
xfeemarket implements a custom PostHandler() for skip-mev’s feemarket. At almost the very end of the execution, the feemarket’s state is updated with the consumed gas amount so that the current block’s gas utilization and, thus, the base fee can be calculated.
However, the gas snapshot that is provided is not 100% accurate. It is taken in line 81. But any gas consumed by the subsequent actions, e.g., GetMinGasPrice() in line 101, or ante.CheckTxFee() in line 112, are not accounted for in the gas snapshot.
As a result, the block utilization for the current height is inaccurate, leading to a slightly lower base fee than it should be.
In line 129, the skip-mev’s feemarket state is updated with the currently used gas so that the block utilization for the current height is calculated correctly.
// Update updates the block utilization for the current height with the given
// transaction utilization i.e. gas limit.
func (s *State) Update(gas uint64, params Params) error {
update := s.Window[s.Index] + gas
if update > params.MaxBlockUtilization {
return fmt.Errorf("block utilization of %d cannot exceed max block utilization of %d", update, params.MaxBlockUtilization)
}
s.Window[s.Index] = update
return nil
}
However, the gas snapshot is taken in line 81, which does not include any gas consumed in lines 101-127 up until the state update.
81: gas := ctx.GasMeter().GasConsumed() // use context gas consumed
Recommended mitigation steps
Consider taking the snapshot right before calling state.Update().
Is indeed valid as the excluded operations cost gas. We followed skip-mev implementation where they have the same issue. Low at best as this has no chance for exploits and would not come up in normal circumstance. Even if it does, it has limited impact as users just have to do the transaction with more slightly gas.
+ // figure out the diff of gas consumed since snapshot
+ gasDiff := ctx.GasMeter().GasConsumed() - snapshot
+ fmt.Println("gasDiff", gasDiff)
Copy paste the following test case to x/xfeemarket/post/fee_test.go
func TestPostHandle_Audit(t *testing.T) {
// Same data for every test case
const (
baseDenom = "stake"
resolvableDenom = "atom"
expectedConsumedGas = 59122
gasLimit = 100000
)
validFeeAmount := types.DefaultMinBaseGasPrice.MulInt64(int64(gasLimit))
validFee := sdk.NewCoins(sdk.NewCoin(baseDenom, validFeeAmount.TruncateInt()))
testCases := []postsuite.TestCase{
{
Name: "signer has enough funds, should pass, with tip",
Malleate: func(s *postsuite.TestSuite) postsuite.TestCaseArgs {
accs := s.CreateTestAccounts(1)
balance := postsuite.TestAccountBalance{
TestAccount: accs[0],
Coins: validFee,
}
s.SetAccountBalances([]postsuite.TestAccountBalance{balance})
return postsuite.TestCaseArgs{
Msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].Account.GetAddress())},
GasLimit: gasLimit,
FeeAmount: validFee,
}
},
RunAnte: true,
RunPost: true,
Simulate: false,
ExpPass: true,
ExpErr: nil,
ExpectConsumedGas: 34836,
Mock: false,
},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Case %s", tc.Name), func(t *testing.T) {
s := postsuite.SetupTestSuite(t, tc.Mock)
s.TxBuilder = s.ClientCtx.TxConfig.NewTxBuilder()
args := tc.Malleate(s)
s.RunTestCase(t, tc, args)
s.Fail("fail")
})
}
}
Running the test logs shows the gas diff since taking a snapshot:
gasDiff 31368
To put this into context: A simple bank send tx, consumed 151,517 gas, that’s 31,368 / 151,517 = ~21% of the gas of such a tx.
When Mantra chain is heavily utilized, this gas inaccuracy is not negligible and results in underestimated base fees, failing to properly increase gas fees to counteract high utilization (and e.g. spam). Therefore, I kindly ask the judge to reconsider the severity and consider Medium appropriate.
Agreed, 21% of the gas accounting being off for reasonably popular transactions is enough impact for Medium severity.
The original report as published on Code4rena, by berndartmueller. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the gas snapshot was taken before all gas-consuming operations in the post-handler, so the recorded gas was lower than the actual gas used.
Impact
The base fee could be slightly underestimated, especially during high network utilization, but there is no direct exploit and users would just need to pay slightly more gas.
What the attacker needed
None
The fix
The snapshot should be taken right before calling state.Update().
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In a blockchain, the fee you pay depends on how busy the network is. This report found that the system measured how busy the network was a bit too early, so it thought the network was less busy than it really was. That meant fees were a little lower than they should have been, but it wasn't a security hole that could be attacked.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.