Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -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 | #include <gtest/gtest.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 18 | #include "BionicDeathTest.h" |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 19 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <malloc.h> |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 22 | #include <poll.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 23 | #include <signal.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 29 | #include <time.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 31 | #if __BIONIC__ |
| 32 | #define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "FORTIFY") |
| 33 | #else |
| 34 | #define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "") |
| 35 | #endif |
| 36 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 37 | // Fortify test code needs to run multiple times, so TEST_NAME macro is used to |
| 38 | // distinguish different tests. TEST_NAME is defined in compilation command. |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 39 | #define DEATHTEST_PASTER(name) name##_DeathTest |
| 40 | #define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name) |
| 41 | #define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME) |
| 42 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 43 | class DEATHTEST : public BionicDeathTest {}; |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 44 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 45 | #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2 |
| 46 | struct foo { |
| 47 | char empty[0]; |
| 48 | char one[1]; |
| 49 | char a[10]; |
| 50 | char b[10]; |
| 51 | }; |
| 52 | |
| 53 | #ifndef __clang__ |
| 54 | // This test is disabled in clang because clang doesn't properly detect |
| 55 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 56 | TEST_F(DEATHTEST, stpncpy_fortified2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 57 | foo myfoo; |
| 58 | int copy_amt = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 59 | ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 60 | } |
| 61 | #endif |
| 62 | |
| 63 | #ifndef __clang__ |
| 64 | // This test is disabled in clang because clang doesn't properly detect |
| 65 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 66 | TEST_F(DEATHTEST, stpncpy2_fortified2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 67 | foo myfoo; |
| 68 | memset(&myfoo, 0, sizeof(myfoo)); |
| 69 | myfoo.one[0] = 'A'; // not null terminated string |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 70 | ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b))); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #ifndef __clang__ |
| 75 | // This test is disabled in clang because clang doesn't properly detect |
| 76 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 77 | TEST_F(DEATHTEST, strncpy_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 78 | foo myfoo; |
| 79 | int copy_amt = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 80 | ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 81 | } |
| 82 | #endif |
| 83 | |
| 84 | #ifndef __clang__ |
| 85 | // This test is disabled in clang because clang doesn't properly detect |
| 86 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 87 | TEST_F(DEATHTEST, strncpy2_fortified2) { |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 88 | foo myfoo; |
| 89 | memset(&myfoo, 0, sizeof(myfoo)); |
| 90 | myfoo.one[0] = 'A'; // not null terminated string |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 91 | ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b))); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 92 | } |
| 93 | #endif |
| 94 | |
| 95 | #ifndef __clang__ |
| 96 | // This test is disabled in clang because clang doesn't properly detect |
| 97 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 98 | TEST_F(DEATHTEST, sprintf_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 99 | foo myfoo; |
| 100 | char source_buf[15]; |
| 101 | memcpy(source_buf, "12345678901234", 15); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 102 | ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 103 | } |
| 104 | #endif |
| 105 | |
| 106 | #ifndef __clang__ |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 107 | // This test is disabled in clang because clang doesn't properly detect |
| 108 | // this buffer overflow. TODO: Fix clang. |
| 109 | TEST_F(DEATHTEST, sprintf2_fortified2) { |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 110 | foo myfoo; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 111 | ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789")); |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 112 | } |
| 113 | #endif |
| 114 | |
| 115 | #ifndef __clang__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 116 | // These tests are disabled in clang because clang doesn't properly detect |
| 117 | // this buffer overflow. TODO: Fix clang. |
| 118 | static int vsprintf_helper2(const char *fmt, ...) { |
| 119 | foo myfoo; |
| 120 | va_list va; |
| 121 | int result; |
| 122 | |
| 123 | va_start(va, fmt); |
| 124 | result = vsprintf(myfoo.a, fmt, va); // should crash here |
| 125 | va_end(va); |
| 126 | return result; |
| 127 | } |
| 128 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 129 | TEST_F(DEATHTEST, vsprintf_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 130 | ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 133 | TEST_F(DEATHTEST, vsprintf2_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 134 | ASSERT_FORTIFY(vsprintf_helper2("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #ifndef __clang__ |
| 139 | // These tests are disabled in clang because clang doesn't properly detect |
| 140 | // this buffer overflow. TODO: Fix clang. |
| 141 | static int vsnprintf_helper2(const char *fmt, ...) { |
| 142 | foo myfoo; |
| 143 | va_list va; |
| 144 | int result; |
| 145 | size_t size = atoi("11"); |
| 146 | |
| 147 | va_start(va, fmt); |
| 148 | result = vsnprintf(myfoo.a, size, fmt, va); // should crash here |
| 149 | va_end(va); |
| 150 | return result; |
| 151 | } |
| 152 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 153 | TEST_F(DEATHTEST, vsnprintf_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 154 | ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 157 | TEST_F(DEATHTEST, vsnprintf2_fortified2) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 158 | ASSERT_FORTIFY(vsnprintf_helper2("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 159 | } |
| 160 | #endif |
| 161 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 162 | #ifndef __clang__ |
| 163 | // zero sized target with "\0" source (should fail) |
| 164 | // This test is disabled in clang because clang doesn't properly detect |
| 165 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 166 | TEST_F(DEATHTEST, stpcpy_fortified2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 167 | #if defined(__BIONIC__) |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 168 | foo myfoo; |
| 169 | char* src = strdup(""); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 170 | ASSERT_FORTIFY(stpcpy(myfoo.empty, src)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 171 | free(src); |
| 172 | #else // __BIONIC__ |
| 173 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 174 | #endif // __BIONIC__ |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #ifndef __clang__ |
| 179 | // zero sized target with "\0" source (should fail) |
| 180 | // This test is disabled in clang because clang doesn't properly detect |
| 181 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 182 | TEST_F(DEATHTEST, strcpy_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 183 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 184 | foo myfoo; |
| 185 | char* src = strdup(""); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 186 | ASSERT_FORTIFY(strcpy(myfoo.empty, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 187 | free(src); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 188 | #else // __BIONIC__ |
| 189 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 190 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #ifndef __clang__ |
| 195 | // zero sized target with longer source (should fail) |
| 196 | // This test is disabled in clang because clang doesn't properly detect |
| 197 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 198 | TEST_F(DEATHTEST, strcpy2_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 199 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 200 | foo myfoo; |
| 201 | char* src = strdup("1"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 202 | ASSERT_FORTIFY(strcpy(myfoo.empty, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 203 | free(src); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 204 | #else // __BIONIC__ |
| 205 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 206 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 207 | } |
| 208 | #endif |
| 209 | |
| 210 | #ifndef __clang__ |
| 211 | // one byte target with longer source (should fail) |
| 212 | // This test is disabled in clang because clang doesn't properly detect |
| 213 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 214 | TEST_F(DEATHTEST, strcpy3_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 215 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 216 | foo myfoo; |
| 217 | char* src = strdup("12"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 218 | ASSERT_FORTIFY(strcpy(myfoo.one, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 219 | free(src); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 220 | #else // __BIONIC__ |
| 221 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 222 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 223 | } |
| 224 | #endif |
| 225 | |
| 226 | #ifndef __clang__ |
| 227 | // This test is disabled in clang because clang doesn't properly detect |
| 228 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 229 | TEST_F(DEATHTEST, strchr_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 230 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 231 | foo myfoo; |
| 232 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 233 | myfoo.b[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 234 | ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 235 | #else // __BIONIC__ |
| 236 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 237 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 238 | } |
| 239 | #endif |
| 240 | |
| 241 | #ifndef __clang__ |
| 242 | // This test is disabled in clang because clang doesn't properly detect |
| 243 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 244 | TEST_F(DEATHTEST, strrchr_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 245 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 246 | foo myfoo; |
| 247 | memcpy(myfoo.a, "0123456789", 10); |
| 248 | memcpy(myfoo.b, "01234", 6); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 249 | ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 250 | #else // __BIONIC__ |
| 251 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 252 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 253 | } |
| 254 | #endif |
| 255 | |
| 256 | #ifndef __clang__ |
| 257 | // This test is disabled in clang because clang doesn't properly detect |
| 258 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 259 | TEST_F(DEATHTEST, strlcpy_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 260 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 261 | foo myfoo; |
| 262 | strcpy(myfoo.a, "01"); |
| 263 | size_t n = strlen(myfoo.a); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 264 | ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 265 | #else // __BIONIC__ |
| 266 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 267 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 268 | } |
| 269 | #endif |
| 270 | |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 271 | #ifndef __clang__ |
| 272 | // This test is disabled in clang because clang doesn't properly detect |
| 273 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 274 | TEST_F(DEATHTEST, strlcat_fortified2) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 275 | #if defined(__BIONIC__) |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 276 | foo myfoo; |
| 277 | strcpy(myfoo.a, "01"); |
| 278 | myfoo.one[0] = '\0'; |
| 279 | size_t n = strlen(myfoo.a); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 280 | ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 281 | #else // __BIONIC__ |
| 282 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 283 | #endif // __BIONIC__ |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 284 | } |
| 285 | #endif |
| 286 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 287 | #ifndef __clang__ |
| 288 | // This test is disabled in clang because clang doesn't properly detect |
| 289 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 290 | TEST_F(DEATHTEST, strncat_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 291 | foo myfoo; |
| 292 | size_t n = atoi("10"); // avoid compiler optimizations |
| 293 | strncpy(myfoo.a, "012345678", n); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 294 | ASSERT_FORTIFY(strncat(myfoo.a, "9", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 295 | } |
| 296 | #endif |
| 297 | |
| 298 | #ifndef __clang__ |
| 299 | // This test is disabled in clang because clang doesn't properly detect |
| 300 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 301 | TEST_F(DEATHTEST, strncat2_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 302 | foo myfoo; |
| 303 | myfoo.a[0] = '\0'; |
| 304 | size_t n = atoi("10"); // avoid compiler optimizations |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 305 | ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 306 | } |
| 307 | #endif |
| 308 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 309 | TEST_F(DEATHTEST, strncat3_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 310 | foo myfoo; |
| 311 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 312 | myfoo.b[0] = '\0'; |
| 313 | size_t n = atoi("10"); // avoid compiler optimizations |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 314 | ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | #ifndef __clang__ |
| 318 | // This test is disabled in clang because clang doesn't properly detect |
| 319 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 320 | TEST_F(DEATHTEST, strcat_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 321 | char src[11]; |
| 322 | strcpy(src, "0123456789"); |
| 323 | foo myfoo; |
| 324 | myfoo.a[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 325 | ASSERT_FORTIFY(strcat(myfoo.a, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 326 | } |
| 327 | #endif |
| 328 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 329 | TEST_F(DEATHTEST, strcat2_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 330 | foo myfoo; |
| 331 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string |
| 332 | myfoo.b[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 333 | ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 336 | TEST_F(DEATHTEST, snprintf_fortified2) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 337 | foo myfoo; |
| 338 | strcpy(myfoo.a, "012345678"); |
| 339 | size_t n = strlen(myfoo.a) + 2; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 340 | ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 343 | TEST_F(DEATHTEST, bzero_fortified2) { |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 344 | foo myfoo; |
| 345 | memcpy(myfoo.b, "0123456789", sizeof(myfoo.b)); |
| 346 | size_t n = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 347 | ASSERT_FORTIFY(bzero(myfoo.b, n)); |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 350 | #endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */ |
| 351 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 352 | // multibyte target where we over fill (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 353 | TEST_F(DEATHTEST, strcpy_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 354 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 355 | char buf[10]; |
| 356 | char *orig = strdup("0123456789"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 357 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 358 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 359 | #else // __BIONIC__ |
| 360 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 361 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // zero sized target with "\0" source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 365 | TEST_F(DEATHTEST, strcpy2_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 366 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 367 | char buf[0]; |
| 368 | char *orig = strdup(""); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 369 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 370 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 371 | #else // __BIONIC__ |
| 372 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 373 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // zero sized target with longer source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 377 | TEST_F(DEATHTEST, strcpy3_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 378 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 379 | char buf[0]; |
| 380 | char *orig = strdup("1"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 381 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 382 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 383 | #else // __BIONIC__ |
| 384 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 385 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // one byte target with longer source (should fail) |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 389 | TEST_F(DEATHTEST, strcpy4_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 390 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 391 | char buf[1]; |
| 392 | char *orig = strdup("12"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 393 | ASSERT_FORTIFY(strcpy(buf, orig)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 394 | free(orig); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 395 | #else // __BIONIC__ |
| 396 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 397 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 400 | TEST_F(DEATHTEST, strlen_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 401 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 402 | char buf[10]; |
| 403 | memcpy(buf, "0123456789", sizeof(buf)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 404 | ASSERT_FORTIFY(printf("%zd", strlen(buf))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 405 | #else // __BIONIC__ |
| 406 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 407 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 410 | TEST_F(DEATHTEST, strchr_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 411 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 412 | char buf[10]; |
| 413 | memcpy(buf, "0123456789", sizeof(buf)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 414 | ASSERT_FORTIFY(printf("%s", strchr(buf, 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 415 | #else // __BIONIC__ |
| 416 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 417 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 420 | TEST_F(DEATHTEST, strrchr_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 421 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 422 | char buf[10]; |
| 423 | memcpy(buf, "0123456789", sizeof(buf)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 424 | ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a'))); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 425 | #else // __BIONIC__ |
| 426 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 427 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 430 | TEST_F(DEATHTEST, strlcpy_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 431 | #if defined(__BIONIC__) |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 432 | char bufa[15]; |
| 433 | char bufb[10]; |
| 434 | strcpy(bufa, "01234567890123"); |
| 435 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 436 | ASSERT_FORTIFY(strlcpy(bufb, bufa, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 437 | #else // __BIONIC__ |
| 438 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 439 | #endif // __BIONIC__ |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 442 | TEST_F(DEATHTEST, strlcat_fortified) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 443 | #if defined(__BIONIC__) |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 444 | char bufa[15]; |
| 445 | char bufb[10]; |
| 446 | bufb[0] = '\0'; |
| 447 | strcpy(bufa, "01234567890123"); |
| 448 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 449 | ASSERT_FORTIFY(strlcat(bufb, bufa, n)); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 450 | #else // __BIONIC__ |
| 451 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 452 | #endif // __BIONIC__ |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 455 | TEST_F(DEATHTEST, sprintf_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 456 | char buf[10]; |
| 457 | char source_buf[15]; |
| 458 | memcpy(source_buf, "12345678901234", 15); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 459 | ASSERT_FORTIFY(sprintf(buf, "%s", source_buf)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 462 | #ifndef __clang__ |
| 463 | // This test is disabled in clang because clang doesn't properly detect |
| 464 | // this buffer overflow. TODO: Fix clang. |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 465 | TEST_F(DEATHTEST, sprintf_malloc_fortified) { |
Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 466 | char* buf = (char *) malloc(10); |
| 467 | char source_buf[11]; |
| 468 | memcpy(source_buf, "1234567890", 11); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 469 | ASSERT_FORTIFY(sprintf(buf, "%s", source_buf)); |
Nick Kralevich | b91791d | 2013-10-02 14:14:40 -0700 | [diff] [blame] | 470 | free(buf); |
| 471 | } |
| 472 | #endif |
| 473 | |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 474 | TEST_F(DEATHTEST, sprintf2_fortified) { |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 475 | char buf[5]; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 476 | ASSERT_FORTIFY(sprintf(buf, "aaaaa")); |
Nick Kralevich | 884a3de | 2014-10-06 00:39:47 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 479 | static int vsprintf_helper(const char *fmt, ...) { |
| 480 | char buf[10]; |
| 481 | va_list va; |
| 482 | int result; |
| 483 | |
| 484 | va_start(va, fmt); |
| 485 | result = vsprintf(buf, fmt, va); // should crash here |
| 486 | va_end(va); |
| 487 | return result; |
| 488 | } |
| 489 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 490 | TEST_F(DEATHTEST, vsprintf_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 491 | ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 494 | TEST_F(DEATHTEST, vsprintf2_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 495 | ASSERT_FORTIFY(vsprintf_helper("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static int vsnprintf_helper(const char *fmt, ...) { |
| 499 | char buf[10]; |
| 500 | va_list va; |
| 501 | int result; |
| 502 | size_t size = atoi("11"); |
| 503 | |
| 504 | va_start(va, fmt); |
| 505 | result = vsnprintf(buf, size, fmt, va); // should crash here |
| 506 | va_end(va); |
| 507 | return result; |
| 508 | } |
| 509 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 510 | TEST_F(DEATHTEST, vsnprintf_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 511 | ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 514 | TEST_F(DEATHTEST, vsnprintf2_fortified) { |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 515 | ASSERT_FORTIFY(vsnprintf_helper("0123456789")); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 518 | TEST_F(DEATHTEST, strncat_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 519 | char buf[10]; |
| 520 | size_t n = atoi("10"); // avoid compiler optimizations |
| 521 | strncpy(buf, "012345678", n); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 522 | ASSERT_FORTIFY(strncat(buf, "9", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 525 | TEST_F(DEATHTEST, strncat2_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 526 | char buf[10]; |
| 527 | buf[0] = '\0'; |
| 528 | size_t n = atoi("10"); // avoid compiler optimizations |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 529 | ASSERT_FORTIFY(strncat(buf, "0123456789", n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 532 | TEST_F(DEATHTEST, strcat_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 533 | char src[11]; |
| 534 | strcpy(src, "0123456789"); |
| 535 | char buf[10]; |
| 536 | buf[0] = '\0'; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 537 | ASSERT_FORTIFY(strcat(buf, src)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 540 | TEST_F(DEATHTEST, memmove_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 541 | char buf[20]; |
| 542 | strcpy(buf, "0123456789"); |
| 543 | size_t n = atoi("10"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 544 | ASSERT_FORTIFY(memmove(buf + 11, buf, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 547 | TEST_F(DEATHTEST, memcpy_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 548 | char bufa[10]; |
| 549 | char bufb[10]; |
| 550 | strcpy(bufa, "012345678"); |
| 551 | size_t n = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 552 | ASSERT_FORTIFY(memcpy(bufb, bufa, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 555 | TEST_F(DEATHTEST, stpncpy_fortified) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 556 | char bufa[15]; |
| 557 | char bufb[10]; |
| 558 | strcpy(bufa, "01234567890123"); |
| 559 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 560 | ASSERT_FORTIFY(stpncpy(bufb, bufa, n)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 563 | TEST_F(DEATHTEST, stpncpy2_fortified) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 564 | char dest[11]; |
| 565 | char src[10]; |
| 566 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 567 | ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest))); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 570 | TEST_F(DEATHTEST, strncpy_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 571 | char bufa[15]; |
| 572 | char bufb[10]; |
| 573 | strcpy(bufa, "01234567890123"); |
| 574 | size_t n = strlen(bufa); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 575 | ASSERT_FORTIFY(strncpy(bufb, bufa, n)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 578 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 579 | TEST_F(DEATHTEST, strncpy2_fortified) { |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 580 | char dest[11]; |
| 581 | char src[10]; |
| 582 | memcpy(src, "0123456789", sizeof(src)); // src is not null terminated |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 583 | ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest))); |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 586 | TEST_F(DEATHTEST, snprintf_fortified) { |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 587 | char bufa[15]; |
| 588 | char bufb[10]; |
| 589 | strcpy(bufa, "0123456789"); |
| 590 | size_t n = strlen(bufa) + 1; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 591 | ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa)); |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 594 | TEST_F(DEATHTEST, bzero_fortified) { |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 595 | char buf[10]; |
| 596 | memcpy(buf, "0123456789", sizeof(buf)); |
| 597 | size_t n = atoi("11"); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 598 | ASSERT_FORTIFY(bzero(buf, n)); |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 601 | TEST_F(DEATHTEST, umask_fortified) { |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 602 | mode_t mask = atoi("1023"); // 01777 in octal |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 603 | ASSERT_FORTIFY(umask(mask)); |
Nick Kralevich | a6cde39 | 2013-06-29 08:15:25 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 606 | TEST_F(DEATHTEST, recv_fortified) { |
Nick Kralevich | 60f4f9a | 2013-09-24 16:32:07 -0700 | [diff] [blame] | 607 | size_t data_len = atoi("11"); // suppress compiler optimizations |
| 608 | char buf[10]; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 609 | ASSERT_FORTIFY(recv(0, buf, data_len, 0)); |
Nick Kralevich | 60f4f9a | 2013-09-24 16:32:07 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 612 | TEST_F(DEATHTEST, FD_ISSET_fortified) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 613 | #if defined(__BIONIC__) // glibc catches this at compile-time. |
Nick Kralevich | 90201d5 | 2013-10-02 16:11:30 -0700 | [diff] [blame] | 614 | fd_set set; |
| 615 | memset(&set, 0, sizeof(set)); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 616 | ASSERT_FORTIFY(FD_ISSET(-1, &set)); |
Elliott Hughes | 409588c | 2014-04-23 23:02:43 -0700 | [diff] [blame] | 617 | #endif |
Nick Kralevich | 90201d5 | 2013-10-02 16:11:30 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 620 | TEST_F(DEATHTEST, FD_ISSET_2_fortified) { |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 621 | char buf[1]; |
| 622 | fd_set* set = (fd_set*) buf; |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 623 | ASSERT_FORTIFY(FD_ISSET(0, set)); |
Nick Kralevich | 7943df6 | 2013-10-03 14:08:39 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame^] | 626 | TEST_F(DEATHTEST, pread_fortified) { |
| 627 | char buf[1]; |
| 628 | size_t ct = atoi("2"); // prevent optimizations |
| 629 | int fd = open("/dev/null", O_RDONLY); |
| 630 | ASSERT_FORTIFY(pread(fd, buf, ct, 0)); |
| 631 | close(fd); |
| 632 | } |
| 633 | |
| 634 | TEST_F(DEATHTEST, pread64_fortified) { |
| 635 | char buf[1]; |
| 636 | size_t ct = atoi("2"); // prevent optimizations |
| 637 | int fd = open("/dev/null", O_RDONLY); |
| 638 | ASSERT_FORTIFY(pread64(fd, buf, ct, 0)); |
| 639 | close(fd); |
| 640 | } |
| 641 | |
Nick Kralevich | be0e43b | 2014-07-23 13:56:23 -0700 | [diff] [blame] | 642 | TEST_F(DEATHTEST, read_fortified) { |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 643 | char buf[1]; |
| 644 | size_t ct = atoi("2"); // prevent optimizations |
| 645 | int fd = open("/dev/null", O_RDONLY); |
Elliott Hughes | d036e94 | 2015-02-02 11:18:58 -0800 | [diff] [blame] | 646 | ASSERT_FORTIFY(read(fd, buf, ct)); |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 647 | close(fd); |
| 648 | } |
| 649 | |
Nick Kralevich | 5bcf398 | 2013-06-28 10:34:09 -0700 | [diff] [blame] | 650 | extern "C" char* __strncat_chk(char*, const char*, size_t, size_t); |
| 651 | extern "C" char* __strcat_chk(char*, const char*, size_t); |
| 652 | |
| 653 | TEST(TEST_NAME, strncat) { |
| 654 | char buf[10]; |
| 655 | memset(buf, 'A', sizeof(buf)); |
| 656 | buf[0] = 'a'; |
| 657 | buf[1] = '\0'; |
| 658 | char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf)); |
| 659 | ASSERT_EQ(buf, res); |
| 660 | ASSERT_EQ('a', buf[0]); |
| 661 | ASSERT_EQ('0', buf[1]); |
| 662 | ASSERT_EQ('1', buf[2]); |
| 663 | ASSERT_EQ('2', buf[3]); |
| 664 | ASSERT_EQ('3', buf[4]); |
| 665 | ASSERT_EQ('4', buf[5]); |
| 666 | ASSERT_EQ('\0', buf[6]); |
| 667 | ASSERT_EQ('A', buf[7]); |
| 668 | ASSERT_EQ('A', buf[8]); |
| 669 | ASSERT_EQ('A', buf[9]); |
| 670 | } |
| 671 | |
| 672 | TEST(TEST_NAME, strncat2) { |
| 673 | char buf[10]; |
| 674 | memset(buf, 'A', sizeof(buf)); |
| 675 | buf[0] = 'a'; |
| 676 | buf[1] = '\0'; |
| 677 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 678 | ASSERT_EQ(buf, res); |
| 679 | ASSERT_EQ('a', buf[0]); |
| 680 | ASSERT_EQ('0', buf[1]); |
| 681 | ASSERT_EQ('1', buf[2]); |
| 682 | ASSERT_EQ('2', buf[3]); |
| 683 | ASSERT_EQ('3', buf[4]); |
| 684 | ASSERT_EQ('4', buf[5]); |
| 685 | ASSERT_EQ('\0', buf[6]); |
| 686 | ASSERT_EQ('A', buf[7]); |
| 687 | ASSERT_EQ('A', buf[8]); |
| 688 | ASSERT_EQ('A', buf[9]); |
| 689 | } |
| 690 | |
| 691 | TEST(TEST_NAME, strncat3) { |
| 692 | char buf[10]; |
| 693 | memset(buf, 'A', sizeof(buf)); |
| 694 | buf[0] = '\0'; |
| 695 | char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf)); |
| 696 | ASSERT_EQ(buf, res); |
| 697 | ASSERT_EQ('0', buf[0]); |
| 698 | ASSERT_EQ('1', buf[1]); |
| 699 | ASSERT_EQ('2', buf[2]); |
| 700 | ASSERT_EQ('3', buf[3]); |
| 701 | ASSERT_EQ('4', buf[4]); |
| 702 | ASSERT_EQ('\0', buf[5]); |
| 703 | ASSERT_EQ('A', buf[6]); |
| 704 | ASSERT_EQ('A', buf[7]); |
| 705 | ASSERT_EQ('A', buf[8]); |
| 706 | ASSERT_EQ('A', buf[9]); |
| 707 | } |
| 708 | |
| 709 | TEST(TEST_NAME, strncat4) { |
| 710 | char buf[10]; |
| 711 | memset(buf, 'A', sizeof(buf)); |
| 712 | buf[9] = '\0'; |
| 713 | char* res = __strncat_chk(buf, "", 5, sizeof(buf)); |
| 714 | ASSERT_EQ(buf, res); |
| 715 | ASSERT_EQ('A', buf[0]); |
| 716 | ASSERT_EQ('A', buf[1]); |
| 717 | ASSERT_EQ('A', buf[2]); |
| 718 | ASSERT_EQ('A', buf[3]); |
| 719 | ASSERT_EQ('A', buf[4]); |
| 720 | ASSERT_EQ('A', buf[5]); |
| 721 | ASSERT_EQ('A', buf[6]); |
| 722 | ASSERT_EQ('A', buf[7]); |
| 723 | ASSERT_EQ('A', buf[8]); |
| 724 | ASSERT_EQ('\0', buf[9]); |
| 725 | } |
| 726 | |
| 727 | TEST(TEST_NAME, strncat5) { |
| 728 | char buf[10]; |
| 729 | memset(buf, 'A', sizeof(buf)); |
| 730 | buf[0] = 'a'; |
| 731 | buf[1] = '\0'; |
| 732 | char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf)); |
| 733 | ASSERT_EQ(buf, res); |
| 734 | ASSERT_EQ('a', buf[0]); |
| 735 | ASSERT_EQ('0', buf[1]); |
| 736 | ASSERT_EQ('1', buf[2]); |
| 737 | ASSERT_EQ('2', buf[3]); |
| 738 | ASSERT_EQ('3', buf[4]); |
| 739 | ASSERT_EQ('4', buf[5]); |
| 740 | ASSERT_EQ('5', buf[6]); |
| 741 | ASSERT_EQ('6', buf[7]); |
| 742 | ASSERT_EQ('7', buf[8]); |
| 743 | ASSERT_EQ('\0', buf[9]); |
| 744 | } |
| 745 | |
| 746 | TEST(TEST_NAME, strncat6) { |
| 747 | char buf[10]; |
| 748 | memset(buf, 'A', sizeof(buf)); |
| 749 | buf[0] = 'a'; |
| 750 | buf[1] = '\0'; |
| 751 | char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf)); |
| 752 | ASSERT_EQ(buf, res); |
| 753 | ASSERT_EQ('a', buf[0]); |
| 754 | ASSERT_EQ('0', buf[1]); |
| 755 | ASSERT_EQ('1', buf[2]); |
| 756 | ASSERT_EQ('2', buf[3]); |
| 757 | ASSERT_EQ('3', buf[4]); |
| 758 | ASSERT_EQ('4', buf[5]); |
| 759 | ASSERT_EQ('5', buf[6]); |
| 760 | ASSERT_EQ('6', buf[7]); |
| 761 | ASSERT_EQ('7', buf[8]); |
| 762 | ASSERT_EQ('\0', buf[9]); |
| 763 | } |
| 764 | |
| 765 | |
| 766 | TEST(TEST_NAME, strcat) { |
| 767 | char buf[10]; |
| 768 | memset(buf, 'A', sizeof(buf)); |
| 769 | buf[0] = 'a'; |
| 770 | buf[1] = '\0'; |
| 771 | char* res = __strcat_chk(buf, "01234", sizeof(buf)); |
| 772 | ASSERT_EQ(buf, res); |
| 773 | ASSERT_EQ('a', buf[0]); |
| 774 | ASSERT_EQ('0', buf[1]); |
| 775 | ASSERT_EQ('1', buf[2]); |
| 776 | ASSERT_EQ('2', buf[3]); |
| 777 | ASSERT_EQ('3', buf[4]); |
| 778 | ASSERT_EQ('4', buf[5]); |
| 779 | ASSERT_EQ('\0', buf[6]); |
| 780 | ASSERT_EQ('A', buf[7]); |
| 781 | ASSERT_EQ('A', buf[8]); |
| 782 | ASSERT_EQ('A', buf[9]); |
| 783 | } |
| 784 | |
| 785 | TEST(TEST_NAME, strcat2) { |
| 786 | char buf[10]; |
| 787 | memset(buf, 'A', sizeof(buf)); |
| 788 | buf[0] = 'a'; |
| 789 | buf[1] = '\0'; |
| 790 | char* res = __strcat_chk(buf, "01234567", sizeof(buf)); |
| 791 | ASSERT_EQ(buf, res); |
| 792 | ASSERT_EQ('a', buf[0]); |
| 793 | ASSERT_EQ('0', buf[1]); |
| 794 | ASSERT_EQ('1', buf[2]); |
| 795 | ASSERT_EQ('2', buf[3]); |
| 796 | ASSERT_EQ('3', buf[4]); |
| 797 | ASSERT_EQ('4', buf[5]); |
| 798 | ASSERT_EQ('5', buf[6]); |
| 799 | ASSERT_EQ('6', buf[7]); |
| 800 | ASSERT_EQ('7', buf[8]); |
| 801 | ASSERT_EQ('\0', buf[9]); |
| 802 | } |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 803 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 804 | TEST(TEST_NAME, stpncpy) { |
| 805 | char src[10]; |
| 806 | char dst[10]; |
| 807 | memcpy(src, "0123456789", sizeof(src)); // non null terminated string |
| 808 | stpncpy(dst, src, sizeof(dst)); |
| 809 | ASSERT_EQ('0', dst[0]); |
| 810 | ASSERT_EQ('1', dst[1]); |
| 811 | ASSERT_EQ('2', dst[2]); |
| 812 | ASSERT_EQ('3', dst[3]); |
| 813 | ASSERT_EQ('4', dst[4]); |
| 814 | ASSERT_EQ('5', dst[5]); |
| 815 | ASSERT_EQ('6', dst[6]); |
| 816 | ASSERT_EQ('7', dst[7]); |
| 817 | ASSERT_EQ('8', dst[8]); |
| 818 | ASSERT_EQ('9', dst[9]); |
| 819 | } |
| 820 | |
| 821 | TEST(TEST_NAME, stpncpy2) { |
| 822 | char src[10]; |
| 823 | char dst[15]; |
| 824 | memcpy(src, "012345678\0", sizeof(src)); |
| 825 | stpncpy(dst, src, sizeof(dst)); |
| 826 | ASSERT_EQ('0', dst[0]); |
| 827 | ASSERT_EQ('1', dst[1]); |
| 828 | ASSERT_EQ('2', dst[2]); |
| 829 | ASSERT_EQ('3', dst[3]); |
| 830 | ASSERT_EQ('4', dst[4]); |
| 831 | ASSERT_EQ('5', dst[5]); |
| 832 | ASSERT_EQ('6', dst[6]); |
| 833 | ASSERT_EQ('7', dst[7]); |
| 834 | ASSERT_EQ('8', dst[8]); |
| 835 | ASSERT_EQ('\0', dst[9]); |
| 836 | ASSERT_EQ('\0', dst[10]); |
| 837 | ASSERT_EQ('\0', dst[11]); |
| 838 | ASSERT_EQ('\0', dst[12]); |
| 839 | ASSERT_EQ('\0', dst[13]); |
| 840 | ASSERT_EQ('\0', dst[14]); |
| 841 | } |
| 842 | |
Nick Kralevich | 93501d3 | 2013-08-28 10:47:43 -0700 | [diff] [blame] | 843 | TEST(TEST_NAME, strncpy) { |
| 844 | char src[10]; |
| 845 | char dst[10]; |
| 846 | memcpy(src, "0123456789", sizeof(src)); // non null terminated string |
| 847 | strncpy(dst, src, sizeof(dst)); |
| 848 | ASSERT_EQ('0', dst[0]); |
| 849 | ASSERT_EQ('1', dst[1]); |
| 850 | ASSERT_EQ('2', dst[2]); |
| 851 | ASSERT_EQ('3', dst[3]); |
| 852 | ASSERT_EQ('4', dst[4]); |
| 853 | ASSERT_EQ('5', dst[5]); |
| 854 | ASSERT_EQ('6', dst[6]); |
| 855 | ASSERT_EQ('7', dst[7]); |
| 856 | ASSERT_EQ('8', dst[8]); |
| 857 | ASSERT_EQ('9', dst[9]); |
| 858 | } |
| 859 | |
| 860 | TEST(TEST_NAME, strncpy2) { |
| 861 | char src[10]; |
| 862 | char dst[15]; |
| 863 | memcpy(src, "012345678\0", sizeof(src)); |
| 864 | strncpy(dst, src, sizeof(dst)); |
| 865 | ASSERT_EQ('0', dst[0]); |
| 866 | ASSERT_EQ('1', dst[1]); |
| 867 | ASSERT_EQ('2', dst[2]); |
| 868 | ASSERT_EQ('3', dst[3]); |
| 869 | ASSERT_EQ('4', dst[4]); |
| 870 | ASSERT_EQ('5', dst[5]); |
| 871 | ASSERT_EQ('6', dst[6]); |
| 872 | ASSERT_EQ('7', dst[7]); |
| 873 | ASSERT_EQ('8', dst[8]); |
| 874 | ASSERT_EQ('\0', dst[9]); |
| 875 | ASSERT_EQ('\0', dst[10]); |
| 876 | ASSERT_EQ('\0', dst[11]); |
| 877 | ASSERT_EQ('\0', dst[12]); |
| 878 | ASSERT_EQ('\0', dst[13]); |
| 879 | ASSERT_EQ('\0', dst[14]); |
| 880 | } |
Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 881 | |
| 882 | TEST(TEST_NAME, strcat_chk_max_int_size) { |
| 883 | char buf[10]; |
| 884 | memset(buf, 'A', sizeof(buf)); |
| 885 | buf[0] = 'a'; |
| 886 | buf[1] = '\0'; |
| 887 | char* res = __strcat_chk(buf, "01234567", (size_t)-1); |
| 888 | ASSERT_EQ(buf, res); |
| 889 | ASSERT_EQ('a', buf[0]); |
| 890 | ASSERT_EQ('0', buf[1]); |
| 891 | ASSERT_EQ('1', buf[2]); |
| 892 | ASSERT_EQ('2', buf[3]); |
| 893 | ASSERT_EQ('3', buf[4]); |
| 894 | ASSERT_EQ('4', buf[5]); |
| 895 | ASSERT_EQ('5', buf[6]); |
| 896 | ASSERT_EQ('6', buf[7]); |
| 897 | ASSERT_EQ('7', buf[8]); |
| 898 | ASSERT_EQ('\0', buf[9]); |
| 899 | } |
| 900 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 901 | extern "C" char* __stpcpy_chk(char*, const char*, size_t); |
| 902 | |
| 903 | TEST(TEST_NAME, stpcpy_chk_max_int_size) { |
| 904 | char buf[10]; |
| 905 | char* res = __stpcpy_chk(buf, "012345678", (size_t)-1); |
| 906 | ASSERT_EQ(buf + strlen("012345678"), res); |
| 907 | ASSERT_STREQ("012345678", buf); |
| 908 | } |
| 909 | |
Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 910 | extern "C" char* __strcpy_chk(char*, const char*, size_t); |
| 911 | |
| 912 | TEST(TEST_NAME, strcpy_chk_max_int_size) { |
| 913 | char buf[10]; |
| 914 | char* res = __strcpy_chk(buf, "012345678", (size_t)-1); |
| 915 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 916 | ASSERT_STREQ("012345678", buf); |
Christopher Ferris | 16e185c | 2013-09-10 16:56:34 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t); |
| 920 | |
| 921 | TEST(TEST_NAME, memcpy_chk_max_int_size) { |
| 922 | char buf[10]; |
| 923 | void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1); |
| 924 | ASSERT_EQ((void*)buf, res); |
| 925 | ASSERT_EQ('0', buf[0]); |
| 926 | ASSERT_EQ('1', buf[1]); |
| 927 | ASSERT_EQ('2', buf[2]); |
| 928 | ASSERT_EQ('3', buf[3]); |
| 929 | ASSERT_EQ('4', buf[4]); |
| 930 | ASSERT_EQ('5', buf[5]); |
| 931 | ASSERT_EQ('6', buf[6]); |
| 932 | ASSERT_EQ('7', buf[7]); |
| 933 | ASSERT_EQ('8', buf[8]); |
| 934 | ASSERT_EQ('\0', buf[9]); |
| 935 | } |
Stephen Hines | 6e38072 | 2013-10-11 00:45:24 -0700 | [diff] [blame] | 936 | |
| 937 | // Verify that macro expansion is done properly for sprintf/snprintf (which |
| 938 | // are defined as macros in stdio.h under clang). |
| 939 | #define CONTENTS "macro expansion" |
| 940 | #define BUF_AND_SIZE(A) A, sizeof(A) |
| 941 | #define BUF_AND_CONTENTS(A) A, CONTENTS |
| 942 | #define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS |
| 943 | TEST(TEST_NAME, s_n_printf_macro_expansion) { |
| 944 | char buf[BUFSIZ]; |
| 945 | snprintf(BUF_AND_SIZE(buf), CONTENTS); |
| 946 | EXPECT_STREQ(CONTENTS, buf); |
| 947 | |
| 948 | snprintf(BUF_AND_SIZE_AND_CONTENTS(buf)); |
| 949 | EXPECT_STREQ(CONTENTS, buf); |
| 950 | |
| 951 | sprintf(BUF_AND_CONTENTS(buf)); |
| 952 | EXPECT_STREQ(CONTENTS, buf); |
| 953 | } |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 954 | |
| 955 | TEST_F(DEATHTEST, poll_fortified) { |
| 956 | nfds_t fd_count = atoi("2"); // suppress compiler optimizations |
| 957 | pollfd buf[1] = {{0, POLLIN, 0}}; |
Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 958 | // Set timeout to zero to prevent waiting in poll when fortify test fails. |
| 959 | ASSERT_FORTIFY(poll(buf, fd_count, 0)); |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | TEST_F(DEATHTEST, ppoll_fortified) { |
| 963 | nfds_t fd_count = atoi("2"); // suppress compiler optimizations |
| 964 | pollfd buf[1] = {{0, POLLIN, 0}}; |
Yabin Cui | f4fe693 | 2015-02-03 17:52:32 -0800 | [diff] [blame] | 965 | // Set timeout to zero to prevent waiting in ppoll when fortify test fails. |
| 966 | timespec timeout; |
| 967 | timeout.tv_sec = timeout.tv_nsec = 0; |
| 968 | ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, NULL)); |
Elliott Hughes | 4674e38 | 2015-02-02 09:15:19 -0800 | [diff] [blame] | 969 | } |