Skip to content
Business logic Audit finding Intermediate

Swap Signer fails if final owner is invalid due to off by one error in loop

The signer-swap loop in the HatsSignerGate contract stops one owner short, so the last owner is never checked for validity. When only that final owner is invalid, a new user cannot claim the signer role even though a slot should be freed for them.

james_wu Follow with obront

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-hats-judging/issues/38

Found by

james_wu, obront

Summary

New users attempting to call claimSigner() when there is already a full slate of owners are supposed to kick any invalid owners off the safe in order to swap in and take their place. However, the loop that checks this has an off-by-one error that misses checking the final owner.

Vulnerability Detail

When claimSigner() is called, it adds the msg.sender as a signer, as long as there aren't already too many owners on the safe.

However, in the case that there are already the maximum number of owners on the safe, it performs a check whether any of them are invalid. If they are, it swaps out the invalid owner for the new owner.

Solidity
if (ownerCount >= maxSigs) {
    bool swapped = _swapSigner(owners, ownerCount, maxSigs, currentSignerCount, msg.sender);
    if (!swapped) {
        // if there are no invalid owners, we can't add a new signer, so we revert
        revert NoInvalidSignersToReplace();
    }
}
Solidity
function _swapSigner(
    address[] memory _owners,
    uint256 _ownerCount,
    uint256 _maxSigners,
    uint256 _currentSignerCount,
    address _signer
) internal returns (bool success) {
    address ownerToCheck;
    bytes memory data;

    for (uint256 i; i < _ownerCount - 1;) {
        ownerToCheck = _owners[i];

        if (!isValidSigner(ownerToCheck)) {
            // prep the swap
            data = abi.encodeWithSignature(
                "swapOwner(address,address,address)",
                _findPrevOwner(_owners, ownerToCheck), // prevOwner
                ownerToCheck, // oldOwner
                _signer // newOwner
            );

            // execute the swap, reverting if it fails for some reason
            success = safe.execTransactionFromModule(
                address(safe), // to
                0, // value
                data, // data
                Enum.Operation.Call // operation
            );

            if (!success) {
                revert FailedExecRemoveSigner();
            }

            if (_currentSignerCount < _maxSigners) ++signerCount;
            break;
        }
        unchecked {
            ++i;
        }
    }
}

This function is intended to iterate through all the owners, check if any is no longer valid, and — if that's the case — swap it for the new one.

However, in the case that all owners are valid except for the final one, it will miss the swap and reject the new owner.

This is because there is an off by one error in the loop, where it iterates through for (uint256 i; i < _ownerCount - 1;)...

This only iterates through all the owners up until the final one, and will miss the check for the validity and possible swap of the final owner.

Impact

When only the final owner is invalid, new users will not be able to claim their role as signer, even through they should.

Code Snippet

https://github.com/Hats-Protocol/hats-zodiac/blob/9455cc0957762f5dbbd8e62063d970199109b977/src/HatsSignerGate.sol#L57-L69

https://github.com/Hats-Protocol/hats-zodiac/blob/9455cc0957762f5dbbd8e62063d970199109b977/src/HatsSignerGateBase.sol#L308-L350

Tool used

Manual Review

Recommendation

Perform the loop with ownerCount instead of ownerCount - 1 to check all owners:

Diff
- for (uint256 i; i < _ownerCount - 1;) {
+ for (uint256 i; i < _ownerCount ;) {
     ownerToCheck = _owners[i];
    ...
}

Discussion

spengrah

https://github.com/Hats-Protocol/hats-zodiac/pull/12

MLON33

zobront added "Fix Approved" label

The original report as published on Sherlock, by james_wu. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 loop condition uses `_ownerCount - 1` as the upper bound, so the final owner index is never examined.

Impact

When only the final owner is invalid, new users cannot claim their signer role even though they should be able to.

What the attacker needed

Safe already at maximum owner count; only the final owner is invalid

The fix

The loop should iterate up to `ownerCount` rather than `ownerCount - 1` so every owner is checked; the report notes a fix was approved.

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 keeps a list of trusted signers, like a list of people allowed to approve things. When the list is full, a newcomer can only join if one existing member is no longer valid and gets replaced. The code that looks for an invalid member stops one item too early, so it never looks at the last person on the list.

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.

More from Hats

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.