attacker can perform malicious transactions in the safe because reentrancy is not implemented correctly in the checkTransaction() and checkAfterExecution() function in HSG
The reentrancy guard in the HatsSignerGate contract increments a counter in checkTransaction and decrements it in checkAfterExecution, but the logic is flawed. An attacker could exploit this to change the Safe's guard or threshold during execTransaction and execute a malicious transaction.
cducrest-brainbot Follow with unforgiven
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-hats-judging/issues/124
Found by
cducrest-brainbot, unforgiven
Summary
to prevent reentrancy during the safe's execTransaction() function call code use _guardEntries and increase it in the checkTransaction() and decrease it in the checkAfterExecution(). but the logic is wrong and code won't underflow in the checkAfterExecution() if attacker perform reentrancy during the execTransaction()
Vulnerability Detail
This is some part of the checkTransaction() and checkAfterExecution() code:
function checkTransaction(
address to,
uint256 value,
bytes calldata data,
Enum.Operation operation,
uint256 safeTxGas,
uint256 baseGas,
uint256 gasPrice,
address gasToken,
address payable refundReceiver,
bytes memory signatures,
address // msgSender
) external override {
if (msg.sender != address(safe)) revert NotCalledFromSafe();
uint256 safeOwnerCount = safe.getOwners().length;
// uint256 validSignerCount = _countValidSigners(safe.getOwners());
// ensure that safe threshold is correct
reconcileSignerCount();
if (safeOwnerCount < minThreshold) {
revert BelowMinThreshold(minThreshold, safeOwnerCount);
}
// get the tx hash; view function
bytes32 txHash = safe.getTransactionHash(
// Transaction info
to,
value,
data,
operation,
safeTxGas,
// Payment info
baseGas,
gasPrice,
gasToken,
refundReceiver,
// Signature info
// We subtract 1 since nonce was just incremented in the parent function call
safe.nonce() - 1 // view function
);
uint256 validSigCount = countValidSignatures(txHash, signatures, signatures.length / 65);
unchecked {
++_guardEntries;
}
}
/// @notice Post-flight check to prevent `safe` signers from removing this contract guard, changing any modules, or changing the threshold
/// @dev Modified from https://github.com/gnosis/zodiac-guard-mod/blob/988ebc7b71e352f121a0be5f6ae37e79e47a4541/contracts/ModGuard.sol#L86
function checkAfterExecution(bytes32, bool) external override {
if (msg.sender != address(safe)) revert NotCalledFromSafe();
// leave checked to catch underflows triggered by re-erntry attempts
--_guardEntries;
}
as you can see code increase the value of the _guardEntries in the checkTransaction() which is called before the transaction execution and decrease its value in the checkAfterExecution which is called after transaction execution. this won't protect against reentrancy during the safe's execTransaction() call. attacker can perform this actions:
- Transaction1 which has valid number of signers and set the value of the guard to 0x0. and call
safe.execTransaction(Transaction2). - Transaction2 which reset the value of the guard to the HSG address.
- now by calling
Tsafe.execTransaction(Transaction1)code would first callcheckTransaction()and would see the number of the signers is correct and then increase the value of the_guardEntiriesto 1 and then code in safe would execute the Transaction1 which would set the guard to 0x0 and execute the Transaction2 in safe. - because guard is 0x0 code would execute the Transaction2 and then during that code would re-set the value of the guard to the HSG address.
- now
checkAfterExecution()would get exeucted and would see that guard value is correct and would decrease the_guardEntiries
the attack is possible by changing the value of the threshhold in the safe. because code would perform two increase and one decrease during the reentrancy so the underflow won't happen.
Impact
it's possible to set guard or threshold during the execTransaction() and execute another malicious transaction which resets guard and threshold
Code Snippet
Tool used
Manual Review
Recommendation
set the value of the guard to 1 and decrease in the checkTransaction() and increase in the checkAfterExecution().
Discussion
MLON33
zobront clarified 124 was previously duped to 41, but was separated because it didn't accurately express the High severity exploit.
MLON33
zobront added "Fix Approved" label
jacksanford1
Fix added by spengrah for reference: https://github.com/Hats-Protocol/hats-zodiac/pull/13
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 counter is incremented in checkTransaction and decremented in checkAfterExecution, but during reentrancy the counter can be incremented twice and decremented once, so the underflow check does not trigger.
Impact
It is possible to set the guard or threshold during execTransaction and execute another malicious transaction that resets the guard and threshold.
What the attacker needed
Attacker controls a Safe signer; valid signatures for initial transaction
The fix
The guard counter should be set to 1 and decremented in checkTransaction, then incremented in checkAfterExecution.
How it was found
- Transaction1 sets the guard to 0x0 and calls safe.execTransaction(Transaction2).
- Transaction2 resets the guard to the HSG address.
- Calling safe.execTransaction(Transaction1) triggers checkTransaction, which increments _guardEntries to 1, then executes Transaction1, setting the guard to 0x0 and executing Transaction2.
- Because the guard is 0x0, Transaction2 executes and resets the guard to the HSG address.
- checkAfterExecution runs, sees the guard is correct, and decrements _guardEntries.
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 reentrancy guard is a mechanism to prevent a function from being called again before it finishes. Here, the guard uses a counter that goes up before the transaction and down after, but because of how the Safe calls the guard, an attacker can make the counter go up twice and down only once, so the guard fails to block a second transaction. This lets the attacker sneak in a malicious transaction that changes important settings.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.