Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: returnAsset exploit #222

Merged
merged 14 commits into from
Feb 23, 2024
Merged

fix: returnAsset exploit #222

merged 14 commits into from
Feb 23, 2024

Conversation

xenide
Copy link
Contributor

@xenide xenide commented Feb 6, 2024

Motivation

  • bug was due to
uint256 lCurrentShares = shares[aPair][aToken];
// this is to prevent underflow as we round up in the previous division operation
if (rShares > lCurrentShares) {
   rShares = lCurrentShares;
}
  • code was introduced in test: asset manager corner cases #187

  • mulDivUp behavior

    • does not round up if division is exact, only rounds up when there is remainder
  • in our current production deployment of asset manager, all pairs have redeemed successfully (tokens managed are all 0 from the perspective of the pair), but individual and total shares are more than 0. Of course we have a special case where there was a loss (wasn't strictly increasing)

Screenshot 2024-02-22 at 10 29 07

Potential Solutions

  1. add a condition to check that lCurrentShares is greater than 0
if (rShares > lCurrentShares && lCurrentShares > 0) 
  • this is not secure, as lCurrentShares can be 1, and rShares can be a really large number. i.e. an attacker can get several shares by letting the asset manager manage its assets, and then still attack using returnAsset
  1. remove rShares > lCurrentShares check completely, relying on checked arithmetic.
  • this is the recommended solution (implemented in this PR), as underflow will occur in the case of attempting to redeem 0 shares
  • I've attempted to break this by fuzzing 100K times. So far none of them have triggered the subtraction underflow problem
    • the total number of shares and individual number of shares may not end up being 0, but at least the token0/1Managed will be zero.
      • this is only happening for very small amounts (e.g. amounts less than 10 wei)
  • are there specific numbers to break it?
  1. any other solutions you can think of?

Copy link

codecov bot commented Feb 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.08%. Comparing base (5fe192e) to head (631803e).

❗ Current head 631803e differs from pull request most recent head 4ab4bc0. Consider uploading reports for the commit 4ab4bc0 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##              dev     #222       +/-   ##
===========================================
+ Coverage   68.21%   88.08%   +19.86%     
===========================================
  Files          12       12               
  Lines         623      621        -2     
===========================================
+ Hits          425      547      +122     
+ Misses        198       74      -124     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@OliverNChalk
Copy link
Contributor

This change was originally introduced to ensure a full redemption was possible right? I think provided we have a way to recover if this maths breaks in full redemption mode then we can be reasonably comfortable. I.e. it may make sense to give the admin super user style power to make calls from the asset manager if needed?

You could imagine a function like this:

function rawCall(address target, uint256 value, bytes data) external onlyOwner

@xenide
Copy link
Contributor Author

xenide commented Feb 22, 2024

So previously when we did the PR, we were relying on an _getExchangeRate internal function, which was only accurate to 18 decimal places, which probably resulted in a few wei of difference.

Then during the audit, the auditors recommended us to perform the arithmetic directly (without using an intermediate exchange rate) which probably eliminated this problem. That said, with the testing I've done, I haven't been able to come up with a scenario in which full redemption will fail.

Yes I thought of the same regarding introducing rawCall too. If we put it behind a timelock I think it's acceptable to everyone. Will implement

@OliverNChalk
Copy link
Contributor

So previously when we did the PR, we were relying on an _getExchangeRate internal function, which was only accurate to 18 decimal places, which probably resulted in a few wei of difference.

Then during the audit, the auditors recommended us to perform the arithmetic directly (without using an intermediate exchange rate) which probably eliminated this problem. That said, with the testing I've done, I haven't been able to come up with a scenario in which full redemption will fail.

Yes I thought of the same regarding introducing rawCall too. If we put it behind a timelock I think it's acceptable to everyone. Will implement

Assuming the owner is the timelock, it should be acceptable?

@xenide xenide marked this pull request as ready for review February 23, 2024 06:59
test/integration/Aave.t.sol Outdated Show resolved Hide resolved
@xenide xenide merged commit c78ecaf into dev Feb 23, 2024
8 of 9 checks passed
@xenide xenide deleted the fix/returnAsset-exploit branch February 23, 2024 14:16
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.

2 participants