Skip to content
Business logic Audit finding Intermediate

Magnetar unwrap operations broken due to bad ownership and checks

The Magnetar contract's unwrap operation fails because it does not transfer the user's tokens to itself before calling the TOFT contract's unwrap function, which burns tokens from the caller's address. Additionally, the sender check validates the destination address instead of the owner, so the function cannot work as intended.

carrotsmuggler Follow

Originally disclosed on Code4rena

Submitted by carrotsmuggler

The _processWrapOperation function can be triggered on the Magnetar contract to wrap/unwrap the user’s tokens into or out of the TOFT contracts. This function allows 2 selectors, wrap and unwrap to be called.

The issue is that unwrap function has 2 issues which prevent it from working properly. Below is the code from the BaseTOFT.sol contract, showing the implementation of the unwrap: function.

function _unwrap(address _toAddress, uint256 _amount) internal virtual {
    _burn(msg.sender, _amount);
    vault.withdraw(_toAddress, _amount);
}

As seen here, the token is burnt from the msg.sender address. However, the _processWrapOperation function in the Magnetar contract does not transfer out the token from the user’s address to itself before calling _unwrap.

if (funcSig == ITOFT.wrap.selector || funcSig == ITOFT.unwrap.selector) {
    /// @dev Owner param check. See Warning above.
    _checkSender(abi.decode(_actionCalldata[4:36], (address)));
    _executeCall(_target, _actionCalldata, _actionValue, _allowFailure);
    return;
}

So the Magnetar contract isn’t in possession of the token that the TOFT contract is trying to burn. The Magnetar contract itself is not designed to hold user tokens, since anyone can claim them. Users should not send their tokens to the Magnetar contract manually and then call this function, since MEV bots can steal tokens from this contract by just calling the unwrap function before them. Due to this, there is no way for Magnetar to actually unwrap the tokens.

Secondly, the _checkSender function is used to check the first passed address against msg.sender. the issue is that the first address passed to the unwrap function is the destination address, not the owner’s address. The owner is assumed to be msg.sender. So this contract essentially only makes sure that the msg.sender matches the destination of the unwrapped tokens, which is not a useful check.

Since these two issues break the functionality of this function, this is a medium severity issue.

Proof of Concept

The fact that Magnetar isn’t designed to hold tokens is evident from the fact that any user can just call the unwrap function from magnetar and burn up any tokens it is currently holding. So the absence of the transferring of the token to the Magnetar contract itself causes an issue.

Recommended Mitigation Steps

The unwrap functionality should transfer the token from the msg.sender to itself first. This will ensure both that the caller owns the token and that the token is in the possession of the Magnetar contract. The _checkSender check for the unwrap case is unnecessary and prevents users from choosing a destination address.

Assessed type

Invalid Validation

cryptotechmaker (Tapioca) confirmed and commented:

PR here.


The original report as published on Code4rena, by carrotsmuggler. 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 Magnetar contract does not transfer the user's tokens to itself before calling the TOFT unwrap function, and it checks the destination address instead of the owner.

Impact

The unwrap functionality is broken, preventing users from unwrapping their tokens through Magnetar; additionally, any tokens held by Magnetar could be stolen by anyone calling unwrap.

What the attacker needed

Magnetar contract deployed; User has tokens to unwrap

The fix

The unwrap functionality should transfer the token from the msg.sender to itself first, and the _checkSender check for the unwrap case should be removed.

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.

This report is about a smart contract function that is supposed to let users unwrap tokens. The function is broken because it tries to burn tokens from the wrong address and checks the wrong person. As a result, users cannot unwrap their tokens through this contract.

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 carrotsmuggler

View all

42 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.

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.