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> |
Nick Kralevich | c8ae8bd | 2013-06-27 08:58:14 -0700 | [diff] [blame^] | 22 | #include <stdarg.h> |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 23 | |
| 24 | struct foo { |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 25 | char empty[0]; |
| 26 | char one[1]; |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 27 | char a[10]; |
| 28 | char b[10]; |
| 29 | }; |
| 30 | |
| 31 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 32 | // in its own process. |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 33 | TEST(Fortify2_DeathTest, strncpy_fortified2) { |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 34 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 35 | foo myfoo; |
| 36 | int copy_amt = atoi("11"); |
| 37 | ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 38 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 41 | TEST(Fortify2_DeathTest, sprintf_fortified2) { |
| 42 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 43 | foo myfoo; |
| 44 | char source_buf[15]; |
| 45 | memcpy(source_buf, "12345678901234", 15); |
| 46 | ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 47 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 50 | TEST(Fortify2_DeathTest, sprintf2_fortified2) { |
| 51 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 52 | foo myfoo; |
| 53 | ASSERT_EXIT(sprintf(myfoo.a, "0123456789"), |
| 54 | testing::KilledBySignal(SIGABRT), ""); |
| 55 | } |
| 56 | |
Nick Kralevich | c8ae8bd | 2013-06-27 08:58:14 -0700 | [diff] [blame^] | 57 | static int vsprintf_helper2(const char *fmt, ...) { |
| 58 | foo myfoo; |
| 59 | va_list va; |
| 60 | int result; |
| 61 | |
| 62 | va_start(va, fmt); |
| 63 | result = vsprintf(myfoo.a, fmt, va); // should crash here |
| 64 | va_end(va); |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | TEST(Fortify2_DeathTest, vsprintf_fortified2) { |
| 69 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 70 | ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 71 | } |
| 72 | |
| 73 | TEST(Fortify2_DeathTest, vsprintf2_fortified2) { |
| 74 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 75 | ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 76 | } |
| 77 | |
| 78 | static int vsnprintf_helper2(const char *fmt, ...) { |
| 79 | foo myfoo; |
| 80 | va_list va; |
| 81 | int result; |
| 82 | size_t size = atoi("11"); |
| 83 | |
| 84 | va_start(va, fmt); |
| 85 | result = vsnprintf(myfoo.a, size, fmt, va); // should crash here |
| 86 | va_end(va); |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | TEST(Fortify2_DeathTest, vsnprintf_fortified2) { |
| 91 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 92 | ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 93 | } |
| 94 | |
| 95 | TEST(Fortify2_DeathTest, vsnprintf2_fortified2) { |
| 96 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 97 | ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 98 | } |
| 99 | |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 100 | #if __BIONIC__ |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 101 | // zero sized target with "\0" source (should fail) |
| 102 | TEST(Fortify2_DeathTest, strcpy_fortified2) { |
| 103 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 104 | foo myfoo; |
| 105 | char* src = strdup(""); |
| 106 | ASSERT_EXIT(strcpy(myfoo.empty, src), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 107 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 108 | free(src); |
| 109 | } |
| 110 | |
| 111 | // zero sized target with longer source (should fail) |
| 112 | TEST(Fortify2_DeathTest, strcpy2_fortified2) { |
| 113 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 114 | foo myfoo; |
| 115 | char* src = strdup("1"); |
| 116 | ASSERT_EXIT(strcpy(myfoo.empty, src), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 117 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 118 | free(src); |
| 119 | } |
| 120 | |
| 121 | // one byte target with longer source (should fail) |
| 122 | TEST(Fortify2_DeathTest, strcpy3_fortified2) { |
| 123 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 124 | foo myfoo; |
| 125 | char* src = strdup("12"); |
| 126 | ASSERT_EXIT(strcpy(myfoo.one, src), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 127 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 128 | free(src); |
| 129 | } |
| 130 | |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 131 | TEST(Fortify2_DeathTest, strchr_fortified2) { |
| 132 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 133 | foo myfoo; |
| 134 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 135 | myfoo.b[0] = '\0'; |
| 136 | ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 137 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Nick Kralevich | 277226b | 2013-05-01 15:05:01 -0700 | [diff] [blame] | 140 | TEST(Fortify2_DeathTest, strrchr_fortified2) { |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 141 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 142 | foo myfoo; |
| 143 | memcpy(myfoo.a, "0123456789", 10); |
| 144 | memcpy(myfoo.b, "01234", 6); |
| 145 | ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')), |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 146 | testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 147 | } |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame] | 148 | |
| 149 | TEST(Fortify2_DeathTest, strlcpy_fortified2) { |
| 150 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 151 | foo myfoo; |
| 152 | strcpy(myfoo.a, "01"); |
| 153 | size_t n = strlen(myfoo.a); |
| 154 | ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n), |
| 155 | testing::KilledBySignal(SIGABRT), ""); |
| 156 | } |
| 157 | |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 158 | #endif |
| 159 | |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 160 | TEST(Fortify2_DeathTest, strncat_fortified2) { |
| 161 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 162 | foo myfoo; |
| 163 | size_t n = atoi("10"); // avoid compiler optimizations |
| 164 | strncpy(myfoo.a, "012345678", n); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 165 | ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST(Fortify2_DeathTest, strncat2_fortified2) { |
| 169 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 170 | foo myfoo; |
| 171 | myfoo.a[0] = '\0'; |
| 172 | size_t n = atoi("10"); // avoid compiler optimizations |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 173 | ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 176 | TEST(Fortify2_DeathTest, strncat3_fortified2) { |
| 177 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 178 | foo myfoo; |
| 179 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 180 | myfoo.b[0] = '\0'; |
| 181 | size_t n = atoi("10"); // avoid compiler optimizations |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 182 | ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST(Fortify2_DeathTest, strcat_fortified2) { |
| 186 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 187 | char src[11]; |
| 188 | strcpy(src, "0123456789"); |
| 189 | foo myfoo; |
| 190 | myfoo.a[0] = '\0'; |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 191 | ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | TEST(Fortify2_DeathTest, strcat2_fortified2) { |
| 195 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 196 | foo myfoo; |
| 197 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 198 | myfoo.b[0] = '\0'; |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 199 | ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 202 | TEST(Fortify2_DeathTest, snprintf_fortified2) { |
| 203 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 204 | foo myfoo; |
| 205 | strcpy(myfoo.a, "012345678"); |
| 206 | size_t n = strlen(myfoo.a) + 2; |
| 207 | ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), ""); |
| 208 | } |
| 209 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 210 | /***********************************************************/ |
| 211 | /* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */ |
| 212 | /***********************************************************/ |
| 213 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 214 | #if __BIONIC__ |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 215 | // multibyte target where we over fill (should fail) |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 216 | TEST(Fortify2_DeathTest, strcpy_fortified) { |
| 217 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 218 | char buf[10]; |
| 219 | char *orig = strdup("0123456789"); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 220 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 221 | free(orig); |
| 222 | } |
| 223 | |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 224 | // zero sized target with "\0" source (should fail) |
| 225 | TEST(Fortify2_DeathTest, strcpy2_fortified) { |
| 226 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 227 | char buf[0]; |
| 228 | char *orig = strdup(""); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 229 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 230 | free(orig); |
| 231 | } |
| 232 | |
| 233 | // zero sized target with longer source (should fail) |
| 234 | TEST(Fortify2_DeathTest, strcpy3_fortified) { |
| 235 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 236 | char buf[0]; |
| 237 | char *orig = strdup("1"); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 238 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 239 | free(orig); |
| 240 | } |
| 241 | |
| 242 | // one byte target with longer source (should fail) |
| 243 | TEST(Fortify2_DeathTest, strcpy4_fortified) { |
| 244 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 245 | char buf[1]; |
| 246 | char *orig = strdup("12"); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 247 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 248 | free(orig); |
| 249 | } |
| 250 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 251 | TEST(Fortify2_DeathTest, strlen_fortified) { |
| 252 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 253 | char buf[10]; |
| 254 | memcpy(buf, "0123456789", sizeof(buf)); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 255 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | TEST(Fortify2_DeathTest, strchr_fortified) { |
| 259 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 260 | char buf[10]; |
| 261 | memcpy(buf, "0123456789", sizeof(buf)); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 262 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST(Fortify2_DeathTest, strrchr_fortified) { |
| 266 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 267 | char buf[10]; |
| 268 | memcpy(buf, "0123456789", sizeof(buf)); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 269 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 270 | } |
Nick Kralevich | 8bafa74 | 2013-06-20 12:17:44 -0700 | [diff] [blame] | 271 | |
| 272 | TEST(Fortify2_DeathTest, strlcpy_fortified) { |
| 273 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 274 | char bufa[15]; |
| 275 | char bufb[10]; |
| 276 | strcpy(bufa, "01234567890123"); |
| 277 | size_t n = strlen(bufa); |
| 278 | ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 279 | } |
| 280 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 281 | #endif |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 282 | |
| 283 | TEST(Fortify2_DeathTest, sprintf_fortified) { |
| 284 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 285 | char buf[10]; |
| 286 | char source_buf[15]; |
| 287 | memcpy(source_buf, "12345678901234", 15); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 288 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 289 | } |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 290 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame] | 291 | TEST(Fortify2_DeathTest, sprintf2_fortified) { |
| 292 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 293 | char buf[5]; |
| 294 | ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), ""); |
| 295 | } |
| 296 | |
Nick Kralevich | c8ae8bd | 2013-06-27 08:58:14 -0700 | [diff] [blame^] | 297 | static int vsprintf_helper(const char *fmt, ...) { |
| 298 | char buf[10]; |
| 299 | va_list va; |
| 300 | int result; |
| 301 | |
| 302 | va_start(va, fmt); |
| 303 | result = vsprintf(buf, fmt, va); // should crash here |
| 304 | va_end(va); |
| 305 | return result; |
| 306 | } |
| 307 | |
| 308 | TEST(Fortify2_DeathTest, vsprintf_fortified) { |
| 309 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 310 | ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 311 | } |
| 312 | |
| 313 | TEST(Fortify2_DeathTest, vsprintf2_fortified) { |
| 314 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 315 | ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 316 | } |
| 317 | |
| 318 | static int vsnprintf_helper(const char *fmt, ...) { |
| 319 | char buf[10]; |
| 320 | va_list va; |
| 321 | int result; |
| 322 | size_t size = atoi("11"); |
| 323 | |
| 324 | va_start(va, fmt); |
| 325 | result = vsnprintf(buf, size, fmt, va); // should crash here |
| 326 | va_end(va); |
| 327 | return result; |
| 328 | } |
| 329 | |
| 330 | TEST(Fortify2_DeathTest, vsnprintf_fortified) { |
| 331 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 332 | ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 333 | } |
| 334 | |
| 335 | TEST(Fortify2_DeathTest, vsnprintf2_fortified) { |
| 336 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 337 | ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), ""); |
| 338 | } |
| 339 | |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 340 | TEST(Fortify2_DeathTest, strncat_fortified) { |
| 341 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 342 | char buf[10]; |
| 343 | size_t n = atoi("10"); // avoid compiler optimizations |
| 344 | strncpy(buf, "012345678", n); |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 345 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | TEST(Fortify2_DeathTest, strncat2_fortified) { |
| 349 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 350 | char buf[10]; |
| 351 | buf[0] = '\0'; |
| 352 | size_t n = atoi("10"); // avoid compiler optimizations |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 353 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame] | 354 | } |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 355 | |
| 356 | TEST(Fortify2_DeathTest, strcat_fortified) { |
| 357 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 358 | char src[11]; |
| 359 | strcpy(src, "0123456789"); |
| 360 | char buf[10]; |
| 361 | buf[0] = '\0'; |
Nick Kralevich | fd0325b | 2013-06-11 15:45:23 -0700 | [diff] [blame] | 362 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), ""); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 363 | } |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 364 | |
| 365 | TEST(Fortify2_DeathTest, memmove_fortified) { |
| 366 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 367 | char buf[20]; |
| 368 | strcpy(buf, "0123456789"); |
| 369 | size_t n = atoi("10"); |
| 370 | ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), ""); |
| 371 | } |
| 372 | |
| 373 | TEST(Fortify2_DeathTest, memcpy_fortified) { |
| 374 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 375 | char bufa[10]; |
| 376 | char bufb[10]; |
| 377 | strcpy(bufa, "012345678"); |
| 378 | size_t n = atoi("11"); |
| 379 | ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 380 | } |
| 381 | |
| 382 | TEST(Fortify2_DeathTest, strncpy_fortified) { |
| 383 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 384 | char bufa[15]; |
| 385 | char bufb[10]; |
| 386 | strcpy(bufa, "01234567890123"); |
| 387 | size_t n = strlen(bufa); |
| 388 | ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 389 | } |
Nick Kralevich | 621b19d | 2013-06-25 10:02:35 -0700 | [diff] [blame] | 390 | |
| 391 | TEST(Fortify2_DeathTest, snprintf_fortified) { |
| 392 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 393 | char bufa[15]; |
| 394 | char bufb[10]; |
| 395 | strcpy(bufa, "0123456789"); |
| 396 | size_t n = strlen(bufa) + 1; |
| 397 | ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), ""); |
| 398 | } |