Risk of overpayment due to race condition between repay and liquidateWithReplacement transactions
A race condition between a borrower's repay transaction and a keeper's liquidateWithReplacement transaction can cause the borrower to repay another borrower's debt. The borrower ends up paying more than double their future value because the debt position's borrower is changed before their repay executes.
Submitted by elhaj, also found by KupiaSec, mt030d, and VAD37
https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/Size.sol#L198-L201 https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/Size.sol#L229-L244
Impact
- Likelihood: Medium - Users and liquidation bots are likely to call their respective functions around the same time.
- Impact: High - Users end up repaying more than double their future value.
Proof of Concept
The LiquidateWithReplacement mechanism in the Size protocol allows a permissioned role (KEEPER_ROLE) to perform the liquidation of a debt position by replacing an unhealthy borrower with a healthier one from the order book. Here’s how it works:
- The debt position of the unhealthy borrower is liquidated, and the
futureValueis paid to the Size contract by the liquidator,while the liquidator gets an equivalent amount plus reward in terms of collateral tokens from the unhealthy borrower. - The borrower of this
debtPositionis updated to the new healthier borrower. - The debt position retains its same
futureValueandID. - A portion of the repaid funds is used to fund a new borrower.
function executeLiquidateWithReplacement(State storage state, LiquidateWithReplacementParams calldata params)
external
returns (uint256 issuanceValue, uint256 liquidatorProfitCollateralToken, uint256 liquidatorProfitBorrowToken)
{
// some code ...
/*1*/>> liquidatorProfitCollateralToken = state.executeLiquidate(LiquidateParams({debtPositionId: params.debtPositionId, minimumCollateralProfit: params.minimumCollateralProfit}));
uint256 ratePerTenor = borrowOffer.getRatePerTenor(
VariablePoolBorrowRateParams({
variablePoolBorrowRate: state.oracle.variablePoolBorrowRate,
variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt,
variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval
}),
tenor
);
issuanceValue = Math.mulDivDown(debtPositionCopy.futureValue, PERCENT, PERCENT + ratePerTenor);
liquidatorProfitBorrowToken = debtPositionCopy.futureValue - issuanceValue;
/*2*/>> debtPosition.borrower = params.borrower;
/*3*/>> debtPosition.futureValue = debtPositionCopy.futureValue;
debtPosition.liquidityIndexAtRepayment = 0;
emit Events.UpdateDebtPosition(params.debtPositionId, debtPosition.borrower, debtPosition.futureValue, debtPosition.liquidityIndexAtRepayment);
// @note : in this case the borrower doesn't pay swapFees
state.data.debtToken.mint(params.borrower, debtPosition.futureValue);
/*4*/>> state.data.borrowAToken.transferFrom(address(this), params.borrower, issuanceValue);
state.data.borrowAToken.transferFrom(address(this), state.feeConfig.feeRecipient, liquidatorProfitBorrowToken);
}
By maintaining the same debt position ID and future value, lenders (all credit holders of this debt position) can still expect repayment on the original due date.
On the other hand, a user can always repay a debt position. To repay, the user specifies the ID of the debt position and calls the repay function, which repays the loan of the specified debt position.
function executeRepay(State storage state, RepayParams calldata params) external {
DebtPosition storage debtPosition = state.getDebtPosition(params.debtPositionId);
state.data.borrowAToken.transferFrom(msg.sender, address(this), debtPosition.futureValue);
debtPosition.liquidityIndexAtRepayment = state.data.borrowAToken.liquidityIndex();
state.repayDebt(params.debtPositionId, debtPosition.futureValue);
emit Events.Repay(params.debtPositionId);
}
The issue arises when a borrower sends a transaction to repay their debt position when it becomes liquidatable, but a liquidateWithReplacement transaction is executed just before the user’s transaction. This way, the user will be liquidated, and their collateral will be used to repay debt. Since liquidateWithReplacement only changes the borrower of a debtPosition, the user ends up paying the debt of another borrower immediately, resulting in the user repaying more than double their future value (more than 2x because of liquidation rewards).
This doesn’t require a malicious keeper (who calls liquidateWithReplacement) as it’s trusted, it can happen unintentionally, and there is a pretty good chance for this to occur; as users and the liquidator (keeper bot in this case) will probably call their respective functions around the same time.
When a user notices that their position is liquidatable (probably through a UI notification), there is a high chance they will repay their debt to avoid liquidation, and the same goes for the liquidation bot. Thus, there is a good chance that the liquidateWithReplacement transaction will be executed before the user’s repayment transaction. The impact is high here, as the user ends up paying 2x their debt.
In case liquidateWithReplacement becomes permissionless and anyone can call it, this will be critically exploitable. An exploit scenario would be:
- The collateral token price drops, making Bob’s position liquidatable.
- Bob gets notified and sends a transaction to repay his debt position with
ID=0. - Alice sees Bob’s transaction in the mempool and front-runs him to liquidate the debt position with
ID=0, setting herself as the borrower (it can set any curve borrow offer). An equivalent amount to thefutureValuein terms of collateral tokens is sent from Bob’s account to Alice, and Alice receives another cash ofborrowATokensince she’s the borrower. - Bob’s transaction gets executed after that, and Bob ends up paying Alice’s debt. Bob suffers loss.
Recommended Mitigation Steps
To prevent unintended debt repayment due to borrower changes, users should specify both the borrower address and the debt position ID when repaying. This ensures users have control over who they are repaying for, avoiding scenarios where they might repay another borrower’s debt due to a recent liquidateWithReplacement transaction.
Make this changes in repay lib:
struct RepayParams {
uint256 debtPositionId;
+ address borrower;
}
function validateRepay(State storage state, RepayParams calldata params) external view {
// validate debtPositionId
if (state.getLoanStatus(params.debtPositionId) == LoanStatus.REPAID) {
revert Errors.LOAN_ALREADY_REPAID(params.debtPositionId);
}
+ if (state.getDebtPosition(params.debtPositionId).borrower != params.borrower) revert("invalid borrower");
// validate msg.sender
// N/A
}
Assessed type
Timing
aviggiano (Size) acknowledged and commented:
Feedback from the team:
This looks valid since it does not look like a user mistake, it is a kind of MEV. In practice, I think it can happen rarely since for this to happen we need this to be true:
When a user notices that their position is liquidatable (probably through a UI notification), there is a high chance they will repay their debt to avoid liquidation,
In my honest opinion, there is a high chance they deposit more collateral or compensate to avoid liquidation, since repaying early would mean losing all the interest; so not a great way to do that (even though in some cases it could be the only chance). Technically valid, but practically low impact.
Fixed in https://github.com/SizeCredit/size-solidity/pull/141.
I will keep as High because the impact is critical and the likelihood is not rare.
This is an interesting case, but, a high severity seems to be overgenerous because of the pre-conditions that needs to exist for this to become a problem.
- There needs to be a borrower for the APR and Tenor of the liquidated Position.
- The borrower being liquidated needs to attempt to repay the same position being liquidated (Borrower can have multiple debt positions and can attempt to repay any of the others, not necessarily the exact position being liquidated and replaced).
- The liquidate and replacement transaction must be executed BEFORE than the repay tx.
@hansfriese - because of the multiple on-chain conditions, this seems tofit more of a medium severity.
I agree that it requires some prerequisites. However, with a liquidatable position, it is not that strong assumption that those 2 functions could be called simultaneously. According to the C4 severity categorization, High is more appropriate due to the direct fund loss and the likelihood of this scenario occurring.
MotokoKusanagi-aka-Major (Size) commented:
This issue arises from a potential race condition involving two function calls regarding a debt position eligible for liquidation:
- A borrower calling
repay()to avoid the liquidation and- The protocol backend calling
liquidateWithReplacement()to liquidate the position with replacement (this is a permissioned method).Since there was no check regarding the ownership of that debt, any address was allowed to repay the debt of any other address so if the
liquidateWithReplacement()was executed first, the borrower would have ended up being liquidated and repaying someone else debt, because of the replacement.The impact is certainly high but the likelihood of this issue happening in our opinion has been largely overestimated, and is for all practical purposes, non-existent.
The reason is that a borrower eligible for liquidation has no incentive to repay loans to avoid liquidation since he would forfeit the interest for the remaining lifetime of the loan. In that case, what makes more sense is for them to increase their collateral ratio by
- Depositing more collateral and
- Compensating some of their debt with eligible credit
The race between 1 and 2 with
liquidateWithReplacement()is not associated with any issue.The only case where the borrower can make sense to repay his loan early is when the forfeited interests would be negligible, meaning at the end of the loan lifecycle. However, in that case, the replacement is extremely unlikely to happen since it would require a borrower willing to borrow passively for a very short tenor. This is very unlikely to happen as a borrower would need some time to react to his limit borrow order being filled and quickly decide what to do with the borrowed money, before this new loan goes overdue and he becomes eligible for liquidation.
On top of this, the
liquidateWithReplacement()method is permissioned and we have a negligible economic incentive in running it vs a standard liquidation. This is because the latter is unaffected by the issue mentioned above since the value the protocol captures from running a liquidation with replacement is proportional to the remaining loan lifetime. It would therefore be a net loss to useliquidateWithReplacement()instead ofliquidate()for a loan very close to maturity as we would gain almost nothing while potentially significantly affecting our users.
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
The repay function only checks the debt position ID and does not verify that the caller is the current borrower, so after liquidateWithReplacement changes the borrower, a repayment from the original borrower pays the new borrower's debt.
Impact
A borrower could end up repaying more than double their future value, though the team argues the likelihood is very low because borrowers rarely repay early to avoid liquidation.
What the attacker needed
A liquidatable debt position; borrower attempts to repay that position; liquidateWithReplacement executes first; keeper role (permissioned)
The fix
The repay function should require the caller to specify both the borrower address and the debt position ID, and validate that the borrower matches the current debt position owner.
How it was found
- The collateral token price drops, making Bob’s position liquidatable.
- Bob sends a transaction to repay his debt position with ID=0.
- Alice sees Bob’s transaction in the mempool and front-runs him to liquidate the debt position with ID=0, setting herself as the borrower.
- Bob’s transaction gets executed after that, and Bob ends up paying Alice’s debt.
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 system, a debt position has an ID and a borrower. A keeper can liquidate a risky position and replace the borrower with a new one, keeping the same ID. If the original borrower tries to repay at the same time, their repayment may go toward the new borrower's debt instead of their own, so they lose money.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.