-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix upAp7 signed integer overflow (#735)
* Fix possible signed integer overflow in ijToIjk * Update changelog * reorder * add _upAp7Checked and _upAp7rChecked for processing user input * Add PR number to changelog * update with macro tests * force k to 0 in fuzzer * Change per review: add an overall bounds check around the overflow checks
- Loading branch information
1 parent
975de7a
commit fc7f3dd
Showing
12 changed files
with
405 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2022 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** @file | ||
* @brief Fuzzer program for internal functions in coordijk.c | ||
*/ | ||
|
||
#include "aflHarness.h" | ||
#include "algos.h" | ||
#include "h3api.h" | ||
#include "utility.h" | ||
|
||
typedef struct { | ||
CoordIJK ijk; | ||
} inputArgs; | ||
|
||
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | ||
if (size < sizeof(inputArgs)) { | ||
return 0; | ||
} | ||
const inputArgs *args = (const inputArgs *)data; | ||
|
||
// These functions require that input be non-negative | ||
if (args->ijk.i >= 0 && args->ijk.j >= 0 && args->ijk.k >= 0) { | ||
CoordIJK ijkCopy1 = args->ijk; | ||
_upAp7Checked(&ijkCopy1); | ||
CoordIJK ijkCopy2 = args->ijk; | ||
_upAp7rChecked(&ijkCopy2); | ||
} | ||
// This function needs a guard check to be safe and that guard | ||
// check assumes k = 0. | ||
CoordIJK ijkCopy3 = args->ijk; | ||
ijkCopy3.k = 0; | ||
if (!_ijkNormalizeCouldOverflow(&ijkCopy3)) { | ||
_ijkNormalize(&ijkCopy3); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
AFL_HARNESS_MAIN(sizeof(inputArgs)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2022 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** @file | ||
* @brief tests functions and macros in mathExtensions.h | ||
* | ||
* usage: `testMathExtensions` | ||
*/ | ||
|
||
#include "mathExtensions.h" | ||
#include "test.h" | ||
#include "utility.h" | ||
|
||
#define ASSERT_SUB_OVERFLOW(a, b) \ | ||
t_assert(SUB_INT32S_OVERFLOWS((a), (b)), #a " - " #b " Overflows") | ||
#define ASSERT_SUB_DOES_NOT_OVERFLOW(a, b) \ | ||
t_assert(!SUB_INT32S_OVERFLOWS((a), (b)), #a " - " #b \ | ||
" Does Not " \ | ||
"Overflow") | ||
#define ASSERT_ADD_OVERFLOW(a, b) \ | ||
t_assert(ADD_INT32S_OVERFLOWS((a), (b)), #a " + " #b " Overflows") | ||
#define ASSERT_ADD_DOES_NOT_OVERFLOW(a, b) \ | ||
t_assert(!ADD_INT32S_OVERFLOWS((a), (b)), #a " + " #b \ | ||
" Does Not " \ | ||
"Overflow") | ||
|
||
SUITE(mathExtensions) { | ||
TEST(_ipow) { | ||
t_assert(_ipow(7, 0) == 1, "7 ** 0 == 1"); | ||
t_assert(_ipow(7, 1) == 7, "7 ** 1 == 7"); | ||
t_assert(_ipow(7, 2) == 49, "7 ** 2 == 49"); | ||
t_assert(_ipow(1, 20) == 1, "1 ** 20 == 1"); | ||
t_assert(_ipow(2, 5) == 32, "2 ** 5 == 32"); | ||
} | ||
|
||
TEST(subOverflows) { | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(0, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN, 0); | ||
ASSERT_SUB_OVERFLOW(INT32_MIN, 1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN, -1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, 1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, -1); | ||
ASSERT_SUB_OVERFLOW(INT32_MIN + 1, 2); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN + 1, -2); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(100, 10); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX, 1); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX, -1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX - 1, 1); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX - 1, -1); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX - 1, -2); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX - 1, -2); | ||
ASSERT_SUB_OVERFLOW(INT32_MIN, INT32_MAX); | ||
ASSERT_SUB_OVERFLOW(INT32_MAX, INT32_MIN); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MIN, INT32_MIN); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(INT32_MAX, INT32_MAX); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, 0); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, 10); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, -10); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, INT32_MAX); | ||
ASSERT_SUB_OVERFLOW(-2, INT32_MAX); | ||
ASSERT_SUB_DOES_NOT_OVERFLOW(-1, INT32_MIN); | ||
ASSERT_SUB_OVERFLOW(0, INT32_MIN); | ||
} | ||
|
||
TEST(addOverflows) { | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(0, 0); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN, 0); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN, 1); | ||
ASSERT_ADD_OVERFLOW(INT32_MIN, -1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN + 1, 0); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN + 1, 1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN + 1, -1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN + 1, 2); | ||
ASSERT_ADD_OVERFLOW(INT32_MIN + 1, -2); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(100, 10); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MAX, 0); | ||
ASSERT_ADD_OVERFLOW(INT32_MAX, 1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MAX, -1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MAX - 1, 1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MAX - 1, -1); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MAX - 1, -2); | ||
ASSERT_ADD_OVERFLOW(INT32_MAX - 1, 2); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MIN, INT32_MAX); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(INT32_MAX, INT32_MIN); | ||
ASSERT_ADD_OVERFLOW(INT32_MAX, INT32_MAX); | ||
ASSERT_ADD_OVERFLOW(INT32_MIN, INT32_MIN); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(-1, 0); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(-1, 10); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(-1, -10); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(-1, INT32_MAX); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(-2, INT32_MAX); | ||
ASSERT_ADD_OVERFLOW(-1, INT32_MIN); | ||
ASSERT_ADD_DOES_NOT_OVERFLOW(0, INT32_MIN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.