Missing check on helper contract allows arbitrary actions and theft of assets
The MagnetarOptionModule contract fails to whitelist the marketHelper address supplied by the user, allowing an attacker to provide a malicious helper that returns arbitrary calldata. This calldata is executed by the bigBang market, which trusts the Magnetar contract's approvals, enabling theft of collateral or borrowing against any user's position.
The MagnetarOptionModule contract implements the exitPositionAndRemoveCollateral function which allows users to do a series of operations which is irrelevant to the issue. The user passes in the variable data, and later, data.externalData is used to extract out relevant contract addresses. These are then checked against a whitelist.
if (data.externalData.bigBang != address(0)) {
if (!cluster.isWhitelisted(0, data.externalData.bigBang)) {
revert Magnetar_TargetNotWhitelisted(data.externalData.bigBang);
}
}
if (data.externalData.singularity != address(0)) {
if (!cluster.isWhitelisted(0, data.externalData.singularity)) {
revert Magnetar_TargetNotWhitelisted(data.externalData.singularity);
}
}
The main issue is that the data.externalData also has a marketHelper field which is not checked against a whitelist and ends up being used.
The helper contracts are used to construct the calldata for market operations. In the above snippet, the helper contract is passed in some data, and it is expected to create a calldata out of the passed in data. The expected output is the repay module and a call value which when executed, will repay for the data.user’s account.
However, since the marketHelper contract is never checked against a whitelist, malicious user can pass in any address in that place. So the above call can return any data payload, and the bigBang_.execute will execute it without any checks. This means the malicious helper contract can return a borrow payload of some random user, and the contract will end up borrowing USDO against that user’s position. The Magnetar contract is assumed to have approval for market operations, and thus the Magnetar’s approval is essentially exploited by the attacker to perform arbitrary actions on any user’s account.
This can be used by any user to steal collateral from other user’s bigbang position, or borrow out usdo tokens on their position. Since this is direct theft, this is a high severity issue.
Proof of Concept
The absence of checks is evident from the code snippet. Assuming marketHelper contract is malicious, we see that is used in 2 places to create payloads, which must also be deemed malicious.
These are then executed, and the Magnetar is assumed to have approvals from users, so these are obviously malicious interactions.
In the other module contracts, the marketHelper is checked against a whitelist, but not in this module. This is a clear oversight. Below is the example from the MagnetarMintCommonModule:
if (!cluster.isWhitelisted(0, marketHelper)) {
revert Magnetar_TargetNotWhitelisted(marketHelper);
}
Low/Invalid; even if the market helper is not checked (and I agree it’s ok to add that verification) the module which is going to be executed is checked on the BB/SGL side and the action that’s being performed also checks the allowances
/// @dev Modules will not return result data.
if (_action.id == MagnetarAction.OptionModule) {
_executeModule(MagnetarModule.OptionModule, _action.call);
continue; // skip the rest of the loop
}
User needs to give a lot of approve for magnetar contract to allow magnetar contract pull fund out of user’s account to complete transaction.
To prevent abuse of allowance, this check is made in-place.
function exitPositionAndRemoveCollateral(ExitPositionAndRemoveCollateralData memory data) public payable {
// Check sender
_checkSender(data.user);
Which calls:
function _checkSender(address _from) internal view {
if (_from != msg.sender && !cluster.isWhitelisted(0, msg.sender)) {
revert Magnetar_NotAuthorized(msg.sender, _from);
}
}
The from != msg.sender is super important, otherwise.
If user A gives allowance to magnetar contract, user B can set data.user to user A and steal fund from user A directly.
Lack of validation of market helper allows malicious actor executes arbitrary multicall. See here.
The module which is going to be executed is checked on the BB/SGL side and the action that’s being performed also checks the allowances.
This is the code in BBCollateral module:
bigBang_.execute multicall to bigBang module and one of the module is BBCollateral module:
function removeCollateral(address from, address to, uint256 share)
external
optionNotPaused(PauseType.RemoveCollateral)
solvent(from, false)
notSelf(to)
allowedBorrow(from, share)
{
_removeCollateral(from, to, share);
}
The validation that sponsor mentions is in the modifier:
allowedBorrow(from, share)
Which calls:
function _allowedBorrow(address from, uint256 share) internal virtual override {
if (from != msg.sender) {
// TODO review risk of using this
(uint256 pearlmitAllowed,) = penrose.pearlmit().allowance(from, msg.sender, address(yieldBox), collateralId);
require(allowanceBorrow[from][msg.sender] >= share || pearlmitAllowed >= share, "Market: not approved");
if (allowanceBorrow[from][msg.sender] != type(uint256).max) {
allowanceBorrow[from][msg.sender] -= share;
}
}
}
Obviously “from” is not msg.sender, but msg.sender is the magnetar contract that hold user’s allowance.
Protocol fix the lack of market helper validation in the other part of the codebase, see here. The exact same issue should be fixed in Option module as well.
Other way to abuse pending allowance is marked as high severity here.
This should be valid. According to the sponsor, even if the market helper is not checked the module which is going to be executed is checked on the BB/SGL side.
This is true. However the bigbang/sgl markets do the check on msg.sender, which is the magnetar contract itself, which is expected to have allowance from the users. Checks are not done on the initiator of this transaction. This is highlighted here and below.
function _allowedBorrow(address from, uint256 share) internal virtual override {
if (from != msg.sender) {
if (share == 0) revert AllowanceNotValid();
// TODO review risk of using this
(uint256 pearlmitAllowed,) = penrose.pearlmit().allowance(from, msg.sender, address(yieldBox), collateralId);
require(allowanceBorrow[from][msg.sender] >= share || pearlmitAllowed >= share, "Market: not approved");
if (allowanceBorrow[from][msg.sender] != type(uint256).max) {
allowanceBorrow[from][msg.sender] -= share;
}
}
}
Magnetar is a privileged contract, and this function allows other users to abuse this privilege. This is basically approval hijacking, and so is high severity.
While we switched the model to use “atomic” approvals using Pearlmit, it’s better to be safe than sorry. The reviewed code also still has an obsolete allowanceBorrow which could help initiate this attack.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The contract fails to validate the user-supplied marketHelper address against a whitelist before using it to construct calldata for execution.
Impact
An attacker could steal collateral from any user's bigBang position or borrow USDO against it, because the Magnetar contract's approvals are abused.
What the attacker needed
Victim has approved Magnetar for market operations; Attacker can deploy a malicious helper contract; Attacker knows victim's address
The fix
The helper contract should be checked against a whitelist, as is done in other modules.
How it was found
Attacker deploys a malicious contract implementing the IMarketHelper interface.
Attacker calls exitPositionAndRemoveCollateral with data.externalData.marketHelper set to the malicious contract and data.user set to a victim's address.
The malicious helper returns arbitrary calldata (e.g., a borrow or removeCollateral call) for the victim's account.
The Magnetar contract executes the calldata on the bigBang market, which trusts Magnetar's approvals, allowing theft of collateral or borrowing against the victim's position.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
This is a smart contract bug where a function lets users supply a helper contract address without checking it against a list of trusted contracts. An attacker can supply a malicious helper that returns instructions to move other people's funds, and because the main contract already has permission to move those funds, the attack succeeds. It's like letting someone write their own permission slip and then acting on 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.
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.