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