Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -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 empty[0]; |
| 25 | char one[1]; |
| 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. |
| 32 | TEST(Fortify2_Clang_DeathTest, strncat3_fortified2) { |
| 33 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 34 | foo myfoo; |
| 35 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 36 | myfoo.b[0] = '\0'; |
| 37 | size_t n = atoi("10"); // avoid compiler optimizations |
| 38 | ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), ""); |
| 39 | } |
| 40 | |
| 41 | TEST(Fortify2_Clang_DeathTest, strcat2_fortified2) { |
| 42 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 43 | foo myfoo; |
| 44 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 45 | myfoo.b[0] = '\0'; |
| 46 | ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), ""); |
| 47 | } |
| 48 | |
| 49 | /*****************************************************************/ |
| 50 | /* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test_clang.cpp */ |
| 51 | /*****************************************************************/ |
| 52 | |
| 53 | #if __BIONIC__ |
| 54 | // multibyte target where we over fill (should fail) |
| 55 | TEST(Fortify2_Clang_DeathTest, strcpy_fortified) { |
| 56 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 57 | char buf[10]; |
| 58 | char *orig = strdup("0123456789"); |
| 59 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 60 | free(orig); |
| 61 | } |
| 62 | |
| 63 | // zero sized target with "\0" source (should fail) |
| 64 | TEST(Fortify2_Clang_DeathTest, strcpy2_fortified) { |
| 65 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 66 | char buf[0]; |
| 67 | char *orig = strdup(""); |
| 68 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 69 | free(orig); |
| 70 | } |
| 71 | |
| 72 | // zero sized target with longer source (should fail) |
| 73 | TEST(Fortify2_Clang_DeathTest, strcpy3_fortified) { |
| 74 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 75 | char buf[0]; |
| 76 | char *orig = strdup("1"); |
| 77 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 78 | free(orig); |
| 79 | } |
| 80 | |
| 81 | // one byte target with longer source (should fail) |
| 82 | TEST(Fortify2_Clang_DeathTest, strcpy4_fortified) { |
| 83 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 84 | char buf[1]; |
| 85 | char *orig = strdup("12"); |
| 86 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), ""); |
| 87 | free(orig); |
| 88 | } |
| 89 | |
| 90 | TEST(Fortify2_Clang_DeathTest, strlen_fortified) { |
| 91 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 92 | char buf[10]; |
| 93 | memcpy(buf, "0123456789", sizeof(buf)); |
| 94 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), ""); |
| 95 | } |
| 96 | |
| 97 | TEST(Fortify2_Clang_DeathTest, strchr_fortified) { |
| 98 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 99 | char buf[10]; |
| 100 | memcpy(buf, "0123456789", sizeof(buf)); |
| 101 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
| 102 | } |
| 103 | |
| 104 | TEST(Fortify2_Clang_DeathTest, strrchr_fortified) { |
| 105 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 106 | char buf[10]; |
| 107 | memcpy(buf, "0123456789", sizeof(buf)); |
| 108 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), ""); |
| 109 | } |
| 110 | #endif |
| 111 | |
Nick Kralevich | c6eb985 | 2013-06-24 11:44:00 -0700 | [diff] [blame^] | 112 | TEST(Fortify2_Clang_DeathTest, sprintf_fortified) { |
| 113 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 114 | char buf[10]; |
| 115 | char source_buf[15]; |
| 116 | memcpy(source_buf, "12345678901234", 15); |
| 117 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), ""); |
| 118 | } |
| 119 | |
| 120 | TEST(Fortify2_Clang_DeathTest, sprintf2_fortified) { |
| 121 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 122 | char buf[5]; |
| 123 | ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), ""); |
| 124 | } |
| 125 | |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 126 | TEST(Fortify2_Clang_DeathTest, strncat_fortified) { |
| 127 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 128 | char buf[10]; |
| 129 | size_t n = atoi("10"); // avoid compiler optimizations |
| 130 | strncpy(buf, "012345678", n); |
| 131 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), ""); |
| 132 | } |
| 133 | |
| 134 | TEST(Fortify2_Clang_DeathTest, strncat2_fortified) { |
| 135 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 136 | char buf[10]; |
| 137 | buf[0] = '\0'; |
| 138 | size_t n = atoi("10"); // avoid compiler optimizations |
| 139 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
| 140 | } |
| 141 | |
| 142 | TEST(Fortify2_Clang_DeathTest, strcat_fortified) { |
| 143 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 144 | char src[11]; |
| 145 | strcpy(src, "0123456789"); |
| 146 | char buf[10]; |
| 147 | buf[0] = '\0'; |
| 148 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), ""); |
| 149 | } |
| 150 | |
| 151 | TEST(Fortify2_Clang_DeathTest, memmove_fortified) { |
| 152 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 153 | char buf[20]; |
| 154 | strcpy(buf, "0123456789"); |
| 155 | size_t n = atoi("10"); |
| 156 | ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), ""); |
| 157 | } |
| 158 | |
| 159 | TEST(Fortify2_Clang_DeathTest, memcpy_fortified) { |
| 160 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 161 | char bufa[10]; |
| 162 | char bufb[10]; |
| 163 | strcpy(bufa, "012345678"); |
| 164 | size_t n = atoi("11"); |
| 165 | ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 166 | } |
| 167 | |
| 168 | TEST(Fortify2_Clang_DeathTest, strncpy_fortified) { |
| 169 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 170 | char bufa[15]; |
| 171 | char bufb[10]; |
| 172 | strcpy(bufa, "01234567890123"); |
| 173 | size_t n = strlen(bufa); |
| 174 | ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 175 | } |
| 176 | |
| 177 | __BIONIC_FORTIFY_INLINE |
| 178 | size_t test_fortify2_inline(char* buf) { |
Nick Kralevich | 3cd4cac | 2013-06-19 10:25:44 -0700 | [diff] [blame] | 179 | return __bos(buf); |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST(Fortify2_Clang, fortify_inline) { |
| 183 | char buf[1024]; |
Nick Kralevich | 3cd4cac | 2013-06-19 10:25:44 -0700 | [diff] [blame] | 184 | // no-op. Prints nothing. Needed to prevent the compiler |
| 185 | // from optimizing out buf. |
| 186 | buf[0] = '\0'; |
| 187 | printf("%s", buf); |
| 188 | ASSERT_EQ(sizeof(buf), test_fortify2_inline(buf)); |
Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame] | 189 | } |