TitlesGraph::acknowledgeEdge() methods do not write acknowledgments to storage
The acknowledgeEdge function fails to persist the acknowledged flag because it modifies a memory copy of the Edge struct instead of storage. An attacker cannot exploit this directly, but the intended functionality of acknowledging edges is broken.
Source: https://github.com/sherlock-audit/2024-04-titles-judging/issues/212
Found by
4rdiii, AlexCzm, CodeWasp, KupiaSec, alexzoid, brakeless, cducrest-brainbot, den_sosnovskyi, fugazzi, i3arba, ironside, kennedy1030, radin200, recursiveEth, ubl4nk
Summary
When acknowledgeEdge() is called, the downstream call to the _setAcknowledged() method caches edges[edgeId_] in memory, instead of storage, which does not preserve changes to the Edge struct after the transaction concludes.
Vulnerability Detail
function acknowledgeEdge(bytes32 edgeId_, bytes calldata data_)
external
override
returns (Edge memory edge)
{
if (!_isCreatorOrEntity(edges[edgeId_].to, msg.sender)) revert Unauthorized();
return _setAcknowledged(edgeId_, data_, true);
}
function _setAcknowledged(bytes32 edgeId_, bytes calldata data_, bool acknowledged_)
internal
returns (Edge memory edge)
{
if (!_edgeIds.contains(edgeId_)) revert NotFound();
edge = edges[edgeId_];
edge.acknowledged = acknowledged_; // @audit does this actually write to storage? the state isn't saved?
if (acknowledged_) {
emit EdgeAcknowledged(edge, msg.sender, data_);
} else {
emit EdgeUnacknowledged(edge, msg.sender, data_);
}
}
In the code snippet above, edges[edgeId_] is cached in edge, then modified.
The issue is that edge, the return variable, is marked as memory, which does not save the state after the transaction ends.
Therefore, the acknowledged value never changes and the acknowledgeEdge() methods do not work as intended.
Add the following test to TitlesGraph.t.sol.
Run with the following command: forge test --match-test test_acknowledgeEdgeFailure -vvvv
function test_acknowledgeEdgeFailure() public {
Node memory from = Node({
nodeType: NodeType.COLLECTION_ERC1155,
entity: Target({target: address(1), chainId: block.chainid}),
creator: Target({target: address(2), chainId: block.chainid}),
data: ""
});
Node memory to = Node({
nodeType: NodeType.TOKEN_ERC1155,
entity: Target({target: address(3), chainId: block.chainid}),
creator: Target({target: address(4), chainId: block.chainid}),
data: abi.encode(42)
});
// Only the `from` node's entity can create the edge.
vm.prank(from.entity.target);
titlesGraph.createEdge(from, to, "");
vm.expectEmit(true, true, true, true);
emit IEdgeManager.EdgeAcknowledged(
Edge({from: from, to: to, acknowledged: true, data: ""}), to.creator.target, ""
);
// Only the `to` node's creator (or the entity itself) can acknowledge it
vm.prank(to.creator.target);
titlesGraph.acknowledgeEdge(keccak256(abi.encode(from, to)), "");
(Node memory nodeTo,
Node memory nodeFrom,
bool ack,
bytes memory dataResult
) = titlesGraph.edges(titlesGraph.getEdgeId(from, to));
console.log(ack);
// edge[edgeId].acknowledged should be set to true after successful call to titlesGraph.acknowledgeEdge()
// However, the value is still false.
assert(true == ack);
}
[FAIL. Reason: panic: assertion failed (0x01)] test_acknowledgeEdgeFailure() (gas: 429897)
Logs:
false
Impact
Edges are unable to be acknowledged.
Code Snippet
Tool used
Manual Review
Recommendation
The simplest fix would be to change memory to storage.
function _setAcknowledged(bytes32 edgeId_, bytes calldata data_, bool acknowledged_)
internal
- returns (Edge memory edge)
+ returns (Edge storage edge)
{
if (!_edgeIds.contains(edgeId_)) revert NotFound();
edge = edges[edgeId_];
edge.acknowledged = acknowledged_;
if (acknowledged_) {
emit EdgeAcknowledged(edge, msg.sender, data_);
} else {
emit EdgeUnacknowledged(edge, msg.sender, data_);
}
}
Discussion
pqseags
This will be fixed
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/titlesnyc/wallflower-contract-v2/pull/1
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 function returns a memory copy of the Edge struct and modifies that copy, so the change is not written to storage.
Impact
Edges cannot be acknowledged, so the intended functionality is broken.
What the attacker needed
None
The fix
The issue was fixed by changing the return variable from memory to storage.
How it was found
- Create an edge between two nodes.
- Call acknowledgeEdge as the creator of the 'to' node.
- Check the edge's acknowledged status; it remains false.
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.
In Solidity, 'memory' means a temporary copy of data that disappears after the function ends, while 'storage' means the permanent data on the blockchain. The code copied the edge data into memory, changed the copy, but never saved it back, so the change was lost. This meant the acknowledge feature never actually worked.
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.