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