Synthetix Muhlifain Release Smart Contract Audit

# 1. Introduction
iosiro was commissioned by [Synthetix]( to conduct a smart contract audit of the Muhlifain Release, which included audits of the following components:
* [SIP-230]( between 25 July 2022 and 4 August 2022 by 2 auditors, consuming a total of 8 resource days.
* [SIP-257]( on 4 August 2022, consuming 1 resource day.

This report is organized into the following sections.

* **[Section 2 - Executive summary:](#section-2)** A high-level description of the findings of the audit.
* **[Section 3 - Audit details:](#section-3)** A description of the scope and methodology of the audit.
* **[Section 4 - Design specification:](#section-4)** An outline of the intended functionality of the smart contracts.
* **[Section 5 - Detailed findings:](#section-5)** Detailed descriptions of the findings of the audit.

The information in this report should be used to understand the smart contracts' risk exposure better and as a guide to improving the security posture of the smart contracts by remediating issues identified. The results of this audit reflect the in-scope source code reviewed at the time of the audit.

The purpose of this audit was to achieve the following:

* Identify potential security flaws.
* Ensure that the smart contracts function according to the documentation provided.

Assessing the off-chain functionality associated with the contracts, for example, backend web application code, was outside of the scope of this audit.

Due to the unregulated nature and ease of transfer of cryptocurrencies, operations that store or interact with these assets are considered high risk from cyber attacks. As such, the highest level of security should be observed when interacting with these assets. This requires a forward-thinking approach, which takes into account the new and experimental nature of blockchain technologies. Strategies that should be used to encourage secure code development include:

* Security should be integrated into the development lifecycle, and the level of perceived security should not be limited to a single code audit.
* Defensive programming should be employed to account for unforeseen circumstances.
* Current best practices should be followed where possible.

<a name="section-2"></a>
# 2. Executive summary

This report presents the findings of a smart contract audit performed by iosiro of Synthetix's Muhlifain release.  

## SIP-230

[SIP-230]( iterated on the existing circuit breaker functionality to allow for granular tracking of each oracle, as opposed to a global circuit breaker. The change also enables circuit breakers for any oracle and not only for currency prices.

An initial review was performed while the code was still under active development and feedback was provided on an ongoing basis. No issues were identified in the final implementation of this SIP.

## SIP-257

[SIP-257]( expanded the `ExchangeRates` smart contract to support aggregators with more than 18 decimals. Two informational issues were identified and corrected during the audit.

<a name="section-3"></a>
# 3. Audit details

## 3.1 Scope

The source code considered in-scope for the assessment is described below. Code from all other files was considered to be out-of-scope. Out-of-scope code that interacts with in-scope code was assumed to function as intended and not introduce any functional or security vulnerabilities for the purposes of this audit.

### 3.1.1 Synthetix SIP-230 Smart Contracts

**Project Name:** Synthetix<br/>
**Commits:** [bbce42e]( [458cced](, [6e8972c](<br/>
**Final Commit:** [6e8972c](<br/>
**Files:** contracts/CircuitBreaker.sol, contracts/ExchangeCircuitBreaker.sol, contracts/ExchangeRates.sol, contracts/Exchanger.sol, contracts/ExchangerWithFeeRecAlternatives.sol, contracts/Issuer.sol, contracts/OneNetAggregatorDebtRatio.sol

### 3.1.2 Synthetix SIP-257 Smart Contracts

**Project Name:** Synthetix<br/>
**Commits:** [0ed08fe](, [72e8c4e](<br/>
**Final Commit:** [72e8c4e](<br/>
**Files:** contracts/ExchangeRates.sol

## 3.2  Methodology

The audit was conducted using a variety of techniques described below.

### 3.2.1 Code review

The source code was manually inspected to identify potential security flaws. Code review is a useful approach for detecting security flaws, discrepancies between the specification and implementation, design improvements, and high-risk areas of the system.

### 3.2.2 Dynamic analysis

The contracts were compiled, deployed, and tested in a test environment, both manually and through the test suite provided. Manual analysis was used to confirm that the code was functional and discover security issues that could be exploited. The coverage report of the provided tests as on the final day of the audit is given below.

### 3.2.3 Automated analysis

Tools were used to automatically detect the presence of several types of security vulnerabilities, including reentrancy, timestamp dependency bugs, and transaction-ordering dependency bugs. Static analysis results were reviewed manually and any false positives were removed. Any true positive results are included in this report.

Static analysis tools commonly used include Slither, Securify, and MythX. Tools such as the Remix IDE, compilation output, and linters could also be used to identify potential areas of concern.

## 3.3  Risk ratings

Each issue identified during the audit has been assigned a risk rating. The rating is determined based on the criteria outlined below.

* **High risk** - The issue could result in a loss of funds for the contract owner or system users.
* **Medium risk** - The issue resulted in the code specification being implemented incorrectly.
* **Low risk** - A best practice or design issue that could affect the security of the contract.
* **Informational** - A lapse in best practice or a suboptimal design pattern that has a minimal risk of affecting the security of the contract.
* **Closed** - The issue was identified during the audit and has since been satisfactorily addressed, removing the risk it posed.

<a name="section-4"></a>
# 4. Design specification

The following section outlines the intended functionality of the system at a high level.

## 4.1 SIP-230

The specification of SIP-230 was based on commit hash [65dd639](

## 4.2 SIP-257

The specification of SIP-257 was based on commit hash [950d20f](

<a name="section-5"></a>
# 5. Detailed findings

The following section details the findings of the audit.

## 5.1 High risk

No identified high-risk issues were open at the conclusion of the review.

## 5.2 Medium risk

No identified medium-risk issues were open at the conclusion of the review.

## 5.3 Low risk

No identified low-risk issues were open at the conclusion of the review.

## 5.4 Informational

No identified informational issues were open at the conclusion of the review.

##  5.5 Closed

### 5.5.1 Required statements removed from Atomic Exchanges (high risk)


#### Description

As part of the restructuring of the circuit breaker logic, the internal call to `_ensureCanExchange` was removed from `ExchangerWithFeeRecAlternatives::_exchangeAtomically`. This would allow users to specify the same source and destination currency, which could result in undesirable behavior. Furthermore, it resulted in the implementations of `exchange` and `exchangeAtomically` to deviate significantly and unjustifiably increased the complexity of the system.

#### Recommendation

Refactor the `_ensureCanExchange` function so that it is suitable to be used in both the `_exchangeAtomically` and `exchange` functions.

#### Update

The circuit breaker logic was incorporated into `_ensureCanExchange` and the function was readded to both `_exchange` and `_exchangeAtomically` as per the recommendation in commit [d561f44](

### 5.5.2 Incorrect parameter argument (high risk)

#### Description

The incorrect parameter was used when calling `_rateIsCircuitBroken` in `ExchangeRates::ratesAndInvalidForCurrencies`. Instead of providing the current rate of the entry, the timestamp of the entry was instead used. This would result in `ratesAndInvalidForCurrencies` always reporting that the rates were invalid.

#### Recommendation

The entry's rate value should be used instead of its timestamp:

@@ -326,7 +326,7 @@ contract ExchangeRates is Owned, MixinSystemSettings, IExchangeRates {
               anyRateInvalid =
                   flagList[i] ||
                   _rateIsStaleWithTime(_rateStalePeriod, rateEntry.time) ||
-                    _rateIsCircuitBroken(currencyKeys[i], rateEntry.time);
+                    _rateIsCircuitBroken(currencyKeys[i], rateEntry.rate);

#### Update

The parameter was fixed as per the recommendation in commit [cbe8ad9](

### 5.5.3 Incomplete implementation (low risk)

*[ExchangeRates.sol#L335](,[ExchangeRates.sol#L345](, [ExchangeRates.sol#L363](*

#### Description

The implementations of `rateIsInvalid`, `anyRateIsInvalid`, and `anyRateIsInvalidAtRound` did not include the status of the currency's circuit breaker in the `isValid` return parameter; whereas, other synonymous functions such as `ratesAndInvalidForCurrencies` and `rateAndInvalid` did. Based on this discrepancy, it was concluded the implementation was incomplete.

#### Recommendation

Functions that implement identical logic, but are kept for backward compatibility should be refactored to use a single shared implementation to avoid inconsistencies. The functions `rateIsInvalid`, `anyRateIsInvalid` and `anyRateIsInvalidAtRound` should also be updated to include the status of the currency's circuit breaker.

#### Update

The issue was addressed in commit [a6783f3]( as per the recommendations. Each of the affected functions was modified to include the `_rateIsCircuitBroken` check and `rateIsInvalid` was modified to reuse `rateAndInvalid`.

### 5.5.4 Unnecessary use of Safe Math library (informational)
The subtraction operations performed in the `_formatAggregatorAnswer` function were previously validated to ensure that the result was always positive, and as such, the Safe Math library was not necessary in these instances.

#### Update
The `sub` calls were replaced with the `-` operator in [9d67f44](

### 5.5.5 Improve test quality (informational)
The unit test that tested whether a 27 decimal aggregator answer was valid only performed the test with a 2 decimal value that was scaled up to 27 decimals. To improve the utility of the unit test, it is recommended that a 27 decimal value is used to ensure that the value is correctly set.

#### Update
The suggestion was implemented in [9d67f44](

Secure your system.
Request a service
Start Now