Skip to content
Business logic Audit finding Intermediate

OpenLevV1.closeTrade with V3 DEX doesn’t correctly accounts fee on transfer tokens for repayments

The contract uses the DEX's reported swap output as the actual amount received, but for fee-on-transfer tokens the actual received amount is less. An attacker could exploit this to drain contract funds or cause position closes to fail, freezing user funds.

hyh Follow

Originally disclosed on Code4rena

Submitted by hyh

The amount that OpenLevV1 will receive can be less than V3 DEX indicated as a swap result, while it is used as given for position debt repayment accounting.

This way actual funds received can be less than accounted, leaving to system funds deficit, which can be exploited by a malicious user, draining contract funds with multiple open/close with a taxed token.

In the trade.depositToken != longToken case when flashSell is used this can imply inability to send remainder funds to a user and the failure of the whole closeTrade function, the end result is a freezing of user’s funds within the system.

Proof of Concept

trade.depositToken != longToken case, can be wrong repayment accounting, which will lead to a deficit if the received funds are less than DEX returned closeTradeVars.receiveAmount.

As a side effect, doTransferOut is done without balance check, so the whole position close can revert, leading to inability to close the position and freeze of user’s funds this way:

https://github.com/code-423n4/2022-01-openleverage/blob/main/openleverage-contracts/contracts/OpenLevV1.sol#L197-204

I.e. if there is enough funds in the system they will be drained, if there is not enough funds, user’s position close will fail.

V3 sell function doesn’t check for balance change, using DEX returned amount as is:

https://github.com/code-423n4/2022-01-openleverage/blob/main/openleverage-contracts/contracts/dex/eth/UniV3Dex.sol#L61-70

Recommended Mitigation Steps

If fee on tranfer tokens are fully in scope, do control all the accounting and amounts to be returned to a user via balance before/after calculations for DEX V3 logic as well.

ColaM12 (OpenLeverage) confirmed and resolved

0xleastwood (judge) commented:

Awesome find. I was able to confirm that UniV3Dex.uniV3Sell() does not properly handle fee-on-transfer tokens by treating the amount received as the difference between before balance and after balance.


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

The contract used the DEX's reported swap output as the actual received amount without checking the balance change, which is incorrect for fee-on-transfer tokens.

Impact

A malicious user could drain contract funds with multiple open/close operations using a taxed token, or cause position closes to fail and freeze user funds.

What the attacker needed

Fee-on-transfer token; multiple open/close operations

The fix

The accounting and amounts returned to users should be controlled via balance before/after calculations for DEX V3 logic as well.

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.

Some tokens take a fee when transferred, so the receiver gets less than the sender sent. The contract assumed it got the full amount reported by the exchange, so its accounting was wrong. This let an attacker repeatedly open and close positions to drain funds, or made it impossible for users to close positions.

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 OpenLeverage 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.