Wrong timing of check allows users to withdraw collateral without paying for the debt
Submitted by WatchPug, also found by IllIllI
function pay(PayParam calldata param)
external
override
lock
returns (
uint128 assetIn,
uint128 collateralOut
)
{
require(block.timestamp < param.maturity, 'E202');
require(param.owner != address(0), 'E201');
require(param.to != address(0), 'E201');
require(param.to != address(this), 'E204');
require(param.ids.length == param.assetsIn.length, 'E205');
require(param.ids.length == param.collateralsOut.length, 'E205');
Pool storage pool = pools[param.maturity];
Due[] storage dues = pool.dues[param.owner];
require(dues.length >= param.ids.length, 'E205');
for (uint256 i; i < param.ids.length;) {
Due storage due = dues[param.ids[i]];
require(due.startBlock != BlockNumber.get(), 'E207');
if (param.owner != msg.sender) require(param.collateralsOut[i] == 0, 'E213');
require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303');
due.debt -= param.assetsIn[i];
due.collateral -= param.collateralsOut[i];
assetIn += param.assetsIn[i];
collateralOut += param.collateralsOut[i];
unchecked { ++i; }
}
...
At L484, if there is only one id, and for the first and only time of the for loop, assetIn and collateralOut will be 0, therefore require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303'); will pass.
A attacker can call pay() with param.assetsIn[0] == 0 and param.collateralsOut[i] == due.collateral.
Proof of Concept
The attacker can:
borrow()10,000 USDCwith1 BTCascollateral;pay()with0 USDCasassetsInand1 BTCascollateralsOut.
As a result, the attacker effectively stole 10,000 USDC.
Recommended Mitigation Steps
Change to:
for (uint256 i; i < param.ids.length;) {
Due storage due = dues[param.ids[i]];
require(due.startBlock != BlockNumber.get(), 'E207');
if (param.owner != msg.sender) require(param.collateralsOut[i] == 0, 'E213');
due.debt -= param.assetsIn[i];
due.collateral -= param.collateralsOut[i];
assetIn += param.assetsIn[i];
collateralOut += param.collateralsOut[i];
unchecked { ++i; }
}
require(uint256(assetIn) * due.collateral >= uint256(collateralOut) * due.debt, 'E303');
...
Mathepreneur (Timeswap) resolved and commented:
0xleastwood (judge) commented:
This is an interesting find. It appears that
assetInandcollateralOutare not checked properly during the first iteration of the for loop. As a result, this functionality of this function is inherently broken as therequirestatement will always be satisfied. Nice job!
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A high-severity business logic bug in Timeswap contest. Code4rena made it public on 17 May 2022. Found in the 2022-03-timeswap audit contest (finding H-01).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.