Repository navigation
fix(savings): track storedAssets to prevent donation attacks - #26
Merged
FabienCoutant merged 4 commits intoMay 27, 2026
Merged
FabienCoutant merged 4 commits into
FabienCoutant merged 4 commits into
Conversation
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
merged commit May 27, 2026
4baa958
into
feat/savings-donation-and-pause-refactor
3 of 4 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scope
Feature branch to be aggregated into
feat/savings-donation-and-pause-refactor(the code intended for audit).Changes
fix(savings): introducestoredAssets(the real backing);totalAssets()now projectsstoredAssetsinstead of the rawbalanceOf→ neutralizes donation/inflation attacks.recoverSurplus(to)lets governance recover the untracked surplus.togglePauseintopause()/unpause()with idempotence guards (AlreadyPaused/NotPaused), keeping the_accrue()on each transition (Cyfrin I-2 fix).ISavingsaligned (pause/unpause,estimatedAPY,recoverSurplus),recoverSurplusNatSpec (scope limited toasset()).initializeStoredAssets()reinitializer(2) to seed on upgrade.Tests
Full suite green: 372 tests passed, 0 failed (fuzz included).
🤖 Generated with Claude Code