WellUpgradeable can be upgraded by anyone
The WellUpgradeable contract's _authorizeUpgrade function lacks an onlyOwner modifier, allowing any address to call upgradeTo and upgradeToAndCall. An attacker could upgrade the contract to a malicious implementation, compromising its integrity and potentially causing total fund loss.
zanderbyte Follow with debo, unnamed and 10 others
Originally disclosed on Code4rena
Submitted by zanderbyte, also found by Egis_Security (1, 2), 0xSecuri (1, 2), debo, Walter (1, 2), unnamed, mgf15, Honour, 0x11singh99, Flare, ZanyBonzy, shaflow2, Mrxstrange, NoOne, John_Femi, and 0xvd
WellUpgradeable is an upgradeable version of the Well contract, inheriting from OpenZeppelin’s UUPSUpgradeable and OwnableUpgradeable contracts. According to OpenZeppelin’s documentation for UUPSUpgradeable.sol (here and here), the internal _authorizeUpgrade function must be overridden to include access restriction, typically using the onlyOwner modifier. This must be done to prevent unauthorized users from upgrading the contract to a potentially malicious implementation.
However, in the current implementation the _authorizeUpgrade function is overridden with custom logic but lacks the onlyOwner modifier. As a result, the upgradeTo and upgradeToAndCall methods can be invoked by any address, allowing anyone to upgrade the contract, leading to the deployment of malicious code and compromise the integrity of the contract.
Proof of concept
The following test demonstrates that WellUpgradeable can be upgraded by any address, not just the owner. It is based on testUpgradeToNewImplementation with the difference that a new user address is created and used to call the upgradeTo function, successfully upgrading the contract and exposing the lack of access control.
Paste the following test into WellUpgradeable.t.sol:
function testUpgradeToNewImplementationNotOwner() public {
IERC20[] memory tokens = new IERC20[](2);
tokens[0] = new MockToken("BEAN", "BEAN", 6);
tokens[1] = new MockToken("WETH", "WETH", 18);
Call memory wellFunction = Call(wellFunctionAddress, abi.encode("2"));
Call[] memory pumps = new Call[](1);
pumps[0] = Call(mockPumpAddress, abi.encode("2"));
// create new mock Well Implementation:
address wellImpl = address(new MockWellUpgradeable());
WellUpgradeable well2 =
encodeAndBoreWellUpgradeable(aquifer, wellImpl, tokens, wellFunction, pumps, bytes32(abi.encode("2")));
vm.label(address(well2), "upgradeableWell2");
address user = makeAddr("user");
vm.startPrank(user);
WellUpgradeable proxy = WellUpgradeable(payable(proxyAddress));
proxy.upgradeTo(address(well2));
// verify proxy was upgraded.
assertEq(address(well2), MockWellUpgradeable(proxyAddress).getImplementation());
vm.stopPrank();
}
Recommended mitigation steps
Add the onlyOwner modifier to the _authorizeUpgrade function in WellUpgradeable.sol to restrict upgrade permissions:
+ function _authorizeUpgrade(address newImplmentation) internal view override onlyOwner {
// verify the function is called through a delegatecall.
require(address(this) != ___self, "Function must be called through delegatecall");
// verify the function is called through an active proxy bored by an aquifer.
address aquifer = aquifer();
address activeProxy = IAquifer(aquifer).wellImplementation(_getImplementation());
require(activeProxy == ___self, "Function must be called through active proxy bored by an aquifer");
// verify the new implementation is a well bored by an aquifier.
require(
IAquifer(aquifer).wellImplementation(newImplmentation) != address(0),
"New implementation must be a well implementation"
);
// verify the new impelmentation is a valid ERC-1967 impelmentation.
require(
UUPSUpgradeable(newImplmentation).proxiableUUID() == _IMPLEMENTATION_SLOT,
"New implementation must be a valid ERC-1967 implementation"
);
}
Assessed type
Access Control
nickkatsios (Basin) confirmed and commented via duplicate Issue #18:
The modifier was mistakenly omitted here and should definitely be added.
The Warden and its duplicates have properly identified that the upgrade methodology of a
WellUpgradeableis insecure, permitting any contract to be upgraded to an arbitrary implementation. Specifically, the upgrade authorization mechanism will ensure that:
- The call was routed through the proxy.
- The new implementation complies with the EIP-1967 standard.
- The new implementation has been registered in the Aquifier.
Given that well registration on the Aquifier is unrestricted as seen here, it is possible to practically upgrade any well to any implementation.
I believe a high-risk assessment is valid as this represents a critical security issue that can directly lead to total fund loss for all wells deployed in the system.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The _authorizeUpgrade function was overridden without the onlyOwner modifier, so access control was missing.
Impact
Any address could upgrade the contract to a malicious implementation, leading to total fund loss for all wells deployed in the system.
What the attacker needed
None
The fix
Add the onlyOwner modifier to the _authorizeUpgrade function to restrict upgrade permissions.
How it was found
- Create a new mock Well implementation contract.
- Deploy a new upgradeable Well proxy using the Aquifer.
- As a non-owner user, call upgradeTo on the proxy with the new implementation address.
- Verify that the proxy's implementation was updated to the new contract.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Smart contracts can be designed to be upgradeable, meaning their code can be replaced later. This is done through a special function that should only be callable by the owner. Here, that function was missing the owner check, so anyone could replace the contract's code with their own, potentially stealing funds or breaking the contract.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.