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

ProtocolDAO::upgradeExistingContract is not working as intended and can have bad consequences #706

Closed
code423n4 opened this issue Jan 3, 2023 · 3 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-742 satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/ProtocolDAO.sol#L209-L217

Vulnerability details

The contracts used in GoGoPool protocol are using fixed names when interacting with each other. For example:

Vault vault = Vault(getContractAddress("Vault"));
Staking staking = Staking(getContractAddress("Staking"));
RewardsPool rewardsPool = RewardsPool(getContractAddress("RewardsPool"));
TokenGGP ggp = TokenGGP(getContractAddress("TokenGGP"));
...

The function upgradeExistingContract if used to upgrade an existing contract name with a new address will result in getContractAddress function to return zero address for the upgraded contract address.

If used with a new name then getContractAddress will return the supplied address but as already stated the protocol uses fixed names.

Impact

If an upgrade like this happen funds can be lost for example:

When distributing rewards

TokenGGP ggp = TokenGGP(getContractAddress("TokenGGP"));
Vault vault = Vault(getContractAddress("Vault"));

if (daoClaimContractAllotment > 0) {
    emit ProtocolDAORewardsTransfered(daoClaimContractAllotment);
    vault.transferToken("ClaimProtocolDAO", ggp, daoClaimContractAllotment);
}

funds can be lost if the upgrade happened for "Vault" when vault.transferToken("ClaimProtocolDAO", ggp, daoClaimContractAllotment); is called the function will not revert but also the token amount will be lost

Or for example in claimAndRestake function:

if (claimAmt > 0) {
    vault.withdrawToken(msg.sender, ggp, claimAmt);
}

again vault.withdrawToken(msg.sender, ggp, claimAmt); will not work as expected.

Proof of Concept

https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/ProtocolDAO.sol#L209-L217

Add the following test to ProtocolDAO.t.sol unit test file:

function testUpgradeExistingContract_POC1() public {
    address addr = randAddress();

    address existingAddr = randAddress();
    string memory existingName = "existingName";

    bytes32 testKey = "testKey";

    vm.prank(guardian);
    dao.registerContract(existingAddr, existingName);
    assertEq(store.getBool(keccak256(abi.encodePacked("contract.exists", existingAddr))), true);
    assertEq(store.getAddress(keccak256(abi.encodePacked("contract.address", existingName))), existingAddr);
    assertEq(store.getString(keccak256(abi.encodePacked("contract.name", existingAddr))), existingName);

    vm.prank(guardian);
    dao.upgradeExistingContract(addr, existingName, existingAddr);
    assertEq(store.getBool(keccak256(abi.encodePacked("contract.exists", addr))), true);
    assertEq(store.getAddress(keccak256(abi.encodePacked("contract.address", existingName))), addr);
}

run with forge test -m testUpgradeExistingContract_POC1 the test will fail when comapring the expected address with the real return after upgrading an existing contract name:

[FAIL. Reason: Assertion failed.] testUpgradeExistingContract_POC1() (gas: 159000)
Logs:
  Error: a == b not satisfied [address]
    Expected: 0x934eb1f9d0f59695050f761dc64e443e5030a569
      Actual: 0x0000000000000000000000000000000000000000

Tools Used

Manual review

Recommended Mitigation Steps

Invert the order of operation as stated in the function comment header

/// @notice Upgrade a contract by unregistering the existing address, and registring a new address and name
function upgradeExistingContract(
    address newAddr,
    string memory newName,
    address existingAddr
) external onlyGuardian {
-	registerContract(newAddr, newName);
-	unregisterContract(existingAddr);
+   unregisterContract(existingAddr);
+   registerContract(newAddr, newName);
	}
@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Jan 3, 2023
code423n4 added a commit that referenced this issue Jan 3, 2023
C4-Staff added a commit that referenced this issue Jan 6, 2023
@GalloDaSballo
Copy link

I really liked the logs of the reverting test with error

@c4-judge
Copy link
Contributor

c4-judge commented Jan 9, 2023

GalloDaSballo marked the issue as duplicate of #742

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Feb 8, 2023
@c4-judge
Copy link
Contributor

c4-judge commented Feb 8, 2023

GalloDaSballo marked the issue as satisfactory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-742 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

3 participants