| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 18 | |
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <malloc.h> |
| Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 21 | #include <poll.h> |
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 22 | #include <signal.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 28 | #include <time.h> |
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 29 | |
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 30 | #include <android-base/silent_death_test.h> |
| 31 | |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 32 | #include "DoNotOptimize.h" |
| 33 | |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 34 | #if defined(__BIONIC__) |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 35 | #define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "FORTIFY") |
| 36 | #else |
| 37 | #define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "") |
| 38 | #endif |
| 39 | |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 40 | #if __has_feature(hwaddress_sanitizer) |
| 41 | #define ASSERT_FORTIFY_OR_HWASAN(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "HWAddressSanitizer") |
| 42 | #else |
| 43 | #define ASSERT_FORTIFY_OR_HWASAN ASSERT_FORTIFY |
| 44 | #endif |
| 45 | |
| Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 46 | // Fortify test code needs to run multiple times, so TEST_NAME macro is used to |
| 47 | // distinguish different tests. TEST_NAME is defined in compilation command. |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 48 | #define DEATHTEST_PASTER(name) name##_DeathTest |
| 49 | #define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name) |
| 50 | #define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME) |
| 51 | |
| Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 52 | using DEATHTEST = SilentDeathTest; |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 53 | |
| Elliott Hughes | 6fc9aa7 | 2025-03-14 12:40:40 -0700 | [diff] [blame] | 54 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE >= 2 |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 55 | struct foo { |
| 56 | char empty[0]; |
| 57 | char one[1]; |
| 58 | char a[10]; |
| 59 | char b[10]; |
| 60 | }; |
| 61 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 62 | TEST_F(DEATHTEST, stpncpy_fortified2) { |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 63 | foo myfoo; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 64 | volatile int copy_amt = 11; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 65 | ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt)); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 66 | } |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 67 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 68 | TEST_F(DEATHTEST, stpncpy2_fortified2) { |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 69 | foo myfoo = {}; |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 70 | myfoo.one[0] = 'A'; // not null terminated string |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 71 | ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b))); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 72 | } |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 73 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 74 | TEST_F(DEATHTEST, strncpy_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 75 | foo myfoo; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 76 | volatile int copy_amt = 11; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 77 | ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 78 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 79 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 80 | TEST_F(DEATHTEST, strncpy2_fortified2) { |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 81 | foo myfoo = {}; |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 82 | myfoo.one[0] = 'A'; // not null terminated string |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 83 | ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b))); |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 84 | } |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 85 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 86 | TEST_F(DEATHTEST, sprintf_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 87 | foo myfoo; |
| 88 | char source_buf[15]; |
| 89 | memcpy(source_buf, "12345678901234", 15); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 90 | ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 91 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 92 | |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 93 | TEST_F(DEATHTEST, sprintf2_fortified2) { |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 94 | foo myfoo; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 95 | ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789")); |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 96 | } |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 97 | |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 98 | static int vsprintf_helper2(const char* fmt, ...) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 99 | va_list va; |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 100 | va_start(va, fmt); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 101 | foo myfoo; |
| 102 | int result = vsprintf(myfoo.a, fmt, va); // should crash here |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 103 | va_end(va); |
| 104 | return result; |
| 105 | } |
| 106 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 107 | TEST_F(DEATHTEST, vsprintf_fortified2) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 108 | ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 111 | TEST_F(DEATHTEST, vsprintf2_fortified2) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 112 | ASSERT_FORTIFY(vsprintf_helper2("0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 113 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 114 | |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 115 | static int vsnprintf_helper2(const char* fmt, ...) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 116 | va_list va; |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 117 | va_start(va, fmt); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 118 | foo myfoo; |
| 119 | volatile size_t size = 11; |
| 120 | int result = vsnprintf(myfoo.a, size, fmt, va); // should crash here |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 121 | va_end(va); |
| 122 | return result; |
| 123 | } |
| 124 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 125 | TEST_F(DEATHTEST, vsnprintf_fortified2) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 126 | ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 129 | TEST_F(DEATHTEST, vsnprintf2_fortified2) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 130 | ASSERT_FORTIFY(vsnprintf_helper2("0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 131 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 132 | |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 133 | // zero sized target with "\0" source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 134 | TEST_F(DEATHTEST, stpcpy_fortified2) { |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 135 | #if defined(__BIONIC__) |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 136 | foo myfoo; |
| 137 | char* src = strdup(""); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 138 | ASSERT_FORTIFY(stpcpy(myfoo.empty, src)); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 139 | free(src); |
| 140 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 141 | GTEST_SKIP() << "stpcpy not available"; |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 142 | #endif // __BIONIC__ |
| 143 | } |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 144 | |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 145 | // zero sized target with "\0" source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 146 | TEST_F(DEATHTEST, strcpy_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 147 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 148 | foo myfoo; |
| 149 | char* src = strdup(""); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 150 | ASSERT_FORTIFY(strcpy(myfoo.empty, src)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 151 | free(src); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 152 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 153 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 154 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 155 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 156 | |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 157 | // zero sized target with longer source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 158 | TEST_F(DEATHTEST, strcpy2_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 159 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 160 | foo myfoo; |
| 161 | char* src = strdup("1"); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 162 | ASSERT_FORTIFY(strcpy(myfoo.empty, src)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 163 | free(src); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 164 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 165 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 166 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 167 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 168 | |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 169 | // one byte target with longer source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 170 | TEST_F(DEATHTEST, strcpy3_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 171 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 172 | foo myfoo; |
| 173 | char* src = strdup("12"); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 174 | ASSERT_FORTIFY(strcpy(myfoo.one, src)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 175 | free(src); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 176 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 177 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 178 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 179 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 180 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 181 | TEST_F(DEATHTEST, strchr_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 182 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 183 | foo myfoo; |
| 184 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 185 | myfoo.b[0] = '\0'; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 186 | ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a'))); |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 187 | ASSERT_FORTIFY(printf("%s", strchr(static_cast<const char*>(myfoo.a), 'a'))); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 188 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 189 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 190 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 191 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 192 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 193 | TEST_F(DEATHTEST, strrchr_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 194 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 195 | foo myfoo; |
| 196 | memcpy(myfoo.a, "0123456789", 10); |
| 197 | memcpy(myfoo.b, "01234", 6); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 198 | ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a'))); |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 199 | ASSERT_FORTIFY(printf("%s", strrchr(static_cast<const char*>(myfoo.a), 'a'))); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 200 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 201 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 202 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 203 | } |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 204 | |
| 205 | TEST_F(DEATHTEST, memchr_fortified2) { |
| 206 | #if defined(__BIONIC__) |
| 207 | foo myfoo; |
| 208 | volatile int asize = sizeof(myfoo.a) + 1; |
| 209 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| Stephen Hines | 62165a1 | 2020-08-18 01:38:14 -0700 | [diff] [blame] | 210 | ASSERT_FORTIFY(printf("%s", static_cast<const char*>(memchr(myfoo.a, 'a', asize)))); |
| 211 | ASSERT_FORTIFY(printf( |
| 212 | "%s", static_cast<const char*>(memchr(static_cast<const void*>(myfoo.a), 'a', asize)))); |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 213 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 214 | GTEST_SKIP() << "glibc is broken"; |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 215 | #endif // __BIONIC__ |
| 216 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 217 | |
| Elliott Hughes | 55a8cc2 | 2017-11-08 21:22:44 -0800 | [diff] [blame] | 218 | TEST_F(DEATHTEST, memrchr_fortified2) { |
| 219 | #if defined(__BIONIC__) |
| 220 | foo myfoo; |
| 221 | volatile int asize = sizeof(myfoo.a) + 1; |
| 222 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| Stephen Hines | 62165a1 | 2020-08-18 01:38:14 -0700 | [diff] [blame] | 223 | ASSERT_FORTIFY(printf("%s", static_cast<const char*>(memrchr(myfoo.a, 'a', asize)))); |
| 224 | ASSERT_FORTIFY(printf( |
| 225 | "%s", static_cast<const char*>(memrchr(static_cast<const void*>(myfoo.a), 'a', asize)))); |
| Elliott Hughes | 55a8cc2 | 2017-11-08 21:22:44 -0800 | [diff] [blame] | 226 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 227 | GTEST_SKIP() << "glibc is broken"; |
| Elliott Hughes | 55a8cc2 | 2017-11-08 21:22:44 -0800 | [diff] [blame] | 228 | #endif // __BIONIC__ |
| 229 | } |
| 230 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 231 | TEST_F(DEATHTEST, strlcpy_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 232 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 233 | foo myfoo; |
| 234 | strcpy(myfoo.a, "01"); |
| 235 | size_t n = strlen(myfoo.a); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 236 | ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n)); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 237 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 238 | GTEST_SKIP() << "strlcpy not available"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 239 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 240 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 241 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 242 | TEST_F(DEATHTEST, strlcat_fortified2) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 243 | #if defined(__BIONIC__) |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 244 | foo myfoo; |
| 245 | strcpy(myfoo.a, "01"); |
| 246 | myfoo.one[0] = '\0'; |
| 247 | size_t n = strlen(myfoo.a); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 248 | ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n)); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 249 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 250 | GTEST_SKIP() << "strlcat not available"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 251 | #endif // __BIONIC__ |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 252 | } |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 253 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 254 | TEST_F(DEATHTEST, strncat_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 255 | foo myfoo; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 256 | volatile size_t n = 10; |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 257 | strncpy(myfoo.a, "012345678", n); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 258 | ASSERT_FORTIFY(strncat(myfoo.a, "9", n)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 259 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 260 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 261 | TEST_F(DEATHTEST, strncat2_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 262 | foo myfoo; |
| 263 | myfoo.a[0] = '\0'; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 264 | volatile size_t n = 10; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 265 | ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 266 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 267 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 268 | TEST_F(DEATHTEST, strncat3_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 269 | foo myfoo; |
| 270 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 271 | myfoo.b[0] = '\0'; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 272 | volatile size_t n = 10; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 273 | ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 276 | TEST_F(DEATHTEST, strcat_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 277 | char src[11]; |
| 278 | strcpy(src, "0123456789"); |
| 279 | foo myfoo; |
| 280 | myfoo.a[0] = '\0'; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 281 | ASSERT_FORTIFY(strcat(myfoo.a, src)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 282 | } |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 283 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 284 | TEST_F(DEATHTEST, strcat2_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 285 | foo myfoo; |
| 286 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 287 | myfoo.b[0] = '\0'; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 288 | ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 291 | TEST_F(DEATHTEST, snprintf_fortified2) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 292 | foo myfoo; |
| 293 | strcpy(myfoo.a, "012345678"); |
| 294 | size_t n = strlen(myfoo.a) + 2; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 295 | ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 298 | TEST_F(DEATHTEST, bzero_fortified2) { |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 299 | foo myfoo; |
| 300 | memcpy(myfoo.b, "0123456789", sizeof(myfoo.b)); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 301 | volatile size_t n = 11; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 302 | ASSERT_FORTIFY(bzero(myfoo.b, n)); |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 305 | #endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */ |
| 306 | |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 307 | // multibyte target where we over fill (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 308 | TEST_F(DEATHTEST, strcpy_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 309 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 310 | char buf[10]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 311 | char* orig = strdup("0123456789"); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 312 | ASSERT_FORTIFY(strcpy(buf, orig)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 313 | free(orig); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 314 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 315 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 316 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // zero sized target with "\0" source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 320 | TEST_F(DEATHTEST, strcpy2_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 321 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 322 | char buf[0]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 323 | char* orig = strdup(""); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 324 | ASSERT_FORTIFY(strcpy(buf, orig)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 325 | free(orig); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 326 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 327 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 328 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // zero sized target with longer source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 332 | TEST_F(DEATHTEST, strcpy3_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 333 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 334 | char buf[0]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 335 | char* orig = strdup("1"); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 336 | ASSERT_FORTIFY(strcpy(buf, orig)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 337 | free(orig); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 338 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 339 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 340 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // one byte target with longer source (should fail) |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 344 | TEST_F(DEATHTEST, strcpy4_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 345 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 346 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 347 | char* orig = strdup("12"); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 348 | ASSERT_FORTIFY(strcpy(buf, orig)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 349 | free(orig); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 350 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 351 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 352 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 355 | TEST_F(DEATHTEST, strlen_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 356 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 357 | char buf[10]; |
| 358 | memcpy(buf, "0123456789", sizeof(buf)); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 359 | ASSERT_FORTIFY(printf("%zd", strlen(buf))); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 360 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 361 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 362 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 365 | TEST_F(DEATHTEST, strchr_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 366 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 367 | char buf[10]; |
| 368 | memcpy(buf, "0123456789", sizeof(buf)); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 369 | ASSERT_FORTIFY(printf("%s", strchr(buf, 'a'))); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 370 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 371 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 372 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 375 | TEST_F(DEATHTEST, strrchr_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 376 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 377 | char buf[10]; |
| 378 | memcpy(buf, "0123456789", sizeof(buf)); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 379 | ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a'))); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 380 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 381 | GTEST_SKIP() << "glibc is broken"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 382 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 385 | TEST_F(DEATHTEST, strlcpy_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 386 | #if defined(__BIONIC__) |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 387 | char bufa[15]; |
| 388 | char bufb[10]; |
| 389 | strcpy(bufa, "01234567890123"); |
| 390 | size_t n = strlen(bufa); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 391 | ASSERT_FORTIFY(strlcpy(bufb, bufa, n)); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 392 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 393 | GTEST_SKIP() << "strlcpy not available"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 394 | #endif // __BIONIC__ |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 397 | TEST_F(DEATHTEST, strlcat_fortified) { |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 398 | #if defined(__BIONIC__) |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 399 | char bufa[15]; |
| 400 | char bufb[10]; |
| 401 | bufb[0] = '\0'; |
| 402 | strcpy(bufa, "01234567890123"); |
| 403 | size_t n = strlen(bufa); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 404 | ASSERT_FORTIFY(strlcat(bufb, bufa, n)); |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 405 | #else // __BIONIC__ |
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 406 | GTEST_SKIP() << "strlcat not available"; |
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 407 | #endif // __BIONIC__ |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 410 | TEST_F(DEATHTEST, sprintf_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 411 | char buf[10]; |
| 412 | char source_buf[15]; |
| 413 | memcpy(source_buf, "12345678901234", 15); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 414 | ASSERT_FORTIFY(sprintf(buf, "%s", source_buf)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 417 | TEST_F(DEATHTEST, sprintf_malloc_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 418 | char* buf = static_cast<char*>(malloc(10)); |
| Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 419 | char source_buf[11]; |
| 420 | memcpy(source_buf, "1234567890", 11); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 421 | ASSERT_FORTIFY(sprintf(buf, "%s", source_buf)); |
| Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 422 | free(buf); |
| 423 | } |
| Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 424 | |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 425 | TEST_F(DEATHTEST, sprintf2_fortified) { |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 426 | // glibc's fortified implementation of sprintf is smart enough to be able to detect this bug at |
| 427 | // compile time, but we want to check if it can also be detected at runtime. |
| 428 | #pragma clang diagnostic push |
| 429 | #pragma clang diagnostic ignored "-Wformat-overflow" |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 430 | char buf[5]; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 431 | ASSERT_FORTIFY(sprintf(buf, "aaaaa")); |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 432 | #pragma clang diagnostic pop |
| Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 435 | static int vsprintf_helper(const char* fmt, ...) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 436 | va_list va; |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 437 | va_start(va, fmt); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 438 | char buf[10]; |
| 439 | int result = vsprintf(buf, fmt, va); // should crash here |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 440 | va_end(va); |
| 441 | return result; |
| 442 | } |
| 443 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 444 | TEST_F(DEATHTEST, vsprintf_fortified) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 445 | ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 448 | TEST_F(DEATHTEST, vsprintf2_fortified) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 449 | ASSERT_FORTIFY(vsprintf_helper("0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 452 | static int vsnprintf_helper(const char* fmt, ...) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 453 | va_list va; |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 454 | va_start(va, fmt); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 455 | char buf[10]; |
| 456 | volatile size_t size = 11; |
| 457 | int result = vsnprintf(buf, size, fmt, va); // should crash here |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 458 | va_end(va); |
| 459 | return result; |
| 460 | } |
| 461 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 462 | TEST_F(DEATHTEST, vsnprintf_fortified) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 463 | ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 466 | TEST_F(DEATHTEST, vsnprintf2_fortified) { |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 467 | ASSERT_FORTIFY(vsnprintf_helper("0123456789")); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 470 | TEST_F(DEATHTEST, strncat_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 471 | char buf[10]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 472 | volatile size_t n = 10; |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 473 | strncpy(buf, "012345678", n); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 474 | ASSERT_FORTIFY(strncat(buf, "9", n)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 477 | TEST_F(DEATHTEST, strncat2_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 478 | char buf[10]; |
| 479 | buf[0] = '\0'; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 480 | volatile size_t n = 10; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 481 | ASSERT_FORTIFY(strncat(buf, "0123456789", n)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 484 | TEST_F(DEATHTEST, strcat_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 485 | char src[11]; |
| 486 | strcpy(src, "0123456789"); |
| 487 | char buf[10]; |
| 488 | buf[0] = '\0'; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 489 | ASSERT_FORTIFY(strcat(buf, src)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 492 | TEST_F(DEATHTEST, memmove_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 493 | char buf[20]; |
| 494 | strcpy(buf, "0123456789"); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 495 | volatile size_t n = 10; |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 496 | ASSERT_FORTIFY_OR_HWASAN(DoNotOptimize(memmove(buf + 11, buf, n))); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 499 | TEST_F(DEATHTEST, memcpy_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 500 | char bufa[10]; |
| 501 | char bufb[10]; |
| 502 | strcpy(bufa, "012345678"); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 503 | volatile size_t n = 11; |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 504 | ASSERT_FORTIFY_OR_HWASAN(DoNotOptimize(memcpy(bufb, bufa, n))); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| Elliott Hughes | 62e5964 | 2016-03-01 11:22:42 -0800 | [diff] [blame] | 507 | TEST_F(DEATHTEST, memset_fortified) { |
| 508 | char buf[10]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 509 | volatile size_t n = 11; |
| Peter Collingbourne | dd12616 | 2025-03-25 17:32:35 -0700 | [diff] [blame] | 510 | ASSERT_FORTIFY_OR_HWASAN(DoNotOptimize(memset(buf, 0, n))); |
| Elliott Hughes | 62e5964 | 2016-03-01 11:22:42 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 513 | TEST_F(DEATHTEST, stpncpy_fortified) { |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 514 | char bufa[15]; |
| 515 | char bufb[10]; |
| 516 | strcpy(bufa, "01234567890123"); |
| 517 | size_t n = strlen(bufa); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 518 | ASSERT_FORTIFY(stpncpy(bufb, bufa, n)); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 521 | TEST_F(DEATHTEST, stpncpy2_fortified) { |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 522 | char dest[11]; |
| 523 | char src[10]; |
| 524 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 525 | ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest))); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 528 | TEST_F(DEATHTEST, strncpy_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 529 | char bufa[15]; |
| 530 | char bufb[10]; |
| 531 | strcpy(bufa, "01234567890123"); |
| 532 | size_t n = strlen(bufa); |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 533 | ASSERT_FORTIFY(strncpy(bufb, bufa, n)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 536 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 537 | TEST_F(DEATHTEST, strncpy2_fortified) { |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 538 | char dest[11]; |
| 539 | char src[10]; |
| 540 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 541 | ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest))); |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 544 | TEST_F(DEATHTEST, snprintf_fortified) { |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 545 | char bufa[15]; |
| 546 | char bufb[10]; |
| 547 | strcpy(bufa, "0123456789"); |
| 548 | size_t n = strlen(bufa) + 1; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 549 | ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa)); |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 552 | TEST_F(DEATHTEST, bzero_fortified) { |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 553 | char buf[10]; |
| 554 | memcpy(buf, "0123456789", sizeof(buf)); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 555 | size_t n = 11; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 556 | ASSERT_FORTIFY(bzero(buf, n)); |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 559 | TEST_F(DEATHTEST, umask_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 560 | volatile mode_t mask = 01777; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 561 | ASSERT_FORTIFY(umask(mask)); |
| Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 564 | TEST_F(DEATHTEST, recv_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 565 | volatile size_t data_len = 11; |
| Nick Kralevich | 60f4f9a | 2013-09-24 16:32:07 -0700 | [diff] [blame] | 566 | char buf[10]; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 567 | ASSERT_FORTIFY(recv(0, buf, data_len, 0)); |
| Nick Kralevich | 60f4f9a | 2013-09-24 16:32:07 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 570 | TEST_F(DEATHTEST, send_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 571 | volatile size_t data_len = 11; |
| Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 572 | char buf[10] = {0}; |
| 573 | ASSERT_FORTIFY(send(0, buf, data_len, 0)); |
| 574 | } |
| 575 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 576 | TEST_F(DEATHTEST, FD_ISSET_fortified) { |
| Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 577 | #if defined(__BIONIC__) // glibc catches this at compile-time. |
| Christopher Ferris | 2a39188 | 2024-12-19 13:44:35 -0800 | [diff] [blame] | 578 | fd_set set = {}; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 579 | ASSERT_FORTIFY(FD_ISSET(-1, &set)); |
| Elliott Hughes | 409588c | 2014-04-23 23:02:43 -0700 | [diff] [blame] | 580 | #endif |
| Nick Kralevich | 90201d5 | 2013-10-02 16:11:30 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 583 | TEST_F(DEATHTEST, FD_ISSET_2_fortified) { |
| Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 584 | char buf[1]; |
| 585 | fd_set* set = (fd_set*) buf; |
| Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 586 | ASSERT_FORTIFY(FD_ISSET(0, set)); |
| Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 589 | TEST_F(DEATHTEST, getcwd_fortified) { |
| 590 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 591 | volatile size_t n = 2; |
| 592 | ASSERT_FORTIFY(getcwd(buf, n)); |
| Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 593 | } |
| 594 | |
| Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 595 | TEST_F(DEATHTEST, pread_fortified) { |
| 596 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 597 | volatile size_t n = 2; |
| Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 598 | int fd = open("/dev/null", O_RDONLY); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 599 | ASSERT_FORTIFY(pread(fd, buf, n, 0)); |
| Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 600 | close(fd); |
| 601 | } |
| 602 | |
| 603 | TEST_F(DEATHTEST, pread64_fortified) { |
| 604 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 605 | volatile size_t n = 2; |
| Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 606 | int fd = open("/dev/null", O_RDONLY); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 607 | ASSERT_FORTIFY(pread64(fd, buf, n, 0)); |
| Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 608 | close(fd); |
| 609 | } |
| 610 | |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 611 | TEST_F(DEATHTEST, pwrite_fortified) { |
| 612 | char buf[1] = {0}; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 613 | volatile size_t n = 2; |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 614 | int fd = open("/dev/null", O_WRONLY); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 615 | ASSERT_FORTIFY(pwrite(fd, buf, n, 0)); |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 616 | close(fd); |
| 617 | } |
| 618 | |
| 619 | TEST_F(DEATHTEST, pwrite64_fortified) { |
| 620 | char buf[1] = {0}; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 621 | volatile size_t n = 2; |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 622 | int fd = open("/dev/null", O_WRONLY); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 623 | ASSERT_FORTIFY(pwrite64(fd, buf, n, 0)); |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 624 | close(fd); |
| 625 | } |
| 626 | |
| Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 627 | TEST_F(DEATHTEST, read_fortified) { |
| Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 628 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 629 | volatile size_t n = 2; |
| Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 630 | int fd = open("/dev/null", O_RDONLY); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 631 | ASSERT_FORTIFY(read(fd, buf, n)); |
| Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 632 | close(fd); |
| 633 | } |
| 634 | |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 635 | TEST_F(DEATHTEST, write_fortified) { |
| 636 | char buf[1] = {0}; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 637 | volatile size_t n = 2; |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 638 | int fd = open("/dev/null", O_WRONLY); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 639 | ASSERT_FORTIFY(write(fd, buf, n)); |
| Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 640 | close(fd); |
| 641 | } |
| 642 | |
| Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 643 | TEST_F(DEATHTEST, fread_fortified) { |
| 644 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 645 | volatile size_t n = 2; |
| Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 646 | FILE* fp = fopen("/dev/null", "r"); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 647 | ASSERT_FORTIFY(fread(buf, 1, n, fp)); |
| Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 648 | fclose(fp); |
| 649 | } |
| 650 | |
| 651 | TEST_F(DEATHTEST, fwrite_fortified) { |
| 652 | char buf[1] = {0}; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 653 | volatile size_t n = 2; |
| Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 654 | FILE* fp = fopen("/dev/null", "w"); |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 655 | ASSERT_FORTIFY(fwrite(buf, 1, n, fp)); |
| Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 656 | fclose(fp); |
| 657 | } |
| 658 | |
| Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 659 | TEST_F(DEATHTEST, readlink_fortified) { |
| 660 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 661 | volatile size_t n = 2; |
| 662 | ASSERT_FORTIFY(readlink("/dev/null", buf, n)); |
| Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | TEST_F(DEATHTEST, readlinkat_fortified) { |
| 666 | char buf[1]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 667 | volatile size_t n = 2; |
| 668 | ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, n)); |
| Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 669 | } |
| 670 | |
| zijunzhao | e1833e5 | 2023-04-26 21:43:30 +0000 | [diff] [blame] | 671 | TEST(TEST_NAME, snprintf_nullptr_valid) { |
| 672 | ASSERT_EQ(10, snprintf(nullptr, 0, "0123456789")); |
| 673 | } |
| 674 | |
| Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 675 | extern "C" char* __strncat_chk(char*, const char*, size_t, size_t); |
| 676 | extern "C" char* __strcat_chk(char*, const char*, size_t); |
| 677 | |
| 678 | TEST(TEST_NAME, strncat) { |
| 679 | char buf[10]; |
| 680 | memset(buf, 'A', sizeof(buf)); |
| 681 | buf[0] = 'a'; |
| 682 | buf[1] = '\0'; |
| 683 | char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf)); |
| 684 | ASSERT_EQ(buf, res); |
| 685 | ASSERT_EQ('a', buf[0]); |
| 686 | ASSERT_EQ('0', buf[1]); |
| 687 | ASSERT_EQ('1', buf[2]); |
| 688 | ASSERT_EQ('2', buf[3]); |
| 689 | ASSERT_EQ('3', buf[4]); |
| 690 | ASSERT_EQ('4', buf[5]); |
| 691 | ASSERT_EQ('\0', buf[6]); |
| 692 | ASSERT_EQ('A', buf[7]); |
| 693 | ASSERT_EQ('A', buf[8]); |
| 694 | ASSERT_EQ('A', buf[9]); |
| 695 | } |
| 696 | |
| 697 | TEST(TEST_NAME, strncat2) { |
| 698 | char buf[10]; |
| 699 | memset(buf, 'A', sizeof(buf)); |
| 700 | buf[0] = 'a'; |
| 701 | buf[1] = '\0'; |
| 702 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 703 | ASSERT_EQ(buf, res); |
| 704 | ASSERT_EQ('a', buf[0]); |
| 705 | ASSERT_EQ('0', buf[1]); |
| 706 | ASSERT_EQ('1', buf[2]); |
| 707 | ASSERT_EQ('2', buf[3]); |
| 708 | ASSERT_EQ('3', buf[4]); |
| 709 | ASSERT_EQ('4', buf[5]); |
| 710 | ASSERT_EQ('\0', buf[6]); |
| 711 | ASSERT_EQ('A', buf[7]); |
| 712 | ASSERT_EQ('A', buf[8]); |
| 713 | ASSERT_EQ('A', buf[9]); |
| 714 | } |
| 715 | |
| 716 | TEST(TEST_NAME, strncat3) { |
| 717 | char buf[10]; |
| 718 | memset(buf, 'A', sizeof(buf)); |
| 719 | buf[0] = '\0'; |
| 720 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 721 | ASSERT_EQ(buf, res); |
| 722 | ASSERT_EQ('0', buf[0]); |
| 723 | ASSERT_EQ('1', buf[1]); |
| 724 | ASSERT_EQ('2', buf[2]); |
| 725 | ASSERT_EQ('3', buf[3]); |
| 726 | ASSERT_EQ('4', buf[4]); |
| 727 | ASSERT_EQ('\0', buf[5]); |
| 728 | ASSERT_EQ('A', buf[6]); |
| 729 | ASSERT_EQ('A', buf[7]); |
| 730 | ASSERT_EQ('A', buf[8]); |
| 731 | ASSERT_EQ('A', buf[9]); |
| 732 | } |
| 733 | |
| 734 | TEST(TEST_NAME, strncat4) { |
| 735 | char buf[10]; |
| 736 | memset(buf, 'A', sizeof(buf)); |
| 737 | buf[9] = '\0'; |
| 738 | char* res = __strncat_chk(buf, "", 5, sizeof(buf)); |
| 739 | ASSERT_EQ(buf, res); |
| 740 | ASSERT_EQ('A', buf[0]); |
| 741 | ASSERT_EQ('A', buf[1]); |
| 742 | ASSERT_EQ('A', buf[2]); |
| 743 | ASSERT_EQ('A', buf[3]); |
| 744 | ASSERT_EQ('A', buf[4]); |
| 745 | ASSERT_EQ('A', buf[5]); |
| 746 | ASSERT_EQ('A', buf[6]); |
| 747 | ASSERT_EQ('A', buf[7]); |
| 748 | ASSERT_EQ('A', buf[8]); |
| 749 | ASSERT_EQ('\0', buf[9]); |
| 750 | } |
| 751 | |
| 752 | TEST(TEST_NAME, strncat5) { |
| 753 | char buf[10]; |
| 754 | memset(buf, 'A', sizeof(buf)); |
| 755 | buf[0] = 'a'; |
| 756 | buf[1] = '\0'; |
| 757 | char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf)); |
| 758 | ASSERT_EQ(buf, res); |
| 759 | ASSERT_EQ('a', buf[0]); |
| 760 | ASSERT_EQ('0', buf[1]); |
| 761 | ASSERT_EQ('1', buf[2]); |
| 762 | ASSERT_EQ('2', buf[3]); |
| 763 | ASSERT_EQ('3', buf[4]); |
| 764 | ASSERT_EQ('4', buf[5]); |
| 765 | ASSERT_EQ('5', buf[6]); |
| 766 | ASSERT_EQ('6', buf[7]); |
| 767 | ASSERT_EQ('7', buf[8]); |
| 768 | ASSERT_EQ('\0', buf[9]); |
| 769 | } |
| 770 | |
| 771 | TEST(TEST_NAME, strncat6) { |
| 772 | char buf[10]; |
| 773 | memset(buf, 'A', sizeof(buf)); |
| 774 | buf[0] = 'a'; |
| 775 | buf[1] = '\0'; |
| 776 | char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf)); |
| 777 | ASSERT_EQ(buf, res); |
| 778 | ASSERT_EQ('a', buf[0]); |
| 779 | ASSERT_EQ('0', buf[1]); |
| 780 | ASSERT_EQ('1', buf[2]); |
| 781 | ASSERT_EQ('2', buf[3]); |
| 782 | ASSERT_EQ('3', buf[4]); |
| 783 | ASSERT_EQ('4', buf[5]); |
| 784 | ASSERT_EQ('5', buf[6]); |
| 785 | ASSERT_EQ('6', buf[7]); |
| 786 | ASSERT_EQ('7', buf[8]); |
| 787 | ASSERT_EQ('\0', buf[9]); |
| 788 | } |
| 789 | |
| 790 | |
| 791 | TEST(TEST_NAME, strcat) { |
| 792 | char buf[10]; |
| 793 | memset(buf, 'A', sizeof(buf)); |
| 794 | buf[0] = 'a'; |
| 795 | buf[1] = '\0'; |
| 796 | char* res = __strcat_chk(buf, "01234", sizeof(buf)); |
| 797 | ASSERT_EQ(buf, res); |
| 798 | ASSERT_EQ('a', buf[0]); |
| 799 | ASSERT_EQ('0', buf[1]); |
| 800 | ASSERT_EQ('1', buf[2]); |
| 801 | ASSERT_EQ('2', buf[3]); |
| 802 | ASSERT_EQ('3', buf[4]); |
| 803 | ASSERT_EQ('4', buf[5]); |
| 804 | ASSERT_EQ('\0', buf[6]); |
| 805 | ASSERT_EQ('A', buf[7]); |
| 806 | ASSERT_EQ('A', buf[8]); |
| 807 | ASSERT_EQ('A', buf[9]); |
| 808 | } |
| 809 | |
| 810 | TEST(TEST_NAME, strcat2) { |
| 811 | char buf[10]; |
| 812 | memset(buf, 'A', sizeof(buf)); |
| 813 | buf[0] = 'a'; |
| 814 | buf[1] = '\0'; |
| 815 | char* res = __strcat_chk(buf, "01234567", sizeof(buf)); |
| 816 | ASSERT_EQ(buf, res); |
| 817 | ASSERT_EQ('a', buf[0]); |
| 818 | ASSERT_EQ('0', buf[1]); |
| 819 | ASSERT_EQ('1', buf[2]); |
| 820 | ASSERT_EQ('2', buf[3]); |
| 821 | ASSERT_EQ('3', buf[4]); |
| 822 | ASSERT_EQ('4', buf[5]); |
| 823 | ASSERT_EQ('5', buf[6]); |
| 824 | ASSERT_EQ('6', buf[7]); |
| 825 | ASSERT_EQ('7', buf[8]); |
| 826 | ASSERT_EQ('\0', buf[9]); |
| 827 | } |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 828 | |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 829 | TEST(TEST_NAME, stpncpy) { |
| 830 | char src[10]; |
| 831 | char dst[10]; |
| 832 | memcpy(src, "0123456789", sizeof(src)); // non null terminated string |
| 833 | stpncpy(dst, src, sizeof(dst)); |
| 834 | ASSERT_EQ('0', dst[0]); |
| 835 | ASSERT_EQ('1', dst[1]); |
| 836 | ASSERT_EQ('2', dst[2]); |
| 837 | ASSERT_EQ('3', dst[3]); |
| 838 | ASSERT_EQ('4', dst[4]); |
| 839 | ASSERT_EQ('5', dst[5]); |
| 840 | ASSERT_EQ('6', dst[6]); |
| 841 | ASSERT_EQ('7', dst[7]); |
| 842 | ASSERT_EQ('8', dst[8]); |
| 843 | ASSERT_EQ('9', dst[9]); |
| 844 | } |
| 845 | |
| 846 | TEST(TEST_NAME, stpncpy2) { |
| 847 | char src[10]; |
| 848 | char dst[15]; |
| 849 | memcpy(src, "012345678\0", sizeof(src)); |
| 850 | stpncpy(dst, src, sizeof(dst)); |
| 851 | ASSERT_EQ('0', dst[0]); |
| 852 | ASSERT_EQ('1', dst[1]); |
| 853 | ASSERT_EQ('2', dst[2]); |
| 854 | ASSERT_EQ('3', dst[3]); |
| 855 | ASSERT_EQ('4', dst[4]); |
| 856 | ASSERT_EQ('5', dst[5]); |
| 857 | ASSERT_EQ('6', dst[6]); |
| 858 | ASSERT_EQ('7', dst[7]); |
| 859 | ASSERT_EQ('8', dst[8]); |
| 860 | ASSERT_EQ('\0', dst[9]); |
| 861 | ASSERT_EQ('\0', dst[10]); |
| 862 | ASSERT_EQ('\0', dst[11]); |
| 863 | ASSERT_EQ('\0', dst[12]); |
| 864 | ASSERT_EQ('\0', dst[13]); |
| 865 | ASSERT_EQ('\0', dst[14]); |
| 866 | } |
| 867 | |
| Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 868 | TEST(TEST_NAME, strncpy) { |
| 869 | char src[10]; |
| 870 | char dst[10]; |
| 871 | memcpy(src, "0123456789", sizeof(src)); // non null terminated string |
| 872 | strncpy(dst, src, sizeof(dst)); |
| 873 | ASSERT_EQ('0', dst[0]); |
| 874 | ASSERT_EQ('1', dst[1]); |
| 875 | ASSERT_EQ('2', dst[2]); |
| 876 | ASSERT_EQ('3', dst[3]); |
| 877 | ASSERT_EQ('4', dst[4]); |
| 878 | ASSERT_EQ('5', dst[5]); |
| 879 | ASSERT_EQ('6', dst[6]); |
| 880 | ASSERT_EQ('7', dst[7]); |
| 881 | ASSERT_EQ('8', dst[8]); |
| 882 | ASSERT_EQ('9', dst[9]); |
| 883 | } |
| 884 | |
| 885 | TEST(TEST_NAME, strncpy2) { |
| 886 | char src[10]; |
| 887 | char dst[15]; |
| 888 | memcpy(src, "012345678\0", sizeof(src)); |
| 889 | strncpy(dst, src, sizeof(dst)); |
| 890 | ASSERT_EQ('0', dst[0]); |
| 891 | ASSERT_EQ('1', dst[1]); |
| 892 | ASSERT_EQ('2', dst[2]); |
| 893 | ASSERT_EQ('3', dst[3]); |
| 894 | ASSERT_EQ('4', dst[4]); |
| 895 | ASSERT_EQ('5', dst[5]); |
| 896 | ASSERT_EQ('6', dst[6]); |
| 897 | ASSERT_EQ('7', dst[7]); |
| 898 | ASSERT_EQ('8', dst[8]); |
| 899 | ASSERT_EQ('\0', dst[9]); |
| 900 | ASSERT_EQ('\0', dst[10]); |
| 901 | ASSERT_EQ('\0', dst[11]); |
| 902 | ASSERT_EQ('\0', dst[12]); |
| 903 | ASSERT_EQ('\0', dst[13]); |
| 904 | ASSERT_EQ('\0', dst[14]); |
| 905 | } |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 906 | |
| 907 | TEST(TEST_NAME, strcat_chk_max_int_size) { |
| 908 | char buf[10]; |
| 909 | memset(buf, 'A', sizeof(buf)); |
| 910 | buf[0] = 'a'; |
| 911 | buf[1] = '\0'; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 912 | volatile size_t n = -1; |
| 913 | char* res = __strcat_chk(buf, "01234567", n); |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 914 | ASSERT_EQ(buf, res); |
| 915 | ASSERT_EQ('a', buf[0]); |
| 916 | ASSERT_EQ('0', buf[1]); |
| 917 | ASSERT_EQ('1', buf[2]); |
| 918 | ASSERT_EQ('2', buf[3]); |
| 919 | ASSERT_EQ('3', buf[4]); |
| 920 | ASSERT_EQ('4', buf[5]); |
| 921 | ASSERT_EQ('5', buf[6]); |
| 922 | ASSERT_EQ('6', buf[7]); |
| 923 | ASSERT_EQ('7', buf[8]); |
| 924 | ASSERT_EQ('\0', buf[9]); |
| 925 | } |
| 926 | |
| George Burgess IV | 849c0b9 | 2019-06-10 16:22:09 -0700 | [diff] [blame] | 927 | TEST(TEST_NAME, mempcpy_chk) { |
| 928 | const char input_str[] = "abcdefg"; |
| 929 | size_t input_str_size = strlen(input_str) + 1; |
| 930 | |
| 931 | char buf1[10] = {}; |
| 932 | char buf2[10] = {}; |
| 933 | |
| 934 | __builtin_mempcpy(buf1, input_str, input_str_size); |
| 935 | __builtin___mempcpy_chk(buf2, input_str, input_str_size, __bos0(buf2)); |
| 936 | |
| 937 | ASSERT_EQ(memcmp(buf1, buf2, sizeof(buf2)), 0); |
| 938 | |
| 939 | void *builtin_ptr = __builtin_mempcpy(buf1, input_str, input_str_size); |
| 940 | void *fortify_ptr = __builtin___mempcpy_chk(buf1, input_str, input_str_size, __bos0(buf2)); |
| 941 | |
| 942 | ASSERT_EQ(builtin_ptr, fortify_ptr); |
| 943 | } |
| 944 | |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 945 | extern "C" char* __stpcpy_chk(char*, const char*, size_t); |
| 946 | |
| 947 | TEST(TEST_NAME, stpcpy_chk_max_int_size) { |
| 948 | char buf[10]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 949 | volatile size_t n = -1; |
| 950 | char* res = __stpcpy_chk(buf, "012345678", n); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 951 | ASSERT_EQ(buf + strlen("012345678"), res); |
| 952 | ASSERT_STREQ("012345678", buf); |
| 953 | } |
| 954 | |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 955 | extern "C" char* __strcpy_chk(char*, const char*, size_t); |
| 956 | |
| 957 | TEST(TEST_NAME, strcpy_chk_max_int_size) { |
| 958 | char buf[10]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 959 | volatile size_t n = -1; |
| 960 | char* res = __strcpy_chk(buf, "012345678", n); |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 961 | ASSERT_EQ(buf, res); |
| Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 962 | ASSERT_STREQ("012345678", buf); |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t); |
| 966 | |
| Daniel Verkamp | df4e06c | 2025-01-24 14:08:16 -0800 | [diff] [blame] | 967 | TEST(TEST_NAME, memcpy_chk_smaller) { |
| 968 | char buf[10] = "XXXXXXXXX"; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 969 | volatile size_t n = 5; |
| Daniel Verkamp | df4e06c | 2025-01-24 14:08:16 -0800 | [diff] [blame] | 970 | void* res = __memcpy_chk(buf, "012346578", n, sizeof(buf)); |
| 971 | ASSERT_EQ((void*)buf, res); |
| 972 | ASSERT_EQ('0', buf[0]); |
| 973 | ASSERT_EQ('1', buf[1]); |
| 974 | ASSERT_EQ('2', buf[2]); |
| 975 | ASSERT_EQ('3', buf[3]); |
| 976 | ASSERT_EQ('4', buf[4]); |
| 977 | ASSERT_EQ('X', buf[5]); |
| 978 | ASSERT_EQ('X', buf[6]); |
| 979 | ASSERT_EQ('X', buf[7]); |
| 980 | ASSERT_EQ('X', buf[8]); |
| 981 | ASSERT_EQ('\0', buf[9]); |
| 982 | } |
| 983 | |
| 984 | TEST(TEST_NAME, memcpy_chk_exact_size) { |
| 985 | char buf[10] = "XXXXXXXXX"; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 986 | volatile size_t n = 10; |
| Daniel Verkamp | df4e06c | 2025-01-24 14:08:16 -0800 | [diff] [blame] | 987 | void* res = __memcpy_chk(buf, "012345678", n, sizeof(buf)); |
| 988 | ASSERT_EQ((void*)buf, res); |
| 989 | ASSERT_EQ('0', buf[0]); |
| 990 | ASSERT_EQ('1', buf[1]); |
| 991 | ASSERT_EQ('2', buf[2]); |
| 992 | ASSERT_EQ('3', buf[3]); |
| 993 | ASSERT_EQ('4', buf[4]); |
| 994 | ASSERT_EQ('5', buf[5]); |
| 995 | ASSERT_EQ('6', buf[6]); |
| 996 | ASSERT_EQ('7', buf[7]); |
| 997 | ASSERT_EQ('8', buf[8]); |
| 998 | ASSERT_EQ('\0', buf[9]); |
| 999 | } |
| 1000 | |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 1001 | TEST(TEST_NAME, memcpy_chk_max_int_size) { |
| 1002 | char buf[10]; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1003 | volatile size_t n = -1; |
| 1004 | void* res = __memcpy_chk(buf, "012345678", sizeof(buf), n); |
| Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 1005 | ASSERT_EQ((void*)buf, res); |
| 1006 | ASSERT_EQ('0', buf[0]); |
| 1007 | ASSERT_EQ('1', buf[1]); |
| 1008 | ASSERT_EQ('2', buf[2]); |
| 1009 | ASSERT_EQ('3', buf[3]); |
| 1010 | ASSERT_EQ('4', buf[4]); |
| 1011 | ASSERT_EQ('5', buf[5]); |
| 1012 | ASSERT_EQ('6', buf[6]); |
| 1013 | ASSERT_EQ('7', buf[7]); |
| 1014 | ASSERT_EQ('8', buf[8]); |
| 1015 | ASSERT_EQ('\0', buf[9]); |
| 1016 | } |
| Stephen Hines | 6e38072 | 2013-10-11 00:45:24 -0700 | [diff] [blame] | 1017 | |
| 1018 | // Verify that macro expansion is done properly for sprintf/snprintf (which |
| 1019 | // are defined as macros in stdio.h under clang). |
| 1020 | #define CONTENTS "macro expansion" |
| 1021 | #define BUF_AND_SIZE(A) A, sizeof(A) |
| 1022 | #define BUF_AND_CONTENTS(A) A, CONTENTS |
| 1023 | #define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS |
| 1024 | TEST(TEST_NAME, s_n_printf_macro_expansion) { |
| 1025 | char buf[BUFSIZ]; |
| 1026 | snprintf(BUF_AND_SIZE(buf), CONTENTS); |
| 1027 | EXPECT_STREQ(CONTENTS, buf); |
| 1028 | |
| 1029 | snprintf(BUF_AND_SIZE_AND_CONTENTS(buf)); |
| 1030 | EXPECT_STREQ(CONTENTS, buf); |
| 1031 | |
| 1032 | sprintf(BUF_AND_CONTENTS(buf)); |
| 1033 | EXPECT_STREQ(CONTENTS, buf); |
| 1034 | } |
| Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 1035 | |
| 1036 | TEST_F(DEATHTEST, poll_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1037 | volatile nfds_t fd_count = 2; |
| Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 1038 | pollfd buf[1] = {{0, POLLIN, 0}}; |
| Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 1039 | // Set timeout to zero to prevent waiting in poll when fortify test fails. |
| 1040 | ASSERT_FORTIFY(poll(buf, fd_count, 0)); |
| Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | TEST_F(DEATHTEST, ppoll_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1044 | volatile nfds_t fd_count = 2; |
| Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 1045 | pollfd buf[1] = {{0, POLLIN, 0}}; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1046 | // Set timeout to zero to prevent waiting in ppoll if fortify test fails. |
| 1047 | timespec timeout = {}; |
| Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 1048 | ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, nullptr)); |
| 1049 | } |
| 1050 | |
| 1051 | TEST_F(DEATHTEST, ppoll64_fortified) { |
| Elliott Hughes | e7943f8 | 2023-09-28 08:20:20 -0700 | [diff] [blame] | 1052 | #if defined(__BIONIC__) // glibc doesn't have ppoll64. |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1053 | volatile nfds_t fd_count = 2; |
| Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 1054 | pollfd buf[1] = {{0, POLLIN, 0}}; |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1055 | // Set timeout to zero to prevent waiting in ppoll if fortify test fails. |
| 1056 | timespec timeout= {}; |
| Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 1057 | ASSERT_FORTIFY(ppoll64(buf, fd_count, &timeout, nullptr)); |
| 1058 | #endif |
| Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 1059 | } |
| Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 1060 | |
| 1061 | TEST_F(DEATHTEST, open_O_CREAT_without_mode_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1062 | volatile int flags = O_CREAT; |
| Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 1063 | ASSERT_FORTIFY(open("", flags)); |
| 1064 | } |
| 1065 | |
| 1066 | TEST_F(DEATHTEST, open_O_TMPFILE_without_mode_fortified) { |
| Elliott Hughes | 5866673 | 2025-01-28 13:15:01 -0800 | [diff] [blame] | 1067 | volatile int flags = O_TMPFILE; |
| Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 1068 | ASSERT_FORTIFY(open("", flags)); |
| Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 1069 | } |