OperatorProposal.sol : Leftover ETH is not refunded to the msg.sender
The OperatorProposal contract sends ETH to an operator contract but does not refund any leftover ETH to the executor who provided it. An attacker (or any executor) could lose funds because the remaining ETH is effectively transferred to the Party.
HollaDieWaldfee Follow with hansfriese
Originally disclosed on Code4rena
Submitted by HollaDieWaldfee, also found by hansfriese
The OperatorProposal contract is a type of proposal that allows to execute operations on contracts that implement the IOperator interface.
Upon execution of the proposal it might be necessary that the executor provides ETH.
This is true especially when allowOperatorsToSpendPartyEth=false, i.e. when ETH cannot be spent from the Party’s balance. So it must be provided by the executor.
The amount of ETH that is needed to execute the operation is sent to the operator contract:
data.operator.execute{ value: data.operatorValue }(data.operatorData, executionData);
The operator contract then spends whatever amount of ETH is actually necessary and returns the remaining ETH.
For example the CollectionBatchBuyOperator contract may not spend all of the ETH because the actual purchases that are made are not necessarily known at the time the proposal is created. Also not all purchases may succeed.
So it is clear that some of the ETH may be returned from the operator to the OperatorProposal contract.
The issue is that the remaining ETH is not refunded to the executor and therefore this results in a direct loss of funds for the executor.
I discussed this issue with the sponsor and it is clear that the remaining ETH needs to be refunded when allowOperatorsToSpendPartyEth=false.
However it is not clear what to do when allowOperatorsToSpendPartyEth=true. In this case ETH can be spent from the party’s balance. So there should be limited use cases for the executor providing additional ETH.
But if the executor provides additional ETH what should happen?
Should the ETH be taken from the executor first? Or should it be taken from the Party balance first?
The sponsor mentioned that since there are limited use cases for the executor providing additional ETH it may be ok to not refund ETH at all.
I disagree with this. Even when allowOperatorsToSpendPartyEth=true there should be a policy for refunds. I.e. the necessary ETH should either be taken from the Party’s balance or from the executor first and any remaining funds from the executor should be returned.
However since it is not clear how to proceed in this case and since it is less important compared to the case where allowOperatorsToSpendPartyEth=false I will only make a suggestion for the case where allowOperatorsToSpendPartyEth=false.
The sponsor should decide what to do in the other case and make the appropriate changes.
Proof of Concept
When the executor executes an OperatorProposal, operatorValue amount of ETH is sent to the operator contract (when allowOperatorsToSpendPartyEth=false all of these funds must come from the msg.value):
if (!allowOperatorsToSpendPartyEth && data.operatorValue > msg.value) {
revert NotEnoughEthError(data.operatorValue, msg.value);
}
// Execute the operation.
data.operator.execute{ value: data.operatorValue }(data.operatorData, executionData);
Currently the only operator contract that is implemented is the CollectionBatchBuyOperator and as explained above not all of the funds may be used so the funds are sent back to the Party:
uint256 unusedEth = msg.value - totalEthUsed;
if (unusedEth > 0) payable(msg.sender).transferEth(unusedEth);
However after calling the operator contract, the OperatorProposal contract just returns without sending back the unused funds to the executor (msg.sender).
// Nothing left to do.
return "";
So there is a loss of funds for the executor. The leftover funds are effectively transferred to the Party.
Tools Used
VSCode
Recommended Mitigation Steps
As mentioned before, this is only a fix for the case when allowOperatorsToSpendPartyEth=false.
Fix:
diff --git a/contracts/proposals/OperatorProposal.sol b/contracts/proposals/OperatorProposal.sol
index 23e2897..507e0d5 100644
--- a/contracts/proposals/OperatorProposal.sol
+++ b/contracts/proposals/OperatorProposal.sol
@@ -4,7 +4,11 @@ pragma solidity 0.8.17;
import "./IProposalExecutionEngine.sol";
import "../operators/IOperator.sol";
+import "../utils/LibAddress.sol";
+
contract OperatorProposal {
+ using LibAddress for address payable;
+
struct OperatorProposalData {
// Addresses that are allowed to execute the proposal and decide what
// calldata used by the operator proposal at the time of execution.
@@ -41,9 +45,17 @@ contract OperatorProposal {
revert NotEnoughEthError(data.operatorValue, msg.value);
}
+ uint256 partyBalanceBefore = address(this).balance - msg.value;
+
// Execute the operation.
data.operator.execute{ value: data.operatorValue }(data.operatorData, executionData);
+ if (!allowOperatorsToSpendPartyEth) {
+ if (address(this).balance - partyBalanceBefore > 0) {
+ payable(msg.sender).transferEth(address(this).balance - partyBalanceBefore);
+ }
+ }
+
// Nothing left to do.
return "";
}
Looking forward to sponsor comment on this. As described in my comment on
#30, I am not entirely sure that this qualifies as Medium based on the fact that the caller presumably is able to call with the correct value. QA may be more appropriate.
HollaDieWaldfee (warden) commented:
@0xean - It is not true that the caller can just use the function with the correct value:
“For example the CollectionBatchBuyOperator contract may not spend all of the ETH because the actual purchases that are made are not necessarily known at the time the proposal is created. Also not all purchases may succeed.”
0xble (Party) confirmed and commented:
@HollaDieWaldfee is right, there is at least one case with the
CollectionBatchBuyOperatorwhere it may not use all the ETH and the user may expect to be refunded. I think marking as Medium is valid.
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 OperatorProposal contract does not track or refund any unused ETH after calling the operator, leaving the leftover funds in the contract.
Impact
The executor loses any leftover ETH, which is effectively transferred to the Party; this occurs when allowOperatorsToSpendPartyEth is false.
What the attacker needed
AllowOperatorsToSpendPartyEth is false; executor sends more ETH than needed; operator does not use all ETH
The fix
The contract should refund any leftover ETH to the executor when allowOperatorsToSpendPartyEth is false, as shown in the suggested code change.
How it was found
- The executor calls the proposal with an ETH value greater than the operatorValue.
- The OperatorProposal sends operatorValue ETH to the operator contract.
- The operator uses only part of the ETH and returns the unused portion to the OperatorProposal contract.
- The OperatorProposal contract returns without refunding the leftover ETH to the executor.
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.
When someone runs a proposal, they may send extra money (ETH) to cover costs. The operator contract uses only what it needs and sends the rest back to the proposal contract. But the proposal contract keeps that leftover money instead of returning it to the person who sent it, so that person loses money.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.