Synthetix Algol Release Smart Contract Audit

# 1. Introduction
iosiro was commissioned by [Synthetix](https://www.synthetix.io) to conduct a smart contract audit of their Algol Release, which included the following component:
* [SIP-251](https://sips.synthetix.io/sips/sip-251) - performed on 21 June 2022 by 1 auditor, 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 Algol release.  

[SIP-251](https://sips.synthetix.io/sips/sip-251) introduced a new `snxLiquidationPenalty` variable, which allowed for the collateral loans and Synthetix staking mechanisms to have different liquidation penalties.

One medium risk issue and several informational issues were identified and resolved 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-251 smart contracts
**Project Name:** Synthetix<br/>
**Commits:** [9eb7902](https://github.com/Synthetixio/synthetix/commit/9eb790220b8ec72fc52b4ad50416fab70a6fe80c), [c27eef5](https://github.com/Synthetixio/synthetix/commit/c27eef59fd6fe2f11fd505cac19687fae2689c8c), [9eb7902](https://github.com/Synthetixio/synthetix/commit/9eb790220b8ec72fc52b4ad50416fab70a6fe80c)<br/>
**Files:**  CollateralUtil.sol, Issuer.sol, MixinSystemSettings.sol, SystemSettings.sol, SystemSettingsLib.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-251

The specification of SIP-251 was based on commit hash [ce45cea](https://github.com/Synthetixio/SIPs/blob/3e56e9270ac770a15f430fc0c4c1dfb3f4d99852/content/sips/sip-251.md).

<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 Incorrect calculation of liquidation ratio (medium risk)
[SystemSettings.sol#L320](https://github.com/Synthetixio/synthetix/blob/4b1b1b41598ca2f7f4cfc53e462c718023fe767e/contracts/SystemSettings.sol#L320)

#### Description
The `SystemSettingsLib.setLiquidationRatio()` function used the `getLiquidationPenalty()` variable to validate whether the new liquidation ratio was sufficiently low relative to the liquidation penalty. In this instance, the collateral loan liquidation penalty was used instead of the SNX staking penalty. As a result, the validation on the new `setLiquidationRatio()` was performed incorrectly.

#### Recommendation
The `SystemSettingsLib.setLiquidationRatio()` function should use `snxLiquidationPenalty` when setting the liquidation ratio instead of `getLiquidationPenalty()`.

#### Update
Fixed in [b99cb6c](https://github.com/Synthetixio/synthetix/pull/1829/commits/b99cb6cbc719ce494b1de51a1cf365ca45dc295a#diff-c38d92bc70fa84a77b4a61b8bf827fcc630d5a33314ec903069b40451d3a089cR152).

### 5.5.2 Design comments (informational)

#### Improve test quality
Unit tests were found to use pre-calculated values to validate test outcomes. It is recommended that these values are derived in code to improve readability and maintainability.

#### Update
Fixed in [fab5f52](https://github.com/Synthetixio/synthetix/pull/1820/commits/fab5f5223d99e8d45ecf2a113223cb4cf5c63629).

#### Code cleanup
The following code changes are suggested for improved code quality:
1. Remove `liquidationPenalty()` from Liquidator.sol as it is no longer used.
2. The [comment referencing SIP-15](https://github.com/Synthetixio/synthetix/blob/d6aefacc436f9c36ea136c9f3d37a5f5f3a170fc/contracts/SystemSettings.sol#L75) should be moved to the `liquidationPenalty` function.
3. The [comment](https://github.com/Synthetixio/synthetix/blob/4b1b1b41598ca2f7f4cfc53e462c718023fe767e/contracts/SystemSettings.sol#L75) describing the collateral liquidation penalty should be updated to reference SIP-97 or SIP-251 instead of SIP-15.

#### Update
1. Fixed in [ac0ab60](https://github.com/Synthetixio/synthetix/pull/1826/commits/ac0ab60ed652de5ce94ecb1fd96ad2b337ac666b#diff-37dd7cd188d069f8930b6d23c5e851794a9d566eafb9c1dd5f48b6be7483fbb6L13).
2. Fixed in [5da6d68](https://github.com/Synthetixio/synthetix/pull/1829/commits/5da6d68c48862512d4fb68712366812d854bb848#diff-6d6308f453bc49a44e7984af899b162a739139a95cb0517db28808cbb235fa93R76).
3. Fixed in [5da6d68](https://github.com/Synthetixio/synthetix/pull/1829/commits/5da6d68c48862512d4fb68712366812d854bb848#diff-6d6308f453bc49a44e7984af899b162a739139a95cb0517db28808cbb235fa93R81).

#### Shared upper bound
The [`MAX_LIQUIDATION_PENALTY`](https://github.com/Synthetixio/synthetix/blob/d6aefacc436f9c36ea136c9f3d37a5f5f3a170fc/contracts/SystemSettingsLib.sol#L177) variable is enforced on both collateral and staking liquidation penalties. As these values are independent, it may be helpful to enforce two distinct maximum values.

#### Update
Synthetix noted and accepted the issue.

Secure your system.
Request a service
Start Now