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