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 | |
Dan Albert | f68dcbe | 2016-02-02 17:13:03 -0800 | [diff] [blame] | 17 | // -Werror is on whether we like it or not, and we're intentionally doing awful |
| 18 | // things in this file. GCC is dumb and doesn't have a specific error class for |
| 19 | // the fortify failures (it's just -Werror), so we can't use anything more |
| 20 | // constrained than disabling all the warnings in the file :( It also won't let |
| 21 | // us use system_header in a .cpp file, so we have to #include this from |
| 22 | // fortify_test_main.cpp. |
| 23 | #pragma GCC system_header |
| 24 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 26 | #include "BionicDeathTest.h" |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 27 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 28 | #include <fcntl.h> |
| 29 | #include <malloc.h> |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 30 | #include <poll.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 31 | #include <signal.h> |
| 32 | #include <stdarg.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 37 | #include <time.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 39 | #if __BIONIC__ |
| 40 | #define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "FORTIFY") |
| 41 | #else |
| 42 | #define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "") |
| 43 | #endif |
| 44 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 45 | // Fortify test code needs to run multiple times, so TEST_NAME macro is used to |
| 46 | // distinguish different tests. TEST_NAME is defined in compilation command. |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 47 | #define DEATHTEST_PASTER(name) name##_DeathTest |
| 48 | #define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name) |
| 49 | #define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME) |
| 50 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 51 | class DEATHTEST : public BionicDeathTest {}; |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 52 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 53 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2 |
| 54 | struct foo { |
| 55 | char empty[0]; |
| 56 | char one[1]; |
| 57 | char a[10]; |
| 58 | char b[10]; |
| 59 | }; |
| 60 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 61 | TEST_F(DEATHTEST, stpncpy_fortified2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 62 | foo myfoo; |
| 63 | int copy_amt = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 64 | ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 65 | } |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 66 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 67 | TEST_F(DEATHTEST, stpncpy2_fortified2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 68 | foo myfoo; |
| 69 | memset(&myfoo, 0, sizeof(myfoo)); |
| 70 | myfoo.one[0] = 'A'; // not null terminated string |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 71 | ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b))); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 72 | } |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 73 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 74 | TEST_F(DEATHTEST, strncpy_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 75 | foo myfoo; |
| 76 | int copy_amt = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 77 | ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 78 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 79 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 80 | TEST_F(DEATHTEST, strncpy2_fortified2) { |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 81 | foo myfoo; |
| 82 | memset(&myfoo, 0, sizeof(myfoo)); |
| 83 | myfoo.one[0] = 'A'; // not null terminated string |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 84 | ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b))); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 85 | } |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 86 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 87 | TEST_F(DEATHTEST, sprintf_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 88 | foo myfoo; |
| 89 | char source_buf[15]; |
| 90 | memcpy(source_buf, "12345678901234", 15); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 91 | ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 92 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 93 | |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 94 | TEST_F(DEATHTEST, sprintf2_fortified2) { |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 95 | foo myfoo; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 96 | ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789")); |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 97 | } |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 98 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 99 | static int vsprintf_helper2(const char *fmt, ...) { |
| 100 | foo myfoo; |
| 101 | va_list va; |
| 102 | int result; |
| 103 | |
| 104 | va_start(va, fmt); |
| 105 | result = vsprintf(myfoo.a, fmt, va); // should crash here |
| 106 | va_end(va); |
| 107 | return result; |
| 108 | } |
| 109 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 110 | TEST_F(DEATHTEST, vsprintf_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 111 | ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 114 | TEST_F(DEATHTEST, vsprintf2_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 115 | ASSERT_FORTIFY(vsprintf_helper2("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 116 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 117 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 118 | static int vsnprintf_helper2(const char *fmt, ...) { |
| 119 | foo myfoo; |
| 120 | va_list va; |
| 121 | int result; |
| 122 | size_t size = atoi("11"); |
| 123 | |
| 124 | va_start(va, fmt); |
| 125 | result = vsnprintf(myfoo.a, size, fmt, va); // should crash here |
| 126 | va_end(va); |
| 127 | return result; |
| 128 | } |
| 129 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 130 | TEST_F(DEATHTEST, vsnprintf_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 131 | ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 134 | TEST_F(DEATHTEST, vsnprintf2_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 135 | ASSERT_FORTIFY(vsnprintf_helper2("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 136 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 137 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 138 | // zero sized target with "\0" source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 139 | TEST_F(DEATHTEST, stpcpy_fortified2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 140 | #if defined(__BIONIC__) |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 141 | foo myfoo; |
| 142 | char* src = strdup(""); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 143 | ASSERT_FORTIFY(stpcpy(myfoo.empty, src)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 144 | free(src); |
| 145 | #else // __BIONIC__ |
| 146 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 147 | #endif // __BIONIC__ |
| 148 | } |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 149 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 150 | // zero sized target with "\0" source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 151 | TEST_F(DEATHTEST, strcpy_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 152 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 153 | foo myfoo; |
| 154 | char* src = strdup(""); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 155 | ASSERT_FORTIFY(strcpy(myfoo.empty, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 156 | free(src); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 157 | #else // __BIONIC__ |
| 158 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 159 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 160 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 161 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 162 | // zero sized target with longer source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 163 | TEST_F(DEATHTEST, strcpy2_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 164 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 165 | foo myfoo; |
| 166 | char* src = strdup("1"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 167 | ASSERT_FORTIFY(strcpy(myfoo.empty, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 168 | free(src); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 169 | #else // __BIONIC__ |
| 170 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 171 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 172 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 173 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 174 | // one byte target with longer source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 175 | TEST_F(DEATHTEST, strcpy3_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 176 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 177 | foo myfoo; |
| 178 | char* src = strdup("12"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 179 | ASSERT_FORTIFY(strcpy(myfoo.one, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 180 | free(src); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 181 | #else // __BIONIC__ |
| 182 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 183 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 184 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 185 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 186 | TEST_F(DEATHTEST, strchr_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 187 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 188 | foo myfoo; |
| 189 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 190 | myfoo.b[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 191 | ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a'))); |
George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 192 | ASSERT_FORTIFY(printf("%s", strchr(static_cast<const char*>(myfoo.a), 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 193 | #else // __BIONIC__ |
| 194 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 195 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 196 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 197 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 198 | TEST_F(DEATHTEST, strrchr_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 199 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 200 | foo myfoo; |
| 201 | memcpy(myfoo.a, "0123456789", 10); |
| 202 | memcpy(myfoo.b, "01234", 6); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 203 | ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a'))); |
George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 204 | ASSERT_FORTIFY(printf("%s", strrchr(static_cast<const char*>(myfoo.a), 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 205 | #else // __BIONIC__ |
| 206 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 207 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 208 | } |
George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 209 | |
| 210 | TEST_F(DEATHTEST, memchr_fortified2) { |
| 211 | #if defined(__BIONIC__) |
| 212 | foo myfoo; |
| 213 | volatile int asize = sizeof(myfoo.a) + 1; |
| 214 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 215 | ASSERT_FORTIFY(printf("%s", memchr(myfoo.a, 'a', asize))); |
| 216 | ASSERT_FORTIFY(printf("%s", memchr(static_cast<const void*>(myfoo.a), 'a', asize))); |
| 217 | #else // __BIONIC__ |
| 218 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 219 | #endif // __BIONIC__ |
| 220 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 221 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 222 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 223 | TEST_F(DEATHTEST, strlcpy_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 224 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 225 | foo myfoo; |
| 226 | strcpy(myfoo.a, "01"); |
| 227 | size_t n = strlen(myfoo.a); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 228 | ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 229 | #else // __BIONIC__ |
| 230 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 231 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 232 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 233 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 234 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 235 | TEST_F(DEATHTEST, strlcat_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 236 | #if defined(__BIONIC__) |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 237 | foo myfoo; |
| 238 | strcpy(myfoo.a, "01"); |
| 239 | myfoo.one[0] = '\0'; |
| 240 | size_t n = strlen(myfoo.a); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 241 | ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 242 | #else // __BIONIC__ |
| 243 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 244 | #endif // __BIONIC__ |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 245 | } |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 246 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 247 | TEST_F(DEATHTEST, strncat_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 248 | foo myfoo; |
| 249 | size_t n = atoi("10"); // avoid compiler optimizations |
| 250 | strncpy(myfoo.a, "012345678", n); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 251 | ASSERT_FORTIFY(strncat(myfoo.a, "9", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 252 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 253 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 254 | TEST_F(DEATHTEST, strncat2_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 255 | foo myfoo; |
| 256 | myfoo.a[0] = '\0'; |
| 257 | size_t n = atoi("10"); // avoid compiler optimizations |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 258 | ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 259 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 260 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 261 | TEST_F(DEATHTEST, strncat3_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 262 | foo myfoo; |
| 263 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 264 | myfoo.b[0] = '\0'; |
| 265 | size_t n = atoi("10"); // avoid compiler optimizations |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 266 | ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 269 | TEST_F(DEATHTEST, strcat_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 270 | char src[11]; |
| 271 | strcpy(src, "0123456789"); |
| 272 | foo myfoo; |
| 273 | myfoo.a[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 274 | ASSERT_FORTIFY(strcat(myfoo.a, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 275 | } |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 276 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 277 | TEST_F(DEATHTEST, strcat2_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 278 | foo myfoo; |
| 279 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 280 | myfoo.b[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 281 | ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 284 | TEST_F(DEATHTEST, snprintf_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 285 | foo myfoo; |
| 286 | strcpy(myfoo.a, "012345678"); |
| 287 | size_t n = strlen(myfoo.a) + 2; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 288 | ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 291 | TEST_F(DEATHTEST, bzero_fortified2) { |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 292 | foo myfoo; |
| 293 | memcpy(myfoo.b, "0123456789", sizeof(myfoo.b)); |
| 294 | size_t n = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 295 | ASSERT_FORTIFY(bzero(myfoo.b, n)); |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 298 | #endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */ |
| 299 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 300 | // multibyte target where we over fill (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 301 | TEST_F(DEATHTEST, strcpy_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 302 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 303 | char buf[10]; |
| 304 | char *orig = strdup("0123456789"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 305 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 306 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 307 | #else // __BIONIC__ |
| 308 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 309 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // zero sized target with "\0" source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 313 | TEST_F(DEATHTEST, strcpy2_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 314 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 315 | char buf[0]; |
| 316 | char *orig = strdup(""); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 317 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 318 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 319 | #else // __BIONIC__ |
| 320 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 321 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // zero sized target with longer source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 325 | TEST_F(DEATHTEST, strcpy3_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 326 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 327 | char buf[0]; |
| 328 | char *orig = strdup("1"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 329 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 330 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 331 | #else // __BIONIC__ |
| 332 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 333 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // one byte target with longer source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 337 | TEST_F(DEATHTEST, strcpy4_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 338 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 339 | char buf[1]; |
| 340 | char *orig = strdup("12"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 341 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 342 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 343 | #else // __BIONIC__ |
| 344 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 345 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 348 | TEST_F(DEATHTEST, strlen_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 349 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 350 | char buf[10]; |
| 351 | memcpy(buf, "0123456789", sizeof(buf)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 352 | ASSERT_FORTIFY(printf("%zd", strlen(buf))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 353 | #else // __BIONIC__ |
| 354 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 355 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 358 | TEST_F(DEATHTEST, strchr_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 359 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 360 | char buf[10]; |
| 361 | memcpy(buf, "0123456789", sizeof(buf)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 362 | ASSERT_FORTIFY(printf("%s", strchr(buf, 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 363 | #else // __BIONIC__ |
| 364 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 365 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 368 | TEST_F(DEATHTEST, strrchr_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 369 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 370 | char buf[10]; |
| 371 | memcpy(buf, "0123456789", sizeof(buf)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 372 | ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 373 | #else // __BIONIC__ |
| 374 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 375 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 378 | TEST_F(DEATHTEST, strlcpy_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 379 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 380 | char bufa[15]; |
| 381 | char bufb[10]; |
| 382 | strcpy(bufa, "01234567890123"); |
| 383 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 384 | ASSERT_FORTIFY(strlcpy(bufb, bufa, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 385 | #else // __BIONIC__ |
| 386 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 387 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 390 | TEST_F(DEATHTEST, strlcat_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 391 | #if defined(__BIONIC__) |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 392 | char bufa[15]; |
| 393 | char bufb[10]; |
| 394 | bufb[0] = '\0'; |
| 395 | strcpy(bufa, "01234567890123"); |
| 396 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 397 | ASSERT_FORTIFY(strlcat(bufb, bufa, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 398 | #else // __BIONIC__ |
| 399 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 400 | #endif // __BIONIC__ |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 403 | TEST_F(DEATHTEST, sprintf_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 404 | char buf[10]; |
| 405 | char source_buf[15]; |
| 406 | memcpy(source_buf, "12345678901234", 15); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 407 | ASSERT_FORTIFY(sprintf(buf, "%s", source_buf)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Yi Kong | 2d3122c | 2017-04-30 15:08:05 -0700 | [diff] [blame^] | 410 | #ifdef __clang__ && !__has_attribute(alloc_size) |
| 411 | // TODO: remove this after Clang prebuilt rebase. |
George Burgess IV | 5f8a673 | 2017-04-06 11:26:11 -0700 | [diff] [blame] | 412 | #else |
Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 413 | // This test is disabled in clang because clang doesn't properly detect |
| 414 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 415 | TEST_F(DEATHTEST, sprintf_malloc_fortified) { |
Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 416 | char* buf = (char *) malloc(10); |
| 417 | char source_buf[11]; |
| 418 | memcpy(source_buf, "1234567890", 11); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 419 | ASSERT_FORTIFY(sprintf(buf, "%s", source_buf)); |
Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 420 | free(buf); |
| 421 | } |
| 422 | #endif |
| 423 | |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 424 | TEST_F(DEATHTEST, sprintf2_fortified) { |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 425 | char buf[5]; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 426 | ASSERT_FORTIFY(sprintf(buf, "aaaaa")); |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 429 | static int vsprintf_helper(const char *fmt, ...) { |
| 430 | char buf[10]; |
| 431 | va_list va; |
| 432 | int result; |
| 433 | |
| 434 | va_start(va, fmt); |
| 435 | result = vsprintf(buf, fmt, va); // should crash here |
| 436 | va_end(va); |
| 437 | return result; |
| 438 | } |
| 439 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 440 | TEST_F(DEATHTEST, vsprintf_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 441 | ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 444 | TEST_F(DEATHTEST, vsprintf2_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 445 | ASSERT_FORTIFY(vsprintf_helper("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static int vsnprintf_helper(const char *fmt, ...) { |
| 449 | char buf[10]; |
| 450 | va_list va; |
| 451 | int result; |
| 452 | size_t size = atoi("11"); |
| 453 | |
| 454 | va_start(va, fmt); |
| 455 | result = vsnprintf(buf, size, fmt, va); // should crash here |
| 456 | va_end(va); |
| 457 | return result; |
| 458 | } |
| 459 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 460 | TEST_F(DEATHTEST, vsnprintf_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 461 | ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 464 | TEST_F(DEATHTEST, vsnprintf2_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 465 | ASSERT_FORTIFY(vsnprintf_helper("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 468 | TEST_F(DEATHTEST, strncat_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 469 | char buf[10]; |
| 470 | size_t n = atoi("10"); // avoid compiler optimizations |
| 471 | strncpy(buf, "012345678", n); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 472 | ASSERT_FORTIFY(strncat(buf, "9", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 475 | TEST_F(DEATHTEST, strncat2_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 476 | char buf[10]; |
| 477 | buf[0] = '\0'; |
| 478 | size_t n = atoi("10"); // avoid compiler optimizations |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 479 | ASSERT_FORTIFY(strncat(buf, "0123456789", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 482 | TEST_F(DEATHTEST, strcat_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 483 | char src[11]; |
| 484 | strcpy(src, "0123456789"); |
| 485 | char buf[10]; |
| 486 | buf[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 487 | ASSERT_FORTIFY(strcat(buf, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 490 | TEST_F(DEATHTEST, memmove_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 491 | char buf[20]; |
| 492 | strcpy(buf, "0123456789"); |
| 493 | size_t n = atoi("10"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 494 | ASSERT_FORTIFY(memmove(buf + 11, buf, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 497 | TEST_F(DEATHTEST, memcpy_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 498 | char bufa[10]; |
| 499 | char bufb[10]; |
| 500 | strcpy(bufa, "012345678"); |
| 501 | size_t n = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 502 | ASSERT_FORTIFY(memcpy(bufb, bufa, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Elliott Hughes | 62e5964 | 2016-03-01 11:22:42 -0800 | [diff] [blame] | 505 | TEST_F(DEATHTEST, memset_fortified) { |
| 506 | char buf[10]; |
| 507 | size_t n = atoi("11"); |
| 508 | ASSERT_FORTIFY(memset(buf, 0, n)); |
| 509 | } |
| 510 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 511 | TEST_F(DEATHTEST, stpncpy_fortified) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 512 | char bufa[15]; |
| 513 | char bufb[10]; |
| 514 | strcpy(bufa, "01234567890123"); |
| 515 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 516 | ASSERT_FORTIFY(stpncpy(bufb, bufa, n)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 519 | TEST_F(DEATHTEST, stpncpy2_fortified) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 520 | char dest[11]; |
| 521 | char src[10]; |
| 522 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 523 | ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest))); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 526 | TEST_F(DEATHTEST, strncpy_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 527 | char bufa[15]; |
| 528 | char bufb[10]; |
| 529 | strcpy(bufa, "01234567890123"); |
| 530 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 531 | ASSERT_FORTIFY(strncpy(bufb, bufa, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 534 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 535 | TEST_F(DEATHTEST, strncpy2_fortified) { |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 536 | char dest[11]; |
| 537 | char src[10]; |
| 538 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 539 | ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest))); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 542 | TEST_F(DEATHTEST, snprintf_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 543 | char bufa[15]; |
| 544 | char bufb[10]; |
| 545 | strcpy(bufa, "0123456789"); |
| 546 | size_t n = strlen(bufa) + 1; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 547 | ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 550 | TEST_F(DEATHTEST, bzero_fortified) { |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 551 | char buf[10]; |
| 552 | memcpy(buf, "0123456789", sizeof(buf)); |
| 553 | size_t n = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 554 | ASSERT_FORTIFY(bzero(buf, n)); |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 557 | TEST_F(DEATHTEST, umask_fortified) { |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 558 | mode_t mask = atoi("1023"); // 01777 in octal |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 559 | ASSERT_FORTIFY(umask(mask)); |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 562 | TEST_F(DEATHTEST, recv_fortified) { |
Nick Kralevich | 60f4f9a | 2013-09-24 16:32:07 -0700 | [diff] [blame] | 563 | size_t data_len = atoi("11"); // suppress compiler optimizations |
| 564 | char buf[10]; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 565 | ASSERT_FORTIFY(recv(0, buf, data_len, 0)); |
Nick Kralevich | 60f4f9a | 2013-09-24 16:32:07 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 568 | TEST_F(DEATHTEST, send_fortified) { |
| 569 | size_t data_len = atoi("11"); // suppress compiler optimizations |
| 570 | char buf[10] = {0}; |
| 571 | ASSERT_FORTIFY(send(0, buf, data_len, 0)); |
| 572 | } |
| 573 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 574 | TEST_F(DEATHTEST, FD_ISSET_fortified) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 575 | #if defined(__BIONIC__) // glibc catches this at compile-time. |
Nick Kralevich | 90201d5 | 2013-10-02 16:11:30 -0700 | [diff] [blame] | 576 | fd_set set; |
| 577 | memset(&set, 0, sizeof(set)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 578 | ASSERT_FORTIFY(FD_ISSET(-1, &set)); |
Elliott Hughes | 409588c | 2014-04-23 23:02:43 -0700 | [diff] [blame] | 579 | #endif |
Nick Kralevich | 90201d5 | 2013-10-02 16:11:30 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 582 | TEST_F(DEATHTEST, FD_ISSET_2_fortified) { |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 583 | char buf[1]; |
| 584 | fd_set* set = (fd_set*) buf; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 585 | ASSERT_FORTIFY(FD_ISSET(0, set)); |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 588 | TEST_F(DEATHTEST, getcwd_fortified) { |
| 589 | char buf[1]; |
| 590 | size_t ct = atoi("2"); // prevent optimizations |
| 591 | ASSERT_FORTIFY(getcwd(buf, ct)); |
| 592 | } |
| 593 | |
Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 594 | TEST_F(DEATHTEST, pread_fortified) { |
| 595 | char buf[1]; |
| 596 | size_t ct = atoi("2"); // prevent optimizations |
| 597 | int fd = open("/dev/null", O_RDONLY); |
| 598 | ASSERT_FORTIFY(pread(fd, buf, ct, 0)); |
| 599 | close(fd); |
| 600 | } |
| 601 | |
| 602 | TEST_F(DEATHTEST, pread64_fortified) { |
| 603 | char buf[1]; |
| 604 | size_t ct = atoi("2"); // prevent optimizations |
| 605 | int fd = open("/dev/null", O_RDONLY); |
| 606 | ASSERT_FORTIFY(pread64(fd, buf, ct, 0)); |
| 607 | close(fd); |
| 608 | } |
| 609 | |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 610 | TEST_F(DEATHTEST, pwrite_fortified) { |
| 611 | char buf[1] = {0}; |
| 612 | size_t ct = atoi("2"); // prevent optimizations |
| 613 | int fd = open("/dev/null", O_WRONLY); |
| 614 | ASSERT_FORTIFY(pwrite(fd, buf, ct, 0)); |
| 615 | close(fd); |
| 616 | } |
| 617 | |
| 618 | TEST_F(DEATHTEST, pwrite64_fortified) { |
| 619 | char buf[1] = {0}; |
| 620 | size_t ct = atoi("2"); // prevent optimizations |
| 621 | int fd = open("/dev/null", O_WRONLY); |
| 622 | ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0)); |
| 623 | close(fd); |
| 624 | } |
| 625 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 626 | TEST_F(DEATHTEST, read_fortified) { |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 627 | char buf[1]; |
| 628 | size_t ct = atoi("2"); // prevent optimizations |
| 629 | int fd = open("/dev/null", O_RDONLY); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 630 | ASSERT_FORTIFY(read(fd, buf, ct)); |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 631 | close(fd); |
| 632 | } |
| 633 | |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 634 | TEST_F(DEATHTEST, write_fortified) { |
| 635 | char buf[1] = {0}; |
| 636 | size_t ct = atoi("2"); // prevent optimizations |
| 637 | int fd = open("/dev/null", O_WRONLY); |
| 638 | ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), ""); |
| 639 | close(fd); |
| 640 | } |
| 641 | |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 642 | TEST_F(DEATHTEST, fread_fortified) { |
| 643 | char buf[1]; |
| 644 | size_t ct = atoi("2"); // prevent optimizations |
| 645 | FILE* fp = fopen("/dev/null", "r"); |
| 646 | ASSERT_FORTIFY(fread(buf, 1, ct, fp)); |
| 647 | fclose(fp); |
| 648 | } |
| 649 | |
| 650 | TEST_F(DEATHTEST, fwrite_fortified) { |
| 651 | char buf[1] = {0}; |
| 652 | size_t ct = atoi("2"); // prevent optimizations |
| 653 | FILE* fp = fopen("/dev/null", "w"); |
| 654 | ASSERT_FORTIFY(fwrite(buf, 1, ct, fp)); |
| 655 | fclose(fp); |
| 656 | } |
| 657 | |
Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 658 | TEST_F(DEATHTEST, readlink_fortified) { |
| 659 | char buf[1]; |
| 660 | size_t ct = atoi("2"); // prevent optimizations |
| 661 | ASSERT_FORTIFY(readlink("/dev/null", buf, ct)); |
| 662 | } |
| 663 | |
| 664 | TEST_F(DEATHTEST, readlinkat_fortified) { |
| 665 | char buf[1]; |
| 666 | size_t ct = atoi("2"); // prevent optimizations |
| 667 | ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct)); |
| 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 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 921 | extern "C" char* __stpcpy_chk(char*, const char*, size_t); |
| 922 | |
| 923 | TEST(TEST_NAME, stpcpy_chk_max_int_size) { |
| 924 | char buf[10]; |
| 925 | char* res = __stpcpy_chk(buf, "012345678", (size_t)-1); |
| 926 | ASSERT_EQ(buf + strlen("012345678"), res); |
| 927 | ASSERT_STREQ("012345678", buf); |
| 928 | } |
| 929 | |
Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 930 | extern "C" char* __strcpy_chk(char*, const char*, size_t); |
| 931 | |
| 932 | TEST(TEST_NAME, strcpy_chk_max_int_size) { |
| 933 | char buf[10]; |
| 934 | char* res = __strcpy_chk(buf, "012345678", (size_t)-1); |
| 935 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 936 | ASSERT_STREQ("012345678", buf); |
Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t); |
| 940 | |
| 941 | TEST(TEST_NAME, memcpy_chk_max_int_size) { |
| 942 | char buf[10]; |
| 943 | void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1); |
| 944 | ASSERT_EQ((void*)buf, res); |
| 945 | ASSERT_EQ('0', buf[0]); |
| 946 | ASSERT_EQ('1', buf[1]); |
| 947 | ASSERT_EQ('2', buf[2]); |
| 948 | ASSERT_EQ('3', buf[3]); |
| 949 | ASSERT_EQ('4', buf[4]); |
| 950 | ASSERT_EQ('5', buf[5]); |
| 951 | ASSERT_EQ('6', buf[6]); |
| 952 | ASSERT_EQ('7', buf[7]); |
| 953 | ASSERT_EQ('8', buf[8]); |
| 954 | ASSERT_EQ('\0', buf[9]); |
| 955 | } |
Stephen Hines | 6e38072 | 2013-10-11 00:45:24 -0700 | [diff] [blame] | 956 | |
| 957 | // Verify that macro expansion is done properly for sprintf/snprintf (which |
| 958 | // are defined as macros in stdio.h under clang). |
| 959 | #define CONTENTS "macro expansion" |
| 960 | #define BUF_AND_SIZE(A) A, sizeof(A) |
| 961 | #define BUF_AND_CONTENTS(A) A, CONTENTS |
| 962 | #define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS |
| 963 | TEST(TEST_NAME, s_n_printf_macro_expansion) { |
| 964 | char buf[BUFSIZ]; |
| 965 | snprintf(BUF_AND_SIZE(buf), CONTENTS); |
| 966 | EXPECT_STREQ(CONTENTS, buf); |
| 967 | |
| 968 | snprintf(BUF_AND_SIZE_AND_CONTENTS(buf)); |
| 969 | EXPECT_STREQ(CONTENTS, buf); |
| 970 | |
| 971 | sprintf(BUF_AND_CONTENTS(buf)); |
| 972 | EXPECT_STREQ(CONTENTS, buf); |
| 973 | } |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 974 | |
| 975 | TEST_F(DEATHTEST, poll_fortified) { |
| 976 | nfds_t fd_count = atoi("2"); // suppress compiler optimizations |
| 977 | pollfd buf[1] = {{0, POLLIN, 0}}; |
Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 978 | // Set timeout to zero to prevent waiting in poll when fortify test fails. |
| 979 | ASSERT_FORTIFY(poll(buf, fd_count, 0)); |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | TEST_F(DEATHTEST, ppoll_fortified) { |
| 983 | nfds_t fd_count = atoi("2"); // suppress compiler optimizations |
| 984 | pollfd buf[1] = {{0, POLLIN, 0}}; |
Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 985 | // Set timeout to zero to prevent waiting in ppoll when fortify test fails. |
| 986 | timespec timeout; |
| 987 | timeout.tv_sec = timeout.tv_nsec = 0; |
| 988 | ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, NULL)); |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 989 | } |