blob: be59a1824b3e1fb0bc9463a4016bbee04f23c13b [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 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.
Nick Kralevich13476de2013-06-03 10:58:06 -070026
27// multibyte target where we over fill (should fail)
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070028TEST(Fortify1_DeathTest, strcpy_fortified) {
29 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
30 char buf[10];
31 char *orig = strdup("0123456789");
Nick Kralevichfd0325b2013-06-11 15:45:23 -070032 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070033 free(orig);
34}
35
Nick Kralevich13476de2013-06-03 10:58:06 -070036// zero sized target with "\0" source (should fail)
37TEST(Fortify1_DeathTest, strcpy2_fortified) {
38 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
39 char buf[0];
40 char *orig = strdup("");
Nick Kralevichfd0325b2013-06-11 15:45:23 -070041 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070042 free(orig);
43}
44
45// zero sized target with longer source (should fail)
46TEST(Fortify1_DeathTest, strcpy3_fortified) {
47 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
48 char buf[0];
49 char *orig = strdup("1");
Nick Kralevichfd0325b2013-06-11 15:45:23 -070050 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070051 free(orig);
52}
53
54// one byte target with longer source (should fail)
55TEST(Fortify1_DeathTest, strcpy4_fortified) {
56 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
57 char buf[1];
58 char *orig = strdup("12");
Nick Kralevichfd0325b2013-06-11 15:45:23 -070059 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich13476de2013-06-03 10:58:06 -070060 free(orig);
61}
62
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070063TEST(Fortify1_DeathTest, strlen_fortified) {
64 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
65 char buf[10];
66 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -070067 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070068}
69
70TEST(Fortify1_DeathTest, strchr_fortified) {
71 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
72 char buf[10];
73 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -070074 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070075}
76
77TEST(Fortify1_DeathTest, strrchr_fortified) {
78 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
79 char buf[10];
80 memcpy(buf, "0123456789", sizeof(buf));
Nick Kralevichfd0325b2013-06-11 15:45:23 -070081 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070082}
83#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -070084
85TEST(Fortify1_DeathTest, sprintf_fortified) {
86 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
87 char buf[10];
88 char source_buf[15];
89 memcpy(source_buf, "12345678901234", 15);
Nick Kralevichfd0325b2013-06-11 15:45:23 -070090 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -070091}
Nick Kralevich8cc145e2013-05-30 13:21:14 -070092
93TEST(Fortify1_DeathTest, strncat_fortified) {
94 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
95 char buf[10];
96 size_t n = atoi("10"); // avoid compiler optimizations
97 strncpy(buf, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -070098 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -070099}
100
101TEST(Fortify1_DeathTest, strncat2_fortified) {
102 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
103 char buf[10];
104 buf[0] = '\0';
105 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700106 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700107}
Nick Kralevichcf870192013-05-30 16:48:53 -0700108
109TEST(Fortify1_DeathTest, strcat_fortified) {
110 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
111 char src[11];
112 strcpy(src, "0123456789");
113 char buf[10];
114 buf[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700115 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700116}
117
Nick Kralevich16d1af12013-06-17 14:49:19 -0700118TEST(Fortify1_DeathTest, memmove_fortified) {
119 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
120 char buf[20];
121 strcpy(buf, "0123456789");
122 size_t n = atoi("10");
123 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
124}
125
126TEST(Fortify1_DeathTest, memcpy_fortified) {
127 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
128 char bufa[10];
129 char bufb[10];
130 strcpy(bufa, "012345678");
131 size_t n = atoi("11");
132 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
133}
134
135TEST(Fortify1_DeathTest, strncpy_fortified) {
136 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
137 char bufa[15];
138 char bufb[10];
139 strcpy(bufa, "01234567890123");
140 size_t n = strlen(bufa);
141 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
142}
143
Nick Kralevichcf870192013-05-30 16:48:53 -0700144extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
145extern "C" char* __strcat_chk(char*, const char*, size_t);
146
147TEST(Fortify1, strncat) {
148 char buf[10];
149 memset(buf, 'A', sizeof(buf));
150 buf[0] = 'a';
151 buf[1] = '\0';
152 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
153 ASSERT_EQ(buf, res);
154 ASSERT_EQ('a', buf[0]);
155 ASSERT_EQ('0', buf[1]);
156 ASSERT_EQ('1', buf[2]);
157 ASSERT_EQ('2', buf[3]);
158 ASSERT_EQ('3', buf[4]);
159 ASSERT_EQ('4', buf[5]);
160 ASSERT_EQ('\0', buf[6]);
161 ASSERT_EQ('A', buf[7]);
162 ASSERT_EQ('A', buf[8]);
163 ASSERT_EQ('A', buf[9]);
164}
165
166TEST(Fortify1, strncat2) {
167 char buf[10];
168 memset(buf, 'A', sizeof(buf));
169 buf[0] = 'a';
170 buf[1] = '\0';
171 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
172 ASSERT_EQ(buf, res);
173 ASSERT_EQ('a', buf[0]);
174 ASSERT_EQ('0', buf[1]);
175 ASSERT_EQ('1', buf[2]);
176 ASSERT_EQ('2', buf[3]);
177 ASSERT_EQ('3', buf[4]);
178 ASSERT_EQ('4', buf[5]);
179 ASSERT_EQ('\0', buf[6]);
180 ASSERT_EQ('A', buf[7]);
181 ASSERT_EQ('A', buf[8]);
182 ASSERT_EQ('A', buf[9]);
183}
184
185TEST(Fortify1, strncat3) {
186 char buf[10];
187 memset(buf, 'A', sizeof(buf));
188 buf[0] = '\0';
189 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
190 ASSERT_EQ(buf, res);
191 ASSERT_EQ('0', buf[0]);
192 ASSERT_EQ('1', buf[1]);
193 ASSERT_EQ('2', buf[2]);
194 ASSERT_EQ('3', buf[3]);
195 ASSERT_EQ('4', buf[4]);
196 ASSERT_EQ('\0', buf[5]);
197 ASSERT_EQ('A', buf[6]);
198 ASSERT_EQ('A', buf[7]);
199 ASSERT_EQ('A', buf[8]);
200 ASSERT_EQ('A', buf[9]);
201}
202
203TEST(Fortify1, strncat4) {
204 char buf[10];
205 memset(buf, 'A', sizeof(buf));
206 buf[9] = '\0';
207 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
208 ASSERT_EQ(buf, res);
209 ASSERT_EQ('A', buf[0]);
210 ASSERT_EQ('A', buf[1]);
211 ASSERT_EQ('A', buf[2]);
212 ASSERT_EQ('A', buf[3]);
213 ASSERT_EQ('A', buf[4]);
214 ASSERT_EQ('A', buf[5]);
215 ASSERT_EQ('A', buf[6]);
216 ASSERT_EQ('A', buf[7]);
217 ASSERT_EQ('A', buf[8]);
218 ASSERT_EQ('\0', buf[9]);
219}
220
221TEST(Fortify1, strncat5) {
222 char buf[10];
223 memset(buf, 'A', sizeof(buf));
224 buf[0] = 'a';
225 buf[1] = '\0';
226 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
227 ASSERT_EQ(buf, res);
228 ASSERT_EQ('a', buf[0]);
229 ASSERT_EQ('0', buf[1]);
230 ASSERT_EQ('1', buf[2]);
231 ASSERT_EQ('2', buf[3]);
232 ASSERT_EQ('3', buf[4]);
233 ASSERT_EQ('4', buf[5]);
234 ASSERT_EQ('5', buf[6]);
235 ASSERT_EQ('6', buf[7]);
236 ASSERT_EQ('7', buf[8]);
237 ASSERT_EQ('\0', buf[9]);
238}
239
240TEST(Fortify1, strncat6) {
241 char buf[10];
242 memset(buf, 'A', sizeof(buf));
243 buf[0] = 'a';
244 buf[1] = '\0';
245 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
246 ASSERT_EQ(buf, res);
247 ASSERT_EQ('a', buf[0]);
248 ASSERT_EQ('0', buf[1]);
249 ASSERT_EQ('1', buf[2]);
250 ASSERT_EQ('2', buf[3]);
251 ASSERT_EQ('3', buf[4]);
252 ASSERT_EQ('4', buf[5]);
253 ASSERT_EQ('5', buf[6]);
254 ASSERT_EQ('6', buf[7]);
255 ASSERT_EQ('7', buf[8]);
256 ASSERT_EQ('\0', buf[9]);
257}
258
259
260TEST(Fortify1, strcat) {
261 char buf[10];
262 memset(buf, 'A', sizeof(buf));
263 buf[0] = 'a';
264 buf[1] = '\0';
265 char* res = __strcat_chk(buf, "01234", sizeof(buf));
266 ASSERT_EQ(buf, res);
267 ASSERT_EQ('a', buf[0]);
268 ASSERT_EQ('0', buf[1]);
269 ASSERT_EQ('1', buf[2]);
270 ASSERT_EQ('2', buf[3]);
271 ASSERT_EQ('3', buf[4]);
272 ASSERT_EQ('4', buf[5]);
273 ASSERT_EQ('\0', buf[6]);
274 ASSERT_EQ('A', buf[7]);
275 ASSERT_EQ('A', buf[8]);
276 ASSERT_EQ('A', buf[9]);
277}
278
279TEST(Fortify1, strcat2) {
280 char buf[10];
281 memset(buf, 'A', sizeof(buf));
282 buf[0] = 'a';
283 buf[1] = '\0';
284 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
285 ASSERT_EQ(buf, res);
286 ASSERT_EQ('a', buf[0]);
287 ASSERT_EQ('0', buf[1]);
288 ASSERT_EQ('1', buf[2]);
289 ASSERT_EQ('2', buf[3]);
290 ASSERT_EQ('3', buf[4]);
291 ASSERT_EQ('4', buf[5]);
292 ASSERT_EQ('5', buf[6]);
293 ASSERT_EQ('6', buf[7]);
294 ASSERT_EQ('7', buf[8]);
295 ASSERT_EQ('\0', buf[9]);
296}