Loss of funds during user adjusting
When the owner adjusts a user's total claimable token amount, the claim function later resets the total back to the originally purchased amount, discarding the adjustment. This causes the user to lose the extra tokens that were added or to have a decrease ignored, leading to a loss of funds.
0xMAKEOUTHILL Follow with Yuki
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-tokensoft-judging/issues/195
Found by
0xMAKEOUTHILL, Yuki
Summary
Adjusting a user's total claimable value not working correctly
Vulnerability Detail
Whenever the owner is adjusting user's total claimable value, the records[beneficiary].total is decreased or increased by uint256 diff = uint256(amount > 0 ? amount : -amount);.
However some assumptions made are not correct. Scenario:
- User has bought 200 FOO tokens for example.
- In
PriceTierVestingSale_2_0.solhe calls theinitializeDistributionRecordwhich sets hisrecords[beneficiary].totalto the purchased amount || 200. So records[beneficiary].total = 200 - After that the owner decides to adjust his
records[beneficiary].totalto 300. So records[beneficiary].total = 300 - User decides to claim his claimable amount which should be equal to 300. He calls the
claimfunction inPriceTierVestingSale_2_0.sol.
function claim(
address beneficiary // the address that will receive tokens
) external validSaleParticipant(beneficiary) nonReentrant {
uint256 claimableAmount = getClaimableAmount(beneficiary);
uint256 purchasedAmount = getPurchasedAmount(beneficiary);
// effects
uint256 claimedAmount = super._executeClaim(beneficiary, purchasedAmount);
// interactions
super._settleClaim(beneficiary, claimedAmount);
}
As we can see here the _executeClaim is called with the purchasedAmount of the user which is still 200.
function _executeClaim(
address beneficiary,
uint256 _totalAmount
) internal virtual returns (uint256) {
uint120 totalAmount = uint120(_totalAmount);
// effects
if (records[beneficiary].total != totalAmount) {
// re-initialize if the total has been updated
_initializeDistributionRecord(beneficiary, totalAmount);
}
uint120 claimableAmount = uint120(getClaimableAmount(beneficiary));
require(claimableAmount > 0, 'Distributor: no more tokens claimable right now');
records[beneficiary].claimed += claimableAmount;
claimed += claimableAmount;
return claimableAmount;
}
Now check the if statement:
if (records[beneficiary].total != totalAmount) {
// re-initialize if the total has been updated
_initializeDistributionRecord(beneficiary, totalAmount);
}
The point of this is if the total of the user has been adjusted, to re-initialize to the corresponding amount, but since it's updated by the input value which is 200, records[beneficiary].total = 200 , the user will lose the 100 added from the owner during the adjust
Impact
Loss of funds for the user and the protocol
Code Snippet
Tool used
Manual Review
Recommendation
I am not sure if it is enough to just set it the following way:
if (records[beneficiary].total != totalAmount) {
// re-initialize if the total has been updated
`--` _initializeDistributionRecord(beneficiary, totalAmount);
`++` _initializeDistributionRecord(beneficiary, records[beneficiary].total);
}
Think of different scenarios if it is done that way and also keep in mind that the same holds for the decrease of records[beneficiary].total by adjust
Discussion
MAKEOUTHILL6
Escalate
I think this issue is wrongly excluded as judge thinks it's a type of an admin error. However I believe it should be addressed, because the functionality of this adjust function is broken for PriceTierVestingSale_2_0.sol regardless of any admin actions taken. The function just doesn't work right and as intended.
sherlock-admin2
Escalate I think this issue is wrongly excluded as judge thinks it's a type of an admin error. However I believe it should be addressed, because the functionality of this
adjustfunction is broken forPriceTierVestingSale_2_0.solregardless of any admin actions taken. The function just doesn't work right and as intended.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
Shogoki
I do not think the adjust function is intended to be called for the PriceTierVestingSale Distributor, as it does not really make a lot of sense imho. That is why i think it is an admin error if it is called for this kind of Distributor.
cr-walker
@Shogoki - Alas, I think this is a valid issue because we might want to adjust distributors that refer to sales.
Real world use cases:
- A user participated in a sale but shouldn't have: their distribution record needs to be adjusted downward
- A user participated in a sale and complains a lot about the project: adjust their distribution record upward
hrishibhat
Result: Medium Has duplicates Considering this a valid medium based on the issue and Sponsor comments
sherlock-admin2
Escalations have been resolved successfully!
Escalation status:
- MAKEOUTHILL6: accepted
cr-walker
Fixed by https://github.com/SoftDAO/contracts/pull/16
maarcweiss
Fixed by accounting for the adjustment of the user. This has been done by getting the records[beneficiary].total instead of the purchasedAmount which was used before.
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 passes the purchased amount to _executeClaim, which re-initializes the distribution record with that amount if it differs from the stored total, overwriting any adjustment made by the owner.
Impact
The user loses the adjusted tokens, resulting in a loss of funds for the user and the protocol.
What the attacker needed
Owner calls adjust on a user's record; user then claims tokens
The fix
The fix was to use the stored total from records[beneficiary].total instead of the purchased amount when re-initializing the distribution record.
How it was found
- User buys tokens and initializeDistributionRecord sets their total to the purchased amount.
- Owner calls adjust to change the user's total claimable amount.
- User calls claim, which passes the purchased amount to _executeClaim.
- _executeClaim sees the stored total differs from the purchased amount and re-initializes the record with the purchased amount, discarding the adjustment.
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 lets an owner change how many tokens a user can claim. But when the user claims, the contract mistakenly resets the total to the original purchase amount, so the owner's change is lost. This means the user gets fewer tokens than they should, or a reduction is ignored.
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.