Skip to content
DoS Audit finding Intermediate

Determining how many votes to buy may run OOG

The contract uses a linear loop to find the maximum number of votes a user can buy, which is gas-inefficient. An attacker could cause transactions to run out of gas by buying a large number of votes, making them impossible to execute or very expensive.

bughuntoor Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-12-ethos-update-judging/issues/43

Found by

bughuntoor

Summary

The current way buying votes works is that a user sends a certain msg.value and a minimum and maximum amount they wish to buy, and the contract loops through the values to find the maximum amount the user can actually buy.

Solidity
    (, , , uint256 total) = _calculateBuy(markets[profileId], isPositive, minVotesToBuy);
    if (total > msg.value) revert InsufficientFunds();

    (
      uint256 purchaseCostBeforeFees,
      uint256 protocolFee,
      uint256 donation,
      uint256 totalCostIncludingFees
    ) = _calculateBuy(markets[profileId], isPositive, maxVotesToBuy);
    uint256 currentVotesToBuy = maxVotesToBuy;
    // if the cost is greater than the maximum votes to buy,
    // decrement vote count and recalculate until we identify the max number of votes they can afford
    while (totalCostIncludingFees > msg.value) {
      currentVotesToBuy--;
      (purchaseCostBeforeFees, protocolFee, donation, totalCostIncludingFees) = _calculateBuy(
        markets[profileId],
        isPositive,
        currentVotesToBuy
      );
    }

The problem is that this way is highly gas inefficient. And even though protocol is to be deployed on Base where gas costs are low, it would still be possible to reach significant gas costs.

Looping to check a certain buy's gas costs, costs around ~33k gas (PoC attached below). Considering users can easily be buying tens of thousands of votes, these checks could possibly reach the Base gas limit, which would not only make the tx impossible to execute, but even if it was possible it would cost a substantial amount of funds. As the gas limit is 240,000,000, it would require ~7000 iterations to run OOG.

Root Cause

Gas inefficient system design.

Attack Path

  1. There is a market with base price of 0.0001 eth.
  2. The market is one-sided and a Trust vote costs 1/10th of that - 0.00001 eth.
  3. A user wants to buy votes for 1 ETH. That would be 100,000 votes.
  4. User applies 10% slippage, this makes the minVotesToBuy 90,000.
  5. Due to the price movements, the user can only buy 91,000 votes. As this requires 9,000 iterations, the tx will run OOG, as it cannot fit in a single Base block.
  6. In the scenario where the gas required is just enough to fit in a block, this would cost the user 240_000_000 * 0.12e9 = 0.0288e18 = 0.0288 ETH. (assuming historical base gas costs of 0.12 gwei). At current prices this is ~$100.

Impact

Impossible to execute certain transactions due to OOG. High gas costs.

Affected Code

https://github.com/sherlock-audit/2024-12-ethos-update/blob/main/ethos/packages/contracts/contracts/ReputationMarket.sol#L460

PoC

As the codebase lacked a Foundry test suite, I integrated the necessary contract parts in my own test suite.

Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/LMSR.sol";

contract MyTest is Test {
    uint256 basePrice = 1e18;
    struct Market { 
        uint256[2] votes;
    }

    function test_gasCalc() public { 
        uint256 initYesVotes = 50;
        uint256 initNoVotes = 30;

        Market memory testMarket;
        testMarket.votes[0] = initYesVotes;
        testMarket.votes[1] = initNoVotes;

        uint256 gasLeft = gasleft();    

        uint256[] memory voteDelta = new uint256[](2);

        voteDelta[0] = testMarket.votes[0];
        voteDelta[1] = testMarket.votes[1] + 1;

        int256 costRatio = LMSR.getCost(
            initYesVotes, 
            initNoVotes, 
            voteDelta[0],
            voteDelta[1],
            1000);


        uint256 positiveCostRatio = costRatio > 0 ? uint256(costRatio) : uint256(costRatio * -1 );

        uint256 cost = positiveCostRatio * basePrice / 1e18;

        uint256 gasLeft2 = gasleft();
        uint256 gasConsumed = gasLeft - gasLeft2;
        console.log(gasConsumed);
    }
}

Mitigation

Consider using a binary search.

Discussion

sherlock-admin2

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

The original report as published on Sherlock, by bughuntoor. 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 linear search algorithm used to determine the maximum affordable votes, which iterates one by one and becomes prohibitively expensive for large quantities.

Impact

Certain transactions become impossible to execute due to running out of gas, and even if they fit in a block, they incur high gas costs.

What the attacker needed

A market with low base price; user buys a large number of votes; price movement causes many iterations

The fix

The protocol team fixed this issue by using a binary search instead of a linear loop.

How it was found

  1. There is a market with base price of 0.0001 eth.
  2. The market is one-sided and a Trust vote costs 1/10th of that - 0.00001 eth.
  3. A user wants to buy votes for 1 ETH. That would be 100,000 votes.
  4. User applies 10% slippage, this makes the minVotesToBuy 90,000.
  5. Due to the price movements, the user can only buy 91,000 votes. As this requires 9,000 iterations, the tx will run OOG, as it cannot fit in a single Base block.

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, the smart contract tries to find the most votes you can afford by trying one vote less at a time in a loop. Each try costs gas, and if you buy many votes, the loop runs many times, using up all the gas and making the transaction fail. This is like checking every item on a long receipt one by one instead of using a calculator.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by bughuntoor

View all

16 more disclosed reports, best first.

More from Ethos Reputation Market Fix Review Contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.