blob: b5b858fb5542b42c8655a9c16e37f4938b6f41bd [file] [log] [blame]
Nick Kralevich5bcf3982013-06-28 10:34:09 -07001/*
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 Albertf68dcbe2016-02-02 17:13:03 -080017// -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 Kralevich5bcf3982013-06-28 10:34:09 -070025#include <gtest/gtest.h>
Yabin Cui9df70402014-11-05 18:01:01 -080026#include "BionicDeathTest.h"
Nick Kralevich5bcf3982013-06-28 10:34:09 -070027
Yabin Cui9df70402014-11-05 18:01:01 -080028#include <fcntl.h>
29#include <malloc.h>
Elliott Hughes4674e382015-02-02 09:15:19 -080030#include <poll.h>
Yabin Cui9df70402014-11-05 18:01:01 -080031#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 Cuif4fe6932015-02-03 17:52:32 -080037#include <time.h>
Yabin Cui9df70402014-11-05 18:01:01 -080038
Elliott Hughesd036e942015-02-02 11:18:58 -080039#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 Cui9df70402014-11-05 18:01:01 -080045// 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 Kralevich5bcf3982013-06-28 10:34:09 -070047#define DEATHTEST_PASTER(name) name##_DeathTest
48#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
49#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
50
Yabin Cui9df70402014-11-05 18:01:01 -080051class DEATHTEST : public BionicDeathTest {};
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070052
Nick Kralevich5bcf3982013-06-28 10:34:09 -070053#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
54struct foo {
55 char empty[0];
56 char one[1];
57 char a[10];
58 char b[10];
59};
60
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070061TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070062 foo myfoo;
63 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080064 ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
Christopher Ferris950a58e2014-04-04 14:38:18 -070065}
Christopher Ferris950a58e2014-04-04 14:38:18 -070066
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070067TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070068 foo myfoo;
69 memset(&myfoo, 0, sizeof(myfoo));
70 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080071 ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Christopher Ferris950a58e2014-04-04 14:38:18 -070072}
Christopher Ferris950a58e2014-04-04 14:38:18 -070073
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070074TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070075 foo myfoo;
76 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080077 ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070078}
Nick Kralevich5bcf3982013-06-28 10:34:09 -070079
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070080TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070081 foo myfoo;
82 memset(&myfoo, 0, sizeof(myfoo));
83 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080084 ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Nick Kralevich93501d32013-08-28 10:47:43 -070085}
Nick Kralevich93501d32013-08-28 10:47:43 -070086
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070087TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070088 foo myfoo;
89 char source_buf[15];
90 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -080091 ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070092}
Nick Kralevich5bcf3982013-06-28 10:34:09 -070093
Nick Kralevich884a3de2014-10-06 00:39:47 +000094TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +000095 foo myfoo;
Elliott Hughesd036e942015-02-02 11:18:58 -080096 ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
Nick Kralevich884a3de2014-10-06 00:39:47 +000097}
Nick Kralevich884a3de2014-10-06 00:39:47 +000098
Nick Kralevich5bcf3982013-06-28 10:34:09 -070099static 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 Kralevichbe0e43b2014-07-23 13:56:23 -0700110TEST_F(DEATHTEST, vsprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800111 ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700112}
113
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700114TEST_F(DEATHTEST, vsprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800115 ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700116}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700117
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700118static 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 Kralevichbe0e43b2014-07-23 13:56:23 -0700130TEST_F(DEATHTEST, vsnprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800131 ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700132}
133
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700134TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800135 ASSERT_FORTIFY(vsnprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700136}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700137
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700138// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700139TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700140#if defined(__BIONIC__)
Christopher Ferris950a58e2014-04-04 14:38:18 -0700141 foo myfoo;
142 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800143 ASSERT_FORTIFY(stpcpy(myfoo.empty, src));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700144 free(src);
145#else // __BIONIC__
146 GTEST_LOG_(INFO) << "This test does nothing.\n";
147#endif // __BIONIC__
148}
Christopher Ferris950a58e2014-04-04 14:38:18 -0700149
Christopher Ferris950a58e2014-04-04 14:38:18 -0700150// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700151TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800152#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700153 foo myfoo;
154 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800155 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700156 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800157#else // __BIONIC__
158 GTEST_LOG_(INFO) << "This test does nothing.\n";
159#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700160}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700161
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700162// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700163TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800164#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700165 foo myfoo;
166 char* src = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800167 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700168 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800169#else // __BIONIC__
170 GTEST_LOG_(INFO) << "This test does nothing.\n";
171#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700172}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700173
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700174// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700175TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800176#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700177 foo myfoo;
178 char* src = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800179 ASSERT_FORTIFY(strcpy(myfoo.one, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700180 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800181#else // __BIONIC__
182 GTEST_LOG_(INFO) << "This test does nothing.\n";
183#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700184}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700185
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700186TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800187#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700188 foo myfoo;
189 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
190 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800191 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700192 ASSERT_FORTIFY(printf("%s", strchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800193#else // __BIONIC__
194 GTEST_LOG_(INFO) << "This test does nothing.\n";
195#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700196}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700197
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700198TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800199#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700200 foo myfoo;
201 memcpy(myfoo.a, "0123456789", 10);
202 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800203 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700204 ASSERT_FORTIFY(printf("%s", strrchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800205#else // __BIONIC__
206 GTEST_LOG_(INFO) << "This test does nothing.\n";
207#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700208}
George Burgess IVbd3d2082017-04-04 17:34:02 -0700209
210TEST_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__
218 GTEST_LOG_(INFO) << "This test does nothing.\n";
219#endif // __BIONIC__
220}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700221
Elliott Hughes55a8cc22017-11-08 21:22:44 -0800222TEST_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__
230 GTEST_LOG_(INFO) << "This test does nothing.\n";
231#endif // __BIONIC__
232}
233
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700234TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800235#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700236 foo myfoo;
237 strcpy(myfoo.a, "01");
238 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800239 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800240#else // __BIONIC__
241 GTEST_LOG_(INFO) << "This test does nothing.\n";
242#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700243}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700244
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700245TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800246#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700247 foo myfoo;
248 strcpy(myfoo.a, "01");
249 myfoo.one[0] = '\0';
250 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800251 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800252#else // __BIONIC__
253 GTEST_LOG_(INFO) << "This test does nothing.\n";
254#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700255}
Nick Kralevicha6cde392013-06-29 08:15:25 -0700256
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700257TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700258 foo myfoo;
259 size_t n = atoi("10"); // avoid compiler optimizations
260 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800261 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700262}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700263
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700264TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700265 foo myfoo;
266 myfoo.a[0] = '\0';
267 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800268 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700269}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700270
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700271TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700272 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 Hughesd036e942015-02-02 11:18:58 -0800276 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700277}
278
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700279TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700280 char src[11];
281 strcpy(src, "0123456789");
282 foo myfoo;
283 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800284 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700285}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700286
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700287TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700288 foo myfoo;
289 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
290 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800291 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700292}
293
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700294TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700295 foo myfoo;
296 strcpy(myfoo.a, "012345678");
297 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800298 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700299}
300
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700301TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700302 foo myfoo;
303 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
304 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800305 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700306}
307
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700308#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
309
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700310// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700311TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800312#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700313 char buf[10];
314 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800315 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700316 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800317#else // __BIONIC__
318 GTEST_LOG_(INFO) << "This test does nothing.\n";
319#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700320}
321
322// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700323TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800324#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700325 char buf[0];
326 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800327 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700328 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800329#else // __BIONIC__
330 GTEST_LOG_(INFO) << "This test does nothing.\n";
331#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700332}
333
334// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700335TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800336#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700337 char buf[0];
338 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800339 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700340 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800341#else // __BIONIC__
342 GTEST_LOG_(INFO) << "This test does nothing.\n";
343#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700344}
345
346// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700347TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800348#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700349 char buf[1];
350 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800351 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700352 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800353#else // __BIONIC__
354 GTEST_LOG_(INFO) << "This test does nothing.\n";
355#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700356}
357
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700358TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800359#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700360 char buf[10];
361 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800362 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800363#else // __BIONIC__
364 GTEST_LOG_(INFO) << "This test does nothing.\n";
365#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700366}
367
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700368TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800369#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700370 char buf[10];
371 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800372 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800373#else // __BIONIC__
374 GTEST_LOG_(INFO) << "This test does nothing.\n";
375#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700376}
377
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700378TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800379#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700380 char buf[10];
381 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800382 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800383#else // __BIONIC__
384 GTEST_LOG_(INFO) << "This test does nothing.\n";
385#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700386}
387
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700388TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800389#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700390 char bufa[15];
391 char bufb[10];
392 strcpy(bufa, "01234567890123");
393 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800394 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800395#else // __BIONIC__
396 GTEST_LOG_(INFO) << "This test does nothing.\n";
397#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700398}
399
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700400TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800401#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700402 char bufa[15];
403 char bufb[10];
404 bufb[0] = '\0';
405 strcpy(bufa, "01234567890123");
406 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800407 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800408#else // __BIONIC__
409 GTEST_LOG_(INFO) << "This test does nothing.\n";
410#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700411}
412
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700413TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700414 char buf[10];
415 char source_buf[15];
416 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800417 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700418}
419
Yi Kong2d3122c2017-04-30 15:08:05 -0700420#ifdef __clang__ && !__has_attribute(alloc_size)
421// TODO: remove this after Clang prebuilt rebase.
George Burgess IV5f8a6732017-04-06 11:26:11 -0700422#else
Nick Kralevichb91791d2013-10-02 14:14:40 -0700423// This test is disabled in clang because clang doesn't properly detect
424// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700425TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700426 char* buf = (char *) malloc(10);
427 char source_buf[11];
428 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800429 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700430 free(buf);
431}
432#endif
433
Nick Kralevich884a3de2014-10-06 00:39:47 +0000434TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000435 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800436 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000437}
438
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700439static int vsprintf_helper(const char *fmt, ...) {
440 char buf[10];
441 va_list va;
442 int result;
443
444 va_start(va, fmt);
445 result = vsprintf(buf, fmt, va); // should crash here
446 va_end(va);
447 return result;
448}
449
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700450TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800451 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700452}
453
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700454TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800455 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700456}
457
458static int vsnprintf_helper(const char *fmt, ...) {
459 char buf[10];
460 va_list va;
461 int result;
462 size_t size = atoi("11");
463
464 va_start(va, fmt);
465 result = vsnprintf(buf, size, fmt, va); // should crash here
466 va_end(va);
467 return result;
468}
469
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700470TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800471 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700472}
473
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700474TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800475 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700476}
477
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700478TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700479 char buf[10];
480 size_t n = atoi("10"); // avoid compiler optimizations
481 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800482 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700483}
484
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700485TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700486 char buf[10];
487 buf[0] = '\0';
488 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800489 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700490}
491
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700492TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700493 char src[11];
494 strcpy(src, "0123456789");
495 char buf[10];
496 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800497 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700498}
499
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700500TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700501 char buf[20];
502 strcpy(buf, "0123456789");
503 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800504 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700505}
506
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700507TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700508 char bufa[10];
509 char bufb[10];
510 strcpy(bufa, "012345678");
511 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800512 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700513}
514
Elliott Hughes62e59642016-03-01 11:22:42 -0800515TEST_F(DEATHTEST, memset_fortified) {
516 char buf[10];
517 size_t n = atoi("11");
518 ASSERT_FORTIFY(memset(buf, 0, n));
519}
520
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700521TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700522 char bufa[15];
523 char bufb[10];
524 strcpy(bufa, "01234567890123");
525 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800526 ASSERT_FORTIFY(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700527}
528
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700529TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700530 char dest[11];
531 char src[10];
532 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800533 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700534}
535
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700536TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700537 char bufa[15];
538 char bufb[10];
539 strcpy(bufa, "01234567890123");
540 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800541 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700542}
543
Christopher Ferris950a58e2014-04-04 14:38:18 -0700544
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700545TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700546 char dest[11];
547 char src[10];
548 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800549 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700550}
551
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700552TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700553 char bufa[15];
554 char bufb[10];
555 strcpy(bufa, "0123456789");
556 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800557 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700558}
559
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700560TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700561 char buf[10];
562 memcpy(buf, "0123456789", sizeof(buf));
563 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800564 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700565}
566
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700567TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700568 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800569 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700570}
571
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700572TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700573 size_t data_len = atoi("11"); // suppress compiler optimizations
574 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800575 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700576}
577
Daniel Micay95b59c52017-02-13 17:27:59 -0800578TEST_F(DEATHTEST, send_fortified) {
579 size_t data_len = atoi("11"); // suppress compiler optimizations
580 char buf[10] = {0};
581 ASSERT_FORTIFY(send(0, buf, data_len, 0));
582}
583
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700584TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700585#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700586 fd_set set;
587 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800588 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700589#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700590}
591
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700592TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700593 char buf[1];
594 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800595 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700596}
597
Daniel Micay9101b002015-05-20 15:31:26 -0400598TEST_F(DEATHTEST, getcwd_fortified) {
599 char buf[1];
600 size_t ct = atoi("2"); // prevent optimizations
601 ASSERT_FORTIFY(getcwd(buf, ct));
602}
603
Daniel Micaye7e1c872015-04-16 09:07:45 -0400604TEST_F(DEATHTEST, pread_fortified) {
605 char buf[1];
606 size_t ct = atoi("2"); // prevent optimizations
607 int fd = open("/dev/null", O_RDONLY);
608 ASSERT_FORTIFY(pread(fd, buf, ct, 0));
609 close(fd);
610}
611
612TEST_F(DEATHTEST, pread64_fortified) {
613 char buf[1];
614 size_t ct = atoi("2"); // prevent optimizations
615 int fd = open("/dev/null", O_RDONLY);
616 ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
617 close(fd);
618}
619
Daniel Micayafdd1542015-07-20 21:37:29 -0400620TEST_F(DEATHTEST, pwrite_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(pwrite(fd, buf, ct, 0));
625 close(fd);
626}
627
628TEST_F(DEATHTEST, pwrite64_fortified) {
629 char buf[1] = {0};
630 size_t ct = atoi("2"); // prevent optimizations
631 int fd = open("/dev/null", O_WRONLY);
632 ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0));
633 close(fd);
634}
635
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700636TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700637 char buf[1];
638 size_t ct = atoi("2"); // prevent optimizations
639 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800640 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700641 close(fd);
642}
643
Daniel Micayafdd1542015-07-20 21:37:29 -0400644TEST_F(DEATHTEST, write_fortified) {
645 char buf[1] = {0};
646 size_t ct = atoi("2"); // prevent optimizations
647 int fd = open("/dev/null", O_WRONLY);
648 ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
649 close(fd);
650}
651
Daniel Micayfed26592015-07-18 13:55:51 -0400652TEST_F(DEATHTEST, fread_fortified) {
653 char buf[1];
654 size_t ct = atoi("2"); // prevent optimizations
655 FILE* fp = fopen("/dev/null", "r");
656 ASSERT_FORTIFY(fread(buf, 1, ct, fp));
657 fclose(fp);
658}
659
660TEST_F(DEATHTEST, fwrite_fortified) {
661 char buf[1] = {0};
662 size_t ct = atoi("2"); // prevent optimizations
663 FILE* fp = fopen("/dev/null", "w");
664 ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
665 fclose(fp);
666}
667
Daniel Micay42281882015-04-17 11:26:36 -0400668TEST_F(DEATHTEST, readlink_fortified) {
669 char buf[1];
670 size_t ct = atoi("2"); // prevent optimizations
671 ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
672}
673
674TEST_F(DEATHTEST, readlinkat_fortified) {
675 char buf[1];
676 size_t ct = atoi("2"); // prevent optimizations
677 ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
678}
679
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700680extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
681extern "C" char* __strcat_chk(char*, const char*, size_t);
682
683TEST(TEST_NAME, strncat) {
684 char buf[10];
685 memset(buf, 'A', sizeof(buf));
686 buf[0] = 'a';
687 buf[1] = '\0';
688 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
689 ASSERT_EQ(buf, res);
690 ASSERT_EQ('a', buf[0]);
691 ASSERT_EQ('0', buf[1]);
692 ASSERT_EQ('1', buf[2]);
693 ASSERT_EQ('2', buf[3]);
694 ASSERT_EQ('3', buf[4]);
695 ASSERT_EQ('4', buf[5]);
696 ASSERT_EQ('\0', buf[6]);
697 ASSERT_EQ('A', buf[7]);
698 ASSERT_EQ('A', buf[8]);
699 ASSERT_EQ('A', buf[9]);
700}
701
702TEST(TEST_NAME, strncat2) {
703 char buf[10];
704 memset(buf, 'A', sizeof(buf));
705 buf[0] = 'a';
706 buf[1] = '\0';
707 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
708 ASSERT_EQ(buf, res);
709 ASSERT_EQ('a', buf[0]);
710 ASSERT_EQ('0', buf[1]);
711 ASSERT_EQ('1', buf[2]);
712 ASSERT_EQ('2', buf[3]);
713 ASSERT_EQ('3', buf[4]);
714 ASSERT_EQ('4', buf[5]);
715 ASSERT_EQ('\0', buf[6]);
716 ASSERT_EQ('A', buf[7]);
717 ASSERT_EQ('A', buf[8]);
718 ASSERT_EQ('A', buf[9]);
719}
720
721TEST(TEST_NAME, strncat3) {
722 char buf[10];
723 memset(buf, 'A', sizeof(buf));
724 buf[0] = '\0';
725 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
726 ASSERT_EQ(buf, res);
727 ASSERT_EQ('0', buf[0]);
728 ASSERT_EQ('1', buf[1]);
729 ASSERT_EQ('2', buf[2]);
730 ASSERT_EQ('3', buf[3]);
731 ASSERT_EQ('4', buf[4]);
732 ASSERT_EQ('\0', buf[5]);
733 ASSERT_EQ('A', buf[6]);
734 ASSERT_EQ('A', buf[7]);
735 ASSERT_EQ('A', buf[8]);
736 ASSERT_EQ('A', buf[9]);
737}
738
739TEST(TEST_NAME, strncat4) {
740 char buf[10];
741 memset(buf, 'A', sizeof(buf));
742 buf[9] = '\0';
743 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
744 ASSERT_EQ(buf, res);
745 ASSERT_EQ('A', buf[0]);
746 ASSERT_EQ('A', buf[1]);
747 ASSERT_EQ('A', buf[2]);
748 ASSERT_EQ('A', buf[3]);
749 ASSERT_EQ('A', buf[4]);
750 ASSERT_EQ('A', buf[5]);
751 ASSERT_EQ('A', buf[6]);
752 ASSERT_EQ('A', buf[7]);
753 ASSERT_EQ('A', buf[8]);
754 ASSERT_EQ('\0', buf[9]);
755}
756
757TEST(TEST_NAME, strncat5) {
758 char buf[10];
759 memset(buf, 'A', sizeof(buf));
760 buf[0] = 'a';
761 buf[1] = '\0';
762 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
763 ASSERT_EQ(buf, res);
764 ASSERT_EQ('a', buf[0]);
765 ASSERT_EQ('0', buf[1]);
766 ASSERT_EQ('1', buf[2]);
767 ASSERT_EQ('2', buf[3]);
768 ASSERT_EQ('3', buf[4]);
769 ASSERT_EQ('4', buf[5]);
770 ASSERT_EQ('5', buf[6]);
771 ASSERT_EQ('6', buf[7]);
772 ASSERT_EQ('7', buf[8]);
773 ASSERT_EQ('\0', buf[9]);
774}
775
776TEST(TEST_NAME, strncat6) {
777 char buf[10];
778 memset(buf, 'A', sizeof(buf));
779 buf[0] = 'a';
780 buf[1] = '\0';
781 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
782 ASSERT_EQ(buf, res);
783 ASSERT_EQ('a', buf[0]);
784 ASSERT_EQ('0', buf[1]);
785 ASSERT_EQ('1', buf[2]);
786 ASSERT_EQ('2', buf[3]);
787 ASSERT_EQ('3', buf[4]);
788 ASSERT_EQ('4', buf[5]);
789 ASSERT_EQ('5', buf[6]);
790 ASSERT_EQ('6', buf[7]);
791 ASSERT_EQ('7', buf[8]);
792 ASSERT_EQ('\0', buf[9]);
793}
794
795
796TEST(TEST_NAME, strcat) {
797 char buf[10];
798 memset(buf, 'A', sizeof(buf));
799 buf[0] = 'a';
800 buf[1] = '\0';
801 char* res = __strcat_chk(buf, "01234", sizeof(buf));
802 ASSERT_EQ(buf, res);
803 ASSERT_EQ('a', buf[0]);
804 ASSERT_EQ('0', buf[1]);
805 ASSERT_EQ('1', buf[2]);
806 ASSERT_EQ('2', buf[3]);
807 ASSERT_EQ('3', buf[4]);
808 ASSERT_EQ('4', buf[5]);
809 ASSERT_EQ('\0', buf[6]);
810 ASSERT_EQ('A', buf[7]);
811 ASSERT_EQ('A', buf[8]);
812 ASSERT_EQ('A', buf[9]);
813}
814
815TEST(TEST_NAME, strcat2) {
816 char buf[10];
817 memset(buf, 'A', sizeof(buf));
818 buf[0] = 'a';
819 buf[1] = '\0';
820 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
821 ASSERT_EQ(buf, res);
822 ASSERT_EQ('a', buf[0]);
823 ASSERT_EQ('0', buf[1]);
824 ASSERT_EQ('1', buf[2]);
825 ASSERT_EQ('2', buf[3]);
826 ASSERT_EQ('3', buf[4]);
827 ASSERT_EQ('4', buf[5]);
828 ASSERT_EQ('5', buf[6]);
829 ASSERT_EQ('6', buf[7]);
830 ASSERT_EQ('7', buf[8]);
831 ASSERT_EQ('\0', buf[9]);
832}
Nick Kralevich93501d32013-08-28 10:47:43 -0700833
Christopher Ferris950a58e2014-04-04 14:38:18 -0700834TEST(TEST_NAME, stpncpy) {
835 char src[10];
836 char dst[10];
837 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
838 stpncpy(dst, src, sizeof(dst));
839 ASSERT_EQ('0', dst[0]);
840 ASSERT_EQ('1', dst[1]);
841 ASSERT_EQ('2', dst[2]);
842 ASSERT_EQ('3', dst[3]);
843 ASSERT_EQ('4', dst[4]);
844 ASSERT_EQ('5', dst[5]);
845 ASSERT_EQ('6', dst[6]);
846 ASSERT_EQ('7', dst[7]);
847 ASSERT_EQ('8', dst[8]);
848 ASSERT_EQ('9', dst[9]);
849}
850
851TEST(TEST_NAME, stpncpy2) {
852 char src[10];
853 char dst[15];
854 memcpy(src, "012345678\0", sizeof(src));
855 stpncpy(dst, src, sizeof(dst));
856 ASSERT_EQ('0', dst[0]);
857 ASSERT_EQ('1', dst[1]);
858 ASSERT_EQ('2', dst[2]);
859 ASSERT_EQ('3', dst[3]);
860 ASSERT_EQ('4', dst[4]);
861 ASSERT_EQ('5', dst[5]);
862 ASSERT_EQ('6', dst[6]);
863 ASSERT_EQ('7', dst[7]);
864 ASSERT_EQ('8', dst[8]);
865 ASSERT_EQ('\0', dst[9]);
866 ASSERT_EQ('\0', dst[10]);
867 ASSERT_EQ('\0', dst[11]);
868 ASSERT_EQ('\0', dst[12]);
869 ASSERT_EQ('\0', dst[13]);
870 ASSERT_EQ('\0', dst[14]);
871}
872
Nick Kralevich93501d32013-08-28 10:47:43 -0700873TEST(TEST_NAME, strncpy) {
874 char src[10];
875 char dst[10];
876 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
877 strncpy(dst, src, sizeof(dst));
878 ASSERT_EQ('0', dst[0]);
879 ASSERT_EQ('1', dst[1]);
880 ASSERT_EQ('2', dst[2]);
881 ASSERT_EQ('3', dst[3]);
882 ASSERT_EQ('4', dst[4]);
883 ASSERT_EQ('5', dst[5]);
884 ASSERT_EQ('6', dst[6]);
885 ASSERT_EQ('7', dst[7]);
886 ASSERT_EQ('8', dst[8]);
887 ASSERT_EQ('9', dst[9]);
888}
889
890TEST(TEST_NAME, strncpy2) {
891 char src[10];
892 char dst[15];
893 memcpy(src, "012345678\0", sizeof(src));
894 strncpy(dst, src, sizeof(dst));
895 ASSERT_EQ('0', dst[0]);
896 ASSERT_EQ('1', dst[1]);
897 ASSERT_EQ('2', dst[2]);
898 ASSERT_EQ('3', dst[3]);
899 ASSERT_EQ('4', dst[4]);
900 ASSERT_EQ('5', dst[5]);
901 ASSERT_EQ('6', dst[6]);
902 ASSERT_EQ('7', dst[7]);
903 ASSERT_EQ('8', dst[8]);
904 ASSERT_EQ('\0', dst[9]);
905 ASSERT_EQ('\0', dst[10]);
906 ASSERT_EQ('\0', dst[11]);
907 ASSERT_EQ('\0', dst[12]);
908 ASSERT_EQ('\0', dst[13]);
909 ASSERT_EQ('\0', dst[14]);
910}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700911
912TEST(TEST_NAME, strcat_chk_max_int_size) {
913 char buf[10];
914 memset(buf, 'A', sizeof(buf));
915 buf[0] = 'a';
916 buf[1] = '\0';
917 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
918 ASSERT_EQ(buf, res);
919 ASSERT_EQ('a', buf[0]);
920 ASSERT_EQ('0', buf[1]);
921 ASSERT_EQ('1', buf[2]);
922 ASSERT_EQ('2', buf[3]);
923 ASSERT_EQ('3', buf[4]);
924 ASSERT_EQ('4', buf[5]);
925 ASSERT_EQ('5', buf[6]);
926 ASSERT_EQ('6', buf[7]);
927 ASSERT_EQ('7', buf[8]);
928 ASSERT_EQ('\0', buf[9]);
929}
930
Christopher Ferris950a58e2014-04-04 14:38:18 -0700931extern "C" char* __stpcpy_chk(char*, const char*, size_t);
932
933TEST(TEST_NAME, stpcpy_chk_max_int_size) {
934 char buf[10];
935 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
936 ASSERT_EQ(buf + strlen("012345678"), res);
937 ASSERT_STREQ("012345678", buf);
938}
939
Christopher Ferris16e185c2013-09-10 16:56:34 -0700940extern "C" char* __strcpy_chk(char*, const char*, size_t);
941
942TEST(TEST_NAME, strcpy_chk_max_int_size) {
943 char buf[10];
944 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
945 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700946 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700947}
948
949extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
950
951TEST(TEST_NAME, memcpy_chk_max_int_size) {
952 char buf[10];
953 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
954 ASSERT_EQ((void*)buf, res);
955 ASSERT_EQ('0', buf[0]);
956 ASSERT_EQ('1', buf[1]);
957 ASSERT_EQ('2', buf[2]);
958 ASSERT_EQ('3', buf[3]);
959 ASSERT_EQ('4', buf[4]);
960 ASSERT_EQ('5', buf[5]);
961 ASSERT_EQ('6', buf[6]);
962 ASSERT_EQ('7', buf[7]);
963 ASSERT_EQ('8', buf[8]);
964 ASSERT_EQ('\0', buf[9]);
965}
Stephen Hines6e380722013-10-11 00:45:24 -0700966
967// Verify that macro expansion is done properly for sprintf/snprintf (which
968// are defined as macros in stdio.h under clang).
969#define CONTENTS "macro expansion"
970#define BUF_AND_SIZE(A) A, sizeof(A)
971#define BUF_AND_CONTENTS(A) A, CONTENTS
972#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
973TEST(TEST_NAME, s_n_printf_macro_expansion) {
974 char buf[BUFSIZ];
975 snprintf(BUF_AND_SIZE(buf), CONTENTS);
976 EXPECT_STREQ(CONTENTS, buf);
977
978 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
979 EXPECT_STREQ(CONTENTS, buf);
980
981 sprintf(BUF_AND_CONTENTS(buf));
982 EXPECT_STREQ(CONTENTS, buf);
983}
Elliott Hughes4674e382015-02-02 09:15:19 -0800984
985TEST_F(DEATHTEST, poll_fortified) {
986 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
987 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800988 // Set timeout to zero to prevent waiting in poll when fortify test fails.
989 ASSERT_FORTIFY(poll(buf, fd_count, 0));
Elliott Hughes4674e382015-02-02 09:15:19 -0800990}
991
992TEST_F(DEATHTEST, ppoll_fortified) {
993 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
994 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800995 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
996 timespec timeout;
997 timeout.tv_sec = timeout.tv_nsec = 0;
Elliott Hughesb83bf142018-03-22 11:01:25 -0700998 ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, nullptr));
999}
1000
1001TEST_F(DEATHTEST, ppoll64_fortified) {
1002#if __BIONIC__ // glibc doesn't have ppoll64.
1003 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1004 pollfd buf[1] = {{0, POLLIN, 0}};
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;
1008 ASSERT_FORTIFY(ppoll64(buf, fd_count, &timeout, nullptr));
1009#endif
Elliott Hughes4674e382015-02-02 09:15:19 -08001010}
Elliott Hughesb115aef2017-08-04 09:34:19 -07001011
1012TEST_F(DEATHTEST, open_O_CREAT_without_mode_fortified) {
1013 int flags = O_CREAT; // Fool the compiler.
1014 ASSERT_FORTIFY(open("", flags));
1015}
1016
1017TEST_F(DEATHTEST, open_O_TMPFILE_without_mode_fortified) {
1018#if __BIONIC__ // Our glibc is too old for O_TMPFILE.
1019 int flags = O_TMPFILE; // Fool the compiler.
1020 ASSERT_FORTIFY(open("", flags));
1021#endif
1022}