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"); |
| 32 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); |
| 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(""); |
| 41 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); |
| 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"); |
| 50 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); |
| 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"); |
| 59 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); |
| 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)); |
| 67 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), ""); |
| 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)); |
| 74 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); |
| 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)); |
| 81 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); |
| 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); |
| 90 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), ""); |
| 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); |
| 98 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGSEGV), ""); |
| 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 |
| 106 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGSEGV), ""); |
| 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'; |
| 115 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGSEGV), ""); |
| 116 | } |
| 117 | |
| 118 | extern "C" char* __strncat_chk(char*, const char*, size_t, size_t); |
| 119 | extern "C" char* __strcat_chk(char*, const char*, size_t); |
| 120 | |
| 121 | TEST(Fortify1, strncat) { |
| 122 | char buf[10]; |
| 123 | memset(buf, 'A', sizeof(buf)); |
| 124 | buf[0] = 'a'; |
| 125 | buf[1] = '\0'; |
| 126 | char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf)); |
| 127 | ASSERT_EQ(buf, res); |
| 128 | ASSERT_EQ('a', buf[0]); |
| 129 | ASSERT_EQ('0', buf[1]); |
| 130 | ASSERT_EQ('1', buf[2]); |
| 131 | ASSERT_EQ('2', buf[3]); |
| 132 | ASSERT_EQ('3', buf[4]); |
| 133 | ASSERT_EQ('4', buf[5]); |
| 134 | ASSERT_EQ('\0', buf[6]); |
| 135 | ASSERT_EQ('A', buf[7]); |
| 136 | ASSERT_EQ('A', buf[8]); |
| 137 | ASSERT_EQ('A', buf[9]); |
| 138 | } |
| 139 | |
| 140 | TEST(Fortify1, strncat2) { |
| 141 | char buf[10]; |
| 142 | memset(buf, 'A', sizeof(buf)); |
| 143 | buf[0] = 'a'; |
| 144 | buf[1] = '\0'; |
| 145 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 146 | ASSERT_EQ(buf, res); |
| 147 | ASSERT_EQ('a', buf[0]); |
| 148 | ASSERT_EQ('0', buf[1]); |
| 149 | ASSERT_EQ('1', buf[2]); |
| 150 | ASSERT_EQ('2', buf[3]); |
| 151 | ASSERT_EQ('3', buf[4]); |
| 152 | ASSERT_EQ('4', buf[5]); |
| 153 | ASSERT_EQ('\0', buf[6]); |
| 154 | ASSERT_EQ('A', buf[7]); |
| 155 | ASSERT_EQ('A', buf[8]); |
| 156 | ASSERT_EQ('A', buf[9]); |
| 157 | } |
| 158 | |
| 159 | TEST(Fortify1, strncat3) { |
| 160 | char buf[10]; |
| 161 | memset(buf, 'A', sizeof(buf)); |
| 162 | buf[0] = '\0'; |
| 163 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 164 | ASSERT_EQ(buf, res); |
| 165 | ASSERT_EQ('0', buf[0]); |
| 166 | ASSERT_EQ('1', buf[1]); |
| 167 | ASSERT_EQ('2', buf[2]); |
| 168 | ASSERT_EQ('3', buf[3]); |
| 169 | ASSERT_EQ('4', buf[4]); |
| 170 | ASSERT_EQ('\0', buf[5]); |
| 171 | ASSERT_EQ('A', buf[6]); |
| 172 | ASSERT_EQ('A', buf[7]); |
| 173 | ASSERT_EQ('A', buf[8]); |
| 174 | ASSERT_EQ('A', buf[9]); |
| 175 | } |
| 176 | |
| 177 | TEST(Fortify1, strncat4) { |
| 178 | char buf[10]; |
| 179 | memset(buf, 'A', sizeof(buf)); |
| 180 | buf[9] = '\0'; |
| 181 | char* res = __strncat_chk(buf, "", 5, sizeof(buf)); |
| 182 | ASSERT_EQ(buf, res); |
| 183 | ASSERT_EQ('A', buf[0]); |
| 184 | ASSERT_EQ('A', buf[1]); |
| 185 | ASSERT_EQ('A', buf[2]); |
| 186 | ASSERT_EQ('A', buf[3]); |
| 187 | ASSERT_EQ('A', buf[4]); |
| 188 | ASSERT_EQ('A', buf[5]); |
| 189 | ASSERT_EQ('A', buf[6]); |
| 190 | ASSERT_EQ('A', buf[7]); |
| 191 | ASSERT_EQ('A', buf[8]); |
| 192 | ASSERT_EQ('\0', buf[9]); |
| 193 | } |
| 194 | |
| 195 | TEST(Fortify1, strncat5) { |
| 196 | char buf[10]; |
| 197 | memset(buf, 'A', sizeof(buf)); |
| 198 | buf[0] = 'a'; |
| 199 | buf[1] = '\0'; |
| 200 | char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf)); |
| 201 | ASSERT_EQ(buf, res); |
| 202 | ASSERT_EQ('a', buf[0]); |
| 203 | ASSERT_EQ('0', buf[1]); |
| 204 | ASSERT_EQ('1', buf[2]); |
| 205 | ASSERT_EQ('2', buf[3]); |
| 206 | ASSERT_EQ('3', buf[4]); |
| 207 | ASSERT_EQ('4', buf[5]); |
| 208 | ASSERT_EQ('5', buf[6]); |
| 209 | ASSERT_EQ('6', buf[7]); |
| 210 | ASSERT_EQ('7', buf[8]); |
| 211 | ASSERT_EQ('\0', buf[9]); |
| 212 | } |
| 213 | |
| 214 | TEST(Fortify1, strncat6) { |
| 215 | char buf[10]; |
| 216 | memset(buf, 'A', sizeof(buf)); |
| 217 | buf[0] = 'a'; |
| 218 | buf[1] = '\0'; |
| 219 | char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf)); |
| 220 | ASSERT_EQ(buf, res); |
| 221 | ASSERT_EQ('a', buf[0]); |
| 222 | ASSERT_EQ('0', buf[1]); |
| 223 | ASSERT_EQ('1', buf[2]); |
| 224 | ASSERT_EQ('2', buf[3]); |
| 225 | ASSERT_EQ('3', buf[4]); |
| 226 | ASSERT_EQ('4', buf[5]); |
| 227 | ASSERT_EQ('5', buf[6]); |
| 228 | ASSERT_EQ('6', buf[7]); |
| 229 | ASSERT_EQ('7', buf[8]); |
| 230 | ASSERT_EQ('\0', buf[9]); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | TEST(Fortify1, strcat) { |
| 235 | char buf[10]; |
| 236 | memset(buf, 'A', sizeof(buf)); |
| 237 | buf[0] = 'a'; |
| 238 | buf[1] = '\0'; |
| 239 | char* res = __strcat_chk(buf, "01234", sizeof(buf)); |
| 240 | ASSERT_EQ(buf, res); |
| 241 | ASSERT_EQ('a', buf[0]); |
| 242 | ASSERT_EQ('0', buf[1]); |
| 243 | ASSERT_EQ('1', buf[2]); |
| 244 | ASSERT_EQ('2', buf[3]); |
| 245 | ASSERT_EQ('3', buf[4]); |
| 246 | ASSERT_EQ('4', buf[5]); |
| 247 | ASSERT_EQ('\0', buf[6]); |
| 248 | ASSERT_EQ('A', buf[7]); |
| 249 | ASSERT_EQ('A', buf[8]); |
| 250 | ASSERT_EQ('A', buf[9]); |
| 251 | } |
| 252 | |
| 253 | TEST(Fortify1, strcat2) { |
| 254 | char buf[10]; |
| 255 | memset(buf, 'A', sizeof(buf)); |
| 256 | buf[0] = 'a'; |
| 257 | buf[1] = '\0'; |
| 258 | char* res = __strcat_chk(buf, "01234567", sizeof(buf)); |
| 259 | ASSERT_EQ(buf, res); |
| 260 | ASSERT_EQ('a', buf[0]); |
| 261 | ASSERT_EQ('0', buf[1]); |
| 262 | ASSERT_EQ('1', buf[2]); |
| 263 | ASSERT_EQ('2', buf[3]); |
| 264 | ASSERT_EQ('3', buf[4]); |
| 265 | ASSERT_EQ('4', buf[5]); |
| 266 | ASSERT_EQ('5', buf[6]); |
| 267 | ASSERT_EQ('6', buf[7]); |
| 268 | ASSERT_EQ('7', buf[8]); |
| 269 | ASSERT_EQ('\0', buf[9]); |
| 270 | } |