Skip to content
Smart contracts Audit finding Advanced

Missing state validation upon Upgrade

The contract's initialise function after an upgrade does not validate global state invariants such as the fee being below 100%. A rogue admin could set an excessive fee before upgrading, then claim fees that exceed actual rewards, stealing user funds.

k13n Follow

Originally disclosed on Immunefi

Informational View original
  • Disclosed after under an hour

#37854 [SC-Insight] Missing state validation upon Upgrade

Submitted on Dec 17th 2024 at 13:51:59 UTC by @k13n for Audit Comp | Folks: Liquid Staking

Description

Brief/Intro

The contract is updatable meaning that the code of the contract can be changed. Right now an old version of this contract is deployed on Algorand and according to the developers this old contract will be updated to contain the code subject to this audit competition. This new code does not validate the global state of the contract upon update. Therefore, certain invariants cannot be guaranteed after an update. This can lead to unexpected behavior and loss of user funds.

Vulnerability Details

Function initialise must be called after upgrading the contract and before any other meaningful operation (minting, burning, etc.) can be executed. This function assumes that certain global state exists and is valid without further checks. In case this global state was manipulated prior to the upgrade, certain invariants may not hold.

Function update_fee ensures the invariant that the fee is between 0 and 100%. This invariant is asserted in function check_fee which checks that the fee is below 100% (since Algorand only supports unsigned integers, the fee must be >= 0%). This check_fee() (and other similar sanity checks) are missing in the initialise function. Therefore, it is possible that the invariant doesn't hold.

Impact Details

Assume that through the above loophole the fee is set to 10000% by a rogue admin. As a result, the admin can collect more fees than he should possibly get and these additional fees are taken from the users' stake, resulting in the theft of user funds.

Recommendation

Check that the given invariants (check_fee, check_premium, ...) are maintained after updating in initialise.

References

Proof of Concept

Proof of Concept

Initial State

  • The old code (code that is not subject to this audit) is deployed on Algorand

(Rogue) Admin prepares Attack

  • A (rouge) admin manipulates the global state and for example sets the fee to 10'000%

Admin deploys update

  • An admin updates the contract and deploys the code subject to this audit competition

Attacker withdraws user funds

  • The attacker makes a protocol donation to ensure that rewards are > 0
  • The attacker calls claim_fee, which in turns calls send_unclaimed_fees and update_total_rewards_and_unclaimed_fees.
  • Here the code computes the new rewards (which are > 0 because of the donation) and multiplies that with the fee percentage that the attacker set to > 100%. As a result, the global state total_unclaimed_fees is updated to a large value, larger than the actual rewards that have been collected
  • Function claim_fee transfers the fees to the admin account which results in the theft of user funds

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

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 initialise function after an upgrade does not re-validate global state invariants such as the fee being below 100%.

Impact

A rogue admin could steal user funds by claiming fees that exceed actual rewards.

What the attacker needed

Rogue admin; contract upgrade

The fix

The initialise function should check that invariants such as check_fee and check_premium are maintained after an update.

How it was found

  1. Deploy the old contract code on Algorand.
  2. A rogue admin manipulates the global state, for example setting the fee to 10,000%.
  3. The admin updates the contract to the new code.
  4. The attacker makes a protocol donation so that rewards are greater than zero.
  5. The attacker calls claim_fee, which computes new rewards multiplied by the excessive fee and updates total_unclaimed_fees to a large value.
  6. The claim_fee function transfers the inflated fees to the admin account, stealing user funds.

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 can be upgraded to new code, but the new code must check that old settings are still safe. Here, the fee percentage was not checked to be below 100%, so an admin could set it to a huge value and then take more fees than earned, taking money from users.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Folks: Liquid Staking

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.