Skip to content

Commit

Permalink
Fix PoolPair value overflow undefined behavior bug (#2077)
Browse files Browse the repository at this point in the history
* Fixes to check for max PoolPair value to prevent value overflow

* Revert validation and legacy const expr

* Fixes to poolprice max and compatibility with legacy max poolprice

* Use setMaxPoolPrice in customtx rpc

* Fix max poolpair value check

* Refactor for more clarity

* Fix new unit test value

* Refactor items

* Switch to getMaxValid, cleanup headers

* Simplfy actions checkout

* Remove unused code after re-abstractions

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
sieniven and prasannavl authored Jul 4, 2023
1 parent f272e5e commit 3075740
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/tests-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ jobs:
runs-on: [self-hosted, linux, x64, builder]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Populate environment
run: ./make.sh ci-export-vars
Expand Down
6 changes: 3 additions & 3 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4615,7 +4615,7 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView &view, std::vector<DCT_ID> poolIDs, con
}

// Single swap if no pool IDs provided
auto poolPrice = POOLPRICE_MAX;
auto poolPrice = PoolPrice::getMaxValid();
std::optional<std::pair<DCT_ID, CPoolPair> > poolPair;
if (poolIDs.empty()) {
poolPair = view.GetPoolPair(obj.idTokenFrom, obj.idTokenTo);
Expand Down Expand Up @@ -4788,7 +4788,7 @@ Res CPoolSwap::ExecuteSwap(CCustomCSView &view, std::vector<DCT_ID> poolIDs, con
}

// Reject if price paid post-swap above max price provided
if (height >= static_cast<uint32_t>(consensus.FortCanningHeight) && obj.maxPrice != POOLPRICE_MAX) {
if (height >= static_cast<uint32_t>(consensus.FortCanningHeight) && !obj.maxPrice.isAboveValid()) {
if (swapAmountResult.nValue != 0) {
const auto userMaxPrice = arith_uint256(obj.maxPrice.integer) * COIN + obj.maxPrice.fraction;
if (arith_uint256(obj.amountFrom) * COIN / swapAmountResult.nValue > userMaxPrice) {
Expand Down Expand Up @@ -4822,7 +4822,7 @@ Res SwapToDFIorDUSD(CCustomCSView &mnview,
obj.idTokenFrom = tokenId;
obj.idTokenTo = DCT_ID{0};
obj.amountFrom = amount;
obj.maxPrice = POOLPRICE_MAX;
obj.maxPrice = PoolPrice::getMaxValid();

auto poolSwap = CPoolSwap(obj, height);
auto token = mnview.GetToken(tokenId);
Expand Down
12 changes: 9 additions & 3 deletions src/masternodes/poolpairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ struct PoolPrice {
}

bool operator!=(const PoolPrice &rhs) const { return integer != rhs.integer || fraction != rhs.fraction; }
};

static constexpr auto POOLPRICE_MAX =
PoolPrice{std::numeric_limits<CAmount>::max(), std::numeric_limits<CAmount>::max()};
static constexpr PoolPrice getMaxValid() {
return { MAX_MONEY / COIN, MAX_MONEY % COIN };
}

bool isAboveValid() const {
const auto maxPrice = PoolPrice::getMaxValid();
return ((integer > maxPrice.integer) || (integer == maxPrice.integer && fraction >= maxPrice.fraction));
}
};

struct CPoolSwapMessage {
CScript from, to;
Expand Down
13 changes: 7 additions & 6 deletions src/masternodes/rpc_customtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,14 @@ class CCustomTxRpcVisitor {
rpcInfo.pushKV("fromAmount", ValueFromAmount(obj.amountFrom));
rpcInfo.pushKV("toAddress", ScriptToString(obj.to));
rpcInfo.pushKV("toToken", obj.idTokenTo.ToString());

CAmount userMaxPrice = std::numeric_limits<CAmount>::max();
if ((arith_uint256(obj.maxPrice.integer) * COIN + obj.maxPrice.fraction) < userMaxPrice) {
userMaxPrice = (obj.maxPrice.integer * COIN) + obj.maxPrice.fraction;

if (obj.maxPrice.isAboveValid()) {
auto price = PoolPrice::getMaxValid();
rpcInfo.pushKV("maxPrice", ValueFromAmount((price.integer * COIN) + price.fraction));
}
else {
rpcInfo.pushKV("maxPrice", ValueFromAmount((obj.maxPrice.integer * COIN) + obj.maxPrice.fraction));
}

rpcInfo.pushKV("maxPrice", ValueFromAmount(userMaxPrice));
}

void operator()(const CPoolSwapMessageV2 &obj) const {
Expand Down
12 changes: 5 additions & 7 deletions src/masternodes/rpc_poolpair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,13 @@ void CheckAndFillPoolSwapMessage(const JSONRPCRequest &request, CPoolSwapMessage
if (!token2)
throw JSONRPCError(RPC_INVALID_PARAMETER, "TokenTo was not found");

CAmount maxPrice = std::numeric_limits<CAmount>::max();
if (!metadataObj["maxPrice"].isNull()) {
CAmount maxPrice = AmountFromValue(metadataObj["maxPrice"]);
poolSwapMsg.maxPrice.integer = maxPrice / COIN;
poolSwapMsg.maxPrice.fraction = maxPrice % COIN;
} else {
// There is no maxPrice calculation anymore
poolSwapMsg.maxPrice.integer = std::numeric_limits<CAmount>::max();
poolSwapMsg.maxPrice.fraction = std::numeric_limits<CAmount>::max();
maxPrice = AmountFromValue(metadataObj["maxPrice"]);
}

poolSwapMsg.maxPrice.integer = maxPrice / COIN;
poolSwapMsg.maxPrice.fraction = maxPrice % COIN;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(rpc_rawparams)
BOOST_CHECK_EQUAL(find_value(resultsObj, "fromAmount").get_real(), 2.0);
BOOST_CHECK_EQUAL(find_value(resultsObj, "toAddress").get_str(), "dKTimnyUteWgQCGaFe2m49MzYuU7sSVv2N");
BOOST_CHECK_EQUAL(find_value(resultsObj, "toToken").get_str(), "1");
BOOST_CHECK_EQUAL(find_value(resultsObj, "maxPrice").get_real(), 3973919657.0);
BOOST_CHECK_EQUAL(find_value(resultsObj, "maxPrice").get_real(), 92233720368.54776);

BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decodecustomtx ")+txHexString + " true"));
BOOST_CHECK_THROW(CallRPC(std::string("decodecustomtx ")+txHexString+" extra"), std::runtime_error);
Expand Down

0 comments on commit 3075740

Please sign in to comment.