interfaceFee Incorrectly converted uint40 when stored
The interface fee amount is checked against a uint48 maximum but stored as uint40, so values above about 1.1M are truncated, potentially to zero. An attacker or user could set a large interface fee that is saved incorrectly, leading to a smaller or zero fee being charged when the order executes.
Source: https://github.com/sherlock-audit/2023-10-perennial-judging/issues/43
Found by
bin2chen
Summary
The interfaceFee.amount is currently defined as uint48 , with a maximum value of approximately 281m.
However, it is incorrectly converted to uint40 when saved, uint40(UFixed6.unwrap(newValue.interfaceFee.amount)), which means the maximum value can only be approximately 1.1M.
If a user sets an order where interfaceFee.amount is greater than 1.1M, the order can be saved successfully
but the actual stored value may be truncated to 0.
This is not what the user expects, and the user may think that the order has been set, but in reality, it is an incorrect order. Although a fee of 1.1M is large, it is not impossible.
Vulnerability Detail
interfaceFee.amount is defined as uint48
the legality check also uses type(uint48).max, but uint40 is used when saving.
struct StoredTriggerOrder {
/* slot 0 */
uint8 side; // 0 = maker, 1 = long, 2 = short, 3 = collateral
int8 comparison; // -2 = lt, -1 = lte, 0 = eq, 1 = gte, 2 = gt
uint64 fee; // <= 18.44tb
int64 price; // <= 9.22t
int64 delta; // <= 9.22t
@> uint48 interfaceFeeAmount; // <= 281m
/* slot 1 */
address interfaceFeeReceiver;
bool interfaceFeeUnwrap;
bytes11 __unallocated0__;
}
library TriggerOrderLib {
function store(TriggerOrderStorage storage self, TriggerOrder memory newValue) internal {
if (newValue.side > type(uint8).max) revert TriggerOrderStorageInvalidError();
if (newValue.comparison > type(int8).max) revert TriggerOrderStorageInvalidError();
if (newValue.comparison < type(int8).min) revert TriggerOrderStorageInvalidError();
if (newValue.fee.gt(UFixed6.wrap(type(uint64).max))) revert TriggerOrderStorageInvalidError();
if (newValue.price.gt(Fixed6.wrap(type(int64).max))) revert TriggerOrderStorageInvalidError();
if (newValue.price.lt(Fixed6.wrap(type(int64).min))) revert TriggerOrderStorageInvalidError();
if (newValue.delta.gt(Fixed6.wrap(type(int64).max))) revert TriggerOrderStorageInvalidError();
if (newValue.delta.lt(Fixed6.wrap(type(int64).min))) revert TriggerOrderStorageInvalidError();
@> if (newValue.interfaceFee.amount.gt(UFixed6.wrap(type(uint48).max))) revert TriggerOrderStorageInvalidError();
self.value = StoredTriggerOrder(
uint8(newValue.side),
int8(newValue.comparison),
uint64(UFixed6.unwrap(newValue.fee)),
int64(Fixed6.unwrap(newValue.price)),
int64(Fixed6.unwrap(newValue.delta)),
@> uint40(UFixed6.unwrap(newValue.interfaceFee.amount)),
newValue.interfaceFee.receiver,
newValue.interfaceFee.unwrap,
bytes11(0)
);
}
We can see that when saving, it is forcibly converted to uint40, as in uint40(UFixed6.unwrap(newValue.interfaceFee.amount)). The order can be saved successfully, but the actual storage may be truncated to 0.
Impact
For orders where interfaceFee.amount is greater than 1.1M, the order can be saved successfully, but the actual storage may be truncated to 0.
This is not what users expect and may lead to incorrect fee payments when the order is executed.
Although a fee of 1.1M is large, it is not impossible.
Code Snippet
Tool used
Manual Review
Recommendation
library TriggerOrderLib {
function store(TriggerOrderStorage storage self, TriggerOrder memory newValue) internal {
if (newValue.side > type(uint8).max) revert TriggerOrderStorageInvalidError();
if (newValue.comparison > type(int8).max) revert TriggerOrderStorageInvalidError();
if (newValue.comparison < type(int8).min) revert TriggerOrderStorageInvalidError();
if (newValue.fee.gt(UFixed6.wrap(type(uint64).max))) revert TriggerOrderStorageInvalidError();
if (newValue.price.gt(Fixed6.wrap(type(int64).max))) revert TriggerOrderStorageInvalidError();
if (newValue.price.lt(Fixed6.wrap(type(int64).min))) revert TriggerOrderStorageInvalidError();
if (newValue.delta.gt(Fixed6.wrap(type(int64).max))) revert TriggerOrderStorageInvalidError();
if (newValue.delta.lt(Fixed6.wrap(type(int64).min))) revert TriggerOrderStorageInvalidError();
if (newValue.interfaceFee.amount.gt(UFixed6.wrap(type(uint48).max))) revert TriggerOrderStorageInvalidError();
self.value = StoredTriggerOrder(
uint8(newValue.side),
int8(newValue.comparison),
uint64(UFixed6.unwrap(newValue.fee)),
int64(Fixed6.unwrap(newValue.price)),
int64(Fixed6.unwrap(newValue.delta)),
- uint40(UFixed6.unwrap(newValue.interfaceFee.amount)),
+ uint48(UFixed6.unwrap(newValue.interfaceFee.amount)),
newValue.interfaceFee.receiver,
newValue.interfaceFee.unwrap,
bytes11(0)
);
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
panprog commented:
medium, because there seems to really be an incorrect cast to uint40 instead of uint48, so the fee might be stored incorrectly and incorrect (smaller) fee will be charged, losing funds for the interface
panprog
Mitigation Review:
Fixed.
MLON33
Fix: https://github.com/equilibria-xyz/perennial-v2/pull/175
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 code validates the interface fee against the uint48 maximum but casts it to uint40 when storing, causing truncation for values above the uint40 maximum.
Impact
Orders with an interface fee above about 1.1M can be saved but the stored fee may be truncated to zero, so the interface receives a smaller or zero fee when the order executes.
What the attacker needed
User sets interfaceFee.amount above ~1.1M
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 smart contract stores a fee value in a smaller number type than it checks against. This means a very large fee can be silently cut down, possibly to zero, so the fee receiver gets less than expected. The fix is to store the value in the same size type used for the check.