Truncation in casting can lead to a founder receiving all the base tokens
The Token contract's initialize function truncates each founder's ownership percentage to uint8 when summing, allowing the total to pass the 100% check even when the actual sum exceeds 100. An attacker (the deployer) could set up founders with percentages that cause one founder to receive all base tokens.
Originally disclosed on Code4rena
Submitted by CertoraInc, also found by 0xSky, antonttc, bin2chen, izhuer, pcarranzav, peritoflores, PwnPatrol, rbserver, scaraven, Tomo, V_B, wagmi, and zzzitron
https://github.com/code-423n4/2022-09-nouns-builder/blob/7e9fddbbacdd7d7812e912a369cfd862ee67dc03/src/token/Token.sol#L71-L126 https://github.com/code-423n4/2022-09-nouns-builder/blob/7e9fddbbacdd7d7812e912a369cfd862ee67dc03/src/token/Token.sol#L88
The initialize function of the Token contract receives an array of FounderParams, which contains the ownership percent of each founder as a uint256. The initialize function checks that the sum of the percents is not more than 100, but the value that is added to the sum of the percent is truncated to fit in uint8. This leads to an error because the value that is used for assigning the base tokens is the original, not truncated, uint256 value.
This can lead to wrong assignment of the base tokens, and can also lead to a situation where not all the users will get the correct share of base tokens (if any).
Proof of Concept
To verify this bug I created a foundry test. You can add it to the test folder and run it with forge test --match-test testFounderGettingAllBaseTokensBug.
This test deploys a token implementation and an ERC1967 proxy that points to it, and initializes the proxy using an array of 2 founders, each having 256 ownership percent. The value which is added to the totalOwnership variable is a uint8, and when truncating 256 to fit in a uint8 it will turn to 0, so this check will pass.
After the call to initialize, the test asserts that all the base token ids belongs to the first founder, which means the second founder didn’t get any base tokens at all.
What actually happens here is that the first founder gets the first 256 token ids, and the second founder gets the next 256 token ids, but because the base token is calculated % 100, only the first 100 matters and they will be owned by the first owner.
This happens because schedule, which is equal to 100 / founderPct, will be zero (100 / 256 == 0 due to uint div operation), and the base token id won’t be updated in (baseTokenId += schedule) % 100 (this line contains another mistake, which will be reported in another finding). The place where it will be updated is in the _getNextTokenId, where it will be incremented by 1.
This exploit can work as long as the sum of the percents modulo 256 (truncation to uint8) is not more than 100.
// The relative path of this file is "test/FounderGettingAllBaseTokensBug.t.sol"
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import { Test } from "forge-std/Test.sol";
import { IManager } from "../src/manager/Manager.sol";
import { IToken, Token } from "../src/token/Token.sol";
import { TokenTypesV1 } from "../src/token/types/TokenTypesV1.sol";
import { ERC1967Proxy } from "../src/lib/proxy/ERC1967Proxy.sol";
contract FounderGettingAllBaseTokensBug is Test, TokenTypesV1 {
Token imp;
address proxy;
function setUp() public virtual {
// Deploying the implementation and the proxy
imp = new Token(address(this));
proxy = address(new ERC1967Proxy(address(imp), ""));
}
function testFounderGettingAllBaseTokensBug() public {
IToken token = IToken(proxy);
address chadFounder = address(0xdeadbeef);
address betaFounder = address(0xBBBBBBBB); // beta
// Creating 2 founders with `ownershipPct = 256`
IManager.FounderParams[] memory founders = new IManager.FounderParams[](2);
founders[0] = IManager.FounderParams({
wallet: chadFounder,
ownershipPct: 256,
vestExpiry: 1 weeks
});
founders[1] = IManager.FounderParams({
wallet: betaFounder,
ownershipPct: 256,
vestExpiry: 1 weeks
});
// Initializing the proxy with the founders data
token.initialize(
founders,
// we don't care about these
abi.encode("", "", "", "", ""),
address(0),
address(0)
);
// Asserting that the chad founder got all the base token ids
// (`tokenId % 100` is calculated to get the base token, so it is enough to check only the first 100 token ids)
for (uint i; i < 100; ++i) {
assertEq(token.getScheduledRecipient(i).wallet == chadFounder, true);
}
// Run with `forge test --match-test testFounderGettingAllBaseTokensBug`
// Results:
// [PASS] testFounderGettingAllBaseTokensBug() (gas: 13537465)
// Great success
}
Tools Used
Manual audit & foundry for the PoC
Recommended Mitigation Steps
Don’t truncate the founderPct variable to a uint8 when adding it to the totalOwnership variable, or alternatively check that it is less than type(uint8).max (or less or equal to 100). After applying this fix and running the test again, the result is:
[FAIL. Reason: INVALID_FOUNDER_OWNERSHIP()] testFounderGettingAllBaseTokensBug() (gas: 58674)
Alex the Entreprenerd (judge) commented:
While this creates other issues (infinite loop), I will judge it separately as if the infinite loop was fixed, founders would be getting above 100% allocation.
kulkarohan (Nouns Builder) confirmed
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
The Warden has shown how, due to a unsafe casting, and mixed usage of variables, what is supposed to be a percentage of tokens receiveable by founders can reach a number above 100%.
Beside an infinite loop, this can be used to sneakily set all future tokens, to have
tokenRecipient[_tokenId].walletset to a founder.I believe that a High Severity is not out of place for what is a unchecked overflow that can be abused, however, the specific attack vector that would trigger this can only be triggered at initial setup.
Only the deployers can set founders and their percentages to be above 100%, and this can only happen at deployment.
For that reason, I think Medium Severity is more appropriate.
I invite all users to verify that the sum of all
founderPctis way below 100% and I believe mitigation consists of either performing the safe cast, or changing the code to revert if allocation is above a more realistic value
kulkarohan (Nouns Builder) disagreed with severity and commented:
Per the following line, any founder that’s specified with a percentage ownership that results in the total ownership exceeding 100 will cause the function to revert: Token.sol#L88.
Therefore the function would never actually reach
uint256 schedule = 100 / founderPct;if the provided PoC were the case.There is however a small mistake in casting
uint8(founderPct), which is completely unnecessary on our part. But I believe this is a low/QA issue at best.
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 code casts the founder's percentage to uint8 before adding it to the total, causing truncation and allowing the sum to bypass the 100% limit check.
Impact
A founder could receive all base tokens, depriving other founders of their intended share; however, this can only be triggered at initial setup by the deployer.
What the attacker needed
Deployer controls founder percentages at setup
The fix
The code should not truncate the founder percentage to uint8 when summing, or should explicitly check that each percentage is within a valid range (e.g., less than or equal to 100).
How it was found
- Deploy the Token implementation and an ERC1967 proxy.
- Create an array of two founders, each with ownershipPct set to 256.
- Call initialize on the proxy with these founders.
- Observe that the first founder receives all base token IDs.
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 a smart contract sets up founders' ownership shares, it adds up their percentages to make sure they don't exceed 100%. But the code accidentally cuts off large numbers when adding them, so a very large percentage can wrap around to a small number and pass the check. This means a founder could end up with more than their fair share, or even all of the tokens.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.