Security Audit
February 12, 2024
Version 1.0.0
Presented by 0xMacro
This document includes the results of the security audit for IDEX's smart contract code as found in the section titled ‘Source Code’. The security audit was performed by the Macro security team from January 12 to January 16, and on February 2, 2024.
The purpose of this audit is to review the source code of certain IDEX Solidity contracts, and provide feedback on the design, architecture, and quality of the source code with an emphasis on validating the correctness and security of the software in its entirety.
Disclaimer: While Macro’s review is comprehensive and has surfaced some changes that should be made to the source code, this audit should not solely be relied upon for security, as no single audit is guaranteed to catch all possible bugs.
The following is an aggregation of issues found by the Macro Audit team:
Severity | Count | Acknowledged | Won't Do | Addressed |
---|---|---|---|---|
Medium | 1 | - | - | 1 |
Low | 3 | 1 | - | 2 |
Code Quality | 2 | 1 | - | 1 |
IDEX was quick to respond to these issues.
Our understanding of the specification was based on the following sources:
The following source code was reviewed during the audit:
9d53426e2508587671ca891602f3262b76703d63
6c713066070ae62e163150585431096779030289
cc2a38c738828d945348c90421e44bd201edaf55
57f90117f408f3dccf17fb28a15be18bc52b9b69
789f07db872d53776a2dc29fbbacbf60bbefe1ab
612e842fca36031f7423e0497a0eededa8561b4a
9fce821850ef0f981e055ec05f3d8dc3f649bd1d
691e9b9e8f181edc1686569c5bac1509b8a4b14a
c6d611f5739b826606813bca5d4bda7815b445e0
Changes for PR 85
Source Code | SHA256 |
---|---|
contracts/libraries/BalanceTracking.sol |
|
contracts/libraries/IndexPriceMargin.sol |
|
contracts/libraries/Trading.sol |
|
Changes for PR 86
Source Code | SHA256 |
---|---|
contracts/bridge-adapters/ExchangeStargateAdapter.sol |
|
contracts/libraries/Constants.sol |
|
contracts/libraries/Hashing.sol |
|
contracts/libraries/IndexPriceMargin.sol |
|
contracts/libraries/PositionBelowMinimumLiquidation.sol |
|
contracts/libraries/Structs.sol |
|
contracts/libraries/TradeValidations.sol |
|
contracts/libraries/Withdrawing.sol |
|
Changes for PR 87
Source Code | SHA256 |
---|---|
contracts/Exchange.sol |
|
contracts/libraries/Depositing.sol |
|
contracts/libraries/IndexPriceMargin.sol |
|
contracts/libraries/OraclePriceMargin.sol |
|
contracts/libraries/Withdrawing.sol |
|
contracts/util/ExchangeWalletStateAggregator.sol |
|
Changes for PR 88
Source Code | SHA256 |
---|---|
contracts/libraries/IndexPriceMargin.sol |
|
contracts/libraries/LiquidationValidations.sol |
|
contracts/libraries/Structs.sol |
|
contracts/libraries/WalletExitAcquisitionDeleveraging.sol |
|
contracts/libraries/WalletInMaintenanceAcquisitionDeleveraging.sol |
|
Changes for PR 89
Source Code | SHA256 |
---|---|
contracts/bridge-adapters/ExchangeStargateAdapter.sol |
|
Changes for PR 90
Source Code | SHA256 |
---|---|
contracts/bridge-adapters/ExchangeStargateAdapter.sol |
|
contracts/libraries/WalletExitAcquisitionDeleveraging.sol |
|
contracts/libraries/WalletInMaintenanceAcquisitionDeleveraging.sol |
|
Changes for PR 91
Source Code | SHA256 |
---|---|
contracts/Exchange.sol |
|
Changes for PR 93
Source Code | SHA256 |
---|---|
contracts/EarningsEscrow.sol |
|
Changes for PR 94
Source Code | SHA256 |
---|---|
contracts/Custodian.sol |
|
contracts/EarningsEscrow.sol |
|
contracts/Exchange.sol |
|
contracts/Governance.sol |
|
contracts/Owned.sol |
|
contracts/asset-migrators/USDCeMigrator.sol |
|
contracts/bridge-adapters/ExchangeStargateAdapter.sol |
|
Click on an issue to jump to it, or scroll down to see them all.
EarningsEscrow
call
instead of transfer
/send
for withdrawing tokens
chainId
in signature for distributing escrow earnings
external
instead of public
when not used internally
We quantify issues in three parts:
This third part – the severity level – is a summary of how much consideration the client should give to fixing the issue. We assign severity according to the table of guidelines below:
Severity | Description |
---|---|
(C-x) Critical |
We recommend the client must fix the issue, no matter what, because not fixing would mean significant funds/assets WILL be lost. |
(H-x) High |
We recommend the client must address the issue, no matter what, because not fixing would be very bad, or some funds/assets will be lost, or the code’s behavior is against the provided spec. |
(M-x) Medium |
We recommend the client to seriously consider fixing the issue, as the implications of not fixing the issue are severe enough to impact the project significantly, albiet not in an existential manner. |
(L-x) Low |
The risk is small, unlikely, or may not relevant to the project in a meaningful way. Whether or not the project wants to develop a fix is up to the goals and needs of the project. |
(Q-x) Code Quality |
The issue identified does not pose any obvious risk, but fixing could improve overall code quality, on-chain composability, developer ergonomics, or even certain aspects of protocol design. |
(I-x) Informational |
Warnings and things to keep in mind when operating the protocol. No immediate action required. |
(G-x) Gas Optimizations |
The presented optimization suggestion would save an amount of gas significant enough, in our opinion, to be worth the development cost of implementing it. |
Reference: PR94#R83
Quote token migration allows for the migration of the quote token from the initial source token to a different destination token. The protocol's intention is to initially use BWUSDC and later migrate it to the USDC.e representation, once it is in its final state.
However, the current implementation is vulnerable to a griefing attack, which allows an attacker to break the entire migration process. The problematic part lies in the Custodian.migrateAssets
function, specifically in the following require
statement:
// Entire balance must be migrated
require(
IERC20(destinationAsset).balanceOf(address(this)) == quantityInAssetUnits,
"Balance was not completely migrated"
);
Since the balances of destinationAsset
and the quantity sent to the assetMigrator
must match exactly, an attacker can transfer a small amount (1 unit is enough) of the destination token to the Custodian
contract. This action breaks the above check and causes the transaction to revert.
The issue is classified as medium severity, as it can be remediated by upgrading (with a 3-day delay) the USDCeMigrator
contract.
EarningsEscrow
Reference: PR93#R197
The _transferTo
function in EarningsEscrow.sol does not account for tokens applying transfer tax, which results in less value transferred to the walletOrContract
address than requested.
As a result, transferring out tokens from the contract will fail on the require statement on line L196:
require(
balanceAfter - balanceBefore == quantityInAssetUnits,
"Token contract returned transfer success without expected balance change"
);
This is because (balanceAfter - balanceBefore) will be smaller than the quantity transferred.
Consider handling fee-on-transfer tokens correctly by calculating balanceAfter - balanceBefore
of the EarningsEscrow contract itself as instead to the receiving wallet.
call
instead of transfer
/send
for withdrawing tokens
Reference: PR93#R188, PR93#L266
EarningsEscrow.sol supports earnings provided in native tokens when assetAddress
is set to 0x0
. If so, tokens are withdrawn using the .send
method:
if (asset == address(0x0)) {
require(walletOrContract.send(quantityInAssetUnits), "ETH transfer failed");
} else {
...
However, send
sends a fixed amount of gas, and assumes the address it sends the token to doesn’t have a fallback
or receive
function that runs code. If the address is a contract with a custom fallback function, like a multi-sig, then the send
will fail, and the receiving address will be prevented from withdrawing their funds.
The same issue applies with ExchangeStargateAdapter’s withdrawNativeAsset
function, which uses the transfer
function.
Use a low-level call
to transfer native tokens; doing so will allow any contract or EOA to withdraw without issue.
chainId
in signature for distributing escrow earnings
Reference: PR93#R171-R177
The signature used in EarningsEscrow doesn’t include a chainId
parameter. This makes the protocol susceptible to replay attacks when the protocol is deployed to multiple chains or when a hard fork of the chain happens.
Consider including the chainId
to the signature message.
EarningEscrow contracts are only deployed by an IDEX admin with the operational practice of using a different wallet per target chain. Our operational guide for this release includes:
“The EarningsEscrow contract replay mechanism only includes the contract’s address. When deploying a new contract, confirm that the new contract address is not already used elsewhere, for example on another chain.”
Reference: PR85#R268-R302
Due to new business requirements, the margin requirements have changed. The change requires to meet initialMarginRequirement
on trades when the buy or sell position is expanded and requires maintenanceMarginRequirement
on trades when the buy or sell position is reduced.
Consider updating the documentation properly to reflect the changed behavior.
external
instead of public
when not used internally
Throughout the code base, a lot of functions (e.g. public functions in Custodian, Exchange, ExchangeStargateAdapter, …) can be marked as external
instead of public
as they are not used internally. Consider changing them from public
to external
for clarity sake.
Macro makes no warranties, either express, implied, statutory, or otherwise, with respect to the services or deliverables provided in this report, and Macro specifically disclaims all implied warranties of merchantability, fitness for a particular purpose, noninfringement and those arising from a course of dealing, usage or trade with respect thereto, and all such warranties are hereby excluded to the fullest extent permitted by law.
Macro will not be liable for any lost profits, business, contracts, revenue, goodwill, production, anticipated savings, loss of data, or costs of procurement of substitute goods or services or for any claim or demand by any other party. In no event will Macro be liable for consequential, incidental, special, indirect, or exemplary damages arising out of this agreement or any work statement, however caused and (to the fullest extent permitted by law) under any theory of liability (including negligence), even if Macro has been advised of the possibility of such damages.
The scope of this report and review is limited to a review of only the code presented by the IDEX team and only the source code Macro notes as being within the scope of Macro’s review within this report. This report does not include an audit of the deployment scripts used to deploy the Solidity contracts in the repository corresponding to this audit. Specifically, for the avoidance of doubt, this report does not constitute investment advice, is not intended to be relied upon as investment advice, is not an endorsement of this project or team, and it is not a guarantee as to the absolute security of the project. In this report you may through hypertext or other computer links, gain access to websites operated by persons other than Macro. Such hyperlinks are provided for your reference and convenience only, and are the exclusive responsibility of such websites’ owners. You agree that Macro is not responsible for the content or operation of such websites, and that Macro shall have no liability to your or any other person or entity for the use of third party websites. Macro assumes no responsibility for the use of third party software and shall have no liability whatsoever to any person or entity for the accuracy or completeness of any outcome generated by such software.