Unable to adjust position in some cases
The adjust function in Position.sol reverts when the new price is higher than the current price and the caller tries to reduce collateral or increase minted amount, because a cooldown is set that blocks those operations. An attacker could not exploit this; it is a usability bug that prevents legitimate position adjustments.
Submitted by John, also found by 4710710N
The adjust function in Position.sol is designed to adjust the outstanding amount of ZCHF, the collateral amount, and the price in a single transaction. However, there are certain cases where this function always reverts. Assuming the new price is greater than the current price, if the value of newCollateral is less than colbal or the value of newMinted is greater than minted, the adjust function will always revert with a customized error message reading Hot.
Proof of Concept
If the value of newPrice is great than value of price, the restrictMinting function is triggered. In this case, if the value of cooldown exceeds block.timestamp + 3 days, cooldown will be update to block.timestamp + 3 days, therefore, both the withdrawCollateral and mint functions will be reverted with custom error because of noCooldown modifier.
https://github.com/code-423n4/2023-04-frankencoin/blob/main/contracts/Position.sol#L132-L152
https://github.com/code-423n4/2023-04-frankencoin/blob/main/contracts/Position.sol#L159-L167
I will share the test code
it("Will revert adjest tx when newPrice is greater than prevPrice", async () => {
let collateral = mockVOL.address;
let fliqPrice = floatToDec18(1000);
let minCollateral = floatToDec18(1);
let fInitialCollateral = floatToDec18(initialCollateral);
let duration = BN.from(14*86_400);
let fFees = BN.from(fee * 1000_000);
let fReserve = BN.from(reserve * 1000_000);
let openingFeeZCHF = await mintingHubContract.OPENING_FEE();
let challengePeriod = BN.from(7 * 86400); // 7 days
await mockVOL.connect(accounts[0]).approve(mintingHubContract.address, fInitialCollateral);
let balBefore = await ZCHFContract.balanceOf(owner);
let balBeforeVOL = await mockVOL.balanceOf(owner);
let tx = await mintingHubContract["openPosition(address,uint256,uint256,uint256,uint256,uint256,uint32,uint256,uint32)"]
(collateral, minCollateral, fInitialCollateral, initialLimit, duration, challengePeriod, fFees, fliqPrice, fReserve);
let rc = await tx.wait();
const topic = '0x591ede549d7e337ac63249acd2d7849532b0a686377bbf0b0cca6c8abd9552f2'; // PositionOpened
const log = rc.logs.find(x => x.topics.indexOf(topic) >= 0);
positionAddr = log.address;
let balAfter = await ZCHFContract.balanceOf(owner);
let balAfterVOL = await mockVOL.balanceOf(owner);
let dZCHF = dec18ToFloat(balAfter.sub(balBefore));
let dVOL = dec18ToFloat(balAfterVOL.sub(balBeforeVOL));
expect(dVOL).to.be.equal(-initialCollateral);
expect(dZCHF).to.be.equal(-dec18ToFloat(openingFeeZCHF));
positionContract = await ethers.getContractAt('Position', positionAddr, accounts[0]);
console.log("price:",await positionContract.price());
console.log("minted:",await positionContract.minted());
await ethers.provider.send('evm_increaseTime', [7 * 86_400 + 60]);
await ethers.provider.send("evm_mine");
let erx = positionContract.adjust(1, floatToDec18(8), floatToDec18(1200))
await expect(erx).to.be.revertedWithCustomError(positionContract, "Hot");
console.log("price:",await positionContract.price());
console.log("minted:",await positionContract.minted());
});
Recommended Mitigation Steps
To solve this problem, we can modify the “adjust” function like this;
function adjust(uint256 newMinted, uint256 newCollateral, uint256 newPrice) public onlyOwner {
uint256 colbal = collateralBalance();
if (newCollateral > colbal){
collateral.transferFrom(msg.sender, address(this), newCollateral - colbal);
}
// Must be called after collateral deposit, but before withdrawal
if (newMinted < minted){
zchf.burnFrom(msg.sender, minted - newMinted, reserveContribution);
minted = newMinted;
}
if (newCollateral < colbal){
withdrawCollateral(msg.sender, colbal - newCollateral);
}
// Must be called after collateral withdrawal
if (newMinted > minted){
mint(msg.sender, newMinted - minted);
}
if (newPrice != price){
adjustPrice(newPrice);
}
}
Might be a design choice, need sponsor’s input on this one.
luziusmeisser (Frankencoin) confirmed and commented:
I wouldn’t call this a ‘vulnerability’ but convenience is definitely improved by the recommended mitigation.
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 adjust function calls restrictMinting when the price increases, which sets a cooldown that prevents withdrawing collateral or minting more, causing the subsequent operations to revert.
Impact
Legitimate users cannot adjust their positions in certain cases, but this is not a security vulnerability and does not allow theft or loss of funds.
What the attacker needed
Own a position; new price higher than current price; either reduce collateral or increase minted
The fix
The adjust function should be modified to perform collateral deposit, minted burning, collateral withdrawal, and minting in a specific order that avoids the cooldown restriction, as shown in the recommended mitigation.
How it was found
- Open a position with some collateral and minted amount.
- Wait for the cooldown period to pass.
- Call adjust with a higher new price and either lower collateral or higher minted amount.
- The transaction reverts with the custom error 'Hot'.
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.
This is a smart contract bug where a function that is supposed to let you change your loan position fails in certain cases. When you try to lower your collateral or increase your debt after raising the price, the contract blocks it because of a safety timer, so you cannot adjust your position as intended.
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.