The return value of buyAllAmount is incorrect
The buyAllAmount function returns the amount spent after subtracting the fee, rather than the gross amount actually spent. A caller relying on the return value would understate how much they paid, for example reporting 9,000 instead of 10,000 tokens spent.
Submitted by cccz, also found by cducrest, ElKu and bytes032.
buyAllAmount will return the amount of tokens spent on the exchange, but the actual return value is less than the actual amount of tokens spent.
Consider WBTC price is 10000 USDC and the total fee is 10%. The user calls buyAllAmount to buy 1 WBTC, and the calculated fill_amt is 10,000 USDC.
function buyAllAmount(
ERC20 buy_gem,
uint256 buy_amt,
ERC20 pay_gem,
uint256 max_fill_amount
) external returns (uint256 fill_amt) {
require(!locked);
uint256 offerId;
while (buy_amt > 0) {
//Meanwhile there is amount to buy
offerId = getBestOffer(buy_gem, pay_gem); //Get the best offer for the token pair
require(offerId != 0, "offerId == 0");
// There is a chance that buy_amt is smaller than 1 wei of the other token
if (
mul(buy_amt, 1 ether) <
wdiv(offers[offerId].pay_amt, offers[offerId].buy_amt)
) {
break; //We consider that all amount is sold
}
if (buy_amt >= offers[offerId].pay_amt) {
//If amount to buy is higher or equal than current offer amount to sell
fill_amt = add(fill_amt, offers[offerId].buy_amt); //Add amount sold to acumulator
buy_amt = sub(buy_amt, offers[offerId].pay_amt); //Decrease amount to buy
take(bytes32(offerId), uint128(offers[offerId].pay_amt)); //We take the whole offer
} else {
//if lower
fill_amt = add(
fill_amt,
rmul(
mul(buy_amt, 10 ** 9),
rdiv(offers[offerId].buy_amt, offers[offerId].pay_amt)
) / 10 ** 9
); //Add amount sold to acumulator
take(bytes32(offerId), uint128(buy_amt)); //We take the portion of the offer that we need
buy_amt = 0; //All amount is bought
}
In the buy function, the 1 WBTC to be purchased minus the fee is 0.9 WBTC. That is, the user actually spends 10,000 USDC to buy 0.9 WBTC, and the fill_amt finally returned is 10000 USDC minus the fee is 9000 USDC, but the user actually spends 10000 USDC, which causes the return value of buyAllAmount to be incorrect.
require(
fill_amt <= max_fill_amount,
"fill_amt exceeds max_fill_amount"
);
fill_amt = calcAmountAfterFee(fill_amt);
}
Proof of Concept
Recommended Mitigation Steps
Change to:
require(
fill_amt <= max_fill_amount,
"fill_amt exceeds max_fill_amount"
);
- fill_amt = calcAmountAfterFee(fill_amt);
}
It’s not a duplicate, as it’s not about incorrect calculation. It’s about the return amount being exclusive of fees when it should.
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 deducted from the returned fill amount at the end of buyAllAmount, so the return value no longer reflects the gross amount spent.
Impact
Callers receive a return value lower than the amount actually spent, which could mislead any logic that relies on it.
What the attacker needed
Calling buyAllAmount on the market contract
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 function can report back a number to whoever calls it. Here the function that buys tokens reports the amount spent after a fee has been taken off, so the number is smaller than what the user really paid. Anyone using that number for accounting or limits would be working from a wrong figure.
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.