BondNFT.sol#claim() needs to correct all the missing epochs
The claim function in BondNFT.sol only updates the reward-per-share accumulator for the current epoch when an expired bond is claimed, skipping all epochs between the bond's expiry and the current epoch. This can cause some users to lose rewards, others to receive undeserved rewards, and some rewards to become locked in the contract.
Originally disclosed on Code4rena
Submitted by 141345
Impact
In BondNFT.sol#claim(), accRewardsPerShare[][] is amended to reflect the expired shares. But only accRewardsPerShare[bond.asset][epoch[bond.asset]] is updated. All the epochs between bond.expireEpoch-1 and epoch[bond.asset] are missed.
However, some users claimable rewards calculation could be based on the missed epochs. As a result, the impact might be:
accRewardsPerShareis inaccurate for the epochs in between.- Some users could lose reward due to wrong
accRewardsPerShare, some users might receive undeserved rewards. - Some rewards will be locked in the contract.
Proof of Concept
The rationale behind the unchecked block below seems to take into account the shares of reward of the expired bond. However, if you only update the latest epoch data, the epochs in between could have errors and lead to loss of other users.
File: contracts/BondNFT.sol
168: function claim(
169: uint _id,
170: address _claimer
171: ) public onlyManager() returns(uint amount, address tigAsset) {
177: if (bond.expired) {
178: uint _pendingDelta = (bond.shares * accRewardsPerShare[bond.asset][epoch[bond.asset]] / 1e18 - bondPaid[_id][bond.asset]) - (bond.shares * accRewardsPerShare[bond.asset][bond.expireEpoch-1] / 1e18 - bondPaid[_id][bond.asset]);
179: if (totalShares[bond.asset] > 0) {
180: accRewardsPerShare[bond.asset][epoch[bond.asset]] += _pendingDelta*1e18/totalShares[bond.asset];
181: }
182: }
183: bondPaid[_id][bond.asset] += amount;
Users can claim rewards up to the expiry time, based on accRewardsPerShare[tigAsset][bond.expireEpoch-1]:
235: function idToBond(uint256 _id) public view returns (Bond memory bond) {
238: bond.expired = bond.expireEpoch <= epoch[bond.asset] ? true : false;
239: unchecked {
240: uint _accRewardsPerShare = accRewardsPerShare[bond.asset][bond.expired ? bond.expireEpoch-1 : epoch[bond.asset]];
241: bond.pending = bond.shares * _accRewardsPerShare / 1e18 - bondPaid[_id][bond.asset];
TriHaz (Tigris Trade) acknowledged, but disagreed with severity and commented:
Acknowledged, we cant redistribute past rewards accurately because it would cost too much gas.
I would downgrade it to Medium risk, needs an opinion from judge.
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
The Warden has shown how, due to how epochs are handled, some rewards could be lost unless claimed each epoch.
Because the finding pertains to a loss of Yield, I agree with Medium Severity.
GainsGoblin (Tigris Trade) commented:
It is not feasible to update accRewardsPerShare for every epoch during which bond was expired. This issue is mitigated by the fact that anyone can release an expired bond, so the small difference in yield shouldn’t affect users that much.
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 claim function updates only the current epoch's accRewardsPerShare instead of iterating over all epochs from bond.expireEpoch-1 to the current epoch.
Impact
Some users could lose rewards, others might receive undeserved rewards, and some rewards will be locked in the contract; the team notes that anyone can release an expired bond, so the difference in yield may be small.
What the attacker needed
A bond that has expired; multiple epochs have passed since expiry; someone claims the expired bond
The fix
The contract should update accRewardsPerShare for every epoch between the bond's expiry and the current epoch, though the team says this would cost too much gas.
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.
A smart contract tracks rewards per share for each epoch. When a bond expires, the contract should update the reward tracking for every epoch since expiry, but it only updates the latest one. This means rewards for the skipped epochs are not counted correctly, so some people get too little and others too much.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.