Okay, let's analyze the provided code and documentation for potential critical issues and vulnerabilities in the lending-vault repository.
Critical Issues and Vulnerabilities
Here's a breakdown of the most concerning areas, categorized for clarity:
1. Lack of Insolvent Borrowable Contract Handling (High Severity)
- Problem: The
README.md explicitly states:
"Currently the LendingVault contract assumes that all Borrowable contracts are solvent. There isn't a mechanism in place to realize a loss in case one of the Borrowable contracts become insolvent..."
- Impact: If any of the underlying
Borrowable contracts (Impermax lending pools) become insolvent (e.g., due to a hack, bad debt, or oracle manipulation), the LendingVault will not be able to recover those funds. This will directly lead to a loss of user funds, which is the most critical vulnerability to fix before any further action is taken.
- Recommendation:
- Implement a mechanism to detect and handle insolvency events in
Borrowable contracts. This might involve:
- A circuit breaker that halts allocation to a specific
Borrowable upon detecting insolvency or low health factor
- A way to realize and distribute losses proportionally among vault token holders.
- A mechanism to allow the vault to try to unwind from an insolvent pool.
- An emergency withdraw function, that would be used in case any of the borrowable contracts are reported as insolvent by an oracle
- A way to writeoff the debt from the insolvent pool.
2. Reallocate Function Vulnerability (Medium-High Severity)
- Problem: The
README.md describes a vulnerability related to the permissionless reallocate() function:
"It is possible to force the vault to earn a suboptimal APR by calling reallocate in the middle of a flash loan. For instance, by depositing a large amount of funds in a Borrowable pool, calling reallocate(), and then withdrawing those funds right after..."
- Impact: This allows an attacker to manipulate the vault's allocation to withdraw liquidity from high-APR pools and make the vault earn a lower yield for some time, forcing users to receive a lower APR. While it might be fixed by a subsequent reallocate, this can still be exploited for some time and can create panic among users.
- Recommendation:
- Implement mitigations:
- Add a cooldown period for reallocations, preventing the immediate reallocation after a large deposit or flash loan.
- Implement checks within
reallocate() to detect sudden large changes in Borrowable balances.
- Implement a more sophisticated rebalancing algorithm that is harder to manipulate.
- If the main problem is flash loans, check for flash loans within the reallocate function, to block reallocates if a flash loan is active.
- Consider requiring a minimum time to elapse between reallocates.
3. Gas Limit Restriction and MAX_BORROWABLE_LENGTH (Medium Severity)
- Problem: The
README.md notes a hard cap on the number of Borrowable contracts:
"Due to gas limit restrictions there is a hard cap to the number of N (MAX_BORROWABLE_LENGTH). By default this parameter is set to 10..."
- Impact: This limits the vault's ability to diversify across a larger number of lending pools. If the number of borrowable contracts exceeds
MAX_BORROWABLE_LENGTH it will result in a failed transaction, which can lead to a denial of service.
- Recommendation:
- Increase the gas limit when deploying the contract, if the gas limit of the chain allows.
- Try to implement a more gas optimized code that would allow to increase the number of borrowable contracts without increasing too much the gas limit.
- Add a mechanism to add more borrowable contracts, for example adding a function that replaces the current list of borrowable contracts with a new one.
4. Lack of Input Validation and Potential Overflow/Underflow Issues (Medium Severity)
- Problem: The code has several places where there's a risk of overflow and underflow, especially when handling big numbers or doing calculations on the exchange rate, and with tokens that have a different number of decimal places. While the code uses
SafeMath from OpenZeppelin, it could still have issues related to integer division.
- Impact: Incorrect calculations could lead to loss of funds, incorrect balances, or unexpected contract behavior.
- Recommendation:
- Add input validation for all parameters passed to functions, specifically when dealing with the amount of tokens to mint, redeem, allocate or deallocate.
- Review all mathematical operations, and add checks where necessary to prevent overflow/underflow, specifically when doing divisions.
5. Potential Reentrancy Issues (Medium Severity)
- Problem: While not explicitly mentioned, the code interacts with external contracts (
Borrowable contracts) which could open potential reentrancy vulnerabilities if not handled carefully.
- Impact: A malicious
Borrowable contract could call back into the LendingVault during an operation, potentially leading to manipulation of balances.
- Recommendation:
- Implement reentrancy guards on the
LendingVault, especially when interacting with external contracts.
6. Centralized Admin Control (Medium Severity)
- Problem: The
LendingVault is heavily reliant on the admin account for critical actions like adding/removing Borrowable contracts, and setting the reserve factor.
- Impact: If the admin account is compromised, the entire vault is at risk.
- Recommendation:
- Implement a multi-signature wallet for admin control.
- Consider a governance mechanism to allow for more decentralized control.
7. Lack of Robust Testing (Medium Severity)
- Problem: While there are tests provided, they might not cover all the edge cases, particularly related to the issues described before.
- Impact: Undetected bugs and vulnerabilities can slip into production.
- Recommendation:
- Expand the test suite with more comprehensive tests, including edge cases, and more tests that check for the identified vulnerabilities (e.g. reentrancy, insolvency handling, overflows, flash loan manipulation).
- Implement fuzz testing to identify unexpected bugs.
8. Exchange Rate Manipulation
- Problem: The exchange rate is calculated in the
Borrowable contract and can be manipulated by a malicious actor, thus, the vault relies on the exchange rate of the borrowable contract to be correct.
- Impact: The lack of a mechanism to detect a malicious exchange rate change can lead to the loss of funds.
- Recommendation:
- Add a sanity check to the exchange rate, to make sure it is within a reasonable range.
- Add a mechanism to detect and halt allocation to a specific
Borrowable if a malicious exchange rate change is detected.
Specific Code Observations
BorrowableObject.js Tests:
- The tests for
BorrowableObject focus on math calculations, which is good.
- But lacks testing of edge cases like very big numbers or extreme changes in utilization rate.
- Does not test the case where the supply of a borrowable contract is very low.
LVAllocator.js Tests:
- Tests the allocation and deallocation logic, as well as the
tokensForAtMost and tokensForAtLeast methods.
- Lacks testing of edge cases, specially with the exchange rate, and big numbers.
LVSetter.js Tests:
- Tests the permissioned setters for the vault.
- Does not test the cases when the exchange rate is very high.
LendingVault.js Tests:
- The main file where all the contract interaction is tested.
- Includes a lot of scenarios, but lacks cases with very large exchange rates.
- It's hard to understand what is the expected behaviour of the tests.
- Includes calls to
printSnapshot, which should not be included on production code.
- The tests have a lot of comments which makes it hard to read.
- The tests lack a lot of assertions, making it hard to understand if the test is correct or not.
Recommendations
- Address Insolvency First: The most critical issue is the lack of handling insolvent
Borrowable contracts. This must be resolved before deployment.
- Mitigate Reallocate Attacks: Implement a more robust mechanism, like a cooldown period, to prevent exploitation of the
reallocate() function.
- Implement Input Validation: Add input validation for all parameters to prevent potential overflow/underflow and unexpected behaviour.
- Enhance Testing: Write more tests that focus on edge cases, reentrancy, and the vulnerabilities found during this analysis.
- Review Code Logic: Review all mathematical operations and logic that manipulates big numbers, the exchange rate, and the different parameters of the contract.
- Implement Reentrancy Guards: Add reentrancy guards to the
LendingVault to prevent malicious Borrowable contracts from exploiting the vault.
- Implement Multi-sig: Use a multi-signature wallet for admin control.
- Sanitize Exchange Rate: Add a mechanism to detect and halt allocations to a malicious
Borrowable contract in case the exchange rate is manipulated.
Conclusion
The lending-vault project has significant vulnerabilities, especially regarding insolvency handling, reallocate() manipulation, and lack of input validation. These issues must be addressed before deploying the contracts to a production environment to prevent potential loss of user funds. The tests can be used as a basis to improve the code, but they should not be considered as a robust testing suite.