Skip to content
Smart contracts Audit finding Advanced

Multiplier is incorrectly calculated in Controller

The Controller contract multiplies the entire integral term by i_gain, including the previously accumulated old_i_term, instead of only the new contribution. This makes the multiplier differ from the documented formula and could affect the yin borrowing rate.

ABAIKUNANBAEV Follow

Originally disclosed on Code4rena

Submitted by ABAIKUNANBAEV

In Controller smart contract, due to mistake in multiplication of i_gain, it’s miscalculated and the error occurs. This can lead to a multiplier reflecting an incorrect value and therefore affect yin borrowing rate.

Proof of Concept

According to the multiplier formula, first we calculate p_term. The formula for p_term is following:

p_term = p_gain * error

And that’s done right using the get_p_term_internal() function:

https://github.com/code-423n4/2024-01-opus/blob/main/src/core/controller.cairo#L243-245

fn get_p_term_internal(self: @ContractState) -> SignedRay {
            self.p_gain.read() * nonlinear_transform(self.get_current_error(), self.alpha_p.read(), self.beta_p.read())
        }

Then we add right and go to integral term calculation. It’s almost the same as p_term, only the time scale factor is added:

https://github.com/code-423n4/2024-01-opus/blob/main/src/core/controller.cairo#L256-258

  old_i_term
                + nonlinear_transform(self.get_prev_error(), self.alpha_i.read(), self.beta_i.read())
                    * time_since_last_update_scaled

The problem is that when we add this formula to the multiplier, we multiply it additionally by i_gain:

https://github.com/code-423n4/2024-01-opus/blob/main/src/core/controller.cairo#L166-167

 let new_i_term: SignedRay = self.get_i_term_internal();
                multiplier += i_gain * new_i_term;

And, according to the formula, only this part should be multiplied by i_gain:

nonlinear_transform(self.get_prev_error(), self.alpha_i.read(), self.beta_i.read())
                    * time_since_last_update_scaled

But old_term also ends up multiplied.

Recommended Mitigation Steps

https://github.com/code-423n4/2024-01-opus/blob/main/src/core/controller.cairo#L256-258

-               old_i_term + nonlinear_transform(self.get_prev_error(),
-               self.alpha_i.read(), self.beta_i.read()) *
-               time_since_last_update_scaled

+               old_i_term + i_gain * nonlinear_transform(self.get_prev_error(),
+               self.alpha_i.read(), self.beta_i.read()) *
+               time_since_last_update_scaled

https://github.com/code-423n4/2024-01-opus/blob/main/src/core/controller.cairo#L167

-               multiplier += i_gain * new_i_term;
+               multiplier += new_i_term;

0xsomeone (judge) commented:

The warden has demonstrated how the formula that is in use by the Controller contradicts the documentation of the project, and namely the specification of the aforementioned formula.

In detail, the specification states:

Note: Please see provided formula in the judge’s original comment.

We are interested in the latter part of the formula, specifically:

Note: Please see provided formula in the judge’s original comment.

The documentation states that k_i stands for the:

gain that is applied to the integral term

The problem highlighted by the warden is that the implementation will calculate the following:

Note: Please see provided formula in the judge’s original comment.

The above corresponds to:

let new_i_term: SignedRay = self.get_i_term_internal();
multiplier += i_gain * new_i_term;

Whereby the get_i_term_internal function will ultimately yield:

old_i_term
    + nonlinear_transform(self.get_prev_error(), self.alpha_i.read(), self.beta_i.read())
    * time_since_last_update_scaled

In the above:

Note: please review provided terms in the judge’s original comment.

Based on the above analysis, the documentation indeed contradicts what the code calculates. The Opus team is invited to evaluate this exhibit; however, regardless of the correct behaviour of the system, this submission will be accepted as valid due to the documentation being the “source of truth” during the audit’s duration.

milancermak (Opus) confirmed


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

Likely the gain factor i_gain is applied to the whole integral term (including the stored old_i_term) rather than only to the newly computed contribution.

Impact

The multiplier could reflect an incorrect value and therefore affect the yin borrowing rate.

What the attacker needed

None

The fix

The code should be changed so that i_gain multiplies only the new nonlinear-transform contribution inside get_i_term_internal, and the multiplier should add new_i_term without an extra i_gain multiplication.

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 an interest-like multiplier using a formula with several parts. One part, the integral term, is supposed to have a gain factor applied only to the newly added piece, but the code applies it to the whole running total. This means the number used for borrowing rates can drift from what the designers intended.

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 ABAIKUNANBAEV

View all

5 more disclosed reports, best first.

More from Opus

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.