blob: b8751bbefa5d04bc5fb9acf7d3b8d637e608d37a [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}
Nick Kralevich8bafa742013-06-20 12:17:44 -070083
84TEST(Fortify1_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 Kralevich1aae9bd2013-04-29 14:07:06 -070093#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -070094
95TEST(Fortify1_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);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700100 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich78d6d982013-04-29 16:29:37 -0700101}
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700102
103TEST(Fortify1_DeathTest, strncat_fortified) {
104 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105 char buf[10];
106 size_t n = atoi("10"); // avoid compiler optimizations
107 strncpy(buf, "012345678", n);
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700108 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700109}
110
111TEST(Fortify1_DeathTest, strncat2_fortified) {
112 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
113 char buf[10];
114 buf[0] = '\0';
115 size_t n = atoi("10"); // avoid compiler optimizations
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700116 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700117}
Nick Kralevichcf870192013-05-30 16:48:53 -0700118
119TEST(Fortify1_DeathTest, strcat_fortified) {
120 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
121 char src[11];
122 strcpy(src, "0123456789");
123 char buf[10];
124 buf[0] = '\0';
Nick Kralevichfd0325b2013-06-11 15:45:23 -0700125 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
Nick Kralevichcf870192013-05-30 16:48:53 -0700126}
127
Nick Kralevich16d1af12013-06-17 14:49:19 -0700128TEST(Fortify1_DeathTest, memmove_fortified) {
129 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
130 char buf[20];
131 strcpy(buf, "0123456789");
132 size_t n = atoi("10");
133 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
134}
135
136TEST(Fortify1_DeathTest, memcpy_fortified) {
137 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
138 char bufa[10];
139 char bufb[10];
140 strcpy(bufa, "012345678");
141 size_t n = atoi("11");
142 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
143}
144
145TEST(Fortify1_DeathTest, strncpy_fortified) {
146 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
147 char bufa[15];
148 char bufb[10];
149 strcpy(bufa, "01234567890123");
150 size_t n = strlen(bufa);
151 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
152}
153
Nick Kralevichcf870192013-05-30 16:48:53 -0700154extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
155extern "C" char* __strcat_chk(char*, const char*, size_t);
156
157TEST(Fortify1, strncat) {
158 char buf[10];
159 memset(buf, 'A', sizeof(buf));
160 buf[0] = 'a';
161 buf[1] = '\0';
162 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
163 ASSERT_EQ(buf, res);
164 ASSERT_EQ('a', buf[0]);
165 ASSERT_EQ('0', buf[1]);
166 ASSERT_EQ('1', buf[2]);
167 ASSERT_EQ('2', buf[3]);
168 ASSERT_EQ('3', buf[4]);
169 ASSERT_EQ('4', buf[5]);
170 ASSERT_EQ('\0', buf[6]);
171 ASSERT_EQ('A', buf[7]);
172 ASSERT_EQ('A', buf[8]);
173 ASSERT_EQ('A', buf[9]);
174}
175
176TEST(Fortify1, strncat2) {
177 char buf[10];
178 memset(buf, 'A', sizeof(buf));
179 buf[0] = 'a';
180 buf[1] = '\0';
181 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
182 ASSERT_EQ(buf, res);
183 ASSERT_EQ('a', buf[0]);
184 ASSERT_EQ('0', buf[1]);
185 ASSERT_EQ('1', buf[2]);
186 ASSERT_EQ('2', buf[3]);
187 ASSERT_EQ('3', buf[4]);
188 ASSERT_EQ('4', buf[5]);
189 ASSERT_EQ('\0', buf[6]);
190 ASSERT_EQ('A', buf[7]);
191 ASSERT_EQ('A', buf[8]);
192 ASSERT_EQ('A', buf[9]);
193}
194
195TEST(Fortify1, strncat3) {
196 char buf[10];
197 memset(buf, 'A', sizeof(buf));
198 buf[0] = '\0';
199 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
200 ASSERT_EQ(buf, res);
201 ASSERT_EQ('0', buf[0]);
202 ASSERT_EQ('1', buf[1]);
203 ASSERT_EQ('2', buf[2]);
204 ASSERT_EQ('3', buf[3]);
205 ASSERT_EQ('4', buf[4]);
206 ASSERT_EQ('\0', buf[5]);
207 ASSERT_EQ('A', buf[6]);
208 ASSERT_EQ('A', buf[7]);
209 ASSERT_EQ('A', buf[8]);
210 ASSERT_EQ('A', buf[9]);
211}
212
213TEST(Fortify1, strncat4) {
214 char buf[10];
215 memset(buf, 'A', sizeof(buf));
216 buf[9] = '\0';
217 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
218 ASSERT_EQ(buf, res);
219 ASSERT_EQ('A', buf[0]);
220 ASSERT_EQ('A', buf[1]);
221 ASSERT_EQ('A', buf[2]);
222 ASSERT_EQ('A', buf[3]);
223 ASSERT_EQ('A', buf[4]);
224 ASSERT_EQ('A', buf[5]);
225 ASSERT_EQ('A', buf[6]);
226 ASSERT_EQ('A', buf[7]);
227 ASSERT_EQ('A', buf[8]);
228 ASSERT_EQ('\0', buf[9]);
229}
230
231TEST(Fortify1, strncat5) {
232 char buf[10];
233 memset(buf, 'A', sizeof(buf));
234 buf[0] = 'a';
235 buf[1] = '\0';
236 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
237 ASSERT_EQ(buf, res);
238 ASSERT_EQ('a', buf[0]);
239 ASSERT_EQ('0', buf[1]);
240 ASSERT_EQ('1', buf[2]);
241 ASSERT_EQ('2', buf[3]);
242 ASSERT_EQ('3', buf[4]);
243 ASSERT_EQ('4', buf[5]);
244 ASSERT_EQ('5', buf[6]);
245 ASSERT_EQ('6', buf[7]);
246 ASSERT_EQ('7', buf[8]);
247 ASSERT_EQ('\0', buf[9]);
248}
249
250TEST(Fortify1, strncat6) {
251 char buf[10];
252 memset(buf, 'A', sizeof(buf));
253 buf[0] = 'a';
254 buf[1] = '\0';
255 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
256 ASSERT_EQ(buf, res);
257 ASSERT_EQ('a', buf[0]);
258 ASSERT_EQ('0', buf[1]);
259 ASSERT_EQ('1', buf[2]);
260 ASSERT_EQ('2', buf[3]);
261 ASSERT_EQ('3', buf[4]);
262 ASSERT_EQ('4', buf[5]);
263 ASSERT_EQ('5', buf[6]);
264 ASSERT_EQ('6', buf[7]);
265 ASSERT_EQ('7', buf[8]);
266 ASSERT_EQ('\0', buf[9]);
267}
268
269
270TEST(Fortify1, strcat) {
271 char buf[10];
272 memset(buf, 'A', sizeof(buf));
273 buf[0] = 'a';
274 buf[1] = '\0';
275 char* res = __strcat_chk(buf, "01234", sizeof(buf));
276 ASSERT_EQ(buf, res);
277 ASSERT_EQ('a', buf[0]);
278 ASSERT_EQ('0', buf[1]);
279 ASSERT_EQ('1', buf[2]);
280 ASSERT_EQ('2', buf[3]);
281 ASSERT_EQ('3', buf[4]);
282 ASSERT_EQ('4', buf[5]);
283 ASSERT_EQ('\0', buf[6]);
284 ASSERT_EQ('A', buf[7]);
285 ASSERT_EQ('A', buf[8]);
286 ASSERT_EQ('A', buf[9]);
287}
288
289TEST(Fortify1, strcat2) {
290 char buf[10];
291 memset(buf, 'A', sizeof(buf));
292 buf[0] = 'a';
293 buf[1] = '\0';
294 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
295 ASSERT_EQ(buf, res);
296 ASSERT_EQ('a', buf[0]);
297 ASSERT_EQ('0', buf[1]);
298 ASSERT_EQ('1', buf[2]);
299 ASSERT_EQ('2', buf[3]);
300 ASSERT_EQ('3', buf[4]);
301 ASSERT_EQ('4', buf[5]);
302 ASSERT_EQ('5', buf[6]);
303 ASSERT_EQ('6', buf[7]);
304 ASSERT_EQ('7', buf[8]);
305 ASSERT_EQ('\0', buf[9]);
306}