Gas issuance is inflated and will halt the chain or lead to incorrect base fee
The base fee calculation in the anchor() function incorrectly issues gas based on the number of L1 blocks since the last sync, which is only updated every 5 blocks, leading to over-issuance. This can cause the chain to halt if the calculated base fee does not match block.basefee, or result in a severely deflated base fee if the flawed calculation is used.
The base fee calculation in the anchor() function is incorrect. Issuance is over inflated and will either lead to the chain halting or a severely deflated base fee.
if (gasExcess > 0) {
// We always add the gas used by parent block to the gas excess
// value as this has already happened
uint256 excess = uint256(gasExcess) + _parentGasUsed;
// Calculate how much more gas to issue to offset gas excess.
// after each L1 block time, config.gasTarget more gas is issued,
// the gas excess will be reduced accordingly.
// Note that when lastSyncedBlock is zero, we skip this step
// because that means this is the first time calculating the basefee
// and the difference between the L1 height would be extremely big,
// reverting the initial gas excess value back to 0.
uint256 numL1Blocks;
if (lastSyncedBlock > 0 && _l1BlockId > lastSyncedBlock) {
numL1Blocks = _l1BlockId - lastSyncedBlock;
}
if (numL1Blocks > 0) {
uint256 issuance = numL1Blocks * _config.gasTargetPerL1Block;
excess = excess > issuance ? excess - issuance : 1;
}
gasExcess_ = uint64(excess.min(type(uint64).max));
// The base fee per gas used by this block is the spot price at the
// bonding curve, regardless the actual amount of gas used by this
// block, however, this block's gas used will affect the next
// block's base fee.
basefee_ = Lib1559Math.basefee(
gasExcess_, uint256(_config.basefeeAdjustmentQuotient) * _config.gasTargetPerL1Block
);
}
Instead of issuing _config.gasTargetPerL1Block for each L1 block we end up issuing uint256 issuance = (_l1BlockOd - lastSyncedBlock) * _config.gasTargetPerL1Block.
if (_l1BlockId > lastSyncedBlock + BLOCK_SYNC_THRESHOLD) {
// Store the L1's state root as a signal to the local signal service to
// allow for multi-hop bridging.
ISignalService(resolve("signal_service", false)).syncChainData(
ownerChainId, LibSignals.STATE_ROOT, _l1BlockId, _l1StateRoot
);
lastSyncedBlock = _l1BlockId;
}
If anchor() is called on 5 consecutive blocks we end up issuing in total 15 * _config.gasTargetPerL1Block instead of 5 * _config.gasTargetPerL1Block.
When the calculated base fee is compared to the block.basefee the following happens:
If block.basefeereports the correct base fee this will end up halting the chain since they will not match.
If block.basefee is using the same flawed calculation the chain continues but with a severely reduced and incorrect base fee.
Here is a simple POC showing the actual issuance compared to the expected issuance. Paste the code into TaikoL1LibProvingWithTiers.t.sol and run forge test --match-test testIssuance -vv.
struct Config {
uint32 gasTargetPerL1Block;
uint8 basefeeAdjustmentQuotient;
}
function getConfig() public view virtual returns (Config memory config_) {
config_.gasTargetPerL1Block = 15 * 1e6 * 4;
config_.basefeeAdjustmentQuotient = 8;
}
uint256 lastSyncedBlock = 1;
uint256 gasExcess = 10;
function _calc1559BaseFee(
Config memory _config,
uint64 _l1BlockId,
uint32 _parentGasUsed
)
private
view
returns (uint256 issuance, uint64 gasExcess_)
{
if (gasExcess > 0) {
uint256 excess = uint256(gasExcess) + _parentGasUsed;
uint256 numL1Blocks;
if (lastSyncedBlock > 0 && _l1BlockId > lastSyncedBlock) {
numL1Blocks = _l1BlockId - lastSyncedBlock;
}
if (numL1Blocks > 0) {
issuance = numL1Blocks * _config.gasTargetPerL1Block;
excess = excess > issuance ? excess - issuance : 1;
}
// I have commented out the below basefee calculation
// and return issuance instead to show the actual
// accumulated issuance over 5 L1 blocks.
// nothing else is changed
//gasExcess_ = uint64(excess.min(type(uint64).max));
//basefee_ = Lib1559Math.basefee(
// gasExcess_, uint256(_config.basefeeAdjustmentQuotient) * _config.gasTargetPerL1Block
//);
}
//if (basefee_ == 0) basefee_ = 1;
}
function testIssuance() external {
uint256 issuance;
uint256 issuanceAdded;
Config memory config = getConfig();
for (uint64 x=2; x <= 6 ;x++){
(issuanceAdded ,) = _calc1559BaseFee(config, x, 0);
issuance += issuanceAdded;
console2.log("added", issuanceAdded);
}
uint256 expectedIssuance = config.gasTargetPerL1Block*5;
console2.log("Issuance", issuance);
console2.log("Expected Issuance", expectedIssuance);
assertEq(expectedIssuance*3, issuance);
Tools Used
Foundry, VScode
Recommended Mitigation Steps
Issue exactly config.gasTargetPerL1Block for each L1 block.
I don’t see a direct loss of funds here and believe M is the correct severity.
2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).
A halted chain leads to frozen funds. The chain will progress for a minimum of 2 blocks since the calculation is correct when lastSyncedBlock =0 and when _l1BlockID-lastSyncedBlock=1
At the block where the base fee is incorrect the chain is halted and funds are locked since the anchor now reverts in perpetuity.
In practice Taiko can easily release all funds by upgrading the contracts but I believe such an intervention should not be considered when evaluating the severity of an issue. From C4 Supreme Court session, Fall 2023
Contract upgradability should never be used as a severity mitigation, i.e. we assume contracts are non-upgradable.
I don’t entirely agree since the chain would be halted so soon in its existence, that being said, some amount of funds, albeit small, would likely be lost. @dantaik / @adaki2004 any last comments before leaving as H severity?
The original report as published on Code4rena, by monrel. 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 issuance calculation multiplies the number of L1 blocks since the last sync by the per-block gas target, but lastSyncedBlock is only updated every 5 blocks, so the same L1 blocks are counted multiple times.
Impact
The chain could halt, freezing funds, or continue with an incorrect base fee, depending on how block.basefee is set.
What the attacker needed
Anchor() called on consecutive blocks; lastSyncedBlock not updated every block
The fix
The fix was to issue exactly config.gasTargetPerL1Block for each L1 block.
How it was found
Call anchor() on 5 consecutive blocks.
Observe that the total gas issued is 15 times the per-block target instead of 5 times.
Compare the calculated base fee to block.basefee; if they differ, the transaction reverts and the chain halts.
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
A blockchain uses a base fee to manage congestion. This report found that a function calculating that fee gave out too much gas credit, which could make the fee wrong and stop the chain from processing new blocks.
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.