Skip to content
Business logic Audit finding Intermediate

Users approved for a single token id cannot wit

A user approved for a single token id cannot withdraw or merge that token because the burn step clears approvals by calling approve with the caller as sender, which fails for a single-token approval. An attacker cannot exploit this; it is a functional bug that forces users to grant approval for all their tokens.

imsrybr0 Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

30694 - [SC - Low] Users approved for a single token id cannot wit...

Submitted on May 5th 2024 at 00:02:52 UTC by @imsrybr0 for Boost | Alchemix

Report ID: #30694

Report type: Smart Contract

Report severity: Low

Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/VotingEscrow.sol

Impacts:

  • Contract fails to deliver promised returns, but doesn't lose value

Description

Brief/Intro

Users approved for a single token id cannot withdraw or merge for that token id.

Vulnerability Details

Solidity
    function _isApprovedOrOwner(address _spender, uint256 _tokenId) internal view returns (bool) {
        address owner = idToOwner[_tokenId];
        bool spenderIsOwner = owner == _spender;
        bool spenderIsApproved = _spender == idToApprovals[_tokenId];
        bool spenderIsApprovedForAll = (ownerToOperators[owner])[_spender];
        return spenderIsOwner || spenderIsApproved || spenderIsApprovedForAll;
    }

    function merge(uint256 _from, uint256 _to) external {
        //...
        require(_isApprovedOrOwner(msg.sender, _from), "not approved or owner");
        require(_isApprovedOrOwner(msg.sender, _to), "not approved or owner");
        // ...

        _burn(_from, value0);
        _depositFor(_to, value0, end, _locked1.maxLockEnabled, _locked1, DepositType.MERGE_TYPE);
    }

    function withdraw(uint256 _tokenId) public nonreentrant {
        require(_isApprovedOrOwner(msg.sender, _tokenId), "not approved or owner");

       // ...

        _burn(_tokenId, value);

        emit Withdraw(msg.sender, _tokenId, value, block.timestamp);
    }

    function _burn(uint256 _tokenId, uint256 _value) internal {
        // ...
        approve(address(0), _tokenId);
        // ...
    }

    function approve(address _approved, uint256 _tokenId) public {
        address owner = idToOwner[_tokenId];
        // Throws if `_tokenId` is not a valid token
        require(owner != address(0), "owner not found");
        // Throws if `_approved` is the current owner
        require(_approved != owner, "Approved is already owner");
        // Check requirements
        bool senderIsOwner = (owner == msg.sender);
        bool senderIsApprovedForAll = (ownerToOperators[owner])[msg.sender];
        require(senderIsOwner || senderIsApprovedForAll, "sender is not owner or approved");
        // Set the approval
        idToApprovals[_tokenId] = _approved;
        emit Approval(owner, _approved, _tokenId);
    }

Both withdraw and merge check if the msg.sender is owner of the given token id or is approved to use it (either for all tokens from the same owner of specifically the one being used).

They will also burn the token after carrying on their logic and clear its approvals.

Approvals are cleared using approve(address(0), _tokenId) which will fail if a msg.sender is only approved for that token id specifically.

Impact Details

  • User approved for a single token cannot withdraw or merge.
  • Users need to give permission for all their tokens to another user when they want that user to carry withdraw or merge operations for them.

Recommendation

Use the _clearApproval(owner, _tokenId) to clear the approvals in the _burn function.

References

Proof of Concept

Solidity
    function testApproveSingleAndMerge() public {
        uint256 tokenId1 = createVeAlcx(admin, TOKEN_1, MAXTIME, false);
        uint256 tokenId2 = createVeAlcx(beef, TOKEN_1, MAXTIME, false);

        hevm.prank(admin);
        veALCX.approve(beef, tokenId1);

        hevm.prank(beef);
        veALCX.merge(tokenId1, tokenId2);

        assertEq(veALCX.lockedAmount(tokenId2), TOKEN_1 + TOKEN_1);
    }

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

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 burn function clears approvals by calling approve(address(0), _tokenId), which requires the caller to be the owner or approved for all tokens, so a caller approved only for that token id fails the check.

Impact

A user approved for a single token cannot withdraw or merge it, so owners must grant approval for all their tokens instead.

What the attacker needed

A token owner approves another account for one token id; the approved account calls withdraw or merge

The fix

The burn function should clear approvals with an internal helper that does not require the caller to be the owner or an all-token approver.

How it was found

  1. Create two tokens owned by different accounts.
  2. The owner of the first token approves a second account for that single token id.
  3. The second account calls merge with the first token as source and its own token as destination.
  4. The merge reverts because the approval-clearing call fails.

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.

In this smart contract, a token owner can let someone else act on their token. They can approve one specific token, or approve all their tokens. The code that removes a token calls a function that only the owner or an all-token approver is allowed to call, so a person approved for just one token hits an error and cannot finish the action.

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 imsrybr0

View all

12 more disclosed reports, best first.

More from Alchemix

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.