Skip to content
Business logic Audit finding Intermediate

Can get around hats per level constraints using phantom levels

The Hats protocol limits each hat to 65,536 direct children, but an admin could bypass this by setting a non-existent intermediate hat ID as the admin, creating phantom levels. This allowed unlimited direct children, violating the intended constraint.

0xMojito Follow with obront

Originally disclosed on Sherlock

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

Found by

0xMojito, obront

Summary

Each hat has the ability to mint 2^16 = 65,536 direct children. The id of the next child to be minted is saved in _hats[_admin].lastHatId and is incremented with each new child created. Once it hits 65,536, no more children can be created, as specified in the comments:

// use the overflow check to constrain to correct number of hats per level

However, this check can be bypassed by using phantom levels. The basic idea is that there is nothing stopping an admin from creating a new hat and setting the admin to a non-existent path, using intermediate hatIds of 0 to accomplish this.

Vulnerability Detail

Here's an example to make it more concrete:

  • I am a topHat with domain 0x00000001
  • I have created all 65,536 possible children (0x1.0001 to 0x1.ffff)
  • But nothing is stopping me from setting 0x1.0000.0001 as the admin and continuing to create hats
  • This hatId cannot possible exist (because no hatId immediately following a 0000 can actually be minted)
  • Therefore, it is functionally the same as having 0x1 as the only admin
  • This user can then continue to create children from 0x1.0000.0001.0001 to 0x1.0000.0001.ffff
  • This can be repeated at each level, creating the ability to have unlimited direct children

Impact

The constraints around the number of direct descendants that an individual hat can have can be easily violated.

Code Snippet

https://github.com/Hats-Protocol/hats-protocol/blob/fafcfdf046c0369c1f9e077eacd94a328f9d7af0/src/Hats.sol#L143-L170

Tool used

Manual Review

Recommendation

Add logic in isAdminOfHat() to ensure that each hat along the path being checked has a maxSupply > 0 and is therefore a real hat.

Discussion

spengrah

The child count constraint only exists because we have to be able to represent the hat ids correctly within the available 256 bits.

Assuming we appropriately mitigate the over-writing risk described by #11 and #72, phantom levels is actually kind of a nice feature to allow a given hat to have more than 2**16-1 immediate children. After all, they can achieve something close to this anyways by creating grandchild hats with a non-existent intermediate admin of 0001.

spengrah

Based on the impact of this issue described in #64, I'm going to remove the dispute of this one. We should ensure that all hat ids are valid hat ids (ie have no empty levels) to facilitate interactions and interpretation by other protocols, indexers, and apps.

TODO devise an efficient method for ensuring that the admin parameter of createHat, requestLinkTopHatToTree, and relinkTopHatWithinTree do not have empty levels.

spengrah

@zobront, my initial idea is to create a new pure function that traverses up the admin's id and returns false if it finds an empty level. We'd call that function from within the above 3 functions and revert if false. Should be relatively cheap since its just bitwise ops.

Solidity
function isValidHatId(uint256 _hatId) public pure returns (bool validHatId) {
  // valid top hats are valid hats
  if (isLocalTopHat(_hatId)) return true;
  uint32 level = getLocalHatLevel(_hatId);
  uint256 admin;
  // for each subsequent level up the tree, check if the level is 0 and return false if so
  for (level; level > 0; ++level) {
    // find the admin at this level; we don't need to check _hatId itself since the getAdmin call already ignores trailing empty levels
    admin = getAdminAtLocalLevel(_hatId, level);
    // truncate to the 16 bits relevant to the direct admin and return false if 0
    if (uint16(admin) == 0) return false;
    }
  // if there are no empty levels, return true
  return true;
}

**spengrah**

I do think its worth considering whether we actually *do* want to support empty levels. Quick pros/cons list:

| Pros | Cons |
| --- | --- |
|  More flexibility for admins to create > 2**32 direct child hats  |  Doesn't work for admins at level 1 |
|  Increases the maximum number of hats per tree  |  Potential for confusion by users, protocols, indexers, and apps; would need to document very clearly  |
|  Not requiring the isValidHatId in `createHat` check saves a small amount of gas  |  More complexity, in general  |

Interested in what @nintynick @davehrlichman think here.

**spengrah**

Decision: in the spirit of keeping the protocol as simple as possible, we're going to close this loophole ~~/feature~~ with the above `isValidHatId` check. 

cc @zobront 

**zobront**

@spengrah Instead of adding an additional check, couldn't this be a part of the the `_adminCheck()`, since you're already traversing the tree to check `if (isWearerOfHat(_user, getAdminAtLocalLevel(_hatId, adminLocalHatLevel))) { ...`.

There may be a situation where this causes a problem. I can dig in deeper, but wanted to float the idea.

On the whole, agreed with the solution.

**spengrah**

@zobront my thinking is that I'd prefer adding cost to `createHat` over `isAdminOfHat` since the former is called just once while the latter is called many times over the life of the hat.

**zobront**

@spengrah Very good point, you're right.

**spengrah**

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

**MLON33**

zobront added "Fix Approved" label

The original report as published on Sherlock, by 0xMojito. 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 validate that all levels in a hat ID correspond to real hats, allowing non-existent intermediate levels to be used as admins.

Impact

The constraint on the number of direct descendants per hat could be violated, allowing unlimited direct children.

What the attacker needed

Be an admin of a hat; have created all direct children

The fix

The protocol was fixed by adding a check to ensure that all hat IDs used as admins have no empty levels.

How it was found

  1. Create a top hat with domain 0x00000001.
  2. Create all 65,536 possible direct children (0x1.0001 to 0x1.ffff).
  3. Set a non-existent hat ID like 0x1.0000.0001 as the admin for new hats.
  4. Continue creating children under that phantom admin, repeating at each level.

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, each hat can only have a limited number of direct child hats. The limit is enforced by counting how many children have been created. The bug is that you could pretend a non-existent hat is the parent, which tricks the system into allowing more children than intended.

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 0xMojito

View all

1 more disclosed report, 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.