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 { |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 24 | char empty[0]; |
| 25 | char one[1]; |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 26 | char a[10]; |
| 27 | char b[10]; |
| 28 | }; |
| 29 | |
| 30 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 31 | // in its own process. |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 32 | TEST(Fortify2_DeathTest, strncpy_fortified2) { |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 33 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 34 | foo myfoo; |
| 35 | int copy_amt = atoi("11"); |
| 36 | ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 37 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 40 | TEST(Fortify2_DeathTest, sprintf_fortified2) { |
| 41 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 42 | foo myfoo; |
| 43 | char source_buf[15]; |
| 44 | memcpy(source_buf, "12345678901234", 15); |
| 45 | ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 46 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 49 | #if __BIONIC__ |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 50 | // zero sized target with "\0" source (should fail) |
| 51 | TEST(Fortify2_DeathTest, strcpy_fortified2) { |
| 52 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 53 | foo myfoo; |
| 54 | char* src = strdup(""); |
| 55 | ASSERT_EXIT(strcpy(myfoo.empty, src), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 56 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 57 | free(src); |
| 58 | } |
| 59 | |
| 60 | // zero sized target with longer source (should fail) |
| 61 | TEST(Fortify2_DeathTest, strcpy2_fortified2) { |
| 62 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 63 | foo myfoo; |
| 64 | char* src = strdup("1"); |
| 65 | ASSERT_EXIT(strcpy(myfoo.empty, src), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 66 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 67 | free(src); |
| 68 | } |
| 69 | |
| 70 | // one byte target with longer source (should fail) |
| 71 | TEST(Fortify2_DeathTest, strcpy3_fortified2) { |
| 72 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 73 | foo myfoo; |
| 74 | char* src = strdup("12"); |
| 75 | ASSERT_EXIT(strcpy(myfoo.one, src), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 76 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 77 | free(src); |
| 78 | } |
| 79 | |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 80 | TEST(Fortify2_DeathTest, strchr_fortified2) { |
| 81 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 82 | foo myfoo; |
| 83 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 84 | myfoo.b[0] = '\0'; |
| 85 | ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 86 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Nick Kralevich | 277226b | 2013-05-01 15:05:01 -0700 | [diff] [blame] | 89 | TEST(Fortify2_DeathTest, strrchr_fortified2) { |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 90 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 91 | foo myfoo; |
| 92 | memcpy(myfoo.a, "0123456789", 10); |
| 93 | memcpy(myfoo.b, "01234", 6); |
| 94 | ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 95 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 96 | } |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame^] | 97 | |
| 98 | TEST(Fortify2_DeathTest, strlcpy_fortified2) { |
| 99 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 100 | foo myfoo; |
| 101 | strcpy(myfoo.a, "01"); |
| 102 | size_t n = strlen(myfoo.a); |
| 103 | ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n), |
| 104 | testing::KilledBySignal(SIGABRT), ""); |
| 105 | } |
| 106 | |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 107 | #endif |
| 108 | |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 109 | TEST(Fortify2_DeathTest, strncat_fortified2) { |
| 110 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 111 | foo myfoo; |
| 112 | size_t n = atoi("10"); // avoid compiler optimizations |
| 113 | strncpy(myfoo.a, "012345678", n); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 114 | ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(Fortify2_DeathTest, strncat2_fortified2) { |
| 118 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 119 | foo myfoo; |
| 120 | myfoo.a[0] = '\0'; |
| 121 | size_t n = atoi("10"); // avoid compiler optimizations |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 122 | ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 125 | TEST(Fortify2_DeathTest, strncat3_fortified2) { |
| 126 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 127 | foo myfoo; |
| 128 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 129 | myfoo.b[0] = '\0'; |
| 130 | size_t n = atoi("10"); // avoid compiler optimizations |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 131 | ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST(Fortify2_DeathTest, strcat_fortified2) { |
| 135 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 136 | char src[11]; |
| 137 | strcpy(src, "0123456789"); |
| 138 | foo myfoo; |
| 139 | myfoo.a[0] = '\0'; |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 140 | ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST(Fortify2_DeathTest, strcat2_fortified2) { |
| 144 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 145 | foo myfoo; |
| 146 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 147 | myfoo.b[0] = '\0'; |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 148 | ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 151 | /***********************************************************/ |
| 152 | /* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */ |
| 153 | /***********************************************************/ |
| 154 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 155 | #if __BIONIC__ |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 156 | // multibyte target where we over fill (should fail) |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 157 | TEST(Fortify2_DeathTest, strcpy_fortified) { |
| 158 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 159 | char buf[10]; |
| 160 | char *orig = strdup("0123456789"); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 161 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 162 | free(orig); |
| 163 | } |
| 164 | |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 165 | // zero sized target with "\0" source (should fail) |
| 166 | TEST(Fortify2_DeathTest, strcpy2_fortified) { |
| 167 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 168 | char buf[0]; |
| 169 | char *orig = strdup(""); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 170 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 171 | free(orig); |
| 172 | } |
| 173 | |
| 174 | // zero sized target with longer source (should fail) |
| 175 | TEST(Fortify2_DeathTest, strcpy3_fortified) { |
| 176 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 177 | char buf[0]; |
| 178 | char *orig = strdup("1"); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 179 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 180 | free(orig); |
| 181 | } |
| 182 | |
| 183 | // one byte target with longer source (should fail) |
| 184 | TEST(Fortify2_DeathTest, strcpy4_fortified) { |
| 185 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 186 | char buf[1]; |
| 187 | char *orig = strdup("12"); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 188 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 189 | free(orig); |
| 190 | } |
| 191 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 192 | TEST(Fortify2_DeathTest, strlen_fortified) { |
| 193 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 194 | char buf[10]; |
| 195 | memcpy(buf, "0123456789", sizeof(buf)); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 196 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST(Fortify2_DeathTest, strchr_fortified) { |
| 200 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 201 | char buf[10]; |
| 202 | memcpy(buf, "0123456789", sizeof(buf)); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 203 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | TEST(Fortify2_DeathTest, strrchr_fortified) { |
| 207 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 208 | char buf[10]; |
| 209 | memcpy(buf, "0123456789", sizeof(buf)); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 210 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 211 | } |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame^] | 212 | |
| 213 | TEST(Fortify2_DeathTest, strlcpy_fortified) { |
| 214 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 215 | char bufa[15]; |
| 216 | char bufb[10]; |
| 217 | strcpy(bufa, "01234567890123"); |
| 218 | size_t n = strlen(bufa); |
| 219 | ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 220 | } |
| 221 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 222 | #endif |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 223 | |
| 224 | TEST(Fortify2_DeathTest, sprintf_fortified) { |
| 225 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 226 | char buf[10]; |
| 227 | char source_buf[15]; |
| 228 | memcpy(source_buf, "12345678901234", 15); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 229 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 230 | } |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 231 | |
| 232 | TEST(Fortify2_DeathTest, strncat_fortified) { |
| 233 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 234 | char buf[10]; |
| 235 | size_t n = atoi("10"); // avoid compiler optimizations |
| 236 | strncpy(buf, "012345678", n); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 237 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST(Fortify2_DeathTest, strncat2_fortified) { |
| 241 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 242 | char buf[10]; |
| 243 | buf[0] = '\0'; |
| 244 | size_t n = atoi("10"); // avoid compiler optimizations |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 245 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 246 | } |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 247 | |
| 248 | TEST(Fortify2_DeathTest, strcat_fortified) { |
| 249 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 250 | char src[11]; |
| 251 | strcpy(src, "0123456789"); |
| 252 | char buf[10]; |
| 253 | buf[0] = '\0'; |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 254 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 255 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 256 | |
| 257 | TEST(Fortify2_DeathTest, memmove_fortified) { |
| 258 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 259 | char buf[20]; |
| 260 | strcpy(buf, "0123456789"); |
| 261 | size_t n = atoi("10"); |
| 262 | ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), ""); |
| 263 | } |
| 264 | |
| 265 | TEST(Fortify2_DeathTest, memcpy_fortified) { |
| 266 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 267 | char bufa[10]; |
| 268 | char bufb[10]; |
| 269 | strcpy(bufa, "012345678"); |
| 270 | size_t n = atoi("11"); |
| 271 | ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 272 | } |
| 273 | |
| 274 | TEST(Fortify2_DeathTest, strncpy_fortified) { |
| 275 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 276 | char bufa[15]; |
| 277 | char bufb[10]; |
| 278 | strcpy(bufa, "01234567890123"); |
| 279 | size_t n = strlen(bufa); |
| 280 | ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 281 | } |