Changing hat toggle address can lead to unexpected changes in status
Changing a hat's toggle address from a contract to an EOA can cause the hat's status to revert to an unsynced local state, potentially reactivating a hat that should be off. An attacker could sandwich a transaction to exploit this, using a hat that should be disabled.
Source: https://github.com/sherlock-audit/2023-02-hats-judging/issues/34
Found by
obront
Summary
Changing the toggle address should not change the current status unless intended to. However, in the event that a contract's toggle status hasn't been synced to local state, this change can accidentally toggle the hat back on when it isn't intended.
Vulnerability Detail
When an admin for a hat calls changeHatToggle(), the toggle address is updated to a new address they entered:
function changeHatToggle(uint256 _hatId, address _newToggle) external {
if (_newToggle == address(0)) revert ZeroAddress();
_checkAdmin(_hatId);
Hat storage hat = _hats[_hatId];
if (!_isMutable(hat)) {
revert Immutable();
}
hat.toggle = _newToggle;
emit HatToggleChanged(_hatId, _newToggle);
}
Toggle addresses can be either EOAs (who must call setHatStatus() to change the local config) or contracts (who must implement the getHatStatus() function and return the value).
The challenge comes if a hat has a toggle address that is a contract. The contract changes its toggle value to false but is never checked (which would push the update to the local state). The admin thus expects that the hat is turned off.
Then, the toggle is changed to an EOA. One would expect that, until a change is made, the hat would remain in the same state, but in this case, the hat defaults back to its local storage state, which has not yet been updated and is therefore set to true.
Even in the event that the admin knows this and tries to immediately toggle the status back to false, it is possible for a malicious user to sandwich their transaction between the change to the EOA and the transaction to toggle the hat off, making use of a hat that should be off. This could have dramatic consequences when hats are used for purposes such as multisig signing.
Impact
Hats may unexpectedly be toggled from off to on during toggle address transfer, reactivating hats that are intended to be turned off.
Code Snippet
Tool used
Manual Review
Recommendation
The changeHatToggle() function needs to call checkHatToggle() before changing over to the new toggle address, to ensure that the latest status is synced up.
Discussion
spengrah
The risk here is limited since toggle modules are treated as the source of truth for hat status and so admins should be aware that changing the toggle also changes the source of truth. But the recommended fix is simple and cheap enough that it makes sense to apply.
@zobront, does this same concept apply to eligibility?
zobront
@spengrah Agreed. The only situation I think would break a user's assumption is a change from contract => EOA, because in most cases the toggle will be saved in hat.config (so they won't need to take any action), but in this edge case it will be reset to an old version. That inconsistent behavior is the root of the problem.
The same is true with eligibility, but not sure if there's anything you can do about it, because it's on an individual hat by hat basis. If the eligibility contract is changed, you can't go through all the hats and update them. So my advice is just to be sure to document it very clearly in this case.
spengrah
https://github.com/Hats-Protocol/hats-protocol/pull/116
cducrest
Escalate for 10 USDC
Disagree with being sever enough for being a medium. The Sherlock doc state "The vulnerability must be something that is not considered an acceptable risk by a reasonable protocol team." However, as was explained, this problem is present both for status and eligibility, and a fix is possible and implemented in https://github.com/Hats-Protocol/hats-protocol/pull/116 only for status, proving that the risk is acceptable for the eligibility part thus acceptable for the status part.
As noted in the comment by spengrah, the fix was implemented mostly because it is not expensive to do so, even though the risk is limited.
sherlock-admin
Escalate for 10 USDC
Disagree with being sever enough for being a medium. The Sherlock doc state "The vulnerability must be something that is not considered an acceptable risk by a reasonable protocol team." However, as was explained, this problem is present both for
statusandeligibility, and a fix is possible and implemented in https://github.com/Hats-Protocol/hats-protocol/pull/116 only forstatus, proving that the risk is acceptable for theeligibilitypart thus acceptable for thestatuspart.As noted in the comment by spengrah, the fix was implemented mostly because it is not expensive to do so, even though the risk is limited.
You've created a valid escalation for 10 USDC!
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.
hrishibhat
Escalation rejected
The issue is a valid medium Given the edge case that the attack is still possible and can cause a hat to be activated which should not be and vice versa.
sherlock-admin
Escalation rejected
The issue is a valid medium Given the edge case that the attack is still possible and can cause a hat to be activated which should not be and vice versa.
This issue's escalations have been rejected!
Watsons who escalated this issue will have their escalation amount deducted from their next payout.
MLON33
zobront added "Fix Approved" label
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 changeHatToggle function does not sync the current toggle status from the old toggle address before switching to the new one, so the local state may be stale.
What the attacker needed
Hat with a contract toggle address; admin changes toggle to an EOA; attacker can sandwich transactions
How it was found
- A hat's toggle address is a contract that sets the toggle value to false, but the status is not synced to local state.
- The admin changes the toggle address to an EOA.
- The hat's status reverts to the unsynced local state, which is true.
- A malicious user can sandwich a transaction to toggle the hat off, using the hat while it should be off.
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 hat is like a permission badge in a smart contract system. The badge's on/off status can be controlled by an external contract or by a person's wallet. If the controller contract sets the status to off but the system doesn't record it, and then the controller is switched to a wallet, the badge may show as on again. This could let someone use a badge that was supposed to be turned off.
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.