Lend offer can be deleted multiple times
A lend offer in perpetual mode with zero available amount can be deleted multiple times because the changePerpetual function does not set isActive to false. This repeatedly decrements activeOrdersCount, eventually preventing other lend offers from being deleted.
Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/119
Found by
0x37, 0xAristos, 0xSolus, KlosMitSoss, Vidus, bbl4de, copperscrewer, liquidbuddha, newspacexyz, onthehunt, theweb3mechanic
Summary
Lack of check in addFunds() function. This will cause one lend offer can be deleted twice.
Root Cause
In DebitaLendOffer-Implementation:178, there is one perpetual mode.
Considering one scenario: The lend offer is in perpetual mode and current availableAmount equals 0. Now when we try to change perpetual to false, we will delete this lend order.
The problem is that we lack updating isActive to false in changePerpetual(). This will cause that the owner can trigger changePerpetual multiple times to delete the same lend order.
When we repeat deleting the same lend order in deleteOrder, we will keep decreasing activeOrdersCount. This will impact other lend offer. Other lend offers may not be deleted.
function changePerpetual(bool _perpetual) public onlyOwner nonReentrant {
require(isActive, "Offer is not active");
lendInformation.perpetual = _perpetual;
if (_perpetual == false && lendInformation.availableAmount == 0) {
IDLOFactory(factoryContract).emitDelete(address(this));
IDLOFactory(factoryContract).deleteOrder(address(this));
} else {
IDLOFactory(factoryContract).emitUpdate(address(this));
}
}
function deleteOrder(address _lendOrder) external onlyLendOrder {
uint index = LendOrderIndex[_lendOrder];
LendOrderIndex[_lendOrder] = 0;
// switch index of the last borrow order to the deleted borrow order
allActiveLendOrders[index] = allActiveLendOrders[activeOrdersCount - 1];
LendOrderIndex[allActiveLendOrders[activeOrdersCount - 1]] = index;
// take out last borrow order
allActiveLendOrders[activeOrdersCount - 1] = address(0);
activeOrdersCount--;
}
Internal pre-conditions
N/A
External pre-conditions
N/A
Attack Path
- Alice creates one lend order with perpetual mode.
- Match Alice's lend order to let
availableAmountto 0. - Alice triggers
changePerpetualrepeatedly to letactiveOrdersCountto 0. - Other lend orders cannot be deleted.
Impact
All lend orders cannot be deleted. This will cause that lend order cannot be cancelled or may not accept this lend offer if we want to use the whole lend order's principle.
PoC
N/A
Mitigation
When we delete the lend order, we should set it to inactive. This will prevent changePerpetual() retriggered repeatedly.
function changePerpetual(bool _perpetual) public onlyOwner nonReentrant {
require(isActive, "Offer is not active");
lendInformation.perpetual = _perpetual;
if (_perpetual == false && lendInformation.availableAmount == 0) {
+ isActive = false;
IDLOFactory(factoryContract).emitDelete(address(this));
IDLOFactory(factoryContract).deleteOrder(address(this));
} else {
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/307e2360dd9aaac443f17547466c51718d4cefd3
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 changePerpetual function does not set the isActive flag to false when deleting the lend order, allowing repeated deletion calls.
Impact
All lend orders cannot be deleted, so they cannot be cancelled or may not accept the full principal.
What the attacker needed
Attacker owns a lend offer; offer is in perpetual mode; availableAmount is zero
The fix
The protocol team fixed this issue by setting isActive to false when deleting the lend order.
How it was found
- Alice creates one lend order with perpetual mode.
- Match Alice's lend order to let availableAmount to 0.
- Alice triggers changePerpetual repeatedly to let activeOrdersCount to 0.
- Other lend orders cannot be deleted.
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.
A smart contract lets someone create a lend offer. If the offer is set to 'perpetual' and has no funds left, the owner can turn off perpetual mode, which deletes the offer. But the contract forgets to mark the offer as inactive, so the owner can repeat this deletion many times. Each deletion reduces a counter that tracks active offers, and if the counter goes to zero, other offers can no longer be deleted.
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.