blob: b6f6661069e9251c16e55ca706106345f9b7d76b [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 Kralevich80541922013-05-01 14:55:33 -070049#if __BIONIC__
Nick Kralevich13476de2013-06-03 10:58:06 -070050// zero sized target with "\0" source (should fail)
51TEST(Fortify2_DeathTest, strcpy_fortified2) {
52 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
53 foo myfoo;
54 char* src = strdup("");
55 ASSERT_EXIT(strcpy(myfoo.empty, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070056 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070057 free(src);
58}
59
60// zero sized target with longer source (should fail)
61TEST(Fortify2_DeathTest, strcpy2_fortified2) {
62 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
63 foo myfoo;
64 char* src = strdup("1");
65 ASSERT_EXIT(strcpy(myfoo.empty, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070066 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070067 free(src);
68}
69
70// one byte target with longer source (should fail)
71TEST(Fortify2_DeathTest, strcpy3_fortified2) {
72 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
73 foo myfoo;
74 char* src = strdup("12");
75 ASSERT_EXIT(strcpy(myfoo.one, src),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070076 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070077 free(src);
78}
79
Nick Kralevich4f40e512013-04-19 16:54:22 -070080TEST(Fortify2_DeathTest, strchr_fortified2) {
81 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
82 foo myfoo;
83 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
84 myfoo.b[0] = '\0';
85 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070086 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich4f40e512013-04-19 16:54:22 -070087}
88
Nick Kralevich277226b2013-05-01 15:05:01 -070089TEST(Fortify2_DeathTest, strrchr_fortified2) {
Nick Kralevich80541922013-05-01 14:55:33 -070090 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
91 foo myfoo;
92 memcpy(myfoo.a, "0123456789", 10);
93 memcpy(myfoo.b, "01234", 6);
94 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
Nick Kralevichfd0325b2013-06-11 15:45:23 -070095 testing::KilledBySignal(SIGABRT), "");
Nick Kralevich80541922013-05-01 14:55:33 -070096}
Nick Kralevich8bafa742013-06-20 12:17:44 -070097
98TEST(Fortify2_DeathTest, strlcpy_fortified2) {
99 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
100 foo myfoo;
101 strcpy(myfoo.a, "01");
102 size_t n = strlen(myfoo.a);
103 ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
104 testing::KilledBySignal(SIGABRT), "");
105}
106
Nick Kralevich80541922013-05-01 14:55:33 -0700107#endif
108
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700109TEST(Fortify2_DeathTest, strncat_fortified2) {
110 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
111 foo myfoo;
112 size_t n = atoi("10"); // avoid compiler optimizations
113 strncpy(myfoo.a, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700114 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700115}
116
117TEST(Fortify2_DeathTest, strncat2_fortified2) {
118 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
119 foo myfoo;
120 myfoo.a[0] = '\0';
121 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700122 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700123}
124
Nick Kralevichcf870192013-05-30 16:48:53 -0700125TEST(Fortify2_DeathTest, strncat3_fortified2) {
126 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
127 foo myfoo;
128 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
129 myfoo.b[0] = '\0';
130 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700131 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700132}
133
134TEST(Fortify2_DeathTest, strcat_fortified2) {
135 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
136 char src[11];
137 strcpy(src, "0123456789");
138 foo myfoo;
139 myfoo.a[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700140 ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700141}
142
143TEST(Fortify2_DeathTest, strcat2_fortified2) {
144 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
145 foo myfoo;
146 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
147 myfoo.b[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700148 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700149}
150
Nick Kralevich78d6d982013-04-29 16:29:37 -0700151/***********************************************************/
152/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
153/***********************************************************/
154
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700155#if __BIONIC__
Nick Kralevich13476de2013-06-03 10:58:06 -0700156// multibyte target where we over fill (should fail)
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700157TEST(Fortify2_DeathTest, strcpy_fortified) {
158 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
159 char buf[10];
160 char *orig = strdup("0123456789");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700161 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700162 free(orig);
163}
164
Nick Kralevich13476de2013-06-03 10:58:06 -0700165// zero sized target with "\0" source (should fail)
166TEST(Fortify2_DeathTest, strcpy2_fortified) {
167 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
168 char buf[0];
169 char *orig = strdup("");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700170 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700171 free(orig);
172}
173
174// zero sized target with longer source (should fail)
175TEST(Fortify2_DeathTest, strcpy3_fortified) {
176 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
177 char buf[0];
178 char *orig = strdup("1");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700179 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700180 free(orig);
181}
182
183// one byte target with longer source (should fail)
184TEST(Fortify2_DeathTest, strcpy4_fortified) {
185 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
186 char buf[1];
187 char *orig = strdup("12");
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700188 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -0700189 free(orig);
190}
191
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700192TEST(Fortify2_DeathTest, strlen_fortified) {
193 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
194 char buf[10];
195 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700196 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700197}
198
199TEST(Fortify2_DeathTest, strchr_fortified) {
200 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
201 char buf[10];
202 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700203 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700204}
205
206TEST(Fortify2_DeathTest, strrchr_fortified) {
207 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
208 char buf[10];
209 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700210 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700211}
Nick Kralevich8bafa742013-06-20 12:17:44 -0700212
213TEST(Fortify2_DeathTest, strlcpy_fortified) {
214 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
215 char bufa[15];
216 char bufb[10];
217 strcpy(bufa, "01234567890123");
218 size_t n = strlen(bufa);
219 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
220}
221
Nick Kralevich1aae9bd2013-04-29 14:07:06 -0700222#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -0700223
224TEST(Fortify2_DeathTest, sprintf_fortified) {
225 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
226 char buf[10];
227 char source_buf[15];
228 memcpy(source_buf, "12345678901234", 15);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700229 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -0700230}
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700231
232TEST(Fortify2_DeathTest, strncat_fortified) {
233 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
234 char buf[10];
235 size_t n = atoi("10"); // avoid compiler optimizations
236 strncpy(buf, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700237 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700238}
239
240TEST(Fortify2_DeathTest, strncat2_fortified) {
241 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
242 char buf[10];
243 buf[0] = '\0';
244 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700245 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700246}
Nick Kralevichcf870192013-05-30 16:48:53 -0700247
248TEST(Fortify2_DeathTest, strcat_fortified) {
249 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
250 char src[11];
251 strcpy(src, "0123456789");
252 char buf[10];
253 buf[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700254 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700255}
Nick Kralevich16d1af12013-06-17 14:49:19 -0700256
257TEST(Fortify2_DeathTest, memmove_fortified) {
258 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
259 char buf[20];
260 strcpy(buf, "0123456789");
261 size_t n = atoi("10");
262 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
263}
264
265TEST(Fortify2_DeathTest, memcpy_fortified) {
266 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
267 char bufa[10];
268 char bufb[10];
269 strcpy(bufa, "012345678");
270 size_t n = atoi("11");
271 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
272}
273
274TEST(Fortify2_DeathTest, strncpy_fortified) {
275 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
276 char bufa[15];
277 char bufb[10];
278 strcpy(bufa, "01234567890123");
279 size_t n = strlen(bufa);
280 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
281}