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