Unintended or malicious use of prize winners’ hooks
The vault's setHooks function lets a user attach arbitrary hook contracts that are called during prize claims, and these hooks can make external calls and change state. An attacker could use a malicious hook to perform unauthorised side transactions, reentrant calls, or denial-of-service on claiming.
wvleak Follow with wvleak, serial-coder and 9 others
Originally disclosed on Code4rena
Submitted by wvleak, also found by wvleak, serial-coder, DadeKuma, 0xbepresent, dirk_y, keccak123, rvierdiiev, shaka, 3docSec, Udsen, and Jeiwan
The setHooks function in Vault.sol allows users to set arbitrary hooks, potentially enabling them to make external calls with unintended consequences. This vulnerability could lead to various unexpected behaviors, such as unauthorized side transactions with gas paid unbeknownst to the claimer, reentrant calls, or denial-of-service attacks on claiming transactions.
function setHooks(VaultHooks memory hooks) external {
_hooks[msg.sender] = hooks;
emit SetHooks(msg.sender, hooks);
}
Proof of Concept
Consider the following side contract and malicious hook implementation:
Side Contract:
// SPDX-License_Identifier: MIT
pragma solidity 0.8.17;
import "forge-std/console.sol";
contract SideContract {
uint256 public codex;
function execute() external {
codex = 1;
console.log("Entered side contract");
}
}
Malicious Hook:
// SPDX-License_Identifier: MIT
pragma solidity 0.8.17;
import { SideContract } from "./SideContract.sol";
contract ViciousHook {
SideContract public sideContract = new SideContract();
function beforeClaimPrize(
address winner,
uint8 tier,
uint32 prizeIndex
) external returns (address) {
sideContract.execute();
return address(this);
}
}
Modified Test File:
diff --git a/test/unit/Vault/Vault.t.sol b/test/unit/Vault/Vault.t.sol
index d0d6d30..65caca1 100644
--- a/test/unit/Vault/Vault.t.sol
+++ b/test/unit/Vault/Vault.t.sol
@@ -4,6 +4,7 @@ pragma solidity 0.8.17;
import { UnitBaseSetup, LiquidationPair, PrizePool, TwabController, VaultMock, ERC20, IERC20, IERC4626 } from "test/utils/UnitBaseSetup.t.sol";
import { IVaultHooks, VaultHooks } from "../../../src/interfaces/IVaultHooks.sol";
import "src/Vault.sol";
+import { ViciousHook } from "./ViciousHook.sol";
contract VaultTest is UnitBaseSetup {
/* ============ Events ============ */
@@ -174,6 +175,25 @@ contract VaultTest is UnitBaseSetup {
vm.stopPrank();
}
+ function testClaimPrize_viciousHook() public {
+ ViciousHook viciousHook = new ViciousHook();
+ vm.startPrank(alice);
+ VaultHooks memory hooks = VaultHooks({
+ useBeforeClaimPrize: true,
+ useAfterClaimPrize: false,
+ implementation: IVaultHooks(address(viciousHook))
+ });
+ vault.setHooks(hooks);
+ vm.stopPrank();
+
+ vm.startPrank(address(claimer));
+
+ mockPrizePoolClaimPrize(uint8(1), alice, 0, address(viciousHook), 1e18, address(claimer));
+ claimPrize(uint8(1), alice, 0, 1e18, address(claimer));
+
+ vm.stopPrank();
+ }
+
function testClaimPrize_beforeHook() public {
vm.startPrank(alice);
VaultHooks memory hooks = VaultHooks({
When running the test with forge test --match-test testClaimPrize_viciousHook -vv the output is:
Running 1 test for test/unit/Vault/Vault.t.sol:VaultTest
[PASS] testClaimPrize_viciousHook() (gas: 321545)
Logs:
Entered side contract
Test result: okay. 1 passed; 0 failed; finished in 7.29ms
This indicates that it is possible for a hook to make an external call and modify the EVM state. With that fact, there are multiple attack vectors.
Tools Used
Foundry
Recommended Mitigation Steps
To prevent any malicious calls, there are two possible solutions:
- Change the
IVaultHook.solto set the hooks as view functions and prevent EVM state changes:
diff --git a/src/interfaces/IVaultHooks.sol b/src/interfaces/IVaultHooks.sol
index 15217b8..95482c1 100644
--- a/src/interfaces/IVaultHooks.sol
+++ b/src/interfaces/IVaultHooks.sol
@@ -23,7 +23,7 @@ interface IVaultHooks {
address winner,
uint8 tier,
uint32 prizeIndex
- ) external returns (address);
+ ) external view returns (address);
/// @notice Triggered after the prize pool claim prize function is called.
/// @param winner The user who won the prize and for whom this hook is attached
@@ -37,5 +37,5 @@ interface IVaultHooks {
uint32 prizeIndex,
uint256 payout,
address recipient
- ) external;
+ ) external view;
}
- As this solution may disturb the business logic, another solution would be to cap the gas used by the hooks. In
Vault.sol, set a gas limit variable that can be adjusted by the owner of the vault for flexibility:
function _claimPrize(
address _winner,
uint8 _tier,
uint32 _prizeIndex,
uint96 _fee,
address _feeRecipient
) internal returns (uint256) {
VaultHooks memory hooks = _hooks[_winner];
address recipient;
if (hooks.useBeforeClaimPrize) {
recipient = hooks.implementation.beforeClaimPrize{gas: gasLimit}(_winner, _tier, _prizeIndex); @audit-info
} else {
recipient = _winner;
}
...
if (hooks.useAfterClaimPrize) {
hooks.implementation.afterClaimPrize{gas: gasLimit}( //@audit-info
_winner,
_tier,
_prizeIndex,
prizeTotal - _fee,
recipient
);
}
return prizeTotal;
}
asselstine (PoolTogether) confirmed and commented:
Adding an internal gas limit and then catching the revert would be ideal; so the claimer won’t be grieved and can detect badly-written hooks.
The issue here, is not the possible DoS, as a claimer can just skip one user, but the possibility of the grieving attack by, for example, front-running by a malicious hook. I’ll give partial credit to duplicates if there is too much focus on DoS.
Added hook gas limits and error handling. PR: https://github.com/GenerationSoftware/pt-v5-vault/pull/21
Status: Not fully mitigated. Full details in reports from dirk_y, rvierdiiev and 0xStalin, and in the Mitigation Review section below.
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
Likely the hook interface allows non-view external calls with no gas limit, so a malicious hook can execute arbitrary state-changing code during a prize claim.
Impact
A malicious hook could cause unauthorised side transactions with gas paid by the claimer, reentrant calls, or denial-of-service on claiming; the report notes a claimer can skip one user, but a grieving attack such as front-running by a malicious hook remains possible.
What the attacker needed
A vault user who can set hooks; a prize claim for that user
The fix
The hooks should be restricted to view functions or given a gas limit with error handling so a failing hook does not block claims; the project added hook gas limits and error handling, but the report says the issue was not fully mitigated.
How it was found
- Deploy a malicious hook contract that makes an external call in beforeClaimPrize.
- Call setHooks to attach the malicious hook to the attacker's address.
- Trigger a prize claim for that address so the hook runs.
- Observe that the external call executes and changes EVM state.
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.
A hook is a piece of code that runs automatically at a certain point, here when a prize is claimed. The vault let users supply any hook code, and that code could do extra things, like call other contracts or change data, while the claim was happening. This could disrupt or abuse the claiming process.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.