tOLP positions created through MagnetarAction.Permit can be stolen
The Magnetar contract's Permit action validates only that the first calldata argument equals msg.sender, so an attacker can call approve or setApprovalForAll with themselves as the operator and then transfer out any tokens held by Magnetar. Combined with TapToken leaving a user's tOLP position stuck in Magnetar, an attacker could steal that position.
Submitted by immeas, also found by immeas
Description
This issue is a combination of three other issues:
MagnetarAction.TapTokenintegration will leave tokens stuck inMagnetarcontract.- A lot of calls in
MagnetarAction.Permitenables anyone to steal whitelisted tokens held byMagnetar. - Anyone can take any whitelisted tokens approved to
Magnetar.
In short, the first issue describes that, due to how MagnetarAction.TapToken is setup, it will leave the tOLP position a user creates through TapiocaOptionBroker::participate stuck in the Magnetar contract. As it mints the position to msg.sender which will be the Magnetar contract:
File: tap-token/contracts/options/TapiocaOptionBroker.sol
301: oTAPTokenID = oTAP.mint(msg.sender, lockExpiry, uint128(target), _tOLPTokenID);
The two following ones:
Firstly, describes how anyone can take any tokens that are in the Magnetar contract by granting themselves permissions to transfer any whitelisted tokens in using MagnetarAction.Permit. Because of how MagnetarStorage::_checkSender validates that the first argument in the calldata to MagnetarAction.Permit is the same as msg.sender. This together with that a lot of the calls allowed through MagnetarAction.Permit (IYieldBox::setApprovalForAll, IYieldBox::setApprovalForAsset, IERC20::approve, and IERC721::approve) have address to; i.e. the operator/approvee as the first argument. Hence, any user can approve themselves to transfer tokens out of the contract.
Secondly, by using MagnetarAction.OFT; which allows anyone to transfer tokens out of Magnetar (and steal any approved tokens to Magnetar) since there is an unvalidated call done to any whitelisted contract in MagnetarAction.OFT.
The contract is not in itself supposed to hold any tokens; in itself these issues are not that severe by themselves. However, these issues combined allows an attacker to steal the position completely. Since the first one makes the position be stuck in Magnetar and the two last ones makes it not actually stuck, but retrievable by anyone.
Impact
If a user uses MagnetarAction.TapToken to create their position they can have their position and/or rewards stolen.
Proof of Concept
Test in tap-token/test/Magnetar.t.sol, builds on the test described in MagnetarAction.TapToken integration will leave tokens stuck in Magnetar contract:
address attacker = makeAddr("Attacker");
function testStealTokensStuckInMagnetar() public {
testMagnetarParticipateOTAPStuckInMagnetar();
MagnetarCall memory approve = MagnetarCall({
id: MagnetarAction.Permit,
target: address(oTAP),
value: 0,
allowFailure: false,
call: abi.encodeWithSelector(IERC721.setApprovalForAll.selector, address(attacker), true)
});
MagnetarCall[] memory calls = new MagnetarCall[](1);
calls[0] = approve;
vm.startPrank(attacker);
magnetar.burst(calls);
oTAP.transferFrom(address(magnetar), address(attacker), 1);
vm.stopPrank();
assertEq(oTAP.ownerOf(1),address(attacker));
}
Full test with setup can be found here.
Recommended Mitigation Steps
Consider implementing the mitigations described in the three mentioned referenced issues:
- Adding the ability for the caller to declare a receiver in
TapiocaOptionBroker::participateandexerciseOption. Similar to how it’s done inTapiocaOptionLiquidityProvision::lock. - Rethinking how
MagnetarAction.Permitshould work. As there is support in modules for a lot of the calls it is used for perhaps it is unnecessary to have. - Removing the general
MagnetarAction.OFTcall. Most of the interactions with the contracts in the Tapioca ecosystem is already handled in the Magnetar module system which handles approvals and transfers.
Assessed type
Invalid Validation
cryptotechmaker (Tapioca) disagreed with severity and commented:
The last 2 seems possible only if you approve the attacker, which is valid even for any random ERC20.
The first one can be an issue, but I don’t think the severity should be H. Maybe Medium or Low. I’ll let @0xRektora confirm as well.
0xRektora (Tapioca) confirmed and commented:
While this is a valid finding, the probability of it happening are very low. Will still put it as
mediumdue to the nature of the issue.MagnetarAction.TapToken integration will leave tokens stuck in Magnetar contract. A lot of calls in MagnetarAction. Permit enables anyone to steal whitelisted tokens held by Magnetar.
We don’t actually use TapToken singular actions, instead we use the
MagnetarOptionModule. As for permits, we useTapiocaOmnichainEngine/OFTfor that.The probability are very low because the flow of action taken to the casual user is dictated by a different code. While this is possible to happen it’d have to be an advanced user, who probably will batch the actions together of permitting/locking/sending the token to his address. The Tx will revert if those are not done in the same Tx using Magnetar batching.
LSDan (judge) decreased severity to Medium
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 Permit action's sender check only compares the first calldata argument to msg.sender, which for approve and setApprovalForAll is the operator/approvee, allowing an attacker to approve themselves.
Impact
If a user creates a position through MagnetarAction.TapToken, an attacker could steal the position and/or its rewards, though the project says the probability is very low because normal flows use different modules.
What the attacker needed
Victim uses MagnetarAction.TapToken; attacker can call Magnetar burst
The fix
The report recommends letting callers declare a receiver in participate and exerciseOption, rethinking or removing MagnetarAction.Permit, and removing the general MagnetarAction.OFT call.
How it was found
- A user creates a tOLP position through MagnetarAction.TapToken, and the position is minted to the Magnetar contract.
- The attacker calls MagnetarAction.Permit targeting the oTAP token with setApprovalForAll, naming themselves as operator.
- The attacker transfers the position token from Magnetar to their own address.
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 run several actions in one transaction. One action, Permit, was supposed to let a user approve tokens for themselves, but the check only looked at the first argument of the call. Because approve functions take the person being approved as that first argument, an attacker could put their own address there and gain permission to move tokens out. Separately, a user's position token was being left inside Magnetar, so the attacker could take 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.