Incorrect rounding in the `ReputationMarket._calcCost()` function
The cost calculation in the ReputationMarket contract rounds based on whether the vote is positive (trust) rather than whether the action is a buy or sell. This causes rounding up when selling distrust votes, which can decrement market funds by a small amount and potentially prevent the last vote from being sold.
Source: https://github.com/sherlock-audit/2024-12-ethos-update-judging/issues/66
Found by
0xaxaxa, 0xnegan, Al-Qa-qa, Benterkii, JohnTPark24, X12, bughuntoor, dobrevaleri, future2_22, moray5554, theodore, tnquanghuy0512, whitehair0330, zxriptor
Summary
When buying and selling, the cost is calculated using rounding based on isPositive. However, the rounding should be based on isBuy, not isPositive.
Root Cause
The _calcCost() function calculates cost by rounding based on isPositive.
If isPositive is false (indicating that DISTRUST votes are being traded), the calculation is rounded up.
Consider the following scenario:
- A user buys 2
DISTRUSTvotes. - The user sells a
DISTRUSTvote. - The user sells another
DISTRUSTvote.
During the buying process, rounding up occurs once, but when selling, rounding up occurs twice—at steps 2 and 3. As a result, marketFunds will be decremented by a dust amount.
If marketFunds was originally 0 (with the market created by the admin at a 0 creation cost), then step 3 becomes impossible.
In fact, isPositive is never related to the rounding direction.
function _calcCost(
Market memory market,
bool isPositive,
bool isBuy,
uint256 amount
) private pure returns (uint256 cost) {
...
int256 costRatio = LMSR.getCost(
market.votes[TRUST],
market.votes[DISTRUST],
voteDelta[0],
voteDelta[1],
market.liquidityParameter
);
uint256 positiveCostRatio = costRatio > 0 ? uint256(costRatio) : uint256(costRatio * -1);
// multiply cost ratio by base price to get cost; divide by 1e18 to apply ratio
cost = positiveCostRatio.mulDiv(
market.basePrice,
1e18,
1057 isPositive ? Math.Rounding.Floor : Math.Rounding.Ceil
);
}
Internal pre-conditions
External pre-conditions
Attack Path
Impact
The last vote might not be sold.
PoC
Mitigation
Use !isBuy instead of isPositive.
cost = positiveCostRatio.mulDiv(
market.basePrice,
1e18,
- isPositive ? Math.Rounding.Floor : Math.Rounding.Ceil
+ !isBuy ? Math.Rounding.Floor : Math.Rounding.Ceil
);
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/trust-ethos/ethos/pull/2387
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
Impact
The market funds may be reduced by a dust amount, and if the market was created with zero funds, the last vote might not be sellable.
What the attacker needed
Market with DISTRUST votes; market funds low or zero
The fix
The rounding should be based on !isBuy instead of isPositive, and the protocol team fixed this issue.
How it was found
- A user buys 2 DISTRUST votes.
- The user sells a DISTRUST vote.
- The user sells another DISTRUST vote.
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.
When you buy or sell votes in this market, the contract calculates a cost and rounds it to a whole number. The code mistakenly uses the type of vote (trust or distrust) to decide whether to round up or down, instead of whether you are buying or selling. This can cause the market to lose a tiny amount of funds each time someone sells distrust votes, and in rare cases, the last vote cannot be sold.