Skip to content
Business logic Audit finding Intermediate

Incorrect calculations in debtCeiling

The debtCeiling function incorrectly applies a gauge weight delta only to the gauge's own weight, not to the total weight, causing the calculated ceiling to be too low when a user tries to decrement their gauge weight. This prevents users from withdrawing Guild tokens from the gauge even when it is the only gauge of its type and should have an unlimited ceiling.

SpicyMeatball Follow with nocoder, cccz and 9 others

Originally disclosed on Code4rena

Submitted by SpicyMeatball, also found by JCN (1, 2), nocoder, cccz, niroh, mojito_auditor, rbserver, klau5, santipu_, TheSchnilch, Byteblockers, kaden, and ether_sky

Incorrect debtCeiling value will prevent users from withdrawing Guild tokens out of the gauge.

Proof of Concept

If there is a loan in the gauge and user wants to decrement the weight, we need to check it’s ceiling first:

https://github.com/code-423n4/2023-12-ethereumcreditguild/blob/main/src/tokens/GuildToken.sol#L224-L231

        // check if gauge is currently using its allocated debt ceiling.
        // To decrement gauge weight, guild holders might have to call loans if the debt ceiling is used.
        uint256 issuance = LendingTerm(gauge).issuance();
        if (issuance != 0) {
            uint256 debtCeilingAfterDecrement = LendingTerm(gauge).debtCeiling(-int256(weight));
            require(
                issuance <= debtCeilingAfterDecrement,
                "GuildToken: debt ceiling used"
            );
        }

This is needed to ensure that loans are distributed between the gauges according to their weights and tolerance. This only matters if there are multiple gauges of the same type. If there is, only one gauge ceiling is limited by the hardcap and the minter buffer.

https://github.com/code-423n4/2023-12-ethereumcreditguild/blob/main/src/loan/LendingTerm.sol#L287

        } else if (gaugeWeight == totalWeight) {
            // one gauge, unlimited debt ceiling
            // returns min(hardCap, creditMinterBuffer)
            return
                _hardCap < creditMinterBuffer ? _hardCap : creditMinterBuffer;
        }

Unfortunately, debtCeiling(int256 gaugeWeightDelta) applies delta only to the gaugeWeight leaving totalWeight as-is:

https://github.com/code-423n4/2023-12-ethereumcreditguild/blob/main/src/loan/LendingTerm.sol#L270

    function debtCeiling(
        int256 gaugeWeightDelta
    ) public view returns (uint256) {
        address _guildToken = refs.guildToken; // cached SLOAD
        uint256 gaugeWeight = GuildToken(_guildToken).getGaugeWeight(
            address(this)
        );
        gaugeWeight = uint256(int256(gaugeWeight) + gaugeWeightDelta);
        uint256 gaugeType = GuildToken(_guildToken).gaugeType(address(this));
        uint256 totalWeight = GuildToken(_guildToken).totalTypeWeight(
            gaugeType
        );

Check this test case for GuildToken.t.sol, where the user is unable to withdraw tokens from the gauge even it it’s the only gauge of it’s type and ceiling should be “unlimited”.

Modify debtCeiling to act like a LendingTerm:

https://github.com/code-423n4/2023-12-ethereumcreditguild/blob/main/test/unit/tokens/GuildToken.t.sol#L27

    function debtCeiling(int256 gaugeWeightDelta) public view returns (uint256) {
        uint256 gaugeWeight = token.getGaugeWeight(address(this));
        gaugeWeight = uint256(int256(gaugeWeight) + gaugeWeightDelta);
        uint256 gaugeType = token.gaugeType(address(this));
        uint256 totalWeight = token.totalTypeWeight(gaugeType);
        if (gaugeWeight == 0) {
            return 0; // no gauge vote, 0 debt ceiling
        } else if (gaugeWeight == totalWeight) {
            // one gauge, unlimited debt ceiling
            // return unlimited
            return 1_000_000 ether;
        }
        uint256 _issuance = issuance; // cached SLOAD
        uint256 totalBorrowedCredit = credit.totalSupply();
        uint256 gaugeWeightTolerance = 1e18;
        if (totalBorrowedCredit == 0 && gaugeWeight != 0) {
            // first-ever CREDIT mint on a non-zero gauge weight term
            // does not check the relative debt ceilings
            return 1_000_000 ether;
        }
        uint256 toleratedGaugeWeight = (gaugeWeight * gaugeWeightTolerance) /
            1e18;
        uint256 debtCeilingBefore = (totalBorrowedCredit *
            toleratedGaugeWeight) / totalWeight;
        if (_issuance >= debtCeilingBefore) {
            return debtCeilingBefore; // no more borrows allowed
        }
        uint256 remainingDebtCeiling = debtCeilingBefore - _issuance; // always >0
        if (toleratedGaugeWeight >= totalWeight) {
            // if the gauge weight is above 100% when we include tolerance,
            // the gauge relative debt ceilings are not constraining.
            return 1_000_000 ether;
        }
        uint256 otherGaugesWeight = totalWeight - toleratedGaugeWeight; // always >0
        uint256 maxBorrow = (remainingDebtCeiling * totalWeight) /
            otherGaugesWeight;
        uint256 _debtCeiling = _issuance + maxBorrow;
        // return min(creditMinterBuffer, hardCap, debtCeiling)
        if (1_000_000 ether < _debtCeiling) {
            return 1_000_000 ether;
        }
        return _debtCeiling;
    }

run the test

    function testDebtCeilingOneGauge() public {
        // grant roles to test contract
        vm.startPrank(governor);
        core.grantRole(CoreRoles.CREDIT_MINTER, address(this));
        core.grantRole(CoreRoles.GUILD_MINTER, address(this));
        core.grantRole(CoreRoles.GAUGE_ADD, address(this));
        core.grantRole(CoreRoles.GAUGE_REMOVE, address(this));
        core.grantRole(CoreRoles.GAUGE_PARAMETERS, address(this));
        vm.stopPrank();

        // setup
        token.mint(alice, 100e18);

        token.setMaxGauges(3);
        token.addGauge(1, address(this));

        vm.startPrank(alice);
        token.incrementGauge(address(this), 10e18);
        vm.stopPrank();

        credit.mint(alice, 100e18);
        issuance = 100e18;

        vm.prank(alice);
        // will revert with "GuildToken: debt ceiling used"
        token.decrementGauge(address(this), 8e18);
    }

Tools Used

Foundry

Recommended Mitigation Steps

Apply delta to totalWeight as well:

https://github.com/code-423n4/2023-12-ethereumcreditguild/blob/main/src/loan/LendingTerm.sol#L270

    function debtCeiling(
        int256 gaugeWeightDelta
    ) public view returns (uint256) {
        address _guildToken = refs.guildToken; // cached SLOAD
        uint256 gaugeWeight = GuildToken(_guildToken).getGaugeWeight(
            address(this)
        );
        gaugeWeight = uint256(int256(gaugeWeight) + gaugeWeightDelta);
        uint256 gaugeType = GuildToken(_guildToken).gaugeType(address(this));
        uint256 totalWeight = GuildToken(_guildToken).totalTypeWeight(
           gaugeType
        );
+     totalWeight = uint256(int256(totalWeight) + gaugeWeightDelta);

Assessed type

Math

eswak (Ethereum Credit Guild) confirmed via duplicate issue #880


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

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

The debtCeiling function applies the gauge weight delta only to the gauge's weight, leaving totalWeight unchanged, so the relative debt ceiling is miscalculated when decrementing.

Impact

Users are unable to withdraw Guild tokens from the gauge when they should be able to, because the incorrect debt ceiling calculation blocks the decrement.

What the attacker needed

A gauge with existing loans; user has Guild tokens locked in the gauge; user wants to decrement gauge weight

The fix

The fix is to apply the gauge weight delta to totalWeight as well, so that the relative debt ceiling is calculated correctly.

How it was found

  1. Set up a test contract with necessary roles and mint tokens to a user.
  2. Add a gauge and have the user increment their gauge weight.
  3. Mint credit to simulate issuance equal to the user's weight.
  4. User attempts to decrement their gauge weight.
  5. The transaction reverts with 'GuildToken: debt ceiling used'.

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.

In this system, users vote for gauges by locking tokens, and the amount of debt a gauge can support depends on its share of total votes. When a user wants to reduce their vote, the code checks if the remaining debt would still be within the new limit. Because the calculation forgets to update the total votes when the user's votes decrease, it thinks the limit is lower than it actually is, so it blocks the withdrawal.

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 SpicyMeatball

View all

18 more disclosed reports, best first.

More from Ethereum Credit Guild

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.