Skip to content
Access control Audit finding Advanced

Attacker can use MagnetarAction.OFT action of the Magnet to perform operations as any user including directly stealing user tokens

The Magnetar contract's OFT action did not verify the function selector in the calldata against the target address, and because Magnetar itself was whitelisted, an attacker could make it call itself and bypass the sender check. This allowed the attacker to impersonate any user and withdraw their collateral to an attacker-controlled address on another chain.

ronnyx2017 Follow with immeas, rvierdiiev and 3 others

Originally disclosed on Code4rena

Submitted by ronnyx2017, also found by immeas, carrotsmuggler (1, 2), GalloDaSballo (1, 2, 3), rvierdiiev, deadrxsezzz, cccz, and ladboy233

https://github.com/Tapioca-DAO/tapioca-periph/blob/032396f701be935b04a7e5cf3cb40a0136259dbc/contracts/Magnetar/Magnetar.sol#L153-L156

https://github.com/Tapioca-DAO/tapioca-periph/blob/032396f701be935b04a7e5cf3cb40a0136259dbc/contracts/Magnetar/Magnetar.sol#L325-L333

https://github.com/Tapioca-DAO/tapioca-periph/blob/032396f701be935b04a7e5cf3cb40a0136259dbc/contracts/Magnetar/MagnetarStorage.sol#L93-L97

This issue requires the combination of two vulnerabilities to achieve the impact described in the title. The first vulnerability is that the Magnetar._processOFTOperation function doesn’t check the function sigs in the action calldata with the the target addresses. It only ensures the calling target addresses are in the Whitelist of the Cluster. So an attacker can use this vuln to call any whitelist target address from the Magnetar.

The second vulnerability is that the Magnetar contract address itself will also be added to the Cluster whitelist. It can be found in the following integration test here. If the attacker can let the Magnetar call itself, the msg.sender in the sub-call will be in the whitelist. It will bypass the _checkSender check:

    function _checkSender(address _from) internal view {
        if (_from != msg.sender && !cluster.isWhitelisted(0, msg.sender)) {
            revert Magnetar_NotAuthorized(msg.sender, _from);
        }
    }

Impact

Combining the two issues mentioned above, we can carry out the following exploitation.

  1. Call Magnetar.burst function with _action.id == MagnetarAction.OFT, which will call _processOFTOperation function. The _target is the Magnetar contract itself, and the _actionCalldata is still an encoded calldata to call the Magnetar.burst function with _action.id == MagnetarAction.OFT again.
  2. In the second call, the msg.sender will be the Magnetar itself, so the from address check in the _checkSender function will be skipped directly because the msg.sender is in the whitelist.
  3. Now the attacker can pretend to be anyone and call any contract through the Magnetar. Please note that users approved their tokens to Magnetar if they used it.

Proof of Concept

Taking MagnetarAssetModule as an example, it demonstrates how an attacker could steal users’ collateral in the market.

Firstly the _checkSender(data.user); can be bypassed directly.

And make data.withdrawCollateralParams.withdraw = true, so the collateralWithdrawReceiver will be the Magnetar contract itself:

address collateralWithdrawReceiver = data.withdrawCollateralParams.withdraw ? address(this) : data.user;

Then if collateralShare > 0, the function will call _withdrawToChain(data.withdrawCollateralParams); to withdraw the collateral to the another chain by sending LZ message.

There is no check for withdrawCollateralParams in the _withdrawToChain function. The attacker can set the receiver address to his address and finally receive the tokens on the target chain.

Recommendation

Check the target function sig in the _processOFTOperation function.

cryptotechmaker (Tapioca) confirmed, but disagreed with severity and commented via duplicate Issue #170:

Medium, the same happens if you approve the attacker for an ERC20. However, this is worth fixing in my opinion. I think we can add specific selectors instead of allowing any call to be executed.

Fixed here.


The original report as published on Code4rena, by ronnyx2017. 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 missing validation of the function selector in the OFT action calldata combined with the Magnetar contract being whitelisted, allowing a self-call that bypasses the sender check.

Impact

An attacker could impersonate any user and withdraw their collateral to an attacker-controlled address on another chain, potentially stealing user tokens.

What the attacker needed

Users have approved tokens to Magnetar; Magnetar is whitelisted in the Cluster

The fix

The issue was fixed by adding specific selectors instead of allowing any call to be executed.

How it was found

  1. Call Magnetar.burst with action id OFT, setting the target to Magnetar itself and the calldata to another burst call with OFT action.
  2. In the second call, msg.sender is Magnetar, which is whitelisted, so the _checkSender check is skipped.
  3. Set withdrawCollateralParams.withdraw to true so the collateral withdraw receiver becomes the Magnetar contract.
  4. Call _withdrawToChain with the attacker's address as receiver to receive the tokens on the target chain.

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 smart contract called Magnetar lets users perform actions like withdrawing collateral. It checked who was asking but not what function they were calling. Because the contract was allowed to call itself, an attacker could trick it into thinking the request came from a trusted source, then act as any user and steal their funds.

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 ronnyx2017

View all

24 more disclosed reports, best first.

More from Tapioca 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.