Excess ETH from forgingFee can get stuck in EntityForging under certain situations
The forgeWithListed function accepts more ETH than the required forging fee but only distributes the exact fee, leaving excess ETH permanently locked in the contract. An attacker could exploit this if the fee is reduced between transaction submission and execution, causing the overpayment to become stuck.
nnez Follow with Bauchibred, 0x3b and 7 others
Originally disclosed on Code4rena
Submitted by nnez, also found by Bauchibred, 0x3b, p0wd3r, samuraii77, FastChecker, 0xHash, pep7siup, broccolirob, and dontonka
ETH being permanently locked in the contract under certain situations.
Description
The forgeWithListed function contains a vulnerability in the fee checking mechanism. The line that checks the forging fee allows users to send more ETH than the required amount. Normally, users would want to send the exact amount of the forging fee to avoid overpayment. However, if the forging fee is somehow reduced between the time a user initiates a transaction and when it’s executed, the excess ETH becomes permanently locked in the contract.
See: EntityForging.sol#L102-L175:
uint256 forgingFee = _forgerListingInfo.fee;
require(msg.value >= forgingFee, 'Insufficient fee for forging');
// ... (later in the function)
uint256 devFee = forgingFee / taxCut;
uint256 forgerShare = forgingFee - devFee;
address payable forgerOwner = payable(nftContract.ownerOf(forgerTokenId));
// ... (fee distribution)
(bool success, ) = nukeFundAddress.call{ value: devFee }('');
require(success, 'Failed to send to NukeFund');
(bool success_forge, ) = forgerOwner.call{ value: forgerShare }('');
require(success_forge, 'Failed to send to Forge Owner');
The >= check in the require statement allows for overpayment, but the contract only distributes the exact forgingFee amount, leaving excess ETH locked in the contract.
Here is an example of a scenario that could get the fund stuck from allowing overpayment:
- Bob lists his forger entity with a fee of 0.1 ETH.
- Alice initiates a transaction to forge with Bob’s entity, sending exactly 0.1 ETH.
- Alice’s transaction is delayed or stuck due to gas price fluctuations or other network conditions.
- Before Alice’s transaction is executed, Bob reduces his forging fee to 0.08 ETH.
- Alice’s transaction is finally executed after the fee reduction.
- The contract accepts Alice’s 0.1 ETH, sends 0.08 ETH to Bob, and 0.02 ETH remains locked in the contract.
[Bob's Entity (Forger)]
|
Listing: Fee = 0.1 ETH
|
Alice sends 0.1 ETH
|
------------------------------
| Transaction Delayed |
------------------------------
|
Bob reduces fee to 0.08 ETH
|
-----------------------------
| Alice's Transaction |
| Executed After Delay |
-----------------------------
|
Contract Receives 0.1 ETH
|
-----------------------------
| Sends 0.08 ETH to Bob |
| 0.02 ETH Locked in Contract|
-----------------------------
Rationale for Severity
The severity is set to Medium because:
- It can cause financial losses to users, but only in limited scenarios.
- The impact is restricted to the difference between the original and reduced forging fee.
Recommended Mitigations
There are two available options:
- Only allow paying an exact fee.
- Allow overpayment but send excess amount back to
msg.sender.
Proof of Concept
The following test demonstrates that an overpayment is allowed, and excess amount get stuck in the contract. Combining with the detailed scenario above, it should prove the claimed impact.
- Place a new file,
Overpayment.test.tsinside/testdirectory with the test in the code block below. - Run
npx hardhat test test/Overpayment.test.ts. - Observe that the excess fund is stuck in
EntityForgingcontract.
import { parseEther } from 'ethers';
import {
Airdrop,
DevFund,
EntityForging,
EntropyGenerator,
EntityTrading,
NukeFund,
TraitForgeNft,
} from '../typechain-types';
import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers';
import generateMerkleTree from '../scripts/genMerkleTreeLib';
const { expect } = require('chai');
const { ethers } = require('hardhat');
import { fastForward } from '../utils/evm';
describe('EntityForging', () => {
let entityForging: EntityForging;
let nft: TraitForgeNft;
let owner: HardhatEthersSigner;
let user1: HardhatEthersSigner;
let user2: HardhatEthersSigner;
let user3: HardhatEthersSigner;
let entityTrading: EntityTrading;
let nukeFund: NukeFund;
let devFund: DevFund;
let FORGER_TOKEN_ID: number;
let MERGER_TOKEN_ID: number;
const FORGING_FEE = ethers.parseEther('1.0'); // 1 ETH
before(async () => {
[owner, user1, user2, user3] = await ethers.getSigners();
// Deploy TraitForgeNft contract
const TraitForgeNft = await ethers.getContractFactory('TraitForgeNft');
nft = (await TraitForgeNft.deploy()) as TraitForgeNft;
// Deploy Airdrop contract
const airdropFactory = await ethers.getContractFactory('Airdrop');
const airdrop = (await airdropFactory.deploy()) as Airdrop;
await nft.setAirdropContract(await airdrop.getAddress());
await airdrop.transferOwnership(await nft.getAddress());
// Deploy EntityForging contract
const EntropyGenerator = await ethers.getContractFactory(
'EntropyGenerator'
);
const entropyGenerator = (await EntropyGenerator.deploy(
await nft.getAddress()
)) as EntropyGenerator;
await entropyGenerator.writeEntropyBatch1();
entropyGenerator.connect(owner).transferOwnership(await nft.getAddress());
await nft.setEntropyGenerator(await entropyGenerator.getAddress());
// Deploy EntityForging contract
const EntityForging = await ethers.getContractFactory('EntityForging');
entityForging = (await EntityForging.deploy(
await nft.getAddress()
)) as EntityForging;
await nft.setEntityForgingContract(await entityForging.getAddress());
devFund = await ethers.deployContract('DevFund');
await devFund.waitForDeployment();
const NukeFund = await ethers.getContractFactory('NukeFund');
nukeFund = (await NukeFund.deploy(
await nft.getAddress(),
await airdrop.getAddress(),
await devFund.getAddress(),
owner.address
)) as NukeFund;
await nukeFund.waitForDeployment();
await nft.setNukeFundContract(await nukeFund.getAddress());
entityTrading = await ethers.deployContract('EntityTrading', [
await nft.getAddress(),
]);
// Set NukeFund address
await entityTrading.setNukeFundAddress(await nukeFund.getAddress());
const merkleInfo = generateMerkleTree([
owner.address,
user1.address,
user2.address,
]);
await nft.setRootHash(merkleInfo.rootHash);
// Mint some tokens for testing
await nft.connect(owner).mintToken(merkleInfo.whitelist[0].proof, {
value: ethers.parseEther('1'),
});
await nft.connect(user1).mintToken(merkleInfo.whitelist[1].proof, {
value: ethers.parseEther('1'),
});
await nft.connect(user1).mintToken(merkleInfo.whitelist[1].proof, {
value: ethers.parseEther('1'),
});
for (let i = 0; i < 10; i++) {
await nft.connect(owner).mintToken(merkleInfo.whitelist[0].proof, {
value: ethers.parseEther('1'),
});
const isForger = await nft.isForger(i + 4);
if (isForger) {
FORGER_TOKEN_ID = i + 4;
break;
}
}
MERGER_TOKEN_ID = 3;
});
describe('Overpayment is allowed', () => {
it.only('should demonstrate that overpayment is allowed and funds are stuck in EntityForging', async () => {
let initialForgingFee = ethers.parseEther('1');
let overpaymentForgingFee = ethers.parseEther('1.1');
let intialBalanceInEntityForging = await ethers.provider.getBalance(await entityForging.getAddress());
console.log("@> Forger lists entity with fee = 1 ETH");
await entityForging.connect(owner).listForForging(FORGER_TOKEN_ID, initialForgingFee);
console.log("@> Merger forges, sends fee = 1.1 ETH");
await entityForging.connect(user1).forgeWithListed(FORGER_TOKEN_ID, MERGER_TOKEN_ID, {
value: overpaymentForgingFee,
});
let finalBalanceInEntityForging = await ethers.provider.getBalance(await entityForging.getAddress());
console.log("@> Excess is stuck in EntityForging: ", ethers.formatEther(finalBalanceInEntityForging - intialBalanceInEntityForging), "ETH");
});
});
});
Koolex (judge) decreased severity to Low
@Koolex - I believe the severity of this finding should be reevaluated to Medium.
From C4 Severity Categorization:
“Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.”
This finding could fall under Medium category:
- Value leak: funds get stuck.
- Hypothetical path with stated assumptions: it requires a situation as described in my finding.
In addition, the root cause of this issue is that the contract allows forging with mismatched fees from users’ intention which doesn’t require users’ error as I suspected is the reason for downgrading to QA.
Koolex (judge) increased severity to Medium
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
Impact
Excess ETH sent by users can become permanently locked in the contract, causing financial loss, but only under specific conditions where the fee is reduced after transaction submission.
What the attacker needed
Forging fee is reduced after transaction submission; user sends more than the current fee
The fix
The contract should either require exact payment or refund any excess ETH to the sender.
How it was found
- Bob lists his forger entity with a fee of 0.1 ETH.
- Alice initiates a transaction to forge with Bob’s entity, sending exactly 0.1 ETH.
- Alice’s transaction is delayed or stuck due to gas price fluctuations or other network conditions.
- Before Alice’s transaction is executed, Bob reduces his forging fee to 0.08 ETH.
- Alice’s transaction is finally executed after the fee reduction.
- The contract accepts Alice’s 0.1 ETH, sends 0.08 ETH to Bob, and 0.02 ETH remains locked in the contract.
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 send money to a smart contract, the contract should either use it all or return the extra. Here, the contract accepts more money than needed but only pays out the required fee, so the extra money stays stuck in the contract forever. This can happen if the fee changes while your transaction is waiting to be processed.
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.