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