Reentrancy in depositBribeERC20 function
The depositBribeERC20 function in BribeVault is reentrant because it calls an attacker-supplied token contract before registering the token. An attacker with DEPOSITOR_ROLE can use a malicious token to reenter and inflate a bribe's amount to the maximum integer, allowing them to steal all funds when the bribe is withdrawn.
Submitted by Czar102, also found by 0x1f8b and SolidityScan
depositBribeERC20 function in BriveVault is reentrant in line 187, where an address supplied by the caller is called.
A bad actor that has DEPOSITOR_ROLE and is a contract can execute a folowing attack:
- Create a dummy token contract, reentrant in the transferFrom() function. All tokens are approved to the
BriveVaultand the attacker contract has unlimited tokens. Reentrancy aims back to a function in the attacker contract, which callsdepositBribeERC20again. - The first call by the contract must use a novel
bribeIdentifier.tokenis set to a dummy contract andamounttouint(-2). - All checks pass,
transferFromis called, which calls attacker contract, which can calldepositBribeERC20again, this time will transfer 1 wei of a valuable token, using the samebribeIdentifier. All checks pass as the previous token hasn’t been registered yet. Then, a valid transfer happens. After that, the amount is set to 1 wei and the token is saved. Event is emitted and the function returns value. Then, attacker function returns and dummy token returns. The operation is to increment amount in storage by the transfer value, which increasesb.amountto the maximum integer. The token is nonzero, so the if statement is passed.
Thus, an attacker can grant any amount of tokens from BriveVault to a certain bribe, stealing all the funds once the bribe will be withdrawn.
Recommended Mitigation Steps
Set bribe token before the transfer is made.
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
I do believe re-entrancy is possible, so I recommend the sponsor to add the
nonReentrantmodifier to the deposit function.I’ll keep the finding separate [from M-02] as this deals with reEntrancy. Mitigation would be to enforce a bribeIdentifier to be used for a specific token (and it being enforced), as well as adding
nonReentrant.Because the function is permissioned, I believe medium severity to be more appropriate.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The function performs an external call to transferFrom before updating the stored token and amount for the bribe, allowing reentrancy.
Impact
An attacker with DEPOSITOR_ROLE could inflate a bribe's amount to the maximum integer and steal all funds when the bribe is withdrawn.
What the attacker needed
Attacker has DEPOSITOR_ROLE; Attacker contract can call depositBribeERC20; Attacker can create a malicious token contract
The fix
The bribe token should be set before the transfer is made, and a nonReentrant modifier should be added to the deposit function.
How it was found
- Create a dummy token contract whose transferFrom function reenters depositBribeERC20.
- Call depositBribeERC20 with a novel bribeIdentifier, the dummy token, and amount uint(-2).
- During transferFrom, reenter depositBribeERC20 with the same bribeIdentifier, a valuable token, and amount 1 wei.
- The inner call succeeds because the token has not been registered yet, and it records the valuable token and amount.
- After the inner call returns, the outer call adds uint(-2) to the stored amount, causing an integer overflow to the maximum value.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A reentrancy attack happens when a contract calls another contract before it has finished updating its own records. The called contract can call back into the original function and exploit the incomplete state. Here, the vault calls a token contract before saving which token was deposited, so a malicious token can reenter and trick the vault into recording a huge deposit.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.