`unbond_public` logic causes issues for some delegators preventing partial withdrawals
A logic error in the `unbond_public` function causes a delegator who shares a withdrawal address with their validator to always be treated as being force-unbonded, so any partial withdrawal request triggers a full unbonding of both the delegator and the validator. This can accidentally force their credits into the unbonding period even when the delegator does not intend to fully unbond.
Source: https://github.com/sherlock-audit/2024-05-aleo-judging/issues/31
Found by
dtheo
Summary
Delegators that share the same withdrawal address as a validator will be unable to make partial withdrawal due to a logic error in unbond_public. In some cases this can accidentally trigger the complete unbonding of the valdiator even when the delegator does not intend this.
Vulnerability Detail
If a validator ever attempts to delegate to itself or for any reason a delegator shares a withdrawal address with the validator it is bonded to then the logic in unbond_public will prevent it from making partial withdrawals.
Imagine the case:
- A validator AAA is validating with 100 credits and a withdrawal address of BBB
- A delegator CCC is bonded to validator AAA with 10M credits and also uses withdrawal address BBB
- Delegator CCC attempts to unbond ANY amount of credits from the validator using
unbond_public, lets say 1 microcredit.eg. delegator callsunbond_public(stakers_addr = CCC, microcredits = 1)
The following logic to determine if the delegators bond should be entirely removed will always incorrectly report that it should:
// Check if the delegator will fall below 10,000 bonded credits.
lt r16 10_000_000_000u64 into r17;
// If the validator is forcing the delegator to unbond OR the delegator will fall below 10,000 bonded credits.
or r11 r17 into r18;
// Determine the amount to unbond: requested amount if >= 10,000 credits, otherwise the full bonded amount.
ternary r18 r5.microcredits r2 into r19;
This is due to the fact that the logic to determine if the delegator is being force unbonded by the validator will always incorrectly report that it is. This will cause both the deleagator and the validator to completely unbond and force their credits into the unbonding period (360 blocks). This will happen even if the delegator calls unbond_public with 0 microcredits.
Impact
This can incorrectly force unbonding for delegators and validators.
Code Snippet
snarkVM/synthesizer/program/src/resources/credits.aleo#L487-L494)
I have also created an annotated pseudo code version of unbond_public to make it easier to read.
Tool used
Manual Review
Recommendation
Add a a conditional to check r7 ( this is set to true if caller == stakers withdrawal address). If this is set and a smaller amount of bond reduction is supplied than the total (as the function argument microcredits then consider this a delegator withdrawal and handle the arithmetic correctly. This will allow validators to still force unbond and will prevent delegators from accidentally unbonding everything without meaning to.
Discussion
evanmarshall
This is a real issue. I would categorize it as minor. Why it's minor is due to the factor that a single user (with both a validator and a delegator must set it up this way) and this single user has the ability to migrate to use separate withdrawal addresses.
sherlock-admin3
Escalate
The delegator setting the same withdraw address as the validator is essentially a self-rekt and therefore when this same withdraw address unbonds the delegator it is logically expected that the "validator unbond delegator" logic will apply here.
You've deleted an escalation for this issue.
evanmarshall
I'm not clear on the meaning of escalation here. Can you explain?
I think both interpretations are fair but I tend to side with the OP that you should be able to partial unbond. It is also a self rekt with very little consequence ie no funds lost or stolen. Only 360 blocks + one more transaction solve the issue.
infosecual
Only 360 blocks + one more transaction solve the issue.
This downplays the possible impact a bit much in my opinion. This could force-remove a validator and its delegator without the operator meaning to. You can clean up the mess by changing your withdrawal address but there is no functionality to "just change withdrawal address". Depending how the delegator and validator are set up they might have to withdrawal the validator and all of its delegators, wait the unbonding period, then re-enroll them with different withdrawal addresses. They could not re-bond the new validator until all of the previous delegators that helped it get to the 10M credit minimum have done this.
infosecual
I'm not clear on the meaning of escalation here. Can you explain?
Edit: Haxatron is escalating issues that he believes have been judged incorrectly. If he is right and can prove it he stands to be rewarded. https://docs.sherlock.xyz/audits/judging/escalation-period
Haxatron
Haxatron is attempting to escalate to get valid issues marked as invalid. If he is successful and he can get some of his invalids changed to valid he stands to make more of the pot.
Firstly, this comment is uncalled for and I apologise if you feel this way. But I believe I have been quite fair in my escalations thus far. If you believe I have made any incorrect points you are free to dispute it.
Secondly, I still do not think this is a real issue because the delegator must set the same withdrawal address as the validator, for which there is no legitimate reason to do so. And then, when this validator withdrawal address which is the same as the delegator withdrawal address unbonds the delegator it is expected that the "validator unbonds delegator" logic applies.
morbsel
I agree with Haxatron, if the withdrawal is set to the same withdrawal address as the validator it means only the validator withdrawal address can unbond the delegator's bond and when the validator unbonds a delegator's bond the whole amount should get unbonded, stated at: https://github.com/sherlock-audit/2024-05-aleo/blob/55b2e4a02f27602a54c11f964f6f610fee6f4ab8/snarkVM/synthesizer/program/src/resources/credits.aleo#L415-L417 So partial unbond shouldn't even be a point of discussion.
infosecual
Firstly, this comment is uncalled for and I apologise if you feel this way.
I apologize. I did not mean this in a condescending way or anything. I see how that came across now and I did not mean for it to sound this way. Please excuse me. This is better wording and is more in line with what I mean to say:
Haxatron is escalating issues that he believes have been judged incorrectly. If he is right and can prove it he stands to be rewarded.
evanmarshall
I still believe this issue as a whole remains an issue. In theory, a delegator should be able to partially unbond. It will be common for validator operators to handle large credit amounts by delegating to themselves. Validator keys are necessarily hot (why only the 100 credit self bond is required). Delegator keys and withdrawals addresses can be cold and use offline signing. It's possible that a validator tries to use the same withdrawal address for both validator and delegator. Through normal operation, a validator operator will want to withdraw rewards to fund operations. Partial unbonds would be expected in this case.
Haxatron
Since the sponsor has stated that there is a legitimate use case for a validator and a delegator to have the same withdrawal address, I do agree that this is a valid issue and will delete the escalation.
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
Likely the condition that checks whether the validator is force-unbonding the delegator does not distinguish between a validator-initiated unbond and a delegator-initiated partial unbond when both share the same withdrawal address, so it always evaluates to true.
Impact
This can incorrectly force unbonding for delegators and validators, locking their credits for 360 blocks, though no funds are lost or stolen and the situation can be resolved by changing withdrawal addresses after the unbonding period.
What the attacker needed
Delegator shares withdrawal address with validator; delegator initiates a partial unbond
The fix
The function should be fixed by adding a check for whether the caller is the staker's withdrawal address and, if so, treating a partial withdrawal as a delegator withdrawal and handling the arithmetic correctly, while still allowing validators to force unbond.
How it was found
- A validator with a withdrawal address bonds 100 credits.
- A delegator bonds a large amount to that validator and uses the same withdrawal address.
- The delegator calls `unbond_public` to withdraw a tiny amount.
- The function incorrectly treats this as a forced unbond and removes the entire bond for both delegator and validator.
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.
In this blockchain system, a delegator can ask to take back only part of their staked credits. But if the delegator uses the same withdrawal address as the validator they are staked with, the code mistakenly thinks the validator is forcing them out and removes all their stake instead. This means a small withdrawal request can unexpectedly lock up all their credits for a waiting period.
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.