blob: 83f57e8f468444090bbda73f5be7e47019fac99c [file] [log] [blame]
Nick Kralevich16d1af12013-06-17 14:49:19 -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#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 2
19
20#include <gtest/gtest.h>
21#include <string.h>
Nick Kralevichc8ae8bd2013-06-27 08:58:14 -070022#include <stdarg.h>
Nick Kralevich16d1af12013-06-17 14:49:19 -070023
24struct foo {
25 char empty[0];
26 char one[1];
27 char a[10];
28 char b[10];
29};
30
31// We have to say "DeathTest" here so gtest knows to run this test (which exits)
32// in its own process.
33TEST(Fortify2_Clang_DeathTest, strncat3_fortified2) {
34 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
35 foo myfoo;
36 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
37 myfoo.b[0] = '\0';
38 size_t n = atoi("10"); // avoid compiler optimizations
39 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
40}
41
42TEST(Fortify2_Clang_DeathTest, strcat2_fortified2) {
43 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
44 foo myfoo;
45 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
46 myfoo.b[0] = '\0';
47 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
48}
49
50/*****************************************************************/
51/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test_clang.cpp */
52/*****************************************************************/
53
54#if __BIONIC__
55// multibyte target where we over fill (should fail)
56TEST(Fortify2_Clang_DeathTest, strcpy_fortified) {
57 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
58 char buf[10];
59 char *orig = strdup("0123456789");
60 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
61 free(orig);
62}
63
64// zero sized target with "\0" source (should fail)
65TEST(Fortify2_Clang_DeathTest, strcpy2_fortified) {
66 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
67 char buf[0];
68 char *orig = strdup("");
69 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
70 free(orig);
71}
72
73// zero sized target with longer source (should fail)
74TEST(Fortify2_Clang_DeathTest, strcpy3_fortified) {
75 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
76 char buf[0];
77 char *orig = strdup("1");
78 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
79 free(orig);
80}
81
82// one byte target with longer source (should fail)
83TEST(Fortify2_Clang_DeathTest, strcpy4_fortified) {
84 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
85 char buf[1];
86 char *orig = strdup("12");
87 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
88 free(orig);
89}
90
91TEST(Fortify2_Clang_DeathTest, strlen_fortified) {
92 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
93 char buf[10];
94 memcpy(buf, "0123456789", sizeof(buf));
95 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
96}
97
98TEST(Fortify2_Clang_DeathTest, strchr_fortified) {
99 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
100 char buf[10];
101 memcpy(buf, "0123456789", sizeof(buf));
102 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
103}
104
105TEST(Fortify2_Clang_DeathTest, strrchr_fortified) {
106 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
107 char buf[10];
108 memcpy(buf, "0123456789", sizeof(buf));
109 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
110}
111#endif
112
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700113TEST(Fortify2_Clang_DeathTest, sprintf_fortified) {
114 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
115 char buf[10];
116 char source_buf[15];
117 memcpy(source_buf, "12345678901234", 15);
118 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
119}
120
121TEST(Fortify2_Clang_DeathTest, sprintf2_fortified) {
122 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
123 char buf[5];
124 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
125}
126
Nick Kralevichc8ae8bd2013-06-27 08:58:14 -0700127static int vsprintf_helper(const char *fmt, ...) {
128 char buf[10];
129 va_list va;
130 int result;
131
132 va_start(va, fmt);
133 result = vsprintf(buf, fmt, va); // should crash here
134 va_end(va);
135 return result;
136}
137
138TEST(Fortify2_Clang_DeathTest, vsprintf_fortified) {
139 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
140 ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
141}
142
143TEST(Fortify2_Clang_DeathTest, vsprintf2_fortified) {
144 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
145 ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
146}
147
148static int vsnprintf_helper(const char *fmt, ...) {
149 char buf[10];
150 va_list va;
151 int result;
152 size_t size = atoi("11");
153
154 va_start(va, fmt);
155 result = vsnprintf(buf, size, fmt, va); // should crash here
156 va_end(va);
157 return result;
158}
159
160TEST(Fortify2_Clang_DeathTest, vsnprintf_fortified) {
161 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
162 ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
163}
164
165TEST(Fortify2_Clang_DeathTest, vsnprintf2_fortified) {
166 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
167 ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
168}
169
Nick Kralevich16d1af12013-06-17 14:49:19 -0700170TEST(Fortify2_Clang_DeathTest, strncat_fortified) {
171 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
172 char buf[10];
173 size_t n = atoi("10"); // avoid compiler optimizations
174 strncpy(buf, "012345678", n);
175 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
176}
177
178TEST(Fortify2_Clang_DeathTest, strncat2_fortified) {
179 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
180 char buf[10];
181 buf[0] = '\0';
182 size_t n = atoi("10"); // avoid compiler optimizations
183 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
184}
185
186TEST(Fortify2_Clang_DeathTest, strcat_fortified) {
187 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
188 char src[11];
189 strcpy(src, "0123456789");
190 char buf[10];
191 buf[0] = '\0';
192 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
193}
194
195TEST(Fortify2_Clang_DeathTest, memmove_fortified) {
196 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
197 char buf[20];
198 strcpy(buf, "0123456789");
199 size_t n = atoi("10");
200 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
201}
202
203TEST(Fortify2_Clang_DeathTest, memcpy_fortified) {
204 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
205 char bufa[10];
206 char bufb[10];
207 strcpy(bufa, "012345678");
208 size_t n = atoi("11");
209 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
210}
211
212TEST(Fortify2_Clang_DeathTest, strncpy_fortified) {
213 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
214 char bufa[15];
215 char bufb[10];
216 strcpy(bufa, "01234567890123");
217 size_t n = strlen(bufa);
218 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
219}
220
Nick Kralevich621b19d2013-06-25 10:02:35 -0700221TEST(Fortify2_Clang_DeathTest, snprintf_fortified) {
222 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
223 char bufa[15];
224 char bufb[10];
225 strcpy(bufa, "0123456789");
226 size_t n = strlen(bufa) + 1;
227 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
228}
229
Nick Kralevich16d1af12013-06-17 14:49:19 -0700230__BIONIC_FORTIFY_INLINE
231size_t test_fortify2_inline(char* buf) {
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700232 return __bos(buf);
Nick Kralevich16d1af12013-06-17 14:49:19 -0700233}
234
235TEST(Fortify2_Clang, fortify_inline) {
236 char buf[1024];
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700237 // no-op. Prints nothing. Needed to prevent the compiler
238 // from optimizing out buf.
239 buf[0] = '\0';
240 printf("%s", buf);
241 ASSERT_EQ(sizeof(buf), test_fortify2_inline(buf));
Nick Kralevich16d1af12013-06-17 14:49:19 -0700242}