Synthetix Avior Release Smart Contract Audit

# 1. Introduction
iosiro was commissioned by [Synthetix](https://www.synthetix.io) to conduct a smart contract audit of their Avior Release, which included the following component:
* [SIP-182](https://sips.synthetix.io/sips/sip-182) from 23 to 27 October, consuming 5 resource days.

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 Avior release.  

[SIP-182](https://sips.synthetix.io/sips/sip-182) implemented a generic wrapper contract that allowed users to wrap ERC-20 tokens for their synthetic counterparts. One high risk, one medium risk, and several low and informational issues were identified and remediated by the end of the audit.

It should be noted that specific types of tokens will be incompatible with the Wrapper, and an in-depth review of underlying tokens should be performed before deploying a new Wrapper instance. Incompatible tokens include:
- Interest bearing tokens that compound their supply (e.g. Aave tokens)
- Rebasing tokens (e.g. AMPL)
- Tokens that have transfer fees (e.g. USDT, if fees are enabled)
- Tokens with callbacks (e.g. ERC-777)

<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-182 smart contracts

**Project Name:** Synthetix<br/>
**Commits:** [4f6e1b6](https://github.com/Synthetixio/synthetix/pull/1489/commits/4f6e1b641d4d71999336be3dc6514889d289a7fd), [2b76dca](https://github.com/Synthetixio/synthetix/pull/1489/commits/2b76dca935658ca261246975b94825a676243bad), [9d6ac2e](https://github.com/Synthetixio/synthetix/commit/9d6ac2e150c079a2c6fdd25e026c388762328534)<br/>
**Files:** BaseDebtCache.sol, DebtCache.sol, FeePool.sol, MixinSystemSettings.sol, MultiCollateralSynth.sol, SystemSettings.sol, Wrapper.sol, WrapperFactory.sol

## 3.2  Methodology

A variety of techniques were used in order to perform the audit. These techniques are briefly 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.

### 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-182

The specification of SIP-182 was based on commit hash [fec9131](https://github.com/Synthetixio/SIPs/blob/fec9131b744735f7201bb5d766531a41f844a836/content/sips/sip-182.md).

<a name="section-5"></a>
# 5. Detailed findings
The following section includes in-depth descriptions of the findings of the audit.

## 5.1 High risk

No high-risk issues were present at the conclusion of the audit.

## 5.2 Medium risk

No medium-risk issues were present at the conclusion of the audit.

## 5.3 Low risk

No low-risk issues were present at the conclusion of the audit.

## 5.4 Informational  

No informational issues were present at the conclusion of the audit.

## 5.5 Closed

### 5.5.1 Missing token approval could affect withdrawals (high risk)
*[Wrapper.sol#L210](https://github.com/Synthetixio/synthetix/blob/2b76dca935658ca261246975b94825a676243bad/contracts/Wrapper.sol#L210)*

#### Description
The wrapper contract may not be able to process withdrawals for most ERC-20 tokens due to having a zero allowance when performing a `transferFrom` from itself. WETH allows a token owner to transfer an arbitrary number of tokens from themself through the `transferFrom` function. As a result, unit tests which were exclusively tested with WETH passed; however, this behavior is non-standard and cannot be assumed for arbitrary tokens added to the system. If no allowance is given, users will be able to deposit tokens into the system, but will not be able to withdraw the underlying assets when trying to exit the system.

#### Recommendation
The wrapper contract either needs the ability to approve the maximum allowance of the target token for itself, or a separate `safeTransfer` function should be used when burning.

#### Update
An approval call was added for the token in the wrapper constructor in [9d6ac2e](https://github.com/Synthetixio/synthetix/commit/9d6ac2e150c079a2c6fdd25e026c388762328534).

### 5.5.2 Potentially dangerous fee rates (medium risk)

#### Description
There was no mechanism in place to prevent minting and burning in the same transaction, leaving it possible to exploit a negative fee rate under the correct conditions. For example, if a pool with low volume had a -0.5% mint fee to incentivize activity and a 0% burn fee, a malicious user could use a flashloan to skim the 0.5% profit from the flashloan by borrowing, minting, burning, and returning the funds. This would result in the user profiting, while not rebalancing the pool. This could also be attacked through a sandwich attack when the fee rates are being updated.

#### Recommendation
Validation should be added to ensure two conditions:
* The mint and burn fees cannot be be negative at the same time.
* The positive fee should always be greater than the absolute value of the negative fee.

Additionally, care should always be used when setting new fee rates. If a fee change that might be susceptible to a sandwich attack is required, the system should be paused while the change is applied.

#### Update
The specified validation was implemented in [2b76dca](https://github.com/Synthetixio/synthetix/pull/1489/commits/2b76dca935658ca261246975b94825a676243bad). This may still be susceptible to a sandwich attack under the correct conditions and if the system is not paused during the update.  

### 5.5.3 Not using `safeTransfer` (low risk)

#### Description
The OpenZeppelin SafeERC20 library or equivalent implementation should be used to wrap all important ERC-20 functions. The library should be used on mint and burn to account for tokens with no return value, as detailed in [this article](https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca).

#### Update
A `safeTransfer` implementation was included in [2b76dca](https://github.com/Synthetixio/synthetix/pull/1489/commits/2b76dca935658ca261246975b94825a676243bad).

### 5.5.4 Missing `issuanceActive` modifier (low risk)

#### Description
Across the Synthetix smart contract ecosystem, the `issuanceActive` system setting is used to control when issuance of synths can be performed. It is recommended to add this modifier to the mint and burn functions to be consistent with the rest of the system.

#### Update
The `issuanceActive` modifier was added in [2b76dca](https://github.com/Synthetixio/synthetix/pull/1489/commits/2b76dca935658ca261246975b94825a676243bad).

### 5.5.5 Users may burn an excessive amount of tokens (low risk)
*[Wrapper.sol#L193](https://github.com/Synthetixio/synthetix/blob/5ddb558b8e1ddd71e4bbfc63e67c5308e0373dd2/contracts/Wrapper.sol#L193)*

The affected code results in the user potentially burning more tokens than they intended. The logic could be changed to `targetSynthIssued.add(burnFee) < amountIn ? targetSynthIssued.add(burnFee) : amountIn;` to only cap the amount burned when necessary. Currently, the maximum amount of extra tokens burned would be `calcMintFee(targetSynthIssued)`, and the user would be compensated in the `targetSynth`, but they may not necessarily have enough of the target synth available to burn, which would result in a reverted transaction.

#### Update
Changed in [2b76dca](https://github.com/Synthetixio/synthetix/pull/1489/commits/2b76dca935658ca261246975b94825a676243bad).

### 5.5.6 Avoid paying fees if none are available (informational)
*[Wrapper.sol#L233](https://github.com/Synthetixio/synthetix/blob/7017dd0188cea8cb0df313ef35165f5e8c91a836/contracts/Wrapper.sol#L233)*, *[Wrapper.sol#L253](https://github.com/Synthetixio/synthetix/blob/7017dd0188cea8cb0df313ef35165f5e8c91a836/contracts/Wrapper.sol#L253)*

Before issuing fees, `excessAmountUsd` should be validated to ensure that it is greater than 0. This will avoid wasting gas if no fee should be paid.

#### Update
Implemented in [2b76dca](https://github.com/Synthetixio/synthetix/pull/1489/commits/2b76dca935658ca261246975b94825a676243bad).

### 5.5.7 Improve comments (informational)
*[BaseDebtCache.sol#L209](https://github.com/Synthetixio/synthetix/blob/5ddb558b8e1ddd71e4bbfc63e67c5308e0373dd2/contracts/BaseDebtCache.sol#L209)*

This comment should be updated to reflect that the `excludedDebt` is now included. A comment should also be added lower down in the function to separate the EtherWrapper logic from the Wrapper logic.

#### Update
Comment updated in [9d6ac2e](https://github.com/Synthetixio/synthetix/commit/9d6ac2e150c079a2c6fdd25e026c388762328534).

Secure your system.
Request a service
Start Now