Skip to content
Business logic Audit finding Intermediate

Users could overpay fees when buying votes

The buyVotes function calculates fees on the total funds a user sends rather than on the actual amount spent on votes, so users can overpay fees when the vote price changes before their transaction executes. An attacker could exploit this by front-running a buy to cause the victim to pay more in fees than necessary.

0x486776 Follow with 0xPhantom2, 0xaxaxa and 24 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-ethos-network-ii-judging/issues/314

Found by

0x486776, 0xPhantom2, 0xaxaxa, 0xc0ffEE, 0xlucky, Abhan1041, BengalCatBalu, DenTonylifer, John44, Ryonen, X12, aycozyOx, bughuntoor, chaos304, dobrevaleri, hals, irresponsible, newspacexyz, novaman33, pashap9990, smbv-1923, tmotfl, underdog, vatsal, wellbyt3, ydlee, zxriptor

Summary

The previewFees function in _calculateBuy is applied to the total funds being transferred by the user. This leads to users paying for more funds that they are actually transacting.

Root Cause

In ReputationMarket:960, users will specify an amount of funds that they are willing to pay in exchange for votes. However, the specified funds might not be fully used in order to buy the votes, given the following logic in _calculateBuy:

Solidity
// ReputationMarket.sol

function _calculateBuy(
    Market memory market,
    bool isPositive,
    uint256 funds
  )
    private
    view
    returns (
      uint256 votesBought,
      uint256 fundsPaid,
      uint256 newVotePrice,
      uint256 protocolFee,
      uint256 donation,
      uint256 minVotePrice,
      uint256 maxVotePrice
    )
  {
    uint256 fundsAvailable;
    (fundsAvailable, protocolFee, donation) = previewFees(funds, true); 
    uint256 votePrice = _calcVotePrice(market, isPositive);

    uint256 minPrice = votePrice;
    uint256 maxPrice;

    if (fundsAvailable < votePrice) {
      revert InsufficientFunds();
    }

    while (fundsAvailable >= votePrice) {
      fundsAvailable -= votePrice;
      fundsPaid += votePrice;
      votesBought++;

      market.votes[isPositive ? TRUST : DISTRUST] += 1;
      votePrice = _calcVotePrice(market, isPositive);
    }
    fundsPaid += protocolFee + donation;

    maxPrice = votePrice;

    return (votesBought, fundsPaid, votePrice, protocolFee, donation, minPrice, maxPrice);
  }

As shown in the snippet, the amount of votes bought is determined by a loop that will run while the fundsAvailable are greater than the votePrice. As the price of votes increases due to the buy pressure, the loop will be finished, having fundsAvailable (which consists of the user's submitted funds with the fees substracted) be nearly always greater than the actual fundsPaid (which consists of the price paid for each vote + fees + donation fee).

The problem with this approach is that fees are being applied to an amount that, as demonstrated, is not necessarily the total amount used to actually buy the votes.

Internal pre-conditions

None

External pre-conditions

None

Attack Path

Let's say a user 1 wants to buy 2 TRUST votes for a recently created market, where basePrice is 0,01 ETH and theres 1 TRUST and 1 DISTRUST vote already in the market.

  1. The price of one vote is given by market.votes[isPositive ? TRUST : DISTRUST] * market.basePrice) / totalVotes, so the price is 0,005 ETH (1 * 0,01 / 2) for the first vote, and ≈ 0,0066 (2 * 0,01 / 3) for the second vote, which adds up to a total of 0,0116 ETH. A 10 % fee is applied (so fee is 0,00116), so the total that user should deposit is 0,01276.

  2. At the same time, and prior to user 1 buying the votes, user 2 submits a buy transaction for one TRUST vote. This leaves the state of the vote prices to a different price than the expected by user 1. Still, user 1 submits the transaction with a value of 0,01276.

  3. Because user 2 has triggered the buy operation prior to user 1, the initial vote price for user 1 is 0,0066 ETH (2 * 0,01 / 3). As user 1 submitted 0,01276 as funds, and without the 10% in fees, the fundsAvailable for user 1 are 0,011484. Note that 0,001276 are paid in fees.

  4. While in the loop:

  • First iteration: votePrice starts at 0,0066, and fundsAvailable are 0,011484, so one vote is purchased. The fundsPaid increases to 0,0066.
  • Second iteration: votePrice now is at 0,0075 (3 * 0,01 / 4). fundsAvailable are 0,004884, so it is not enough to buy a second vote
  1. The result is that user 1 has only been able to purchase a single vote. The actual transacted value has only been 0,0066 (the price of one vote), for which a 10% fee would have implied 0,00066 ETH (≈ 2,442 USD). However, as shown in step 1, user 1 has paid 0,00116 ETH (≈ 4,292 USD, nearly the double!).

On the long run, situations like this will arise, leading to a perpetual loss of funds for protocol users due to the increase

Impact

Medium. As shown in the "Attack Path" section, fees will be overcharged for users that buy votes. On the long term, it is easy that the total value loss exceeds 10 USD or 10% of user value, (considering that new markets might also be configured, with base prices of 0,1 or even 1 ETH).

PoC

No response

Mitigation

When triggering buyVotes, allow users to specify the amount of votes they want, instead of the amount of ETH to pay (similar to the logic used when selling). Then, apply the corresponding fees considering the actual amount that user will pay.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/trust-ethos/ethos/pull/2214

The original report as published on Sherlock, by 0x486776. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 calculated on the total funds provided by the user before determining how many votes can actually be purchased, rather than on the actual funds spent on votes.

Impact

Users could overpay fees when buying votes, leading to a loss of funds that may exceed 10 USD or 10% of user value over time.

What the attacker needed

User buys votes; another user buys votes before them

The fix

The protocol team fixed this issue in PR #2214.

How it was found

  1. User 1 wants to buy 2 TRUST votes and calculates the required funds including fees based on current vote prices.
  2. User 2 submits a buy transaction for one TRUST vote before user 1's transaction, changing the vote prices.
  3. User 1's transaction executes with the original funds amount, but due to higher prices, only one vote is purchased.
  4. The fee is still calculated on the full funds amount, so user 1 pays nearly double the fee compared to the actual vote cost.

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 votes, you send some money (ETH). The contract takes a fee based on the total money you sent, not on how much it actually uses to buy votes. If the price of votes goes up before your transaction, you might not be able to buy as many votes as you expected, but you still pay the fee on the full amount you sent. This means you pay more in fees than you should.

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 by 0x486776

View all

7 more disclosed reports, best first.

More from Ethos Network Financial Contracts

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.