New subcontractor can be set for a SCConfirmed task without current subcontractor consent
The inviteSC function allows a builder or contractor to change the subcontractor of a task that is already confirmed and active, without the current subcontractor's consent. An attacker could front-run a legitimate setComplete call to redirect the task budget to their own subcontractor, stealing funds from the original subcontractor.
Malicious builder/contractor can change the subcontractor for any task even if all the terms was agreed upon and work was started/finished, but the task wasn’t set to completed yet, i.e. it’s SCConfirmed, getAlerts(_taskID)[2] == true. This condition is not checked by inviteSC().
For example, a contractor can create a subcontractor of her own and front run valid setComplete() call with a sequence of inviteSC(task, own_subcontractor) -> setComplete() with a signatory from the own_subcontractor, stealing the task budget from the subcontractor who did the job. Contractor will not breach any duties with the community as the task will be done, while raiseDispute() will not work for a real subcontractor as the task record will be already changed.
Setting the severity to be high as this creates an attack vector to fully steal task budget from the subcontractor as at the moment of any valid setComplete() call the task budget belongs to subcontractor as the job completion is already verified by all the parties.
Proof of Concept
inviteSC() requires either builder or contractor to call for the change and verify nothing else:
// Mark task as complete. Only works when task is active.
tasks[_taskID].setComplete();
// Transfer funds to subcontractor.
currency.safeTransfer(
tasks[_taskID].subcontractor,
tasks[_taskID].cost
);
This way the absence of getAlerts(_taskID)[2] check and checkSignatureTask() call in inviteSC() provides a way for builder or contractor to steal task budget from a subcontractor.
Recommended Mitigation Steps
Consider calling checkSignatureTask() when getAlerts(_taskID)[2] is true, schematically:
// Invite subcontractor for each task.
for (uint256 i = 0; i < _length; i++) {
+ if (getAlerts(_taskList[i])[2])
+ checkSignatureTask(_data_with_scList[i], _signature, _taskList[i]);
_inviteSC(_taskList[i], _scList[i], false);
}
This approach is already implemented in changeOrder() where _newSC is a part of hash that has to be signed by all the parties:
// If new subcontractor is not zero address.
if (_newSC != address(0)) {
// Invite the new subcontractor for the task.
_inviteSC(_taskID, _newSC, true);
}
// When builder has not delegated rights to contractor
else {
// Check for B, SC and GC signatures
checkSignatureValidity(builder, _hash, _signature, 0);
checkSignatureValidity(contractor, _hash, _signature, 1);
checkSignatureValidity(_sc, _hash, _signature, 2);
}
When a SC accepts an invitation the task is marked as active Tasks.sol#L128.
So as you noted here above the inviteSubcontractor for the same task will fail because of the modifier.
function inviteSubcontractor(Task storage _self, address _sc)
internal
onlyInactive(_self)
{
Yes, you are right, in general case onlyInactive modifier guards the reset.
The issue appears to be more specific, in the case when task budget is increased, while there is no budget to cover it, i.e. totalLent - _totalAllocated < _newCost - _taskCost, the subcontractor signs only the budget increase itself, while subcontractor ends up being unassigned from it fully:
// If tasks are already allocated with old cost.
if (tasks[_taskID].alerts[1]) {
// If new task cost is less than old task cost.
if (_newCost < _taskCost) {
// Find the difference between old - new.
uint256 _withdrawDifference = _taskCost - _newCost;
// Reduce this difference from total cost allocated.
// As the same task is now allocated with lesser cost.
totalAllocated -= _withdrawDifference;
// Withdraw the difference back to builder's account.
// As this additional amount may not be required by the project.
autoWithdraw(_withdrawDifference);
}
// If new cost is more than task cost but total lent is enough to cover for it.
else if (totalLent - _totalAllocated >= _newCost - _taskCost) {
// Increase the difference of new cost and old cost to total allocated.
totalAllocated += _newCost - _taskCost;
}
// If new cost is more than task cost and totalLent is not enough.
else {
// Un-confirm SC, mark task as inactive, mark allocated as false, mark lifecycle as None
// Mark task as inactive by unapproving subcontractor.
// As subcontractor can only be approved if task is allocated
_unapproved = true;
tasks[_taskID].unApprove();
// Mark task as not allocated.
tasks[_taskID].unAllocateFunds();
// Reduce total allocation by old task cost.
// As as needs to go though funding process again.
totalAllocated -= _taskCost;
// Add this task to _changeOrderedTask array. These tasks will be allocated first.
_changeOrderedTask.push(_taskID);
}
}
Suppose task is 95% complete, its budget is fully spent, so changeOrder() is called per mutual agreement to add extra 0.05 * old_cost / 0.95 funds, which aren’t lent yet. Dishonest contractor can call inviteSC with own subcontractor, who will receive full old_cost / 0.95 on completion.
I.e. fully removing subcontractor from already funded and started task provides a more specific similar attack surface.
By definition unApprove deals with the case of new task that needs to be reviewed:
/**
* @dev Set a task as un accepted/approved for SC
* @dev modifier onlyActive
* @param _self Task the task being set as funded
*/
function unApprove(Task storage _self) internal {
// State/ lifecycle //
_self.alerts[uint256(Lifecycle.SCConfirmed)] = false;
_self.state = TaskStatus.Inactive;
}
But in changeOrder() all the parties already reviewed and accepted the terms:
// Decode params from _data
(
uint256 _taskID,
address _newSC,
uint256 _newCost,
address _project
) = abi.decode(_data, (uint256, address, uint256, address));
// If the sender is disputes contract, then do not check for signatures.
if (_msgSender() != disputes) {
// Check for required signatures.
checkSignatureTask(_data, _signature, _taskID);
}
// When builder has not delegated rights to contractor
else {
// Check for B, SC and GC signatures
checkSignatureValidity(builder, _hash, _signature, 0);
checkSignatureValidity(contractor, _hash, _signature, 1);
checkSignatureValidity(_sc, _hash, _signature, 2);
}
So, marking the task as not active and not SCConfirmed doesn’t look correct in this case.
Straightforward mitigation here is to keep it active, i.e. do partial flag removal, say do unConfirm instead of unApprove:
else {
- // Un-confirm SC, mark task as inactive, mark allocated as false, mark lifecycle as None
- // Mark task as inactive by unapproving subcontractor.
- // As subcontractor can only be approved if task is allocated
- _unapproved = true;
- tasks[_taskID].unApprove();
+ // Mark task as inactive, mark allocated as false, add to the allocation queue
+ // Mark task as inactive
+ tasks[_taskID].deActivate();
// Mark task as not allocated.
tasks[_taskID].unAllocateFunds();
// Reduce total allocation by old task cost.
// As as needs to go though funding process again.
totalAllocated -= _taskCost;
// Add this task to _changeOrderedTask array. These tasks will be allocated first.
_changeOrderedTask.push(_taskID);
}
Notice that the mitigation here is to make Active and SCConfirmed states independent (as a general note, it doesn’t make much sense to have some fully coinciding states). Active flags whether task is in progress right now, while SCConfirmed flags whether it ever was started, being either Active (work is being done right now) or Inactive (work had started, something was done, now it’s paused).
The issue basically means that the states are different and moving a task to another SC while it’s SCConfirmed should be prohibited as some work was done and some payment to current SC is due
This is a good one with a very detailed explanation, but I’m afraid it fits a Medium severity better, as funds are not directly at risk but rather a malfunctioning feature that can indirectly cause damage to certain roles.
The original report as published on Code4rena, by hyh. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the inviteSC function lacks a check for the SCConfirmed alert and does not require signatures from all parties when the task is already confirmed, allowing the subcontractor to be changed without consent.
Impact
A malicious builder or contractor could steal the task budget from the legitimate subcontractor by changing the subcontractor just before completion, and the real subcontractor cannot raise a dispute because the task record is already changed.
What the attacker needed
Attacker is builder or contractor; task is SCConfirmed; attacker can front-run setComplete
The fix
The inviteSC function should check if the task is SCConfirmed and require signatures from all parties before changing the subcontractor, similar to changeOrder.
How it was found
A contractor creates a subcontractor of their own.
The contractor calls inviteSC to set the new subcontractor for a task that is already SCConfirmed.
The contractor front-runs a valid setComplete call with a sequence of inviteSC and setComplete, using a signatory from the new subcontractor.
The task budget is transferred to the new subcontractor, stealing from the original subcontractor.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In a smart contract for project tasks, a task can be assigned to a subcontractor. Once the subcontractor accepts and starts work, the task becomes 'confirmed'. The bug is that the function to invite a new subcontractor does not check if the task is already confirmed, so someone could replace the subcontractor just before the task is marked complete and steal the payment.
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.