code-423n4 / 2021-04-marginswap-findings

1 stars 0 forks source link

Users are credited more tokens when paying back debt with `registerTradeAndBorrow` #24

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Email address

mail@cmichel.io

Handle

@cmichelio

Eth address

0x6823636c2462cfdcD8d33fE53fBCD0EdbE2752ad

Vulnerability details

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.

Impact

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.

Recommended mitigation steps

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