blob: 9f5b8b56c9c3e2a48e9575a18a54f6a2344c4443 [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 1
19
20#include <gtest/gtest.h>
21#include <string.h>
22
23#if __BIONIC__
24// We have to say "DeathTest" here so gtest knows to run this test (which exits)
25// in its own process.
26
27// multibyte target where we over fill (should fail)
28TEST(Fortify1_Clang_DeathTest, strcpy_fortified) {
29 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
30 char buf[10];
31 char *orig = strdup("0123456789");
32 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
33 free(orig);
34}
35
36// zero sized target with "\0" source (should fail)
37TEST(Fortify1_Clang_DeathTest, strcpy2_fortified) {
38 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
39 char buf[0];
40 char *orig = strdup("");
41 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
42 free(orig);
43}
44
45// zero sized target with longer source (should fail)
46TEST(Fortify1_Clang_DeathTest, strcpy3_fortified) {
47 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
48 char buf[0];
49 char *orig = strdup("1");
50 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
51 free(orig);
52}
53
54// one byte target with longer source (should fail)
55TEST(Fortify1_Clang_DeathTest, strcpy4_fortified) {
56 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
57 char buf[1];
58 char *orig = strdup("12");
59 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
60 free(orig);
61}
62
63TEST(Fortify1_Clang_DeathTest, strlen_fortified) {
64 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
65 char buf[10];
66 memcpy(buf, "0123456789", sizeof(buf));
67 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
68}
69
70TEST(Fortify1_Clang_DeathTest, strchr_fortified) {
71 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
72 char buf[10];
73 memcpy(buf, "0123456789", sizeof(buf));
74 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
75}
76
77TEST(Fortify1_Clang_DeathTest, strrchr_fortified) {
78 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
79 char buf[10];
80 memcpy(buf, "0123456789", sizeof(buf));
81 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
82}
Nick Kralevich8bafa742013-06-20 12:17:44 -070083
84TEST(Fortify1_Clang_DeathTest, strlcpy_fortified) {
85 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
86 char bufa[15];
87 char bufb[10];
88 strcpy(bufa, "01234567890123");
89 size_t n = strlen(bufa);
90 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
91}
92
Nick Kralevich16d1af12013-06-17 14:49:19 -070093#endif
94
Nick Kralevichc6eb9852013-06-24 11:44:00 -070095TEST(Fortify1_Clang_DeathTest, sprintf_fortified) {
96 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
97 char buf[10];
98 char source_buf[15];
99 memcpy(source_buf, "12345678901234", 15);
100 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
101}
102
103TEST(Fortify1_Clang_DeathTest, sprintf2_fortified) {
104 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105 char buf[5];
106 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
107}
108
Nick Kralevich16d1af12013-06-17 14:49:19 -0700109TEST(Fortify1_Clang_DeathTest, strncat_fortified) {
110 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
111 char buf[10];
112 size_t n = atoi("10"); // avoid compiler optimizations
113 strncpy(buf, "012345678", n);
114 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
115}
116
117TEST(Fortify1_Clang_DeathTest, strncat2_fortified) {
118 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
119 char buf[10];
120 buf[0] = '\0';
121 size_t n = atoi("10"); // avoid compiler optimizations
122 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
123}
124
125TEST(Fortify1_Clang_DeathTest, strcat_fortified) {
126 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
127 char src[11];
128 strcpy(src, "0123456789");
129 char buf[10];
130 buf[0] = '\0';
131 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
132}
133
134TEST(Fortify1_Clang_DeathTest, memmove_fortified) {
135 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
136 char buf[20];
137 strcpy(buf, "0123456789");
138 size_t n = atoi("10");
139 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
140}
141
142TEST(Fortify1_Clang_DeathTest, memcpy_fortified) {
143 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
144 char bufa[10];
145 char bufb[10];
146 strcpy(bufa, "012345678");
147 size_t n = atoi("11");
148 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
149}
150
151TEST(Fortify1_Clang_DeathTest, strncpy_fortified) {
152 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
153 char bufa[15];
154 char bufb[10];
155 strcpy(bufa, "01234567890123");
156 size_t n = strlen(bufa);
157 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
158}
159
Nick Kralevich621b19d2013-06-25 10:02:35 -0700160TEST(Fortify1_Clang_DeathTest, snprintf_fortified) {
161 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
162 char bufa[15];
163 char bufb[10];
164 strcpy(bufa, "0123456789");
165 size_t n = strlen(bufa) + 1;
166 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
167}
168
Nick Kralevich16d1af12013-06-17 14:49:19 -0700169extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
170extern "C" char* __strcat_chk(char*, const char*, size_t);
171
172TEST(Fortify1_Clang, strncat) {
173 char buf[10];
174 memset(buf, 'A', sizeof(buf));
175 buf[0] = 'a';
176 buf[1] = '\0';
177 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
178 ASSERT_EQ(buf, res);
179 ASSERT_EQ('a', buf[0]);
180 ASSERT_EQ('0', buf[1]);
181 ASSERT_EQ('1', buf[2]);
182 ASSERT_EQ('2', buf[3]);
183 ASSERT_EQ('3', buf[4]);
184 ASSERT_EQ('4', buf[5]);
185 ASSERT_EQ('\0', buf[6]);
186 ASSERT_EQ('A', buf[7]);
187 ASSERT_EQ('A', buf[8]);
188 ASSERT_EQ('A', buf[9]);
189}
190
191TEST(Fortify1_Clang, strncat2) {
192 char buf[10];
193 memset(buf, 'A', sizeof(buf));
194 buf[0] = 'a';
195 buf[1] = '\0';
196 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
197 ASSERT_EQ(buf, res);
198 ASSERT_EQ('a', buf[0]);
199 ASSERT_EQ('0', buf[1]);
200 ASSERT_EQ('1', buf[2]);
201 ASSERT_EQ('2', buf[3]);
202 ASSERT_EQ('3', buf[4]);
203 ASSERT_EQ('4', buf[5]);
204 ASSERT_EQ('\0', buf[6]);
205 ASSERT_EQ('A', buf[7]);
206 ASSERT_EQ('A', buf[8]);
207 ASSERT_EQ('A', buf[9]);
208}
209
210TEST(Fortify1_Clang, strncat3) {
211 char buf[10];
212 memset(buf, 'A', sizeof(buf));
213 buf[0] = '\0';
214 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
215 ASSERT_EQ(buf, res);
216 ASSERT_EQ('0', buf[0]);
217 ASSERT_EQ('1', buf[1]);
218 ASSERT_EQ('2', buf[2]);
219 ASSERT_EQ('3', buf[3]);
220 ASSERT_EQ('4', buf[4]);
221 ASSERT_EQ('\0', buf[5]);
222 ASSERT_EQ('A', buf[6]);
223 ASSERT_EQ('A', buf[7]);
224 ASSERT_EQ('A', buf[8]);
225 ASSERT_EQ('A', buf[9]);
226}
227
228TEST(Fortify1_Clang, strncat4) {
229 char buf[10];
230 memset(buf, 'A', sizeof(buf));
231 buf[9] = '\0';
232 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
233 ASSERT_EQ(buf, res);
234 ASSERT_EQ('A', buf[0]);
235 ASSERT_EQ('A', buf[1]);
236 ASSERT_EQ('A', buf[2]);
237 ASSERT_EQ('A', buf[3]);
238 ASSERT_EQ('A', buf[4]);
239 ASSERT_EQ('A', buf[5]);
240 ASSERT_EQ('A', buf[6]);
241 ASSERT_EQ('A', buf[7]);
242 ASSERT_EQ('A', buf[8]);
243 ASSERT_EQ('\0', buf[9]);
244}
245
246TEST(Fortify1_Clang, strncat5) {
247 char buf[10];
248 memset(buf, 'A', sizeof(buf));
249 buf[0] = 'a';
250 buf[1] = '\0';
251 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
252 ASSERT_EQ(buf, res);
253 ASSERT_EQ('a', buf[0]);
254 ASSERT_EQ('0', buf[1]);
255 ASSERT_EQ('1', buf[2]);
256 ASSERT_EQ('2', buf[3]);
257 ASSERT_EQ('3', buf[4]);
258 ASSERT_EQ('4', buf[5]);
259 ASSERT_EQ('5', buf[6]);
260 ASSERT_EQ('6', buf[7]);
261 ASSERT_EQ('7', buf[8]);
262 ASSERT_EQ('\0', buf[9]);
263}
264
265TEST(Fortify1_Clang, strncat6) {
266 char buf[10];
267 memset(buf, 'A', sizeof(buf));
268 buf[0] = 'a';
269 buf[1] = '\0';
270 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
271 ASSERT_EQ(buf, res);
272 ASSERT_EQ('a', buf[0]);
273 ASSERT_EQ('0', buf[1]);
274 ASSERT_EQ('1', buf[2]);
275 ASSERT_EQ('2', buf[3]);
276 ASSERT_EQ('3', buf[4]);
277 ASSERT_EQ('4', buf[5]);
278 ASSERT_EQ('5', buf[6]);
279 ASSERT_EQ('6', buf[7]);
280 ASSERT_EQ('7', buf[8]);
281 ASSERT_EQ('\0', buf[9]);
282}
283
284
285TEST(Fortify1_Clang, strcat) {
286 char buf[10];
287 memset(buf, 'A', sizeof(buf));
288 buf[0] = 'a';
289 buf[1] = '\0';
290 char* res = __strcat_chk(buf, "01234", sizeof(buf));
291 ASSERT_EQ(buf, res);
292 ASSERT_EQ('a', buf[0]);
293 ASSERT_EQ('0', buf[1]);
294 ASSERT_EQ('1', buf[2]);
295 ASSERT_EQ('2', buf[3]);
296 ASSERT_EQ('3', buf[4]);
297 ASSERT_EQ('4', buf[5]);
298 ASSERT_EQ('\0', buf[6]);
299 ASSERT_EQ('A', buf[7]);
300 ASSERT_EQ('A', buf[8]);
301 ASSERT_EQ('A', buf[9]);
302}
303
304TEST(Fortify1_Clang, strcat2) {
305 char buf[10];
306 memset(buf, 'A', sizeof(buf));
307 buf[0] = 'a';
308 buf[1] = '\0';
309 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
310 ASSERT_EQ(buf, res);
311 ASSERT_EQ('a', buf[0]);
312 ASSERT_EQ('0', buf[1]);
313 ASSERT_EQ('1', buf[2]);
314 ASSERT_EQ('2', buf[3]);
315 ASSERT_EQ('3', buf[4]);
316 ASSERT_EQ('4', buf[5]);
317 ASSERT_EQ('5', buf[6]);
318 ASSERT_EQ('6', buf[7]);
319 ASSERT_EQ('7', buf[8]);
320 ASSERT_EQ('\0', buf[9]);
321}
322
323__BIONIC_FORTIFY_INLINE
324size_t test_fortify_inline(char* buf) {
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700325 return __bos(buf);
Nick Kralevich16d1af12013-06-17 14:49:19 -0700326}
327
328TEST(Fortify1_Clang, fortify_inline) {
329 char buf[1024];
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700330 // no-op. Prints nothing. Needed to prevent the compiler
331 // from optimizing out buf.
332 buf[0] = '\0';
333 printf("%s", buf);
334 ASSERT_EQ(sizeof(buf), test_fortify_inline(buf));
Nick Kralevich16d1af12013-06-17 14:49:19 -0700335}