blob: 9a136123669d1337276d35705fa51333d58d4bfc [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>
22
23struct foo {
Nick Kralevich13476de2013-06-03 10:58:06 -070024 char empty[0];
25 char one[1];
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070026 char a[10];
27 char b[10];
28};
29
30// We have to say "DeathTest" here so gtest knows to run this test (which exits)
31// in its own process.
Nick Kralevich78d6d982013-04-29 16:29:37 -070032TEST(Fortify2_DeathTest, strncpy_fortified2) {
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070033 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
34 foo myfoo;
35 int copy_amt = atoi("11");
36 ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070037 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070038}
39
Nick Kralevich78d6d982013-04-29 16:29:37 -070040TEST(Fortify2_DeathTest, sprintf_fortified2) {
41 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
42 foo myfoo;
43 char source_buf[15];
44 memcpy(source_buf, "12345678901234", 15);
45 ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070046 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -070047}
48
Nick Kralevichc6eb9852013-06-24 11:44:00 -070049TEST(Fortify2_DeathTest, sprintf2_fortified2) {
50 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
51 foo myfoo;
52 ASSERT_EXIT(sprintf(myfoo.a, "0123456789"),
53 testing::KilledBySignal(SIGABRT), "");
54}
55
Nick Kralevich80541922013-05-01 14:55:33 -070056#if __BIONIC__
Nick Kralevich13476de2013-06-03 10:58:06 -070057// zero sized target with "\0" source (should fail)
58TEST(Fortify2_DeathTest, strcpy_fortified2) {
59 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
60 foo myfoo;
61 char* src = strdup("");
62 ASSERT_EXIT(strcpy(myfoo.empty, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070063 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070064 free(src);
65}
66
67// zero sized target with longer source (should fail)
68TEST(Fortify2_DeathTest, strcpy2_fortified2) {
69 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
70 foo myfoo;
71 char* src = strdup("1");
72 ASSERT_EXIT(strcpy(myfoo.empty, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070073 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070074 free(src);
75}
76
77// one byte target with longer source (should fail)
78TEST(Fortify2_DeathTest, strcpy3_fortified2) {
79 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
80 foo myfoo;
81 char* src = strdup("12");
82 ASSERT_EXIT(strcpy(myfoo.one, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070083 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070084 free(src);
85}
86
Nick Kralevich4f40e512013-04-19 16:54:22 -070087TEST(Fortify2_DeathTest, strchr_fortified2) {
88 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
89 foo myfoo;
90 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
91 myfoo.b[0] = '\0';
92 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070093 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich4f40e512013-04-19 16:54:22 -070094}
95
Nick Kralevich277226b2013-05-01 15:05:01 -070096TEST(Fortify2_DeathTest, strrchr_fortified2) {
Nick Kralevich80541922013-05-01 14:55:33 -070097 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
98 foo myfoo;
99 memcpy(myfoo.a, "0123456789", 10);
100 memcpy(myfoo.b, "01234", 6);
101 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700102 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich80541922013-05-01 14:55:33 -0700103}
Nick Kralevich8bafa742013-06-20 12:17:44 -0700104
105TEST(Fortify2_DeathTest, strlcpy_fortified2) {
106 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
107 foo myfoo;
108 strcpy(myfoo.a, "01");
109 size_t n = strlen(myfoo.a);
110 ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
111 testing::KilledBySignal(SIGABRT), "");
112}
113
Nick Kralevich80541922013-05-01 14:55:33 -0700114#endif
115
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700116TEST(Fortify2_DeathTest, strncat_fortified2) {
117 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
118 foo myfoo;
119 size_t n = atoi("10"); // avoid compiler optimizations
120 strncpy(myfoo.a, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700121 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700122}
123
124TEST(Fortify2_DeathTest, strncat2_fortified2) {
125 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
126 foo myfoo;
127 myfoo.a[0] = '\0';
128 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700129 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700130}
131
Nick Kralevichcf870192013-05-30 16:48:53 -0700132TEST(Fortify2_DeathTest, strncat3_fortified2) {
133 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
134 foo myfoo;
135 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
136 myfoo.b[0] = '\0';
137 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700138 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700139}
140
141TEST(Fortify2_DeathTest, strcat_fortified2) {
142 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
143 char src[11];
144 strcpy(src, "0123456789");
145 foo myfoo;
146 myfoo.a[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700147 ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700148}
149
150TEST(Fortify2_DeathTest, strcat2_fortified2) {
151 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
152 foo myfoo;
153 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
154 myfoo.b[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700155 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700156}
157
Nick Kralevich621b19d2013-06-25 10:02:35 -0700158TEST(Fortify2_DeathTest, snprintf_fortified2) {
159 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
160 foo myfoo;
161 strcpy(myfoo.a, "012345678");
162 size_t n = strlen(myfoo.a) + 2;
163 ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), "");
164}
165
Nick Kralevich78d6d982013-04-29 16:29:37 -0700166/***********************************************************/
167/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
168/***********************************************************/
169
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700170#if __BIONIC__
Nick Kralevich13476de2013-06-03 10:58:06 -0700171// multibyte target where we over fill (should fail)
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700172TEST(Fortify2_DeathTest, strcpy_fortified) {
173 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
174 char buf[10];
175 char *orig = strdup("0123456789");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700176 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700177 free(orig);
178}
179
Nick Kralevich13476de2013-06-03 10:58:06 -0700180// zero sized target with "\0" source (should fail)
181TEST(Fortify2_DeathTest, strcpy2_fortified) {
182 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
183 char buf[0];
184 char *orig = strdup("");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700185 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700186 free(orig);
187}
188
189// zero sized target with longer source (should fail)
190TEST(Fortify2_DeathTest, strcpy3_fortified) {
191 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
192 char buf[0];
193 char *orig = strdup("1");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700194 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700195 free(orig);
196}
197
198// one byte target with longer source (should fail)
199TEST(Fortify2_DeathTest, strcpy4_fortified) {
200 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
201 char buf[1];
202 char *orig = strdup("12");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700203 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700204 free(orig);
205}
206
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700207TEST(Fortify2_DeathTest, strlen_fortified) {
208 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
209 char buf[10];
210 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700211 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700212}
213
214TEST(Fortify2_DeathTest, strchr_fortified) {
215 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
216 char buf[10];
217 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700218 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700219}
220
221TEST(Fortify2_DeathTest, strrchr_fortified) {
222 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
223 char buf[10];
224 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700225 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700226}
Nick Kralevich8bafa742013-06-20 12:17:44 -0700227
228TEST(Fortify2_DeathTest, strlcpy_fortified) {
229 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
230 char bufa[15];
231 char bufb[10];
232 strcpy(bufa, "01234567890123");
233 size_t n = strlen(bufa);
234 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
235}
236
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700237#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -0700238
239TEST(Fortify2_DeathTest, sprintf_fortified) {
240 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
241 char buf[10];
242 char source_buf[15];
243 memcpy(source_buf, "12345678901234", 15);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700244 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -0700245}
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700246
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700247TEST(Fortify2_DeathTest, sprintf2_fortified) {
248 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
249 char buf[5];
250 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
251}
252
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700253TEST(Fortify2_DeathTest, strncat_fortified) {
254 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
255 char buf[10];
256 size_t n = atoi("10"); // avoid compiler optimizations
257 strncpy(buf, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700258 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700259}
260
261TEST(Fortify2_DeathTest, strncat2_fortified) {
262 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
263 char buf[10];
264 buf[0] = '\0';
265 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700266 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700267}
Nick Kralevichcf870192013-05-30 16:48:53 -0700268
269TEST(Fortify2_DeathTest, strcat_fortified) {
270 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
271 char src[11];
272 strcpy(src, "0123456789");
273 char buf[10];
274 buf[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700275 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700276}
Nick Kralevich16d1af12013-06-17 14:49:19 -0700277
278TEST(Fortify2_DeathTest, memmove_fortified) {
279 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
280 char buf[20];
281 strcpy(buf, "0123456789");
282 size_t n = atoi("10");
283 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
284}
285
286TEST(Fortify2_DeathTest, memcpy_fortified) {
287 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
288 char bufa[10];
289 char bufb[10];
290 strcpy(bufa, "012345678");
291 size_t n = atoi("11");
292 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
293}
294
295TEST(Fortify2_DeathTest, strncpy_fortified) {
296 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
297 char bufa[15];
298 char bufb[10];
299 strcpy(bufa, "01234567890123");
300 size_t n = strlen(bufa);
301 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
302}
Nick Kralevich621b19d2013-06-25 10:02:35 -0700303
304TEST(Fortify2_DeathTest, snprintf_fortified) {
305 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
306 char bufa[15];
307 char bufb[10];
308 strcpy(bufa, "0123456789");
309 size_t n = strlen(bufa) + 1;
310 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
311}