Incorrect formula for calculating depositAmount
value inside abortBidTaker
function of PreMarkets
contract may result in loss of user's funds.
Inside the abortBidTaker
function of the PreMarkets
contract there is an error in the formula for calculating the depositAmount
value.
Code fragment of the abortBidTaker
function where the mistake occurred:
The mistake in the formula is that preOfferInfo.points
and preOfferInfo.amount
should be swapped around.
Consider the following case as an example:
User creates the offer to sell 1000 points for 10 ERC20 tokens. This token has 18 decimals.
Next, some User2 decides to buy 250 points. To do this, he calls createTaker and pays 2.5 ERC20 tokens.
Next, the User who created the original offer decides to abort it. To do this, he calls the abortAskOffer
function.
Then User2 decides to abort the buy offer and calls abortBidTaker
function with the hope of getting his 2.5 ERC20 tokens back, but due to a mistake in the formula the user will get back 0 tokens and a Finished buy offer. That is, he will lose his money.
The user will get 0 because the formula will have the following calculations:
depositAmount = stockInfo.points * preOfferInfo.points / preOfferInfo.amount
depositAmount = 250 * 1000 / (10 * 10^18) = 0
What should be the calculations in the correct formula:
depositAmount = stockInfo.points * preOfferInfo.amount / preOfferInfo.points
depositAmount = 250 * (10 * 10^18) / 1000 = 2500000000000000000 (2.5 ERC20 tokens)
To run the test, its code should be placed in the PreMarkets.t.sol file.
Due to an error in the formula refund amount will be incorrectly calculated, which will result in loss of user funds.
The vulnerability was discovered during a manual audit of the protocol's contract code. To test the vulnerability for validity and demonstrate it, a unit test was written.
The formula simply needs to be corrected.
Here's the correct code:
Valid high severity, due to incorrect computation of `depositAmount` within `abortBidTaker`, when aborting bid offers created by takers, the collateral refund will be completely wrong for the taker, and depending on the difference between the value of `points` and `amount`, it can possibly even round down to zero, causing definite loss of funds. If not, if points were worth less than the collateral, this could instead be used to drain the CapitalPool contract instead.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.