Attacker can steal victim’s oTAP position contents via MagnetarMarketModule#_exitPositionAndRemoveCollateral()
The MagnetarMarketModule's exit function accepts caller-controlled target addresses and a user parameter, so an attacker can direct it to transfer a victim's approved oTAP position to itself and then unlock the resulting shares to the attacker. This allows theft of twAML-locked oTAP positions once Magnetar implements ERC721 receiving.
Submitted by Ack, also found by zzebra83
NOTE: This vulnerability relies on the team implementing an onERC721Received() function in Magnetar. As is currently written, attempts to exit oTAP positions via Magnetar will always revert as Magnetar cannot receive ERC721s, despite this being the clear intention of the function. This code path was not covered in the tests. Once implemented, however, an attack vector to steal twAML-locked oTAP positions opens.
Also, this is a similar but distinct attack vector from #933.
MagnetarMarketModule#_exitPositionAndRemoveCollateral() is a complex function used to perform any combination of: exiting an oTAP position, unlocking a locked tOLP position, removing assets and collateral from Singularity/bigBang, and repaying loans. The function achieves this by employing separate “if” clauses for each task that the caller would like to perform. These clauses are entered based on flags the caller provides in the argument struct removeAndRepayData.
Along with the set of operations to perform, the caller also provides:
- address
userto operate on - address externalData.bigBang
- address externalData.singularity
- address yieldbox (obtained by calling the user-provided
ISingularity(externalData.singularity).yieldBox())
As the caller has full control of all of these parameters, he can execute attacks to steal assets that have been approved to Magnetar.
Impact
High - Theft of funds
Proof of concept
Unfortunately the Magnetar tests do not cover the case where we wish to exit our oTAP position, and the periphery testing infrastructure does not include helper functions for the oTAP workflows (at least that I was able to find). This makes a coded PoC difficult and time consuming. Please consider the following walkthrough and reach out if a coded example is necessary.
In this case, we’re going to assume that a victim wants to use this function as-designed to exit his twAML-locked oTAP position. In order to do so he needs to grant approval in for Magnetar to transfer his position.
Once approved, anyone can call _exitPositionAndRemoveCollateral() with his own set of target addresses (some valid Tapioca addresses, some attacker-owned contracts) and the approver as user.
The attack is as follows:
-
Victim approves Magnetar to control his oTAP positions
-
Attacker first calls
_exitPositionAndRemoveCollateral(), with:removeAndRepayData.exitData.exit = trueremoveAndRepayData.unlockData.unlock = true- The
userto steal from - the
tokenIdto exit + steal - removeAndRepayData.unlockData.target = an attacker-controlled contract that just passes when called with
.unlock()
-
Magnetar transfers the oTAP position to itself and exits the position. It retains the yieldbox shares at the end of the call
-
Attacker calls
_exitPositionAndRemoveCollateral(), with:- removeAndRepayData.exitData.exit = false
- removeAndRepayData.unlockData.unlock = true
- The
useris the attacker’s address for receiving the unlocked shares - the tokenId to exit + steal
- removeAndRepayData.unlockData.target = the real tOLP contract
(The attacker could have alternately used a similar bigBang attacker contract approach for removing the yieldbox shares in step 4 as in #933 )
function _exitPositionAndRemoveCollateral( // @audit called via delegatecall from core MagnetarV2 w/o any agument validation
address user,
ICommonData.ICommonExternalContracts calldata externalData,
IUSDOBase.IRemoveAndRepay calldata removeAndRepayData
) private {
IMarket bigBang = IMarket(externalData.bigBang); // @audit all 3 of these can be attacker controlled
ISingularity singularity = ISingularity(externalData.singularity);
IYieldBoxBase yieldBox = IYieldBoxBase(singularity.yieldBox());
uint256 tOLPId = 0;
if (removeAndRepayData.exitData.exit) { // @audit true, enter this block
require(
removeAndRepayData.exitData.oTAPTokenID > 0, // @audit oTAP ID we want to unlock+steal
"Magnetar: oTAPTokenID 0"
);
address oTapAddress = ITapiocaOptionsBroker(
removeAndRepayData.exitData.target // @audit target here is attacker-controlled, but retrieve the real oTAP address
).oTAP();
(, ITapiocaOptions.TapOption memory oTAPPosition) = ITapiocaOptions( // @audit get the oTAP postion we're exiting
oTapAddress
).attributes(removeAndRepayData.exitData.oTAPTokenID);
tOLPId = oTAPPosition.tOLP;
address ownerOfTapTokenId = IERC721(oTapAddress).ownerOf(
removeAndRepayData.exitData.oTAPTokenID
);
require(
ownerOfTapTokenId == user || ownerOfTapTokenId == address(this), // @audit owner is user, passes
"Magnetar: oTAPTokenID owner mismatch"
);
if (ownerOfTapTokenId == user) { // @audit true
IERC721(oTapAddress).safeTransferFrom( // @audit transfer the token here
user,
address(this),
removeAndRepayData.exitData.oTAPTokenID,
"0x"
);
}
ITapiocaOptionsBroker(removeAndRepayData.exitData.target) // @audit address is
.exitPosition(removeAndRepayData.exitData.oTAPTokenID);
if (!removeAndRepayData.unlockData.unlock) { // @audit unlock = true, skip this
IERC721(oTapAddress).safeTransferFrom(
address(this),
user,
removeAndRepayData.exitData.oTAPTokenID,
"0x"
);
}
}
// performs a tOLP.unlock operation
if (removeAndRepayData.unlockData.unlock) {
if (removeAndRepayData.unlockData.tokenId != 0) {
if (tOLPId != 0) {
require(
tOLPId == removeAndRepayData.unlockData.tokenId,
"Magnetar: tOLPId mismatch"
);
}
tOLPId = removeAndRepayData.unlockData.tokenId;
}
// @audit .target here is attacker controlled - just pass first time!
// Second time, the attacker specifies the real tOLP contract and themselves as "user" to have the tokens unlocked to their address
ITapiocaOptionLiquidityProvision(
removeAndRepayData.unlockData.target
).unlock(tOLPId, externalData.singularity, user);
}
Recommended Mitigation Steps
- Do not allow arbitrary address input in these complex, multi-use functions.
- Consider breaking this into multiple standalone functions
- Require user == msg.sender
> 0xRektora (Tapioca) confirmed
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 trusts caller-supplied addresses for external contracts and the user parameter without validating them against msg.sender or a whitelist.
Impact
An attacker could steal a victim's twAML-locked oTAP position, but only after the team implements the missing ERC721 receiving function in Magnetar.
What the attacker needed
Victim approves Magnetar for oTAP; Magnetar implements onERC721Received; attacker can call the function
The fix
The function should reject arbitrary address input, be split into separate functions, and require user to equal msg.sender.
How it was found
- Victim approves Magnetar to control his oTAP positions
- Attacker calls the exit function with exit and unlock flags set, the victim as user, the victim's tokenId, and an attacker-controlled unlock target
- Magnetar transfers the oTAP position to itself and exits it, retaining the yieldbox shares
- Attacker calls the exit function again with exit false, unlock true, the attacker as user, the same tokenId, and the real tOLP contract as target
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 user approves a helper contract (Magnetar) to move their oTAP position, which represents locked tokens. Because the helper lets the caller choose which contracts it talks to and whose position it handles, an attacker can make the helper move the victim's position to itself and then release the underlying value to the attacker.
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.