Closed sherlock-admin3 closed 6 months ago
Both functions underneath have nonreentrant?
1 comment(s) were left on this issue during the judging contest.
panprog commented:
invalid, report statements are incorrect, these are just 2 functions one after another, nothing can be reentered here
0bing076
medium
H-1 Reentrancy Vulnerability in ZivoeTranches.depositBoth(uint256,address,uint256,address) (src/ZivoeTranches.sol#322-325)
Summary
The ZivoeTranches contract is susceptible to a reentrancy attack due to the lack of proper reentrancy guard mechanisms in the depositBoth function. Additionally, the function does not follow the Checks-Effects-Interactions (CEI) pattern, which is crucial for preventing reentrancy attacks and ensuring the integrity of the contract's state. https://github.com/sherlock-audit/2024-03-zivoe/blob/main/zivoe-core-foundry/src/ZivoeTranches.sol?plain=1#L322#L323#L324 Label:(src/ZivoeTranches.sol) LoC #322-325 Severity:high
Vulnerability Detail
The
depositBoth
function in the ZivoeTranches contract is vulnerable to reentrancy attacks because it makes external calls todepositSenior
anddepositJunior
without implementing proper CEI checks. This vulnerability allows an attacker to repeatedly call the function before the state is updated, potentially draining funds from the contract. Furthermore, the function does not follow the CEI pattern, which involves performing all checks (validations) before making any state changes or external calls. This lack of pattern can lead to unexpected behaviour and potential loss of funds.Impact
If exploited, this vulnerability could allow an attacker to drain funds from the ZivoeTranches contract by repeatedly calling the
depositBoth
function before the contract's state is updated. This could have significant financial implications for the contract and its users, potentially leading to loss of trust and operational disruption.Code Snippet
Tool used
Manual Review
Recommendation
To mitigate this vulnerability, the ZivoeTranches contract should follow the CEI pattern by performing all checks and state updates before making external calls. This ensures that the contract's state is consistent and prevents reentrancy attacks.
Use the SafeERC20 library for safe token transfers to prevent unexpected behaviour and ensure the integrity of the contract's state. By addressing these issues, the ZivoeTranches contract can significantly enhance its security, protecting it from reentrancy attacks and ensuring the integrity of its state.
Proof of Concept We'll simulate an attack scenario where an attacker contract exploits the vulnerability. This POC will demonstrate how an attacker could drain funds from the ZivoeTranches contract by repeatedly calling the
depositBoth
function before the contract's state is updated. Attacker ContractThe attacker contract is initialized with the address of the ZivoeTranches contract and the address of the token to be drained. The attack function is called by the attacker. It approves the ZivoeTranches contract to spend a specified amount of tokens on behalf of the attacker. Then, it calls the
depositBoth
function of the ZivoeTranches contract, attempting to exploit the reentrancy vulnerability. Reentrancy Exploit: When thedepositBoth
function callsdepositSenior
, the Attacker contract'sdepositSenior
function is triggered. Since thedepositBoth
function has not yet updated its state (effects), the attacker can calldepositSenior
again before the first call is finished, draining more tokens than intended. The attacker contract includes a fallback function to receive Ether, which could be used to fund the attack. To help mitigate this vulnerability, the ZivoeTranches contract could follow the CEI pattern by performing all checks and state updates before making external calls. We've provided the POC to demonstrate the potential impact of a reentrancy vulnerability and highlights the importance of implementing proper security measures in smart contracts.