Closed sherlock-admin2 closed 10 months ago
1 comment(s) were left on this issue during the judging contest.
auditsea commented:
The issue describes about TWAP can be manipulated because
update
function can be called anytime and by anyone, thus TWAP period can be as short as 1 block. It seems like a valid issue but after caeful consideration, it's noticed that the TWAP issue does not come from its period but the logic itself is incorrect, thus marking this as Invalid
1 comment(s) were left on this issue during the judging contest.
auditsea commented:
The issue describes about TWAP can be manipulated because
update
function can be called anytime and by anyone, thus TWAP period can be as short as 1 block. It seems like a valid issue but after caeful consideration, it's noticed that the TWAP issue does not come from its period but the logic itself is incorrect, thus marking this as Invalid
evmboi32
high
TWAP oracle can be easily manipulated
Summary
TWAP can be manipulated due to the short sample time.
Vulnerability Detail
The average price in the
LibTWAPOracle.sol
is sampled in every block (if any trades happen) which is incorrect and can let the attacker to manipulate the price.The price will be averaged out if
blockTimestamp - ts.pricesBlockTimestampLast > 0
which means if the attacker can manipulate the price of the tokens in the blockn
to 10x of that in blockn-1
or greater the TWAP oracle will return the incorrect inflated price.Coded POC
Add this test to a
./packages/contracts/test/diamond/facets/TWAPManipulation.t.sol
And run with
forge test --match-path ./test/diamond/facets/TWAPManipulation.t.sol -vvv --fork-url RPC_URL
Impact
TWAP oracle can be easily manipulated by an attacker due to the short sample time. This can cause a number of problems as the attacks can manipulate the price as he wants. He can mint or redeem dollars freely as he can bring the contract into that state. Liquidations can happen if they rely on the TWAP oracle.
Code Snippet
https://github.com/sherlock-audit/2023-12-ubiquity/blob/main/ubiquity-dollar/packages/contracts/src/dollar/libraries/LibTWAPOracle.sol#L74
Tool used
Manual Review
Recommendation
Increase the TWAP sample time to a larger value instead of 1 block. Increase it to 1 hour or more. The functions relying on the TWAP price should be disabled during the first period as the TWAP has no data yet.
Duplicate of #20