-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExternalCallLib which checks revert data for malicious data (#2004)
* feat: add ExternalCallLib which checks revert data for malicious data * feat: add protection for swap as well as join/exits * style: linting * feat: integrate ExternalCallLib into AaveLinearPool * style: linter * Apply suggestions from code review Co-authored-by: Nicolás Venturo <[email protected]> * refactor: rename `bubbleUpNonMaliciousRevert` to `bubbleUpNonMaliciousRevert` * test: remove duplication of test code in ExternalCallLib.test.ts * test: add tests to ensure that protection doesn't mangle non-malicious revert data * style: linter * test: test LinearPool handles malicious joins/exits as well as swaps * style: move constructor test to top of file * test: ensure that asset manager addresses are non-zero * test: add test to prevent rebalancing in case of malicious external call revert Co-authored-by: Nicolás Venturo <[email protected]>
- Loading branch information
1 parent
aa992dd
commit 551df1b
Showing
11 changed files
with
467 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
pragma solidity ^0.7.0; | ||
|
||
import "@balancer-labs/v2-interfaces/contracts/pool-linear/IStaticAToken.sol"; | ||
|
||
import "@balancer-labs/v2-pool-utils/contracts/test/MaliciousQueryReverter.sol"; | ||
|
||
import "@balancer-labs/v2-solidity-utils/contracts/test/TestToken.sol"; | ||
|
||
contract MockAaveLendingPool is ILendingPool, MaliciousQueryReverter { | ||
uint256 private _rate = 1e27; | ||
|
||
function getReserveNormalizedIncome(address) external view override returns (uint256) { | ||
maybeRevertMaliciously(); | ||
return _rate; | ||
} | ||
|
||
function setReserveNormalizedIncome(uint256 newRate) external { | ||
_rate = newRate; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
pragma solidity ^0.7.0; | ||
|
||
import "@balancer-labs/v2-interfaces/contracts/solidity-utils/helpers/BalancerErrors.sol"; | ||
|
||
library ExternalCallLib { | ||
function bubbleUpNonMaliciousRevert(bytes memory errorData) internal pure { | ||
uint256 errorLength = errorData.length; | ||
|
||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
// If the first 4 bytes match the selector for one of the error signatures used by `BasePool._queryAction` | ||
// or `Vault.queryBatchSwap` then this error is attempting to impersonate the query mechanism used by these | ||
// contracts in order to inject bogus data. This can result in loss of funds if the return value is then | ||
// used in a later calculation. | ||
// | ||
// We then want to reject the following error signatures: | ||
// - `QueryError(uint256,uint256[])` (used by `BasePool._queryAction`) | ||
// - `QueryError(int256[])` (used by `Vault.queryBatchSwap`) | ||
|
||
// We only bubble up the revert reason if it doesn't match the any of the selectors for these error | ||
// sigatures, otherwise we revert with a new error message flagging that the revert was malicious. | ||
let error := and( | ||
mload(add(errorData, 0x20)), | ||
0xffffffff00000000000000000000000000000000000000000000000000000000 | ||
) | ||
if iszero( | ||
or( | ||
// BasePool._queryAction | ||
eq(error, 0x43adbafb00000000000000000000000000000000000000000000000000000000), | ||
// Vault.queryBatchSwap | ||
eq(error, 0xfa61cc1200000000000000000000000000000000000000000000000000000000) | ||
) | ||
) { | ||
revert(add(errorData, 0x20), errorLength) | ||
} | ||
} | ||
|
||
// We expect the assembly block to revert for all non-malicious errors. | ||
_revert(Errors.MALICIOUS_QUERY_REVERT); | ||
} | ||
} |
Oops, something went wrong.