Skip to content
Business logic Audit finding Intermediate

Hats can be overwritten

The smart contract allowed creating a child hat under an admin hat that did not yet exist. When the admin hat was later created, it overwrote the child hat's properties, including its maximum supply, even if the child hat was meant to be immutable.

Allarious Follow with carrot

Originally disclosed on Sherlock

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

Found by

Allarious, carrot

Summary

Child hats can be created under a non-existent admin. Creating the admin allows overwriting the properties of the child-hats, which goes against the immutability of hats.

Vulnerability Detail

When creating a hat, the code never checks if the admin passed actually exists or not. Thus it allows the creation of a hat under an admin who hasn't been created yet. Lets say top hat is 1.0.0, and we call admin the hat 1.1.0 and child is hat 1.1.1. The child can be created before admin. When admin (1.1.0) is created after this, it overwrites the lastHatId of the admin, as shown here https://github.com/Hats-Protocol/hats-protocol/blob/fafcfdf046c0369c1f9e077eacd94a328f9d7af0/src/Hats.sol#L421-L439

Solidity
    function _createHat(
        uint256 _id,
        string calldata _details,
        uint32 _maxSupply,
        address _eligibility,
        address _toggle,
        bool _mutable,
        string calldata _imageURI
    ) internal returns (Hat memory hat) {
        hat.details = _details;
        hat.maxSupply = _maxSupply;
        hat.eligibility = _eligibility;
        hat.toggle = _toggle;
        hat.imageURI = _imageURI;
        hat.config = _mutable ? uint96(3 << 94) : uint96(1 << 95);
        _hats[_id] = hat;


        emit HatCreated(_id, _details, _maxSupply, _eligibility, _toggle, _mutable, _imageURI);
    }

Now, the next eligible hat for this admin is 1.1.1, which is a hat that was already created and minted. This can allow the admin to change the properties of the child, even if the child hat was previously immutable. This contradicts the immutability of hats, and can be used to rug users in multiple ways, and is thus classified as high severity. This attack can be carried out by any hat wearer on their child tree, mutating their properties.

Impact

Code Snippet

The attack can be demonstrated with the following code which carries out the following steps:

  1. Child 1.1.1 is created with max supply 10, and false mutability. Thus its properties should be locked.
  2. Admin 1.1.0 is created
  3. Child 1.1.1 is re-created, now with supply of 20, overwriting its previous instance
  4. The children are shown to be on the same hatId, and their max supplies are shown to be different values.
Solidity
function testATTACKoverwrite() public {
        vm.startPrank(address(topHatWearer));
        uint256 emptyAdmin = hats.getNextId(topHatId);
        uint256 child1 = hats.createHat(
            emptyAdmin,
            _details,
            10,
            _eligibility,
            _toggle,
            false,
            secondHatImageURI
        );
        (, uint256 maxsup, , , , , , , ) = hats.viewHat(child1);
        assertEq(maxsup, 10);
        hats.createHat(
            topHatId,
            _details,
            _maxSupply,
            _eligibility,
            _toggle,
            false,
            secondHatImageURI
        );
        uint256 child2 = hats.createHat(
            emptyAdmin,
            _details,
            20,
            _eligibility,
            _toggle,
            false,
            secondHatImageURI
        );
        (,  maxsup, , , , , , , ) = hats.viewHat(child1);
        assertEq(child1, child2);
        assertEq(maxsup, 20);
    }

Tool used

Manual Review

Recommendation

Check if admin exists, before minting by checking any of its properties against default values

Solidity
require(_hats[admin].maxSupply > 0, "Admin not created")

Discussion

spengrah

The ability for an admin to skip levels when creating hats is a desired feature. However, we definitely do not want those hats to be able to overwritten if/when the skipped admins are created. Therefore, what we need to do is not overwrite a hat's lastHatId property when creating it.

For example, add something like the following to _createHat:

Solidity
uint16 lastId = hat.lastHatId;
if (lastId > 0) hat.lastHatId = lastId;

spengrah

https://github.com/Hats-Protocol/hats-protocol/pull/109

MLON33

zobront added "Fix Approved" label.

The original report as published on Sherlock, by Allarious. 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 contract did not check that the admin hat existed before creating a child hat, and when the admin was later created, it overwrote the child's lastHatId, allowing the child's properties to be changed.

Impact

An admin could change the properties of an existing child hat, even if it was meant to be immutable, potentially allowing them to rug users.

What the attacker needed

Any hat wearer on their child tree

The fix

The fix was to prevent overwriting the lastHatId when creating a hat, so that existing child hats are not modified.

How it was found

  1. Create a child hat under a non-existent admin with a maximum supply of 10 and immutability set to false.
  2. Create the admin hat that was previously missing.
  3. Re-create the same child hat, now with a maximum supply of 20.
  4. Observe that the child hat's ID remains the same but its maximum supply has changed to 20.

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 system, hats are like digital badges with rules, such as how many can exist. A child hat is supposed to inherit from an admin hat, but the code let you create a child before its admin existed. Later, creating the admin would accidentally change the child's rules, even if they were supposed to be locked.

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 by Allarious

View all

8 more disclosed reports, best first.

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.