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> |
| 18 | #include <string.h> |
| 19 | #include <stdarg.h> |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 22 | |
| 23 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 24 | // in its own process. Unfortunately, the C preprocessor doesn't give us an |
| 25 | // easy way to concatenate strings, so we need to use the complicated method |
| 26 | // below. *sigh* |
| 27 | #define DEATHTEST_PASTER(name) name##_DeathTest |
| 28 | #define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name) |
| 29 | #define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME) |
| 30 | |
| 31 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2 |
| 32 | struct foo { |
| 33 | char empty[0]; |
| 34 | char one[1]; |
| 35 | char a[10]; |
| 36 | char b[10]; |
| 37 | }; |
| 38 | |
| 39 | #ifndef __clang__ |
| 40 | // This test is disabled in clang because clang doesn't properly detect |
| 41 | // this buffer overflow. TODO: Fix clang. |
| 42 | TEST(DEATHTEST, strncpy_fortified2) { |
| 43 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 44 | foo myfoo; |
| 45 | int copy_amt = atoi("11"); |
| 46 | ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt), |
| 47 | testing::KilledBySignal(SIGABRT), ""); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | #ifndef __clang__ |
| 52 | // This test is disabled in clang because clang doesn't properly detect |
| 53 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 54 | TEST(DEATHTEST, strncpy2_fortified2) { |
| 55 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 56 | foo myfoo; |
| 57 | memset(&myfoo, 0, sizeof(myfoo)); |
| 58 | myfoo.one[0] = 'A'; // not null terminated string |
| 59 | ASSERT_EXIT(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)), |
| 60 | testing::KilledBySignal(SIGABRT), ""); |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | #ifndef __clang__ |
| 65 | // This test is disabled in clang because clang doesn't properly detect |
| 66 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 67 | TEST(DEATHTEST, sprintf_fortified2) { |
| 68 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 69 | foo myfoo; |
| 70 | char source_buf[15]; |
| 71 | memcpy(source_buf, "12345678901234", 15); |
| 72 | ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf), |
| 73 | testing::KilledBySignal(SIGABRT), ""); |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | #ifndef __clang__ |
| 78 | // This test is disabled in clang because clang doesn't properly detect |
| 79 | // this buffer overflow. TODO: Fix clang. |
| 80 | TEST(DEATHTEST, sprintf2_fortified2) { |
| 81 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 82 | foo myfoo; |
| 83 | ASSERT_EXIT(sprintf(myfoo.a, "0123456789"), |
| 84 | testing::KilledBySignal(SIGABRT), ""); |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | #ifndef __clang__ |
| 89 | // These tests are disabled in clang because clang doesn't properly detect |
| 90 | // this buffer overflow. TODO: Fix clang. |
| 91 | static int vsprintf_helper2(const char *fmt, ...) { |
| 92 | foo myfoo; |
| 93 | va_list va; |
| 94 | int result; |
| 95 | |
| 96 | va_start(va, fmt); |
| 97 | result = vsprintf(myfoo.a, fmt, va); // should crash here |
| 98 | va_end(va); |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | TEST(DEATHTEST, vsprintf_fortified2) { |
| 103 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 104 | ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 105 | } |
| 106 | |
| 107 | TEST(DEATHTEST, vsprintf2_fortified2) { |
| 108 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 109 | ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | #ifndef __clang__ |
| 114 | // These tests are disabled in clang because clang doesn't properly detect |
| 115 | // this buffer overflow. TODO: Fix clang. |
| 116 | static int vsnprintf_helper2(const char *fmt, ...) { |
| 117 | foo myfoo; |
| 118 | va_list va; |
| 119 | int result; |
| 120 | size_t size = atoi("11"); |
| 121 | |
| 122 | va_start(va, fmt); |
| 123 | result = vsnprintf(myfoo.a, size, fmt, va); // should crash here |
| 124 | va_end(va); |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | TEST(DEATHTEST, vsnprintf_fortified2) { |
| 129 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 130 | ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 131 | } |
| 132 | |
| 133 | TEST(DEATHTEST, vsnprintf2_fortified2) { |
| 134 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 135 | ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | #if __BIONIC__ |
| 140 | |
| 141 | #ifndef __clang__ |
| 142 | // zero sized target with "\0" source (should fail) |
| 143 | // This test is disabled in clang because clang doesn't properly detect |
| 144 | // this buffer overflow. TODO: Fix clang. |
| 145 | TEST(DEATHTEST, strcpy_fortified2) { |
| 146 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 147 | foo myfoo; |
| 148 | char* src = strdup(""); |
| 149 | ASSERT_EXIT(strcpy(myfoo.empty, src), |
| 150 | testing::KilledBySignal(SIGABRT), ""); |
| 151 | free(src); |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | #ifndef __clang__ |
| 156 | // zero sized target with longer source (should fail) |
| 157 | // This test is disabled in clang because clang doesn't properly detect |
| 158 | // this buffer overflow. TODO: Fix clang. |
| 159 | TEST(DEATHTEST, strcpy2_fortified2) { |
| 160 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 161 | foo myfoo; |
| 162 | char* src = strdup("1"); |
| 163 | ASSERT_EXIT(strcpy(myfoo.empty, src), |
| 164 | testing::KilledBySignal(SIGABRT), ""); |
| 165 | free(src); |
| 166 | } |
| 167 | #endif |
| 168 | |
| 169 | #ifndef __clang__ |
| 170 | // one byte target with longer source (should fail) |
| 171 | // This test is disabled in clang because clang doesn't properly detect |
| 172 | // this buffer overflow. TODO: Fix clang. |
| 173 | TEST(DEATHTEST, strcpy3_fortified2) { |
| 174 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 175 | foo myfoo; |
| 176 | char* src = strdup("12"); |
| 177 | ASSERT_EXIT(strcpy(myfoo.one, src), |
| 178 | testing::KilledBySignal(SIGABRT), ""); |
| 179 | free(src); |
| 180 | } |
| 181 | #endif |
| 182 | |
| 183 | #ifndef __clang__ |
| 184 | // This test is disabled in clang because clang doesn't properly detect |
| 185 | // this buffer overflow. TODO: Fix clang. |
| 186 | TEST(DEATHTEST, strchr_fortified2) { |
| 187 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 188 | foo myfoo; |
| 189 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 190 | myfoo.b[0] = '\0'; |
| 191 | ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')), |
| 192 | testing::KilledBySignal(SIGABRT), ""); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | #ifndef __clang__ |
| 197 | // This test is disabled in clang because clang doesn't properly detect |
| 198 | // this buffer overflow. TODO: Fix clang. |
| 199 | TEST(DEATHTEST, strrchr_fortified2) { |
| 200 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 201 | foo myfoo; |
| 202 | memcpy(myfoo.a, "0123456789", 10); |
| 203 | memcpy(myfoo.b, "01234", 6); |
| 204 | ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')), |
| 205 | testing::KilledBySignal(SIGABRT), ""); |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | #ifndef __clang__ |
| 210 | // This test is disabled in clang because clang doesn't properly detect |
| 211 | // this buffer overflow. TODO: Fix clang. |
| 212 | TEST(DEATHTEST, strlcpy_fortified2) { |
| 213 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 214 | foo myfoo; |
| 215 | strcpy(myfoo.a, "01"); |
| 216 | size_t n = strlen(myfoo.a); |
| 217 | ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n), |
| 218 | testing::KilledBySignal(SIGABRT), ""); |
| 219 | } |
| 220 | #endif |
| 221 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 222 | #ifndef __clang__ |
| 223 | // This test is disabled in clang because clang doesn't properly detect |
| 224 | // this buffer overflow. TODO: Fix clang. |
| 225 | TEST(DEATHTEST, strlcat_fortified2) { |
| 226 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 227 | foo myfoo; |
| 228 | strcpy(myfoo.a, "01"); |
| 229 | myfoo.one[0] = '\0'; |
| 230 | size_t n = strlen(myfoo.a); |
| 231 | ASSERT_EXIT(strlcat(myfoo.one, myfoo.a, n), |
| 232 | testing::KilledBySignal(SIGABRT), ""); |
| 233 | } |
| 234 | #endif |
| 235 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 236 | #endif /* __BIONIC__ */ |
| 237 | |
| 238 | #ifndef __clang__ |
| 239 | // This test is disabled in clang because clang doesn't properly detect |
| 240 | // this buffer overflow. TODO: Fix clang. |
| 241 | TEST(DEATHTEST, strncat_fortified2) { |
| 242 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 243 | foo myfoo; |
| 244 | size_t n = atoi("10"); // avoid compiler optimizations |
| 245 | strncpy(myfoo.a, "012345678", n); |
| 246 | ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), ""); |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | #ifndef __clang__ |
| 251 | // This test is disabled in clang because clang doesn't properly detect |
| 252 | // this buffer overflow. TODO: Fix clang. |
| 253 | TEST(DEATHTEST, strncat2_fortified2) { |
| 254 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 255 | foo myfoo; |
| 256 | myfoo.a[0] = '\0'; |
| 257 | size_t n = atoi("10"); // avoid compiler optimizations |
| 258 | ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | TEST(DEATHTEST, strncat3_fortified2) { |
| 263 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 264 | foo myfoo; |
| 265 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 266 | myfoo.b[0] = '\0'; |
| 267 | size_t n = atoi("10"); // avoid compiler optimizations |
| 268 | ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), ""); |
| 269 | } |
| 270 | |
| 271 | #ifndef __clang__ |
| 272 | // This test is disabled in clang because clang doesn't properly detect |
| 273 | // this buffer overflow. TODO: Fix clang. |
| 274 | TEST(DEATHTEST, strcat_fortified2) { |
| 275 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 276 | char src[11]; |
| 277 | strcpy(src, "0123456789"); |
| 278 | foo myfoo; |
| 279 | myfoo.a[0] = '\0'; |
| 280 | ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), ""); |
| 281 | } |
| 282 | #endif |
| 283 | |
| 284 | TEST(DEATHTEST, strcat2_fortified2) { |
| 285 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 286 | foo myfoo; |
| 287 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 288 | myfoo.b[0] = '\0'; |
| 289 | ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), ""); |
| 290 | } |
| 291 | |
| 292 | TEST(DEATHTEST, snprintf_fortified2) { |
| 293 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 294 | foo myfoo; |
| 295 | strcpy(myfoo.a, "012345678"); |
| 296 | size_t n = strlen(myfoo.a) + 2; |
| 297 | ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), ""); |
| 298 | } |
| 299 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 300 | TEST(DEATHTEST, bzero_fortified2) { |
| 301 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 302 | foo myfoo; |
| 303 | memcpy(myfoo.b, "0123456789", sizeof(myfoo.b)); |
| 304 | size_t n = atoi("11"); |
| 305 | ASSERT_EXIT(bzero(myfoo.b, n), testing::KilledBySignal(SIGABRT), ""); |
| 306 | } |
| 307 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 308 | #endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */ |
| 309 | |
| 310 | #if __BIONIC__ |
| 311 | // multibyte target where we over fill (should fail) |
| 312 | TEST(DEATHTEST, strcpy_fortified) { |
| 313 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 314 | char buf[10]; |
| 315 | char *orig = strdup("0123456789"); |
| 316 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 317 | free(orig); |
| 318 | } |
| 319 | |
| 320 | // zero sized target with "\0" source (should fail) |
| 321 | TEST(DEATHTEST, strcpy2_fortified) { |
| 322 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 323 | char buf[0]; |
| 324 | char *orig = strdup(""); |
| 325 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 326 | free(orig); |
| 327 | } |
| 328 | |
| 329 | // zero sized target with longer source (should fail) |
| 330 | TEST(DEATHTEST, strcpy3_fortified) { |
| 331 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 332 | char buf[0]; |
| 333 | char *orig = strdup("1"); |
| 334 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 335 | free(orig); |
| 336 | } |
| 337 | |
| 338 | // one byte target with longer source (should fail) |
| 339 | TEST(DEATHTEST, strcpy4_fortified) { |
| 340 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 341 | char buf[1]; |
| 342 | char *orig = strdup("12"); |
| 343 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 344 | free(orig); |
| 345 | } |
| 346 | |
| 347 | TEST(DEATHTEST, strlen_fortified) { |
| 348 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 349 | char buf[10]; |
| 350 | memcpy(buf, "0123456789", sizeof(buf)); |
| 351 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), ""); |
| 352 | } |
| 353 | |
| 354 | TEST(DEATHTEST, strchr_fortified) { |
| 355 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 356 | char buf[10]; |
| 357 | memcpy(buf, "0123456789", sizeof(buf)); |
| 358 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
| 359 | } |
| 360 | |
| 361 | TEST(DEATHTEST, strrchr_fortified) { |
| 362 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 363 | char buf[10]; |
| 364 | memcpy(buf, "0123456789", sizeof(buf)); |
| 365 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
| 366 | } |
| 367 | |
| 368 | TEST(DEATHTEST, strlcpy_fortified) { |
| 369 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 370 | char bufa[15]; |
| 371 | char bufb[10]; |
| 372 | strcpy(bufa, "01234567890123"); |
| 373 | size_t n = strlen(bufa); |
| 374 | ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 375 | } |
| 376 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 377 | TEST(DEATHTEST, strlcat_fortified) { |
| 378 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 379 | char bufa[15]; |
| 380 | char bufb[10]; |
| 381 | bufb[0] = '\0'; |
| 382 | strcpy(bufa, "01234567890123"); |
| 383 | size_t n = strlen(bufa); |
| 384 | ASSERT_EXIT(strlcat(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 385 | } |
| 386 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 387 | #endif |
| 388 | |
| 389 | TEST(DEATHTEST, sprintf_fortified) { |
| 390 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 391 | char buf[10]; |
| 392 | char source_buf[15]; |
| 393 | memcpy(source_buf, "12345678901234", 15); |
| 394 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), ""); |
| 395 | } |
| 396 | |
| 397 | TEST(DEATHTEST, sprintf2_fortified) { |
| 398 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 399 | char buf[5]; |
| 400 | ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), ""); |
| 401 | } |
| 402 | |
| 403 | static int vsprintf_helper(const char *fmt, ...) { |
| 404 | char buf[10]; |
| 405 | va_list va; |
| 406 | int result; |
| 407 | |
| 408 | va_start(va, fmt); |
| 409 | result = vsprintf(buf, fmt, va); // should crash here |
| 410 | va_end(va); |
| 411 | return result; |
| 412 | } |
| 413 | |
| 414 | TEST(DEATHTEST, vsprintf_fortified) { |
| 415 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 416 | ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 417 | } |
| 418 | |
| 419 | TEST(DEATHTEST, vsprintf2_fortified) { |
| 420 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 421 | ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 422 | } |
| 423 | |
| 424 | static int vsnprintf_helper(const char *fmt, ...) { |
| 425 | char buf[10]; |
| 426 | va_list va; |
| 427 | int result; |
| 428 | size_t size = atoi("11"); |
| 429 | |
| 430 | va_start(va, fmt); |
| 431 | result = vsnprintf(buf, size, fmt, va); // should crash here |
| 432 | va_end(va); |
| 433 | return result; |
| 434 | } |
| 435 | |
| 436 | TEST(DEATHTEST, vsnprintf_fortified) { |
| 437 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 438 | ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 439 | } |
| 440 | |
| 441 | TEST(DEATHTEST, vsnprintf2_fortified) { |
| 442 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 443 | ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 444 | } |
| 445 | |
| 446 | TEST(DEATHTEST, strncat_fortified) { |
| 447 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 448 | char buf[10]; |
| 449 | size_t n = atoi("10"); // avoid compiler optimizations |
| 450 | strncpy(buf, "012345678", n); |
| 451 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), ""); |
| 452 | } |
| 453 | |
| 454 | TEST(DEATHTEST, strncat2_fortified) { |
| 455 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 456 | char buf[10]; |
| 457 | buf[0] = '\0'; |
| 458 | size_t n = atoi("10"); // avoid compiler optimizations |
| 459 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
| 460 | } |
| 461 | |
| 462 | TEST(DEATHTEST, strcat_fortified) { |
| 463 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 464 | char src[11]; |
| 465 | strcpy(src, "0123456789"); |
| 466 | char buf[10]; |
| 467 | buf[0] = '\0'; |
| 468 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), ""); |
| 469 | } |
| 470 | |
| 471 | TEST(DEATHTEST, memmove_fortified) { |
| 472 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 473 | char buf[20]; |
| 474 | strcpy(buf, "0123456789"); |
| 475 | size_t n = atoi("10"); |
| 476 | ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), ""); |
| 477 | } |
| 478 | |
| 479 | TEST(DEATHTEST, memcpy_fortified) { |
| 480 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 481 | char bufa[10]; |
| 482 | char bufb[10]; |
| 483 | strcpy(bufa, "012345678"); |
| 484 | size_t n = atoi("11"); |
| 485 | ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 486 | } |
| 487 | |
| 488 | TEST(DEATHTEST, strncpy_fortified) { |
| 489 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 490 | char bufa[15]; |
| 491 | char bufb[10]; |
| 492 | strcpy(bufa, "01234567890123"); |
| 493 | size_t n = strlen(bufa); |
| 494 | ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 495 | } |
| 496 | |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 497 | TEST(DEATHTEST, strncpy2_fortified) { |
| 498 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 499 | char dest[11]; |
| 500 | char src[10]; |
| 501 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
| 502 | ASSERT_EXIT(strncpy(dest, src, sizeof(dest)), testing::KilledBySignal(SIGABRT), ""); |
| 503 | } |
| 504 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 505 | TEST(DEATHTEST, snprintf_fortified) { |
| 506 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 507 | char bufa[15]; |
| 508 | char bufb[10]; |
| 509 | strcpy(bufa, "0123456789"); |
| 510 | size_t n = strlen(bufa) + 1; |
| 511 | ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), ""); |
| 512 | } |
| 513 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 514 | TEST(DEATHTEST, bzero_fortified) { |
| 515 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 516 | char buf[10]; |
| 517 | memcpy(buf, "0123456789", sizeof(buf)); |
| 518 | size_t n = atoi("11"); |
| 519 | ASSERT_EXIT(bzero(buf, n), testing::KilledBySignal(SIGABRT), ""); |
| 520 | } |
| 521 | |
| 522 | TEST(DEATHTEST, umask_fortified) { |
| 523 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 524 | mode_t mask = atoi("1023"); // 01777 in octal |
| 525 | ASSERT_EXIT(umask(mask), testing::KilledBySignal(SIGABRT), ""); |
| 526 | } |
| 527 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 528 | extern "C" char* __strncat_chk(char*, const char*, size_t, size_t); |
| 529 | extern "C" char* __strcat_chk(char*, const char*, size_t); |
| 530 | |
| 531 | TEST(TEST_NAME, strncat) { |
| 532 | char buf[10]; |
| 533 | memset(buf, 'A', sizeof(buf)); |
| 534 | buf[0] = 'a'; |
| 535 | buf[1] = '\0'; |
| 536 | char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf)); |
| 537 | ASSERT_EQ(buf, res); |
| 538 | ASSERT_EQ('a', buf[0]); |
| 539 | ASSERT_EQ('0', buf[1]); |
| 540 | ASSERT_EQ('1', buf[2]); |
| 541 | ASSERT_EQ('2', buf[3]); |
| 542 | ASSERT_EQ('3', buf[4]); |
| 543 | ASSERT_EQ('4', buf[5]); |
| 544 | ASSERT_EQ('\0', buf[6]); |
| 545 | ASSERT_EQ('A', buf[7]); |
| 546 | ASSERT_EQ('A', buf[8]); |
| 547 | ASSERT_EQ('A', buf[9]); |
| 548 | } |
| 549 | |
| 550 | TEST(TEST_NAME, strncat2) { |
| 551 | char buf[10]; |
| 552 | memset(buf, 'A', sizeof(buf)); |
| 553 | buf[0] = 'a'; |
| 554 | buf[1] = '\0'; |
| 555 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 556 | ASSERT_EQ(buf, res); |
| 557 | ASSERT_EQ('a', buf[0]); |
| 558 | ASSERT_EQ('0', buf[1]); |
| 559 | ASSERT_EQ('1', buf[2]); |
| 560 | ASSERT_EQ('2', buf[3]); |
| 561 | ASSERT_EQ('3', buf[4]); |
| 562 | ASSERT_EQ('4', buf[5]); |
| 563 | ASSERT_EQ('\0', buf[6]); |
| 564 | ASSERT_EQ('A', buf[7]); |
| 565 | ASSERT_EQ('A', buf[8]); |
| 566 | ASSERT_EQ('A', buf[9]); |
| 567 | } |
| 568 | |
| 569 | TEST(TEST_NAME, strncat3) { |
| 570 | char buf[10]; |
| 571 | memset(buf, 'A', sizeof(buf)); |
| 572 | buf[0] = '\0'; |
| 573 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 574 | ASSERT_EQ(buf, res); |
| 575 | ASSERT_EQ('0', buf[0]); |
| 576 | ASSERT_EQ('1', buf[1]); |
| 577 | ASSERT_EQ('2', buf[2]); |
| 578 | ASSERT_EQ('3', buf[3]); |
| 579 | ASSERT_EQ('4', buf[4]); |
| 580 | ASSERT_EQ('\0', buf[5]); |
| 581 | ASSERT_EQ('A', buf[6]); |
| 582 | ASSERT_EQ('A', buf[7]); |
| 583 | ASSERT_EQ('A', buf[8]); |
| 584 | ASSERT_EQ('A', buf[9]); |
| 585 | } |
| 586 | |
| 587 | TEST(TEST_NAME, strncat4) { |
| 588 | char buf[10]; |
| 589 | memset(buf, 'A', sizeof(buf)); |
| 590 | buf[9] = '\0'; |
| 591 | char* res = __strncat_chk(buf, "", 5, sizeof(buf)); |
| 592 | ASSERT_EQ(buf, res); |
| 593 | ASSERT_EQ('A', buf[0]); |
| 594 | ASSERT_EQ('A', buf[1]); |
| 595 | ASSERT_EQ('A', buf[2]); |
| 596 | ASSERT_EQ('A', buf[3]); |
| 597 | ASSERT_EQ('A', buf[4]); |
| 598 | ASSERT_EQ('A', buf[5]); |
| 599 | ASSERT_EQ('A', buf[6]); |
| 600 | ASSERT_EQ('A', buf[7]); |
| 601 | ASSERT_EQ('A', buf[8]); |
| 602 | ASSERT_EQ('\0', buf[9]); |
| 603 | } |
| 604 | |
| 605 | TEST(TEST_NAME, strncat5) { |
| 606 | char buf[10]; |
| 607 | memset(buf, 'A', sizeof(buf)); |
| 608 | buf[0] = 'a'; |
| 609 | buf[1] = '\0'; |
| 610 | char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf)); |
| 611 | ASSERT_EQ(buf, res); |
| 612 | ASSERT_EQ('a', buf[0]); |
| 613 | ASSERT_EQ('0', buf[1]); |
| 614 | ASSERT_EQ('1', buf[2]); |
| 615 | ASSERT_EQ('2', buf[3]); |
| 616 | ASSERT_EQ('3', buf[4]); |
| 617 | ASSERT_EQ('4', buf[5]); |
| 618 | ASSERT_EQ('5', buf[6]); |
| 619 | ASSERT_EQ('6', buf[7]); |
| 620 | ASSERT_EQ('7', buf[8]); |
| 621 | ASSERT_EQ('\0', buf[9]); |
| 622 | } |
| 623 | |
| 624 | TEST(TEST_NAME, strncat6) { |
| 625 | char buf[10]; |
| 626 | memset(buf, 'A', sizeof(buf)); |
| 627 | buf[0] = 'a'; |
| 628 | buf[1] = '\0'; |
| 629 | char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf)); |
| 630 | ASSERT_EQ(buf, res); |
| 631 | ASSERT_EQ('a', buf[0]); |
| 632 | ASSERT_EQ('0', buf[1]); |
| 633 | ASSERT_EQ('1', buf[2]); |
| 634 | ASSERT_EQ('2', buf[3]); |
| 635 | ASSERT_EQ('3', buf[4]); |
| 636 | ASSERT_EQ('4', buf[5]); |
| 637 | ASSERT_EQ('5', buf[6]); |
| 638 | ASSERT_EQ('6', buf[7]); |
| 639 | ASSERT_EQ('7', buf[8]); |
| 640 | ASSERT_EQ('\0', buf[9]); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | TEST(TEST_NAME, strcat) { |
| 645 | char buf[10]; |
| 646 | memset(buf, 'A', sizeof(buf)); |
| 647 | buf[0] = 'a'; |
| 648 | buf[1] = '\0'; |
| 649 | char* res = __strcat_chk(buf, "01234", 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, strcat2) { |
| 664 | char buf[10]; |
| 665 | memset(buf, 'A', sizeof(buf)); |
| 666 | buf[0] = 'a'; |
| 667 | buf[1] = '\0'; |
| 668 | char* res = __strcat_chk(buf, "01234567", 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('5', buf[6]); |
| 677 | ASSERT_EQ('6', buf[7]); |
| 678 | ASSERT_EQ('7', buf[8]); |
| 679 | ASSERT_EQ('\0', buf[9]); |
| 680 | } |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 681 | |
| 682 | TEST(TEST_NAME, strncpy) { |
| 683 | char src[10]; |
| 684 | char dst[10]; |
| 685 | memcpy(src, "0123456789", sizeof(src)); // non null terminated string |
| 686 | strncpy(dst, src, sizeof(dst)); |
| 687 | ASSERT_EQ('0', dst[0]); |
| 688 | ASSERT_EQ('1', dst[1]); |
| 689 | ASSERT_EQ('2', dst[2]); |
| 690 | ASSERT_EQ('3', dst[3]); |
| 691 | ASSERT_EQ('4', dst[4]); |
| 692 | ASSERT_EQ('5', dst[5]); |
| 693 | ASSERT_EQ('6', dst[6]); |
| 694 | ASSERT_EQ('7', dst[7]); |
| 695 | ASSERT_EQ('8', dst[8]); |
| 696 | ASSERT_EQ('9', dst[9]); |
| 697 | } |
| 698 | |
| 699 | TEST(TEST_NAME, strncpy2) { |
| 700 | char src[10]; |
| 701 | char dst[15]; |
| 702 | memcpy(src, "012345678\0", sizeof(src)); |
| 703 | strncpy(dst, src, sizeof(dst)); |
| 704 | ASSERT_EQ('0', dst[0]); |
| 705 | ASSERT_EQ('1', dst[1]); |
| 706 | ASSERT_EQ('2', dst[2]); |
| 707 | ASSERT_EQ('3', dst[3]); |
| 708 | ASSERT_EQ('4', dst[4]); |
| 709 | ASSERT_EQ('5', dst[5]); |
| 710 | ASSERT_EQ('6', dst[6]); |
| 711 | ASSERT_EQ('7', dst[7]); |
| 712 | ASSERT_EQ('8', dst[8]); |
| 713 | ASSERT_EQ('\0', dst[9]); |
| 714 | ASSERT_EQ('\0', dst[10]); |
| 715 | ASSERT_EQ('\0', dst[11]); |
| 716 | ASSERT_EQ('\0', dst[12]); |
| 717 | ASSERT_EQ('\0', dst[13]); |
| 718 | ASSERT_EQ('\0', dst[14]); |
| 719 | } |
Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame^] | 720 | |
| 721 | TEST(TEST_NAME, strcat_chk_max_int_size) { |
| 722 | char buf[10]; |
| 723 | memset(buf, 'A', sizeof(buf)); |
| 724 | buf[0] = 'a'; |
| 725 | buf[1] = '\0'; |
| 726 | char* res = __strcat_chk(buf, "01234567", (size_t)-1); |
| 727 | ASSERT_EQ(buf, res); |
| 728 | ASSERT_EQ('a', buf[0]); |
| 729 | ASSERT_EQ('0', buf[1]); |
| 730 | ASSERT_EQ('1', buf[2]); |
| 731 | ASSERT_EQ('2', buf[3]); |
| 732 | ASSERT_EQ('3', buf[4]); |
| 733 | ASSERT_EQ('4', buf[5]); |
| 734 | ASSERT_EQ('5', buf[6]); |
| 735 | ASSERT_EQ('6', buf[7]); |
| 736 | ASSERT_EQ('7', buf[8]); |
| 737 | ASSERT_EQ('\0', buf[9]); |
| 738 | } |
| 739 | |
| 740 | extern "C" char* __strcpy_chk(char*, const char*, size_t); |
| 741 | |
| 742 | TEST(TEST_NAME, strcpy_chk_max_int_size) { |
| 743 | char buf[10]; |
| 744 | char* res = __strcpy_chk(buf, "012345678", (size_t)-1); |
| 745 | ASSERT_EQ(buf, res); |
| 746 | ASSERT_EQ('0', buf[0]); |
| 747 | ASSERT_EQ('1', buf[1]); |
| 748 | ASSERT_EQ('2', buf[2]); |
| 749 | ASSERT_EQ('3', buf[3]); |
| 750 | ASSERT_EQ('4', buf[4]); |
| 751 | ASSERT_EQ('5', buf[5]); |
| 752 | ASSERT_EQ('6', buf[6]); |
| 753 | ASSERT_EQ('7', buf[7]); |
| 754 | ASSERT_EQ('8', buf[8]); |
| 755 | ASSERT_EQ('\0', buf[9]); |
| 756 | } |
| 757 | |
| 758 | extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t); |
| 759 | |
| 760 | TEST(TEST_NAME, memcpy_chk_max_int_size) { |
| 761 | char buf[10]; |
| 762 | void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1); |
| 763 | ASSERT_EQ((void*)buf, res); |
| 764 | ASSERT_EQ('0', buf[0]); |
| 765 | ASSERT_EQ('1', buf[1]); |
| 766 | ASSERT_EQ('2', buf[2]); |
| 767 | ASSERT_EQ('3', buf[3]); |
| 768 | ASSERT_EQ('4', buf[4]); |
| 769 | ASSERT_EQ('5', buf[5]); |
| 770 | ASSERT_EQ('6', buf[6]); |
| 771 | ASSERT_EQ('7', buf[7]); |
| 772 | ASSERT_EQ('8', buf[8]); |
| 773 | ASSERT_EQ('\0', buf[9]); |
| 774 | } |