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