Tokens can be stolen from other users who have approved Magnetar
Several Magnetar helper functions omitted the check that the caller matches the position owner, so a malicious user could pass another user's address and spend approvals those users had granted to Magnetar. This let the attacker drain approved tokens or lever up a victim's position toward liquidation.
Submitted by dirk_y, also found by Madalad, bin2chen, kutugu, Ack, 0xStalin (1, 2), 0xTheC0der, cergyk (1, 2), rvierdiiev, and erebus
The MagnetarV2.sol contract is a helper contract that allows users to interact with other parts of the Tapioca ecosystem. In order for Magnetar to be able to perform actions on behalf of a user, the user has to approve the contract as an approved spender (or equivalent) of the relevant tokens in the part of the Tapioca ecosystem the user wants to interact with.
In order to avoid abuse, many of the actions that Magnetar can perform are protected by a check that the owner of the position/token needs to be the msg.sender of the user interacting with Magnetar. However, there are some methods that are callable through Magnetar that don’t have this check. This allows a malicious user to use approvals other users have made to Magnetar to steal their underlying tokens.
Proof of Concept
As I mentioned above, many of the Magnetar methods have a check to ensure that the msg.sender is the “from” address for the subsequent interactions with other parts of the Tapioca ecosystem. This check is performed by the _checkSender method:
function _checkSender(address _from) internal view {
require(_from == msg.sender, "MagnetarV2: operator not approved");
}
This function does what it is designed to do, however there are some methods that don’t include this protection when they should.
One example is the MARKET_BUY_COLLATERAL action that allows a user to buy collateral in a market:
else if (_action.id == MARKET_BUY_COLLATERAL) {
HelperBuyCollateral memory data = abi.decode(
_action.call[4:],
(HelperBuyCollateral)
);
IMarket(data.market).buyCollateral(
data.from,
data.borrowAmount,
data.supplyAmount,
data.minAmountOut,
address(data.swapper),
data.dexData
);
}
In the market contract there is an underlying call to check whether the sender has the allowance to buy collateral:
function _allowedBorrow(address from, uint share) internal {
if (from != msg.sender) {
if (allowanceBorrow[from][msg.sender] < share) {
revert NotApproved(from, msg.sender);
}
allowanceBorrow[from][msg.sender] -= share;
}
}
Since the msg.sender from the perspective of the market is Magnetar, the user would need to provide a borrow allowance to Magnetar to perform this action through Magnetar.
However, you can see above in the MARKET_BUY_COLLATERAL code snippet that there is no call to _checkSender. As a result, a malicious user can now pass in an arbitrary data.from address to use the allowance provided by another user to perform an unauthorised action. In this case, the malicious user could lever up the user’s position to increase the user’s LTV and therefore push the user closer to insolvency; at which point the user can be liquidated for a profit.
Another example of this issue is with the depositRepayAndRemoveCollateralFromMarket method in MagnetarMarketModule.sol. In this instance a malicious user can drain approved tokens from any other user by depositing into the Magnetar yield box:
// deposit to YieldBox
if (depositAmount > 0) {
_extractTokens(
extractFromSender ? msg.sender : user,
assetAddress,
depositAmount
);
IERC20(assetAddress).approve(address(yieldBox), depositAmount);
yieldBox.depositAsset(
assetId,
address(this),
address(this),
depositAmount,
0
);
}
This is a small snippet from the underlying _depositRepayAndRemoveCollateralFromMarket method that doesn’t include a call to _checkSender and therefore the malicious user can simply set extractFromSender to false and specify an arbitrary user address.
Recommended Mitigation Steps
The _checkSender method should be used in every method in MagnetarV2.sol and MagnetarMarketModule.sol if it isn’t already.
0xRektora (Tapioca) confirmed via duplicate issue 106
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 missing caller-identity checks in some Magnetar action handlers, allowing an arbitrary 'from' or 'user' address to be supplied.
Impact
An attacker could spend other users' approvals to drain approved tokens or push a victim's position closer to liquidation for profit.
What the attacker needed
Victim has approved Magnetar; attacker can call Magnetar actions
The fix
The sender check should be applied in every Magnetar method that acts on a user's behalf.
How it was found
- A victim approves Magnetar to spend or borrow against their tokens.
- The attacker calls a Magnetar action that lacks the sender check.
- The attacker supplies the victim's address as the 'from' or 'user' value.
- Magnetar uses the victim's existing approval to move or lever the victim's tokens.
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 helper contract called Magnetar was allowed by users to move their tokens on their behalf. Some of its functions forgot to check that the person calling was the same person whose tokens were being used, so anyone could name another user and spend that user's permission.
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.