Skip to content
Business logic Audit finding Beginner

Incorrect percentage calculation in NukeFund and EntityForging when taxCut is changed from default value

The contracts NukeFund and EntityForging calculate the developer's share by dividing the amount by taxCut, which only works correctly when taxCut is 10. If the owner changes taxCut to any other value, the percentage taken as fee becomes incorrect, leading to wrong fund distribution.

0xDarko Follow with Fitro, Abdessamed and 7 others

Originally disclosed on Code4rena

Submitted by 0xDarko, also found by Fitro, 0XRolko (1, 2), Abdessamed, 0xDazai, namx05, samuraii77, cryptomoon, perseus, dvrkzy, and 0xAadi

https://github.com/code-423n4/2024-07-traitforge/blob/279b2887e3d38bc219a05d332cbcb0655b2dc644/contracts/EntityForging/EntityForging.sol#L146

https://github.com/code-423n4/2024-07-traitforge/blob/279b2887e3d38bc219a05d332cbcb0655b2dc644/contracts/NukeFund/NukeFund.sol#L41

Impact

In both NukeFund::receive and EntityForging::forgeWithListed, if the owner changes taxCut from its default value of 10, the percentage calculations for fee distribution become severely inaccurate. This flaw can lead to:

  1. Incorrect distribution of funds between developers, users, and the protocol.
  2. Potential for significant financial losses or unintended gains.
  3. Undermining of the protocol’s economic model.
  4. Loss of user trust if discrepancies are noticed.

The root cause is the use of simple division instead of proper percentage calculation:

    // NukeFund
    uint256 devShare = msg.value / taxCut; // Calculate developer's share (10%)
    uint256 remainingFund = msg.value - devShare; // Calculate remaining funds to add to the fund

    // EntityForging
    uint256 devFee = forgingFee / taxCut;
    uint256 forgerShare = forgingFee - devFee;

Some mathematical examples:

  • When taxCut = 10, 1/10 = 0.1 = 10% (correct).
  • When taxCut = 5, 1/5 = 0.2 = 20% (intended 5%, actually 20%).
  • When taxCut = 20, 1/20 = 0.05 = 5% (intended 20%, actually 5%).

Proof of Concept

This is a test for the NukeFund, EntityForging is the same scenario just longer so I chose to send you the more concise test. We are going to set the taxCut to 5:

    function test_FundsRoundWrong() public {
        nukeFund.setTaxCut(5);
        uint256 devShare = 0.05 ether;
        uint256 initialDevBalance = nukeFund.devAddress().balance;
        uint256 initialNukeFund = nukeFund.getFundBalance();

        vm.prank(user1);

        address(nukeFund).call{value: 1 ether}("");

        uint256 finalDevBalance = nukeFund.devAddress().balance;
        uint256 devGained = finalDevBalance - initialDevBalance;

        console.log("Dev Balance Gained:", devGained);

        assertNotEq(address(nukeFund).balance, initialNukeFund + 0.95 ether);

        uint256 devBalance = nukeFund.devAddress().balance;

        assertNotEq(devBalance, initialDevBalance + devShare);
    }

You can clearly see when it’s supposed to be “5%” tax, it’s instead 20%, leading the dev to get 2e17 of the 1e18 and the NukeFund getting 8e17.

Tools Used

Foundry

Recommended Mitigation Steps

Implement a basis point (BPS) system for precise percentage calculations. This means now that with a BPS value of 10_000 you would represent 10% as 1000 and 5% in our tests as 500.

Add the following changes:

  1. At the top of EntityForging and NukeFund where state variables reside, add the following BPS state variable and change default taxCut of 10% to correspond to the BPS values:
+  uint256 private constant BPS = 10_000;
-  uint256 public taxCut = 10;
+  uint256 public taxCut = 10_000;
  1. Update the calculations in EntityForging and NukeFund where taxCut is used.
    // NukeFund
-    uint256 devShare = msg.value / taxCut; // Calculate developer's share (10%)
+    uint256 devShare = (msg.value * taxCut) / BPS;

    // EntityForging
-    uint256 devFee = forgingFee / taxCut;
+    uint256 devShare = (msg.value * taxCut) / BPS;
  1. Also consider adding a boundary setTaxCut to make sure taxCut isn’t greater than BPS:
  function setTaxCut(uint256 _taxCut) external onlyOwner {
+    require(_taxCut <= BPS, "Tax cut cannot exceed 100%");
    taxCut = _taxCut;
  }

Assessed type

Math

TForge1 (TraitForge) confirmed

ZdravkoHr (warden) commented:

@Koolex - I think this issue depends on assumptions that were not confirmed when the audit was running.

What we see in the codebase is a taxCut variable that represent a part of the whole. Nowhere it was said that this cut has to be in percents. In fact, the only place where percents are mentioned, is the second link attached to the report that says 10%. And really, when you divide by 10, you get 10%.

jesjupyter (warden) commented:

I think this issue is pretty easy to solve so it’s actually not a valid issue. Here, if we want 5% rate, we just set taxcut to 20. If we want 20%, set it to 5. Nothing will be changed, so this issue looks like an admin input error.

samuraii77 (warden) commented:

The sponsor has confirmed this one so your assumptions are incorrect. It is supposed to be a %.

Abdessamed (warden) commented:

@ZdravkoHr

Nowhere it was said that this cut has to be in percents

All examples in the docs are referred to a %, not a share.

In fact, the only place where percents are mentioned, is the second link attached to the report that says 10%. And really, when you divide by 10, you get 10%.

There is an explicit setTaxCut function on every contract that demonstrates tax fees can change.

@jesjupyter - What you are proposing is just a workaround. Semantically, the tax represents a fee percentage and that’s how it is expected by admins to work when calling setTaxCut.

Kalogerone (warden) commented:

The setTaxCut function is onlyOwner protected:

  function setTaxCut(uint256 _taxCut) external onlyOwner {
    taxCut = _taxCut;
  }

All owner actions are trusted. The owners can still set the taxCut to their desired value, so nothing is really broken. Nowhere it was mentioned that setting taxCut to 20 should mean 20%. TRUSTED function and still works anyway, probably a low/info.

Koolex (judge) commented:

It is clearly intended to be a percentage. We can see this from the comment in the code:

  // Fallback function to receive ETH and update fund balance
  receive() external payable {
    uint256 devShare = msg.value / taxCut; // Calculate developer's share (10%)
    uint256 remainingFund = msg.value - devShare; // Calculate remaining funds to add to the fund

contracts/NukeFund/NukeFund.sol:L38-L43

This is not an admin issue, but the code is not working as intended (i.e., percentage based calculations).

KupiaSec (warden) commented:

@Koolex - I don’t understand why it is clearly intended to be a percentage in the following code. I believe it is meant to be a denominator, as 1/10 equals 10%.

    uint256 public taxCut = 10;
        [...]
    uint256 devShare = msg.value / taxCut;  // Calculate developer's share (10%)

This is the code from a audit in which I participated.

        // 20% of last users receive rewards
        uint256 Amount;
        if (share == 0 && block.timestamp >= time) {
            share = totalSupply() / 5;
        }

Perhaps it would be better to use taxcut as a percentage, but that’s a design choice rather than an issue.


The original report as published on Code4rena, by 0xDarko. 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 code uses division by taxCut instead of multiplying by a percentage, so the calculation only yields the intended percentage when taxCut equals 10.

Impact

The incorrect fee calculation could lead to significant financial losses or unintended gains and undermine the protocol's economic model.

What the attacker needed

Owner changes taxCut from default; contract has funds or receives payments

The fix

The fix is to implement a basis point system where taxCut is expressed in basis points (e.g., 10_000 for 100%) and calculations use multiplication and division by 10_000, and to add a boundary check in setTaxCut.

How it was found

  1. Set taxCut to 5 using the setTaxCut function.
  2. Send 1 ether to the NukeFund contract.
  3. Observe that the developer receives 0.2 ether (20%) instead of the intended 0.05 ether (5%).

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.

These smart contracts take a small cut of each payment as a fee for the developer. The code assumes the fee setting is always 10, so it divides by that number. If the owner changes the setting to 5, the fee becomes 20% instead of 5%, which is not what anyone intended.

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 from TraitForge

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.