Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tom Sepez | 8726d30e | 2025-01-29 02:11:08 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/390223051): Remove C-library calls to fix the errors. |
| 7 | #pragma allow_unsafe_libc_calls |
| 8 | #endif |
| 9 | |
[email protected] | 1b556f8 | 2013-01-31 02:23:43 | [diff] [blame] | 10 | #include <fcntl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include <stddef.h> |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
[email protected] | 1b556f8 | 2013-01-31 02:23:43 | [diff] [blame] | 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 17 | |
| 18 | #include <algorithm> |
| 19 | #include <limits> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 20 | #include <memory> |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 21 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 22 | #include "base/files/file_util.h" |
dcheng | db5935f | 2016-03-26 00:16:27 | [diff] [blame] | 23 | #include "base/memory/free_deleter.h" |
Peter Collingbourne | 5a35305d | 2019-02-06 02:51:43 | [diff] [blame] | 24 | #include "base/sanitizer_buildflags.h" |
[email protected] | 547683f | 2013-02-04 23:39:48 | [diff] [blame] | 25 | #include "build/build_config.h" |
Arthur Sonzogni | fd39d61 | 2024-06-26 08:16:23 | [diff] [blame] | 26 | #include "partition_alloc/buildflags.h" |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
| 28 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 29 | #if BUILDFLAG(IS_POSIX) |
[email protected] | 547683f | 2013-02-04 23:39:48 | [diff] [blame] | 30 | #include <sys/mman.h> |
| 31 | #include <unistd.h> |
| 32 | #endif |
| 33 | |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 34 | using std::nothrow; |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 35 | using std::numeric_limits; |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 36 | |
| 37 | namespace { |
| 38 | |
[email protected] | fe394f3 | 2013-02-06 03:23:49 | [diff] [blame] | 39 | // This function acts as a compiler optimization barrier. We use it to |
| 40 | // prevent the compiler from making an expression a compile-time constant. |
| 41 | // We also use it so that the compiler doesn't discard certain return values |
| 42 | // as something we don't need (see the comment with calloc below). |
| 43 | template <typename Type> |
Peter Kasting | 0acb7f5ef | 2022-05-05 23:57:27 | [diff] [blame] | 44 | NOINLINE Type HideValueFromCompiler(Type value) { |
[email protected] | 1cdfdb7 | 2013-04-04 12:02:35 | [diff] [blame] | 45 | #if defined(__GNUC__) |
| 46 | // In a GCC compatible compiler (GCC or Clang), make this compiler barrier |
Peter Kasting | 0acb7f5ef | 2022-05-05 23:57:27 | [diff] [blame] | 47 | // more robust. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 48 | __asm__ volatile("" : "+r"(value)); |
[email protected] | 1cdfdb7 | 2013-04-04 12:02:35 | [diff] [blame] | 49 | #endif // __GNUC__ |
[email protected] | fe394f3 | 2013-02-06 03:23:49 | [diff] [blame] | 50 | return value; |
| 51 | } |
| 52 | |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 53 | // There are platforms where these tests are known to fail. We would like to |
| 54 | // be able to easily check the status on the bots, but marking tests as |
| 55 | // FAILS_ is too clunky. |
| 56 | void OverflowTestsSoftExpectTrue(bool overflow_detected) { |
| 57 | if (!overflow_detected) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 58 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \ |
| 59 | BUILDFLAG(IS_APPLE) |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 60 | // Sadly, on Linux, Android, and OSX we don't have a good story yet. Don't |
| 61 | // fail the test, but report. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 62 | printf("Platform has overflow: %s\n", !overflow_detected ? "yes." : "no."); |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 63 | #else |
| 64 | // Otherwise, fail the test. (Note: EXPECT are ok in subfunctions, ASSERT |
| 65 | // aren't). |
| 66 | EXPECT_TRUE(overflow_detected); |
| 67 | #endif |
| 68 | } |
| 69 | } |
| 70 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 71 | #if BUILDFLAG(IS_APPLE) || defined(ADDRESS_SANITIZER) || \ |
Sergey Ulanov | ef5b763 | 2021-09-30 14:59:08 | [diff] [blame] | 72 | defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 73 | BUILDFLAG(IS_HWASAN) || PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
John Abd-El-Malek | 17727ff | 2014-10-02 22:55:15 | [diff] [blame] | 74 | #define MAYBE_NewOverflow DISABLED_NewOverflow |
| 75 | #else |
| 76 | #define MAYBE_NewOverflow NewOverflow |
| 77 | #endif |
Yuki Shiino | cdbedb5 | 2020-08-25 09:23:03 | [diff] [blame] | 78 | // Test that array[TooBig][X] and array[X][TooBig] allocations fail and not |
| 79 | // succeed with the wrong size allocation in case of size_t overflow. This |
| 80 | // test is disabled on environments that operator new (nothrow) crashes in |
| 81 | // case of size_t overflow. |
| 82 | // |
| 83 | // - iOS doesn't honor nothrow. |
| 84 | // - XSan aborts when operator new returns nullptr. |
| 85 | // - PartitionAlloc crashes by design when size_t overflows. |
| 86 | // |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 87 | // TODO(crbug.com/40611888): Fix the test on Mac. |
John Abd-El-Malek | 17727ff | 2014-10-02 22:55:15 | [diff] [blame] | 88 | TEST(SecurityTest, MAYBE_NewOverflow) { |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 89 | const size_t kArraySize = 4096; |
| 90 | // We want something "dynamic" here, so that the compiler doesn't |
| 91 | // immediately reject crazy arrays. |
Avi Drissman | dea3205 | 2022-01-13 21:31:18 | [diff] [blame] | 92 | [[maybe_unused]] const size_t kDynamicArraySize = |
| 93 | HideValueFromCompiler(kArraySize); |
thakis | 4d7b56b | 2017-02-14 16:21:35 | [diff] [blame] | 94 | const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 95 | const size_t kArraySize2 = kMaxSizeT / kArraySize + 10; |
| 96 | const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2); |
| 97 | { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 98 | std::unique_ptr<char[][kArraySize]> array_pointer( |
| 99 | new (nothrow) char[kDynamicArraySize2][kArraySize]); |
thakis | 4d7b56b | 2017-02-14 16:21:35 | [diff] [blame] | 100 | // Prevent clang from optimizing away the whole test. |
| 101 | char* volatile p = reinterpret_cast<char*>(array_pointer.get()); |
| 102 | OverflowTestsSoftExpectTrue(!p); |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 103 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 104 | #if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS) |
Avi Drissman | dea3205 | 2022-01-13 21:31:18 | [diff] [blame] | 105 | // On Windows, the compiler prevents static array sizes of more than |
| 106 | // 0x7fffffff (error C2148). |
Peter Kasting | be940e9 | 2014-11-20 23:14:08 | [diff] [blame] | 107 | #else |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 108 | { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 109 | std::unique_ptr<char[][kArraySize2]> array_pointer( |
| 110 | new (nothrow) char[kDynamicArraySize][kArraySize2]); |
thakis | 4d7b56b | 2017-02-14 16:21:35 | [diff] [blame] | 111 | // Prevent clang from optimizing away the whole test. |
| 112 | char* volatile p = reinterpret_cast<char*>(array_pointer.get()); |
| 113 | OverflowTestsSoftExpectTrue(!p); |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 114 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 115 | #endif // BUILDFLAG(IS_WIN) && defined(ARCH_CPU_64_BITS) |
[email protected] | 9c4729b | 2013-01-26 04:41:15 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | b5bf9a13 | 2013-01-15 20:16:33 | [diff] [blame] | 118 | } // namespace |