Skip to content
Access control Audit finding Intermediate

wLp tokens could be stolen

The removeCollateralWLpTo function only checks that a tokenId belongs to the position when the full amount is removed, so an attacker can use another user's tokenId with a partial withdrawal to steal their wrapped LP tokens and rewards.

sashik_eth Follow with said

Originally disclosed on Code4rena

Submitted by sashik_eth, also found by said

PosManager#removeCollateralWLpTo function allows users to remove collateral wrapped in a wLp token that was previously supplied to the protocol:

File: PosManager.sol
249:     function removeCollateralWLpTo(uint _posId, address _wLp, uint _tokenId, uint _amt, address _receiver)
250:         external
251:         onlyCore
252:         returns (uint)
253:     {
254:         PosCollInfo storage posCollInfo = __posCollInfos[_posId];
255:         // NOTE: balanceOfLp should be 1:1 with amt
256:         uint newWLpAmt = IBaseWrapLp(_wLp).balanceOfLp(_tokenId) - _amt;
257:         if (newWLpAmt == 0) {
258:             _require(posCollInfo.ids[_wLp].remove(_tokenId), Errors.NOT_CONTAIN);
259:             posCollInfo.collCount -= 1;
260:             if (posCollInfo.ids[_wLp].length() == 0) {
261:                 posCollInfo.wLps.remove(_wLp);
262:             }
263:             isCollateralized[_wLp][_tokenId] = false;
264:         }
265:         _harvest(_posId, _wLp, _tokenId);
266:         IBaseWrapLp(_wLp).unwrap(_tokenId, _amt, _receiver);
267:         return _amt;
268:     }

This function could be called only from the core contract using the decollateralizeWLp and liquidateWLp functions. However, it fails to check if the specified tokenId belongs to the current position, this check would take place only if removing is full - meaning no lp tokens remain wrapped in the wLp (line 257).

This would allow anyone to drain any other positions with supplied wLp tokens. The attacker only needs to create its own position, supply dust amount in wLp to it, and call decollateralizeWLp with the desired ‘tokenId’, also withdrawn amount should be less than the full wLp balance to prevent check on line 257. An attacker would receive almost all lp tokens and accrued rewards from the victim’s wLp.

A similar attack for harvesting the victim’s rewards could be done through the liquidateWLp function.

Impact

Attacker could drain any wLp token and harvest all accrued rewards for this token.

Proof of Concept

The next test added to the tests/wrapper/TestWLp.sol file could show an exploit scenario:

    function testExploitStealWlp() public {
        uint victimAmt = 100000000;
        // Bob open position with 'tokenId' 1
        uint bobPosId = _openPositionWithLp(BOB, victimAmt);
        // Alice open position with 'tokenId' 2 and dust amount
        uint alicePosId = _openPositionWithLp(ALICE, 1);
        // Alice successfully de-collateralizes her own position using Bob's 'tokenId' and amounts less than Bob's position by 1 to prevent a revert
        vm.startPrank(ALICE, ALICE);
        initCore.decollateralizeWLp(alicePosId, address(mockWLpUniV2), 1, victimAmt - 1, ALICE);
        vm.stopPrank();

        emit log_uint(positionManager.getCollWLpAmt(bobPosId, address(mockWLpUniV2), 1));
        emit log_uint(IERC20(lp).balanceOf(ALICE));
    }

Recommended Mitigation Steps

Consider adding a check that position holds the specified token into the removeCollateralWLpTo function:

_require(__posCollInfos[_posId].ids[_wlp].contains(_tokenId), Errors.NOT_CONTAIN);

fez-init (INIT) confirmed


The original report as published on Code4rena, by sashik_eth. 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 ownership check for the tokenId is only performed when the withdrawal empties the wrapped balance, so partial withdrawals skip it.

Impact

An attacker could drain any wLp token and harvest all accrued rewards for that token.

What the attacker needed

Attacker has a position with dust wLp; victim has a position with wLp collateral

The fix

Add a check that the position holds the specified tokenId in the removeCollateralWLpTo function.

How it was found

  1. Attacker creates their own position and supplies a dust amount of wLp to it.
  2. Attacker calls decollateralizeWLp with the victim's tokenId and an amount less than the full wLp balance to avoid the ownership check.
  3. Attacker receives almost all LP tokens and accrued rewards from the victim's wLp.

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 wrapped token (wLp) represents a deposit in a liquidity pool. The contract lets users withdraw part of their deposit, but it forgets to verify that the token being withdrawn actually belongs to the caller's position unless the whole amount is taken. An attacker can therefore point to someone else's token and withdraw almost all of it.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by sashik_eth

View all

5 more disclosed reports, best first.

More from INIT Capital Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.