MiloTruck Profile picture
Jun 15 12 tweets 5 min read Twitter logo Read on Twitter
Wanted to share a vulnerability I've seen recently in multiple audit contests.

This could be your next high/medium severity finding on @code4rena or @sherlockdefi!

The vulnerability - using the "delete" keyword on @OpenZeppelin's EnumerableSet or EnumerableMap: Image
According to the following warning, an EnumerableSet cannot be cleared by simply using the "delete" keyword. Instead, this would corrupt its data:

github.com/OpenZeppelin/o… Image
Why is this so?

Under the hood, the EnumerableSet data structure contains an `_indexes` mapping which tracks the positions of values in the `_values` array:

github.com/OpenZeppelin/o… Image
Likewise, EnumerableMap contains a `_values` mapping that is used to access values by their keys:

github.com/OpenZeppelin/o… Image
Since Solidity does not keep track of the keys in a mapping, mappings cannot be cleared using the `delete` keyword.

Therefore, when `delete` is used on an EnumerableSet or EnumerableMap, their underlying mappings will remain uncleared.
What impact does this have?

For EnumerableSet:
• `contains()` returns true for previously stored values
• `add()` behaves as if the set still contains values
• `remove()` reverts as the set's length is now 0

I've created a simple Foundry test to demonstrate this: Image
For EnumerableMap:
• `contains()` returns true for previous keys
• `get()` can still be used to retrieve previous values
• `set()` doesn't increase the map's length for previous keys
• `at()` only returns keys added after the deletion occurred Image
Well, how are we supposed to delete them?

The proper way to clear an EnumerableSet would be to iterate over all its values and delete them individually: Image
This approach also applies to EnumerableMap: Image
How can we learn from such findings?

For auditors - always check the underlying implementation of libraries used.

In the worst-case scenario, you end up with a better understanding of how the library works, which might come in handy for future audits.
For developers, always be aware of the limitations of the libraries used in your contracts (ie. what can/cannot be done).

Additionally, read through the code of the libraries you use. Developers will often include comments that warn against potentially dangerous operations.
And that's all from me!

If you would like to run the tests shown above, they can be found here:

gist.github.com/MiloTruck/887e…

Also, here's an example of this finding on @sherlockdefi:

github.com/sherlock-audit…

Hope this helps you find a high/medium finding in the future!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with MiloTruck

MiloTruck Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(