The _configureVaultConnectedMarkets function in the MarketMakingEngineConfigurationBranch contract contains a critical vulnerability that leads to arithmetic underflow and storage corruption. This occurs when the function attempts to access vault.connectedMarkets.length - 1 after failing to properly update the vault.connectedMarkets array. The issue allows an attacker to potentially manipulate the vault's connected markets or cause the contract to revert unexpectedly.
The function _configureVaultConnectedMarkets is designed to configure connected markets for a vault. It performs the following steps:
1. Loads the vault data using Vault.load(vaultId).
2. Pushes a new array to vault.connectedMarkets.
Accesses the last element of the array using vault.connectedMarkets.length - 1.
Adds the provided market IDs to the new array.
However, the push() operation fails to update the storage, leaving the vault.connectedMarkets array empty. When the function attempts to access vault.connectedMarkets.length - 1, it causes an arithmetic underflow because length is 0.
Code snippet:
Copy POC to path: test/audit-test/POCUncheckedArrayFuzzTest.sol
Run forge test --mc POCUncheckedArrayFuzzTest -vvv
Output:
Test Output: The vault.connectedMarkets.length remains 0 after the function call, despite the function attempting to push a new array.
Root Cause: The push() operation in the function fails to persist the new array in storage. This could be due to:
Incorrect storage handling (e.g., modifying a local copy instead of the actual storage).
Missing state updates after the push() operation.
Implications: The vault.connectedMarkets array is not updated, rendering the function ineffective. This prevents the protocol from correctly configuring connected markets for the vault.
Test Output: The function attempts to access vault.connectedMarkets.length - 1 when the array length is 0.
Root Cause: When vault.connectedMarkets.length is 0, the calculation length - 1 results in 2^256 - 1 due to underflow.
Implications: This underflow can lead to:
Accessing invalid memory locations, causing unexpected behavior.
Reverting the transaction, leading to a denial of service for legitimate users.
Test Output: The function emits the LogConfigureVaultConnectedMarkets event, suggesting it executed successfully.
Root Cause: The event emission occurs before the underflow or storage update failure is detected.
Implications: The function appears to execute successfully, but the vault.connectedMarkets array remains unchanged. This creates a discrepancy between the expected and actual state of the vault, potentially leading to:
Incorrect vault configurations.
Financial losses for users relying on the protocol's functionality.
Direct Risk to Funds:
Incorrectly configured vaults could lead to financial losses for users, as the protocol may not function as intended.
Attackers could exploit the underflow to manipulate vault configurations, potentially draining funds or causing other financial harm.
Severe Disruption of Functionality:
The function fails to update the vault.connectedMarkets array, preventing the protocol from correctly configuring connected markets.
This could lead to inconsistencies in the vault's state, affecting other functions that rely on the connected markets.
Denial of Service:
If the function reverts due to the underflow, it could prevent legitimate users from configuring vaults, leading to a denial of service.
Straightforward Attack Path:
The vulnerability is easily exploitable, as it only requires calling the _configureVaultConnectedMarkets function with valid inputs.
No special conditions or complex steps are needed to trigger the underflow.
High Probability of Exploitation:
The issue is present in a core function of the protocol, making it highly likely to be exploited if left unaddressed.
Foundry
Fix Storage Update:
Ensure vault.connectedMarkets.push() properly updates the storage.
Add checks to verify the array is not empty before accessing length - 1.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.