Ballots not yet past their deadline are incorrectly looped too by tokenWhitelistingBallotWithTheMostVotes()
Submitted by t0x1c
Inside DAO.sol, _finalizeTokenWhitelisting() before finalizing a token whitelisting proposal makes a check that it’s the ballot with most votes:
249 // Fail to whitelist for now if this isn't the whitelisting proposal with the most votes - can try again later.
250 uint256 bestWhitelistingBallotID = proposals.tokenWhitelistingBallotWithTheMostVotes();
251 require( bestWhitelistingBallotID == ballotID, "Only the token whitelisting ballot with the most votes can be finalized" );
The tokenWhitelistingBallotWithTheMostVotes() function however does not care if the other ballots being looped through are yet not past their deadline. This is an incorrect approach because a ballot which has more Yes votes at this point of time may well turn into a rejected ballot by its deadline timestamp if additional No votes are cast in coming days. Hence using its current state to deny whitelisting of a proposal past its deadline is unfair & only contributes to delaying the timelines, since this may happen again & again which would result in the protocol repeatedly pushing the finalization time further & further away into the future.
- Either only ballots past their deadline should be considered, OR
- Only compare ballots which were created within a few hours of each other
Explanation through an example scenario (also provided in the form of a coded PoC in the next section):
- Dan, Alice and Bob stake some salt. Dan =
2,400,000, Alice =290,000and Bob =310,000 - Hence, total staked salt =
6,000,000 - Alice floats ProposalA for token whitelisting with deadline timestamp
t_a - Minimum quorum required is 20% of 6,000,000 =
600,000 - ProposalA receives votes :
YES = 310,000; NO = 290,000. Quorum reached. - Just a day before
t_a, Bob floats ProposalB for whitelisting with deadline timestampt_b - ProposalB receives votes :
YES = 600,000; NO = 0. Quorum reached. Also, ProposalB has moreYesvotes than ProposalA. - At timestamp
t_a,finalizeBallot()is called for ProposalA but it faces areverton L251 because ProposalB has greaterYesvotes - An hour before
t_b, Dan casts hisNovote for ProposalB which results in :YES = 600,000; NO = 2,400,000making it lose with overwhelming majority. - Note that instead of the above step, another possibility is that some of the current ‘Yes
voters change their vote toNo`. finalizeBallot()can be called again for ProposalA and it would pass now given that no other proposal has been floated meanwhile which has accumulated more votes, else we could keep going through the above cycle once again.
This caused a delay of around 11 days in ProposalA’s finalization. This works as a good attack vector for a malicious staker who wishes to postpone someone’s else proposal but does not have enough voting power to defeat it. With some positive votes coming their way from unsuspecting stakers, they can mount this attack. Here’s how:
- Alice and Bob stake some salt. Alice =
290,000and Bob =310,000. Bob is the malicious actor here. - Remaining salt is staked by other normal users amounting to
2,400,000 - Hence, total staked salt =
6,000,000 - Alice floats ProposalA for token whitelisting with deadline timestamp
t_a - Minimum quorum required is 20% of 6,000,000 =
600,000 - ProposalA receives votes :
YES = 315,000; NO = 310,000. Quorum reached. Bob had votedNo, but he could not overcome theYesvotes, getting beat by a small margin. - Bob attempts his attack. A few days before
t_a, Bob floats ProposalB for whitelisting with deadline timestampt_band votesYeshimself. - Some unsuspecting users don’t have anything against his proposal and choose to vote
Yes. At timestampt_ahis vote tally turns out to beYES = 320,000; NO = 319,990. In spite of being very close to losing the ballot, he still has moreYesvotes than ProposalA. - At timestamp
t_awhenfinalizeBallot()is called for ProposalA, it faces arevertdue to having lesser votes. Bob successfully delayed ProposalA’s finalization. - Bob was never really interested in whitelisting any token, so upon reaching close to his deadline
t_b, he can choose to change his votes toNoand let the proposal fail.
Proof of Concept
Add the following tests inside src/dao/tests/DAO.t.sol and run via COVERAGE="yes" NETWORK="sep" forge test -vv --rpc-url https://rpc.ankr.com/eth_sepolia --mt test_tokenWhitelistingDelayed to see the test pass:
function test_tokenWhitelistingDelayed() public
{
// *************** Setup steps ***************
deal(address(salt), address(DEPLOYER), 2_400_000 ether);
uint256 aliceStakedAmount = 290_000 ether;
uint256 bobStakedAmount = 310_000 ether;
deal(address(salt), address(alice), aliceStakedAmount);
deal(address(salt), address(bob), bobStakedAmount);
deal(address(salt), address(dao), 5000000 ether);
vm.prank(DEPLOYER);
staking.stakeSALT(2_400_000 ether);
vm.startPrank(bob);
salt.approve(address(staking), bobStakedAmount);
staking.stakeSALT(bobStakedAmount);
vm.stopPrank();
// *********************************************
// Alice stakes her SALT to get voting power
vm.startPrank(alice);
staking.stakeSALT(aliceStakedAmount);
// Propose a whitelisting ballot and cast vote
// Quorum required = 20% of (2_400_000 + 290_000 + 310_000) = 600_000
uint256 ballotID = 1;
IERC20 test = new TestERC20( "TEST", 18 );
proposals.proposeTokenWhitelisting(test, "url", "description");
proposals.castVote(ballotID, Vote.NO);
vm.stopPrank();
vm.prank(bob);
proposals.castVote(ballotID, Vote.YES); // @audit-info : ballot_1 quorum reached
// Increase block time to 1 hour before finalizing the ballot
skip(daoConfig.ballotMinimumDuration() - 1 hours);
// Bob proposes a whitelisting ballot and casts vote
vm.startPrank(bob);
IERC20 test2 = new TestERC20( "TEST2", 18 );
proposals.proposeTokenWhitelisting(test2, "url2", "description2");
proposals.castVote(ballotID + 1, Vote.YES);
vm.stopPrank();
vm.prank(alice);
proposals.castVote(ballotID + 1, Vote.YES); // @audit-info : ballot_2 quorum reached; All YES votes; deadline still in the future
// Increase block time to that of finalizing ballot_1
skip(1 hours);
// @audit : this would revert since Bob's ballot has more YES votes than Alice's
vm.expectRevert("Only the token whitelisting ballot with the most votes can be finalized");
dao.finalizeBallot(ballotID);
skip(daoConfig.ballotMinimumDuration() - 2 hours);
// more votes are cast for ballot_2
vm.prank(DEPLOYER);
proposals.castVote(ballotID + 1, Vote.NO); // @audit-info : ballot_2 now has NO > YES votes
// @audit : this would pass now
dao.finalizeBallot(ballotID);
// Check that the ballot is finalized
bool isBallotFinalized = !proposals.ballotForID(ballotID).ballotIsLive;
assertTrue(isBallotFinalized);
}
Tools used
Foundry
Recommended Mitigation Steps
- Either only ballots past their deadline should be considered, OR
- Only compare ballots which were created within a few hours of each other.
The following diff uses the first option:
// Returns the ballotID of the whitelisting ballot that currently has the most yes votes
// Requires that the quorum has been reached and that the number of yes votes is greater than the number no votes
function tokenWhitelistingBallotWithTheMostVotes() external view returns (uint256)
{
uint256 quorum = requiredQuorumForBallotType( BallotType.WHITELIST_TOKEN);
uint256 bestID = 0;
uint256 mostYes = 0;
for( uint256 i = 0; i < _openBallotsForTokenWhitelisting.length(); i++ )
{
uint256 ballotID = _openBallotsForTokenWhitelisting.at(i);
+ if (block.timestamp < ballots[ballotID].ballotMinimumEndTime)
+ continue;
uint256 yesTotal = _votesCastForBallot[ballotID][Vote.YES];
uint256 noTotal = _votesCastForBallot[ballotID][Vote.NO];
if ( (yesTotal + noTotal) >= quorum ) // Make sure that quorum has been reached
if ( yesTotal > noTotal ) // Make sure the token vote is favorable
if ( yesTotal > mostYes ) // Make sure these are the most yes votes seen
{
bestID = ballotID;
mostYes = yesTotal;
}
}
return bestID;
}
othernet-global (Salty.IO) confirmed and commented:
Removed maxPendingTokensForWhitelisting. There is now no limit to the number of tokens that can be proposed for whitelisting. Also, any whitelisting proposal that has reached quorum with sufficient approval votes can be executed.
Status: Mitigation confirmed. Full details in reports from 0xpiken, zzebra83, and t0x1c.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Salty.IO. Code4rena made it public on 19 April 2024. Found in the 2024-01-salty audit contest (finding M-14).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.