Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partialRepay and repay can be sandwiched #147

Closed
c4-bot-6 opened this issue Dec 18, 2023 · 4 comments
Closed

partialRepay and repay can be sandwiched #147

c4-bot-6 opened this issue Dec 18, 2023 · 4 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-994 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality

Comments

@c4-bot-6
Copy link
Contributor

c4-bot-6 commented Dec 18, 2023

Lines of code

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

Vulnerability details

Impact

Due to the gauge profit distribution mechanism, every borrower would be able to claim back a certain percentage of their repayment. Similarly, individuals with significant capital (whales) can exploit this situation to make substantial profits without assuming any risk.

Proof of Concept

When a borrower makes a payment through either partialRepay or repay, notifyPnL is called. This function sends the profit (interest + start fee) to the Profit Manager, which then distributes the profit to the buffer, credit token holders, gauge voters, and other special addresses. The issue arises from the instant distribution of profits to gauge voters.

if (amountForGuild != 0) {
    uint256 _gaugeWeight = uint256(GuildToken(guild).getGaugeWeight(gauge));
    if (_gaugeWeight != 0) {
        uint256 _gaugeProfitIndex = gaugeProfitIndex[gauge];
        if (_gaugeProfitIndex == 0) {
            _gaugeProfitIndex = 1e18;
        }
        gaugeProfitIndex[gauge] = _gaugeProfitIndex + (amountForGuild * 1e18) / _gaugeWeight;
    }
}

This implies that anyone with capital can claim some of these profits by simply depositing capital before a borrower initiates payment. Afterwards, they can claim the rewards using getRewards and unstake to avoid the risk of slashing.

Example

  1. Alice detects that a borrower is going to make a payment of 5k to a gauge with a 90,000 weight.
  2. Alice front-runs the borrower, mints, and stakes 10,000 USDC into the gauge.
  3. The borrower repays the 5k, and 50% of it goes to the gauge voters (2500 USDC).
  4. Alice back-runs the borrower with getRewards where she gets 10% of the rewards (250 USDC + 250 guild), with zero risk.
  5. Alice can unstake and redeem while waiting for the next borrower repayment.

This operation can be executed by one whale simultaneously in every market and every term, as the money is only needed to sandwich.

Note that the borrower can perform the same operation, and they even have the ability to flash-loan and stake a huge weight, and thus extract a big amount of their repayment back.

POC

Gist - https://gist.github.com/0x3b33/7ca9b8a4861c96e0b97ad35c4abf5ff9
Add in - security/2023-12-ethereumcreditguild/test/unit/loan/.sol
Run it with:

  • forge test --match-test test_borrowerFL - to see how a borrower can FL and steal some profits
  • forge test --match-test test_sandwichBorrower - to see how a whale can FL and steal some profits

Tools Used

Manual review

Recommended Mitigation Steps

I suggest implementing a mechanism for gauges to drip in a manner similar to how credit does.

Assessed type

Error

@c4-bot-6 c4-bot-6 added 3 (High Risk) Assets can be stolen/lost/compromised directly bug Something isn't working labels Dec 18, 2023
c4-bot-6 added a commit that referenced this issue Dec 18, 2023
@c4-pre-sort
Copy link

0xSorryNotSorry marked the issue as sufficient quality report

@c4-pre-sort c4-pre-sort added the sufficient quality report This report is of sufficient quality label Dec 30, 2023
@c4-pre-sort
Copy link

0xSorryNotSorry marked the issue as duplicate of #994

@c4-judge c4-judge added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue and removed 3 (High Risk) Assets can be stolen/lost/compromised directly labels Jan 25, 2024
@c4-judge
Copy link
Contributor

Trumpero changed the severity to 2 (Med Risk)

@c4-judge
Copy link
Contributor

Trumpero marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-994 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

4 participants