Skip to content
Smart contracts Audit finding Intermediate

Unsafe cast in getCollateralRatio()

The getCollateralRatio() function casts a computed collateral ratio to uint64 without checking for overflow, so the ratio can wrap to an incorrect value when the true ratio exceeds the maximum uint64. An attacker could manipulate the collateral balance to make the view function return a wrong ratio, potentially affecting protocol decisions.

auditor0517 Follow with Lambda

Originally disclosed on Code4rena

Submitted by auditor0517, also found by Lambda

LibGetters.getCollateralRatio() might return the incorrect ratio due to the unsafe cast.

Proof of Concept

getCollateralRatio() outputs the collateral ratio using the total collaterals and issued agTokens.

    // The `stablecoinsIssued` value need to be rounded up because it is then used as a divizer when computing
    // the amount of stablecoins issued
    stablecoinsIssued = uint256(ts.normalizedStables).mulDiv(ts.normalizer, BASE_27, Math.Rounding.Up);
    if (stablecoinsIssued > 0)
        collatRatio = uint64(totalCollateralization.mulDiv(BASE_9, stablecoinsIssued, Math.Rounding.Up)); //@audit unsafe cast
    else collatRatio = type(uint64).max;

Typically, the collatRatio should be around BASE_9 but the ratio might be larger than type(uint64).max during the initial stage.

Furthermore, totalCollateralization is calculated using the raw balance of collaterals and it might be manipulated when stablecoinsIssued is not large.

Then collatRatio might be cast to the wrong value.

After all, getCollateralRatio() will return the wrong ratio and it will affect the protocol seriously.

Recommended Mitigation Steps

I think we should use the SafeCast library in getCollateralRatio().

Picodes (Angle) disagreed with severity and commented:

This seems hardly doable. Assuming there is 1 stablecoin issued (1e18), you’d need 1e28 collateral. Furthermore no impact is described: this would just break this view function. Overall I think Low severity is more appropriate

hansfriese (Judge) commented:

@Picodes - Although it’s an edge case, it’s likely to happen and getCollateralRatio() plays an important role in the protocol. Will keep as Medium.

GuillaumeNervoXS (Angle) commented:

To overflow the protocol would need to be 1 billion % over collateralise so it is not likely to happen.

hansfriese (Judge) commented:

As #9 shows, 1 USD is enough when stablecoinsIssued = 1 wei. I think it should be mitigated for safety.

Angle mitigated:

PR: https://github.com/AngleProtocol/angle-transmuter/commit/6f2ffcb1e89e3bba05c9aa2133ef94347aa42c28 Adds safeCast.

Status: Mitigation error. Full details in reports from Lambda, auditor0517, and Jeiwan - and also shared below in the Mitigation Review section.


The original report as published on Code4rena, by auditor0517. 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

Impact

The view function could return an incorrect collateral ratio, which may affect protocol behaviour, though the text notes that overflowing would require extreme over-collateralisation or a very small issued amount.

What the attacker needed

StablecoinsIssued is very small or zero; collateral balance is large enough to overflow uint64

The fix

The issue was mitigated by adding a safe cast, but the mitigation was later found to be in error according to the report status.

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.

A smart contract calculates a ratio (collateral divided by issued stablecoins) and stores it in a small integer type that has a maximum value. If the real ratio is bigger than that maximum, the number wraps around to a small value instead of raising an error. This means anyone reading the ratio gets a wrong number, which could mislead other parts of the protocol.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by auditor0517

View all

11 more disclosed reports, best first.

More from Angle Protocol - Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.