Skip to content
Business logic Audit finding Intermediate

Users are credited more tokens when paying back debt with registerTradeAndBorrow

The registerTradeAndBorrow function pays back a user's debt with the trade output but then credits the full output amount again as a deposit, so the user is credited twice. An attacker could repeatedly withdraw the extra amount until the contract's funds are drained.

Marginswap

Originally disclosed on Code4rena

The registerTradeAndBorrow is called with the results of a trade (inAmount, outAmount). It first tries to pay back any debt with the outAmount. However, the full outAmount is credited to the user again as a deposit in the adjustAmounts(account, tokenFrom, tokenTo, sellAmount, outAmount); call. As the user pays back their debt and is credited the same amount again, they are essentially credited twice the outAmount, making a profit of one outAmount. This can be withdrawn and the process can be repeated until the funds are empty.

In the adjustAmounts call, it should only credit outAmount - extinguishableDebt as a deposit like in registerDeposit. The registerDeposit function correctly handles this case.

The original report as published on Code4rena. 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 code credits the full outAmount as a deposit after already using it to repay debt, instead of crediting only the remainder after debt repayment.

Impact

An attacker could repeatedly withdraw the duplicated credit and drain the contract's funds.

What the attacker needed

Ability to call registerTradeAndBorrow; a trade producing outAmount

The fix

The adjustAmounts call should credit only the trade output left after debt repayment, as registerDeposit already does.

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 users trade and borrow. When a trade makes money, part of it should go to repaying what the user owes. Here the code used the whole trade output to repay debt and then also added the whole amount to the user's balance, so the user gained money that was never really theirs. Repeating this lets them take funds from the contract.

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 from Marginswap

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.