blob: 1fa34cae872444df857f343ab007abd7954bfe33 [file] [log] [blame]
Nick Kralevich1aae9bd2013-04-29 14:07:06 -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 Kralevich1aae9bd2013-04-29 14:07:06 -070023
24struct foo {
Nick Kralevich13476de2013-06-03 10:58:06 -070025 char empty[0];
26 char one[1];
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070027 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.
Nick Kralevich78d6d982013-04-29 16:29:37 -070033TEST(Fortify2_DeathTest, strncpy_fortified2) {
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070034 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
35 foo myfoo;
36 int copy_amt = atoi("11");
37 ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070038 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070039}
40
Nick Kralevich78d6d982013-04-29 16:29:37 -070041TEST(Fortify2_DeathTest, sprintf_fortified2) {
42 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
43 foo myfoo;
44 char source_buf[15];
45 memcpy(source_buf, "12345678901234", 15);
46 ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070047 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -070048}
49
Nick Kralevichc6eb9852013-06-24 11:44:00 -070050TEST(Fortify2_DeathTest, sprintf2_fortified2) {
51 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
52 foo myfoo;
53 ASSERT_EXIT(sprintf(myfoo.a, "0123456789"),
54 testing::KilledBySignal(SIGABRT), "");
55}
56
Nick Kralevichc8ae8bd2013-06-27 08:58:14 -070057static int vsprintf_helper2(const char *fmt, ...) {
58 foo myfoo;
59 va_list va;
60 int result;
61
62 va_start(va, fmt);
63 result = vsprintf(myfoo.a, fmt, va); // should crash here
64 va_end(va);
65 return result;
66}
67
68TEST(Fortify2_DeathTest, vsprintf_fortified2) {
69 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
70 ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
71}
72
73TEST(Fortify2_DeathTest, vsprintf2_fortified2) {
74 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
75 ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
76}
77
78static int vsnprintf_helper2(const char *fmt, ...) {
79 foo myfoo;
80 va_list va;
81 int result;
82 size_t size = atoi("11");
83
84 va_start(va, fmt);
85 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
86 va_end(va);
87 return result;
88}
89
90TEST(Fortify2_DeathTest, vsnprintf_fortified2) {
91 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
92 ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
93}
94
95TEST(Fortify2_DeathTest, vsnprintf2_fortified2) {
96 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
97 ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
98}
99
Nick Kralevich80541922013-05-01 14:55:33 -0700100#if __BIONIC__
Nick Kralevich13476de2013-06-03 10:58:06 -0700101// zero sized target with "\0" source (should fail)
102TEST(Fortify2_DeathTest, strcpy_fortified2) {
103 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
104 foo myfoo;
105 char* src = strdup("");
106 ASSERT_EXIT(strcpy(myfoo.empty, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700107 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700108 free(src);
109}
110
111// zero sized target with longer source (should fail)
112TEST(Fortify2_DeathTest, strcpy2_fortified2) {
113 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
114 foo myfoo;
115 char* src = strdup("1");
116 ASSERT_EXIT(strcpy(myfoo.empty, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700117 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700118 free(src);
119}
120
121// one byte target with longer source (should fail)
122TEST(Fortify2_DeathTest, strcpy3_fortified2) {
123 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
124 foo myfoo;
125 char* src = strdup("12");
126 ASSERT_EXIT(strcpy(myfoo.one, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700127 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700128 free(src);
129}
130
Nick Kralevich4f40e512013-04-19 16:54:22 -0700131TEST(Fortify2_DeathTest, strchr_fortified2) {
132 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
133 foo myfoo;
134 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
135 myfoo.b[0] = '\0';
136 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700137 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich4f40e512013-04-19 16:54:22 -0700138}
139
Nick Kralevich277226b2013-05-01 15:05:01 -0700140TEST(Fortify2_DeathTest, strrchr_fortified2) {
Nick Kralevich80541922013-05-01 14:55:33 -0700141 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
142 foo myfoo;
143 memcpy(myfoo.a, "0123456789", 10);
144 memcpy(myfoo.b, "01234", 6);
145 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700146 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich80541922013-05-01 14:55:33 -0700147}
Nick Kralevich8bafa742013-06-20 12:17:44 -0700148
149TEST(Fortify2_DeathTest, strlcpy_fortified2) {
150 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
151 foo myfoo;
152 strcpy(myfoo.a, "01");
153 size_t n = strlen(myfoo.a);
154 ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
155 testing::KilledBySignal(SIGABRT), "");
156}
157
Nick Kralevich80541922013-05-01 14:55:33 -0700158#endif
159
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700160TEST(Fortify2_DeathTest, strncat_fortified2) {
161 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
162 foo myfoo;
163 size_t n = atoi("10"); // avoid compiler optimizations
164 strncpy(myfoo.a, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700165 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700166}
167
168TEST(Fortify2_DeathTest, strncat2_fortified2) {
169 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
170 foo myfoo;
171 myfoo.a[0] = '\0';
172 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700173 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700174}
175
Nick Kralevichcf870192013-05-30 16:48:53 -0700176TEST(Fortify2_DeathTest, strncat3_fortified2) {
177 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
178 foo myfoo;
179 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
180 myfoo.b[0] = '\0';
181 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700182 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700183}
184
185TEST(Fortify2_DeathTest, strcat_fortified2) {
186 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
187 char src[11];
188 strcpy(src, "0123456789");
189 foo myfoo;
190 myfoo.a[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700191 ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700192}
193
194TEST(Fortify2_DeathTest, strcat2_fortified2) {
195 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
196 foo myfoo;
197 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
198 myfoo.b[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700199 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700200}
201
Nick Kralevich621b19d2013-06-25 10:02:35 -0700202TEST(Fortify2_DeathTest, snprintf_fortified2) {
203 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
204 foo myfoo;
205 strcpy(myfoo.a, "012345678");
206 size_t n = strlen(myfoo.a) + 2;
207 ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), "");
208}
209
Nick Kralevich78d6d982013-04-29 16:29:37 -0700210/***********************************************************/
211/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
212/***********************************************************/
213
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700214#if __BIONIC__
Nick Kralevich13476de2013-06-03 10:58:06 -0700215// multibyte target where we over fill (should fail)
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700216TEST(Fortify2_DeathTest, strcpy_fortified) {
217 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
218 char buf[10];
219 char *orig = strdup("0123456789");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700220 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700221 free(orig);
222}
223
Nick Kralevich13476de2013-06-03 10:58:06 -0700224// zero sized target with "\0" source (should fail)
225TEST(Fortify2_DeathTest, strcpy2_fortified) {
226 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
227 char buf[0];
228 char *orig = strdup("");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700229 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700230 free(orig);
231}
232
233// zero sized target with longer source (should fail)
234TEST(Fortify2_DeathTest, strcpy3_fortified) {
235 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
236 char buf[0];
237 char *orig = strdup("1");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700238 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700239 free(orig);
240}
241
242// one byte target with longer source (should fail)
243TEST(Fortify2_DeathTest, strcpy4_fortified) {
244 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
245 char buf[1];
246 char *orig = strdup("12");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700247 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700248 free(orig);
249}
250
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700251TEST(Fortify2_DeathTest, strlen_fortified) {
252 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
253 char buf[10];
254 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700255 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700256}
257
258TEST(Fortify2_DeathTest, strchr_fortified) {
259 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
260 char buf[10];
261 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700262 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700263}
264
265TEST(Fortify2_DeathTest, strrchr_fortified) {
266 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
267 char buf[10];
268 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700269 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700270}
Nick Kralevich8bafa742013-06-20 12:17:44 -0700271
272TEST(Fortify2_DeathTest, strlcpy_fortified) {
273 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
274 char bufa[15];
275 char bufb[10];
276 strcpy(bufa, "01234567890123");
277 size_t n = strlen(bufa);
278 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
279}
280
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700281#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -0700282
283TEST(Fortify2_DeathTest, sprintf_fortified) {
284 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
285 char buf[10];
286 char source_buf[15];
287 memcpy(source_buf, "12345678901234", 15);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700288 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -0700289}
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700290
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700291TEST(Fortify2_DeathTest, sprintf2_fortified) {
292 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
293 char buf[5];
294 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
295}
296
Nick Kralevichc8ae8bd2013-06-27 08:58:14 -0700297static int vsprintf_helper(const char *fmt, ...) {
298 char buf[10];
299 va_list va;
300 int result;
301
302 va_start(va, fmt);
303 result = vsprintf(buf, fmt, va); // should crash here
304 va_end(va);
305 return result;
306}
307
308TEST(Fortify2_DeathTest, vsprintf_fortified) {
309 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
310 ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
311}
312
313TEST(Fortify2_DeathTest, vsprintf2_fortified) {
314 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
315 ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
316}
317
318static int vsnprintf_helper(const char *fmt, ...) {
319 char buf[10];
320 va_list va;
321 int result;
322 size_t size = atoi("11");
323
324 va_start(va, fmt);
325 result = vsnprintf(buf, size, fmt, va); // should crash here
326 va_end(va);
327 return result;
328}
329
330TEST(Fortify2_DeathTest, vsnprintf_fortified) {
331 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
332 ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
333}
334
335TEST(Fortify2_DeathTest, vsnprintf2_fortified) {
336 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
337 ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
338}
339
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700340TEST(Fortify2_DeathTest, strncat_fortified) {
341 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
342 char buf[10];
343 size_t n = atoi("10"); // avoid compiler optimizations
344 strncpy(buf, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700345 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700346}
347
348TEST(Fortify2_DeathTest, strncat2_fortified) {
349 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
350 char buf[10];
351 buf[0] = '\0';
352 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700353 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700354}
Nick Kralevichcf870192013-05-30 16:48:53 -0700355
356TEST(Fortify2_DeathTest, strcat_fortified) {
357 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
358 char src[11];
359 strcpy(src, "0123456789");
360 char buf[10];
361 buf[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700362 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700363}
Nick Kralevich16d1af12013-06-17 14:49:19 -0700364
365TEST(Fortify2_DeathTest, memmove_fortified) {
366 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
367 char buf[20];
368 strcpy(buf, "0123456789");
369 size_t n = atoi("10");
370 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
371}
372
373TEST(Fortify2_DeathTest, memcpy_fortified) {
374 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
375 char bufa[10];
376 char bufb[10];
377 strcpy(bufa, "012345678");
378 size_t n = atoi("11");
379 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
380}
381
382TEST(Fortify2_DeathTest, strncpy_fortified) {
383 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
384 char bufa[15];
385 char bufb[10];
386 strcpy(bufa, "01234567890123");
387 size_t n = strlen(bufa);
388 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
389}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700390
391TEST(Fortify2_DeathTest, snprintf_fortified) {
392 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
393 char bufa[15];
394 char bufb[10];
395 strcpy(bufa, "0123456789");
396 size_t n = strlen(bufa) + 1;
397 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
398}