The addUnderlying
function within the LibFertilizer
library is responsible for adding underlying assets to the fertilizer system. It calculates the number of newly deposited beans to be minted based on the amount of tokens contributed and the current recapitalization status. Then, it calculates the number of beans to be added as liquidity provider (LP) tokens and mints them accordingly. Afterward, it transfers the necessary tokens to the Barn Raise Well and approves the transfer of beans to the well. Finally, it adds liquidity to the well, increments the underlying balances of Unripe Tokens, and updates the recapitalization status accordingly.
There is a potential reentrancy vulnerability in the addUnderlying
function. This vulnerability arises due to the transfer of tokens and subsequent approvals within the function. An attacker could exploit this by calling the addUnderlying function recursively before the first call completes, potentially causing unexpected behavior or loss of funds.
See the following code:
The impact of such a reentrancy attack could be significant. If an attacker manages to repeatedly call the addUnderlying
function before the previous calls complete, they could drain the contract of its tokens or cause it to behave unpredictably, disrupting its intended functionality and potentially causing financial losses for users.
Manual Review
One possible solution to mitigate this reentrancy vulnerability is to implement the checks-effects-interactions pattern. This pattern involves performing all necessary checks and calculations before interacting with external contracts. Specifically, in the addUnderlying
function, all calculations and state updates should be completed before any external calls or token transfers are made. Additionally, reentrancy guards can be employed using mutex-like mechanisms to prevent multiple calls to critical functions from overlapping.
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.