Skip to content
Misconfiguration Audit finding Advanced

Future upgrades may be difficult or impossible

The protocol stores data in nested structs within a Diamond proxy storage scheme, which could make future upgrades difficult or impossible without corrupting existing data. An attacker cannot directly exploit this, but future upgrades could lead to data inconsistency and improper system operation.

PNS Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/194

The protocol has acknowledged this issue.

Found by

PNS

Summary

The project uses nested structures to store data, which may complicate or make future upgrades impossible. In extreme cases, upgrades could lead to data inconsistency and improper system operation.

Vulnerability Detail

The project uses a structured storage scheme, allowing data in the form of structures to be appropriately linked with facets, theoretically enabling easy updates. However, a problem may arise in future updates because some of these structures contain nested structures that cannot be expanded without "corrupting" the data stored after them in the parent structure.

Solidity
File: contracts/storage/LpPool.sol:20
    struct Props {
       [...]
        TokenBalance baseTokenBalance; //audit struct
        EnumerableSet.AddressSet stableTokens;
        mapping(address => TokenBalance) stableTokenBalances;
        mapping(address => FeeRewards) tradingFeeRewards;
        BorrowingFee borrowingFee; //audit struct
        uint256 apr;
        uint256 totalClaimedRewards;
    }

Solidity
File: contracts/storage/Position.sol:12
   12:     struct Props {
   13:         bytes32 key;
  [...]
   27:         PositionFee positionFee; //audit struct
   28:         int256 realizedPnl;
   29:         uint256 lastUpdateTime;
   30:     }

This is a problem analogous to storage gaps in upgradable contracts, but in a more advanced and complicated form, which is why it should be rated as medium.

Impact

Using nested structures for data storage complicates future upgrades. In extreme cases, this can lead to data inconsistency and improper system operation, which is particularly dangerous in financial systems.

Code Snippet

Tool used

Manual Review

Recommendation

To enable safe extension of inner structures in future upgrades, avoid directly nesting structures. Instead, use mappings, which allow extending structures without the risk of overwriting existing state variables.

Example Solution

Instead of directly nesting structures, place them in mappings:

Solidity
mapping(uint256 => TokenBalance) tokenBalances;
mapping(uint256 => BorrowingFee) borrowingFees;

Access them using constants:

Solidity
uint256 constant BASE_TOKEN_BALANCE = 0;
uint256 constant BORROWING_FEE = 1;

// Accessing the values
TokenBalance storage baseTokenBalance = tokenBalances[BASE_TOKEN_BALANCE];
BorrowingFee storage borrowingFee = borrowingFees[BORROWING_FEE];

In this way, if there is a need to extend the inner structure in future upgrades, it can be done without the risk of overwriting existing state variables.

Reference

Do not put structs directly in structs unless you don’t plan on ever adding more state variables to the inner structs. You won't be able to add new state variables to inner structs in upgrades without overwriting existing state variables.

Source

Discussion

nevillehuang

Invalid, speculation on future upgrades

pronobis4

Escalate

The point here is that diamond acts as a proxy for facets and these structures will be stored in it. If we update the facet that uses this library with a changed structure, we will overwrite the storage. This is why you should avoid nested structures, and that's also why I compare it to storage gaps.

https://eip2535diamonds.substack.com/p/diamond-upgrades

PS. Escalation reported after a discussion on discord

sherlock-admin3

Escalate

The point here is that diamond acts as a proxy for facets and these structures will be stored in it. If we update the facet that uses this library with a changed structure, we will overwrite the storage. This is why you should avoid nested structures, and that's also why I compare it to storage gaps.

https://eip2535diamonds.substack.com/p/diamond-upgrades

PS. Escalation reported after a discussion on discord

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

nevillehuang

@WangSecurity I believe issue #173 and this issue is valid, but would come down to whether future upgrades are in scope of this contest.

WangSecurity

I assume this one the same as #173 doesn't pose risk to the current version of the protocol but will cause issues in the future during updates, correct?

nevillehuang

@WangSecurity I believe that is correct, for both issues, it will not affect the current codebase.

pronobis4

Yes, that is correct.

WangSecurity

I agree that this is a valid issue related to upgradeability. The contract uses Diamond proxies which I believe is quite complex, hence, I think it's fair to validate this issue. To understand my decision, I think it's quite similar to the exception of the storage gaps rule:

Exception: However, if the protocol design has a highly complex and branched set of contract inheritance with storage gaps inconsistently applied throughout and the submission clearly describes the necessity of storage gaps it can be considered a valid medium

Of course, this issue is not connected to storage gaps in any way, but it's a complex structure and will cause issues after upgrades.

Planning to accept the escalation and validate the issue with medium severity.

WangSecurity

Result: Medium
Has duplicates

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

pronobis4

@WangSecurity @nevillehuang I think there's something wrong with escalations

The original report as published on Sherlock, by PNS. 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

Likely the use of nested structs in Diamond proxy storage, which prevents safe extension of inner structs during future upgrades without overwriting subsequent state variables.

Impact

Future upgrades could lead to data inconsistency and improper system operation, which is particularly dangerous in financial systems; the current codebase is not affected.

What the attacker needed

Future upgrade of the protocol

The fix

The report recommends avoiding direct nesting of structs and instead using mappings with constant keys to allow safe extension of inner structures in future upgrades.

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.

Smart contracts on blockchains are hard to change once deployed, so developers use upgrade patterns to replace parts of the code later. This report says the way data is arranged in storage could cause problems when upgrading, because adding new fields to a nested structure might overwrite other data. It is a design concern rather than an attack that someone can carry out right now.

What is Misconfiguration?

Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.

More by PNS

View all

1 more disclosed report, best first.

More from Elfi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Security misconfiguration

The lesson behind this report.

Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.