Skip to content

fix(savings): track storedAssets to prevent donation attacks - #26

Merged
FabienCoutant merged 4 commits into
feat/savings-donation-and-pause-refactorfrom
fix/savings-donation-attack
May 27, 2026
Merged

FabienCoutant merged 4 commits into
feat/savings-donation-and-pause-refactorfrom
fix/savings-donation-attack

Conversation

@FabienCoutant

@FabienCoutant FabienCoutant commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Scope

Feature branch to be aggregated into feat/savings-donation-and-pause-refactor (the code intended for audit).

Changes

  • fix(savings): introduce storedAssets (the real backing); totalAssets() now projects storedAssets instead of the raw balanceOf → neutralizes donation/inflation attacks. recoverSurplus(to) lets governance recover the untracked surplus.
  • Split the Savings togglePause into pause()/unpause() with idempotence guards (AlreadyPaused/NotPaused), keeping the _accrue() on each transition (Cyfrin I-2 fix).
  • ISavings aligned (pause/unpause, estimatedAPY, recoverSurplus), recoverSurplus NatSpec (scope limited to asset()).
  • initializeStoredAssets() reinitializer(2) to seed on upgrade.

Tests

Full suite green: 372 tests passed, 0 failed (fuzz included).

🤖 Generated with Claude Code

FabienCoutant added 4 commits May 27, 2026 14:10
The audit/eip3009-hotfix-fees branch grew the Storage struct (Surplus
fields) past the point where Yul can route stack variables through
EVM stack slots in a few code paths, surfacing as
"Variable size is 1 too deep in the stack" during compilation.

Disabling via_ir at the default profile sidesteps the issue across
the whole compilation graph. The dev and ci profiles already had
via_ir = false, so this only changes the unprofiled forge invocation
(the one CI and most local runs use).
- SECONDS_PER_YEAR (= 365 days) replaces the hardcoded 31_536_000
  literal in Savings.estimatedAPY().
- AlreadyPaused / NotPaused errors enable explicit pause/unpause
  flows that revert on no-op governance calls instead of silently
  toggling.
The Savings ERC4626 vault was vulnerable to donation/inflation
attacks because totalAssets() read the raw IERC20.balanceOf(self),
allowing any direct USDp transfer to inflate share value pro-rata
to all holders. At low TVL or after long dormancy this turned even
a 1-share position into a captureable windfall.

Mitigation:
- New storedAssets state variable (consumes 1 slot from __gap[48]
  -> [47]) tracks the only legitimate backing: deposits in,
  withdrawals out, accrued interest minted in.
- totalAssets() now projects storedAssets through the existing
  rate formula instead of the raw ERC20 balance.
- _accrue() reads from and writes to storedAssets so dormant
  accruals are properly accounted, never reclassified as surplus.
- _deposit / _withdraw overridden to mirror standard ERC4626
  movements into storedAssets.
- depositWithAuthorization and _redeemWithAuthorization (EIP-3009
  paths that bypass _deposit/_withdraw) update storedAssets
  directly around the receiveWithAuthorization / safeTransfer
  calls so the same backing accounting holds across all entry
  points.
- initializeStoredAssets() reinitializer(2) seeds storedAssets
  from the current balance on upgrade so existing depositors
  remain backed.
- recoverSurplus(to) restricted hatch transfers any
  balanceOf(self) - storedAssets delta (donations, accidental
  transfers) to a destination chosen by governance, with no
  effect on share holders.

Companion refactors in the same surface:
- togglePause split into pause()/unpause() with idempotence checks
  (revert AlreadyPaused / NotPaused) so a misclicked governance
  call cannot silently flip the pause state.
- estimatedAPR renamed to estimatedAPY to match the per-second
  compounding semantics of _computeUpdatedAssets.
- Magic literal 31_536_000 replaced by SECONDS_PER_YEAR constant.

Operational note: the deployment must call upgradeToAndCall with
abi.encodeWithSelector(initializeStoredAssets.selector) atomically.
Skipping the reinitializer would leave storedAssets at 0, blocking
all subsequent withdrawals.
Donation attack invariant fuzz lives next to the existing rate /
deposit / pause fuzz tests in tests/fuzz/Savings.t.sol:
- testFuzz_DonationAttackInflationLockedDown: invariant fuzz over
  donation amount up to 1B USDp — preview and redeem outcomes are
  unchanged by direct ERC20 transfers.

Deterministic coverage moves to tests/units/Savings.t.sol in two
contracts:
- SavingsUpgradeTest groups the existing fresh-deploy + upgradeTo
  cases with the new upgrade-from-legacy path. Cases: donation
  attack succeeds on legacy then is neutralized after upgrade;
  existing depositors still redeem; initializeStoredAssets
  reinitializer cannot be reused; pause/unpause selectors function
  once wired into the access manager.
- SavingsDonationAttackTest covers the storedAssets fix, the
  recoverSurplus governance hatch (drain, zero-address guard,
  unauthorised caller, no-donation case), the dormant-accrual
  accounting and the new pause/unpause idempotence checks.

The upgrade path is exercised through ERC1967Proxy.upgradeToAndCall
backed by a SavingsLegacyMock that mirrors the pre-fix storage
layout and behaviour (BaseSavings + SavingsEIP3009 inheritance,
__gap[48], no storedAssets slot). The mock is confined to
tests/mock/ and never deployed in production paths.
@FabienCoutant
FabienCoutant merged commit 4baa958 into feat/savings-donation-and-pause-refactor May 27, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant