Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -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 | #undef _FORTIFY_SOURCE |
| 18 | #define _FORTIFY_SOURCE 2 |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | struct foo { |
| 24 | char a[10]; |
| 25 | char b[10]; |
| 26 | }; |
| 27 | |
| 28 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 29 | // in its own process. |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 30 | TEST(Fortify2_DeathTest, strncpy_fortified2) { |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 31 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 32 | foo myfoo; |
| 33 | int copy_amt = atoi("11"); |
| 34 | ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt), |
| 35 | testing::KilledBySignal(SIGSEGV), ""); |
| 36 | } |
| 37 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 38 | TEST(Fortify2_DeathTest, sprintf_fortified2) { |
| 39 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 40 | foo myfoo; |
| 41 | char source_buf[15]; |
| 42 | memcpy(source_buf, "12345678901234", 15); |
| 43 | ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf), |
| 44 | testing::KilledBySignal(SIGSEGV), ""); |
| 45 | } |
| 46 | |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 47 | #if __BIONIC__ |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 48 | TEST(Fortify2_DeathTest, strchr_fortified2) { |
| 49 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 50 | foo myfoo; |
| 51 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 52 | myfoo.b[0] = '\0'; |
| 53 | ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')), |
| 54 | testing::KilledBySignal(SIGSEGV), ""); |
| 55 | } |
| 56 | |
Nick Kralevich | 277226b | 2013-05-01 15:05:01 -0700 | [diff] [blame] | 57 | TEST(Fortify2_DeathTest, strrchr_fortified2) { |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 58 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 59 | foo myfoo; |
| 60 | memcpy(myfoo.a, "0123456789", 10); |
| 61 | memcpy(myfoo.b, "01234", 6); |
| 62 | ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')), |
| 63 | testing::KilledBySignal(SIGSEGV), ""); |
| 64 | } |
| 65 | #endif |
| 66 | |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 67 | TEST(Fortify2_DeathTest, strncat_fortified2) { |
| 68 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 69 | foo myfoo; |
| 70 | size_t n = atoi("10"); // avoid compiler optimizations |
| 71 | strncpy(myfoo.a, "012345678", n); |
| 72 | ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGSEGV), ""); |
| 73 | } |
| 74 | |
| 75 | TEST(Fortify2_DeathTest, strncat2_fortified2) { |
| 76 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 77 | foo myfoo; |
| 78 | myfoo.a[0] = '\0'; |
| 79 | size_t n = atoi("10"); // avoid compiler optimizations |
| 80 | ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGSEGV), ""); |
| 81 | } |
| 82 | |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame^] | 83 | TEST(Fortify2_DeathTest, strncat3_fortified2) { |
| 84 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 85 | foo myfoo; |
| 86 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 87 | myfoo.b[0] = '\0'; |
| 88 | size_t n = atoi("10"); // avoid compiler optimizations |
| 89 | ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGSEGV), ""); |
| 90 | } |
| 91 | |
| 92 | TEST(Fortify2_DeathTest, strcat_fortified2) { |
| 93 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 94 | char src[11]; |
| 95 | strcpy(src, "0123456789"); |
| 96 | foo myfoo; |
| 97 | myfoo.a[0] = '\0'; |
| 98 | ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGSEGV), ""); |
| 99 | } |
| 100 | |
| 101 | TEST(Fortify2_DeathTest, strcat2_fortified2) { |
| 102 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 103 | foo myfoo; |
| 104 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 105 | myfoo.b[0] = '\0'; |
| 106 | ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGSEGV), ""); |
| 107 | } |
| 108 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 109 | /***********************************************************/ |
| 110 | /* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */ |
| 111 | /***********************************************************/ |
| 112 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 113 | #if __BIONIC__ |
| 114 | TEST(Fortify2_DeathTest, strcpy_fortified) { |
| 115 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 116 | char buf[10]; |
| 117 | char *orig = strdup("0123456789"); |
| 118 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); |
| 119 | free(orig); |
| 120 | } |
| 121 | |
| 122 | TEST(Fortify2_DeathTest, strlen_fortified) { |
| 123 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 124 | char buf[10]; |
| 125 | memcpy(buf, "0123456789", sizeof(buf)); |
| 126 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), ""); |
| 127 | } |
| 128 | |
| 129 | TEST(Fortify2_DeathTest, strchr_fortified) { |
| 130 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 131 | char buf[10]; |
| 132 | memcpy(buf, "0123456789", sizeof(buf)); |
| 133 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); |
| 134 | } |
| 135 | |
| 136 | TEST(Fortify2_DeathTest, strrchr_fortified) { |
| 137 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 138 | char buf[10]; |
| 139 | memcpy(buf, "0123456789", sizeof(buf)); |
| 140 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); |
| 141 | } |
| 142 | #endif |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 143 | |
| 144 | TEST(Fortify2_DeathTest, sprintf_fortified) { |
| 145 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 146 | char buf[10]; |
| 147 | char source_buf[15]; |
| 148 | memcpy(source_buf, "12345678901234", 15); |
| 149 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), ""); |
| 150 | } |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 151 | |
| 152 | TEST(Fortify2_DeathTest, strncat_fortified) { |
| 153 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 154 | char buf[10]; |
| 155 | size_t n = atoi("10"); // avoid compiler optimizations |
| 156 | strncpy(buf, "012345678", n); |
| 157 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGSEGV), ""); |
| 158 | } |
| 159 | |
| 160 | TEST(Fortify2_DeathTest, strncat2_fortified) { |
| 161 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 162 | char buf[10]; |
| 163 | buf[0] = '\0'; |
| 164 | size_t n = atoi("10"); // avoid compiler optimizations |
| 165 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGSEGV), ""); |
| 166 | } |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame^] | 167 | |
| 168 | TEST(Fortify2_DeathTest, strcat_fortified) { |
| 169 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 170 | char src[11]; |
| 171 | strcpy(src, "0123456789"); |
| 172 | char buf[10]; |
| 173 | buf[0] = '\0'; |
| 174 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGSEGV), ""); |
| 175 | } |