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