Skip to content
Business logic Audit finding Intermediate

Lack of safeApprove(0) prevents some registrations, and the changing of stakers and LP tokens

The contract calls OpenZeppelin's safeApprove without first resetting the allowance to zero, so a second approval to the same spender reverts. This prevents a second customer from registering with the same token and staker vault, and blocks changing back to a previous staker or LP token after a time-lock.

IllIllI Follow

Originally disclosed on Code4rena

Submitted by IllIllI, also found by defsec and Dravee

TopUpAction.sol#L50 LiquidityPool.sol#L721

OpenZeppelin’s safeApprove() will revert if the account already is approved and the new safeApprove() is done with a non-zero value

    function safeApprove(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

OpenZeppelin/SafeERC20.sol#L45-L58

Impact

Customers can be prevented from register()ing the same token/stakerVaultAddress as another customer; and once changed away from, stakers and lptokens can’t be used in the future.

Proof of Concept

There are multiple places where safeApprove() is called a second time without setting the value to zero first.

register() calls lockFunds() for each user registration, and since users will use the same tokens and staker vaults, the second user’s register() call will fail:

File: backd/contracts/actions/topup/TopUpAction.sol   #1

36       function lockFunds(
37           address stakerVaultAddress,
38           address payer,
39           address token,
40           uint256 lockAmount,
41           uint256 depositAmount
42       ) external {
43           uint256 amountLeft = lockAmount;
44           IStakerVault stakerVault = IStakerVault(stakerVaultAddress);
45
46           // stake deposit amount
47           if (depositAmount > 0) {
48               depositAmount = depositAmount > amountLeft ? amountLeft : depositAmount;
49               IERC20(token).safeTransferFrom(payer, address(this), depositAmount);
50               IERC20(token).safeApprove(stakerVaultAddress, depositAmount);

TopUpAction.sol#L36-L50

The changing of either the staker or an lp token is behind a time-lock, and once the time has passed, the changed variables rely on this function:

File: backd/contracts/pool/LiquidityPool.sol   #2

717       function _approveStakerVaultSpendingLpTokens() internal {
718           address staker_ = address(staker);
719           address lpToken_ = address(lpToken);
720           if (staker_ == address(0) || lpToken_ == address(0)) return;
721           IERC20(lpToken_).safeApprove(staker_, type(uint256).max);
722       }

LiquidityPool.sol#L717-L722

If a bug is found in a new staker or lpToken and the governor wishes to change back to the old one(s), the governor will have to wait for the time-lock delay only to find out that the old value(s) cause the code to revert.

I’ve filed the other more-severe instances as a separate high-severity issue, and flagged the remaining low-severity instances in my QA report.

Recommended Mitigation Steps

Always do safeApprove(0) if the allowance is being changed, or use safeIncreaseAllowance().

chase-manning (Backd) confirmed

samwerner (Backd) commented:

It should be noted that the second example referring to _approveStakerVaultSpendingLpTokens() is not an issue. This is neither a member variable that can be updated nor is it behind a time lock. Both the staker and lpToken can only be set once and hence the safeApprove in the aforementioned function can only be called once.

chase-manning (Backd) resolved resolved


The original report as published on Code4rena, by IllIllI. 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

Likely the code calls safeApprove to set a new non-zero allowance while a previous non-zero allowance to the same spender still exists, instead of resetting to zero or using safeIncreaseAllowance.

Impact

Some registrations can be blocked, and after a staker or LP token is changed away from, the old value cannot be restored because the approval reverts; the reporter notes the second example was disputed by the project as not exploitable.

What the attacker needed

Two users registering with the same token and staker vault; or a governor changing staker or LP token back

The fix

The code should reset the allowance to zero before setting a new value, or use safeIncreaseAllowance instead of safeApprove.

How it was found

  1. A first user registers with a given token and staker vault, which calls lockFunds and sets a non-zero allowance.
  2. A second user tries to register with the same token and staker vault, and the safeApprove call reverts because the allowance is already non-zero.

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 token allowance lets one contract spend another contract's tokens. The safeApprove helper only allows setting an allowance from zero to a new value, or resetting it to zero; it refuses to change a non-zero allowance directly to another non-zero value. Because the code never resets the allowance first, a second approval to the same address fails and the action that depends on it cannot complete.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by IllIllI

View all

119 more disclosed reports, best first.

More from Backd Tokenomics contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.