When a batch of withdrawals expires, that batch is often underpaid their owed interest
When a withdrawal batch expires, the market updates the interest scale factor only up to the expiry timestamp rather than the current block timestamp, so lenders in that batch are paid interest only up to expiry. If the batch is then paid out, those lenders lose the interest accrued between expiry and the actual payment time.
Submitted by Toshii, also found by Yanchuan and caventa
Lines of code
https://github.com/code-423n4/2023-10-wildcat/blob/main/src/market/WildcatMarketBase.sol#L361-L376 https://github.com/code-423n4/2023-10-wildcat/blob/main/src/market/WildcatMarketBase.sol#L466-L492
Impact
Until the exact timestamp that a payment is processed for a given withdrawal batch (batch.scaledAmountBurned incremented, batch.normalizedAmountPaid incremented), the underlying amount of that batch should continue to be earning interest. It’s only when the payment is actually made that the underlying amount of the batch should stop earning interest - at this point users in that batch are actually able to withdraw funds. This is clear with the general flow of logic of how _applyWithdrawalBatchPayment is being called.
This flow is not being followed when a batch of withdrawals first expires. Instead of getting paid interest up until the current block.timestamp, they are only paid interest up to the timestamp of the expiry. This means, (assuming theres enough assets currently in the market to pay off that batch of withdrawals) users in that batch are unfairly losing out on (block.timestamp - expiry) the duration of interest. As this duration increases, those lenders are losing increasing amounts of interest.
Proof of Concept
The logic for handling a withdrawal batch when it first expires occurs in the WildcatMarketBase:_getUpdatedState function, which is defined as follows:
function _getUpdatedState() internal returns (MarketState memory state) {
state = _state;
// Handle expired withdrawal batch
if (state.hasPendingExpiredBatch()) {
uint256 expiry = state.pendingWithdrawalExpiry;
// Only accrue interest if time has passed since last update.
// This will only be false if withdrawalBatchDuration is 0.
if (expiry != state.lastInterestAccruedTimestamp) {
(uint256 baseInterestRay, uint256 delinquencyFeeRay, uint256 protocolFee) = state
.updateScaleFactorAndFees(
protocolFeeBips,
delinquencyFeeBips,
delinquencyGracePeriod,
expiry // @issue
);
emit ScaleFactorUpdated(state.scaleFactor, baseInterestRay, delinquencyFeeRay, protocolFee);
}
_processExpiredWithdrawalBatch(state);
}
...
}
Effectively, the market parameters related to the accrued interest owed to lenders (most important of which is state.scaleFactor) is updated in the state.updateScaleFactorAndFees(..) call, which we can see updates the interest up to the expiry timestamp. Let’s consider a scenario where state.lastInterestAccruedTimestamp = N, expiry= N, and block.timestamp= N+86_400 (one day). At this point in time, all lenders in this market (including lenders in this expiring withdrawal batch) are owed interest for N+86_400-N = 86_400, which is the duration from the current block.timestamp to the previous timestamp that interest was accrued.
Considering this, in the above function call, (expiry != state.lastInterestAccruedTimestamp)= False and so it is skipped. However, even if expiry = N+1 for example, and it is not skipped, there is still a fundamental issue. For simplicity, I’m just assuming expiry = N. Next, _processExpiredWithdrawalBatch is called, which is defined as follows:
function _processExpiredWithdrawalBatch(MarketState memory state) internal {
uint32 expiry = state.pendingWithdrawalExpiry;
WithdrawalBatch memory batch = _withdrawalData.batches[expiry];
// Burn as much of the withdrawal batch as possible with available liquidity.
uint256 availableLiquidity = batch.availableLiquidityForPendingBatch(state, totalAssets());
if (availableLiquidity > 0) {
_applyWithdrawalBatchPayment(batch, state, expiry, availableLiquidity);
}
...
}
Here, let’s assume that the borrower has left enough assets in the Market so that the entire withdrawal batch which just expired can be paid out in full. This logic is implemented in the _applyWithdrawalBatchPayment function, which is defined as follows:
function _applyWithdrawalBatchPayment(
WithdrawalBatch memory batch,
MarketState memory state,
uint32 expiry,
uint256 availableLiquidity
) internal {
uint104 scaledAvailableLiquidity = state.scaleAmount(availableLiquidity).toUint104();
uint104 scaledAmountOwed = batch.scaledTotalAmount - batch.scaledAmountBurned;
// Do nothing if batch is already paid
if (scaledAmountOwed == 0) {
return;
}
uint104 scaledAmountBurned = uint104(MathUtils.min(scaledAvailableLiquidity, scaledAmountOwed));
uint128 normalizedAmountPaid = state.normalizeAmount(scaledAmountBurned).toUint128();
batch.scaledAmountBurned += scaledAmountBurned;
batch.normalizedAmountPaid += normalizedAmountPaid;
state.scaledPendingWithdrawals -= scaledAmountBurned;
// Update normalizedUnclaimedWithdrawals so the tokens are only accessible for withdrawals.
state.normalizedUnclaimedWithdrawals += normalizedAmountPaid;
...
}
The important thing to note here is that the normalizedAmountPaid is based on referencing the current state.scaleFactor. However, this state.scaleFactor does not include the 86_400 seconds of interest which is actually owed to the lenders in this withdrawal batch. Rather, they are being cheated out of this interest entirely.
Recommended Mitigation Steps
It is not logical that the most recent expired batch only get paid interest up to the expiry. Instead, they should be getting the full amount of interest up to the amount of time they are paid. The WildcatMarketBase:_getUpdatedState function should be changed accordingly:
function _getUpdatedState() internal returns (MarketState memory state) {
state = _state;
if (block.timestamp != state.lastInterestAccruedTimestamp) {
(uint256 baseInterestRay, uint256 delinquencyFeeRay, uint256 protocolFee) = state
.updateScaleFactorAndFees(
protocolFeeBips,
delinquencyFeeBips,
delinquencyGracePeriod,
block.timestamp
);
emit ScaleFactorUpdated(state.scaleFactor, baseInterestRay, delinquencyFeeRay, protocolFee);
}
// Handle expired withdrawal batch
if (state.hasPendingExpiredBatch()) {
_processExpiredWithdrawalBatch(state);
}
}
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 expired-batch handling calls the interest update with the batch expiry timestamp instead of the current block timestamp, so the scale factor used for payment excludes interest accrued after expiry.
Impact
Lenders in an expired batch could lose the interest accrued between expiry and the actual payment, with the loss growing as that duration increases, provided the market has enough assets to pay the batch.
What the attacker needed
A withdrawal batch that has expired; enough market liquidity to pay the batch
The fix
The state update should accrue interest up to the current block timestamp before processing an expired batch, rather than only up to the expiry.
How it was found
- Have a withdrawal batch reach its expiry while the market holds enough liquidity to pay it.
- Trigger the state update that processes the expired batch.
- Observe that the interest scale factor is updated only to the expiry timestamp, not the current block timestamp.
- Observe that the batch is paid using that scale factor, so interest between expiry and payment is not included.
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.
In this lending market, lenders earn interest over time, and when they request a withdrawal the request waits in a batch until it expires. The code is supposed to keep paying interest on that batch's money until the batch is actually paid out. Instead, it stops counting interest at the expiry moment, so lenders get less money than they are owed.
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.