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