blob: 6907abe9be437f59ad692c7fcaf2535b24519132 [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
17#include <gtest/gtest.h>
Yabin Cui9df70402014-11-05 18:01:01 -080018#include "BionicDeathTest.h"
Nick Kralevich5bcf3982013-06-28 10:34:09 -070019
Yabin Cui9df70402014-11-05 18:01:01 -080020#include <fcntl.h>
21#include <malloc.h>
Elliott Hughes4674e382015-02-02 09:15:19 -080022#include <poll.h>
Yabin Cui9df70402014-11-05 18:01:01 -080023#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 Cuif4fe6932015-02-03 17:52:32 -080029#include <time.h>
Yabin Cui9df70402014-11-05 18:01:01 -080030
Elliott Hughesd036e942015-02-02 11:18:58 -080031#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 Cui9df70402014-11-05 18:01:01 -080037// 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 Kralevich5bcf3982013-06-28 10:34:09 -070039#define DEATHTEST_PASTER(name) name##_DeathTest
40#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
41#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
42
Yabin Cui9df70402014-11-05 18:01:01 -080043class DEATHTEST : public BionicDeathTest {};
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070044
Nick Kralevich5bcf3982013-06-28 10:34:09 -070045#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
46struct foo {
47 char empty[0];
48 char one[1];
49 char a[10];
50 char b[10];
51};
52
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070053TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070054 foo myfoo;
55 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080056 ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
Christopher Ferris950a58e2014-04-04 14:38:18 -070057}
Christopher Ferris950a58e2014-04-04 14:38:18 -070058
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070059TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070060 foo myfoo;
61 memset(&myfoo, 0, sizeof(myfoo));
62 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080063 ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Christopher Ferris950a58e2014-04-04 14:38:18 -070064}
Christopher Ferris950a58e2014-04-04 14:38:18 -070065
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070066TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070067 foo myfoo;
68 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080069 ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070070}
Nick Kralevich5bcf3982013-06-28 10:34:09 -070071
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070072TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070073 foo myfoo;
74 memset(&myfoo, 0, sizeof(myfoo));
75 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080076 ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Nick Kralevich93501d32013-08-28 10:47:43 -070077}
Nick Kralevich93501d32013-08-28 10:47:43 -070078
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070079TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070080 foo myfoo;
81 char source_buf[15];
82 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -080083 ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070084}
Nick Kralevich5bcf3982013-06-28 10:34:09 -070085
Nick Kralevich884a3de2014-10-06 00:39:47 +000086TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +000087 foo myfoo;
Elliott Hughesd036e942015-02-02 11:18:58 -080088 ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
Nick Kralevich884a3de2014-10-06 00:39:47 +000089}
Nick Kralevich884a3de2014-10-06 00:39:47 +000090
Nick Kralevich5bcf3982013-06-28 10:34:09 -070091static 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 Kralevichbe0e43b2014-07-23 13:56:23 -0700102TEST_F(DEATHTEST, vsprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800103 ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700104}
105
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700106TEST_F(DEATHTEST, vsprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800107 ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700108}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700109
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700110static 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 Kralevichbe0e43b2014-07-23 13:56:23 -0700122TEST_F(DEATHTEST, vsnprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800123 ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700124}
125
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700126TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800127 ASSERT_FORTIFY(vsnprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700128}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700129
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700130// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700131TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700132#if defined(__BIONIC__)
Christopher Ferris950a58e2014-04-04 14:38:18 -0700133 foo myfoo;
134 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800135 ASSERT_FORTIFY(stpcpy(myfoo.empty, src));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700136 free(src);
137#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800138 GTEST_SKIP() << "stpcpy not available";
Christopher Ferris950a58e2014-04-04 14:38:18 -0700139#endif // __BIONIC__
140}
Christopher Ferris950a58e2014-04-04 14:38:18 -0700141
Christopher Ferris950a58e2014-04-04 14:38:18 -0700142// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700143TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800144#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700145 foo myfoo;
146 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800147 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700148 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800149#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800150 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800151#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700152}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700153
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700154// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700155TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800156#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700157 foo myfoo;
158 char* src = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800159 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700160 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800161#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800162 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800163#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700164}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700165
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700166// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700167TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800168#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700169 foo myfoo;
170 char* src = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800171 ASSERT_FORTIFY(strcpy(myfoo.one, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700172 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800173#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800174 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800175#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700176}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700177
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700178TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800179#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700180 foo myfoo;
181 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
182 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800183 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700184 ASSERT_FORTIFY(printf("%s", strchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800185#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800186 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800187#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700188}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700189
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700190TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800191#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700192 foo myfoo;
193 memcpy(myfoo.a, "0123456789", 10);
194 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800195 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700196 ASSERT_FORTIFY(printf("%s", strrchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800197#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800198 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800199#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700200}
George Burgess IVbd3d2082017-04-04 17:34:02 -0700201
202TEST_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));
207 ASSERT_FORTIFY(printf("%s", memchr(myfoo.a, 'a', asize)));
208 ASSERT_FORTIFY(printf("%s", memchr(static_cast<const void*>(myfoo.a), 'a', asize)));
209#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800210 GTEST_SKIP() << "glibc is broken";
George Burgess IVbd3d2082017-04-04 17:34:02 -0700211#endif // __BIONIC__
212}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700213
Elliott Hughes55a8cc22017-11-08 21:22:44 -0800214TEST_F(DEATHTEST, memrchr_fortified2) {
215#if defined(__BIONIC__)
216 foo myfoo;
217 volatile int asize = sizeof(myfoo.a) + 1;
218 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
219 ASSERT_FORTIFY(printf("%s", memrchr(myfoo.a, 'a', asize)));
220 ASSERT_FORTIFY(printf("%s", memrchr(static_cast<const void*>(myfoo.a), 'a', asize)));
221#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800222 GTEST_SKIP() << "glibc is broken";
Elliott Hughes55a8cc22017-11-08 21:22:44 -0800223#endif // __BIONIC__
224}
225
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700226TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800227#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700228 foo myfoo;
229 strcpy(myfoo.a, "01");
230 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800231 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800232#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800233 GTEST_SKIP() << "strlcpy not available";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800234#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700235}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700236
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700237TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800238#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700239 foo myfoo;
240 strcpy(myfoo.a, "01");
241 myfoo.one[0] = '\0';
242 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800243 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800244#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800245 GTEST_SKIP() << "strlcat not available";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800246#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700247}
Nick Kralevicha6cde392013-06-29 08:15:25 -0700248
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700249TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700250 foo myfoo;
251 size_t n = atoi("10"); // avoid compiler optimizations
252 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800253 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700254}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700255
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700256TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700257 foo myfoo;
258 myfoo.a[0] = '\0';
259 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800260 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700261}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700262
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700263TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700264 foo myfoo;
265 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
266 myfoo.b[0] = '\0';
267 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800268 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700269}
270
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700271TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700272 char src[11];
273 strcpy(src, "0123456789");
274 foo myfoo;
275 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800276 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700277}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700278
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700279TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700280 foo myfoo;
281 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
282 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800283 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700284}
285
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700286TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700287 foo myfoo;
288 strcpy(myfoo.a, "012345678");
289 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800290 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700291}
292
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700293TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700294 foo myfoo;
295 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
296 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800297 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700298}
299
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700300#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
301
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700302// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700303TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800304#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700305 char buf[10];
306 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800307 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700308 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800309#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800310 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800311#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700312}
313
314// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700315TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800316#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700317 char buf[0];
318 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800319 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700320 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800321#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800322 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800323#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700324}
325
326// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700327TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800328#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700329 char buf[0];
330 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800331 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700332 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800333#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800334 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800335#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700336}
337
338// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700339TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800340#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700341 char buf[1];
342 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800343 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700344 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800345#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800346 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800347#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700348}
349
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700350TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800351#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700352 char buf[10];
353 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800354 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800355#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800356 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800357#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700358}
359
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700360TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800361#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700362 char buf[10];
363 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800364 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800365#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800366 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800367#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700368}
369
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700370TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800371#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700372 char buf[10];
373 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800374 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800375#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800376 GTEST_SKIP() << "glibc is broken";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800377#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700378}
379
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700380TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800381#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700382 char bufa[15];
383 char bufb[10];
384 strcpy(bufa, "01234567890123");
385 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800386 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800387#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800388 GTEST_SKIP() << "strlcpy not available";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800389#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700390}
391
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700392TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800393#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700394 char bufa[15];
395 char bufb[10];
396 bufb[0] = '\0';
397 strcpy(bufa, "01234567890123");
398 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800399 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800400#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800401 GTEST_SKIP() << "strlcat not available";
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800402#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700403}
404
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700405TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700406 char buf[10];
407 char source_buf[15];
408 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800409 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700410}
411
George Burgess IVe9c0e822018-03-22 11:22:59 -0700412#if !__has_attribute(alloc_size)
Yi Kong2d3122c2017-04-30 15:08:05 -0700413// TODO: remove this after Clang prebuilt rebase.
George Burgess IV5f8a6732017-04-06 11:26:11 -0700414#else
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700415TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700416 char* buf = (char *) malloc(10);
417 char source_buf[11];
418 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800419 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700420 free(buf);
421}
422#endif
423
Nick Kralevich884a3de2014-10-06 00:39:47 +0000424TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000425 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800426 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000427}
428
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700429static int vsprintf_helper(const char *fmt, ...) {
430 char buf[10];
431 va_list va;
432 int result;
433
434 va_start(va, fmt);
435 result = vsprintf(buf, fmt, va); // should crash here
436 va_end(va);
437 return result;
438}
439
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700440TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800441 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700442}
443
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700444TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800445 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700446}
447
448static int vsnprintf_helper(const char *fmt, ...) {
449 char buf[10];
450 va_list va;
451 int result;
452 size_t size = atoi("11");
453
454 va_start(va, fmt);
455 result = vsnprintf(buf, size, fmt, va); // should crash here
456 va_end(va);
457 return result;
458}
459
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700460TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800461 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700462}
463
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700464TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800465 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700466}
467
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700468TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700469 char buf[10];
470 size_t n = atoi("10"); // avoid compiler optimizations
471 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800472 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700473}
474
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700475TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700476 char buf[10];
477 buf[0] = '\0';
478 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800479 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700480}
481
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700482TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700483 char src[11];
484 strcpy(src, "0123456789");
485 char buf[10];
486 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800487 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700488}
489
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700490TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700491 char buf[20];
492 strcpy(buf, "0123456789");
493 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800494 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700495}
496
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700497TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700498 char bufa[10];
499 char bufb[10];
500 strcpy(bufa, "012345678");
501 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800502 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700503}
504
Elliott Hughes62e59642016-03-01 11:22:42 -0800505TEST_F(DEATHTEST, memset_fortified) {
506 char buf[10];
507 size_t n = atoi("11");
508 ASSERT_FORTIFY(memset(buf, 0, n));
509}
510
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700511TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700512 char bufa[15];
513 char bufb[10];
514 strcpy(bufa, "01234567890123");
515 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800516 ASSERT_FORTIFY(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700517}
518
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700519TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700520 char dest[11];
521 char src[10];
522 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800523 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700524}
525
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700526TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700527 char bufa[15];
528 char bufb[10];
529 strcpy(bufa, "01234567890123");
530 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800531 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700532}
533
Christopher Ferris950a58e2014-04-04 14:38:18 -0700534
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700535TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700536 char dest[11];
537 char src[10];
538 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800539 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700540}
541
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700542TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700543 char bufa[15];
544 char bufb[10];
545 strcpy(bufa, "0123456789");
546 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800547 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700548}
549
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700550TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700551 char buf[10];
552 memcpy(buf, "0123456789", sizeof(buf));
553 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800554 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700555}
556
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700557TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700558 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800559 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700560}
561
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700562TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700563 size_t data_len = atoi("11"); // suppress compiler optimizations
564 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800565 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700566}
567
Daniel Micay95b59c52017-02-13 17:27:59 -0800568TEST_F(DEATHTEST, send_fortified) {
569 size_t data_len = atoi("11"); // suppress compiler optimizations
570 char buf[10] = {0};
571 ASSERT_FORTIFY(send(0, buf, data_len, 0));
572}
573
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700574TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700575#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700576 fd_set set;
577 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800578 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700579#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700580}
581
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700582TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700583 char buf[1];
584 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800585 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700586}
587
Daniel Micay9101b002015-05-20 15:31:26 -0400588TEST_F(DEATHTEST, getcwd_fortified) {
589 char buf[1];
590 size_t ct = atoi("2"); // prevent optimizations
591 ASSERT_FORTIFY(getcwd(buf, ct));
592}
593
Daniel Micaye7e1c872015-04-16 09:07:45 -0400594TEST_F(DEATHTEST, pread_fortified) {
595 char buf[1];
596 size_t ct = atoi("2"); // prevent optimizations
597 int fd = open("/dev/null", O_RDONLY);
598 ASSERT_FORTIFY(pread(fd, buf, ct, 0));
599 close(fd);
600}
601
602TEST_F(DEATHTEST, pread64_fortified) {
603 char buf[1];
604 size_t ct = atoi("2"); // prevent optimizations
605 int fd = open("/dev/null", O_RDONLY);
606 ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
607 close(fd);
608}
609
Daniel Micayafdd1542015-07-20 21:37:29 -0400610TEST_F(DEATHTEST, pwrite_fortified) {
611 char buf[1] = {0};
612 size_t ct = atoi("2"); // prevent optimizations
613 int fd = open("/dev/null", O_WRONLY);
614 ASSERT_FORTIFY(pwrite(fd, buf, ct, 0));
615 close(fd);
616}
617
618TEST_F(DEATHTEST, pwrite64_fortified) {
619 char buf[1] = {0};
620 size_t ct = atoi("2"); // prevent optimizations
621 int fd = open("/dev/null", O_WRONLY);
622 ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0));
623 close(fd);
624}
625
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700626TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700627 char buf[1];
628 size_t ct = atoi("2"); // prevent optimizations
629 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800630 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700631 close(fd);
632}
633
Daniel Micayafdd1542015-07-20 21:37:29 -0400634TEST_F(DEATHTEST, write_fortified) {
635 char buf[1] = {0};
636 size_t ct = atoi("2"); // prevent optimizations
637 int fd = open("/dev/null", O_WRONLY);
638 ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
639 close(fd);
640}
641
Daniel Micayfed26592015-07-18 13:55:51 -0400642TEST_F(DEATHTEST, fread_fortified) {
643 char buf[1];
644 size_t ct = atoi("2"); // prevent optimizations
645 FILE* fp = fopen("/dev/null", "r");
646 ASSERT_FORTIFY(fread(buf, 1, ct, fp));
647 fclose(fp);
648}
649
650TEST_F(DEATHTEST, fwrite_fortified) {
651 char buf[1] = {0};
652 size_t ct = atoi("2"); // prevent optimizations
653 FILE* fp = fopen("/dev/null", "w");
654 ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
655 fclose(fp);
656}
657
Daniel Micay42281882015-04-17 11:26:36 -0400658TEST_F(DEATHTEST, readlink_fortified) {
659 char buf[1];
660 size_t ct = atoi("2"); // prevent optimizations
661 ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
662}
663
664TEST_F(DEATHTEST, readlinkat_fortified) {
665 char buf[1];
666 size_t ct = atoi("2"); // prevent optimizations
667 ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
668}
669
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700670extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
671extern "C" char* __strcat_chk(char*, const char*, size_t);
672
673TEST(TEST_NAME, strncat) {
674 char buf[10];
675 memset(buf, 'A', sizeof(buf));
676 buf[0] = 'a';
677 buf[1] = '\0';
678 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
679 ASSERT_EQ(buf, res);
680 ASSERT_EQ('a', buf[0]);
681 ASSERT_EQ('0', buf[1]);
682 ASSERT_EQ('1', buf[2]);
683 ASSERT_EQ('2', buf[3]);
684 ASSERT_EQ('3', buf[4]);
685 ASSERT_EQ('4', buf[5]);
686 ASSERT_EQ('\0', buf[6]);
687 ASSERT_EQ('A', buf[7]);
688 ASSERT_EQ('A', buf[8]);
689 ASSERT_EQ('A', buf[9]);
690}
691
692TEST(TEST_NAME, strncat2) {
693 char buf[10];
694 memset(buf, 'A', sizeof(buf));
695 buf[0] = 'a';
696 buf[1] = '\0';
697 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
698 ASSERT_EQ(buf, res);
699 ASSERT_EQ('a', buf[0]);
700 ASSERT_EQ('0', buf[1]);
701 ASSERT_EQ('1', buf[2]);
702 ASSERT_EQ('2', buf[3]);
703 ASSERT_EQ('3', buf[4]);
704 ASSERT_EQ('4', buf[5]);
705 ASSERT_EQ('\0', buf[6]);
706 ASSERT_EQ('A', buf[7]);
707 ASSERT_EQ('A', buf[8]);
708 ASSERT_EQ('A', buf[9]);
709}
710
711TEST(TEST_NAME, strncat3) {
712 char buf[10];
713 memset(buf, 'A', sizeof(buf));
714 buf[0] = '\0';
715 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
716 ASSERT_EQ(buf, res);
717 ASSERT_EQ('0', buf[0]);
718 ASSERT_EQ('1', buf[1]);
719 ASSERT_EQ('2', buf[2]);
720 ASSERT_EQ('3', buf[3]);
721 ASSERT_EQ('4', buf[4]);
722 ASSERT_EQ('\0', buf[5]);
723 ASSERT_EQ('A', buf[6]);
724 ASSERT_EQ('A', buf[7]);
725 ASSERT_EQ('A', buf[8]);
726 ASSERT_EQ('A', buf[9]);
727}
728
729TEST(TEST_NAME, strncat4) {
730 char buf[10];
731 memset(buf, 'A', sizeof(buf));
732 buf[9] = '\0';
733 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
734 ASSERT_EQ(buf, res);
735 ASSERT_EQ('A', buf[0]);
736 ASSERT_EQ('A', buf[1]);
737 ASSERT_EQ('A', buf[2]);
738 ASSERT_EQ('A', buf[3]);
739 ASSERT_EQ('A', buf[4]);
740 ASSERT_EQ('A', buf[5]);
741 ASSERT_EQ('A', buf[6]);
742 ASSERT_EQ('A', buf[7]);
743 ASSERT_EQ('A', buf[8]);
744 ASSERT_EQ('\0', buf[9]);
745}
746
747TEST(TEST_NAME, strncat5) {
748 char buf[10];
749 memset(buf, 'A', sizeof(buf));
750 buf[0] = 'a';
751 buf[1] = '\0';
752 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
753 ASSERT_EQ(buf, res);
754 ASSERT_EQ('a', buf[0]);
755 ASSERT_EQ('0', buf[1]);
756 ASSERT_EQ('1', buf[2]);
757 ASSERT_EQ('2', buf[3]);
758 ASSERT_EQ('3', buf[4]);
759 ASSERT_EQ('4', buf[5]);
760 ASSERT_EQ('5', buf[6]);
761 ASSERT_EQ('6', buf[7]);
762 ASSERT_EQ('7', buf[8]);
763 ASSERT_EQ('\0', buf[9]);
764}
765
766TEST(TEST_NAME, strncat6) {
767 char buf[10];
768 memset(buf, 'A', sizeof(buf));
769 buf[0] = 'a';
770 buf[1] = '\0';
771 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
772 ASSERT_EQ(buf, res);
773 ASSERT_EQ('a', buf[0]);
774 ASSERT_EQ('0', buf[1]);
775 ASSERT_EQ('1', buf[2]);
776 ASSERT_EQ('2', buf[3]);
777 ASSERT_EQ('3', buf[4]);
778 ASSERT_EQ('4', buf[5]);
779 ASSERT_EQ('5', buf[6]);
780 ASSERT_EQ('6', buf[7]);
781 ASSERT_EQ('7', buf[8]);
782 ASSERT_EQ('\0', buf[9]);
783}
784
785
786TEST(TEST_NAME, strcat) {
787 char buf[10];
788 memset(buf, 'A', sizeof(buf));
789 buf[0] = 'a';
790 buf[1] = '\0';
791 char* res = __strcat_chk(buf, "01234", sizeof(buf));
792 ASSERT_EQ(buf, res);
793 ASSERT_EQ('a', buf[0]);
794 ASSERT_EQ('0', buf[1]);
795 ASSERT_EQ('1', buf[2]);
796 ASSERT_EQ('2', buf[3]);
797 ASSERT_EQ('3', buf[4]);
798 ASSERT_EQ('4', buf[5]);
799 ASSERT_EQ('\0', buf[6]);
800 ASSERT_EQ('A', buf[7]);
801 ASSERT_EQ('A', buf[8]);
802 ASSERT_EQ('A', buf[9]);
803}
804
805TEST(TEST_NAME, strcat2) {
806 char buf[10];
807 memset(buf, 'A', sizeof(buf));
808 buf[0] = 'a';
809 buf[1] = '\0';
810 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
811 ASSERT_EQ(buf, res);
812 ASSERT_EQ('a', buf[0]);
813 ASSERT_EQ('0', buf[1]);
814 ASSERT_EQ('1', buf[2]);
815 ASSERT_EQ('2', buf[3]);
816 ASSERT_EQ('3', buf[4]);
817 ASSERT_EQ('4', buf[5]);
818 ASSERT_EQ('5', buf[6]);
819 ASSERT_EQ('6', buf[7]);
820 ASSERT_EQ('7', buf[8]);
821 ASSERT_EQ('\0', buf[9]);
822}
Nick Kralevich93501d32013-08-28 10:47:43 -0700823
Christopher Ferris950a58e2014-04-04 14:38:18 -0700824TEST(TEST_NAME, stpncpy) {
825 char src[10];
826 char dst[10];
827 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
828 stpncpy(dst, src, sizeof(dst));
829 ASSERT_EQ('0', dst[0]);
830 ASSERT_EQ('1', dst[1]);
831 ASSERT_EQ('2', dst[2]);
832 ASSERT_EQ('3', dst[3]);
833 ASSERT_EQ('4', dst[4]);
834 ASSERT_EQ('5', dst[5]);
835 ASSERT_EQ('6', dst[6]);
836 ASSERT_EQ('7', dst[7]);
837 ASSERT_EQ('8', dst[8]);
838 ASSERT_EQ('9', dst[9]);
839}
840
841TEST(TEST_NAME, stpncpy2) {
842 char src[10];
843 char dst[15];
844 memcpy(src, "012345678\0", sizeof(src));
845 stpncpy(dst, src, sizeof(dst));
846 ASSERT_EQ('0', dst[0]);
847 ASSERT_EQ('1', dst[1]);
848 ASSERT_EQ('2', dst[2]);
849 ASSERT_EQ('3', dst[3]);
850 ASSERT_EQ('4', dst[4]);
851 ASSERT_EQ('5', dst[5]);
852 ASSERT_EQ('6', dst[6]);
853 ASSERT_EQ('7', dst[7]);
854 ASSERT_EQ('8', dst[8]);
855 ASSERT_EQ('\0', dst[9]);
856 ASSERT_EQ('\0', dst[10]);
857 ASSERT_EQ('\0', dst[11]);
858 ASSERT_EQ('\0', dst[12]);
859 ASSERT_EQ('\0', dst[13]);
860 ASSERT_EQ('\0', dst[14]);
861}
862
Nick Kralevich93501d32013-08-28 10:47:43 -0700863TEST(TEST_NAME, strncpy) {
864 char src[10];
865 char dst[10];
866 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
867 strncpy(dst, src, sizeof(dst));
868 ASSERT_EQ('0', dst[0]);
869 ASSERT_EQ('1', dst[1]);
870 ASSERT_EQ('2', dst[2]);
871 ASSERT_EQ('3', dst[3]);
872 ASSERT_EQ('4', dst[4]);
873 ASSERT_EQ('5', dst[5]);
874 ASSERT_EQ('6', dst[6]);
875 ASSERT_EQ('7', dst[7]);
876 ASSERT_EQ('8', dst[8]);
877 ASSERT_EQ('9', dst[9]);
878}
879
880TEST(TEST_NAME, strncpy2) {
881 char src[10];
882 char dst[15];
883 memcpy(src, "012345678\0", sizeof(src));
884 strncpy(dst, src, sizeof(dst));
885 ASSERT_EQ('0', dst[0]);
886 ASSERT_EQ('1', dst[1]);
887 ASSERT_EQ('2', dst[2]);
888 ASSERT_EQ('3', dst[3]);
889 ASSERT_EQ('4', dst[4]);
890 ASSERT_EQ('5', dst[5]);
891 ASSERT_EQ('6', dst[6]);
892 ASSERT_EQ('7', dst[7]);
893 ASSERT_EQ('8', dst[8]);
894 ASSERT_EQ('\0', dst[9]);
895 ASSERT_EQ('\0', dst[10]);
896 ASSERT_EQ('\0', dst[11]);
897 ASSERT_EQ('\0', dst[12]);
898 ASSERT_EQ('\0', dst[13]);
899 ASSERT_EQ('\0', dst[14]);
900}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700901
902TEST(TEST_NAME, strcat_chk_max_int_size) {
903 char buf[10];
904 memset(buf, 'A', sizeof(buf));
905 buf[0] = 'a';
906 buf[1] = '\0';
907 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
908 ASSERT_EQ(buf, res);
909 ASSERT_EQ('a', buf[0]);
910 ASSERT_EQ('0', buf[1]);
911 ASSERT_EQ('1', buf[2]);
912 ASSERT_EQ('2', buf[3]);
913 ASSERT_EQ('3', buf[4]);
914 ASSERT_EQ('4', buf[5]);
915 ASSERT_EQ('5', buf[6]);
916 ASSERT_EQ('6', buf[7]);
917 ASSERT_EQ('7', buf[8]);
918 ASSERT_EQ('\0', buf[9]);
919}
920
George Burgess IV849c0b92019-06-10 16:22:09 -0700921TEST(TEST_NAME, mempcpy_chk) {
922 const char input_str[] = "abcdefg";
923 size_t input_str_size = strlen(input_str) + 1;
924
925 char buf1[10] = {};
926 char buf2[10] = {};
927
928 __builtin_mempcpy(buf1, input_str, input_str_size);
929 __builtin___mempcpy_chk(buf2, input_str, input_str_size, __bos0(buf2));
930
931 ASSERT_EQ(memcmp(buf1, buf2, sizeof(buf2)), 0);
932
933 void *builtin_ptr = __builtin_mempcpy(buf1, input_str, input_str_size);
934 void *fortify_ptr = __builtin___mempcpy_chk(buf1, input_str, input_str_size, __bos0(buf2));
935
936 ASSERT_EQ(builtin_ptr, fortify_ptr);
937}
938
Christopher Ferris950a58e2014-04-04 14:38:18 -0700939extern "C" char* __stpcpy_chk(char*, const char*, size_t);
940
941TEST(TEST_NAME, stpcpy_chk_max_int_size) {
942 char buf[10];
943 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
944 ASSERT_EQ(buf + strlen("012345678"), res);
945 ASSERT_STREQ("012345678", buf);
946}
947
Christopher Ferris16e185c2013-09-10 16:56:34 -0700948extern "C" char* __strcpy_chk(char*, const char*, size_t);
949
950TEST(TEST_NAME, strcpy_chk_max_int_size) {
951 char buf[10];
952 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
953 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700954 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700955}
956
957extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
958
959TEST(TEST_NAME, memcpy_chk_max_int_size) {
960 char buf[10];
961 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
962 ASSERT_EQ((void*)buf, res);
963 ASSERT_EQ('0', buf[0]);
964 ASSERT_EQ('1', buf[1]);
965 ASSERT_EQ('2', buf[2]);
966 ASSERT_EQ('3', buf[3]);
967 ASSERT_EQ('4', buf[4]);
968 ASSERT_EQ('5', buf[5]);
969 ASSERT_EQ('6', buf[6]);
970 ASSERT_EQ('7', buf[7]);
971 ASSERT_EQ('8', buf[8]);
972 ASSERT_EQ('\0', buf[9]);
973}
Stephen Hines6e380722013-10-11 00:45:24 -0700974
975// Verify that macro expansion is done properly for sprintf/snprintf (which
976// are defined as macros in stdio.h under clang).
977#define CONTENTS "macro expansion"
978#define BUF_AND_SIZE(A) A, sizeof(A)
979#define BUF_AND_CONTENTS(A) A, CONTENTS
980#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
981TEST(TEST_NAME, s_n_printf_macro_expansion) {
982 char buf[BUFSIZ];
983 snprintf(BUF_AND_SIZE(buf), CONTENTS);
984 EXPECT_STREQ(CONTENTS, buf);
985
986 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
987 EXPECT_STREQ(CONTENTS, buf);
988
989 sprintf(BUF_AND_CONTENTS(buf));
990 EXPECT_STREQ(CONTENTS, buf);
991}
Elliott Hughes4674e382015-02-02 09:15:19 -0800992
993TEST_F(DEATHTEST, poll_fortified) {
994 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
995 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800996 // Set timeout to zero to prevent waiting in poll when fortify test fails.
997 ASSERT_FORTIFY(poll(buf, fd_count, 0));
Elliott Hughes4674e382015-02-02 09:15:19 -0800998}
999
1000TEST_F(DEATHTEST, ppoll_fortified) {
1001 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1002 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -08001003 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
1004 timespec timeout;
1005 timeout.tv_sec = timeout.tv_nsec = 0;
Elliott Hughesb83bf142018-03-22 11:01:25 -07001006 ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, nullptr));
1007}
1008
1009TEST_F(DEATHTEST, ppoll64_fortified) {
1010#if __BIONIC__ // glibc doesn't have ppoll64.
1011 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1012 pollfd buf[1] = {{0, POLLIN, 0}};
1013 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
1014 timespec timeout;
1015 timeout.tv_sec = timeout.tv_nsec = 0;
1016 ASSERT_FORTIFY(ppoll64(buf, fd_count, &timeout, nullptr));
1017#endif
Elliott Hughes4674e382015-02-02 09:15:19 -08001018}
Elliott Hughesb115aef2017-08-04 09:34:19 -07001019
1020TEST_F(DEATHTEST, open_O_CREAT_without_mode_fortified) {
1021 int flags = O_CREAT; // Fool the compiler.
1022 ASSERT_FORTIFY(open("", flags));
1023}
1024
1025TEST_F(DEATHTEST, open_O_TMPFILE_without_mode_fortified) {
1026#if __BIONIC__ // Our glibc is too old for O_TMPFILE.
1027 int flags = O_TMPFILE; // Fool the compiler.
1028 ASSERT_FORTIFY(open("", flags));
1029#endif
1030}