Skip to content
Race condition Audit finding Advanced

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.

Czar102 Follow

Originally disclosed on Code4rena

Submitted by Czar102, also found by 0x1f8b and SolidityScan

BribeVault.sol#L164-L205

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:

  1. Create a dummy token contract, reentrant in the transferFrom() function. All tokens are approved to the BriveVault and the attacker contract has unlimited tokens. Reentrancy aims back to a function in the attacker contract, which calls depositBribeERC20 again.
  2. The first call by the contract must use a novel bribeIdentifier. token is set to a dummy contract and amount to uint(-2).
  3. All checks pass, transferFrom is called, which calls attacker contract, which can call depositBribeERC20 again, this time will transfer 1 wei of a valuable token, using the same bribeIdentifier. 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 increases b.amount to 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 nonReentrant modifier 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.


The original report as published on Code4rena, by Czar102. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Create a dummy token contract whose transferFrom function reenters depositBribeERC20.
  2. Call depositBribeERC20 with a novel bribeIdentifier, the dummy token, and amount uint(-2).
  3. During transferFrom, reenter depositBribeERC20 with the same bribeIdentifier, a valuable token, and amount 1 wei.
  4. The inner call succeeds because the token has not been registered yet, and it records the valuable token and amount.
  5. 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.

More by Czar102

View all

12 more disclosed reports, best first.

More from Redacted Cartel contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.