depositRepayAndRemoveCollateralFromMarket function of MagnetarAssetModule can’t be used on behalf of user
The depositRepayAndRemoveCollateralFromMarket function in MagnetarAssetModule pulls tokens from msg.sender instead of the intended user when called by a whitelisted address on behalf of a user. This causes tokens to be taken from the wrong address or the call to revert due to insufficient balance or allowance.
KIntern_NA Follow with GalloDaSballo
Originally disclosed on Code4rena
Submitted by KIntern_NA, also found by GalloDaSballo
Functions of Magnetar are intended to be callable from whitelisted addresses on behalf of users. This serves purposes such as allowing TOFT or USDT contracts to execute Magnetar functions during lzReceive (receiving tokens cross-chain).
All Magnetar functions use _checkSender to allow whitelisted sender:
function _checkSender(address _from) internal view {
if (_from != msg.sender && !cluster.isWhitelisted(0, msg.sender)) {
revert Magnetar_NotAuthorized(msg.sender, _from);
}
}
However, in the depositRepayAndRemoveCollateralFromMarket function of MagnetarAssetModule, it calls _extractTokens with the msg.sender address to pull tokens from the sender. Therefore, if msg.sender is different from data.user (the sender calling on behalf of the user), this function still pulls tokens from the sender. This behavior is incorrect, resulting in pulling tokens from the wrong address or reverting due to insufficient balance and allowance of msg.sender.
function depositRepayAndRemoveCollateralFromMarket(DepositRepayAndRemoveCollateralFromMarketData memory data)
public
payable
{
// Check sender
_checkSender(data.user);
...
// @dev deposit to YieldBox
if (data.depositAmount > 0) {
data.depositAmount = _extractTokens(msg.sender, assetAddress, data.depositAmount);
IERC20(assetAddress).approve(address(_yieldBox), 0);
IERC20(assetAddress).approve(address(_yieldBox), data.depositAmount);
_yieldBox.depositAsset(assetId, address(this), address(this), data.depositAmount, 0);
}
...
Impact
Senders will be at risk of losses due to mistakenly pulling tokens or facing a DOS attack when calling depositRepayAndRemoveCollateralFromMarket on behalf of the user. This intended functionality will be broken.
Recommended Mitigation Steps
Should _extractTokens from data.user instead of msg.sender.
data.depositAmount = _extractTokens(msg.sender, assetAddress, data.depositAmount);
cryptotechmaker (Tapioca) confirmed and commented:
Fixed here.
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 function uses msg.sender instead of data.user when extracting tokens, so it pulls funds from the caller rather than the intended user.
Impact
Whitelisted senders could lose tokens or face a denial of service when calling the function on behalf of a user, breaking the intended functionality.
What the attacker needed
Caller is whitelisted; caller acts on behalf of a user
The fix
The function was fixed to extract tokens from data.user instead of msg.sender.
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.
Magnetar lets trusted contracts act for users, like a helper taking actions on your behalf. But in one function, the helper mistakenly takes the money from itself instead of from the user it is helping. This means the helper loses funds or the action fails because the helper does not have enough money.
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.