If another module adds a module, the safe will be bricked
A module added by another module bypasses the function that increments the enabled module count, causing the guard's before and after module hash checks to differ. This permanently blocks all transactions, bricking the safe and trapping funds.
minhtrng Follow with obront and roguereddwarf
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-hats-judging/issues/42
Found by
minhtrng, obront, roguereddwarf
Summary
If a module is added by another module, it will bypass the enableNewModule() function that increments enabledModuleCount. This will throw off the module validation in checkTransaction() and checkAfterExecution() and could cause the safe to become permanently bricked.
Vulnerability Detail
In order to ensure that signers cannot add new modules to the safe (thus giving them unlimited future governing power), the guard portion of the gate checks that the hash of the modules before the transaction is the same as the hash after.
Before:
(address[] memory modules,) = safe.getModulesPaginated(SENTINEL_OWNERS, enabledModuleCount);
_existingModulesHash = keccak256(abi.encode(modules));
After:
(address[] memory modules,) = safe.getModulesPaginated(SENTINEL_OWNERS, enabledModuleCount + 1);
if (keccak256(abi.encode(modules)) != _existingModulesHash) {
revert SignersCannotChangeModules();
}
You'll note that the "before" check uses enabledModuleCount and the "after" check uses enabledModuleCount + 1. The reason for this is that we want to be able to catch whether the user added a new module, which requires us taking a larger pagination to make sure we can view the additional module.
However, if we were to start with a number of modules larger than enabledModuleCount, the result would be that the "before" check would clip off the final modules, and the "after" check would include them, thus leading to different hashes.
This situation can only arise if a module is added that bypasses the enableModule() function. But this exact situation can happen if one of the other modules on the safe adds a module to the safe.
In this case, the modules on the safe will increase but enabledModuleCount will not. This will lead to the "before" and "after" checks returning different arrays each time, and therefore disallowing transactions.
The only possible ways to fix this problem will be to have the other module remove the additional one they added. But, depending on the specific circumstances, this option may not be possible. For example, the module that performed the adding may not have the ability to remove modules.
Impact
The safe can be permanently bricked, with the guard functions disallowing any transactions. All funds in the safe will remain permanently stuck.
Code Snippet
Tool used
Manual Review
Recommendation
The module guarding logic needs to be rethought. Given the large number of unbounded risks it opens up, I would recommend not allowing other modules on any safes that use this functionality.
Discussion
spengrah
This is definitely an issue. I can see a few potential approaches to addressing it:
- As suggested, allow no other modules to exist alongside HSG. That would require checks in the factory as well as in
checkAfterExecution.
This approach would eliminate this issue fully, but would reduce flexibility for DAOs to customize how they use their Safes. For example, a common expected pattern is to use a Safe alongside a DAO that both the DAO (via a module; slow) and signers (via sigs; fast) can control. This wouldn't be possible if no additional modules could be added.
-
Add an onlyOwner
removeModulesfunction to enable the owner to true up the module count saved in storage if a module is added to the Safe via a different avenue. -
In
checkTransaction, count and store the number of modules that exist on the safe, then use that value as the page size for the check incheckAfterExecution.
This would allow the Safe to have arbitrary number of modules, but for larger number of modules the gas size be fairly high — in checkTransaction we'd need to get the module addresses from the Safe in a while loop to ensure we count all of them.
cc @zobront
spengrah
Going with option 1 here
spengrah
https://github.com/Hats-Protocol/hats-zodiac/pull/10
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 guard relies on a stored module count that can become out of sync when a module adds another module directly, so the paginated module lists compared before and after differ.
Impact
The safe can be permanently bricked, with the guard functions disallowing any transactions and all funds stuck.
What the attacker needed
A safe using the guard; another module adds a module
The fix
The module guarding logic should be rethought, and the team chose to disallow other modules on safes that use this functionality.
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 safe is a shared wallet controlled by rules. A guard checks that the list of extra helper programs (modules) hasn't changed during a transaction. If another helper program adds a new helper without updating a counter, the guard sees a different list before and after, so it rejects every future transaction and the money is stuck.
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.