boundedSub() might fail to return the result that is bounded to prevent overflows
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/16
Found by
chaduke
Summary
The goal of boundedSub() is to bound the result regardless what the inputs are to prevent overflows/underflows. However, the goal is not achieved for some cases. As a result, boundedSub() still might underflow and still might revert. The goal of the function is not achieved.
As a result, the protocol might not be fault-tolerant as it is supposed to be - when boundedSub() is designed to not revert in any case, it still might revert. For example, function MarketUtils.getNextFundingAmountPerSize() will be affected.
Vulnerability Detail
boundedSub() is designed to always bound its result between type(int256).min and type(int256).max so that it will never overflow/underflow:
It achieves its goal in three cases:
-
Case 1:
if either a or b is zero or the signs are the same there should not be any overflow. -
Case 2:
a > 0, andb < 0, anda-b > type(int256).max, then we need to returntype(int256).max. -
Case 3:
a < 0, andb > 0, anda - b < type(int256).min, then we need to return
type(int256).min
Unfortunately, the third case is implemented wrongly as follows:
// if subtracting `b` from `a` would result in a value less than the min int256 value
// then return the min int256 value
if (a < 0 && b <= type(int256).min - a) {
return type(int256).min;
}
which essentially is checking a < 0 && b + a <= type(int256).min, a wrong condition to check. Because of using this wrong condition, underflow cases will not be detected and the function will revert instead of returning type(int256).min in this case.
To verify, suppose a = type(int256).min and b = 1, a-b needs to be bounded to prevent underflow and the function should have returned type(int256).min. However, the function will fail the condition, as a result, it will not execute the if part, and the following final line will be executed instead:
return a - b;
As a result, instead of returning the minimum, the function will revert in the last line due to underflow. This violates the property of the function: it should have returned the bounded result type(int256).min and should not have reverted in any case.
The following POC in Remix can show that the following function will revert:
function testBoundedSub() public pure returns (int256){
return boundedSub(type(int256).min+3, 4);
}
Impact
boundedSub() does not guarantee underflow/overflow free as it is designed to be. As a result, the protocol might break at points when it is not supposed to break.
For example, function MarketUtils.getNextFundingAmountPerSize() will be affected.
Code Snippet
Tool used
VsCode
Manual Review
Recommendation
The correction is as follows:
function boundedSub(int256 a, int256 b) internal pure returns (int256) {
// if either a or b is zero or the signs are the same there should not be any overflow
if (a == 0 || b == 0 || (a > 0 && b > 0) || (a < 0 && b < 0)) {
return a - b;
}
// if adding `-b` to `a` would result in a value greater than the max int256 value
// then return the max int256 value
if (a > 0 && -b >= type(int256).max - a) {
return type(int256).max;
}
// if subtracting `b` from `a` would result in a value less than the min int256 value
// then return the min int256 value
- if (a < 0 && b <= type(int256).min - a) {
+ if (a < 0 && a <= type(int256).min + b) {
return type(int256).min;
}
return a - b;
}
Discussion
xvi10
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in GMX Update. Sherlock made it public on 4 April 2023. Found in the 6 audit contest (finding M-38).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.