Skip to content
Business logic Audit finding Intermediate

Fee double counting for underwater positions

The contract records fees before checking whether a position is underwater, so fees are counted even when the position's value is fully consumed by debt. This can cause fee withdrawal to burn or transfer more than the actual available fees, leading to failed withdrawals or fund leakage.

hyh Follow

Originally disclosed on Code4rena

Submitted by hyh

Impact

Actual available fees are less than recorded. That’s because a part of them corresponds to underwater positions, and will not have the correct amount stored with the contract: when calculation happens the fee is recorded first, then there is a check for position health, and the funds are channeled to cover the debt firsthand. This way in a case of unfunded position the fee is recorded, but cannot be allocated, so the fees accounted can be greater than the value of fees stored.

This can lead to fee withdrawal malfunction, i.e. disburse() will burn more and attempt to transfer more than needed. This leads either to inability to withdraw fees when disburse is failing due to lack of funds, or funds leakage to fees and then inability to perform other withdrawals because of lack of funds.

Proof of Concept

The fees are accounted for before position health check and aren’t corrected thereafter when there is a shortage of funds.

https://github.com/code-423n4/2021-11-overlay/blob/main/contracts/collateral/OverlayV1OVLCollateral.sol#L311

Recommended Mitigation Steps

Adjust fees after position health check: accrue fees only on a remaining part of position that is available after taking debt into account.

Now:

uint _feeAmount = _userNotional.mulUp(mothership.fee());

uint _userValueAdjusted = _userNotional - _feeAmount;
if (_userValueAdjusted > _userDebt) _userValueAdjusted -= _userDebt;
else _userValueAdjusted = 0;

To be:

uint _feeAmount = _userNotional.mulUp(mothership.fee());

uint _userValueAdjusted = _userNotional - _feeAmount;
if (_userValueAdjusted > _userDebt) {
    _userValueAdjusted -= _userDebt;
} else {
    _userValueAdjusted = 0;
    _feeAmount = _userNotional > _userDebt ? _userNotional - _userDebt : 0;
}

mikeyrf (Overlay) confirmed

The original report as published on Code4rena, by hyh. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 fee is accrued on the full notional amount before the position's debt is subtracted, and is not adjusted when the remaining value is zero.

Impact

Fee withdrawal may malfunction, causing either failed withdrawals due to lack of funds or leakage of funds to fees, which then prevents other withdrawals.

What the attacker needed

Position is underwater (debt exceeds value)

The fix

The fee should be adjusted after the position health check so that fees are only accrued on the remaining value after accounting for 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 a smart contract, fees are supposed to be set aside when a position is closed. But here, the fee is calculated and saved before checking if the position has enough money to cover its debt. If the position is 'underwater' (owes more than it's worth), the fee is still recorded even though there's no money to back it. Later, when someone tries to withdraw fees, the contract may try to send more than it actually has, causing the withdrawal to fail or money to be lost.

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.

More by hyh

View all

137 more disclosed reports, best first.

More from Overlay Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.